blob: a926beb83e520db42bce76fa9d18d8bc3dea2996 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Rob Landleyd921b2e2006-08-03 15:41:12 +00006 * Copyright (C) 2006 Rob Landley
7 * Copyright (C) 2006 Denis Vlasenko
Eric Andersenaad1a882001-03-16 22:47:14 +00008 *
Rob Landley4e9deec2006-02-20 02:44:30 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +000010 */
11
Rob Landley552b56d2006-05-04 21:22:27 +000012#include "busybox.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000013
14#ifndef DMALLOC
Manuel Novoa III cad53642003-03-19 09:13:01 +000015#ifdef L_xmalloc
Rob Landleydfba7412006-03-06 20:47:33 +000016void *xmalloc(size_t size)
Eric Andersenaad1a882001-03-16 22:47:14 +000017{
18 void *ptr = malloc(size);
Matt Kraaia99b1942002-02-26 15:28:22 +000019 if (ptr == NULL && size != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000020 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000021 return ptr;
22}
Manuel Novoa III cad53642003-03-19 09:13:01 +000023#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000024
Manuel Novoa III cad53642003-03-19 09:13:01 +000025#ifdef L_xrealloc
Rob Landleydfba7412006-03-06 20:47:33 +000026void *xrealloc(void *ptr, size_t size)
Eric Andersenaad1a882001-03-16 22:47:14 +000027{
Matt Kraaia99b1942002-02-26 15:28:22 +000028 ptr = realloc(ptr, size);
29 if (ptr == NULL && size != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000031 return ptr;
32}
Manuel Novoa III cad53642003-03-19 09:13:01 +000033#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000034
Rob Landley80b8ff02006-05-19 20:36:49 +000035#ifdef L_xzalloc
36void *xzalloc(size_t size)
37{
38 void *ptr = xmalloc(size);
39 memset(ptr, 0, size);
40 return ptr;
41}
42#endif
43
Eric Andersen9f894f42003-07-05 22:15:43 +000044#endif /* DMALLOC */
Eric Andersenaad1a882001-03-16 22:47:14 +000045
Manuel Novoa III cad53642003-03-19 09:13:01 +000046#ifdef L_xstrdup
Rob Landleyd921b2e2006-08-03 15:41:12 +000047char * xstrdup (const char *s)
Rob Landley31642d72006-03-14 21:45:38 +000048{
Eric Andersenaad1a882001-03-16 22:47:14 +000049 char *t;
50
51 if (s == NULL)
52 return NULL;
53
54 t = strdup (s);
55
56 if (t == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000058
59 return t;
60}
61#endif
62
Manuel Novoa III cad53642003-03-19 09:13:01 +000063#ifdef L_xstrndup
Rob Landleyd921b2e2006-08-03 15:41:12 +000064char * xstrndup (const char *s, int n)
Rob Landley31642d72006-03-14 21:45:38 +000065{
Eric Andersenaad1a882001-03-16 22:47:14 +000066 char *t;
67
Rob Landley8bbdb872006-06-30 16:36:56 +000068 if (ENABLE_DEBUG && s == NULL)
Rob Landleyd921b2e2006-08-03 15:41:12 +000069 bb_error_msg_and_die("xstrndup bug");
Eric Andersenaad1a882001-03-16 22:47:14 +000070
71 t = xmalloc(++n);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000072
Eric Andersenaad1a882001-03-16 22:47:14 +000073 return safe_strncpy(t,s,n);
74}
Manuel Novoa III cad53642003-03-19 09:13:01 +000075#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000076
Manuel Novoa III cad53642003-03-19 09:13:01 +000077#ifdef L_xfopen
Rob Landleyd921b2e2006-08-03 15:41:12 +000078FILE *xfopen(const char *path, const char *mode)
Eric Andersenaad1a882001-03-16 22:47:14 +000079{
80 FILE *fp;
81 if ((fp = fopen(path, mode)) == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 bb_perror_msg_and_die("%s", path);
Eric Andersenaad1a882001-03-16 22:47:14 +000083 return fp;
84}
Manuel Novoa III cad53642003-03-19 09:13:01 +000085#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000086
Manuel Novoa III cad53642003-03-19 09:13:01 +000087#ifdef L_xopen
Rob Landleyd921b2e2006-08-03 15:41:12 +000088int xopen(const char *pathname, int flags)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000089{
Rob Landleyd921b2e2006-08-03 15:41:12 +000090 return xopen3(pathname, flags, 0777);
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +000091}
92#endif
93
94#ifdef L_xopen3
Rob Landleyd921b2e2006-08-03 15:41:12 +000095int xopen3(const char *pathname, int flags, int mode)
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +000096{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000097 int ret;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000098
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +000099 ret = open(pathname, flags, mode);
100 if (ret < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 bb_perror_msg_and_die("%s", pathname);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000102 }
103 return ret;
104}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000106
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107#ifdef L_xread
Rob Landley53437472006-07-16 08:14:35 +0000108
109// Die with an error message if we can't read the entire buffer.
110
111void xread(int fd, void *buf, size_t count)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000112{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113 while (count) {
Rob Landley53437472006-07-16 08:14:35 +0000114 ssize_t size;
115
116 if ((size = safe_read(fd, buf, count)) < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 bb_error_msg_and_die("Short read");
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118 count -= size;
Manuel Novoa III 948d4902004-03-08 05:44:30 +0000119 buf = ((char *) buf) + size;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000120 }
Rob Landley53437472006-07-16 08:14:35 +0000121}
122#endif
123
124#ifdef L_xwrite
125
126// Die with an error message if we can't write the entire buffer.
127
128void xwrite(int fd, void *buf, size_t count)
129{
130 while (count) {
131 ssize_t size;
132
133 if ((size = safe_write(fd, buf, count)) < 1)
134 bb_error_msg_and_die("Short write");
135 count -= size;
136 buf = ((char *) buf) + size;
137 }
138}
139#endif
140
141#ifdef L_xlseek
142
143// Die if we can't lseek to the right spot.
144
145void xlseek(int fd, off_t offset, int whence)
146{
Rob Landley2c55fca2006-08-04 05:24:58 +0000147 if (offset != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000148}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000150
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151#ifdef L_xread_char
Rob Landley53437472006-07-16 08:14:35 +0000152unsigned char xread_char(int fd)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000153{
154 char tmp;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000155
Rob Landley53437472006-07-16 08:14:35 +0000156 xread(fd, &tmp, 1);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000157
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000158 return(tmp);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000159}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000160#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000161
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162#ifdef L_xferror
Rob Landleyd921b2e2006-08-03 15:41:12 +0000163void xferror(FILE *fp, const char *fn)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000164{
165 if (ferror(fp)) {
166 bb_error_msg_and_die("%s", fn);
167 }
168}
169#endif
170
171#ifdef L_xferror_stdout
Rob Landleyd921b2e2006-08-03 15:41:12 +0000172void xferror_stdout(void)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000173{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000174 xferror(stdout, bb_msg_standard_output);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000175}
176#endif
177
178#ifdef L_xfflush_stdout
Rob Landleyd921b2e2006-08-03 15:41:12 +0000179void xfflush_stdout(void)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180{
181 if (fflush(stdout)) {
182 bb_perror_msg_and_die(bb_msg_standard_output);
183 }
184}
185#endif
Rob Landley399d2b52006-05-25 23:02:40 +0000186
187#ifdef L_spawn
188// This does a fork/exec in one call, using vfork().
Rob Landleyd921b2e2006-08-03 15:41:12 +0000189pid_t spawn(char **argv)
Rob Landley399d2b52006-05-25 23:02:40 +0000190{
191 static int failed;
192 pid_t pid;
Rob Landleyc7ddefc2006-06-14 01:24:33 +0000193 void *app = find_applet_by_name(argv[0]);
Rob Landley399d2b52006-05-25 23:02:40 +0000194
195 // Be nice to nommu machines.
196 failed = 0;
197 pid = vfork();
198 if (pid < 0) return pid;
199 if (!pid) {
Rob Landleyc7ddefc2006-06-14 01:24:33 +0000200 execvp(app ? CONFIG_BUSYBOX_EXEC_PATH : *argv, argv);
Rob Landley399d2b52006-05-25 23:02:40 +0000201
202 // We're sharing a stack with blocked parent, let parent know we failed
203 // and then exit to unblock parent (but don't run atexit() stuff, which
204 // would screw up parent.)
205
206 failed = -1;
207 _exit(0);
208 }
209 return failed ? failed : pid;
210}
211#endif
212
213#ifdef L_xspawn
Rob Landleyd921b2e2006-08-03 15:41:12 +0000214pid_t xspawn(char **argv)
Rob Landley399d2b52006-05-25 23:02:40 +0000215{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000216 pid_t pid = spawn(argv);
Rob Landley399d2b52006-05-25 23:02:40 +0000217 if (pid < 0) bb_perror_msg_and_die("%s", *argv);
218 return pid;
219}
220#endif
Rob Landleyc7ddefc2006-06-14 01:24:33 +0000221
222#ifdef L_wait4
223int wait4pid(int pid)
224{
225 int status;
226
227 if (pid == -1 || waitpid(pid, &status, 0) == -1) return -1;
228 if (WIFEXITED(status)) return WEXITSTATUS(status);
229 if (WIFSIGNALED(status)) return WTERMSIG(status);
230 return 0;
231}
Rob Landley5b88a382006-07-10 07:41:34 +0000232#endif
233
234#ifdef L_itoa
235// Largest 32 bit integer is -2 billion plus null terminator.
236// Int should always be 32 bits on a Unix-oid system, see
237// http://www.unix.org/whitepapers/64bit.html
238static char local_buf[12];
239
Rob Landley22d39582006-07-11 00:44:36 +0000240void utoa_to_buf(unsigned n, char *buf, unsigned buflen)
Rob Landley5b88a382006-07-10 07:41:34 +0000241{
242 int i, out = 0;
Rob Landley22d39582006-07-11 00:44:36 +0000243 if (buflen) {
244 for (i=1000000000; i; i/=10) {
245 int res = n/i;
Rob Landley5b88a382006-07-10 07:41:34 +0000246
Rob Landley22d39582006-07-11 00:44:36 +0000247 if ((res || out || i == 1) && --buflen>0) {
248 out++;
249 n -= res*i;
250 *buf++ = '0' + res;
251 }
Rob Landley5b88a382006-07-10 07:41:34 +0000252 }
Rob Landley22d39582006-07-11 00:44:36 +0000253 *buf = 0;
Rob Landley5b88a382006-07-10 07:41:34 +0000254 }
Rob Landley5b88a382006-07-10 07:41:34 +0000255}
256
257// Note: uses static buffer, calling it twice in a row will overwrite.
258
259char *utoa(unsigned n)
260{
261 utoa_to_buf(n, local_buf, sizeof(local_buf));
262
263 return local_buf;
264}
265
Rob Landley22d39582006-07-11 00:44:36 +0000266void itoa_to_buf(int n, char *buf, unsigned buflen)
Rob Landley5b88a382006-07-10 07:41:34 +0000267{
Rob Landley22d39582006-07-11 00:44:36 +0000268 if (buflen && n<0) {
Rob Landley5b88a382006-07-10 07:41:34 +0000269 n = -n;
270 *buf++ = '-';
Rob Landley22d39582006-07-11 00:44:36 +0000271 buflen--;
Rob Landley5b88a382006-07-10 07:41:34 +0000272 }
273 utoa_to_buf((unsigned)n, buf, buflen);
274}
275
276// Note: uses static buffer, calling it twice in a row will overwrite.
277
278char *itoa(int n)
279{
280 itoa_to_buf(n, local_buf, sizeof(local_buf));
281
282 return local_buf;
283}
284#endif
Rob Landleydf822f22006-07-15 23:00:46 +0000285
286#ifdef L_setuid
287void xsetgid(gid_t gid)
288{
289 if (setgid(gid)) bb_error_msg_and_die("setgid");
290}
291
292void xsetuid(uid_t uid)
293{
294 if (setuid(uid)) bb_error_msg_and_die("setuid");
295}
296#endif
Rob Landley53437472006-07-16 08:14:35 +0000297
298#ifdef L_fdlength
299off_t fdlength(int fd)
300{
301 off_t bottom = 0, top = 0, pos;
302 long size;
303
304 // If the ioctl works for this, return it.
305
306 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
307
308 // If not, do a binary search for the last location we can read.
309
310 do {
311 char temp;
312
313 pos = bottom + (top - bottom) / 2;;
314
315 // If we can read from the current location, it's bigger.
316
317 if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) {
318 if (bottom == top) bottom = top = (top+1) * 2;
319 else bottom = pos;
320
321 // If we can't, it's smaller.
322
323 } else {
324 if (bottom == top) {
325 if (!top) return 0;
326 bottom = top/2;
327 }
328 else top = pos;
329 }
330 } while (bottom + 1 != top);
331
332 return pos + 1;
333}
334#endif
Rob Landleyd921b2e2006-08-03 15:41:12 +0000335
336#ifdef L_xasprintf
337char *xasprintf(const char *format, ...)
338{
339 va_list p;
340 int r;
341 char *string_ptr;
342
343#if 1
344 // GNU extension
345 va_start(p, format);
346 r = vasprintf(&string_ptr, format, p);
347 va_end(p);
348#else
349 // Bloat for systems that haven't got the GNU extension.
350 va_start(p, format);
351 r = vsnprintf(NULL, 0, format, p);
352 va_end(p);
353 string_ptr = xmalloc(r+1);
354 va_start(p, format);
355 r = vsnprintf(string_ptr, r+1, format, p);
356 va_end(p);
357#endif
358
359 if (r < 0) bb_perror_msg_and_die("xasprintf");
360 return string_ptr;
361}
362#endif
363
364#ifdef L_xprint_and_close_file
365void xprint_and_close_file(FILE *file)
366{
367 // copyfd outputs error messages for us.
368 if (bb_copyfd_eof(fileno(file), 1) == -1) exit(bb_default_error_retval);
369
370 fclose(file);
371}
372#endif
373
374#ifdef L_xchdir
375void xchdir(const char *path)
376{
377 if (chdir(path))
378 bb_perror_msg_and_die("chdir(%s)", path);
379}
380#endif
381
382#ifdef L_warn_opendir
383DIR *warn_opendir(const char *path)
384{
385 DIR *dp;
386
387 if ((dp = opendir(path)) == NULL) {
388 bb_perror_msg("unable to open `%s'", path);
389 return NULL;
390 }
391 return dp;
392}
393#endif
394
395#ifdef L_xopendir
396DIR *xopendir(const char *path)
397{
398 DIR *dp;
399
400 if ((dp = opendir(path)) == NULL)
401 bb_perror_msg_and_die("unable to open `%s'", path);
402 return dp;
403}
404#endif
405
406#ifdef L_xdaemon
407#ifndef BB_NOMMU
408void xdaemon(int nochdir, int noclose)
409{
410 if (daemon(nochdir, noclose)) bb_perror_msg_and_die("daemon");
411}
412#endif
413#endif
414
415#ifdef L_xbind
416void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
417{
418 if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind");
419}
420#endif
421
422#ifdef L_xsocket
423int xsocket(int domain, int type, int protocol)
424{
425 int r = socket(domain, type, protocol);
426
427 if (r < 0) bb_perror_msg_and_die("socket");
428
429 return r;
430}
431#endif
432
433#ifdef L_xlisten
434void xlisten(int s, int backlog)
435{
436 if (listen(s, backlog)) bb_perror_msg_and_die("listen");
437}
438#endif