int rtmon_init(struct rtmon_t *rtmon, rtmon_callback func) { memset(rtmon, 0, sizeof(struct rtmon_t)); rtmon->fd = open_netlink(); rtmon->cb = func; if (rtmon->fd > 0) { rtmon_discover_ifaces(rtmon); rtmon_discover_routes(rtmon); if (_options.debug) { rtmon_print_ifaces(rtmon, 1); rtmon_print_routes(rtmon, 1); } rtmon_check_updates(rtmon); return 0; } return -1; }
void print_iface_stats(const char *iface, time_t since, FILE * out, cmd_params_st *params, unsigned have_more) { uint64_t tx, rx; char buf1[32], buf2[32]; time_t diff = time(0) - since; int ret; struct rtnl_link *rlink = NULL; if (iface == NULL || iface[0] == 0) return; if (open_netlink() < 0) return; ret = rtnl_link_get_kernel(sock, 0, iface, &rlink); if (ret < 0) { fprintf(stderr, "nl: cannot find %s\n", iface); return; } rx = rtnl_link_get_stat(rlink, RTNL_LINK_RX_BYTES); tx = rtnl_link_get_stat(rlink, RTNL_LINK_TX_BYTES); bytes2human(rx, buf1, sizeof(buf1), NULL); bytes2human(tx, buf2, sizeof(buf2), NULL); if (HAVE_JSON(params)) { fprintf(out, " \"RX\": \"%"PRIu64"\",\n \"TX\": \"%"PRIu64"\",\n", rx, tx); fprintf(out, " \"_RX\": \"%s\",\n \"_TX\": \"%s\",\n", buf1, buf2); } else fprintf(out, "\tRX: %"PRIu64" (%s) TX: %"PRIu64" (%s)\n", rx, buf1, tx, buf2); value2speed(rx, diff, buf1, sizeof(buf1)); value2speed(tx, diff, buf2, sizeof(buf2)); if (HAVE_JSON(params)) fprintf(out, " \"Average RX\": \"%s\",\n \"Average TX\": \"%s\"%s\n", buf1, buf2, have_more?",":""); else fprintf(out, "\tAverage bandwidth RX: %s TX: %s\n", buf1, buf2); return; }
/** * Query NETLINK for ethernet configuration * * @param ethinf Pointer to an available struct etherinfo element. The 'device' member * must contain a valid string to the device to query for information * @param nlc Pointer to the libnl handle, which is used for the query against NETLINK * @param query What to query for. Must be NLQRY_LINK or NLQRY_ADDR. * * @return Returns 1 on success, otherwise 0. */ int get_etherinfo(struct etherinfo_obj_data *data, nlQuery query) { struct nl_cache *link_cache; struct nl_cache *addr_cache; struct rtnl_addr *addr; struct rtnl_link *link; struct etherinfo *ethinf = NULL; int ret = 0; if( !data || !data->ethinfo ) { return 0; } ethinf = data->ethinfo; /* Open a NETLINK connection on-the-fly */ if( !open_netlink(data) ) { PyErr_Format(PyExc_RuntimeError, "Could not open a NETLINK connection for %s", ethinf->device); return 0; } /* Find the interface index we're looking up. * As we don't expect it to change, we're reusing a "cached" * interface index if we have that */ if( ethinf->index < 0 ) { link_cache = rtnl_link_alloc_cache(*data->nlc); ethinf->index = rtnl_link_name2i(link_cache, ethinf->device); if( ethinf->index < 0 ) { return 0; } nl_cache_free(link_cache); } /* Query the for requested info vai NETLINK */ switch( query ) { case NLQRY_LINK: /* Extract MAC/hardware address of the interface */ link_cache = rtnl_link_alloc_cache(*data->nlc); link = rtnl_link_alloc(); rtnl_link_set_ifindex(link, ethinf->index); nl_cache_foreach_filter(link_cache, (struct nl_object *)link, callback_nl_link, ethinf); rtnl_link_put(link); nl_cache_free(link_cache); ret = 1; break; case NLQRY_ADDR: /* Extract IP address information */ addr_cache = rtnl_addr_alloc_cache(*data->nlc); addr = rtnl_addr_alloc(); rtnl_addr_set_ifindex(addr, ethinf->index); /* Make sure we don't have any old IPv6 addresses saved */ if( ethinf->ipv6_addresses ) { free_ipv6addresses(ethinf->ipv6_addresses); ethinf->ipv6_addresses = NULL; } /* Retrieve all address information */ nl_cache_foreach_filter(addr_cache, (struct nl_object *)addr, callback_nl_address, ethinf); rtnl_addr_put(addr); nl_cache_free(addr_cache); ret = 1; break; default: ret = 0; } return ret; }
/** * @brief Entry function for coex * @param argc number of arguments * @param argv A pointer to arguments array * @return MLAN_STATUS_SUCCESS--success, otherwise--fail */ int main(int argc, char *argv[]) { char *ifname = "mlan0"; int c, daemonize = FALSE; for (;;) { c = getopt(argc, argv, "Bhi:v"); /* check if all command-line options have been parsed */ if (c == NO_OPTION) break; switch (c) { case 'B': daemonize = TRUE; break; case 'h': display_usage(); return MLAN_STATUS_SUCCESS; case 'v': fprintf(stdout, "Marvell 20/40coex application version %s\n", COEX_VER); return MLAN_STATUS_SUCCESS; case 'i': ifname = optarg; break; default: fprintf(stdout, "Invalid argument\n"); display_usage(); return MLAN_STATUS_SUCCESS; } } strncpy(dev_name, ifname, IFNAMSIZ); /* create a socket */ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { fprintf(stderr, "mlan2040coex: Cannot open socket.\n"); goto done; } if (get_range() != MLAN_STATUS_SUCCESS) { fprintf(stderr, "mlan2040coex: Cannot get range.\n"); goto done; } /* create a netlink socket */ if ((nl_sk = open_netlink()) == MLAN_STATUS_FAILURE) { fprintf(stderr, "mlan2040coex: Cannot open netlink socket.\n"); goto done; } signal(SIGHUP, terminate_handler); /* catch hangup signal */ signal(SIGTERM, terminate_handler); /* catch kill signal */ /** Make the process background-process */ if (daemonize) daemon(0, 0); /** run the application */ run_app(); done: if (sockfd) close(sockfd); if (nl_sk) close(nl_sk); return MLAN_STATUS_SUCCESS; }
int main(int argc, char **argv) { int result = 0; int i; userui_ops[0] = &userui_text_ops; userui_ops[1] = FBSPLASH_OPS; userui_ops[2] = USPLASH_OPS; active_ops = &userui_text_ops; handle_params(argc, argv); setup_signal_handlers(); open_console(); open_misc(); if (!test_run) { open_netlink(); get_nofreeze(); get_info(); } lock_memory(); prepare_console(); /* Initialise all that we can, use the first */ // active_ops = NULL; for (i = 0; i < NUM_UIS; i++) { if (userui_ops[i] && userui_ops[i]->load) { result = userui_ops[i]->load(); if (result) { if (test_run) fprintf(stderr, "Failed to initialise %s module.\n", userui_ops[i]->name); else printk("Failed to initialise %s module.\n", userui_ops[i]->name); } else if (!active_ops) active_ops = userui_ops[i]; } } if (active_ops->prepare) active_ops->prepare(); register_keypress_handler(); need_cleanup = 1; running = 1; result = nice(1); if (active_ops->memory_required) reserve_memory(active_ops->memory_required()); else reserve_memory(4*1024*1024); /* say 4MB */ enforce_lifesavers(); if (test_run) { safe_to_exit = 0; do_test_run(); return 0; } if (send_ready()) message_loop(); /* The only point we ever reach here is if message_loop crashed out. * If this is the case, we should spin for a few hours before exiting to * ensure that we don't corrupt stuff on disk (if we're past the atomic * copy). */ sleep(60*60*1); /* 1 hours */ _exit(1); }
/*** main ***/ int main (int argc, char **argv) { int rc = EXIT_FAILURE; int i, nls; unsigned int version = 0, help = 0; program = argv[0]; /* get the verbose status */ while ((i = getopt_long(argc, argv, optstring, options_long, NULL)) != -1) { switch (i) { case 'h': help++; break; case 't': notification_timeout = atof(optarg) * 1000; break; case 'v': verbose++; break; case 'V': verbose++; version++; break; } } if (verbose > 0) printf ("%s: %s v%s" #ifdef HAVE_SYSTEMD " +systemd" #endif " (compiled: " __DATE__ ", " __TIME__ ")\n", program, PROGNAME, VERSION); if (help > 0) printf("usage: %s [-h] [-t TIMEOUT] [-v[v]] [-V]\n", program); if (version > 0 || help > 0) return EXIT_SUCCESS; if ((nls = open_netlink()) < 0) { fprintf (stderr, "%s: Error opening netlink socket!\n", program); goto out40; } if (notify_init(PROGNAME) == FALSE) { fprintf (stderr, "%s: Can't create notify.\n", program); goto out30; } signal(SIGINT, received_signal); signal(SIGTERM, received_signal); #ifdef HAVE_SYSTEMD sd_notify(0, "READY=1\nSTATUS=Waiting for netlink events..."); #endif while (doexit == 0) { if (read_event(nls) != EXIT_SUCCESS) { fprintf(stderr, "%s: read_event returned error.\n", program); goto out10; } } if (verbose > 0) printf("%s: Exiting...\n", program); /* report stopping to systemd */ #ifdef HAVE_SYSTEMD sd_notify(0, "STOPPING=1\nSTATUS=Stopping..."); #endif for(; maxinterface > 0; maxinterface--) { if (verbose > 0) printf("%s: Freeing interface %d: %s\n", program, maxinterface, ifs[maxinterface].name); free_addresses(ifs[maxinterface].addresses_seen); if (ifs[maxinterface].notification != NULL) g_object_unref(G_OBJECT(ifs[maxinterface].notification)); } rc = EXIT_SUCCESS; out10: if (ifs != NULL) free(ifs); /* out20: */ notify_uninit(); out30: if (close(nls) < 0) fprintf(stderr, "%s: Failed to close socket.\n", program); out40: #ifdef HAVE_SYSTEMD sd_notify(0, "STATUS=Stopped. Bye!"); #endif return rc; }