Example #1
0
File: nlmon.c Project: el8/nlmon
int main(int argc, char* argv[])
{
	pthread_t proc_events_thread;
	void *status;
	int rc, opt, cycles = INT_MAX;

#ifdef DEBUG_ENABLED
	logfile = fopen(DEBUG_LOGFILE, "w");
	if (logfile == NULL)
		DIE_PERROR("Cannot open file " DEBUG_LOGFILE " for writing");
#endif

#ifdef CONFIG_NCURSES
	/* default is ncurses output */
	output = &oops_ncurses;
#else
	output = &oops_stdout;
#endif

	while (1) {
		int option_index = 0;
		static struct option long_options[] = {
			{ "realtime",	no_argument,		&opt_realtime, 1},
			{ "all_cpus",	no_argument,		&opt_all_cpus, 1},
			{ "sort",	required_argument,	0,  's'},
			{ "output",	required_argument,	0,  'o'},
			{ "seconds",	required_argument,	0,  't' },
			{ "milliseconds",required_argument,	0,  'm' },
			{ "cycles",	required_argument,	0,  'c' },
			{ "help",	no_argument,		0,  'h' },
			{ 0, 0, 0, 0 },
		};

		opt = getopt_long(argc, argv, "s:o:c:h?", long_options, &option_index);
		if (opt == -1)
			break;

		switch (opt) {
		case 'o':
			if (strcmp(optarg, "csv") == 0)
				output = &oops_csv;
			else if (strcmp(optarg, "stdout") == 0)
				output = &oops_stdout;
#ifdef CONFIG_NCURSES
			else if (strcmp(optarg, "ncurses") == 0)
				output = &oops_ncurses;
#endif
			else if (strcmp(optarg, "nop") == 0)
				output = &oops_nop;
			else { /* unknown */
				fprintf(stderr, "Unknown output method %s\n", optarg);
				print_help(argc, argv);
			}
			break;
		case 's':
			if (strcmp(optarg, "name") == 0)
				opt_sort = OPT_SORT_NAME;
			else if (strcmp(optarg, "id") == 0)
				opt_sort = OPT_SORT_TID;
			else if (strcmp(optarg, "time") == 0)
				opt_sort = OPT_SORT_TIME;
			else if (strcmp(optarg, "io") == 0)
				opt_sort = OPT_SORT_IO;
			else if (strcmp(optarg, "mem") == 0)
				opt_sort = OPT_SORT_MEM;
			else { /* unknown */
				fprintf(stderr, "Unknown sort method %s\n", optarg);
				print_help(argc, argv);
			}
			break;
		case 't':
			target.tv_sec = atoi(optarg);
			break;
		case 'm':
			target.tv_nsec = atol(optarg) * NSECS_PER_MSEC;
			break;
		case 'c':
			cycles = atoi(optarg);
			break;
		case 0:
			break;
		case '?':
		case 'h':
		default:
			print_help(argc, argv);
		}
	}

	nr_cpus = get_nr_cpus();

	bm_alloc(PID_MAX);
	rc = pthread_create(&proc_events_thread, NULL, proc_events_main, NULL);
	if (rc)
		DIE_PERROR("pthread_create failed");
	pthread_setname_np(proc_events_thread, "nlmon-pevent");

	setup_netlink();
	start_task_monitor();

	while (!procfs_thread) {
		DEBUG("...\n");
		pthread_yield();
		__sync_synchronize();
	}

	rc = pthread_join(procfs_thread, &status);
	if (rc)
		DIE_PERROR("pthread_join failed");
	else
		DEBUG("procfs scan thread exited\n");

	data_init_cpu();
	if (opt_realtime)
		elevate_prio();

	cache_init();
	output->init_output();
	while (cycles--)
		measure_one_cycle();
	output->exit_output();
	stop_task_monitor();
	exit(EXIT_SUCCESS);
}
Example #2
0
int main(int argc, char **argv) 
{

    /*
     *  Check for superuser privileges
     */

    if (geteuid()) {
        printf("Running %s requires superuser privileges! Exiting...\n", LISPD);
        exit(EXIT_FAILURE);
    }

    /*
     *  Initialize the random number generator
     */
     
    iseed = (unsigned int) time (NULL);
    srandom(iseed);

    /*
     * Set up signal handlers
     */

    signal(SIGHUP,  signal_handler);
    signal(SIGTERM, signal_handler);
    signal(SIGINT,  signal_handler);
    signal(SIGQUIT, signal_handler);

    /*
     *  set up syslog now, checking to see if we're daemonizing...
     */

    set_up_syslog();

    /*
     *  Unload/load LISP kernel modules
     */

    system("/sbin/modprobe -r lisp lisp_int");

    if (system("/sbin/modprobe lisp")) {
        syslog(LOG_DAEMON, "Loading the 'lisp' kernel module failed! Exiting...");
        exit(EXIT_FAILURE);
    }
    syslog(LOG_DAEMON, "Loaded the 'lisp' kernel module");
    sleep(1);

    if (system("/sbin/modprobe lisp_int")) {
        syslog(LOG_DAEMON, "Loading the 'lisp_int' kernel module failed! Exiting...");
        exit(EXIT_FAILURE);
    }
    syslog(LOG_DAEMON, "Loaded the 'lisp_int' kernel module");
    sleep(1);

    /*
     *  Setup LISP and routing netlink sockets
     */

    if (!setup_netlink()) {
        syslog(LOG_DAEMON, "Can't set up netlink socket for lisp_mod communication");
        exit(EXIT_FAILURE);
    }

    if (!setup_netlink_iface()) {
        syslog(LOG_DAEMON, "Can't set up netlink socket for interface events");
        exit(EXIT_FAILURE);
    }

    syslog(LOG_DAEMON, "Netlink sockets created");

    /*
     *  set up databases
     */

    AF4_database  = New_Patricia(sizeof(struct in_addr)  * 8);
    AF6_database  = New_Patricia(sizeof(struct in6_addr) * 8);

    /*
     *  Parse command line options
     */

    handle_lispd_command_line(argc, argv);

    // Modified by acabello
    // init_datacache has the following parameters:
    // void (*cbk)(datacache_elt_t*); -> callback function (see example in lispd_lib.c)
    if (!init_datacache(callback_elt)) {
        syslog(LOG_DAEMON, "malloc (datacache): %s", strerror(errno));
        exit(EXIT_FAILURE);
    }

    /*
     *  Now do the config file
     */

    handle_lispd_config_file();

    /*
     * now build the v4/v6 receive sockets
     */

    if (build_receive_sockets() == 0) 
        exit(EXIT_FAILURE);

    /*
     *  create timers
     */

    if ((map_register_timer_fd = timerfd_create(CLOCK_REALTIME, 0)) == -1)
        syslog(LOG_INFO, "Could not create periodic map register timer");

    /*
     *  see if we need to daemonize, and if so, do it
     */

    if (daemonize) {
        syslog(LOG_INFO, "Starting the daemonizing process");
        if ((pid = fork()) < 0) {
            exit(EXIT_FAILURE);
        } 
        umask(0);
        if (pid > 0)
            exit(EXIT_SUCCESS);
        if ((sid = setsid()) < 0)
            exit(EXIT_FAILURE);
        if ((chdir("/")) < 0)
            exit(EXIT_FAILURE);
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
    }

    /* PN XXX
     * comment out test definition to avoid any
     * interactions with the data plane
     */



#define test
#ifdef test

    int ret = register_lispd_process();
    if (ret < 0) {
        syslog(LOG_INFO, "Couldn't register lispd process, err: %d", ret);
        exit(EXIT_FAILURE);
    }
    syslog(LOG_DAEMON, "Registered lispd with kernel module");

    ret = install_database_mappings();
    if (ret < 0) {
        syslog(LOG_INFO, "Could not install database mappings, err: %d", ret);
        exit(EXIT_FAILURE);
    }
    syslog(LOG_DAEMON, "Installed database mappings");

    ret = install_map_cache_entries();
    if (ret < 0) {
        syslog(LOG_INFO, "Could not install static map-cache entries, err: %d", ret);
    }

#endif

    /*
     *  Dump routing table so we can get the gateway address for source routing
     */

    if (!dump_routing_table(AF_INET, RT_TABLE_MAIN))
        syslog(LOG_INFO, "Dumping main routing table failed");

    /*
     *  Register to the Map-Server(s)
     */

    if (!map_register(AF6_database))
        syslog(LOG_INFO, "Could not map register AF_INET6 with Map Servers");

    if (!map_register(AF4_database))
        syslog(LOG_INFO, "Could not map register AF_INET with Map Servers");

    event_loop();
    syslog(LOG_INFO, "Exiting...");         /* event_loop returned bad */
    closelog();
    return(0);
}
Example #3
0
int main(int argc, char **argv)
{
    int    fd                           = 0;
    pid_t  pid				= 0;	/* child pid */
    pid_t  sid				= 0;

    init();

    /*
     *	set up databases
     */
    if (!db_init()) {
        log_msg(INFO, "Couldn't create databases");
        exit(EXIT_FAILURE);
    }

    /*
     *	Parse command line options
     */
    handle_lispd_command_line(argc, argv);

    /*
     *	see if we need to daemonize, and if so, do it
     */
    if (lispd_config.daemonize) {
        log_msg(INFO, "lispd is backgrounding...");
        if ((pid = fork()) < 0) {
            exit(EXIT_FAILURE);
        }

        if (pid > 0) {
            log_msg(INFO, "done. Running as PID %d", pid);
            exit(EXIT_SUCCESS);
        }
        umask(0);
        if ((sid = setsid()) < 0)
            exit(EXIT_FAILURE);
        if ((chdir("/")) < 0)
            exit(EXIT_FAILURE);

        /*
         * Redirect standard files to /dev/null save fd in
         * case we need to get back to stdout later
         */
        fd = dup(fileno(stdout));
    }

    signal(SIGHUP,  signal_handler);
    signal(SIGTERM, signal_handler);
    signal(SIGINT,  signal_handler);
    signal(SIGQUIT, signal_handler);

    init_timers();

    /*
     * Check if lispd is already running. Only allow one instance!
     */
    if (!get_process_lock(getpid())) {
        log_msg(FATAL, "lispd already running, please stop before restarting. If this seems wrong"
                " remove %s.", LISPD_LOCKFILE);


        printf("lispd already running, please stop before restarting.\n If this appears wrong,"
               " remove %s.\n", LISPD_LOCKFILE);
        exit(EXIT_FAILURE);
    }

    if (!setup_netlink()) {
        log_msg(FATAL, "Can't set up netlink socket (is the kernel module loaded?), exiting...");
        die(EXIT_FAILURE);
    }
    if (!setup_rtnetlink()) {
        log_msg(FATAL, "Can't setup rtnetlink socket, exiting...");
        die(EXIT_FAILURE);
    }

    if (!register_lispd_process()) {
        log_msg(FATAL, "Couldn't register lispd process, exiting...");
        die(EXIT_FAILURE);
    }

    log_msg(INFO, "Version %d.%d.%d starting up...",
            MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION);

    /*
     * now build the v4/v6 receive sockets
     */
    if (build_receive_sockets() == 0) {
        log_msg(FATAL, "  exiting...");
        die(EXIT_FAILURE);
    }

    if (build_event_socket() == 0)
    {
        log_msg(FATAL, "  exiting...");
        die(EXIT_FAILURE);
    }
    log_msg(INFO, "Built receive/event sockets");

    /*
     *	Now do the config file
     */
    if (handle_lispd_config_file()) {
        log_msg(FATAL, "Fatal error parsing config file.");
        dump_fatal_error();
        die(EXIT_FAILURE);
    }

    /*
     *	set up syslog now, checking to see if we're daemonizing...
     */
    setup_log();

    log_msg(INFO, "Read config file");

    if (!install_database_mappings()) {
        log_msg(FATAL, "  exiting...");
        die(EXIT_FAILURE);
    }
#ifdef DEADCODE
    if (!install_map_cache_entries())
        log_msg(INFO, "Could not install static map-cache entries");
#endif
    if (!map_register())
        log_msg(INFO, "Could not map register.");

    set_timer(RLOCProbeScan, RLOC_PROBE_CHECK_INTERVAL);

    clear_map_cache();

    listen_on_well_known_port();

    dump_info_file();
    event_loop();
    log_msg(INFO, "exiting...");		/* event_loop returned bad */
    remove_process_lock();
    exit(0);
}