blob: 3dc5314095594b0d763588024b8aa49773cf4e3d [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-Fischer6c4dade2008-09-25 12:13:34 +00008 * Copyright 2006 Bernhard Reutner-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;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010035int nohup_main(int argc UNUSED_PARAM, 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
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010042 if (!argv[1]) {
43 bb_show_usage();
44 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000045
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000046 /* If stdin is a tty, detach from it. */
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000047 if (isatty(STDIN_FILENO)) {
48 /* bb_error_msg("ignoring input"); */
49 close(STDIN_FILENO);
50 xopen(bb_dev_null, O_RDONLY); /* will be fd 0 (STDIN_FILENO) */
51 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000052
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000053 nohupout = "nohup.out";
54 /* Redirect stdout to nohup.out, either in "." or in "$HOME". */
55 if (isatty(STDOUT_FILENO)) {
56 close(STDOUT_FILENO);
Rob Landleyc020f5f2006-05-21 18:28:13 +000057 if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
58 home = getenv("HOME");
59 if (home) {
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000060 nohupout = concat_path_file(home, nohupout);
Rob Landleyd921b2e2006-08-03 15:41:12 +000061 xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000062 } else {
63 xopen(bb_dev_null, O_RDONLY); /* will be fd 1 */
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000064 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000065 }
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000066 bb_error_msg("appending output to %s", nohupout);
67 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000068
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000069 /* If we have a tty on stderr, redirect to stdout. */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000070 if (isatty(STDERR_FILENO)) {
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000071 /* if (stdout_wasnt_a_tty)
72 bb_error_msg("redirecting stderr to stdout"); */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000073 dup2(STDOUT_FILENO, STDERR_FILENO);
Denis Vlasenkof8157ca2008-02-04 00:30:06 +000074 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000075
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000076 signal(SIGHUP, SIG_IGN);
77
Denys Vlasenko41ddd9f2010-06-25 01:46:53 +020078 argv++;
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020079 BB_EXECVP_or_die(argv);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000080}