blob: 21adfc1c35f3ff11c50321f317fd7d12febc4a66 [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{
Rob Landleyc020f5f2006-05-21 18:28:13 +000017 int temp, nullfd;
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000018 char *nohupout, *home = NULL;
Rob Landley027ea1a2006-05-24 17:58:00 +000019
Denis Vlasenko40920822006-10-03 20:28:06 +000020 xfunc_error_retval = 127;
Rob Landley027ea1a2006-05-24 17:58:00 +000021
Rob Landleyc020f5f2006-05-21 18:28:13 +000022 if (argc<2) bb_show_usage();
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000023
Rob Landleyd921b2e2006-08-03 15:41:12 +000024 nullfd = xopen(bb_dev_null, O_WRONLY|O_APPEND);
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000025 /* If stdin is a tty, detach from it. */
26 if (isatty(STDIN_FILENO)) dup2(nullfd, STDIN_FILENO);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000027
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000028 nohupout = "nohup.out";
29 /* Redirect stdout to nohup.out, either in "." or in "$HOME". */
30 if (isatty(STDOUT_FILENO)) {
31 close(STDOUT_FILENO);
Rob Landleyc020f5f2006-05-21 18:28:13 +000032 if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
33 home = getenv("HOME");
34 if (home) {
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000035 nohupout = concat_path_file(home, nohupout);
Rob Landleyd921b2e2006-08-03 15:41:12 +000036 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 }
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000039 } else dup2(nullfd, STDOUT_FILENO);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000040
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000041 /* If we have a tty on strderr, announce filename and redirect to stdout.
42 * Else redirect to /dev/null.
43 */
44 temp = isatty(STDERR_FILENO);
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000045 if (temp) bb_error_msg("appending to %s", nohupout);
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000046 dup2(temp ? STDOUT_FILENO : nullfd, STDERR_FILENO);
Rob Landleyc020f5f2006-05-21 18:28:13 +000047 close(nullfd);
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000048 signal (SIGHUP, SIG_IGN);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000049
Rob Landleyc020f5f2006-05-21 18:28:13 +000050 execvp(argv[1],argv+1);
Bernhard Reutner-Fischer3503ff72006-09-21 22:10:24 +000051 if (00 && ENABLE_FEATURE_CLEAN_UP && home) free(nohupout);
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000052 bb_perror_msg_and_die("%s", argv[1]);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000053}