]> jfr.im git - solanum.git/blame - librb/src/unix.c
Normalize snprintf size to use sizeof where possible
[solanum.git] / librb / src / unix.c
CommitLineData
db137867
AC
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 *
db137867 24 */
c056dba2 25
fe037171
EM
26#include <librb_config.h>
27#include <rb_lib.h>
3202e249
VY
28#include <sys/wait.h>
29
790e4dce
AC
30#ifdef HAVE_DLINFO
31# include <link.h>
32# include <dlfcn.h>
33#endif
3202e249
VY
34
35#ifdef __APPLE__
790e4dce 36#include <mach-o/dyld.h>
55abcbb2 37#include <crt_externs.h>
3202e249
VY
38#endif
39
d71787ab
AC
40#if defined(__FreeBSD__) || defined(__DragonFly__)
41#include <sys/types.h>
42#include <sys/sysctl.h>
43#endif
44
790e4dce
AC
45#if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN)
46#include <spawn.h>
47
3202e249 48#ifndef __APPLE__
db137867 49extern char **environ;
3202e249 50#endif
db137867
AC
51pid_t
52rb_spawn_process(const char *path, const char **argv)
53{
54 pid_t pid;
55 const void *arghack = argv;
3202e249 56 char **myenviron;
dcb22e07 57 int error;
db137867
AC
58 posix_spawnattr_t spattr;
59 posix_spawnattr_init(&spattr);
60#ifdef POSIX_SPAWN_USEVFORK
61 posix_spawnattr_setflags(&spattr, POSIX_SPAWN_USEVFORK);
62#endif
3202e249
VY
63#ifdef __APPLE__
64 myenviron = *_NSGetEnviron(); /* apple needs to go fuck themselves for this */
65#else
66 myenviron = environ;
67#endif
dcb22e07
JT
68 error = posix_spawn(&pid, path, NULL, &spattr, arghack, myenviron);
69 posix_spawnattr_destroy(&spattr);
70 if (error != 0)
db137867 71 {
dcb22e07
JT
72 errno = error;
73 pid = -1;
db137867
AC
74 }
75 return pid;
76}
77#else
78pid_t
79rb_spawn_process(const char *path, const char **argv)
80{
81 pid_t pid;
82 if(!(pid = vfork()))
83 {
3202e249
VY
84 execv(path, (const void *)argv); /* make gcc shut up */
85 _exit(1); /* if we're still here, we're screwed */
db137867 86 }
3202e249 87 return (pid);
db137867
AC
88}
89#endif
90
91#ifndef HAVE_GETTIMEOFDAY
92int
93rb_gettimeofday(struct timeval *tv, void *tz)
94{
95 if(tv == NULL)
96 {
97 errno = EFAULT;
98 return -1;
99 }
100 tv->tv_usec = 0;
101 if(time(&tv->tv_sec) == -1)
102 return -1;
103 return 0;
104}
105#else
106int
107rb_gettimeofday(struct timeval *tv, void *tz)
108{
3202e249 109 return (gettimeofday(tv, tz));
db137867
AC
110}
111#endif
112
113void
114rb_sleep(unsigned int seconds, unsigned int useconds)
115{
116#ifdef HAVE_NANOSLEEP
117 struct timespec tv;
118 tv.tv_nsec = (useconds * 1000);
119 tv.tv_sec = seconds;
120 nanosleep(&tv, NULL);
3202e249 121#else
db137867
AC
122 struct timeval tv;
123 tv.tv_sec = seconds;
124 tv.tv_usec = useconds;
125 select(0, NULL, NULL, NULL, &tv);
126#endif
127}
3202e249
VY
128
129/* this is to keep some linkers from bitching about exporting a non-existant symbol..bleh */
130char *
131rb_strerror(int error)
132{
133 return strerror(error);
134}
135
136int
137rb_kill(pid_t pid, int sig)
138{
139 return kill(pid, sig);
140}
141
142int
143rb_setenv(const char *name, const char *value, int overwrite)
144{
145 return setenv(name, value, overwrite);
146}
147
148pid_t
149rb_waitpid(pid_t pid, int *status, int options)
150{
151 return waitpid(pid, status, options);
152}
153
154pid_t
155rb_getpid(void)
156{
157 return getpid();
158}
db137867 159
790e4dce
AC
160const char *
161rb_path_to_self(void)
162{
163 static char path_buf[4096];
164#if defined(HAVE_GETEXECNAME)
165 char *s = getexecname();
166 if (s == NULL)
167 return NULL;
168 realpath(s, path_buf);
169 return path_buf;
87f76327 170#elif defined(__linux__) || (defined(__FreeBSD__) && !defined(KERN_PROC_PATHNAME))
4b7abcfb 171 if (readlink("/proc/self/exe", path_buf, sizeof(path_buf)) != -1)
d71787ab
AC
172 return path_buf;
173 return NULL;
174#elif defined(__FreeBSD__) || defined(__DragonFly__)
4b7abcfb 175 size_t path_len = sizeof(path_buf);
d71787ab
AC
176 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
177 if (sysctl(mib, 4, path_buf, &path_len, NULL, 0) == 0)
87b20cbb
AC
178 return path_buf;
179 return NULL;
790e4dce
AC
180#elif defined(HAVE_DLINFO)
181 struct link_map *map = NULL;
182 dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &map);
183 if (map == NULL)
184 return NULL;
185 realpath(map->l_name, path_buf);
a20190d5 186 return path_buf;
790e4dce
AC
187#elif defined(__APPLE__)
188 char tmp_path[4096];
189 uint32_t pathlen = 4096;
190
191 if (_NSGetExecutablePath(tmp_path, &pathlen) < 0)
192 return NULL;
193
194 realpath(tmp_path, path_buf);
195 return path_buf;
87f76327
SA
196#else
197 return NULL;
790e4dce
AC
198#endif
199}