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 */ 30bd06a3ecSMike Barcroft 3154ede02dSPhilippe Charnier #include <sys/cdefs.h> 3254ede02dSPhilippe Charnier __FBSDID("$FreeBSD$"); 3354ede02dSPhilippe Charnier 34c6262cb6SPawel Jakub Dawidek #include <sys/param.h> 35bd06a3ecSMike Barcroft 36bd06a3ecSMike Barcroft #include <err.h> 37846be7bdSPoul-Henning Kamp #include <errno.h> 38c6262cb6SPawel Jakub Dawidek #include <libutil.h> 39bd06a3ecSMike Barcroft #include <stdio.h> 40bd06a3ecSMike Barcroft #include <stdlib.h> 41bd06a3ecSMike Barcroft #include <unistd.h> 42bd06a3ecSMike Barcroft 43bd06a3ecSMike Barcroft static void usage(void); 44bd06a3ecSMike Barcroft 45bd06a3ecSMike Barcroft int 46bd06a3ecSMike Barcroft main(int argc, char *argv[]) 47bd06a3ecSMike Barcroft { 48c6262cb6SPawel Jakub Dawidek struct pidfh *pfh; 49846be7bdSPoul-Henning Kamp int ch, nochdir, noclose, errcode; 50846be7bdSPoul-Henning Kamp const char *pidfile; 51c6262cb6SPawel Jakub Dawidek pid_t otherpid; 52bd06a3ecSMike Barcroft 53bd06a3ecSMike Barcroft nochdir = noclose = 1; 54846be7bdSPoul-Henning Kamp pidfile = NULL; 55846be7bdSPoul-Henning Kamp while ((ch = getopt(argc, argv, "-cfp:")) != -1) { 56bd06a3ecSMike Barcroft switch (ch) { 57bd06a3ecSMike Barcroft case 'c': 58bd06a3ecSMike Barcroft nochdir = 0; 59bd06a3ecSMike Barcroft break; 60bd06a3ecSMike Barcroft case 'f': 61bd06a3ecSMike Barcroft noclose = 0; 62bd06a3ecSMike Barcroft break; 63846be7bdSPoul-Henning Kamp case 'p': 64846be7bdSPoul-Henning Kamp pidfile = optarg; 65846be7bdSPoul-Henning Kamp break; 66bd06a3ecSMike Barcroft default: 67bd06a3ecSMike Barcroft usage(); 68bd06a3ecSMike Barcroft } 69bd06a3ecSMike Barcroft } 70bd06a3ecSMike Barcroft argc -= optind; 71bd06a3ecSMike Barcroft argv += optind; 72bd06a3ecSMike Barcroft 73bd06a3ecSMike Barcroft if (argc == 0) 74bd06a3ecSMike Barcroft usage(); 75846be7bdSPoul-Henning Kamp /* 76846be7bdSPoul-Henning Kamp * Try to open the pidfile before calling daemon(3), 77846be7bdSPoul-Henning Kamp * to be able to report the error intelligently 78846be7bdSPoul-Henning Kamp */ 79846be7bdSPoul-Henning Kamp if (pidfile) { 80c6262cb6SPawel Jakub Dawidek pfh = pidfile_open(pidfile, 0600, &otherpid); 81c6262cb6SPawel Jakub Dawidek if (pfh == NULL) { 82c6262cb6SPawel Jakub Dawidek if (errno == EEXIST) { 83c6262cb6SPawel Jakub Dawidek errx(3, "process already running, pid: %d", 84c6262cb6SPawel Jakub Dawidek otherpid); 85c6262cb6SPawel Jakub Dawidek } 86846be7bdSPoul-Henning Kamp err(2, "pidfile ``%s''", pidfile); 87846be7bdSPoul-Henning Kamp } 88c6262cb6SPawel Jakub Dawidek } 89846be7bdSPoul-Henning Kamp 90bd06a3ecSMike Barcroft if (daemon(nochdir, noclose) == -1) 91bd06a3ecSMike Barcroft err(1, NULL); 92846be7bdSPoul-Henning Kamp 93846be7bdSPoul-Henning Kamp /* Now that we are the child, write out the pid */ 94c6262cb6SPawel Jakub Dawidek if (pidfile) 95c6262cb6SPawel Jakub Dawidek pidfile_write(pfh); 96846be7bdSPoul-Henning Kamp 97bd06a3ecSMike Barcroft execvp(argv[0], argv); 98bd06a3ecSMike Barcroft 99846be7bdSPoul-Henning Kamp /* 100846be7bdSPoul-Henning Kamp * execvp() failed -- unlink pidfile if any, and 101846be7bdSPoul-Henning Kamp * report the error 102846be7bdSPoul-Henning Kamp */ 103846be7bdSPoul-Henning Kamp errcode = errno; /* Preserve errcode -- unlink may reset it */ 104846be7bdSPoul-Henning Kamp if (pidfile) 105c6262cb6SPawel Jakub Dawidek pidfile_remove(pfh); 106846be7bdSPoul-Henning Kamp 107bd06a3ecSMike Barcroft /* The child is now running, so the exit status doesn't matter. */ 108846be7bdSPoul-Henning Kamp errc(1, errcode, "%s", argv[0]); 109bd06a3ecSMike Barcroft } 110bd06a3ecSMike Barcroft 111bd06a3ecSMike Barcroft static void 112bd06a3ecSMike Barcroft usage(void) 113bd06a3ecSMike Barcroft { 114846be7bdSPoul-Henning Kamp (void)fprintf(stderr, 115846be7bdSPoul-Henning Kamp "usage: daemon [-cf] [-p pidfile] command arguments ...\n"); 116bd06a3ecSMike Barcroft exit(1); 117bd06a3ecSMike Barcroft } 118