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