blob: f1cf7aeed2f6f587cb41bc3061ca049cdcc8b614 [file] [log] [blame]
Denis Vlasenko858ebf12008-04-09 00:33:53 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * Copyright (C) 2006 Rob Landley
7 * Copyright (C) 2006 Denys Vlasenko
8 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko858ebf12008-04-09 00:33:53 +000010 */
Denis Vlasenko858ebf12008-04-09 00:33:53 +000011/* We need to have separate xfuncs.c and xfuncs_printf.c because
12 * with current linkers, even with section garbage collection,
13 * if *.o module references any of XXXprintf functions, you pull in
14 * entire printf machinery. Even if you do not use the function
15 * which uses XXXprintf.
16 *
17 * xfuncs.c contains functions (not necessarily xfuncs)
18 * which do not pull in printf, directly or indirectly.
19 * xfunc_printf.c contains those which do.
20 */
Denis Vlasenko858ebf12008-04-09 00:33:53 +000021#include "libbb.h"
22
23
24/* All the functions starting with "x" call bb_error_msg_and_die() if they
25 * fail, so callers never need to check for errors. If it returned, it
26 * succeeded. */
27
Denys Vlasenko899ae532018-04-01 19:59:37 +020028void FAST_FUNC bb_die_memory_exhausted(void)
29{
James Byrne69374872019-07-02 11:35:03 +020030 bb_simple_error_msg_and_die(bb_msg_memory_exhausted);
Denys Vlasenko899ae532018-04-01 19:59:37 +020031}
32
Denis Vlasenko858ebf12008-04-09 00:33:53 +000033#ifndef DMALLOC
34/* dmalloc provides variants of these that do abort() on failure.
35 * Since dmalloc's prototypes overwrite the impls here as they are
36 * included after these prototypes in libbb.h, all is well.
37 */
38// Warn if we can't allocate size bytes of memory.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000039void* FAST_FUNC malloc_or_warn(size_t size)
Denis Vlasenko858ebf12008-04-09 00:33:53 +000040{
41 void *ptr = malloc(size);
42 if (ptr == NULL && size != 0)
James Byrne69374872019-07-02 11:35:03 +020043 bb_simple_error_msg(bb_msg_memory_exhausted);
Denis Vlasenko858ebf12008-04-09 00:33:53 +000044 return ptr;
45}
46
47// Die if we can't allocate size bytes of memory.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000048void* FAST_FUNC xmalloc(size_t size)
Denis Vlasenko858ebf12008-04-09 00:33:53 +000049{
50 void *ptr = malloc(size);
51 if (ptr == NULL && size != 0)
Denys Vlasenko899ae532018-04-01 19:59:37 +020052 bb_die_memory_exhausted();
Denis Vlasenko858ebf12008-04-09 00:33:53 +000053 return ptr;
54}
55
56// Die if we can't resize previously allocated memory. (This returns a pointer
57// to the new memory, which may or may not be the same as the old memory.
58// It'll copy the contents to a new chunk and free the old one if necessary.)
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000059void* FAST_FUNC xrealloc(void *ptr, size_t size)
Denis Vlasenko858ebf12008-04-09 00:33:53 +000060{
61 ptr = realloc(ptr, size);
62 if (ptr == NULL && size != 0)
Denys Vlasenko899ae532018-04-01 19:59:37 +020063 bb_die_memory_exhausted();
Denis Vlasenko858ebf12008-04-09 00:33:53 +000064 return ptr;
65}
66#endif /* DMALLOC */
67
68// Die if we can't allocate and zero size bytes of memory.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000069void* FAST_FUNC xzalloc(size_t size)
Denis Vlasenko858ebf12008-04-09 00:33:53 +000070{
71 void *ptr = xmalloc(size);
72 memset(ptr, 0, size);
73 return ptr;
74}
75
76// Die if we can't copy a string to freshly allocated memory.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000077char* FAST_FUNC xstrdup(const char *s)
Denis Vlasenko858ebf12008-04-09 00:33:53 +000078{
79 char *t;
80
81 if (s == NULL)
82 return NULL;
83
84 t = strdup(s);
85
86 if (t == NULL)
Denys Vlasenko899ae532018-04-01 19:59:37 +020087 bb_die_memory_exhausted();
Denis Vlasenko858ebf12008-04-09 00:33:53 +000088
89 return t;
90}
91
92// Die if we can't allocate n+1 bytes (space for the null terminator) and copy
93// the (possibly truncated to length n) string into it.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000094char* FAST_FUNC xstrndup(const char *s, int n)
Denis Vlasenko858ebf12008-04-09 00:33:53 +000095{
Denis Vlasenko858ebf12008-04-09 00:33:53 +000096 char *t;
97
98 if (ENABLE_DEBUG && s == NULL)
James Byrne69374872019-07-02 11:35:03 +020099 bb_simple_error_msg_and_die("xstrndup bug");
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000100
Martin Lewis9b4a9d92020-03-08 13:35:20 -0500101 t = strndup(s, n);
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000102
Martin Lewis9b4a9d92020-03-08 13:35:20 -0500103 if (t == NULL)
104 bb_die_memory_exhausted();
105
106 return t;
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000107}
108
Ron Yorstond840c5d2015-07-19 23:05:20 +0200109void* FAST_FUNC xmemdup(const void *s, int n)
110{
111 return memcpy(xmalloc(n), s, n);
112}
113
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000114// Die if we can't open a file and return a FILE* to it.
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000115// Notice we haven't got xfread(), This is for use with fscanf() and friends.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000116FILE* FAST_FUNC xfopen(const char *path, const char *mode)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000117{
118 FILE *fp = fopen(path, mode);
119 if (fp == NULL)
120 bb_perror_msg_and_die("can't open '%s'", path);
121 return fp;
122}
123
124// Die if we can't open a file and return a fd.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000125int FAST_FUNC xopen3(const char *pathname, int flags, int mode)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000126{
127 int ret;
128
129 ret = open(pathname, flags, mode);
130 if (ret < 0) {
131 bb_perror_msg_and_die("can't open '%s'", pathname);
132 }
133 return ret;
134}
135
Denys Vlasenkoc05387d2010-10-18 02:38:27 +0200136// Die if we can't open a file and return a fd.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000137int FAST_FUNC xopen(const char *pathname, int flags)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000138{
139 return xopen3(pathname, flags, 0666);
140}
141
142// Warn if we can't open a file and return a fd.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000143int FAST_FUNC open3_or_warn(const char *pathname, int flags, int mode)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000144{
145 int ret;
146
147 ret = open(pathname, flags, mode);
148 if (ret < 0) {
149 bb_perror_msg("can't open '%s'", pathname);
150 }
151 return ret;
152}
153
154// Warn if we can't open a file and return a fd.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000155int FAST_FUNC open_or_warn(const char *pathname, int flags)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000156{
157 return open3_or_warn(pathname, flags, 0666);
158}
159
Ryan Mallon5906a5c2013-10-08 14:52:49 +0200160/* Die if we can't open an existing file readonly with O_NONBLOCK
161 * and return the fd.
162 * Note that for ioctl O_RDONLY is sufficient.
163 */
164int FAST_FUNC xopen_nonblocking(const char *pathname)
165{
166 return xopen(pathname, O_RDONLY | O_NONBLOCK);
167}
168
169int FAST_FUNC xopen_as_uid_gid(const char *pathname, int flags, uid_t u, gid_t g)
170{
171 int fd;
172 uid_t old_euid = geteuid();
173 gid_t old_egid = getegid();
174
175 xsetegid(g);
176 xseteuid(u);
177
178 fd = xopen(pathname, flags);
179
180 xseteuid(old_euid);
181 xsetegid(old_egid);
182
183 return fd;
184}
185
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000186void FAST_FUNC xunlink(const char *pathname)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000187{
188 if (unlink(pathname))
189 bb_perror_msg_and_die("can't remove file '%s'", pathname);
190}
191
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000192void FAST_FUNC xrename(const char *oldpath, const char *newpath)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000193{
194 if (rename(oldpath, newpath))
195 bb_perror_msg_and_die("can't move '%s' to '%s'", oldpath, newpath);
196}
197
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000198int FAST_FUNC rename_or_warn(const char *oldpath, const char *newpath)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000199{
200 int n = rename(oldpath, newpath);
201 if (n)
202 bb_perror_msg("can't move '%s' to '%s'", oldpath, newpath);
203 return n;
204}
205
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000206void FAST_FUNC xpipe(int filedes[2])
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000207{
208 if (pipe(filedes))
James Byrne69374872019-07-02 11:35:03 +0200209 bb_simple_perror_msg_and_die("can't create pipe");
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000210}
211
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000212void FAST_FUNC xdup2(int from, int to)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000213{
214 if (dup2(from, to) != to)
James Byrne69374872019-07-02 11:35:03 +0200215 bb_simple_perror_msg_and_die("can't duplicate file descriptor");
Denys Vlasenko41ef41b2018-07-24 16:54:41 +0200216 // " %d to %d", from, to);
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000217}
218
219// "Renumber" opened fd
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000220void FAST_FUNC xmove_fd(int from, int to)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000221{
222 if (from == to)
223 return;
224 xdup2(from, to);
225 close(from);
226}
227
228// Die with an error message if we can't write the entire buffer.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000229void FAST_FUNC xwrite(int fd, const void *buf, size_t count)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000230{
231 if (count) {
232 ssize_t size = full_write(fd, buf, count);
Denys Vlasenko9fd61be2016-09-05 15:20:10 +0200233 if ((size_t)size != count) {
234 /*
235 * Two cases: write error immediately;
236 * or some writes succeeded, then we hit an error.
237 * In either case, errno is set.
238 */
James Byrne69374872019-07-02 11:35:03 +0200239 bb_simple_perror_msg_and_die(
Denys Vlasenko9fd61be2016-09-05 15:20:10 +0200240 size >= 0 ? "short write" : "write error"
241 );
242 }
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000243 }
244}
Denis Vlasenko73c571a2009-03-09 00:12:37 +0000245void FAST_FUNC xwrite_str(int fd, const char *str)
246{
247 xwrite(fd, str, strlen(str));
248}
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000249
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200250void FAST_FUNC xclose(int fd)
251{
252 if (close(fd))
James Byrne69374872019-07-02 11:35:03 +0200253 bb_simple_perror_msg_and_die("close failed");
Denys Vlasenkodcd27ab2009-10-05 03:03:07 +0200254}
255
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000256// Die with an error message if we can't lseek to the right spot.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000257off_t FAST_FUNC xlseek(int fd, off_t offset, int whence)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000258{
259 off_t off = lseek(fd, offset, whence);
260 if (off == (off_t)-1) {
James Byrne69374872019-07-02 11:35:03 +0200261 bb_perror_msg_and_die("lseek(%"OFF_FMT"u, %d)", offset, whence);
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000262 }
263 return off;
264}
265
Alexander Shishkin67227372010-10-22 13:27:16 +0200266int FAST_FUNC xmkstemp(char *template)
267{
268 int fd = mkstemp(template);
269 if (fd < 0)
270 bb_perror_msg_and_die("can't create temp file '%s'", template);
271 return fd;
272}
273
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000274// Die with supplied filename if this FILE* has ferror set.
275void FAST_FUNC die_if_ferror(FILE *fp, const char *fn)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000276{
277 if (ferror(fp)) {
278 /* ferror doesn't set useful errno */
279 bb_error_msg_and_die("%s: I/O error", fn);
280 }
281}
282
283// Die with an error message if stdout has ferror set.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000284void FAST_FUNC die_if_ferror_stdout(void)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000285{
286 die_if_ferror(stdout, bb_msg_standard_output);
287}
288
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100289int FAST_FUNC fflush_all(void)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000290{
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100291 return fflush(NULL);
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000292}
293
294
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000295int FAST_FUNC bb_putchar(int ch)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000296{
Denys Vlasenkoc0664722010-01-02 18:49:22 +0100297 return putchar(ch);
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000298}
299
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000300/* Die with an error message if we can't copy an entire FILE* to stdout,
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000301 * then close that file. */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000302void FAST_FUNC xprint_and_close_file(FILE *file)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000303{
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100304 fflush_all();
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000305 // copyfd outputs error messages for us.
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100306 if (bb_copyfd_eof(fileno(file), STDOUT_FILENO) == -1)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000307 xfunc_die();
308
309 fclose(file);
310}
311
312// Die with an error message if we can't malloc() enough space and do an
313// sprintf() into that space.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000314char* FAST_FUNC xasprintf(const char *format, ...)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000315{
316 va_list p;
317 int r;
318 char *string_ptr;
319
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000320 va_start(p, format);
321 r = vasprintf(&string_ptr, format, p);
322 va_end(p);
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000323
324 if (r < 0)
Denys Vlasenko899ae532018-04-01 19:59:37 +0200325 bb_die_memory_exhausted();
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000326 return string_ptr;
327}
328
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000329void FAST_FUNC xsetenv(const char *key, const char *value)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000330{
331 if (setenv(key, value, 1))
Denys Vlasenko899ae532018-04-01 19:59:37 +0200332 bb_die_memory_exhausted();
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000333}
334
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000335/* Handles "VAR=VAL" strings, even those which are part of environ
336 * _right now_
337 */
338void FAST_FUNC bb_unsetenv(const char *var)
339{
Denys Vlasenkoef0366e2017-07-22 02:15:17 +0200340 char onstack[128 - 16]; /* smaller stack setup code on x86 */
341 char *tp;
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000342
Denys Vlasenkoef0366e2017-07-22 02:15:17 +0200343 tp = strchr(var, '=');
344 if (tp) {
345 /* In case var was putenv'ed, we can't replace '='
346 * with NUL and unsetenv(var) - it won't work,
347 * env is modified by the replacement, unsetenv
348 * sees "VAR" instead of "VAR=VAL" and does not remove it!
349 * Horror :(
350 */
351 unsigned sz = tp - var;
352 if (sz < sizeof(onstack)) {
353 ((char*)mempcpy(onstack, var, sz))[0] = '\0';
354 tp = NULL;
355 var = onstack;
356 } else {
357 /* unlikely: very long var name */
358 var = tp = xstrndup(var, sz);
359 }
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000360 }
Denys Vlasenkoef0366e2017-07-22 02:15:17 +0200361 unsetenv(var);
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000362 free(tp);
363}
364
Denys Vlasenkodd8adde2010-06-24 05:00:50 +0200365void FAST_FUNC bb_unsetenv_and_free(char *var)
366{
367 bb_unsetenv(var);
368 free(var);
369}
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000370
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000371// Die with an error message if we can't set gid. (Because resource limits may
372// limit this user to a given number of processes, and if that fills up the
373// setgid() will fail and we'll _still_be_root_, which is bad.)
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000374void FAST_FUNC xsetgid(gid_t gid)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000375{
James Byrne69374872019-07-02 11:35:03 +0200376 if (setgid(gid)) bb_simple_perror_msg_and_die("setgid");
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000377}
378
379// Die with an error message if we can't set uid. (See xsetgid() for why.)
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000380void FAST_FUNC xsetuid(uid_t uid)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000381{
James Byrne69374872019-07-02 11:35:03 +0200382 if (setuid(uid)) bb_simple_perror_msg_and_die("setuid");
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000383}
384
Ryan Mallon5906a5c2013-10-08 14:52:49 +0200385void FAST_FUNC xsetegid(gid_t egid)
386{
James Byrne69374872019-07-02 11:35:03 +0200387 if (setegid(egid)) bb_simple_perror_msg_and_die("setegid");
Ryan Mallon5906a5c2013-10-08 14:52:49 +0200388}
389
390void FAST_FUNC xseteuid(uid_t euid)
391{
James Byrne69374872019-07-02 11:35:03 +0200392 if (seteuid(euid)) bb_simple_perror_msg_and_die("seteuid");
Ryan Mallon5906a5c2013-10-08 14:52:49 +0200393}
394
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000395// Die if we can't chdir to a new path.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000396void FAST_FUNC xchdir(const char *path)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000397{
398 if (chdir(path))
Pascal Bellard70fc8c12012-06-12 13:21:02 +0200399 bb_perror_msg_and_die("can't change directory to '%s'", path);
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000400}
401
Denys Vlasenkoc4199f22016-04-01 22:12:44 +0200402void FAST_FUNC xfchdir(int fd)
403{
404 if (fchdir(fd))
James Byrne69374872019-07-02 11:35:03 +0200405 bb_simple_perror_msg_and_die("fchdir");
Denys Vlasenkoc4199f22016-04-01 22:12:44 +0200406}
407
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000408void FAST_FUNC xchroot(const char *path)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000409{
410 if (chroot(path))
Pascal Bellard70fc8c12012-06-12 13:21:02 +0200411 bb_perror_msg_and_die("can't change root directory to '%s'", path);
Denys Vlasenko0687a5b2012-03-08 00:28:24 +0100412 xchdir("/");
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000413}
414
415// Print a warning message if opendir() fails, but don't die.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000416DIR* FAST_FUNC warn_opendir(const char *path)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000417{
418 DIR *dp;
419
420 dp = opendir(path);
421 if (!dp)
422 bb_perror_msg("can't open '%s'", path);
423 return dp;
424}
425
426// Die with an error message if opendir() fails.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000427DIR* FAST_FUNC xopendir(const char *path)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000428{
429 DIR *dp;
430
431 dp = opendir(path);
432 if (!dp)
433 bb_perror_msg_and_die("can't open '%s'", path);
434 return dp;
435}
436
437// Die with an error message if we can't open a new socket.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000438int FAST_FUNC xsocket(int domain, int type, int protocol)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000439{
440 int r = socket(domain, type, protocol);
441
442 if (r < 0) {
443 /* Hijack vaguely related config option */
444#if ENABLE_VERBOSE_RESOLUTION_ERRORS
445 const char *s = "INET";
Jeremie Koenig29885112010-05-27 15:39:24 +0200446# ifdef AF_PACKET
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000447 if (domain == AF_PACKET) s = "PACKET";
Jeremie Koenig29885112010-05-27 15:39:24 +0200448# endif
449# ifdef AF_NETLINK
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000450 if (domain == AF_NETLINK) s = "NETLINK";
Jeremie Koenig29885112010-05-27 15:39:24 +0200451# endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000452IF_FEATURE_IPV6(if (domain == AF_INET6) s = "INET6";)
Denys Vlasenkob2e5fc32009-11-25 14:52:47 +0100453 bb_perror_msg_and_die("socket(AF_%s,%d,%d)", s, type, protocol);
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000454#else
James Byrne69374872019-07-02 11:35:03 +0200455 bb_simple_perror_msg_and_die("socket");
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000456#endif
457 }
458
459 return r;
460}
461
462// Die with an error message if we can't bind a socket to an address.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000463void FAST_FUNC xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000464{
James Byrne69374872019-07-02 11:35:03 +0200465 if (bind(sockfd, my_addr, addrlen)) bb_simple_perror_msg_and_die("bind");
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000466}
467
468// Die with an error message if we can't listen for connections on a socket.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000469void FAST_FUNC xlisten(int s, int backlog)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000470{
James Byrne69374872019-07-02 11:35:03 +0200471 if (listen(s, backlog)) bb_simple_perror_msg_and_die("listen");
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000472}
473
474/* Die with an error message if sendto failed.
475 * Return bytes sent otherwise */
Denis Vlasenkofa65a3d2009-01-24 20:11:36 +0000476ssize_t FAST_FUNC xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000477 socklen_t tolen)
478{
479 ssize_t ret = sendto(s, buf, len, 0, to, tolen);
480 if (ret < 0) {
481 if (ENABLE_FEATURE_CLEAN_UP)
482 close(s);
James Byrne69374872019-07-02 11:35:03 +0200483 bb_simple_perror_msg_and_die("sendto");
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000484 }
485 return ret;
486}
487
488// xstat() - a stat() which dies on failure with meaningful error message
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000489void FAST_FUNC xstat(const char *name, struct stat *stat_buf)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000490{
491 if (stat(name, stat_buf))
492 bb_perror_msg_and_die("can't stat '%s'", name);
493}
494
Denys Vlasenko8d3e2252010-08-31 12:42:06 +0200495void FAST_FUNC xfstat(int fd, struct stat *stat_buf, const char *errmsg)
496{
497 /* errmsg is usually a file name, but not always:
498 * xfstat may be called in a spot where file name is no longer
499 * available, and caller may give e.g. "can't stat input file" string.
500 */
501 if (fstat(fd, stat_buf))
502 bb_simple_perror_msg_and_die(errmsg);
503}
504
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000505// selinux_or_die() - die if SELinux is disabled.
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000506void FAST_FUNC selinux_or_die(void)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000507{
508#if ENABLE_SELINUX
509 int rc = is_selinux_enabled();
510 if (rc == 0) {
James Byrne69374872019-07-02 11:35:03 +0200511 bb_simple_error_msg_and_die("SELinux is disabled");
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000512 } else if (rc < 0) {
James Byrne69374872019-07-02 11:35:03 +0200513 bb_simple_error_msg_and_die("is_selinux_enabled() failed");
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000514 }
515#else
James Byrne69374872019-07-02 11:35:03 +0200516 bb_simple_error_msg_and_die("SELinux support is disabled");
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000517#endif
518}
519
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000520int FAST_FUNC ioctl_or_perror_and_die(int fd, unsigned request, void *argp, const char *fmt,...)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000521{
522 int ret;
523 va_list p;
524
525 ret = ioctl(fd, request, argp);
526 if (ret < 0) {
527 va_start(p, fmt);
528 bb_verror_msg(fmt, p, strerror(errno));
529 /* xfunc_die can actually longjmp, so be nice */
530 va_end(p);
531 xfunc_die();
532 }
533 return ret;
534}
535
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000536int FAST_FUNC ioctl_or_perror(int fd, unsigned request, void *argp, const char *fmt,...)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000537{
538 va_list p;
539 int ret = ioctl(fd, request, argp);
540
541 if (ret < 0) {
542 va_start(p, fmt);
543 bb_verror_msg(fmt, p, strerror(errno));
544 va_end(p);
545 }
546 return ret;
547}
548
549#if ENABLE_IOCTL_HEX2STR_ERROR
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000550int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp, const char *ioctl_name)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000551{
552 int ret;
553
554 ret = ioctl(fd, request, argp);
555 if (ret < 0)
556 bb_simple_perror_msg(ioctl_name);
557 return ret;
558}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000559int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp, const char *ioctl_name)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000560{
561 int ret;
562
563 ret = ioctl(fd, request, argp);
564 if (ret < 0)
565 bb_simple_perror_msg_and_die(ioctl_name);
566 return ret;
567}
568#else
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000569int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000570{
571 int ret;
572
573 ret = ioctl(fd, request, argp);
574 if (ret < 0)
575 bb_perror_msg("ioctl %#x failed", request);
576 return ret;
577}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000578int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp)
Denis Vlasenko858ebf12008-04-09 00:33:53 +0000579{
580 int ret;
581
582 ret = ioctl(fd, request, argp);
583 if (ret < 0)
584 bb_perror_msg_and_die("ioctl %#x failed", request);
585 return ret;
586}
587#endif
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200588
589char* FAST_FUNC xmalloc_ttyname(int fd)
590{
Denys Vlasenko543efd72013-08-06 00:41:06 +0200591 char buf[128];
592 int r = ttyname_r(fd, buf, sizeof(buf) - 1);
593 if (r)
594 return NULL;
595 return xstrdup(buf);
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200596}
597
598void FAST_FUNC generate_uuid(uint8_t *buf)
599{
600 /* http://www.ietf.org/rfc/rfc4122.txt
601 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
602 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
603 * | time_low |
604 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
605 * | time_mid | time_hi_and_version |
606 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
607 * |clk_seq_and_variant | node (0-1) |
608 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
609 * | node (2-5) |
610 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
611 * IOW, uuid has this layout:
612 * uint32_t time_low (big endian)
613 * uint16_t time_mid (big endian)
614 * uint16_t time_hi_and_version (big endian)
615 * version is a 4-bit field:
616 * 1 Time-based
617 * 2 DCE Security, with embedded POSIX UIDs
618 * 3 Name-based (MD5)
619 * 4 Randomly generated
620 * 5 Name-based (SHA-1)
621 * uint16_t clk_seq_and_variant (big endian)
622 * variant is a 3-bit field:
623 * 0xx Reserved, NCS backward compatibility
624 * 10x The variant specified in rfc4122
625 * 110 Reserved, Microsoft backward compatibility
626 * 111 Reserved for future definition
627 * uint8_t node[6]
628 *
629 * For version 4, these bits are set/cleared:
630 * time_hi_and_version & 0x0fff | 0x4000
631 * clk_seq_and_variant & 0x3fff | 0x8000
632 */
633 pid_t pid;
634 int i;
635
636 i = open("/dev/urandom", O_RDONLY);
637 if (i >= 0) {
638 read(i, buf, 16);
639 close(i);
640 }
641 /* Paranoia. /dev/urandom may be missing.
642 * rand() is guaranteed to generate at least [0, 2^15) range,
643 * but lowest bits in some libc are not so "random". */
644 srand(monotonic_us()); /* pulls in printf */
645 pid = getpid();
646 while (1) {
647 for (i = 0; i < 16; i++)
648 buf[i] ^= rand() >> 5;
649 if (pid == 0)
650 break;
651 srand(pid);
652 pid = 0;
653 }
654
655 /* version = 4 */
656 buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40;
657 /* variant = 10x */
658 buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80;
659}
Pascal Bellard926031b2010-07-04 15:32:38 +0200660
661#if BB_MMU
662pid_t FAST_FUNC xfork(void)
663{
664 pid_t pid;
665 pid = fork();
666 if (pid < 0) /* wtf? */
James Byrne69374872019-07-02 11:35:03 +0200667 bb_simple_perror_msg_and_die("vfork"+1);
Pascal Bellard926031b2010-07-04 15:32:38 +0200668 return pid;
669}
670#endif
Denys Vlasenko82203992016-04-02 18:06:24 +0200671
672void FAST_FUNC xvfork_parent_waits_and_exits(void)
673{
674 pid_t pid;
675
676 fflush_all();
677 pid = xvfork();
678 if (pid > 0) {
679 /* Parent */
680 int exit_status = wait_for_exitstatus(pid);
681 if (WIFSIGNALED(exit_status))
682 kill_myself_with_sig(WTERMSIG(exit_status));
683 _exit(WEXITSTATUS(exit_status));
684 }
685 /* Child continues */
686}