2 * Copyright (c) 2009, Erik van der Kouwe
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Functionality implemented according to this specification:
30 * http://www.opengroup.org/onlinepubs/000095399/utilities/nohup.html
42 #define NOHUP_OUT_FILENAME "nohup.out"
44 static void print_usage(const char *argv0
)
46 printf("Usage: %s command [arg...]\n", argv0
);
49 static int redirect_tty(void)
52 char buffer
[PATH_MAX
+ 1], *home
;
54 /* redirect stdout to a file if needed */
55 if (isatty(STDOUT_FILENO
))
57 /* first try: current directory */
58 fd
= open(NOHUP_OUT_FILENAME
, O_WRONLY
| O_APPEND
| O_CREAT
, S_IRUSR
| S_IWUSR
);
61 /* alternative: home directory */
62 home
= getenv("HOME");
65 snprintf(buffer
, sizeof(buffer
), "%s/%s", home
, NOHUP_OUT_FILENAME
);
66 buffer
[sizeof(buffer
) - 1] = 0;
67 fd
= open(buffer
, O_WRONLY
| O_APPEND
| O_CREAT
, S_IRUSR
| S_IWUSR
);
73 perror("cannot create " NOHUP_OUT_FILENAME
" and $HOME/" NOHUP_OUT_FILENAME
);
77 /* move the fd to stdout */
78 if (dup2(fd
, STDOUT_FILENO
) < 0 || close(fd
) < 0)
80 perror("cannot redirect stdout");
85 /* redirect stderr to stdout if needed */
86 if (isatty(STDERR_FILENO
))
88 if (dup2(STDOUT_FILENO
, STDERR_FILENO
) < 0)
90 perror("cannot redirect stderr");
98 int main(int argc
, char **argv
)
102 /* check parameters */
105 print_usage(argv
[0]);
110 sa
.sa_handler
= SIG_IGN
;
111 sigemptyset(&sa
.sa_mask
);
113 if (sigaction(SIGHUP
, &sa
, NULL
) < 0)
115 perror("cannot ignore SIGHUP");
119 /* redirect TTY input and output */
120 if (redirect_tty() < 0)
123 /* run the command */
124 execvp(argv
[1], argv
+ 1);
125 perror("cannot execute");
127 /* exit code depends on whether the utility was found */
134 /* utility not found */
138 /* exec failed for other reason */