]> jfr.im git - irc/rqf/shadowircd.git/blob - libratbox/src/unix.c
a7b2efd2ba44afedda1f84f03cb1b18152ae83b7
[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 25038 2008-01-23 16:03:08Z androsyn $
25 */
26 #include <libratbox_config.h>
27 #include <ratbox_lib.h>
28
29 #ifndef WINDOWS
30 #if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN)
31 #include <spawn.h>
32 extern char **environ;
33 pid_t
34 rb_spawn_process(const char *path, const char **argv)
35 {
36 pid_t pid;
37 const void *arghack = argv;
38 posix_spawnattr_t spattr;
39 posix_spawnattr_init(&spattr);
40 #ifdef POSIX_SPAWN_USEVFORK
41 posix_spawnattr_setflags(&spattr, POSIX_SPAWN_USEVFORK);
42 #endif
43 if(posix_spawn(&pid, path, NULL, &spattr, arghack, environ))
44 {
45 return -1;
46 }
47 return pid;
48 }
49 #else
50 pid_t
51 rb_spawn_process(const char *path, const char **argv)
52 {
53 pid_t pid;
54 if(!(pid = vfork()))
55 {
56 execv(path, (const void *)argv); /* make gcc shut up */
57 _exit(1); /* if we're still here, we're screwed */
58 }
59 return(pid);
60 }
61 #endif
62
63 #ifndef HAVE_GETTIMEOFDAY
64 int
65 rb_gettimeofday(struct timeval *tv, void *tz)
66 {
67 if(tv == NULL)
68 {
69 errno = EFAULT;
70 return -1;
71 }
72 tv->tv_usec = 0;
73 if(time(&tv->tv_sec) == -1)
74 return -1;
75 return 0;
76 }
77 #else
78 int
79 rb_gettimeofday(struct timeval *tv, void *tz)
80 {
81 return(gettimeofday(tv, tz));
82 }
83 #endif
84
85 void
86 rb_sleep(unsigned int seconds, unsigned int useconds)
87 {
88 #ifdef HAVE_NANOSLEEP
89 struct timespec tv;
90 tv.tv_nsec = (useconds * 1000);
91 tv.tv_sec = seconds;
92 nanosleep(&tv, NULL);
93 #else
94 struct timeval tv;
95 tv.tv_sec = seconds;
96 tv.tv_usec = useconds;
97 select(0, NULL, NULL, NULL, &tv);
98 #endif
99 }
100 #endif /* !WINDOWS */
101
102