Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/daemon/daemon.c
Show All 29 Lines | |||||
* From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp | * From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp | ||||
*/ | */ | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <sys/mman.h> | #include <sys/mman.h> | ||||
#include <sys/stat.h> | |||||
#include <sys/wait.h> | #include <sys/wait.h> | ||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <libutil.h> | #include <libutil.h> | ||||
#include <login_cap.h> | #include <login_cap.h> | ||||
#include <pwd.h> | #include <pwd.h> | ||||
Show All 33 Lines | |||||
int | int | ||||
main(int argc, char *argv[]) | main(int argc, char *argv[]) | ||||
{ | { | ||||
const char *pidfile, *ppidfile, *title, *user, *outfn, *logtag; | const char *pidfile, *ppidfile, *title, *user, *outfn, *logtag; | ||||
int ch, nochdir, noclose, restart, dosyslog, child_eof; | int ch, nochdir, noclose, restart, dosyslog, child_eof; | ||||
sigset_t mask_susp, mask_orig, mask_read, mask_term; | sigset_t mask_susp, mask_orig, mask_read, mask_term; | ||||
struct log_params logpar; | struct log_params logpar; | ||||
int pfd[2] = { -1, -1 }, outfd = -1; | int pfd[2] = { -1, -1 }, outfd = -1; | ||||
int stdmask, logpri, logfac, mask; | int stdmask, logpri, logfac; | ||||
struct pidfh *ppfh, *pfh; | struct pidfh *ppfh, *pfh; | ||||
char *p; | char *p; | ||||
memset(&logpar, 0, sizeof(logpar)); | memset(&logpar, 0, sizeof(logpar)); | ||||
stdmask = STDOUT_FILENO | STDERR_FILENO; | stdmask = STDOUT_FILENO | STDERR_FILENO; | ||||
ppidfile = pidfile = user = NULL; | ppidfile = pidfile = user = NULL; | ||||
nochdir = noclose = 1; | nochdir = noclose = 1; | ||||
logpri = LOG_NOTICE; | logpri = LOG_NOTICE; | ||||
logfac = LOG_DAEMON; | logfac = LOG_DAEMON; | ||||
logtag = "daemon"; | logtag = "daemon"; | ||||
restart = 0; | restart = 0; | ||||
dosyslog = 0; | dosyslog = 0; | ||||
outfn = NULL; | outfn = NULL; | ||||
title = NULL; | title = NULL; | ||||
mask = -1; | while ((ch = getopt(argc, argv, "cfSp:P:ru:o:s:l:t:l:m:R:T:")) != -1) { | ||||
while ((ch = getopt(argc, argv, "cfSp:P:ru:o:s:l:t:l:m:M:R:T:")) != -1) { | |||||
switch (ch) { | switch (ch) { | ||||
case 'c': | case 'c': | ||||
nochdir = 0; | nochdir = 0; | ||||
break; | break; | ||||
case 'f': | case 'f': | ||||
noclose = 0; | noclose = 0; | ||||
break; | break; | ||||
case 'l': | case 'l': | ||||
Show All 19 Lines | while ((ch = getopt(argc, argv, "cfSp:P:ru:o:s:l:t:l:m:R:T:")) != -1) { | ||||
case 'r': | case 'r': | ||||
restart = 1; | restart = 1; | ||||
break; | break; | ||||
case 'R': | case 'R': | ||||
restart = strtol(optarg, &p, 0); | restart = strtol(optarg, &p, 0); | ||||
if (p == optarg || restart < 1) | if (p == optarg || restart < 1) | ||||
errx(6, "invalid restart delay"); | errx(6, "invalid restart delay"); | ||||
break; | break; | ||||
case 'M': | |||||
mask = strtol(optarg, &p, 8); | |||||
if (p == optarg || mask < 0 || mask > 0777) | |||||
errx(6, "unrecognized umask"); | |||||
break; | |||||
case 's': | case 's': | ||||
logpri = get_log_mapping(optarg, prioritynames); | logpri = get_log_mapping(optarg, prioritynames); | ||||
if (logpri == -1) | if (logpri == -1) | ||||
errx(4, "unrecognized syslog priority"); | errx(4, "unrecognized syslog priority"); | ||||
dosyslog = 1; | dosyslog = 1; | ||||
break; | break; | ||||
case 'S': | case 'S': | ||||
dosyslog = 1; | dosyslog = 1; | ||||
▲ Show 20 Lines • Show All 150 Lines • ▼ Show 20 Lines | if (pid == 0) { | ||||
if (stdmask & STDOUT_FILENO) { | if (stdmask & STDOUT_FILENO) { | ||||
if (dup2(pfd[1], STDOUT_FILENO) == -1) | if (dup2(pfd[1], STDOUT_FILENO) == -1) | ||||
err(1, "dup2"); | err(1, "dup2"); | ||||
} | } | ||||
if (pfd[1] != STDERR_FILENO && | if (pfd[1] != STDERR_FILENO && | ||||
pfd[1] != STDOUT_FILENO) | pfd[1] != STDOUT_FILENO) | ||||
close(pfd[1]); | close(pfd[1]); | ||||
} | } | ||||
/* Set umask if requested */ | |||||
if (mask > 0) | |||||
umask(mask); | |||||
execvp(argv[0], argv); | execvp(argv[0], argv); | ||||
/* | /* | ||||
* execvp() failed -- report the error. The child is | * execvp() failed -- report the error. The child is | ||||
* now running, so the exit status doesn't matter. | * now running, so the exit status doesn't matter. | ||||
*/ | */ | ||||
err(1, "%s", argv[0]); | err(1, "%s", argv[0]); | ||||
} | } | ||||
setproctitle("%s[%d]", title, (int)pid); | setproctitle("%s[%d]", title, (int)pid); | ||||
▲ Show 20 Lines • Show All 249 Lines • ▼ Show 20 Lines | |||||
static void | static void | ||||
usage(void) | usage(void) | ||||
{ | { | ||||
(void)fprintf(stderr, | (void)fprintf(stderr, | ||||
"usage: daemon [-cfrS] [-p child_pidfile] [-P supervisor_pidfile]\n" | "usage: daemon [-cfrS] [-p child_pidfile] [-P supervisor_pidfile]\n" | ||||
" [-u user] [-o output_file] [-t title]\n" | " [-u user] [-o output_file] [-t title]\n" | ||||
" [-l syslog_facility] [-s syslog_priority]\n" | " [-l syslog_facility] [-s syslog_priority]\n" | ||||
" [-T syslog_tag] [-m output_mask] [-R restart_delay_secs]\n" | " [-T syslog_tag] [-m output_mask] [-R restart_delay_secs]\n" | ||||
" [-M umask]\n" | |||||
"command arguments ...\n"); | "command arguments ...\n"); | ||||
exit(1); | exit(1); | ||||
} | } |