]>
Commit | Line | Data |
---|---|---|
b57f37fb WP |
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 | * | |
b57f37fb WP |
24 | */ |
25 | #include <libratbox_config.h> | |
26 | #include <ratbox_lib.h> | |
27 | ||
94b4fbf9 VY |
28 | |
29 | #ifndef _WIN32 | |
30 | ||
31 | #include <sys/wait.h> | |
32 | ||
33 | ||
b57f37fb WP |
34 | #if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN) |
35 | #include <spawn.h> | |
94b4fbf9 VY |
36 | |
37 | #ifdef __APPLE__ | |
38 | #include <crt_externs.h> | |
39 | #endif | |
40 | ||
41 | #ifndef __APPLE__ | |
b57f37fb | 42 | extern char **environ; |
94b4fbf9 | 43 | #endif |
b57f37fb WP |
44 | pid_t |
45 | rb_spawn_process(const char *path, const char **argv) | |
46 | { | |
47 | pid_t pid; | |
48 | const void *arghack = argv; | |
94b4fbf9 | 49 | char **myenviron; |
8b4cf5c1 | 50 | int error; |
b57f37fb WP |
51 | posix_spawnattr_t spattr; |
52 | posix_spawnattr_init(&spattr); | |
53 | #ifdef POSIX_SPAWN_USEVFORK | |
54 | posix_spawnattr_setflags(&spattr, POSIX_SPAWN_USEVFORK); | |
55 | #endif | |
94b4fbf9 VY |
56 | #ifdef __APPLE__ |
57 | myenviron = *_NSGetEnviron(); /* apple needs to go fuck themselves for this */ | |
58 | #else | |
59 | myenviron = environ; | |
60 | #endif | |
8b4cf5c1 JT |
61 | error = posix_spawn(&pid, path, NULL, &spattr, arghack, myenviron); |
62 | posix_spawnattr_destroy(&spattr); | |
63 | if (error != 0) | |
b57f37fb | 64 | { |
8b4cf5c1 JT |
65 | errno = error; |
66 | pid = -1; | |
b57f37fb WP |
67 | } |
68 | return pid; | |
69 | } | |
70 | #else | |
71 | pid_t | |
72 | rb_spawn_process(const char *path, const char **argv) | |
73 | { | |
74 | pid_t pid; | |
75 | if(!(pid = vfork())) | |
76 | { | |
94b4fbf9 VY |
77 | execv(path, (const void *)argv); /* make gcc shut up */ |
78 | _exit(1); /* if we're still here, we're screwed */ | |
b57f37fb | 79 | } |
94b4fbf9 | 80 | return (pid); |
b57f37fb WP |
81 | } |
82 | #endif | |
83 | ||
84 | #ifndef HAVE_GETTIMEOFDAY | |
85 | int | |
86 | rb_gettimeofday(struct timeval *tv, void *tz) | |
87 | { | |
88 | if(tv == NULL) | |
89 | { | |
90 | errno = EFAULT; | |
91 | return -1; | |
92 | } | |
93 | tv->tv_usec = 0; | |
94 | if(time(&tv->tv_sec) == -1) | |
95 | return -1; | |
96 | return 0; | |
97 | } | |
98 | #else | |
99 | int | |
100 | rb_gettimeofday(struct timeval *tv, void *tz) | |
101 | { | |
94b4fbf9 | 102 | return (gettimeofday(tv, tz)); |
b57f37fb WP |
103 | } |
104 | #endif | |
105 | ||
106 | void | |
107 | rb_sleep(unsigned int seconds, unsigned int useconds) | |
108 | { | |
109 | #ifdef HAVE_NANOSLEEP | |
110 | struct timespec tv; | |
111 | tv.tv_nsec = (useconds * 1000); | |
112 | tv.tv_sec = seconds; | |
113 | nanosleep(&tv, NULL); | |
94b4fbf9 | 114 | #else |
b57f37fb WP |
115 | struct timeval tv; |
116 | tv.tv_sec = seconds; | |
117 | tv.tv_usec = useconds; | |
118 | select(0, NULL, NULL, NULL, &tv); | |
119 | #endif | |
120 | } | |
94b4fbf9 VY |
121 | |
122 | /* this is to keep some linkers from bitching about exporting a non-existant symbol..bleh */ | |
123 | char * | |
124 | rb_strerror(int error) | |
125 | { | |
126 | return strerror(error); | |
127 | } | |
128 | ||
129 | int | |
130 | rb_kill(pid_t pid, int sig) | |
131 | { | |
132 | return kill(pid, sig); | |
133 | } | |
134 | ||
135 | int | |
136 | rb_setenv(const char *name, const char *value, int overwrite) | |
137 | { | |
138 | return setenv(name, value, overwrite); | |
139 | } | |
140 | ||
141 | pid_t | |
142 | rb_waitpid(pid_t pid, int *status, int options) | |
143 | { | |
144 | return waitpid(pid, status, options); | |
145 | } | |
146 | ||
147 | pid_t | |
148 | rb_getpid(void) | |
149 | { | |
150 | return getpid(); | |
151 | } | |
b57f37fb WP |
152 | |
153 | ||
94b4fbf9 | 154 | #endif /* !WIN32 */ |