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