]> jfr.im git - irc/rqf/shadowircd.git/blame - libratbox/src/unix.c
Copied libratbox and related stuff from shadowircd upstream.
[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;
b57f37fb
WP
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
94b4fbf9
VY
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))
b57f37fb
WP
62 {
63 return -1;
64 }
65 return pid;
66}
67#else
68pid_t
69rb_spawn_process(const char *path, const char **argv)
70{
71 pid_t pid;
72 if(!(pid = vfork()))
73 {
94b4fbf9
VY
74 execv(path, (const void *)argv); /* make gcc shut up */
75 _exit(1); /* if we're still here, we're screwed */
b57f37fb 76 }
94b4fbf9 77 return (pid);
b57f37fb
WP
78}
79#endif
80
81#ifndef HAVE_GETTIMEOFDAY
82int
83rb_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
96int
97rb_gettimeofday(struct timeval *tv, void *tz)
98{
94b4fbf9 99 return (gettimeofday(tv, tz));
b57f37fb
WP
100}
101#endif
102
103void
104rb_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);
94b4fbf9 111#else
b57f37fb
WP
112 struct timeval tv;
113 tv.tv_sec = seconds;
114 tv.tv_usec = useconds;
115 select(0, NULL, NULL, NULL, &tv);
116#endif
117}
94b4fbf9
VY
118
119/* this is to keep some linkers from bitching about exporting a non-existant symbol..bleh */
120char *
121rb_strerror(int error)
122{
123 return strerror(error);
124}
125
126int
127rb_kill(pid_t pid, int sig)
128{
129 return kill(pid, sig);
130}
131
132int
133rb_setenv(const char *name, const char *value, int overwrite)
134{
135 return setenv(name, value, overwrite);
136}
137
138pid_t
139rb_waitpid(pid_t pid, int *status, int options)
140{
141 return waitpid(pid, status, options);
142}
143
144pid_t
145rb_getpid(void)
146{
147 return getpid();
148}
b57f37fb
WP
149
150
94b4fbf9 151#endif /* !WIN32 */