Exemplo n.º 1
0
int
do_daemonize(void)
{
    pid_t        child;
    unsigned int i;

    if ((child = fork()) == (pid_t) -1) {
        logger_error(NULL, "Unable to fork() in order to daemonize");
        return -1;
    } else if (child != (pid_t) 0) {
        _exit(0);
    }
    if (setsid() == (pid_t) -1) {
        logger_error(NULL, "Unable to setsid()");
    }
    i = open_max();
    do {
        if (isatty((int) i)) {
            (void) close((int) i);
        }
        i--;
    } while (i > 2U);
    if (closedesc_all(1) != 0) {
        logger_error(NULL, _PATH_DEVNULL " duplication");
        return -1;
    }
    return 0;
}
Exemplo n.º 2
0
static void dodaemonize(void)
{
    pid_t child;

    if (daemonize != 0) {
        if ((child = fork()) == (pid_t) -1) {
            perror("Daemonization failed - fork");
            return;
        } else if (child != (pid_t) 0) {
            _exit(EXIT_SUCCESS);
        } else if (setsid() == (pid_t) -1) {
            perror("Daemonization failed : setsid");
        }
        (void) chdir("/");
#ifdef HAVE_CLOSEFROM
        (void) closefrom(3);
#endif
        (void) closedesc_all(0);
    }
}
Exemplo n.º 3
0
static int run(const char * const who, const char * const file,
               const int upload_pipe_fd)
{
    struct stat st;
    pid_t pid;

    if (script == NULL || *script == 0 ||
        file == NULL || *file == 0 ||
        lstat(file, &st) < 0 ||
        !S_ISREG(st.st_mode)) {
        return -1;
    }
    pid = fork();
    if (pid == (pid_t) 0) {
        /* Yes, there's already the cloexec flag on this fd,
         * but it's really important to close it. Be paranoid.
         */
        if (close(upload_pipe_fd) < 0 || closedesc_all(1) < 0) {
            _exit(EXIT_FAILURE);
        }
        fillenv(who, &st);
        execl(script, script, file, (char *) NULL);
        _exit(EXIT_FAILURE);
    } else if (pid != (pid_t) -1) {
#ifdef HAVE_WAITPID
        (void) waitpid(pid, NULL, 0);
#else
        {
            pid_t foundpid;

            while ((foundpid = wait3(NULL, 0, NULL)) != (pid_t) -1 &&
                   foundpid != pid);
        }
#endif
    }

    return 0;
}