Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/syslogd/syslogd.c
| Show First 20 Lines • Show All 591 Lines • ▼ Show 20 Lines | addfile(int fd) | ||||
| sl->sl_recv = socklist_recv_file; | sl->sl_recv = socklist_recv_file; | ||||
| STAILQ_INSERT_TAIL(&shead, sl, next); | STAILQ_INSERT_TAIL(&shead, sl, next); | ||||
| } | } | ||||
| int | int | ||||
| main(int argc, char *argv[]) | main(int argc, char *argv[]) | ||||
| { | { | ||||
| struct sigaction act = { }; | struct sigaction act = { }; | ||||
| sigset_t sigset = { }; | |||||
| struct kevent ev; | struct kevent ev; | ||||
| struct socklist *sl; | struct socklist *sl; | ||||
| pid_t ppid = -1, spid; | pid_t ppid = -1, spid; | ||||
| int ch, kq, s; | int ch, kq, s; | ||||
| char *p; | char *p; | ||||
| bool bflag = false, pflag = false, Sflag = false; | bool bflag = false, pflag = false, Sflag = false; | ||||
| if (madvise(NULL, 0, MADV_PROTECT) != 0) | if (madvise(NULL, 0, MADV_PROTECT) != 0) | ||||
| ▲ Show 20 Lines • Show All 224 Lines • ▼ Show 20 Lines | #endif | ||||
| STAILQ_FOREACH(sl, &shead, next) { | STAILQ_FOREACH(sl, &shead, next) { | ||||
| EV_SET(&ev, sl->sl_socket, EVFILT_READ, EV_ADD, 0, 0, sl); | EV_SET(&ev, sl->sl_socket, EVFILT_READ, EV_ADD, 0, 0, sl); | ||||
| if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) { | if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) { | ||||
| warn("failed to add kevent to kqueue"); | warn("failed to add kevent to kqueue"); | ||||
| pidfile_remove(pfh); | pidfile_remove(pfh); | ||||
| exit(1); | exit(1); | ||||
| } | } | ||||
| } | } | ||||
| /* | |||||
| * Syslogd will not reap its children via wait(). | |||||
| * When SIGCHLD is ignored, zombie processes are | |||||
| * not created. A child's PID will be recycled | |||||
| * upon its exit. | |||||
| */ | |||||
| act.sa_handler = SIG_IGN; | |||||
| for (size_t i = 0; i < nitems(sigcatch); ++i) { | for (size_t i = 0; i < nitems(sigcatch); ++i) { | ||||
| EV_SET(&ev, sigcatch[i], EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); | EV_SET(&ev, sigcatch[i], EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); | ||||
| if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) { | if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) { | ||||
| warn("failed to add kevent to kqueue"); | warn("failed to add kevent to kqueue"); | ||||
| pidfile_remove(pfh); | pidfile_remove(pfh); | ||||
| exit(1); | exit(1); | ||||
| } | } | ||||
| (void)sigaddset(&sigset, sigcatch[i]); | if (sigaction(sigcatch[i], &act, NULL) == -1) { | ||||
| } | warn("failed to apply signal handler"); | ||||
| if (sigprocmask(SIG_BLOCK, &sigset, NULL) != 0) { | |||||
| warn("failed to apply signal mask"); | |||||
| pidfile_remove(pfh); | pidfile_remove(pfh); | ||||
| exit(1); | exit(1); | ||||
| } | } | ||||
| } | |||||
| (void)alarm(TIMERINTVL); | (void)alarm(TIMERINTVL); | ||||
| /* Do not create zombie processes. */ | |||||
| act.sa_flags = SA_NOCLDWAIT; | |||||
| if (sigaction(SIGCHLD, &act, NULL) == -1) { | |||||
| warn("failed to apply signal handler"); | |||||
| pidfile_remove(pfh); | |||||
| exit(1); | |||||
| } | |||||
| /* tuck my process id away */ | /* tuck my process id away */ | ||||
| pidfile_write(pfh); | pidfile_write(pfh); | ||||
| dprintf("off & running....\n"); | dprintf("off & running....\n"); | ||||
| init(false); | init(false); | ||||
| for (;;) { | for (;;) { | ||||
| if (needdofsync) { | if (needdofsync) { | ||||
| dofsync(); | dofsync(); | ||||
| ▲ Show 20 Lines • Show All 2,720 Lines • ▼ Show 20 Lines | |||||
| /* | /* | ||||
| * Fairly similar to popen(3), but returns an open descriptor, as | * Fairly similar to popen(3), but returns an open descriptor, as | ||||
| * opposed to a FILE *. | * opposed to a FILE *. | ||||
| */ | */ | ||||
| static int | static int | ||||
| p_open(const char *prog, int *rpd) | p_open(const char *prog, int *rpd) | ||||
| { | { | ||||
| sigset_t sigset = { }; | struct sigaction act = { }; | ||||
| int pfd[2], pd; | int pfd[2], pd; | ||||
| pid_t pid; | pid_t pid; | ||||
| char *argv[4]; /* sh -c cmd NULL */ | char *argv[4]; /* sh -c cmd NULL */ | ||||
| char errmsg[200]; | char errmsg[200]; | ||||
| if (pipe(pfd) == -1) | if (pipe(pfd) == -1) | ||||
| return (-1); | return (-1); | ||||
| switch ((pid = pdfork(&pd, PD_CLOEXEC))) { | switch ((pid = pdfork(&pd, PD_CLOEXEC))) { | ||||
| case -1: | case -1: | ||||
| return (-1); | return (-1); | ||||
| case 0: | case 0: | ||||
| (void)setsid(); /* Avoid catching SIGHUPs. */ | (void)setsid(); /* Avoid catching SIGHUPs. */ | ||||
| argv[0] = strdup("sh"); | argv[0] = strdup("sh"); | ||||
| argv[1] = strdup("-c"); | argv[1] = strdup("-c"); | ||||
| argv[2] = strdup(prog); | argv[2] = strdup(prog); | ||||
| argv[3] = NULL; | argv[3] = NULL; | ||||
| if (argv[0] == NULL || argv[1] == NULL || argv[2] == NULL) { | if (argv[0] == NULL || argv[1] == NULL || argv[2] == NULL) { | ||||
| logerror("strdup"); | logerror("strdup"); | ||||
| exit(1); | exit(1); | ||||
| } | } | ||||
| alarm(0); | alarm(0); | ||||
| act.sa_handler = SIG_DFL; | |||||
| for (size_t i = 0; i < nitems(sigcatch); ++i) | for (size_t i = 0; i < nitems(sigcatch); ++i) { | ||||
| (void)sigaddset(&sigset, sigcatch[i]); | if (sigaction(sigcatch[i], &act, NULL) == -1) { | ||||
| if (sigprocmask(SIG_UNBLOCK, &sigset, NULL) != 0) { | logerror("sigaction"); | ||||
| logerror("sigprocmask"); | |||||
| exit(1); | exit(1); | ||||
| } | |||||
| } | } | ||||
| dup2(pfd[0], STDIN_FILENO); | dup2(pfd[0], STDIN_FILENO); | ||||
| dup2(nulldesc, STDOUT_FILENO); | dup2(nulldesc, STDOUT_FILENO); | ||||
| dup2(nulldesc, STDERR_FILENO); | dup2(nulldesc, STDERR_FILENO); | ||||
| closefrom(STDERR_FILENO + 1); | closefrom(STDERR_FILENO + 1); | ||||
| (void)execvp(_PATH_BSHELL, argv); | (void)execvp(_PATH_BSHELL, argv); | ||||
| ▲ Show 20 Lines • Show All 176 Lines • Show Last 20 Lines | |||||