daemon.c revision 82547
182547Smike/*-
282547Smike * Copyright (c) 1999 Berkeley Software Design, Inc. All rights reserved.
382547Smike *
482547Smike * Redistribution and use in source and binary forms, with or without
582547Smike * modification, are permitted provided that the following conditions
682547Smike * are met:
782547Smike * 1. Redistributions of source code must retain the above copyright
882547Smike *    notice, this list of conditions and the following disclaimer.
982547Smike * 2. Redistributions in binary form must reproduce the above copyright
1082547Smike *    notice, this list of conditions and the following disclaimer in the
1182547Smike *    documentation and/or other materials provided with the distribution.
1282547Smike * 3. Berkeley Software Design Inc's name may not be used to endorse or
1382547Smike *    promote products derived from this software without specific prior
1482547Smike *    written permission.
1582547Smike *
1682547Smike * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
1782547Smike * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1882547Smike * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1982547Smike * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
2082547Smike * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2182547Smike * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2282547Smike * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2382547Smike * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2482547Smike * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2582547Smike * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2682547Smike * SUCH DAMAGE.
2782547Smike *
2882547Smike *	From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp
2982547Smike * $FreeBSD: head/usr.sbin/daemon/daemon.c 82547 2001-08-30 04:48:02Z mike $
3082547Smike */
3182547Smike
3282547Smike#include <sys/types.h>
3382547Smike
3482547Smike#include <err.h>
3582547Smike#include <stdio.h>
3682547Smike#include <stdlib.h>
3782547Smike#include <unistd.h>
3882547Smike
3982547Smikestatic void usage(void);
4082547Smike
4182547Smikeint
4282547Smikemain(int argc, char *argv[])
4382547Smike{
4482547Smike	int ch, nochdir, noclose;
4582547Smike
4682547Smike	nochdir = noclose = 1;
4782547Smike	while ((ch = getopt(argc, argv, "-cf")) != -1) {
4882547Smike		switch (ch) {
4982547Smike		case 'c':
5082547Smike			nochdir = 0;
5182547Smike			break;
5282547Smike		case 'f':
5382547Smike			noclose = 0;
5482547Smike			break;
5582547Smike		case '?':
5682547Smike		default:
5782547Smike			usage();
5882547Smike		}
5982547Smike	}
6082547Smike	argc -= optind;
6182547Smike	argv += optind;
6282547Smike
6382547Smike	if (argc == 0)
6482547Smike		usage();
6582547Smike	if (daemon(nochdir, noclose) == -1)
6682547Smike		err(1, NULL);
6782547Smike	execvp(argv[0], argv);
6882547Smike
6982547Smike	/* The child is now running, so the exit status doesn't matter. */
7082547Smike	err(1, "%s", argv[0]);
7182547Smike}
7282547Smike
7382547Smikestatic void
7482547Smikeusage(void)
7582547Smike{
7682547Smike	(void)fprintf(stderr, "usage: daemon [-cf] command arguments ...\n");
7782547Smike	exit(1);
7882547Smike}
79