]>
jfr.im git - solanum.git/blob - librb/src/unix.c
2 * ircd-ratbox: A slightly useful ircd.
3 * unix.c: various unix type functions
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>
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.
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.
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
26 #include <librb_config.h>
39 #include <mach-o/dyld.h>
40 #include <crt_externs.h>
43 #if defined(__FreeBSD__) || defined(__DragonFly__)
44 #include <sys/types.h>
45 #include <sys/sysctl.h>
48 #if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN)
52 extern char **environ
;
55 rb_spawn_process(const char *path
, const char **argv
)
58 const void *arghack
= argv
;
61 posix_spawnattr_t spattr
;
62 posix_spawnattr_init(&spattr
);
63 #ifdef POSIX_SPAWN_USEVFORK
64 posix_spawnattr_setflags(&spattr
, POSIX_SPAWN_USEVFORK
);
67 myenviron
= *_NSGetEnviron(); /* apple needs to go fuck themselves for this */
71 error
= posix_spawn(&pid
, path
, NULL
, &spattr
, arghack
, myenviron
);
72 posix_spawnattr_destroy(&spattr
);
82 rb_spawn_process(const char *path
, const char **argv
)
87 execv(path
, (const void *)argv
); /* make gcc shut up */
88 _exit(1); /* if we're still here, we're screwed */
94 #ifndef HAVE_GETTIMEOFDAY
96 rb_gettimeofday(struct timeval
*tv
, void *tz
)
104 if(time(&tv
->tv_sec
) == -1)
110 rb_gettimeofday(struct timeval
*tv
, void *tz
)
112 return (gettimeofday(tv
, tz
));
117 rb_sleep(unsigned int seconds
, unsigned int useconds
)
119 #ifdef HAVE_NANOSLEEP
121 tv
.tv_nsec
= (useconds
* 1000);
123 nanosleep(&tv
, NULL
);
127 tv
.tv_usec
= useconds
;
128 select(0, NULL
, NULL
, NULL
, &tv
);
132 /* this is to keep some linkers from bitching about exporting a non-existant symbol..bleh */
134 rb_strerror(int error
)
136 return strerror(error
);
140 rb_kill(pid_t pid
, int sig
)
142 return kill(pid
, sig
);
146 rb_setenv(const char *name
, const char *value
, int overwrite
)
148 return setenv(name
, value
, overwrite
);
152 rb_waitpid(pid_t pid
, int *status
, int options
)
154 return waitpid(pid
, status
, options
);
164 rb_path_to_self(void)
166 static char path_buf
[4096];
167 #if defined(HAVE_GETEXECNAME)
168 char *s
= getexecname();
171 realpath(s
, path_buf
);
173 #elif defined(__linux__) || (defined(__FreeBSD__) && !defined(KERN_PROC_PATHNAME))
174 if (readlink("/proc/self/exe", path_buf
, sizeof(path_buf
)) != -1)
177 #elif defined(__FreeBSD__) || defined(__DragonFly__)
178 size_t path_len
= sizeof(path_buf
);
179 int mib
[4] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1 };
180 if (sysctl(mib
, 4, path_buf
, &path_len
, NULL
, 0) == 0)
183 #elif defined(HAVE_DLINFO)
184 struct link_map
*map
= NULL
;
185 dlinfo(RTLD_SELF
, RTLD_DI_LINKMAP
, &map
);
188 realpath(map
->l_name
, path_buf
);
190 #elif defined(__APPLE__)
192 uint32_t pathlen
= 4096;
194 if (_NSGetExecutablePath(tmp_path
, &pathlen
) < 0)
197 realpath(tmp_path
, path_buf
);