Пример #1
0
static int fdevent_libev_reset(fdevents *ev) {
	UNUSED(ev);

	ev_default_fork();

	return 0;
}
Пример #2
0
/// Do the polling. If on several threads, this is done in every thread.
void onion_poller_poll(onion_poller *poller){
	ev_default_fork();
	ev_loop_fork(poller->loop);

	poller->stop=0;
	while(!poller->stop){
		sem_wait(poller->sem);
		ev_run(poller->loop,EVLOOP_ONESHOT);
		sem_post(poller->sem);
	}
}
static int set_daemon_mode(requiem_option_t *opt, const char *arg, requiem_string_t *err, void *context)
{
        int ret;

        ret = requiem_daemonize(config.pidfile);
        if ( ret < 0 )
                return ret;

        requiem_log_set_flags(requiem_log_get_flags() | REQUIEM_LOG_FLAGS_SYSLOG);
        ev_default_fork();

        return 0;
}
Пример #4
0
static void
daemonize(void) {
#ifdef HAVE_DAEMON
    if (daemon(0,0) < 0)
        perror_exit("daemon()");
#else
    pid_t pid;

    /* daemon(0,0) part */
    pid = fork();
    if (pid < 0)
        perror_exit("fork()");
    else if (pid != 0)
        exit(EXIT_SUCCESS);

    if (setsid() < 0)
        perror_exit("setsid()");

    if (chdir("/") < 0)
        perror_exit("chdir()");

    if (freopen("/dev/null", "r", stdin) == NULL)
        perror_exit("freopen(stdin)");

    if (freopen("/dev/null", "a", stdout) == NULL)
        perror_exit("freopen(stdout)");

    if (freopen("/dev/null", "a", stderr) == NULL)
        perror_exit("freopen(stderr)");

    pid = fork();
    if (pid < 0)
        perror_exit("fork()");
    else if (pid != 0)
        exit(EXIT_SUCCESS);
#endif

    /* local part */
    umask(022);
    signal(SIGHUP, SIG_IGN);

    ev_default_fork();

    return;
}
int main(int argc, char **argv)
{
        int ret;
        ev_timer evt;
        struct timeval end;
        struct sigaction action;

        /*
         * Initialize libev.
         */
        ev_default_loop(EVFLAG_AUTO);

        /*
         * make sure we ignore sighup until acceptable.
         */
#if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
        action.sa_flags = 0;
        action.sa_handler = SIG_IGN;
        sigemptyset(&action.sa_mask);
        sigaction(SIGHUP, &action, NULL);
#endif

        memset(&start, 0, sizeof(start));
        memset(&end, 0, sizeof(end));

        requiem_init(&argc, argv);
        global_argv = argv;

        REQUIEM_PLUGIN_SET_PRELOADED_SYMBOLS();

        ret = requiem_option_new_root(&logeater_root_optlist);
        if ( ret < 0 )
                return ret;

        ret = log_plugins_init(LOG_PLUGIN_DIR, logeater_root_optlist);
        if (ret < 0)
                return ret;

        requiem_log_debug(1, "Initialized %d logs plugins.\n", ret);

        ret = logeater_options_init(logeater_root_optlist, argc, argv);
        if ( ret < 0 )
                exit(1);

        /*
         * setup signal handling
         */
        action.sa_flags = 0;
        sigemptyset(&action.sa_mask);
        action.sa_handler = sig_handler;

#ifdef SA_INTERRUPT
        action.sa_flags |= SA_INTERRUPT;
#endif

        sigaction(SIGTERM, &action, NULL);
        sigaction(SIGINT, &action, NULL);
        sigaction(SIGABRT, &action, NULL);
#if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
        sigaction(SIGUSR1, &action, NULL);
        sigaction(SIGQUIT, &action, NULL);
        sigaction(SIGHUP, &action, NULL);
#endif

        ret = file_server_start_monitoring();
        if ( ret < 0 && ! config.udp_nserver ) {
                requiem_log(REQUIEM_LOG_WARN, "No file or UDP server available for monitoring: terminating.\n");
                return -1;
        }

        if ( config.daemon_mode ) {
                requiem_daemonize(config.pidfile);
                if ( config.pidfile )
                        free(config.pidfile);

                ev_default_fork();
        }

        ev_timer_init(&evt, libev_timer_cb, 1, 1);
        ev_timer_start(&evt);

        /*
         * Whether we are using batch-mode or file notification, we need
         * to process the currently un-processed entry.
         */
        gettimeofday(&start, NULL);

        do {
                ret = file_server_read_once();
                requiem_timer_wake_up();
        } while ( ret > 0 );

        /*
         * if either FAM or UDP server is enabled, we use polling to know
         * if there are data available for reading. if batch_mode is set,
         * then we revert to reading every data at once.
         */
        if ( ! config.batch_mode )
                wait_for_event();
        else {
                gettimeofday(&end, NULL);

                /*
                 * only call requiem_client_destroy in case we are running in batch
                 * mode, causing an heartbeat to be sent to notice of a normal exit.
                 */
                if ( ! config.dry_run )
                        requiem_client_destroy(config.logeater_client, REQUIEM_CLIENT_EXIT_STATUS_SUCCESS);

                print_stats("", &end);
        }

        requiem_deinit();
        return 0;
}