blob: 317d2a8ae900e2ac514b24a865d98e3108af19cf [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
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000013#include "busybox.h"
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000014
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000015int nohup_main(int argc, char **argv)
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000016{
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000017 int nullfd;
18 const char *nohupout;
19 char *home = NULL;
Rob Landley027ea1a2006-05-24 17:58:00 +000020
Denis Vlasenko40920822006-10-03 20:28:06 +000021 xfunc_error_retval = 127;
Rob Landley027ea1a2006-05-24 17:58:00 +000022
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000023 if (argc < 2) bb_show_usage();
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000024
Rob Landleyd921b2e2006-08-03 15:41:12 +000025 nullfd = xopen(bb_dev_null, O_WRONLY|O_APPEND);
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000026 /* If stdin is a tty, detach from it. */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000027 if (isatty(STDIN_FILENO))
28 dup2(nullfd, STDIN_FILENO);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000029
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000030 nohupout = "nohup.out";
31 /* Redirect stdout to nohup.out, either in "." or in "$HOME". */
32 if (isatty(STDOUT_FILENO)) {
33 close(STDOUT_FILENO);
Rob Landleyc020f5f2006-05-21 18:28:13 +000034 if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
35 home = getenv("HOME");
36 if (home) {
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000037 nohupout = concat_path_file(home, nohupout);
Rob Landleyd921b2e2006-08-03 15:41:12 +000038 xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000039 }
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000040 }
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000041 } else dup2(nullfd, STDOUT_FILENO);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000042
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000043 /* If we have a tty on stderr, announce filename and redirect to stdout.
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000044 * Else redirect to /dev/null.
45 */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000046 if (isatty(STDERR_FILENO)) {
47 bb_error_msg("appending to %s", nohupout);
48 dup2(STDOUT_FILENO, STDERR_FILENO);
49 } else dup2(nullfd, STDERR_FILENO);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000050
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000051 if (nullfd > 2)
52 close(nullfd);
53 signal(SIGHUP, SIG_IGN);
54
55 execvp(argv[1], argv+1);
56 if (ENABLE_FEATURE_CLEAN_UP && home)
57 free((char*)nohupout);
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000058 bb_perror_msg_and_die("%s", argv[1]);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000059}