]> jfr.im git - irc/rqf/shadowircd.git/blob - libratbox/src/unix.c
Copied libratbox and related stuff from shadowircd upstream.
[irc/rqf/shadowircd.git] / libratbox / 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 <libratbox_config.h>
27 #include <ratbox_lib.h>
28
29
30 #ifndef _WIN32
31
32 #include <sys/wait.h>
33
34
35 #if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN)
36 #include <spawn.h>
37
38 #ifdef __APPLE__
39 #include <crt_externs.h>
40 #endif
41
42 #ifndef __APPLE__
43 extern char **environ;
44 #endif
45 pid_t
46 rb_spawn_process(const char *path, const char **argv)
47 {
48 pid_t pid;
49 const void *arghack = argv;
50 char **myenviron;
51 posix_spawnattr_t spattr;
52 posix_spawnattr_init(&spattr);
53 #ifdef POSIX_SPAWN_USEVFORK
54 posix_spawnattr_setflags(&spattr, POSIX_SPAWN_USEVFORK);
55 #endif
56 #ifdef __APPLE__
57 myenviron = *_NSGetEnviron(); /* apple needs to go fuck themselves for this */
58 #else
59 myenviron = environ;
60 #endif
61 if(posix_spawn(&pid, path, NULL, &spattr, arghack, myenviron))
62 {
63 return -1;
64 }
65 return pid;
66 }
67 #else
68 pid_t
69 rb_spawn_process(const char *path, const char **argv)
70 {
71 pid_t pid;
72 if(!(pid = vfork()))
73 {
74 execv(path, (const void *)argv); /* make gcc shut up */
75 _exit(1); /* if we're still here, we're screwed */
76 }
77 return (pid);
78 }
79 #endif
80
81 #ifndef HAVE_GETTIMEOFDAY
82 int
83 rb_gettimeofday(struct timeval *tv, void *tz)
84 {
85 if(tv == NULL)
86 {
87 errno = EFAULT;
88 return -1;
89 }
90 tv->tv_usec = 0;
91 if(time(&tv->tv_sec) == -1)
92 return -1;
93 return 0;
94 }
95 #else
96 int
97 rb_gettimeofday(struct timeval *tv, void *tz)
98 {
99 return (gettimeofday(tv, tz));
100 }
101 #endif
102
103 void
104 rb_sleep(unsigned int seconds, unsigned int useconds)
105 {
106 #ifdef HAVE_NANOSLEEP
107 struct timespec tv;
108 tv.tv_nsec = (useconds * 1000);
109 tv.tv_sec = seconds;
110 nanosleep(&tv, NULL);
111 #else
112 struct timeval tv;
113 tv.tv_sec = seconds;
114 tv.tv_usec = useconds;
115 select(0, NULL, NULL, NULL, &tv);
116 #endif
117 }
118
119 /* this is to keep some linkers from bitching about exporting a non-existant symbol..bleh */
120 char *
121 rb_strerror(int error)
122 {
123 return strerror(error);
124 }
125
126 int
127 rb_kill(pid_t pid, int sig)
128 {
129 return kill(pid, sig);
130 }
131
132 int
133 rb_setenv(const char *name, const char *value, int overwrite)
134 {
135 return setenv(name, value, overwrite);
136 }
137
138 pid_t
139 rb_waitpid(pid_t pid, int *status, int options)
140 {
141 return waitpid(pid, status, options);
142 }
143
144 pid_t
145 rb_getpid(void)
146 {
147 return getpid();
148 }
149
150
151 #endif /* !WIN32 */