]>
Commit | Line | Data |
---|---|---|
db137867 AC |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * unix.c: various unix type functions | |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 2005 ircd-ratbox development team | |
7 | * Copyright (C) 2005 Aaron Sethman <androsyn@ratbox.org> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | |
22 | * USA | |
23 | * | |
3202e249 | 24 | * $Id: unix.c 26180 2008-11-11 00:00:12Z androsyn $ |
db137867 | 25 | */ |
fe037171 EM |
26 | #include <librb_config.h> |
27 | #include <rb_lib.h> | |
db137867 | 28 | |
3202e249 VY |
29 | |
30 | #ifndef _WIN32 | |
31 | ||
32 | #include <sys/wait.h> | |
33 | ||
34 | ||
db137867 AC |
35 | #if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN) |
36 | #include <spawn.h> | |
3202e249 VY |
37 | |
38 | #ifdef __APPLE__ | |
55abcbb2 | 39 | #include <crt_externs.h> |
3202e249 VY |
40 | #endif |
41 | ||
42 | #ifndef __APPLE__ | |
db137867 | 43 | extern char **environ; |
3202e249 | 44 | #endif |
db137867 AC |
45 | pid_t |
46 | rb_spawn_process(const char *path, const char **argv) | |
47 | { | |
48 | pid_t pid; | |
49 | const void *arghack = argv; | |
3202e249 | 50 | char **myenviron; |
dcb22e07 | 51 | int error; |
db137867 AC |
52 | posix_spawnattr_t spattr; |
53 | posix_spawnattr_init(&spattr); | |
54 | #ifdef POSIX_SPAWN_USEVFORK | |
55 | posix_spawnattr_setflags(&spattr, POSIX_SPAWN_USEVFORK); | |
56 | #endif | |
3202e249 VY |
57 | #ifdef __APPLE__ |
58 | myenviron = *_NSGetEnviron(); /* apple needs to go fuck themselves for this */ | |
59 | #else | |
60 | myenviron = environ; | |
61 | #endif | |
dcb22e07 JT |
62 | error = posix_spawn(&pid, path, NULL, &spattr, arghack, myenviron); |
63 | posix_spawnattr_destroy(&spattr); | |
64 | if (error != 0) | |
db137867 | 65 | { |
dcb22e07 JT |
66 | errno = error; |
67 | pid = -1; | |
db137867 AC |
68 | } |
69 | return pid; | |
70 | } | |
71 | #else | |
72 | pid_t | |
73 | rb_spawn_process(const char *path, const char **argv) | |
74 | { | |
75 | pid_t pid; | |
76 | if(!(pid = vfork())) | |
77 | { | |
3202e249 VY |
78 | execv(path, (const void *)argv); /* make gcc shut up */ |
79 | _exit(1); /* if we're still here, we're screwed */ | |
db137867 | 80 | } |
3202e249 | 81 | return (pid); |
db137867 AC |
82 | } |
83 | #endif | |
84 | ||
85 | #ifndef HAVE_GETTIMEOFDAY | |
86 | int | |
87 | rb_gettimeofday(struct timeval *tv, void *tz) | |
88 | { | |
89 | if(tv == NULL) | |
90 | { | |
91 | errno = EFAULT; | |
92 | return -1; | |
93 | } | |
94 | tv->tv_usec = 0; | |
95 | if(time(&tv->tv_sec) == -1) | |
96 | return -1; | |
97 | return 0; | |
98 | } | |
99 | #else | |
100 | int | |
101 | rb_gettimeofday(struct timeval *tv, void *tz) | |
102 | { | |
3202e249 | 103 | return (gettimeofday(tv, tz)); |
db137867 AC |
104 | } |
105 | #endif | |
106 | ||
107 | void | |
108 | rb_sleep(unsigned int seconds, unsigned int useconds) | |
109 | { | |
110 | #ifdef HAVE_NANOSLEEP | |
111 | struct timespec tv; | |
112 | tv.tv_nsec = (useconds * 1000); | |
113 | tv.tv_sec = seconds; | |
114 | nanosleep(&tv, NULL); | |
3202e249 | 115 | #else |
db137867 AC |
116 | struct timeval tv; |
117 | tv.tv_sec = seconds; | |
118 | tv.tv_usec = useconds; | |
119 | select(0, NULL, NULL, NULL, &tv); | |
120 | #endif | |
121 | } | |
3202e249 VY |
122 | |
123 | /* this is to keep some linkers from bitching about exporting a non-existant symbol..bleh */ | |
124 | char * | |
125 | rb_strerror(int error) | |
126 | { | |
127 | return strerror(error); | |
128 | } | |
129 | ||
130 | int | |
131 | rb_kill(pid_t pid, int sig) | |
132 | { | |
133 | return kill(pid, sig); | |
134 | } | |
135 | ||
136 | int | |
137 | rb_setenv(const char *name, const char *value, int overwrite) | |
138 | { | |
139 | return setenv(name, value, overwrite); | |
140 | } | |
141 | ||
142 | pid_t | |
143 | rb_waitpid(pid_t pid, int *status, int options) | |
144 | { | |
145 | return waitpid(pid, status, options); | |
146 | } | |
147 | ||
148 | pid_t | |
149 | rb_getpid(void) | |
150 | { | |
151 | return getpid(); | |
152 | } | |
db137867 AC |
153 | |
154 | ||
3202e249 | 155 | #endif /* !WIN32 */ |