blob: 5dd90adcc67a887082ae06a960d95922dcd645f1 [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.
3 *
4 * Busybox version based on nohup specification at
Rob Landley027ea1a2006-05-24 17:58:00 +00005 * http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html
Rob Landleyc020f5f2006-05-21 18:28:13 +00006 *
7 * Copyright 2006 Rob Landley <rob@landley.net>
8 *
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000010 */
11
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000012#include "busybox.h"
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000013
Rob Landleyc020f5f2006-05-21 18:28:13 +000014int nohup_main(int argc, char *argv[])
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000015{
Rob Landleyc020f5f2006-05-21 18:28:13 +000016 int temp, nullfd;
17 char *nohupout = "nohup.out", *home = NULL;
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000018
Rob Landley027ea1a2006-05-24 17:58:00 +000019 // I have no idea why the standard cares about this.
20
21 bb_default_error_retval = 127;
22
Rob Landleyc020f5f2006-05-21 18:28:13 +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);
Rob Landleyc020f5f2006-05-21 18:28:13 +000026 // If stdin is a tty, detach from it.
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000027
Rob Landleyc020f5f2006-05-21 18:28:13 +000028 if (isatty(0)) dup2(nullfd, 0);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000029
Rob Landleyc020f5f2006-05-21 18:28:13 +000030 // Redirect stdout to nohup.out, either in "." or in "$HOME".
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000031
Rob Landleyc020f5f2006-05-21 18:28:13 +000032 if (isatty(1)) {
33 close(1);
34 if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
35 home = getenv("HOME");
36 if (home) {
37 home = 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 }
Rob Landleyc020f5f2006-05-21 18:28:13 +000041 } else dup2(nullfd, 1);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000042
Rob Landleyc020f5f2006-05-21 18:28:13 +000043 // If we have a tty on strderr, announce filename and redirect to stdout.
44 // Else redirect to /dev/null.
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000045
Rob Landleyc020f5f2006-05-21 18:28:13 +000046 temp = isatty(2);
47 if (temp) fdprintf(2,"Writing to %s\n", home ? home : nohupout);
48 dup2(temp ? 1 : nullfd, 2);
Rob Landleyc020f5f2006-05-21 18:28:13 +000049 close(nullfd);
Denis Vlasenkoa9801652006-09-07 16:20:03 +000050 signal(SIGHUP, SIG_IGN);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000051
Rob Landleyc020f5f2006-05-21 18:28:13 +000052 // Exec our new program.
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000053
Rob Landleyc020f5f2006-05-21 18:28:13 +000054 execvp(argv[1],argv+1);
55 if (ENABLE_FEATURE_CLEAN_UP) free(home);
56 bb_error_msg_and_die("exec %s",argv[1]);
Bernhard Reutner-Fischer9d7010c2005-09-21 18:25:05 +000057}