nohup.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* vi: set sw=4 ts=4: */
  2. /* nohup - invoke a utility immune to hangups.
  3. *
  4. * Busybox version based on nohup specification at
  5. * http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html
  6. *
  7. * Copyright 2006 Rob Landley <rob@landley.net>
  8. * Copyright 2006 Bernhard Fischer
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  11. */
  12. #include "busybox.h"
  13. int nohup_main(int argc, char **argv)
  14. {
  15. int temp, nullfd;
  16. char *nohupout, *home = NULL;
  17. xfunc_error_retval = 127;
  18. if (argc<2) bb_show_usage();
  19. nullfd = xopen(bb_dev_null, O_WRONLY|O_APPEND);
  20. /* If stdin is a tty, detach from it. */
  21. if (isatty(STDIN_FILENO)) dup2(nullfd, STDIN_FILENO);
  22. nohupout = "nohup.out";
  23. /* Redirect stdout to nohup.out, either in "." or in "$HOME". */
  24. if (isatty(STDOUT_FILENO)) {
  25. close(STDOUT_FILENO);
  26. if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
  27. home = getenv("HOME");
  28. if (home) {
  29. nohupout = concat_path_file(home, nohupout);
  30. xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
  31. }
  32. }
  33. } else dup2(nullfd, STDOUT_FILENO);
  34. /* If we have a tty on strderr, announce filename and redirect to stdout.
  35. * Else redirect to /dev/null.
  36. */
  37. temp = isatty(STDERR_FILENO);
  38. if (temp) bb_error_msg("appending to %s", nohupout);
  39. dup2(temp ? STDOUT_FILENO : nullfd, STDERR_FILENO);
  40. close(nullfd);
  41. signal (SIGHUP, SIG_IGN);
  42. execvp(argv[1],argv+1);
  43. if (00 && ENABLE_FEATURE_CLEAN_UP && home) free(nohupout);
  44. bb_perror_msg_and_die("%s", argv[1]);
  45. }