Esempio n. 1
0
void
Init_native_thread(void)
{
    rb_thread_t *th = GET_THREAD();

    pthread_key_create(&ruby_native_thread_key, NULL);
    th->thread_id = pthread_self();
    native_thread_init(th);
#ifdef USE_SIGNAL_THREAD_LIST
    native_mutex_initialize(&signal_thread_list_lock);
#endif
    posix_signal(SIGVTALRM, null_func);
}
Esempio n. 2
0
static void coredump_handler(int signum)
{
    if (coredump && *coredumpdir) {
        log_msg(LOG_ERR, "Terminating on signal=%d (check \"%s\" for core)",
            signum, coredumpdir);
    }
    else {
        log_msg(LOG_ERR, "Terminating on signal=%d", signum);
    }

    umask(077);
    if (coredump && *coredumpdir) {
        (void) chdir(coredumpdir);
    }
    posix_signal(signum, SIG_DFL);
    (void) kill(getpid(), signum);

    return;
}
Esempio n. 3
0
static void setup_signals(server_conf_t *conf)
{
    posix_signal(SIGCHLD, sig_chld_handler);
    posix_signal(SIGHUP, sig_hup_handler);
    posix_signal(SIGINT, exit_handler);
    posix_signal(SIGPIPE, SIG_IGN);
    posix_signal(SIGTERM, exit_handler);

    /*  These signals have a default action of terminate+core according to SUS.
     */
    posix_signal(SIGABRT, coredump_handler);
    posix_signal(SIGBUS, coredump_handler);
    posix_signal(SIGFPE, coredump_handler);
    posix_signal(SIGILL, coredump_handler);
    posix_signal(SIGQUIT, coredump_handler);
    posix_signal(SIGSEGV, coredump_handler);
    return;
}
Esempio n. 4
0
static void begin_daemonize(int *fd_ptr, pid_t *pgid_ptr)
{
/*  Begins the daemonization of the process.
 *  Despite the fact that this routine backgrounds the process, control
 *    will not be returned to the shell until end_daemonize() is called.
 *  If 'fd_ptr' is non-null, it will be set to the fd to pass to
 *    end_daemonize() in order to complete the daemonization.
 *  If 'pgid_ptr' is non-null, it will be set to the daemonized
 *    process group ID.
 */
    int fds[2];
    pid_t pid;
    int n;
    signed char priority;
    char ebuf[1024];
    pid_t pgid;

    /*  Clear file mode creation mask.
     */
    umask(0);

    /*  Create pipe for IPC so parent process will wait to terminate until
     *    signaled by grandchild process.  This allows messages written to
     *    stdout/stderr by the grandchild to be properly displayed before
     *    the parent process returns control to the shell.
     */
    if (pipe(fds) < 0) {
        log_err(errno, "Unable to create daemon pipe");
    }
    /*  Set the fd used by log_err() to return status back to the parent.
     */
    log_set_err_pipe(fds[1]);

    /*  Automatically background the process and
     *    ensure child is not a process group leader.
     */
    if ((pid = fork()) < 0) {
        log_err(errno, "Unable to create child process");
    }
    else if (pid > 0) {
        log_set_err_pipe(-1);
        /*
         *  FIXME: The following log_err()s don't necessarily indicate the
         *    daemon has failed since the grandchild process may still be
         *    running.  This could confuse the user.  Should they be warnings
         *    instead?
         */
        if (close(fds[1]) < 0) {
            log_err(errno, "Unable to close write-pipe in parent process");
        }
        if ((n = read(fds[0], &priority, sizeof(priority))) < 0) {
            log_err(errno, "Unable to read status from grandchild process");
        }
        if ((n > 0) && (priority >= 0)) {
            if ((n = read(fds[0], ebuf, sizeof(ebuf))) < 0) {
                log_err(errno,
                    "Unable to read error message from grandchild process");
            }
            if ((n > 0) && (ebuf[0] != '\0')) {
                log_set_file(stderr, priority, 0);
                log_msg(priority, "%s", ebuf);
            }
            exit(1);
        }
        exit(0);
    }
    if (close(fds[0]) < 0) {
        log_err(errno, "Unable to close read-pipe in child process");
    }
    /*  Become a session leader and process group leader
     *    with no controlling tty.
     */
    if ((pgid = setsid()) < 0) {
        log_err(errno, "Unable to disassociate controlling tty");
    }
    /*  Ignore SIGHUP to keep child from terminating when
     *    the session leader (ie, the parent) terminates.
     */
    posix_signal(SIGHUP, SIG_IGN);

    /*  Abdicate session leader position in order to guarantee
     *    daemon cannot automatically re-acquire a controlling tty.
     */
    if ((pid = fork()) < 0) {
        log_err(errno, "Unable to create grandchild process");
    }
    else if (pid > 0) {
        exit(0);
    }
    /*  Return.
     */
    if (fd_ptr) {
        *fd_ptr = fds[1];
    }
    if (pgid_ptr) {
        *pgid_ptr = pgid;
    }
    return;
}
Esempio n. 5
0
/*
 * Handling signals on parent
 */
void 
handle_signals_parent( void )
{
    posix_signal( SIGIO,   SIG_IGN );
    posix_signal( SIGURG,  SIG_IGN );
    posix_signal( SIGTSTP, SIG_IGN );
    posix_signal( SIGQUIT, SIG_IGN );
    posix_signal( SIGPIPE, SIG_IGN );
#if !defined(OPENBSD) && !defined(FREEBSD) && !defined(DARWIN)
    posix_signal( SIGPOLL, SIG_IGN );
#endif
    posix_signal( SIGUSR1, final_parent );
    posix_signal( SIGUSR2, SIG_IGN );
    posix_signal( SIGINT,  SIG_IGN );
    posix_signal( SIGTERM, SIG_IGN );
    posix_signal( SIGHUP,  SIG_IGN );
}