static bool run_task(struct dsp_node *node, unsigned long times) { unsigned long exit_status; register_msgs(node); if (!dsp_node_run(dsp_handle, node)) { pr_err("dsp node run failed"); return false; } pr_info("dsp node running"); if (do_ping) run_ping(node, times); else run_dmm(node, times); if (!dsp_node_terminate(dsp_handle, node, &exit_status)) { pr_err("dsp node terminate failed: %lx", exit_status); return false; } pr_info("dsp node terminated"); return true; }
int main(int argc, char **argv) { static const struct option longopts[] = { { "help", 0, 0, 'h' }, { "verbose", 0, 0, 'v' }, { "listen", 0, 0, 'l' }, { "connect", 0, 0, 'c' }, { "node", 1, 0, 'n' }, { "host", 1, 0, 'n' }, { "service", 1, 0, 's' }, { "family", 1, 0, 'f' }, { "socktype", 1, 0, 't' }, { "ping", 0, 0, 'I' }, { "protocol", 1, 0, 'p' }, { "backends", 1, 0, 'b' }, { "srv", 0, 0, 'S' }, { "address", 1, 0, 'a' }, { "port", 1, 0, 'P' }, { "class", 1, 0, 'C' }, { "type", 1, 0, 'T' }, { NULL, 0, 0, 0 } }; static const char *opts = "hvcn::s:f:t:p:b:Sa:P:"; int opt, idx = 0; bool do_connect = false; bool do_listen = false; bool ping = false; char *nodename = NULL, *servname = NULL; char *address_str = NULL, *port_str = NULL; int cls = ns_c_in, type = 0; netresolve_t context; netresolve_query_t query; netresolve_set_log_level(NETRESOLVE_LOG_LEVEL_ERROR); context = netresolve_context_new(); if (!context) { error("netresolve: %s\n", strerror(errno)); return EXIT_FAILURE; } while ((opt = getopt_long(count_argv(argv), argv, opts, longopts, &idx)) != -1) { switch (opt) { case 'h': usage(); case 'v': netresolve_set_log_level(NETRESOLVE_LOG_LEVEL_DEBUG); break; case 'l': do_listen = true; break; case 'c': do_connect = true; break; case 'I': ping = true; break; case 'n': nodename = optarg; break; case 's': servname = optarg; break; case 'f': netresolve_context_set_options(context, NETRESOLVE_OPTION_FAMILY, netresolve_family_from_string(optarg), NETRESOLVE_OPTION_DONE); break; case 't': netresolve_context_set_options(context, NETRESOLVE_OPTION_SOCKTYPE, netresolve_socktype_from_string(optarg), NETRESOLVE_OPTION_DONE); break; case 'p': netresolve_context_set_options(context, NETRESOLVE_OPTION_PROTOCOL, netresolve_protocol_from_string(optarg), NETRESOLVE_OPTION_DONE); break; case 'b': netresolve_set_backend_string(context, optarg); break; case 'S': netresolve_context_set_options(context, NETRESOLVE_OPTION_DNS_SRV_LOOKUP, (int) true, NETRESOLVE_OPTION_DONE); break; case 'a': address_str = optarg; break; case 'P': port_str = optarg; break; case 'C': #ifdef USE_LDNS cls = ldns_get_rr_class_by_name(optarg); #else cls = strtoll(optarg, NULL, 10); #endif break; case 'T': #ifdef USE_LDNS type = ldns_get_rr_type_by_name(optarg); #else type = strtoll(optarg, NULL, 10); #endif break; default: exit(EXIT_FAILURE); } } if (argv[optind]) usage(); if (do_listen || do_connect) { netresolve_query_t query; int sock = -1; struct pollfd fds[2]; /* Linux: I found an interesting inconsistency where zero socktype * is supported by the kernel but not when combined with * `SOCK_NONBLOCK` which is internally used by netresolve * socket API implementation. */ if (!context->request.socktype && !context->request.protocol) { context->request.socktype = SOCK_STREAM; context->request.protocol = IPPROTO_TCP; } if (do_listen) { if (!(query = netresolve_listen(context, nodename, servname, 0, 0, 0))) { error("netresolve: Cannot create listening socket: %s", strerror(errno)); return EXIT_FAILURE; } netresolve_accept(query, on_accept, &sock); } else { query = netresolve_connect(context, nodename, servname, 0, 0, 0, on_socket, &sock); while (sock == -1) { error("netresolve: Socket connection failed: %s", strerror(errno)); if (errno == ENETUNREACH) { netresolve_connect_next(query); continue; } return EXIT_FAILURE; } netresolve_connect_free(query); } debug("Connected."); fds[0].fd = 0; fds[0].events = POLLIN; fds[1].fd = sock; fds[1].events = POLLIN; while (true) { if (poll(fds, 2, -1) == -1) { fprintf(stderr, "poll: %s\n", strerror(errno)); break; } if (fds[0].revents & (POLLIN | POLLHUP)) read_and_write(0, sock, sock); if (fds[1].revents & POLLIN) read_and_write(sock, 1, sock); } return EXIT_SUCCESS; } else if (type) query = netresolve_query_dns(context, nodename, cls, type, NULL, NULL); else if (address_str || port_str) { Address address; int family, ifindex; if (!netresolve_backend_parse_address(address_str, &address, &family, &ifindex)) return EXIT_FAILURE; query = netresolve_query_reverse(context, family, &address, ifindex, -1, port_str ? strtol(port_str, NULL, 10) : 0, NULL, NULL); } else query = netresolve_query_forward(context, nodename, servname, NULL, NULL); if (!query) { fprintf(stderr, "netresolve: %s\n", strerror(errno)); return EXIT_FAILURE; } debug("%s", netresolve_get_request_string(query)); if (ping) { for (int i = 0; i < netresolve_query_get_count(query); i++) if (run_ping(query, i)) goto out; error("netresolve: ping failed"); goto out; } const char *response_string = netresolve_get_response_string(query); char *dns_string = get_dns_string(query); if (response_string) printf("%s", response_string); if (dns_string) { printf("%s", dns_string); free(dns_string); } out: netresolve_context_free(context); return EXIT_SUCCESS; }
int main (int argc, char **argv) { char *cmd = NULL; char *rawcmd = NULL; int result = STATE_UNKNOWN; int this_result = STATE_UNKNOWN; int i; setlocale (LC_ALL, ""); setlocale (LC_NUMERIC, "C"); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); addresses = malloc (sizeof(char*) * max_addr); addresses[0] = NULL; /* Parse extra opts if any */ argv=np_extra_opts (&argc, argv, progname); if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); /* Set signal handling and alarm */ if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) { usage4 (_("Cannot catch SIGALRM")); } /* If ./configure finds ping has timeout values, set plugin alarm slightly * higher so that we can use response from command line ping */ #if defined(PING_PACKETS_FIRST) && defined(PING_HAS_TIMEOUT) alarm (timeout_interval + 1); #else alarm (timeout_interval); #endif for (i = 0 ; i < n_addresses ; i++) { #ifdef PING6_COMMAND if (address_family != AF_INET && is_inet6_addr(addresses[i])) rawcmd = strdup(PING6_COMMAND); else rawcmd = strdup(PING_COMMAND); #else rawcmd = strdup(PING_COMMAND); #endif /* does the host address of number of packets argument come first? */ #ifdef PING_PACKETS_FIRST # ifdef PING_HAS_TIMEOUT xasprintf (&cmd, rawcmd, timeout_interval, max_packets, addresses[i]); # else xasprintf (&cmd, rawcmd, max_packets, addresses[i]); # endif #else xasprintf (&cmd, rawcmd, addresses[i], max_packets); #endif if (verbose >= 2) printf ("CMD: %s\n", cmd); /* run the command */ this_result = run_ping (cmd, addresses[i]); if (pl == UNKNOWN_PACKET_LOSS || rta < 0.0) { printf ("%s\n", cmd); die (STATE_UNKNOWN, _("CRITICAL - Could not interpret output from ping command\n")); } if (pl >= cpl || rta >= crta || rta < 0) this_result = STATE_CRITICAL; else if (pl >= wpl || rta >= wrta) this_result = STATE_WARNING; else if (pl >= 0 && rta >= 0) this_result = max_state (STATE_OK, this_result); if (n_addresses > 1 && this_result != STATE_UNKNOWN) die (STATE_OK, "%s is alive\n", addresses[i]); if (display_html == TRUE) printf ("<A HREF='%s/traceroute.cgi?%s'>", CGIURL, addresses[i]); if (pl == 100) printf (_("PING %s - %sPacket loss = %d%%"), state_text (this_result), warn_text, pl); else printf (_("PING %s - %sPacket loss = %d%%, RTA = %2.2f ms"), state_text (this_result), warn_text, pl, rta); if (display_html == TRUE) printf ("</A>"); /* Print performance data */ printf("|%s", fperfdata ("rta", (double) rta, "ms", wrta>0?TRUE:FALSE, wrta, crta>0?TRUE:FALSE, crta, TRUE, 0, FALSE, 0)); printf(" %s\n", perfdata ("pl", (long) pl, "%", wpl>0?TRUE:FALSE, wpl, cpl>0?TRUE:FALSE, cpl, TRUE, 0, FALSE, 0)); if (verbose >= 2) printf ("%f:%d%% %f:%d%%\n", wrta, wpl, crta, cpl); result = max_state (result, this_result); free (rawcmd); free (cmd); } return result; }