]>
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 | * | |
db137867 | 24 | */ |
c056dba2 | 25 | |
fe037171 EM |
26 | #include <librb_config.h> |
27 | #include <rb_lib.h> | |
db137867 | 28 | |
3202e249 VY |
29 | #ifndef _WIN32 |
30 | ||
31 | #include <sys/wait.h> | |
32 | ||
790e4dce AC |
33 | #ifdef HAVE_DLINFO |
34 | # include <link.h> | |
35 | # include <dlfcn.h> | |
36 | #endif | |
3202e249 VY |
37 | |
38 | #ifdef __APPLE__ | |
790e4dce | 39 | #include <mach-o/dyld.h> |
55abcbb2 | 40 | #include <crt_externs.h> |
3202e249 VY |
41 | #endif |
42 | ||
d71787ab AC |
43 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
44 | #include <sys/types.h> | |
45 | #include <sys/sysctl.h> | |
46 | #endif | |
47 | ||
790e4dce AC |
48 | #if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN) |
49 | #include <spawn.h> | |
50 | ||
3202e249 | 51 | #ifndef __APPLE__ |
db137867 | 52 | extern char **environ; |
3202e249 | 53 | #endif |
db137867 AC |
54 | pid_t |
55 | rb_spawn_process(const char *path, const char **argv) | |
56 | { | |
57 | pid_t pid; | |
58 | const void *arghack = argv; | |
3202e249 | 59 | char **myenviron; |
dcb22e07 | 60 | int error; |
db137867 AC |
61 | posix_spawnattr_t spattr; |
62 | posix_spawnattr_init(&spattr); | |
63 | #ifdef POSIX_SPAWN_USEVFORK | |
64 | posix_spawnattr_setflags(&spattr, POSIX_SPAWN_USEVFORK); | |
65 | #endif | |
3202e249 VY |
66 | #ifdef __APPLE__ |
67 | myenviron = *_NSGetEnviron(); /* apple needs to go fuck themselves for this */ | |
68 | #else | |
69 | myenviron = environ; | |
70 | #endif | |
dcb22e07 JT |
71 | error = posix_spawn(&pid, path, NULL, &spattr, arghack, myenviron); |
72 | posix_spawnattr_destroy(&spattr); | |
73 | if (error != 0) | |
db137867 | 74 | { |
dcb22e07 JT |
75 | errno = error; |
76 | pid = -1; | |
db137867 AC |
77 | } |
78 | return pid; | |
79 | } | |
80 | #else | |
81 | pid_t | |
82 | rb_spawn_process(const char *path, const char **argv) | |
83 | { | |
84 | pid_t pid; | |
85 | if(!(pid = vfork())) | |
86 | { | |
3202e249 VY |
87 | execv(path, (const void *)argv); /* make gcc shut up */ |
88 | _exit(1); /* if we're still here, we're screwed */ | |
db137867 | 89 | } |
3202e249 | 90 | return (pid); |
db137867 AC |
91 | } |
92 | #endif | |
93 | ||
94 | #ifndef HAVE_GETTIMEOFDAY | |
95 | int | |
96 | rb_gettimeofday(struct timeval *tv, void *tz) | |
97 | { | |
98 | if(tv == NULL) | |
99 | { | |
100 | errno = EFAULT; | |
101 | return -1; | |
102 | } | |
103 | tv->tv_usec = 0; | |
104 | if(time(&tv->tv_sec) == -1) | |
105 | return -1; | |
106 | return 0; | |
107 | } | |
108 | #else | |
109 | int | |
110 | rb_gettimeofday(struct timeval *tv, void *tz) | |
111 | { | |
3202e249 | 112 | return (gettimeofday(tv, tz)); |
db137867 AC |
113 | } |
114 | #endif | |
115 | ||
116 | void | |
117 | rb_sleep(unsigned int seconds, unsigned int useconds) | |
118 | { | |
119 | #ifdef HAVE_NANOSLEEP | |
120 | struct timespec tv; | |
121 | tv.tv_nsec = (useconds * 1000); | |
122 | tv.tv_sec = seconds; | |
123 | nanosleep(&tv, NULL); | |
3202e249 | 124 | #else |
db137867 AC |
125 | struct timeval tv; |
126 | tv.tv_sec = seconds; | |
127 | tv.tv_usec = useconds; | |
128 | select(0, NULL, NULL, NULL, &tv); | |
129 | #endif | |
130 | } | |
3202e249 VY |
131 | |
132 | /* this is to keep some linkers from bitching about exporting a non-existant symbol..bleh */ | |
133 | char * | |
134 | rb_strerror(int error) | |
135 | { | |
136 | return strerror(error); | |
137 | } | |
138 | ||
139 | int | |
140 | rb_kill(pid_t pid, int sig) | |
141 | { | |
142 | return kill(pid, sig); | |
143 | } | |
144 | ||
145 | int | |
146 | rb_setenv(const char *name, const char *value, int overwrite) | |
147 | { | |
148 | return setenv(name, value, overwrite); | |
149 | } | |
150 | ||
151 | pid_t | |
152 | rb_waitpid(pid_t pid, int *status, int options) | |
153 | { | |
154 | return waitpid(pid, status, options); | |
155 | } | |
156 | ||
157 | pid_t | |
158 | rb_getpid(void) | |
159 | { | |
160 | return getpid(); | |
161 | } | |
db137867 | 162 | |
790e4dce AC |
163 | const char * |
164 | rb_path_to_self(void) | |
165 | { | |
166 | static char path_buf[4096]; | |
d71787ab | 167 | size_t path_len = sizeof(path_buf); |
790e4dce AC |
168 | #if defined(HAVE_GETEXECNAME) |
169 | char *s = getexecname(); | |
170 | if (s == NULL) | |
171 | return NULL; | |
172 | realpath(s, path_buf); | |
173 | return path_buf; | |
87b20cbb | 174 | #elif defined(__linux__) |
d71787ab AC |
175 | if (readlink("/proc/self/exe", path_buf, path_len) != -1) |
176 | return path_buf; | |
177 | return NULL; | |
178 | #elif defined(__FreeBSD__) || defined(__DragonFly__) | |
179 | int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; | |
180 | if (sysctl(mib, 4, path_buf, &path_len, NULL, 0) == 0) | |
87b20cbb AC |
181 | return path_buf; |
182 | return NULL; | |
790e4dce AC |
183 | #elif defined(HAVE_DLINFO) |
184 | struct link_map *map = NULL; | |
185 | dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &map); | |
186 | if (map == NULL) | |
187 | return NULL; | |
188 | realpath(map->l_name, path_buf); | |
a20190d5 | 189 | return path_buf; |
790e4dce AC |
190 | #elif defined(__APPLE__) |
191 | char tmp_path[4096]; | |
192 | uint32_t pathlen = 4096; | |
193 | ||
194 | if (_NSGetExecutablePath(tmp_path, &pathlen) < 0) | |
195 | return NULL; | |
196 | ||
197 | realpath(tmp_path, path_buf); | |
198 | return path_buf; | |
199 | #endif | |
200 | } | |
db137867 | 201 | |
3202e249 | 202 | #endif /* !WIN32 */ |