int main(int argc, char *argv[]) { struct sigaction sa; xmlrpc_env env; if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) crit("could not ignore SIGPIPE"); sa.sa_handler = sighandler; sa.sa_flags = 0; sigemptyset (&sa.sa_mask); sigaction (SIGHUP, &sa, NULL); sigaction (SIGALRM, &sa, NULL); sigaction (SIGCHLD, &sa, NULL); set_progname(argv[0]); parse_cmdline(argc, argv); logging_init(); #ifdef HAVE_LIBPCAP fg_pcap_init(); #endif /* HAVE_LIBPCAP */ if (log_type == LOGTYPE_SYSLOG) { /* Need to call daemon() before creating the thread because * it internally calls fork() which does not copy threads. */ if (daemon(0, 0) == -1) crit("daemon() failed"); logging_log(LOG_NOTICE, "flowgrindd daemonized"); } if (cpu >= 0) set_affinity(cpu); create_daemon_thread(); xmlrpc_env_init(&env); run_rpc_server(&env, port); critx("control should never reach end of main()"); }
static void parse_option(int argc, char ** argv) { int ch, rc; int argcorig = argc; #ifdef HAVE_GETOPT_LONG /* getopt_long isn't portable, it's GNU extension */ struct option lo[] = { {"help", 0, 0, 'h' }, {"version", 0, 0, 'v'}, {"debug", 0, 0, 'd'}, {0, 0, 0, 0} }; while ((ch = getopt_long(argc, argv, "dDhp:vVw:W:", lo, 0)) != -1) { #else while ((ch = getopt(argc, argv, "dDhp:vVw:W:")) != -1) { #endif switch (ch) { case 'h': usage(); break; case 'd': case 'D': log_type = LOGTYPE_STDERR; increase_debuglevel(); break; case 'p': rc = sscanf(optarg, "%u", &port); if (rc != 1) { fprintf(stderr, "failed to " "parse port number.\n"); usage(); } break; case 'v': case 'V': fprintf(stderr, "flowgrindd version: %s\n", FLOWGRIND_VERSION); exit(0); case 'w': case 'W': #ifdef HAVE_LIBPCAP dump_filename_prefix_server = optarg; break; #endif default: usage(); } } argc = argcorig; argc -= optind; if (argc != 0) usage(); } int main(int argc, char ** argv) { struct sigaction sa; xmlrpc_env env; /* update progname from argv[0] */ if (argc > 0) { /* Strip path */ char *tok = strrchr(argv[0], '/'); if (tok) tok++; else tok = argv[0]; if (*tok) { strncpy(progname, tok, sizeof(progname)); progname[sizeof(progname) - 1] = 0; } } if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { error(ERR_FATAL, "Could not ignore SIGPIPE: %s", strerror(errno)); /* NOTREACHED */ } sa.sa_handler = sighandler; sa.sa_flags = 0; sigemptyset (&sa.sa_mask); sigaction (SIGHUP, &sa, NULL); sigaction (SIGALRM, &sa, NULL); sigaction (SIGCHLD, &sa, NULL); parse_option(argc, argv); logging_init(); #ifdef HAVE_LIBPCAP fg_pcap_init(); #endif if (log_type == LOGTYPE_SYSLOG) { /* Need to call daemon() before creating the thread because * it internally calls fork() which does not copy threads. */ if (daemon(0, 0) == -1) { error(ERR_FATAL, "daemon() failed: %s", strerror(errno)); } logging_log(LOG_NOTICE, "flowgrindd daemonized"); } create_daemon_thread(); xmlrpc_env_init(&env); run_rpc_server(&env, port); fprintf(stderr, "Control should never reach end of main()\n"); return 0; }