]> jfr.im git - irc/rqf/shadowircd.git/blame - libratbox/src/unix.c
Update FAQ.
[irc/rqf/shadowircd.git] / libratbox / src / unix.c
CommitLineData
b57f37fb
WP
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 *
94b4fbf9 24 * $Id: unix.c 26180 2008-11-11 00:00:12Z androsyn $
b57f37fb
WP
25 */
26#include <libratbox_config.h>
27#include <ratbox_lib.h>
28
94b4fbf9
VY
29
30#ifndef _WIN32
31
32#include <sys/wait.h>
33
34
b57f37fb
WP
35#if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN)
36#include <spawn.h>
94b4fbf9
VY
37
38#ifdef __APPLE__
39#include <crt_externs.h>
40#endif
41
42#ifndef __APPLE__
b57f37fb 43extern char **environ;
94b4fbf9 44#endif
b57f37fb
WP
45pid_t
46rb_spawn_process(const char *path, const char **argv)
47{
48 pid_t pid;
49 const void *arghack = argv;
94b4fbf9 50 char **myenviron;
8b4cf5c1 51 int error;
b57f37fb
WP
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
94b4fbf9
VY
57#ifdef __APPLE__
58 myenviron = *_NSGetEnviron(); /* apple needs to go fuck themselves for this */
59#else
60 myenviron = environ;
61#endif
8b4cf5c1
JT
62 error = posix_spawn(&pid, path, NULL, &spattr, arghack, myenviron);
63 posix_spawnattr_destroy(&spattr);
64 if (error != 0)
b57f37fb 65 {
8b4cf5c1
JT
66 errno = error;
67 pid = -1;
b57f37fb
WP
68 }
69 return pid;
70}
71#else
72pid_t
73rb_spawn_process(const char *path, const char **argv)
74{
75 pid_t pid;
76 if(!(pid = vfork()))
77 {
94b4fbf9
VY
78 execv(path, (const void *)argv); /* make gcc shut up */
79 _exit(1); /* if we're still here, we're screwed */
b57f37fb 80 }
94b4fbf9 81 return (pid);
b57f37fb
WP
82}
83#endif
84
85#ifndef HAVE_GETTIMEOFDAY
86int
87rb_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
100int
101rb_gettimeofday(struct timeval *tv, void *tz)
102{
94b4fbf9 103 return (gettimeofday(tv, tz));
b57f37fb
WP
104}
105#endif
106
107void
108rb_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);
94b4fbf9 115#else
b57f37fb
WP
116 struct timeval tv;
117 tv.tv_sec = seconds;
118 tv.tv_usec = useconds;
119 select(0, NULL, NULL, NULL, &tv);
120#endif
121}
94b4fbf9
VY
122
123/* this is to keep some linkers from bitching about exporting a non-existant symbol..bleh */
124char *
125rb_strerror(int error)
126{
127 return strerror(error);
128}
129
130int
131rb_kill(pid_t pid, int sig)
132{
133 return kill(pid, sig);
134}
135
136int
137rb_setenv(const char *name, const char *value, int overwrite)
138{
139 return setenv(name, value, overwrite);
140}
141
142pid_t
143rb_waitpid(pid_t pid, int *status, int options)
144{
145 return waitpid(pid, status, options);
146}
147
148pid_t
149rb_getpid(void)
150{
151 return getpid();
152}
b57f37fb
WP
153
154
94b4fbf9 155#endif /* !WIN32 */