]> jfr.im git - solanum.git/blob - librb/src/unix.c
Merge branch 'master' into authd-framework-2
[solanum.git] / librb / src / unix.c
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 *
24 * $Id: unix.c 26180 2008-11-11 00:00:12Z androsyn $
25 */
26 #include <librb_config.h>
27 #include <rb_lib.h>
28
29 #ifndef _WIN32
30
31 #include <sys/wait.h>
32
33 #ifdef HAVE_DLINFO
34 # include <link.h>
35 # include <dlfcn.h>
36 #endif
37
38 #ifdef __APPLE__
39 #include <mach-o/dyld.h>
40 #include <crt_externs.h>
41 #endif
42
43 #if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN)
44 #include <spawn.h>
45
46 #ifndef __APPLE__
47 extern char **environ;
48 #endif
49 pid_t
50 rb_spawn_process(const char *path, const char **argv)
51 {
52 pid_t pid;
53 const void *arghack = argv;
54 char **myenviron;
55 int error;
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
61 #ifdef __APPLE__
62 myenviron = *_NSGetEnviron(); /* apple needs to go fuck themselves for this */
63 #else
64 myenviron = environ;
65 #endif
66 error = posix_spawn(&pid, path, NULL, &spattr, arghack, myenviron);
67 posix_spawnattr_destroy(&spattr);
68 if (error != 0)
69 {
70 errno = error;
71 pid = -1;
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 {
82 execv(path, (const void *)argv); /* make gcc shut up */
83 _exit(1); /* if we're still here, we're screwed */
84 }
85 return (pid);
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 {
107 return (gettimeofday(tv, tz));
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);
119 #else
120 struct timeval tv;
121 tv.tv_sec = seconds;
122 tv.tv_usec = useconds;
123 select(0, NULL, NULL, NULL, &tv);
124 #endif
125 }
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 }
157
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;
168 #elif defined(__linux__)
169 if (readlink("/proc/self/exe", path_buf, sizeof path_buf) != -1)
170 return path_buf;
171 return NULL;
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);
178 #elif defined(__APPLE__)
179 char tmp_path[4096];
180 uint32_t pathlen = 4096;
181
182 if (_NSGetExecutablePath(tmp_path, &pathlen) < 0)
183 return NULL;
184
185 realpath(tmp_path, path_buf);
186 return path_buf;
187 #endif
188 }
189
190 #endif /* !WIN32 */