int main(int argc,char *argv[])
{
    init_files();
    int  srvfd;
    
    if (server_init() < 0) {
        return -1;
    }

    srvfd = create_server_proc(IPADDR, PORT);
    if (srvfd < 0) {
        fprintf(stderr, "socket create or bind fail.\n");
        server_uninit();
        return -1;
        }

    handle_client_proc(srvfd);

    return 0;

    
}
Exemplo n.º 2
0
int
main(int argc, char* argv[])
{
	const char** local = NULL;
	int n_local = 0;
    const char* pidfile = NULL;
    int daemonize = 1;
    char ch;
    char* t;

#ifdef TEST
    test(argc, argv);
    return 1;
#endif

    /* Initialize the state stuff */
    memset(&g_state, 0, sizeof(g_state));

    g_state.rrddir = DEFAULT_WORK;
    g_state.confdir = DEFAULT_CONFIG;
    g_state.retries = DEFAULT_RETRIES;
    g_state.timeout = DEFAULT_TIMEOUT;

    /* Parse the arguments nicely */
    while((ch = getopt(argc, argv, "b:c:d:m:Mp:r:t:w:V")) != -1)
    {
        switch(ch)
        {

        /* Bind address */
        case 'b':
            local = xrealloc (local, sizeof (char*) * (n_local + 2));
            local[n_local] = optarg;
            local[++n_local] = NULL;
            break;

        /* Config directory */
        case 'c':
            g_state.confdir = optarg;
            break;

        /* Don't daemonize */
        case 'd':
            daemonize = 0;
            debug_level = strtol(optarg, &t, 10);
            if(*t || debug_level > 4)
                errx(1, "invalid debug log level: %s", optarg);
            debug_level += LOG_ERR;
            break;

        /* mib directory */
        case 'm':
            mib_directory = optarg;
            break;

        /* MIB load warnings */
        case 'M':
            mib_warnings = 1;
            break;

        /* Write out a pid file */
        case 'p':
            pidfile = optarg;
            break;

        /* The number of SNMP retries */
        case 'r':
            g_state.retries = strtol(optarg, &t, 10);
            if(*t || g_state.retries < 0)
                errx(1, "invalid number of retries: %s", optarg);
            break;

        /* The default timeout */
        case 't':
            g_state.timeout = strtol(optarg, &t, 10);
            if(*t || g_state.timeout <= 0)
                errx(1, "invalid timeout (must be above zero): %s", optarg);
            break;

        /* The work directory */
        case 'w':
            g_state.rrddir = optarg;
            break;

        /* Print version number */
        case 'V':
            version();
            break;

        /* Usage information */
        case '?':
        default:
            usage();
            break;
        }
    }

    argc -= optind;
    argv += optind;

    if(argc != 0)
        usage();

    /* No bind addresses specified, use defaults... */
    if (local == NULL) {
        local = xrealloc (local, sizeof (char*) * 3);
        local[0] = "0.0.0.0";
        local[1] = NULL;
#ifdef HAVE_INET6
        local[1] = "::";
        local[2] = NULL;
#endif
    }

    /* The mainloop server */
    server_init();

    /* Parse config and setup SNMP system */
    rb_config_parse();

    /* As an optimization we unload the MIB processing data here */
    mib_uninit();

    /* Rev up the main engine */
    snmp_engine_init (local, g_state.retries);
    rb_poll_engine_init();

    free (local);
    n_local = 0;
    local = NULL;

    if(daemonize)
    {
        /* Fork a daemon nicely */
        if(daemon(0, 0) == -1)
            err(1, "couldn't fork as a daemon");

        log_debug("running as a daemon");
        daemonized = 1;
    }

    /* Setup the Async DNS resolver */
    if(async_resolver_init() < 0)
    {
        log_error("couldn't initialize resolver");
        /* Allow things to proceed without resolver */
    }

    /* Handle signals */
    signal(SIGPIPE, SIG_IGN);
    signal(SIGHUP, SIG_IGN);
    signal(SIGINT,  on_quit);
    signal(SIGTERM, on_quit);
    siginterrupt(SIGINT, 1);
    siginterrupt(SIGTERM, 1);

    /* Open the system log */
    openlog("rrdbotd", 0, LOG_DAEMON);

    if(pidfile != NULL)
        writepid(pidfile);

    log_info("rrdbotd version " VERSION " started up");

    /* Now let it go */
    if(server_run() == -1)
        err(1, "critical failure running SNMP engine");

    log_info("rrdbotd stopping");

    /* Cleanups */
    rb_poll_engine_uninit();
    snmp_engine_stop();
    rb_config_free();
    async_resolver_uninit();
    server_uninit();

    if(pidfile != NULL)
        removepid(pidfile);

    return 0;
}