xref: /freebsd/usr.sbin/daemon/daemon.c (revision bd06a3ec97e6b15505087abfd1888439c5b70863)
1bd06a3ecSMike Barcroft /*-
2bd06a3ecSMike Barcroft  * Copyright (c) 1999 Berkeley Software Design, Inc. All rights reserved.
3bd06a3ecSMike Barcroft  *
4bd06a3ecSMike Barcroft  * Redistribution and use in source and binary forms, with or without
5bd06a3ecSMike Barcroft  * modification, are permitted provided that the following conditions
6bd06a3ecSMike Barcroft  * are met:
7bd06a3ecSMike Barcroft  * 1. Redistributions of source code must retain the above copyright
8bd06a3ecSMike Barcroft  *    notice, this list of conditions and the following disclaimer.
9bd06a3ecSMike Barcroft  * 2. Redistributions in binary form must reproduce the above copyright
10bd06a3ecSMike Barcroft  *    notice, this list of conditions and the following disclaimer in the
11bd06a3ecSMike Barcroft  *    documentation and/or other materials provided with the distribution.
12bd06a3ecSMike Barcroft  * 3. Berkeley Software Design Inc's name may not be used to endorse or
13bd06a3ecSMike Barcroft  *    promote products derived from this software without specific prior
14bd06a3ecSMike Barcroft  *    written permission.
15bd06a3ecSMike Barcroft  *
16bd06a3ecSMike Barcroft  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17bd06a3ecSMike Barcroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bd06a3ecSMike Barcroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bd06a3ecSMike Barcroft  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20bd06a3ecSMike Barcroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bd06a3ecSMike Barcroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bd06a3ecSMike Barcroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bd06a3ecSMike Barcroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bd06a3ecSMike Barcroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bd06a3ecSMike Barcroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bd06a3ecSMike Barcroft  * SUCH DAMAGE.
27bd06a3ecSMike Barcroft  *
28bd06a3ecSMike Barcroft  *	From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp
29bd06a3ecSMike Barcroft  * $FreeBSD$
30bd06a3ecSMike Barcroft  */
31bd06a3ecSMike Barcroft 
32bd06a3ecSMike Barcroft #include <sys/types.h>
33bd06a3ecSMike Barcroft 
34bd06a3ecSMike Barcroft #include <err.h>
35bd06a3ecSMike Barcroft #include <stdio.h>
36bd06a3ecSMike Barcroft #include <stdlib.h>
37bd06a3ecSMike Barcroft #include <unistd.h>
38bd06a3ecSMike Barcroft 
39bd06a3ecSMike Barcroft static void usage(void);
40bd06a3ecSMike Barcroft 
41bd06a3ecSMike Barcroft int
42bd06a3ecSMike Barcroft main(int argc, char *argv[])
43bd06a3ecSMike Barcroft {
44bd06a3ecSMike Barcroft 	int ch, nochdir, noclose;
45bd06a3ecSMike Barcroft 
46bd06a3ecSMike Barcroft 	nochdir = noclose = 1;
47bd06a3ecSMike Barcroft 	while ((ch = getopt(argc, argv, "-cf")) != -1) {
48bd06a3ecSMike Barcroft 		switch (ch) {
49bd06a3ecSMike Barcroft 		case 'c':
50bd06a3ecSMike Barcroft 			nochdir = 0;
51bd06a3ecSMike Barcroft 			break;
52bd06a3ecSMike Barcroft 		case 'f':
53bd06a3ecSMike Barcroft 			noclose = 0;
54bd06a3ecSMike Barcroft 			break;
55bd06a3ecSMike Barcroft 		case '?':
56bd06a3ecSMike Barcroft 		default:
57bd06a3ecSMike Barcroft 			usage();
58bd06a3ecSMike Barcroft 		}
59bd06a3ecSMike Barcroft 	}
60bd06a3ecSMike Barcroft 	argc -= optind;
61bd06a3ecSMike Barcroft 	argv += optind;
62bd06a3ecSMike Barcroft 
63bd06a3ecSMike Barcroft 	if (argc == 0)
64bd06a3ecSMike Barcroft 		usage();
65bd06a3ecSMike Barcroft 	if (daemon(nochdir, noclose) == -1)
66bd06a3ecSMike Barcroft 		err(1, NULL);
67bd06a3ecSMike Barcroft 	execvp(argv[0], argv);
68bd06a3ecSMike Barcroft 
69bd06a3ecSMike Barcroft 	/* The child is now running, so the exit status doesn't matter. */
70bd06a3ecSMike Barcroft 	err(1, "%s", argv[0]);
71bd06a3ecSMike Barcroft }
72bd06a3ecSMike Barcroft 
73bd06a3ecSMike Barcroft static void
74bd06a3ecSMike Barcroft usage(void)
75bd06a3ecSMike Barcroft {
76bd06a3ecSMike Barcroft 	(void)fprintf(stderr, "usage: daemon [-cf] command arguments ...\n");
77bd06a3ecSMike Barcroft 	exit(1);
78bd06a3ecSMike Barcroft }
79