int main (int argc, char **argv) { /* Local Vars */ virConnectPtr conn; const char *hvType; unsigned long libVer, libMajor, libMinor, libRelease; unsigned long hvVer, hvMajor, hvMinor, hvRelease; /* Set signal handling and alarm */ if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) critical("Setup SIGALRM trap failed!"); /* Process check arguments */ if (process_arguments(argc, argv) != OK) unknown("Parsing arguments failed!"); /* Start plugin timeout */ alarm(mp_timeout); // PLUGIN CODE conn = virt_connect(); hvType = mp_strdup(virConnectGetType(conn)); if (hvType == NULL) { if (mp_verbose > 0) { virt_showError(conn); } critical("Failed to get hypervisor type."); } if (virConnectGetVersion(conn, &hvVer) != 0) { if (mp_verbose > 0) { virt_showError(conn); } critical("Failed to get hypervisor version."); } if (virConnectGetLibVersion(conn, &libVer) != 0) { if (mp_verbose > 0) { virt_showError(conn); } critical("Failed to get library version."); } virConnectClose(conn); hvMajor = hvVer / 1000000; hvVer %= 1000000; hvMinor = hvVer / 1000; hvRelease = hvVer % 1000; libMajor = libVer / 1000000; libVer %= 1000000; libMinor = libVer / 1000; libRelease = libVer % 1000; /* Output and return */ ok("libvirtd: v.%lu.%lu.%lu Hypervisor: %s (v.%lu.%lu.%lu)", libMajor, libMinor, libRelease, hvType, hvMajor, hvMinor, hvRelease); }
struct rpcent *rpc_getrpcent(const char *prog) { struct rpcent *ent, *ret; if (isalpha(*prog)) { ent = getrpcbyname((char *)prog); } else { ent = getrpcbynumber(atoi(prog)); } if (ent == NULL) return NULL; ret = mp_malloc(sizeof(struct rpcent)); ret->r_name = mp_strdup(ent->r_name); ret->r_number = ent->r_number; return ret; }
END_TEST START_TEST (test_array_push_multi) { char *input = NULL; char **array = NULL; int num = 0; input = mp_strdup("I,LOVE,NALA"); mp_array_push(&array, input, &num); fail_unless (num == 3, "mp_array_push failed: num = %d", num); fail_unless (strcmp(array[0], "I") == 0, "mp_array_push failed: Element 0 %s", array[0]); fail_unless (strcmp(array[1], "LOVE") == 0, "mp_array_push failed: Element 1 %s", array[1]); fail_unless (strcmp(array[2], "NALA") == 0, "mp_array_push failed: Element 2 %s", array[2]); }
netsnmp_session *mp_snmp_init(void) { netsnmp_session session, *ss; int status; init_snmp(progname); snmp_sess_init( &session ); if (mp_snmp_community == NULL) mp_snmp_community = mp_strdup("public"); mp_asprintf(&(session.peername), "%s:%d", hostname, port); switch(mp_snmp_version) { case SNMP_VERSION_1: session.version = SNMP_VERSION_1; session.community = (u_char *)mp_snmp_community; session.community_len = strlen((char *)session.community); break; case SNMP_VERSION_2c: session.version = SNMP_VERSION_2c; session.community = (u_char *)mp_snmp_community; session.community_len = strlen((char *)session.community); break; case SNMP_VERSION_3: session.version = SNMP_VERSION_3; session.securityName = mp_strdup(mp_snmp_secname); session.securityNameLen = strlen(session.securityName); /* set the security level */ session.securityLevel = mp_snmp_seclevel; session.contextName = mp_strdup(mp_snmp_context); session.contextNameLen = strlen(session.contextName); /* set the authentication method */ session.securityAuthProto = mp_snmp_authproto; session.securityAuthProtoLen = 10; session.securityAuthKeyLen = USM_AUTH_KU_LEN; status = generate_Ku(session.securityAuthProto, session.securityAuthProtoLen, (u_char *) mp_snmp_authpass, strlen(mp_snmp_authpass), session.securityAuthKey, &session.securityAuthKeyLen); if (status != SNMPERR_SUCCESS) { snmp_perror(progname); snmp_log(LOG_ERR, "Error generating Ku from authentication pass phrase. \n%s\n",snmp_api_errstring(status)); exit(1); } break; } netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PERSIST_STATE, 1); SOCK_STARTUP; ss = snmp_open(&session); if (!ss) { snmp_sess_perror("ack", &session); SOCK_CLEANUP; exit(1); } free(session.peername); if (mp_snmp_retries > 0) ss->retries = mp_snmp_retries; if (mp_snmp_timeout > 0) ss->timeout = (long)(mp_snmp_timeout * 1000000L); return ss; }
int main (int argc, char **argv) { int fcgiSock = -1; FCGX_Stream *paramsStream; char *pool = NULL; char *content, *data; int type, count; struct json_object *obj, *slaveobj; /* Set signal handling and alarm */ if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR) critical("Setup SIGALRM trap failed!"); /* Process check arguments */ if (process_arguments(argc, argv) != OK) unknown("Parsing arguments failed!"); /* Start plugin timeout */ alarm(mp_timeout); /* Connect to fcgi server */ fcgiSock = mp_fcgi_connect(fcgisocket); /* Prepare Begin Request */ FCGI_BeginRequestBody body; body.roleB1 = 0x00; body.roleB0 = FCGI_RESPONDER; body.flags = 0x000; memset(body.reserved, 0, sizeof(body.reserved)); mp_fcgi_write(fcgiSock, 42, FCGI_BEGIN_REQUEST, (char*)&body, sizeof(body)); /* Set FCGI Params */ paramsStream = FCGX_CreateWriter(fcgiSock, 1, 8192, FCGI_PARAMS); mp_fcgi_putkv(paramsStream, "REQUEST_METHOD", "GET"); mp_fcgi_putkv(paramsStream, "SCRIPT_NAME", query); mp_fcgi_putkv(paramsStream, "SCRIPT_FILENAME", query); mp_fcgi_putkv(paramsStream, "QUERY_STRING", "json&"); FCGX_FClose(paramsStream); FCGX_FreeStream(¶msStream); /* Start request processing by stdin closing */ mp_fcgi_write(fcgiSock, 42, FCGI_STDIN, NULL, 0); /* Wait for answer */ data = NULL; do { content = NULL; type = mp_fcgi_read(fcgiSock, &content, &count); if (type == FCGI_STDOUT) data = content; else if (content) free(content); } while (type != FCGI_END_REQUEST); /* Skip http headers */ content = data; do { (void)strsep(&data, "\n"); } while (data && data[0] != '\r'); /* Parse JSON */ obj = mp_json_tokener_parse(data); /* Read pool name */ mp_json_object_object_get(obj, "pool", &slaveobj); pool = mp_strdup(json_object_get_string(slaveobj)); /* Read accepted connections */ mp_json_object_object_get(obj, "accepted conn", &slaveobj); mp_perfdata_int("accepted_conn", json_object_get_int(slaveobj), "c", NULL); /* Read listen queue */ mp_json_object_object_get(obj, "listen queue", &slaveobj); mp_perfdata_int("listen_queue", json_object_get_int(slaveobj), "", NULL); /* Read idle processes */ mp_json_object_object_get(obj, "idle processes", &slaveobj); mp_perfdata_int("idle_processes", json_object_get_int(slaveobj), "", NULL); /* Read active processes */ mp_json_object_object_get(obj, "active processes", &slaveobj); mp_perfdata_int("active_processes", json_object_get_int(slaveobj), "", NULL); free(content); json_object_put(obj); ok("PHP-FPM: %s", pool); }
void rhcs_clustat_startElement(void *clustat, const char *name, const char **atts) { const char **k, **v; int i; if (strcmp(name, "cluster") == 0) { for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) { if (strcmp(*k, "name") == 0) ((rhcs_clustat *)clustat)->name = mp_strdup(*v); else if (strcmp(*k, "id") == 0) ((rhcs_clustat *)clustat)->id = (unsigned int) strtol(*v, NULL, 10); } return; } if (strcmp(name, "node") == 0) { ((rhcs_clustat *)clustat)->node = mp_realloc(((rhcs_conf *)clustat)->node, (nodes+2)*sizeof(rhcs_clustat_node)); ((rhcs_clustat *)clustat)->node[nodes] = mp_calloc(1, sizeof(rhcs_clustat_node)); ((rhcs_clustat *)clustat)->node[nodes+1] = NULL; for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) { if (strcmp(*k, "name") == 0) ((rhcs_clustat *)clustat)->node[nodes]->name = mp_strdup(*v); else if (strcmp(*k, "state") == 0) ((rhcs_clustat *)clustat)->node[nodes]->state = (unsigned int) strtol(*v, NULL, 10); else if (strcmp(*k, "rgmanager") == 0) ((rhcs_clustat *)clustat)->node[nodes]->rgmanager = (unsigned int) strtol(*v, NULL, 10); else if (strcmp(*k, "nodeid") == 0) ((rhcs_clustat *)clustat)->node[nodes]->id = (unsigned int) strtol(*v, NULL, 16); if (strcmp(*k, "local") == 0 && strcmp(*v, "1") == 0) ((rhcs_clustat *)clustat)->local = ((rhcs_clustat *)clustat)->node[nodes]; } nodes++; return; } if (strcmp(name, "group") == 0) { ((rhcs_clustat *)clustat)->group = mp_realloc(((rhcs_clustat *)clustat)->group, (services+2)*sizeof(rhcs_clustat_group)); ((rhcs_clustat *)clustat)->group[services] = mp_calloc(1, sizeof(rhcs_clustat_group)); ((rhcs_clustat *)clustat)->group[services+1] = NULL; for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) { if (strcmp(*k, "name") == 0) { ((rhcs_clustat *)clustat)->group[services]->name = mp_strdup(*v); strsep(&((rhcs_clustat *)clustat)->group[services]->name, ":"); } else if (strcmp(*k, "state") == 0) { ((rhcs_clustat *)clustat)->group[services]->state = (unsigned int) strtol(*v, NULL, 10); } else if (strcmp(*k, "owner") == 0) { ((rhcs_clustat *)clustat)->group[services]->owner = NULL; for(i=0; i < nodes; i++) { if(strcmp(*v, ((rhcs_clustat *)clustat)->node[i]->name) == 0) { ((rhcs_clustat *)clustat)->group[services]->owner = ((rhcs_clustat *)clustat)->node[i]; } } } else if (strcmp(*k, "last_owner") == 0) { for(i=0; i < nodes; i++) { if(strcmp(*v, ((rhcs_clustat *)clustat)->node[i]->name) == 0) { ((rhcs_clustat *)clustat)->group[services]->last = ((rhcs_clustat *)clustat)->node[i]; } } } } services++; return; } }
void rhcs_conf_startElement(void *conf, const char *name, const char **atts) { const char **k, **v; int i; if (strcmp(name, "cluster") == 0) { for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) { if (strcmp(*k, "name") == 0) ((rhcs_conf *)conf)->name = mp_strdup(*v); else if (strcmp(*k, "alias") == 0) ((rhcs_conf *)conf)->alias = mp_strdup(*v); else if (strcmp(*k, "config_version") == 0) ((rhcs_conf *)conf)->version = (unsigned int) strtol(*v, NULL, 10); } return; } if (strcmp(name, "clusternode") == 0) { ((rhcs_conf *)conf)->node = mp_realloc(((rhcs_conf *)conf)->node, (nodes+2)*sizeof(rhcs_conf_node)); ((rhcs_conf *)conf)->node[nodes] = mp_calloc(1, sizeof(rhcs_conf_node)); ((rhcs_conf *)conf)->node[nodes+1] = NULL; for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) { if (strcmp(*k, "name") == 0) ((rhcs_conf *)conf)->node[nodes]->name = mp_strdup(*v); else if (strcmp(*k, "nodeid") == 0) ((rhcs_conf *)conf)->node[nodes]->id = (unsigned int) strtol(*v, NULL, 10); else if (strcmp(*k, "votes") == 0) ((rhcs_conf *)conf)->node[nodes]->votes = (unsigned int) strtol(*v, NULL, 10); } nodes++; return; } if (strcmp(name, "failoverdomain") == 0) { ((rhcs_conf *)conf)->fodomain = mp_realloc(((rhcs_conf *)conf)->fodomain, (fodomains+2)*sizeof(rhcs_conf_fodom)); ((rhcs_conf *)conf)->fodomain[fodomains] = mp_calloc(1, sizeof(rhcs_conf_fodom)); ((rhcs_conf *)conf)->fodomain[fodomains+1] = NULL; for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) { if (strcmp(*k, "name") == 0) ((rhcs_conf *)conf)->fodomain[fodomains]->name = mp_strdup(*v); else if (strcmp(*k, "nofailback") == 0) ((rhcs_conf *)conf)->fodomain[fodomains]->failback = (strcmp(*v, "0") == 0); else if (strcmp(*k, "ordered") == 0) ((rhcs_conf *)conf)->fodomain[fodomains]->ordered = (strcmp(*v, "1") == 0); else if (strcmp(*k, "restricted") == 0) ((rhcs_conf *)conf)->fodomain[fodomains]->restricted = (strcmp(*v, "1") == 0); } fodomain_nodes = 0; return; } if (strcmp(name, "failoverdomainnode") == 0) { ((rhcs_conf *)conf)->fodomain[fodomains]->node = mp_realloc(((rhcs_conf *)conf)->fodomain[fodomains]->node, (fodomain_nodes+2)*sizeof(rhcs_conf_fodom_node)); ((rhcs_conf *)conf)->fodomain[fodomains]->node[fodomain_nodes] = mp_calloc(1, sizeof(rhcs_conf_fodom_node)); ((rhcs_conf *)conf)->fodomain[fodomains]->node[fodomain_nodes+1] = NULL; for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) { if (strcmp(*k, "name") == 0) for (i=0; i < nodes; i++) { if (strcmp(*v, ((rhcs_conf *)conf)->node[i]->name) == 0) { ((rhcs_conf *)conf)->fodomain[fodomains]->node[fodomain_nodes]->node = ((rhcs_conf *)conf)->node[i]; } } else if (strcmp(*k, "priority") == 0) ((rhcs_conf *)conf)->fodomain[fodomains]->node[fodomain_nodes]->priority = (unsigned int) strtol(*v, NULL, 10); } fodomain_nodes++; return; } if (strcmp(name, "service") == 0) { ((rhcs_conf *)conf)->service = mp_realloc(((rhcs_conf *)conf)->service, (services+2)*sizeof(rhcs_conf_service)); ((rhcs_conf *)conf)->service[services] = mp_calloc(1, sizeof(rhcs_conf_service)); ((rhcs_conf *)conf)->service[services+1] = NULL; for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) { if (strcmp(*k, "name") == 0) ((rhcs_conf *)conf)->service[services]->name = mp_strdup(*v); else if (strcmp(*k, "domain") == 0) for (i=0; i < fodomains; i++) { if (strcmp(*v, ((rhcs_conf *)conf)->fodomain[i]->name) == 0) { ((rhcs_conf *)conf)->service[services]->fodomain = ((rhcs_conf *)conf)->fodomain[i]; } } else if (strcmp(*k, "autostart") == 0) ((rhcs_conf *)conf)->service[services]->autostart = (strcmp(*v, "1") == 0); else if (strcmp(*k, "exclusive") == 0) ((rhcs_conf *)conf)->service[services]->exclusive = (strcmp(*v, "1") == 0); else if (strcmp(*k, "recovery") == 0) { if (strcmp(*v, "relocate") == 0) ((rhcs_conf *)conf)->service[services]->recovery = RELOCATE; else if (strcmp(*v, "restart") == 0) ((rhcs_conf *)conf)->service[services]->recovery = RESTART; } } services++; return; } /* printf("%s\n", name); for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) printf(" '%s' => '%s'\n", *k, *v); */ }
void jnu_set_jclass(void *ctx, const char *name) { struct hotplug *hotplug = (struct hotplug *)ctx; hotplug->jclass = mp_strdup(hotplug->mp, name); }
int mobile_at_command_input(int fd, const char *cmd, const char *opt, const char *input, char ***answer, int *answers) { char *buf; char *ptr; char *line; size_t len; fd_set rfds; int retval; struct timeval tv; buf = mp_malloc(64); // Build command string if (opt) mp_snprintf(buf, 64, "AT%s%s\r", cmd, opt); else mp_snprintf(buf, 64, "AT%s\r", cmd); // Send command len = write(fd, buf, strlen(buf)); if (len != strlen(buf)) { if (mp_verbose > 0) fprintf(stderr, "Write to device failed. " "Written %d of %d chars.\n", (int)len, (int)strlen(buf)); return -1; } if (mp_verbose > 3) printf(" >> %s\n", buf); if (input) { len = write(fd, input, strlen(input)); if (len != strlen(input)) { if (mp_verbose > 0) fprintf(stderr, "Write to device failed. " "Written %d of %d chars.\n", (int)len, (int)strlen(buf)); return -1; } if (mp_verbose > 3) printf(" >> %s\n", input); len = write(fd, "\r\x1A", 2); if (len != 2) { if (mp_verbose > 0) fprintf(stderr, "Write to device failed. " "Written %d of 2 chars.\n", (int)len); return -1; } } // Read answers len = 0; ptr = buf; tv.tv_sec = 5; tv.tv_usec = 0; if (answers) *answers = 0; while (1) { // Build select list; FD_ZERO(&rfds); FD_SET(fd, &rfds); // Wait for input retval = select(fd+1, &rfds, NULL, NULL, &tv); if (retval <= 0) return -1; // Read from serial ptr = buf; ptr += len; retval = read(fd, buf, 64-len); if (retval <= 0) return -1; // Fetch lines ptr = buf; while ((line = strsep(&ptr, "\r\n")) && ptr) { if (strlen(line) == 0) continue; if (mp_verbose > 3) printf(" << %s\n", line); // Status codes if (strcmp(line, "OK") == 0) return 0; if (strcmp(line, "ERROR") == 0) return 1; if (strncmp(line, "+CME ERROR: ", 12) == 0) return 1; // Answer if (strncmp(line, cmd, strlen(cmd)) == 0) { if(!answers) continue; line += strlen(cmd) +2; *answer = mp_realloc(*answer, (sizeof(char **)*((*answers)+2))); (*answer)[*answers] = mp_strdup(line); (*answer)[(*answers)+1] = NULL; (*answers)++; } } // Move buffer if (line) { len = strlen(line); memmove(buf, line, len+1); len = strlen(buf); } } return 0; }