blob: 7d6a51ae9d180add12fd961d6a85fb136f4112d2 [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 Vlasenkof8157ca2008-02-04 00:30:06 +000015/* Compat info: nohup (GNU coreutils 6.8) does this:
16# nohup true
17nohup: ignoring input and appending output to `nohup.out'
18# nohup true 1>/dev/null
19nohup: ignoring input and redirecting stderr to stdout
20# nohup true 2>zz
21# cat zz
22nohup: ignoring input and appending output to `nohup.out'
23# nohup true 2>zz 1>/dev/null
24# cat zz
25nohup: ignoring input
26# nohup true </dev/null 1>/dev/null
27nohup: redirecting stderr to stdout
28# nohup true </dev/null 2>zz 1>/dev/null
29# cat zz
30 (nothing)
31#
32*/
33
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000034int nohup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000035int nohup_main(int argc, char **argv)
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000036{
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000037 const char *nohupout;
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000038 char *home;
Rob Landley027ea1a2006-05-24 17:58:00 +000039
Denis Vlasenko40920822006-10-03 20:28:06 +000040 xfunc_error_retval = 127;
Rob Landley027ea1a2006-05-24 17:58:00 +000041
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000042 if (argc < 2) bb_show_usage();
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000043
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000044 /* If stdin is a tty, detach from it. */
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000045 if (isatty(STDIN_FILENO)) {
46 /* bb_error_msg("ignoring input"); */
47 close(STDIN_FILENO);
48 xopen(bb_dev_null, O_RDONLY); /* will be fd 0 (STDIN_FILENO) */
49 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000050
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000051 nohupout = "nohup.out";
52 /* Redirect stdout to nohup.out, either in "." or in "$HOME". */
53 if (isatty(STDOUT_FILENO)) {
54 close(STDOUT_FILENO);
Rob Landleyc020f5f2006-05-21 18:28:13 +000055 if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
56 home = getenv("HOME");
57 if (home) {
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000058 nohupout = concat_path_file(home, nohupout);
Rob Landleyd921b2e2006-08-03 15:41:12 +000059 xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000060 } else {
61 xopen(bb_dev_null, O_RDONLY); /* will be fd 1 */
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000062 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000063 }
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000064 bb_error_msg("appending output to %s", nohupout);
65 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000066
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000067 /* If we have a tty on stderr, redirect to stdout. */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000068 if (isatty(STDERR_FILENO)) {
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000069 /* if (stdout_wasnt_a_tty)
70 bb_error_msg("redirecting stderr to stdout"); */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000071 dup2(STDOUT_FILENO, STDERR_FILENO);
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000072 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000073
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000074 signal(SIGHUP, SIG_IGN);
75
Denis Vlasenko1d76f432007-02-06 01:20:12 +000076 BB_EXECVP(argv[1], argv+1);
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000077 bb_simple_perror_msg_and_die(argv[1]);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000078}