blob: ea1c4c55a0a11f738f2c8af8483b13edd6cfc36c [file] [log] [blame]
Rob Landleyc020f5f2006-05-21 18:28:13 +00001/* vi:set 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/sed.html
6 *
7 * Copyright 2006 Rob Landley <rob@landley.net>
8 *
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000010 */
11
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000012#include <fcntl.h>
Rob Landleyc020f5f2006-05-21 18:28:13 +000013#include <unistd.h>
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000014#include "busybox.h"
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000015
Rob Landleyc020f5f2006-05-21 18:28:13 +000016int nohup_main(int argc, char *argv[])
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000017{
Rob Landleyc020f5f2006-05-21 18:28:13 +000018 int temp, nullfd;
19 char *nohupout = "nohup.out", *home = NULL;
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000020
Rob Landleyc020f5f2006-05-21 18:28:13 +000021 if (argc<2) bb_show_usage();
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000022
Rob Landleyc020f5f2006-05-21 18:28:13 +000023 nullfd = bb_xopen(bb_dev_null, O_WRONLY|O_APPEND);
24 // If stdin is a tty, detach from it.
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000025
Rob Landleyc020f5f2006-05-21 18:28:13 +000026 if (isatty(0)) dup2(nullfd, 0);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000027
Rob Landleyc020f5f2006-05-21 18:28:13 +000028 // Redirect stdout to nohup.out, either in "." or in "$HOME".
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000029
Rob Landleyc020f5f2006-05-21 18:28:13 +000030 if (isatty(1)) {
31 close(1);
32 if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
33 home = getenv("HOME");
34 if (home) {
35 home = concat_path_file(home, nohupout);
36 bb_xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000037 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000038 }
Rob Landleyc020f5f2006-05-21 18:28:13 +000039 } else dup2(nullfd, 1);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000040
Rob Landleyc020f5f2006-05-21 18:28:13 +000041 // If we have a tty on strderr, announce filename and redirect to stdout.
42 // Else redirect to /dev/null.
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000043
Rob Landleyc020f5f2006-05-21 18:28:13 +000044 temp = isatty(2);
45 if (temp) fdprintf(2,"Writing to %s\n", home ? home : nohupout);
46 dup2(temp ? 1 : nullfd, 2);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000047
Rob Landleyc020f5f2006-05-21 18:28:13 +000048 close(nullfd);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000049
Rob Landleyc020f5f2006-05-21 18:28:13 +000050 // Exec our new program.
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000051
Rob Landleyc020f5f2006-05-21 18:28:13 +000052 execvp(argv[1],argv+1);
53 if (ENABLE_FEATURE_CLEAN_UP) free(home);
54 bb_error_msg_and_die("exec %s",argv[1]);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000055}