vfork_daemon_rexec.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Rexec program for system have fork() as vfork() with foreground option
  3. *
  4. * Copyright (C) Vladimir N. Oleynik <dzo@simtreas.ru>
  5. * Copyright (C) 2003 Russ Dill <Russ.Dill@asu.edu>
  6. *
  7. * daemon() portion taken from uClibc:
  8. *
  9. * Copyright (c) 1991, 1993
  10. * The Regents of the University of California. All rights reserved.
  11. *
  12. * Modified for uClibc by Erik Andersen <andersee@debian.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. */
  28. #include <unistd.h>
  29. #include <stdio.h>
  30. #include <fcntl.h>
  31. #include <paths.h>
  32. #include "libbb.h"
  33. #if defined(__uClinux__)
  34. void vfork_daemon_rexec(int nochdir, int noclose,
  35. int argc, char **argv, char *foreground_opt)
  36. {
  37. int fd;
  38. char **vfork_args;
  39. int a = 0;
  40. setsid();
  41. if (!nochdir)
  42. chdir("/");
  43. if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
  44. dup2(fd, STDIN_FILENO);
  45. dup2(fd, STDOUT_FILENO);
  46. dup2(fd, STDERR_FILENO);
  47. if (fd > 2)
  48. close(fd);
  49. }
  50. vfork_args = xcalloc(sizeof(char *), argc + 3);
  51. vfork_args[a++] = "/bin/busybox";
  52. while(*argv) {
  53. vfork_args[a++] = *argv;
  54. argv++;
  55. }
  56. vfork_args[a] = foreground_opt;
  57. switch (vfork()) {
  58. case 0: /* child */
  59. /* Make certain we are not a session leader, or else we
  60. * might reacquire a controlling terminal */
  61. if (vfork())
  62. _exit(0);
  63. execv(vfork_args[0], vfork_args);
  64. bb_perror_msg_and_die("execv %s", vfork_args[0]);
  65. case -1: /* error */
  66. bb_perror_msg_and_die("vfork");
  67. default: /* parent */
  68. exit(0);
  69. }
  70. }
  71. #endif /* uClinux */