Home | History | Annotate | Line # | Download | only in nohup
nohup.c revision 1.3
      1 /*
      2  * Copyright (c) 1989 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 char copyright[] =
     36 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 static char sccsid[] = "@(#)nohup.c	5.4 (Berkeley) 6/1/90";
     42 #endif /* not lint */
     43 
     44 #include <sys/param.h>
     45 #include <sys/signal.h>
     46 #include <sys/file.h>
     47 #include <sys/stat.h>
     48 #include <fcntl.h>
     49 #include <unistd.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <errno.h>
     54 
     55 static void dofile();
     56 static void usage();
     57 
     58 /* nohup shall exit with one of the following values:
     59    126 - The utility was found but could not be invoked.
     60    127 - An error occured in the nohup utility, or the utility could
     61          not be found. */
     62 #define EXIT_NOEXEC	126
     63 #define EXIT_NOTFOUND	127
     64 #define EXIT_MISC	127
     65 
     66 int
     67 main(argc, argv)
     68 	int argc;
     69 	char **argv;
     70 {
     71 	int exit_status;
     72 
     73 	if (argc < 2)
     74 		usage();
     75 
     76 	if (isatty(STDOUT_FILENO))
     77 		dofile();
     78 	if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1) {
     79 		/* may have just closed stderr */
     80 		(void)fprintf(stdin, "nohup: %s\n", strerror(errno));
     81 		exit(EXIT_MISC);
     82 	}
     83 
     84 	/* The nohup utility shall take the standard action for all signals
     85 	   except that SIGHUP shall be ignored. */
     86 	(void)signal(SIGHUP, SIG_IGN);
     87 
     88 	execvp(argv[1], &argv[1]);
     89 	exit_status = (errno = ENOENT) ? EXIT_NOTFOUND : EXIT_NOEXEC;
     90 	(void)fprintf(stderr, "nohup: %s: %s\n", argv[1], strerror(errno));
     91 	exit(exit_status);
     92 }
     93 
     94 static void
     95 dofile()
     96 {
     97 	int fd;
     98 	char *p, path[MAXPATHLEN];
     99 
    100 	/* If the standard output is a terminal, all output written to
    101 	   its standard output shall be appended to the end of the file
    102 	   nohup.out in the current directory.  If nohup.out cannot be
    103 	   created or opened for appending, the output shall be appended
    104 	   to the end of the file nohup.out in the directory specified
    105 	   by the HOME environment variable.
    106 
    107 	   If a file is created, the file's permission bits shall be
    108 	   set to S_IRUSR | S_IWUSR. */
    109 #define	FILENAME	"nohup.out"
    110 	p = FILENAME;
    111 	if ((fd = open(p, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0)
    112 		goto dupit;
    113 	if ((p = getenv("HOME")) != NULL) {
    114 		(void)strcpy(path, p);
    115 		(void)strcat(path, "/");
    116 		(void)strcat(path, FILENAME);
    117 		if ((fd = open(p = path, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0)
    118 			goto dupit;
    119 	}
    120 	(void)fprintf(stderr, "nohup: can't open a nohup.out file.\n");
    121 	exit(EXIT_MISC);
    122 
    123 dupit:	(void)lseek(fd, 0L, SEEK_END);
    124 	if (dup2(fd, STDOUT_FILENO) == -1) {
    125 		(void)fprintf(stderr, "nohup: %s\n", strerror(errno));
    126 		exit(EXIT_MISC);
    127 	}
    128 	(void)fprintf(stderr, "sending output to %s\n", p);
    129 }
    130 
    131 static void
    132 usage()
    133 {
    134 	(void)fprintf(stderr, "usage: nohup command\n");
    135 	exit(EXIT_MISC);
    136 }
    137