static int start() { int lockfd; int pipe3fd[2]; char temp_buf; lockfd=ll_daemon_start(LOCKFILE); if (lockfd <= 0) { close(3); return (lockfd); } if (lockfd == 3) { lockfd=dup(lockfd); /* Get it out of the way */ if (lockfd < 0) { perror("dup"); return (-1); } } if (reserve3(pipe3fd)) { return (1); } fcntl(lockfd, F_SETFD, FD_CLOEXEC); fcntl(pipe3fd[0], F_SETFD, FD_CLOEXEC); fcntl(3, F_SETFD, FD_CLOEXEC); /* For the logger */ clog_start_logger("courierfilter"); clog_open_stderr(0); fcntl(3, F_SETFD, 0); signal(SIGPIPE, SIG_IGN); dup2(2, 1); sighup(); close(0); open("/dev/null", O_RDONLY); close(3); if (read(pipe3fd[0], &temp_buf, 1) != 1) ; /* ignore */ close(pipe3fd[0]); signal(SIGHUP, sighuphandler); signal(SIGTERM, sigtermhandler); ll_daemon_started(PIDFILE, lockfd); while (!sigterm_received) { if (sighup_received) { sighup_received=0; if (reserve3(pipe3fd) == 0) { sighup(); close(3); close(pipe3fd[0]); } signal(SIGHUP, sighuphandler); } #if HAVE_SIGSUSPEND { sigset_t ss; sigemptyset(&ss); sigsuspend(&ss); } #else sigpause(0); #endif } while (filterlist) kill1filter(&filterlist); unlink(PIDFILE); return (0); }
int main(int argc, char **argv) { int facility = LOG_DEST; int daemon = 0; int lockfd = -1; const char *userarg = 0; const char *grouparg = 0; int droproot=0; if (argc == 2 && argv[1][0] != '-') { /* backwards-compatibility mode */ close(1); close(2); checkfd(open("/dev/null", O_WRONLY), 1); checkfd(open("/dev/null", O_WRONLY), 2); do_logger(argv[1], facility, stdin); exit(0); } if (argc <= 1) { fprintf(stderr, "Usage: courierlogger [-name=name] [-pid=pidfile] [-facility=type]\n" " [-start|-stop|-restart] [cmd [args...]]\n" ); exit(1); } argv++, argc--; while (argc > 0 && argv[0][0]=='-') { if (strncmp(argv[0],"-pid=",5) == 0 && argv[0][5]) { pidarg=&argv[0][5]; lockfilename=malloc(strlen(pidarg)+sizeof(".lock")); if (!lockfilename) { perror("malloc"); exit(1); } strcat(strcpy(lockfilename, pidarg), ".lock"); } else if (strncmp(argv[0],"-name=",6) == 0 && argv[0][6]) namearg=&argv[0][6]; else if (strncmp(argv[0],"-user="******"-group=",7) == 0) grouparg=argv[0]+7; else if (strcmp(argv[0], "-droproot") == 0) droproot=1; else if (strncmp(argv[0],"-facility=",10) == 0) { struct lognames *p = facilitynames; while (p->name && strcmp(p->name, &argv[0][10])) p++; if (p->name == 0) { fprintf(stderr, "Unknown facility name '%s'\n", &argv[0][10]); exit(1); } facility = p->value; } else if (strcmp(argv[0],"-start") == 0) daemon = 1; else if (strcmp(argv[0],"-stop") == 0) daemon = 2; else if (strcmp(argv[0],"-restart") == 0) daemon = 3; else if (strcmp(argv[0],"-respawn") == 0) respawn = 1; else { fprintf(stderr, "Unknown option '%s'\n", argv[0]); exit(1); } argv++, argc--; } if (daemon && !pidarg) { fprintf(stderr, "-pid argument required\n"); exit(1); } if (!daemon && pidarg) daemon = 1; /* -start implied */ if (!namearg && daemon != 2 && daemon != 3) { /* choose a default name based on the program we're running */ if (argc <= 0 || !argv[0] || !argv[0][0]) { fprintf(stderr, "-name option required for standalone logger\n"); exit(1); } namearg = strrchr(argv[0],'/'); namearg = namearg ? namearg+1 : argv[0]; } switch (daemon) { case 1: /* start */ if (argc <= 0 || !argv[0] || !argv[0][0]) { fprintf(stderr, "-start must be followed by a command to execute\n"); exit(1); } lockfd=ll_daemon_start(lockfilename); if (lockfd < 0) { perror("ll_daemon_start"); exit(1); } startchild(argv, droproot ? userarg:NULL, droproot ? grouparg:NULL); ll_daemon_started(pidarg, lockfd); break; case 2: /* stop */ exit(ll_daemon_stop(lockfilename, pidarg)); case 3: /* restart */ exit(ll_daemon_restart(lockfilename, pidarg)); default: /* run in foreground, with or without a child process */ if (argc > 0) startchild(argv, droproot ? userarg:NULL, droproot ? grouparg:NULL); } setuidgid(userarg, grouparg); while (1) { int waitstat; pid_t p2; FILE *f = fdopen(0, "r"); do_logger(namearg, facility, f); fclose(f); if (child_pid < 0) break; while ((p2=wait(&waitstat)) != child_pid && (p2 != -1 || errno != ECHILD)) ; if (hup_restart) hup_restart = 0; else if (respawn) sleep (5); else break; startchild(argv, NULL, NULL); } exit(0); }
static void start() { int lockfd=ll_daemon_start(LOCKFILE); int pubsock; int privsock; if (lockfd < 0) { perror(LOCKFILE); exit(1); } if ((pubsock=pcp_mksocket(PUBDIR, "PCPDLOCAL")) < 0) { exit(1); } if ((privsock=pcp_mksocket(PRIVDIR, "PCPDLOCAL")) < 0) { exit (1); } ll_daemon_resetio(); ll_daemon_started(PIDFILE, lockfd); #if USE_NOCLDWAIT { struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler=SIG_IGN; sa.sa_flags=SA_NOCLDWAIT; sigaction(SIGCHLD, &sa, NULL); } #else signal(SIGCHLD, SIG_IGN); #endif for (;;) { fd_set fds; struct timeval tv; int rc; FD_ZERO(&fds); FD_SET(pubsock, &fds); FD_SET(privsock, &fds); tv.tv_sec=authtoken_check(); tv.tv_usec=0; if ((rc=select ( (pubsock > privsock ? pubsock:privsock)+1, &fds, NULL, NULL, &tv)) < 0) { perror("pcpd: select"); continue; } if (rc == 0) continue; if (FD_ISSET(pubsock, &fds)) accept_pcpd(pubsock, pubsock, privsock, 0); if (FD_ISSET(privsock, &fds)) accept_pcpd(privsock, pubsock, privsock, 1); } }