11590Srgrimes/* 21590Srgrimes * Copyright (c) 1989, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 30114594Sobrien#if 0 311590Srgrimes#ifndef lint 3227785Scharnierstatic const char copyright[] = 331590Srgrimes"@(#) Copyright (c) 1989, 1993\n\ 341590Srgrimes The Regents of the University of California. All rights reserved.\n"; 351590Srgrimes#endif /* not lint */ 361590Srgrimes 371590Srgrimes#ifndef lint 381590Srgrimesstatic char sccsid[] = "@(#)nohup.c 8.1 (Berkeley) 6/6/93"; 39114594Sobrien#endif /* not lint */ 4027785Scharnier#endif 41114594Sobrien#include <sys/cdefs.h> 42114594Sobrien__FBSDID("$FreeBSD$"); 431590Srgrimes 441590Srgrimes#include <sys/param.h> 451590Srgrimes#include <sys/stat.h> 461590Srgrimes 4727785Scharnier#include <err.h> 481590Srgrimes#include <errno.h> 491590Srgrimes#include <fcntl.h> 501590Srgrimes#include <signal.h> 511590Srgrimes#include <stdio.h> 521590Srgrimes#include <stdlib.h> 53200462Sdelphij#include <string.h> 541590Srgrimes#include <unistd.h> 551590Srgrimes 5692921Simpstatic void dofile(void); 5792921Simpstatic void usage(void); 581590Srgrimes 5980007Smike#define FILENAME "nohup.out" 6080007Smike/* 6180007Smike * POSIX mandates that we exit with: 6280007Smike * 126 - If the utility was found, but failed to execute. 6380007Smike * 127 - If any other error occurred. 6480007Smike */ 6580007Smike#define EXIT_NOEXEC 126 6680007Smike#define EXIT_NOTFOUND 127 6780007Smike#define EXIT_MISC 127 6880007Smike 691590Srgrimesint 7099984Salfredmain(int argc, char *argv[]) 711590Srgrimes{ 7280007Smike int exit_status; 7380007Smike 7480007Smike while (getopt(argc, argv, "") != -1) 751590Srgrimes usage(); 7680007Smike argc -= optind; 7780007Smike argv += optind; 7880007Smike if (argc < 1) 7980007Smike usage(); 801590Srgrimes 811590Srgrimes if (isatty(STDOUT_FILENO)) 821590Srgrimes dofile(); 8380007Smike if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1) 841590Srgrimes /* may have just closed stderr */ 8580007Smike err(EXIT_MISC, "%s", argv[0]); 861590Srgrimes 871590Srgrimes (void)signal(SIGHUP, SIG_IGN); 881590Srgrimes 8980007Smike execvp(*argv, argv); 9080007Smike exit_status = (errno == ENOENT) ? EXIT_NOTFOUND : EXIT_NOEXEC; 9180007Smike err(exit_status, "%s", argv[0]); 921590Srgrimes} 931590Srgrimes 9480007Smikestatic void 9599984Salfreddofile(void) 961590Srgrimes{ 971590Srgrimes int fd; 9880007Smike char path[MAXPATHLEN]; 9980007Smike const char *p; 1001590Srgrimes 10180007Smike /* 10280007Smike * POSIX mandates if the standard output is a terminal, the standard 10380007Smike * output is appended to nohup.out in the working directory. Failing 10480007Smike * that, it will be appended to nohup.out in the directory obtained 10580007Smike * from the HOME environment variable. If file creation is required, 10680007Smike * the mode_t is set to S_IRUSR | S_IWUSR. 10780007Smike */ 1081590Srgrimes p = FILENAME; 10980007Smike fd = open(p, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR); 11080007Smike if (fd != -1) 1111590Srgrimes goto dupit; 11280007Smike if ((p = getenv("HOME")) != NULL && *p != '\0' && 11380007Smike (size_t)snprintf(path, sizeof(path), "%s/%s", p, FILENAME) < 11480007Smike sizeof(path)) { 11580007Smike fd = open(p = path, O_RDWR | O_CREAT | O_APPEND, 11680007Smike S_IRUSR | S_IWUSR); 11780007Smike if (fd != -1) 1181590Srgrimes goto dupit; 1191590Srgrimes } 12080007Smike errx(EXIT_MISC, "can't open a nohup.out file"); 1211590Srgrimes 12280007Smikedupit: 12327785Scharnier if (dup2(fd, STDOUT_FILENO) == -1) 12480007Smike err(EXIT_MISC, NULL); 12580007Smike (void)fprintf(stderr, "appending output to %s\n", p); 1261590Srgrimes} 1271590Srgrimes 12880007Smikestatic void 12999984Salfredusage(void) 1301590Srgrimes{ 13198971Stjr (void)fprintf(stderr, "usage: nohup [--] utility [arguments]\n"); 13280007Smike exit(EXIT_MISC); 1331590Srgrimes} 134