Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
Rob Landley | 4e9deec | 2006-02-20 02:44:30 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 10 | #include <sys/types.h> |
| 11 | #include <sys/stat.h> |
Rob Landley | c7ddefc | 2006-06-14 01:24:33 +0000 | [diff] [blame^] | 12 | #include <sys/wait.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <unistd.h> |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 17 | #include <fcntl.h> |
Rob Landley | 552b56d | 2006-05-04 21:22:27 +0000 | [diff] [blame] | 18 | #include "busybox.h" |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 19 | |
| 20 | #ifndef DMALLOC |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 21 | #ifdef L_xmalloc |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 22 | void *xmalloc(size_t size) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 23 | { |
| 24 | void *ptr = malloc(size); |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 25 | if (ptr == NULL && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 27 | return ptr; |
| 28 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 29 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 30 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 31 | #ifdef L_xrealloc |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 32 | void *xrealloc(void *ptr, size_t size) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 33 | { |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 34 | ptr = realloc(ptr, size); |
| 35 | if (ptr == NULL && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 36 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 37 | return ptr; |
| 38 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 39 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 40 | |
Rob Landley | 80b8ff0 | 2006-05-19 20:36:49 +0000 | [diff] [blame] | 41 | #ifdef L_xzalloc |
| 42 | void *xzalloc(size_t size) |
| 43 | { |
| 44 | void *ptr = xmalloc(size); |
| 45 | memset(ptr, 0, size); |
| 46 | return ptr; |
| 47 | } |
| 48 | #endif |
| 49 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 50 | #ifdef L_xcalloc |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 51 | void *xcalloc(size_t nmemb, size_t size) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 52 | { |
| 53 | void *ptr = calloc(nmemb, size); |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 54 | if (ptr == NULL && nmemb != 0 && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 55 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 56 | return ptr; |
| 57 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | #endif |
Eric Andersen | 9f894f4 | 2003-07-05 22:15:43 +0000 | [diff] [blame] | 59 | #endif /* DMALLOC */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 60 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 61 | #ifdef L_xstrdup |
Rob Landley | 31642d7 | 2006-03-14 21:45:38 +0000 | [diff] [blame] | 62 | char * bb_xstrdup (const char *s) |
| 63 | { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 64 | char *t; |
| 65 | |
| 66 | if (s == NULL) |
| 67 | return NULL; |
| 68 | |
| 69 | t = strdup (s); |
| 70 | |
| 71 | if (t == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 73 | |
| 74 | return t; |
| 75 | } |
| 76 | #endif |
| 77 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 78 | #ifdef L_xstrndup |
Rob Landley | 31642d7 | 2006-03-14 21:45:38 +0000 | [diff] [blame] | 79 | char * bb_xstrndup (const char *s, int n) |
| 80 | { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 81 | char *t; |
| 82 | |
| 83 | if (s == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 84 | bb_error_msg_and_die("bb_xstrndup bug"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 85 | |
| 86 | t = xmalloc(++n); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 87 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 88 | return safe_strncpy(t,s,n); |
| 89 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 90 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 91 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 92 | #ifdef L_xfopen |
| 93 | FILE *bb_xfopen(const char *path, const char *mode) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 94 | { |
| 95 | FILE *fp; |
| 96 | if ((fp = fopen(path, mode)) == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 97 | bb_perror_msg_and_die("%s", path); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 98 | return fp; |
| 99 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 100 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 101 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | #ifdef L_xopen |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 103 | int bb_xopen(const char *pathname, int flags) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 104 | { |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 105 | return bb_xopen3(pathname, flags, 0777); |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | #ifdef L_xopen3 |
| 110 | int bb_xopen3(const char *pathname, int flags, int mode) |
| 111 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 112 | int ret; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 113 | |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 114 | ret = open(pathname, flags, mode); |
| 115 | if (ret < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 116 | bb_perror_msg_and_die("%s", pathname); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 117 | } |
| 118 | return ret; |
| 119 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 120 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 121 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 122 | #ifdef L_xread |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 123 | ssize_t bb_xread(int fd, void *buf, size_t count) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 124 | { |
| 125 | ssize_t size; |
| 126 | |
| 127 | size = read(fd, buf, count); |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 128 | if (size < 0) { |
"Vladimir N. Oleynik" | 4eb2fd6 | 2005-11-25 11:36:36 +0000 | [diff] [blame] | 129 | bb_perror_msg_and_die(bb_msg_read_error); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 130 | } |
| 131 | return(size); |
| 132 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 133 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 134 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 135 | #ifdef L_xread_all |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 136 | void bb_xread_all(int fd, void *buf, size_t count) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 137 | { |
| 138 | ssize_t size; |
| 139 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 140 | while (count) { |
| 141 | if ((size = bb_xread(fd, buf, count)) == 0) { /* EOF */ |
| 142 | bb_error_msg_and_die("Short read"); |
| 143 | } |
| 144 | count -= size; |
Manuel Novoa III | 948d490 | 2004-03-08 05:44:30 +0000 | [diff] [blame] | 145 | buf = ((char *) buf) + size; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 146 | } |
| 147 | return; |
| 148 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 149 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 150 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 151 | #ifdef L_xread_char |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 152 | unsigned char bb_xread_char(int fd) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 153 | { |
| 154 | char tmp; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 155 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 156 | bb_xread_all(fd, &tmp, 1); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 157 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 158 | return(tmp); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 159 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 160 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 161 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 162 | #ifdef L_xferror |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 163 | void bb_xferror(FILE *fp, const char *fn) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 164 | { |
| 165 | if (ferror(fp)) { |
| 166 | bb_error_msg_and_die("%s", fn); |
| 167 | } |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | #ifdef L_xferror_stdout |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 172 | void bb_xferror_stdout(void) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 173 | { |
| 174 | bb_xferror(stdout, bb_msg_standard_output); |
| 175 | } |
| 176 | #endif |
| 177 | |
| 178 | #ifdef L_xfflush_stdout |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 179 | void bb_xfflush_stdout(void) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 180 | { |
| 181 | if (fflush(stdout)) { |
| 182 | bb_perror_msg_and_die(bb_msg_standard_output); |
| 183 | } |
| 184 | } |
| 185 | #endif |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 186 | |
| 187 | #ifdef L_spawn |
| 188 | // This does a fork/exec in one call, using vfork(). |
| 189 | pid_t bb_spawn(char **argv) |
| 190 | { |
| 191 | static int failed; |
| 192 | pid_t pid; |
Rob Landley | c7ddefc | 2006-06-14 01:24:33 +0000 | [diff] [blame^] | 193 | void *app = find_applet_by_name(argv[0]); |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 194 | |
| 195 | // Be nice to nommu machines. |
| 196 | failed = 0; |
| 197 | pid = vfork(); |
| 198 | if (pid < 0) return pid; |
| 199 | if (!pid) { |
Rob Landley | c7ddefc | 2006-06-14 01:24:33 +0000 | [diff] [blame^] | 200 | execvp(app ? CONFIG_BUSYBOX_EXEC_PATH : *argv, argv); |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 201 | |
| 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 |
| 214 | pid_t bb_xspawn(char **argv) |
| 215 | { |
| 216 | pid_t pid = bb_spawn(argv); |
| 217 | if (pid < 0) bb_perror_msg_and_die("%s", *argv); |
| 218 | return pid; |
| 219 | } |
| 220 | #endif |
Rob Landley | c7ddefc | 2006-06-14 01:24:33 +0000 | [diff] [blame^] | 221 | |
| 222 | #ifdef L_wait4 |
| 223 | int 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 | } |
| 232 | #endif |