blob: 22419b800de37e5fc02766ab9f50240ed54078fa [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landleyc020f5f2006-05-21 18:28:13 +00002/* nohup - invoke a utility immune to hangups.
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00003 *
Rob Landleyc020f5f2006-05-21 18:28:13 +00004 * Busybox version based on nohup specification at
Rob Landley027ea1a2006-05-24 17:58:00 +00005 * http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00006 *
Rob Landleyc020f5f2006-05-21 18:28:13 +00007 * Copyright 2006 Rob Landley <rob@landley.net>
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +00008 * Copyright 2006 Bernhard Fischer
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00009 *
Rob Landleyc020f5f2006-05-21 18:28:13 +000010 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000011 */
12
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000014
Denis Vlasenko06af2162007-02-03 17:28:39 +000015int nohup_main(int argc, char **argv);
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000016int nohup_main(int argc, char **argv)
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000017{
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000018 int nullfd;
19 const char *nohupout;
20 char *home = NULL;
Rob Landley027ea1a2006-05-24 17:58:00 +000021
Denis Vlasenko40920822006-10-03 20:28:06 +000022 xfunc_error_retval = 127;
Rob Landley027ea1a2006-05-24 17:58:00 +000023
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000024 if (argc < 2) bb_show_usage();
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000025
Rob Landleyd921b2e2006-08-03 15:41:12 +000026 nullfd = xopen(bb_dev_null, O_WRONLY|O_APPEND);
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000027 /* If stdin is a tty, detach from it. */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000028 if (isatty(STDIN_FILENO))
29 dup2(nullfd, STDIN_FILENO);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000030
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000031 nohupout = "nohup.out";
32 /* Redirect stdout to nohup.out, either in "." or in "$HOME". */
33 if (isatty(STDOUT_FILENO)) {
34 close(STDOUT_FILENO);
Rob Landleyc020f5f2006-05-21 18:28:13 +000035 if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
36 home = getenv("HOME");
37 if (home) {
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000038 nohupout = concat_path_file(home, nohupout);
Rob Landleyd921b2e2006-08-03 15:41:12 +000039 xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000040 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000041 }
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000042 } else dup2(nullfd, STDOUT_FILENO);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000043
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000044 /* If we have a tty on stderr, announce filename and redirect to stdout.
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000045 * Else redirect to /dev/null.
46 */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000047 if (isatty(STDERR_FILENO)) {
48 bb_error_msg("appending to %s", nohupout);
49 dup2(STDOUT_FILENO, STDERR_FILENO);
50 } else dup2(nullfd, STDERR_FILENO);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000051
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000052 if (nullfd > 2)
53 close(nullfd);
54 signal(SIGHUP, SIG_IGN);
55
Denis Vlasenko1d76f432007-02-06 01:20:12 +000056 BB_EXECVP(argv[1], argv+1);
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000057 if (ENABLE_FEATURE_CLEAN_UP && home)
58 free((char*)nohupout);
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000059 bb_perror_msg_and_die("%s", argv[1]);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000060}