3
0

vfork_daemon_rexec.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Rexec program for system have fork() as vfork() with foreground option
  4. *
  5. * Copyright (C) Vladimir N. Oleynik <dzo@simtreas.ru>
  6. * Copyright (C) 2003 Russ Dill <Russ.Dill@asu.edu>
  7. *
  8. * daemon() portion taken from uClibc:
  9. *
  10. * Copyright (c) 1991, 1993
  11. * The Regents of the University of California. All rights reserved.
  12. *
  13. * Modified for uClibc by Erik Andersen <andersee@debian.org>
  14. *
  15. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  16. */
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <fcntl.h>
  20. #include <paths.h>
  21. #include "libbb.h"
  22. #ifdef BB_NOMMU
  23. void vfork_daemon_rexec(int nochdir, int noclose,
  24. int argc, char **argv, char *foreground_opt)
  25. {
  26. int fd;
  27. char **vfork_args;
  28. int a = 0;
  29. setsid();
  30. if (!nochdir)
  31. xchdir("/");
  32. if (!noclose && (fd = open(bb_dev_null, O_RDWR, 0)) != -1) {
  33. dup2(fd, STDIN_FILENO);
  34. dup2(fd, STDOUT_FILENO);
  35. dup2(fd, STDERR_FILENO);
  36. if (fd > 2)
  37. close(fd);
  38. }
  39. vfork_args = xcalloc(sizeof(char *), argc + 3);
  40. vfork_args[a++] = CONFIG_BUSYBOX_EXEC_PATH;
  41. while(*argv) {
  42. vfork_args[a++] = *argv;
  43. argv++;
  44. }
  45. vfork_args[a] = foreground_opt;
  46. switch (vfork()) {
  47. case 0: /* child */
  48. /* Make certain we are not a session leader, or else we
  49. * might reacquire a controlling terminal */
  50. if (vfork())
  51. _exit(0);
  52. execv(vfork_args[0], vfork_args);
  53. bb_perror_msg_and_die("execv %s", vfork_args[0]);
  54. case -1: /* error */
  55. bb_perror_msg_and_die("vfork");
  56. default: /* parent */
  57. exit(0);
  58. }
  59. }
  60. #endif /* BB_NOMMU */