Rob Landley | d390493 | 2013-07-16 00:04:56 -0500 | [diff] [blame] | 1 | /* xwrap.c - wrappers around existing library functions. |
| 2 | * |
| 3 | * Functions with the x prefix are wrappers that either succeed or kill the |
| 4 | * program with an error message, but never return failure. They usually have |
| 5 | * the same arguments and return value as the function they wrap. |
| 6 | * |
| 7 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 8 | */ |
| 9 | |
| 10 | #include "toys.h" |
| 11 | |
| 12 | // Strcpy with size checking: exit if there's not enough space for the string. |
| 13 | void xstrncpy(char *dest, char *src, size_t size) |
| 14 | { |
Rob Landley | 3704f82 | 2013-11-02 14:24:54 -0500 | [diff] [blame] | 15 | if (strlen(src)+1 > size) error_exit("'%s' > %ld bytes", src, (long)size); |
Rob Landley | d390493 | 2013-07-16 00:04:56 -0500 | [diff] [blame] | 16 | strcpy(dest, src); |
| 17 | } |
| 18 | |
| 19 | void xexit(void) |
| 20 | { |
| 21 | if (toys.rebound) longjmp(*toys.rebound, 1); |
| 22 | else exit(toys.exitval); |
| 23 | } |
| 24 | |
| 25 | // Die unless we can allocate memory. |
| 26 | void *xmalloc(size_t size) |
| 27 | { |
| 28 | void *ret = malloc(size); |
| 29 | if (!ret) error_exit("xmalloc"); |
| 30 | |
| 31 | return ret; |
| 32 | } |
| 33 | |
| 34 | // Die unless we can allocate prezeroed memory. |
| 35 | void *xzalloc(size_t size) |
| 36 | { |
| 37 | void *ret = xmalloc(size); |
| 38 | memset(ret, 0, size); |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | // Die unless we can change the size of an existing allocation, possibly |
| 43 | // moving it. (Notice different arguments from libc function.) |
| 44 | void *xrealloc(void *ptr, size_t size) |
| 45 | { |
| 46 | ptr = realloc(ptr, size); |
| 47 | if (!ptr) error_exit("xrealloc"); |
| 48 | |
| 49 | return ptr; |
| 50 | } |
| 51 | |
| 52 | // Die unless we can allocate a copy of this many bytes of string. |
| 53 | char *xstrndup(char *s, size_t n) |
| 54 | { |
| 55 | char *ret = xmalloc(++n); |
| 56 | strncpy(ret, s, n); |
| 57 | ret[--n]=0; |
| 58 | |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | // Die unless we can allocate a copy of this string. |
| 63 | char *xstrdup(char *s) |
| 64 | { |
| 65 | return xstrndup(s, strlen(s)); |
| 66 | } |
| 67 | |
| 68 | // Die unless we can allocate enough space to sprintf() into. |
| 69 | char *xmsprintf(char *format, ...) |
| 70 | { |
| 71 | va_list va, va2; |
| 72 | int len; |
| 73 | char *ret; |
| 74 | |
| 75 | va_start(va, format); |
| 76 | va_copy(va2, va); |
| 77 | |
| 78 | // How long is it? |
| 79 | len = vsnprintf(0, 0, format, va); |
| 80 | len++; |
| 81 | va_end(va); |
| 82 | |
| 83 | // Allocate and do the sprintf() |
| 84 | ret = xmalloc(len); |
| 85 | vsnprintf(ret, len, format, va2); |
| 86 | va_end(va2); |
| 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | void xprintf(char *format, ...) |
| 92 | { |
| 93 | va_list va; |
| 94 | va_start(va, format); |
| 95 | |
| 96 | vprintf(format, va); |
| 97 | if (ferror(stdout)) perror_exit("write"); |
| 98 | } |
| 99 | |
| 100 | void xputs(char *s) |
| 101 | { |
| 102 | if (EOF == puts(s) || fflush(stdout)) perror_exit("write"); |
| 103 | } |
| 104 | |
| 105 | void xputc(char c) |
| 106 | { |
| 107 | if (EOF == fputc(c, stdout) || fflush(stdout)) perror_exit("write"); |
| 108 | } |
| 109 | |
| 110 | void xflush(void) |
| 111 | { |
| 112 | if (fflush(stdout)) perror_exit("write");; |
| 113 | } |
| 114 | |
Rob Landley | 7275667 | 2013-07-17 17:22:46 -0500 | [diff] [blame] | 115 | // Call xexec with a chunk of optargs, starting at skip. (You can't just |
| 116 | // call xexec() directly because toy_init() frees optargs.) |
| 117 | void xexec_optargs(int skip) |
| 118 | { |
| 119 | char **s = toys.optargs; |
| 120 | |
| 121 | toys.optargs = 0; |
| 122 | xexec(s+skip); |
| 123 | } |
| 124 | |
| 125 | |
Rob Landley | d390493 | 2013-07-16 00:04:56 -0500 | [diff] [blame] | 126 | // Die unless we can exec argv[] (or run builtin command). Note that anything |
| 127 | // with a path isn't a builtin, so /bin/sh won't match the builtin sh. |
| 128 | void xexec(char **argv) |
| 129 | { |
Rob Landley | d04dc1f | 2013-08-30 01:53:31 -0500 | [diff] [blame] | 130 | if (CFG_TOYBOX) toy_exec(argv); |
Rob Landley | d390493 | 2013-07-16 00:04:56 -0500 | [diff] [blame] | 131 | execvp(argv[0], argv); |
| 132 | |
| 133 | perror_exit("exec %s", argv[0]); |
| 134 | } |
| 135 | |
| 136 | void xaccess(char *path, int flags) |
| 137 | { |
| 138 | if (access(path, flags)) perror_exit("Can't access '%s'", path); |
| 139 | } |
| 140 | |
| 141 | // Die unless we can delete a file. (File must exist to be deleted.) |
| 142 | void xunlink(char *path) |
| 143 | { |
| 144 | if (unlink(path)) perror_exit("unlink '%s'", path); |
| 145 | } |
| 146 | |
| 147 | // Die unless we can open/create a file, returning file descriptor. |
| 148 | int xcreate(char *path, int flags, int mode) |
| 149 | { |
| 150 | int fd = open(path, flags, mode); |
| 151 | if (fd == -1) perror_exit("%s", path); |
| 152 | return fd; |
| 153 | } |
| 154 | |
| 155 | // Die unless we can open a file, returning file descriptor. |
| 156 | int xopen(char *path, int flags) |
| 157 | { |
| 158 | return xcreate(path, flags, 0); |
| 159 | } |
| 160 | |
| 161 | void xclose(int fd) |
| 162 | { |
| 163 | if (close(fd)) perror_exit("xclose"); |
| 164 | } |
| 165 | |
| 166 | int xdup(int fd) |
| 167 | { |
| 168 | if (fd != -1) { |
| 169 | fd = dup(fd); |
| 170 | if (fd == -1) perror_exit("xdup"); |
| 171 | } |
| 172 | return fd; |
| 173 | } |
| 174 | |
Rob Landley | 1aa7511 | 2013-08-07 12:19:51 -0500 | [diff] [blame] | 175 | FILE *xfdopen(int fd, char *mode) |
| 176 | { |
| 177 | FILE *f = fdopen(fd, mode); |
| 178 | |
| 179 | if (!f) perror_exit("xfdopen"); |
| 180 | |
| 181 | return f; |
| 182 | } |
| 183 | |
Rob Landley | d390493 | 2013-07-16 00:04:56 -0500 | [diff] [blame] | 184 | // Die unless we can open/create a file, returning FILE *. |
| 185 | FILE *xfopen(char *path, char *mode) |
| 186 | { |
| 187 | FILE *f = fopen(path, mode); |
| 188 | if (!f) perror_exit("No file %s", path); |
| 189 | return f; |
| 190 | } |
| 191 | |
| 192 | // Die if there's an error other than EOF. |
| 193 | size_t xread(int fd, void *buf, size_t len) |
| 194 | { |
| 195 | ssize_t ret = read(fd, buf, len); |
| 196 | if (ret < 0) perror_exit("xread"); |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | void xreadall(int fd, void *buf, size_t len) |
| 202 | { |
| 203 | if (len != readall(fd, buf, len)) perror_exit("xreadall"); |
| 204 | } |
| 205 | |
| 206 | // There's no xwriteall(), just xwrite(). When we read, there may or may not |
| 207 | // be more data waiting. When we write, there is data and it had better go |
| 208 | // somewhere. |
| 209 | |
| 210 | void xwrite(int fd, void *buf, size_t len) |
| 211 | { |
| 212 | if (len != writeall(fd, buf, len)) perror_exit("xwrite"); |
| 213 | } |
| 214 | |
| 215 | // Die if lseek fails, probably due to being called on a pipe. |
| 216 | |
| 217 | off_t xlseek(int fd, off_t offset, int whence) |
| 218 | { |
| 219 | offset = lseek(fd, offset, whence); |
| 220 | if (offset<0) perror_exit("lseek"); |
| 221 | |
| 222 | return offset; |
| 223 | } |
| 224 | |
| 225 | char *xgetcwd(void) |
| 226 | { |
| 227 | char *buf = getcwd(NULL, 0); |
| 228 | if (!buf) perror_exit("xgetcwd"); |
| 229 | |
| 230 | return buf; |
| 231 | } |
| 232 | |
| 233 | void xstat(char *path, struct stat *st) |
| 234 | { |
| 235 | if(stat(path, st)) perror_exit("Can't stat %s", path); |
| 236 | } |
| 237 | |
| 238 | // Cannonicalize path, even to file with one or more missing components at end. |
| 239 | // if exact, require last path component to exist |
| 240 | char *xabspath(char *path, int exact) |
| 241 | { |
| 242 | struct string_list *todo, *done = 0; |
| 243 | int try = 9999, dirfd = open("/", 0);; |
| 244 | char buf[4096], *ret; |
| 245 | |
| 246 | // If this isn't an absolute path, start with cwd. |
| 247 | if (*path != '/') { |
| 248 | char *temp = xgetcwd(); |
| 249 | |
| 250 | splitpath(path, splitpath(temp, &todo)); |
| 251 | free(temp); |
| 252 | } else splitpath(path, &todo); |
| 253 | |
| 254 | // Iterate through path components |
| 255 | while (todo) { |
| 256 | struct string_list *new = llist_pop(&todo), **tail; |
| 257 | ssize_t len; |
| 258 | |
| 259 | if (!try--) { |
| 260 | errno = ELOOP; |
| 261 | goto error; |
| 262 | } |
| 263 | |
| 264 | // Removable path componenents. |
| 265 | if (!strcmp(new->str, ".") || !strcmp(new->str, "..")) { |
| 266 | int x = new->str[1]; |
| 267 | |
| 268 | free(new); |
| 269 | if (x) { |
| 270 | if (done) free(llist_pop(&done)); |
| 271 | len = 0; |
| 272 | } else continue; |
| 273 | |
| 274 | // Is this a symlink? |
| 275 | } else len=readlinkat(dirfd, new->str, buf, 4096); |
| 276 | |
| 277 | if (len>4095) goto error; |
| 278 | if (len<1) { |
| 279 | int fd; |
| 280 | char *s = ".."; |
| 281 | |
| 282 | // For .. just move dirfd |
| 283 | if (len) { |
| 284 | // Not a symlink: add to linked list, move dirfd, fail if error |
| 285 | if ((exact || todo) && errno != EINVAL) goto error; |
| 286 | new->next = done; |
| 287 | done = new; |
| 288 | if (errno == EINVAL && !todo) break; |
| 289 | s = new->str; |
| 290 | } |
| 291 | fd = openat(dirfd, s, 0); |
| 292 | if (fd == -1 && (exact || todo || errno != ENOENT)) goto error; |
| 293 | close(dirfd); |
| 294 | dirfd = fd; |
| 295 | continue; |
| 296 | } |
| 297 | |
| 298 | // If this symlink is to an absolute path, discard existing resolved path |
| 299 | buf[len] = 0; |
| 300 | if (*buf == '/') { |
| 301 | llist_traverse(done, free); |
| 302 | done=0; |
| 303 | close(dirfd); |
| 304 | dirfd = open("/", 0); |
| 305 | } |
| 306 | free(new); |
| 307 | |
| 308 | // prepend components of new path. Note symlink to "/" will leave new NULL |
| 309 | tail = splitpath(buf, &new); |
| 310 | |
| 311 | // symlink to "/" will return null and leave tail alone |
| 312 | if (new) { |
| 313 | *tail = todo; |
| 314 | todo = new; |
| 315 | } |
| 316 | } |
| 317 | close(dirfd); |
| 318 | |
| 319 | // At this point done has the path, in reverse order. Reverse list while |
| 320 | // calculating buffer length. |
| 321 | |
| 322 | try = 2; |
| 323 | while (done) { |
| 324 | struct string_list *temp = llist_pop(&done);; |
| 325 | |
| 326 | if (todo) try++; |
| 327 | try += strlen(temp->str); |
| 328 | temp->next = todo; |
| 329 | todo = temp; |
| 330 | } |
| 331 | |
| 332 | // Assemble return buffer |
| 333 | |
| 334 | ret = xmalloc(try); |
| 335 | *ret = '/'; |
| 336 | ret [try = 1] = 0; |
| 337 | while (todo) { |
| 338 | if (try>1) ret[try++] = '/'; |
| 339 | try = stpcpy(ret+try, todo->str) - ret; |
| 340 | free(llist_pop(&todo)); |
| 341 | } |
| 342 | |
| 343 | return ret; |
| 344 | |
| 345 | error: |
| 346 | close(dirfd); |
| 347 | llist_traverse(todo, free); |
| 348 | llist_traverse(done, free); |
| 349 | |
| 350 | return NULL; |
| 351 | } |
| 352 | |
| 353 | // Resolve all symlinks, returning malloc() memory. |
| 354 | char *xrealpath(char *path) |
| 355 | { |
| 356 | char *new = realpath(path, NULL); |
| 357 | if (!new) perror_exit("realpath '%s'", path); |
| 358 | return new; |
| 359 | } |
| 360 | |
| 361 | void xchdir(char *path) |
| 362 | { |
| 363 | if (chdir(path)) error_exit("chdir '%s'", path); |
| 364 | } |
| 365 | |
| 366 | // Ensure entire path exists. |
| 367 | // If mode != -1 set permissions on newly created dirs. |
| 368 | // Requires that path string be writable (for temporary null terminators). |
| 369 | void xmkpath(char *path, int mode) |
| 370 | { |
| 371 | char *p, old; |
| 372 | mode_t mask; |
| 373 | int rc; |
| 374 | struct stat st; |
| 375 | |
| 376 | for (p = path; ; p++) { |
| 377 | if (!*p || *p == '/') { |
| 378 | old = *p; |
| 379 | *p = rc = 0; |
| 380 | if (stat(path, &st) || !S_ISDIR(st.st_mode)) { |
| 381 | if (mode != -1) { |
| 382 | mask=umask(0); |
| 383 | rc = mkdir(path, mode); |
| 384 | umask(mask); |
| 385 | } else rc = mkdir(path, 0777); |
| 386 | } |
| 387 | *p = old; |
| 388 | if(rc) perror_exit("mkpath '%s'", path); |
| 389 | } |
| 390 | if (!*p) break; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // setuid() can fail (for example, too many processes belonging to that user), |
| 395 | // which opens a security hole if the process continues as the original user. |
| 396 | |
| 397 | void xsetuid(uid_t uid) |
| 398 | { |
| 399 | if (setuid(uid)) perror_exit("xsetuid"); |
| 400 | } |
| 401 | |
Rob Landley | 9e44a58 | 2013-11-28 20:18:04 -0600 | [diff] [blame^] | 402 | struct passwd *xgetpwuid(uid_t uid) |
| 403 | { |
| 404 | struct passwd *pwd = getpwuid(uid); |
| 405 | if (!pwd) error_exit(NULL); |
| 406 | return pwd; |
| 407 | } |
| 408 | |
| 409 | struct group *xgetgrgid(gid_t gid) |
| 410 | { |
| 411 | struct group *group = getgrgid(gid); |
| 412 | if (!group) error_exit(NULL); |
| 413 | return group; |
| 414 | } |
| 415 | |
Rob Landley | d390493 | 2013-07-16 00:04:56 -0500 | [diff] [blame] | 416 | // This can return null (meaning file not found). It just won't return null |
| 417 | // for memory allocation reasons. |
| 418 | char *xreadlink(char *name) |
| 419 | { |
| 420 | int len, size = 0; |
| 421 | char *buf = 0; |
| 422 | |
| 423 | // Grow by 64 byte chunks until it's big enough. |
| 424 | for(;;) { |
| 425 | size +=64; |
| 426 | buf = xrealloc(buf, size); |
| 427 | len = readlink(name, buf, size); |
| 428 | |
| 429 | if (len<0) { |
| 430 | free(buf); |
| 431 | return 0; |
| 432 | } |
| 433 | if (len<size) { |
| 434 | buf[len]=0; |
| 435 | return buf; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | char *xreadfile(char *name) |
| 441 | { |
Rob Landley | 8fdcfdb | 2013-09-03 17:56:28 -0500 | [diff] [blame] | 442 | char *buf = readfile(name, 0, 0); |
Rob Landley | d390493 | 2013-07-16 00:04:56 -0500 | [diff] [blame] | 443 | if (!buf) perror_exit("xreadfile %s", name); |
| 444 | return buf; |
| 445 | } |
| 446 | |
| 447 | int xioctl(int fd, int request, void *data) |
| 448 | { |
| 449 | int rc; |
| 450 | |
| 451 | errno = 0; |
| 452 | rc = ioctl(fd, request, data); |
| 453 | if (rc == -1 && errno) perror_exit("ioctl %x", request); |
| 454 | |
| 455 | return rc; |
| 456 | } |
| 457 | |
| 458 | // Open a /var/run/NAME.pid file, dying if we can't write it or if it currently |
| 459 | // exists and is this executable. |
| 460 | void xpidfile(char *name) |
| 461 | { |
| 462 | char pidfile[256], spid[32]; |
| 463 | int i, fd; |
| 464 | pid_t pid; |
| 465 | |
| 466 | sprintf(pidfile, "/var/run/%s.pid", name); |
| 467 | // Try three times to open the sucker. |
| 468 | for (i=0; i<3; i++) { |
Felix Janda | dccfb2a | 2013-08-26 21:55:33 +0200 | [diff] [blame] | 469 | fd = open(pidfile, O_CREAT|O_EXCL|O_WRONLY, 0644); |
Rob Landley | d390493 | 2013-07-16 00:04:56 -0500 | [diff] [blame] | 470 | if (fd != -1) break; |
| 471 | |
| 472 | // If it already existed, read it. Loop for race condition. |
| 473 | fd = open(pidfile, O_RDONLY); |
| 474 | if (fd == -1) continue; |
| 475 | |
| 476 | // Is the old program still there? |
| 477 | spid[xread(fd, spid, sizeof(spid)-1)] = 0; |
| 478 | close(fd); |
| 479 | pid = atoi(spid); |
Rob Landley | 46e8e1d | 2013-09-06 04:45:36 -0500 | [diff] [blame] | 480 | if (pid < 1 || (kill(pid, 0) && errno == ESRCH)) unlink(pidfile); |
Rob Landley | d390493 | 2013-07-16 00:04:56 -0500 | [diff] [blame] | 481 | |
| 482 | // An else with more sanity checking might be nice here. |
| 483 | } |
| 484 | |
| 485 | if (i == 3) error_exit("xpidfile %s", name); |
| 486 | |
| 487 | xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid())); |
| 488 | close(fd); |
| 489 | } |
| 490 | |
| 491 | // Copy the rest of in to out and close both files. |
| 492 | |
| 493 | void xsendfile(int in, int out) |
| 494 | { |
| 495 | long len; |
| 496 | char buf[4096]; |
| 497 | |
| 498 | if (in<0) return; |
| 499 | for (;;) { |
| 500 | len = xread(in, buf, 4096); |
| 501 | if (len<1) break; |
| 502 | xwrite(out, buf, len); |
| 503 | } |
| 504 | } |
Rob Landley | 7275667 | 2013-07-17 17:22:46 -0500 | [diff] [blame] | 505 | |
| 506 | // parse fractional seconds with optional s/m/h/d suffix |
| 507 | long xparsetime(char *arg, long units, long *fraction) |
| 508 | { |
| 509 | double d; |
| 510 | long l; |
| 511 | |
| 512 | if (CFG_TOYBOX_FLOAT) d = strtod(arg, &arg); |
| 513 | else l = strtoul(arg, &arg, 10); |
| 514 | |
| 515 | // Parse suffix |
| 516 | if (*arg) { |
| 517 | int ismhd[]={1,60,3600,86400}, i = stridx("smhd", *arg); |
| 518 | |
| 519 | if (i == -1) error_exit("Unknown suffix '%c'", *arg); |
| 520 | if (CFG_TOYBOX_FLOAT) d *= ismhd[i]; |
| 521 | else l *= ismhd[i]; |
| 522 | } |
| 523 | |
| 524 | if (CFG_TOYBOX_FLOAT) { |
| 525 | l = (long)d; |
| 526 | if (fraction) *fraction = units*(d-l); |
| 527 | } else if (fraction) *fraction = 0; |
| 528 | |
| 529 | return l; |
| 530 | } |