aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraldot <aldot@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-09-21 22:10:24 +0000
committeraldot <aldot@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-09-21 22:10:24 +0000
commit4834cc45e58721f8c2348d5a933f5592a493c88d (patch)
treec5749f545015bf7ec8a5089ef2b03d1924eba185
parent4eff22bb36431951be1c56ca541f09585d5a8f98 (diff)
downloadbusybox-w32-4834cc45e58721f8c2348d5a933f5592a493c88d.tar.gz
busybox-w32-4834cc45e58721f8c2348d5a933f5592a493c88d.tar.bz2
busybox-w32-4834cc45e58721f8c2348d5a933f5592a493c88d.zip
- pull r15578 from busybox_scratch branch:
- fix bug where it would behave wrong if ./nohup.out was not writable. - debloat and make it readable while at it. $ size coreutils/nohup.o* text data bss dec hex filename 362 0 0 362 16a coreutils/nohup.o.trunk 344 0 0 344 158 coreutils/nohup.o $ make bloatcheck function old new delta nohup_main 324 310 -14 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-14) Total: -14 bytes git-svn-id: svn://busybox.net/trunk/busybox@16173 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--coreutils/nohup.c46
1 files changed, 21 insertions, 25 deletions
diff --git a/coreutils/nohup.c b/coreutils/nohup.c
index 9e9150d33..cf8ad2cd4 100644
--- a/coreutils/nohup.c
+++ b/coreutils/nohup.c
@@ -5,53 +5,49 @@
5 * http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html 5 * http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html
6 * 6 *
7 * Copyright 2006 Rob Landley <rob@landley.net> 7 * Copyright 2006 Rob Landley <rob@landley.net>
8 * Copyright 2006 Bernhard Fischer
8 * 9 *
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 10 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 */ 11 */
11 12
12#include "busybox.h" 13#include "busybox.h"
13 14
14int nohup_main(int argc, char *argv[]) 15int nohup_main(int argc, char **argv)
15{ 16{
16 int temp, nullfd; 17 int temp, nullfd;
17 char *nohupout = "nohup.out", *home = NULL; 18 char *nohupout, *home = NULL;
18
19 // I have no idea why the standard cares about this.
20 19
21 bb_default_error_retval = 127; 20 bb_default_error_retval = 127;
22 21
23 if (argc<2) bb_show_usage(); 22 if (argc<2) bb_show_usage();
24 23
25 nullfd = xopen(bb_dev_null, O_WRONLY|O_APPEND); 24 nullfd = xopen(bb_dev_null, O_WRONLY|O_APPEND);
26 // If stdin is a tty, detach from it. 25 /* If stdin is a tty, detach from it. */
27 26 if (isatty(STDIN_FILENO)) dup2(nullfd, STDIN_FILENO);
28 if (isatty(0)) dup2(nullfd, 0);
29
30 // Redirect stdout to nohup.out, either in "." or in "$HOME".
31 27
32 if (isatty(1)) { 28 nohupout = "nohup.out";
33 close(1); 29 /* Redirect stdout to nohup.out, either in "." or in "$HOME". */
30 if (isatty(STDOUT_FILENO)) {
31 close(STDOUT_FILENO);
34 if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) { 32 if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
35 home = getenv("HOME"); 33 home = getenv("HOME");
36 if (home) { 34 if (home) {
37 home = concat_path_file(home, nohupout); 35 nohupout = concat_path_file(home, nohupout);
38 xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR); 36 xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
39 } 37 }
40 } 38 }
41 } else dup2(nullfd, 1); 39 } else dup2(nullfd, STDOUT_FILENO);
42 40
43 // If we have a tty on strderr, announce filename and redirect to stdout. 41 /* If we have a tty on strderr, announce filename and redirect to stdout.
44 // Else redirect to /dev/null. 42 * Else redirect to /dev/null.
45 43 */
46 temp = isatty(2); 44 temp = isatty(STDERR_FILENO);
47 if (temp) fdprintf(2,"Writing to %s\n", home ? home : nohupout); 45 if (temp) bb_error_msg("Appending to %s", nohupout);
48 dup2(temp ? 1 : nullfd, 2); 46 dup2(temp ? STDOUT_FILENO : nullfd, STDERR_FILENO);
49 close(nullfd); 47 close(nullfd);
50 signal(SIGHUP, SIG_IGN); 48 signal (SIGHUP, SIG_IGN);
51
52 // Exec our new program.
53 49
54 execvp(argv[1],argv+1); 50 execvp(argv[1],argv+1);
55 if (ENABLE_FEATURE_CLEAN_UP) free(home); 51 if (00 && ENABLE_FEATURE_CLEAN_UP && home) free(nohupout);
56 bb_error_msg_and_die("exec %s",argv[1]); 52 bb_perror_msg_and_die("%s",argv[1]);
57} 53}