]> jfr.im git - solanum.git/blob - librb/src/unix.c
e89bf277f59bea5d26a078d72dfdfc5842c8f1ad
[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 */
25
26 #include <librb_config.h>
27 #include <rb_lib.h>
28 #include <sys/wait.h>
29
30 #ifdef HAVE_DLINFO
31 # include <link.h>
32 # include <dlfcn.h>
33 #endif
34
35 #ifdef __APPLE__
36 #include <mach-o/dyld.h>
37 #include <crt_externs.h>
38 #endif
39
40 #if defined(__FreeBSD__) || defined(__DragonFly__)
41 #include <sys/types.h>
42 #include <sys/sysctl.h>
43 #endif
44
45 #include <spawn.h>
46
47 #ifndef __APPLE__
48 extern char **environ;
49 #endif
50 pid_t
51 rb_spawn_process(const char *path, const char **argv)
52 {
53 pid_t pid;
54 const void *arghack = argv;
55 char **myenviron;
56 int error;
57 posix_spawnattr_t spattr;
58 posix_spawnattr_init(&spattr);
59 #ifdef POSIX_SPAWN_USEVFORK
60 posix_spawnattr_setflags(&spattr, POSIX_SPAWN_USEVFORK);
61 #endif
62 #ifdef __APPLE__
63 myenviron = *_NSGetEnviron(); /* apple needs to go fuck themselves for this */
64 #else
65 myenviron = environ;
66 #endif
67 error = posix_spawn(&pid, path, NULL, &spattr, arghack, myenviron);
68 posix_spawnattr_destroy(&spattr);
69 if (error != 0)
70 {
71 errno = error;
72 pid = -1;
73 }
74 return pid;
75 }
76
77 int
78 rb_gettimeofday(struct timeval *tv, void *tz)
79 {
80 return (gettimeofday(tv, tz));
81 }
82
83 void
84 rb_sleep(unsigned int seconds, unsigned int useconds)
85 {
86 struct timespec tv;
87 tv.tv_nsec = (useconds * 1000);
88 tv.tv_sec = seconds;
89 nanosleep(&tv, NULL);
90 }
91
92 /* this is to keep some linkers from bitching about exporting a non-existant symbol..bleh */
93 char *
94 rb_strerror(int error)
95 {
96 return strerror(error);
97 }
98
99 int
100 rb_kill(pid_t pid, int sig)
101 {
102 return kill(pid, sig);
103 }
104
105 int
106 rb_setenv(const char *name, const char *value, int overwrite)
107 {
108 return setenv(name, value, overwrite);
109 }
110
111 pid_t
112 rb_waitpid(pid_t pid, int *status, int options)
113 {
114 return waitpid(pid, status, options);
115 }
116
117 pid_t
118 rb_getpid(void)
119 {
120 return getpid();
121 }
122
123 const char *
124 rb_path_to_self(void)
125 {
126 static char path_buf[4096];
127 #if defined(HAVE_GETEXECNAME)
128 const char *s = getexecname();
129 if (s == NULL)
130 return NULL;
131 realpath(s, path_buf);
132 return path_buf;
133 #elif defined(__linux__) || (defined(__FreeBSD__) && !defined(KERN_PROC_PATHNAME))
134 if (readlink("/proc/self/exe", path_buf, sizeof(path_buf)) != -1)
135 return path_buf;
136 return NULL;
137 #elif defined(__FreeBSD__) || defined(__DragonFly__)
138 size_t path_len = sizeof(path_buf);
139 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
140 if (sysctl(mib, 4, path_buf, &path_len, NULL, 0) == 0)
141 return path_buf;
142 return NULL;
143 #elif defined(HAVE_DLINFO)
144 struct link_map *map = NULL;
145 dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &map);
146 if (map == NULL)
147 return NULL;
148 realpath(map->l_name, path_buf);
149 return path_buf;
150 #elif defined(__APPLE__)
151 char tmp_path[4096];
152 uint32_t pathlen = 4096;
153
154 if (_NSGetExecutablePath(tmp_path, &pathlen) < 0)
155 return NULL;
156
157 realpath(tmp_path, path_buf);
158 return path_buf;
159 #else
160 return NULL;
161 #endif
162 }