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> 3812d7249eSTom Rhodes #include <pwd.h> 39c6262cb6SPawel Jakub Dawidek #include <libutil.h> 40e6d4b388STom Rhodes #include <login_cap.h> 41bd06a3ecSMike Barcroft #include <stdio.h> 42bd06a3ecSMike Barcroft #include <stdlib.h> 43bd06a3ecSMike Barcroft #include <unistd.h> 44bd06a3ecSMike Barcroft 45e6d4b388STom Rhodes static void restrict_process(const char *); 46bd06a3ecSMike Barcroft static void usage(void); 47bd06a3ecSMike Barcroft 48bd06a3ecSMike Barcroft int 49bd06a3ecSMike Barcroft main(int argc, char *argv[]) 50bd06a3ecSMike Barcroft { 5115543aadSTom Rhodes struct pidfh *pfh = NULL; 52846be7bdSPoul-Henning Kamp int ch, nochdir, noclose, errcode; 53e6d4b388STom Rhodes const char *pidfile, *user; 54c6262cb6SPawel Jakub Dawidek pid_t otherpid; 55bd06a3ecSMike Barcroft 56bd06a3ecSMike Barcroft nochdir = noclose = 1; 57e6d4b388STom Rhodes pidfile = user = NULL; 58e6d4b388STom Rhodes while ((ch = getopt(argc, argv, "-cf:p:u:")) != -1) { 59bd06a3ecSMike Barcroft switch (ch) { 60bd06a3ecSMike Barcroft case 'c': 61bd06a3ecSMike Barcroft nochdir = 0; 62bd06a3ecSMike Barcroft break; 63bd06a3ecSMike Barcroft case 'f': 64bd06a3ecSMike Barcroft noclose = 0; 65bd06a3ecSMike Barcroft break; 66846be7bdSPoul-Henning Kamp case 'p': 67846be7bdSPoul-Henning Kamp pidfile = optarg; 68846be7bdSPoul-Henning Kamp break; 69e6d4b388STom Rhodes case 'u': 70e6d4b388STom Rhodes user = optarg; 71e6d4b388STom Rhodes break; 72bd06a3ecSMike Barcroft default: 73bd06a3ecSMike Barcroft usage(); 74bd06a3ecSMike Barcroft } 75bd06a3ecSMike Barcroft } 76bd06a3ecSMike Barcroft argc -= optind; 77bd06a3ecSMike Barcroft argv += optind; 78bd06a3ecSMike Barcroft 79bd06a3ecSMike Barcroft if (argc == 0) 80bd06a3ecSMike Barcroft usage(); 8112d7249eSTom Rhodes 82e6d4b388STom Rhodes if (user != NULL) 83e6d4b388STom Rhodes restrict_process(user); 8412d7249eSTom Rhodes 85846be7bdSPoul-Henning Kamp /* 86846be7bdSPoul-Henning Kamp * Try to open the pidfile before calling daemon(3), 87846be7bdSPoul-Henning Kamp * to be able to report the error intelligently 88846be7bdSPoul-Henning Kamp */ 89846be7bdSPoul-Henning Kamp if (pidfile) { 90c6262cb6SPawel Jakub Dawidek pfh = pidfile_open(pidfile, 0600, &otherpid); 91c6262cb6SPawel Jakub Dawidek if (pfh == NULL) { 92c6262cb6SPawel Jakub Dawidek if (errno == EEXIST) { 93c6262cb6SPawel Jakub Dawidek errx(3, "process already running, pid: %d", 94c6262cb6SPawel Jakub Dawidek otherpid); 95c6262cb6SPawel Jakub Dawidek } 96846be7bdSPoul-Henning Kamp err(2, "pidfile ``%s''", pidfile); 97846be7bdSPoul-Henning Kamp } 98c6262cb6SPawel Jakub Dawidek } 99846be7bdSPoul-Henning Kamp 100bd06a3ecSMike Barcroft if (daemon(nochdir, noclose) == -1) 101bd06a3ecSMike Barcroft err(1, NULL); 102846be7bdSPoul-Henning Kamp 103846be7bdSPoul-Henning Kamp /* Now that we are the child, write out the pid */ 104c6262cb6SPawel Jakub Dawidek if (pidfile) 105c6262cb6SPawel Jakub Dawidek pidfile_write(pfh); 106846be7bdSPoul-Henning Kamp 107bd06a3ecSMike Barcroft execvp(argv[0], argv); 108bd06a3ecSMike Barcroft 109846be7bdSPoul-Henning Kamp /* 110846be7bdSPoul-Henning Kamp * execvp() failed -- unlink pidfile if any, and 111846be7bdSPoul-Henning Kamp * report the error 112846be7bdSPoul-Henning Kamp */ 113846be7bdSPoul-Henning Kamp errcode = errno; /* Preserve errcode -- unlink may reset it */ 114846be7bdSPoul-Henning Kamp if (pidfile) 115c6262cb6SPawel Jakub Dawidek pidfile_remove(pfh); 116846be7bdSPoul-Henning Kamp 117bd06a3ecSMike Barcroft /* The child is now running, so the exit status doesn't matter. */ 118846be7bdSPoul-Henning Kamp errc(1, errcode, "%s", argv[0]); 119bd06a3ecSMike Barcroft } 120bd06a3ecSMike Barcroft 121bd06a3ecSMike Barcroft static void 122e6d4b388STom Rhodes restrict_process(const char *user) 12312d7249eSTom Rhodes { 12412d7249eSTom Rhodes struct passwd *pw = NULL; 12512d7249eSTom Rhodes 126e6d4b388STom Rhodes pw = getpwnam(user); 127e6d4b388STom Rhodes if (pw == NULL) 128e6d4b388STom Rhodes errx(1, "unknown user: %s", user); 12912d7249eSTom Rhodes 130e6d4b388STom Rhodes if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) 131e6d4b388STom Rhodes errx(1, "failed to set user environment"); 13212d7249eSTom Rhodes } 13312d7249eSTom Rhodes 13412d7249eSTom Rhodes static void 135bd06a3ecSMike Barcroft usage(void) 136bd06a3ecSMike Barcroft { 137846be7bdSPoul-Henning Kamp (void)fprintf(stderr, 138e6d4b388STom Rhodes "usage: daemon [-cf] [-p pidfile] [-u user] command " 13912d7249eSTom Rhodes "arguments ...\n"); 140bd06a3ecSMike Barcroft exit(1); 141bd06a3ecSMike Barcroft } 142