static void run_tests() { encode_null(); encode_twice(); circular_references(); encode_other_than_array_or_object(); escape_slashes(); }
static int swap_read_separate (void) /* {{{ */ { FILE *fh; char buffer[1024]; fh = fopen ("/proc/swaps", "r"); if (fh == NULL) { char errbuf[1024]; WARNING ("swap plugin: fopen (/proc/swaps) failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } while (fgets (buffer, sizeof (buffer), fh) != NULL) { char *fields[8]; int numfields; char *endptr; char path[PATH_MAX]; gauge_t size; gauge_t used; gauge_t free; numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields)); if (numfields != 5) continue; sstrncpy (path, fields[0], sizeof (path)); escape_slashes (path, sizeof (path)); errno = 0; endptr = NULL; size = strtod (fields[2], &endptr); if ((endptr == fields[2]) || (errno != 0)) continue; errno = 0; endptr = NULL; used = strtod (fields[3], &endptr); if ((endptr == fields[3]) || (errno != 0)) continue; if (size < used) continue; free = size - used; swap_submit_gauge (path, "used", used); swap_submit_gauge (path, "free", free); } fclose (fh); return (0); } /* }}} int swap_read_separate */
/* swapctl-based read function */ static int swap_read (void) /* {{{ */ { swaptbl_t *s; char *s_paths; int swap_num; int status; int i; derive_t avail = 0; derive_t total = 0; swap_num = swapctl (SC_GETNSWP, NULL); if (swap_num < 0) { ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.", swap_num); return (-1); } else if (swap_num == 0) return (0); /* Allocate and initialize the swaptbl_t structure */ s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable)); if (s == NULL) { ERROR ("swap plugin: smalloc failed."); return (-1); } /* Memory to store the path names. We only use these paths when the * separate option has been configured, but it's easier to just * allocate enough memory in any case. */ s_paths = calloc (swap_num, PATH_MAX); if (s_paths == NULL) { ERROR ("swap plugin: malloc failed."); sfree (s); return (-1); } for (i = 0; i < swap_num; i++) s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX); s->swt_n = swap_num; status = swapctl (SC_LIST, s); if (status < 0) { char errbuf[1024]; ERROR ("swap plugin: swapctl (SC_LIST) failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); sfree (s_paths); sfree (s); return (-1); } else if (swap_num < status) { /* more elements returned than requested */ ERROR ("swap plugin: I allocated memory for %i structure%s, " "but swapctl(2) claims to have returned %i. " "I'm confused and will give up.", swap_num, (swap_num == 1) ? "" : "s", status); sfree (s_paths); sfree (s); return (-1); } else if (swap_num > status) /* less elements returned than requested */ swap_num = status; for (i = 0; i < swap_num; i++) { char path[PATH_MAX]; derive_t this_total; derive_t this_avail; if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0) continue; this_total = ((derive_t) s->swt_ent[i].ste_pages) * pagesize; this_avail = ((derive_t) s->swt_ent[i].ste_free) * pagesize; /* Shortcut for the "combined" setting (default) */ if (!report_by_device) { avail += this_avail; total += this_total; continue; } sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path)); escape_slashes (path, sizeof (path)); swap_submit_gauge (path, "used", (gauge_t) (this_total - this_avail)); swap_submit_gauge (path, "free", (gauge_t) this_avail); } /* for (swap_num) */ if (total < avail) { ERROR ("swap plugin: Total swap space (%"PRIi64") " "is less than free swap space (%"PRIi64").", total, avail); sfree (s_paths); sfree (s); return (-1); } /* If the "separate" option was specified (report_by_device == 2), all * values have already been dispatched from within the loop. */ if (!report_by_device) { swap_submit_gauge (NULL, "used", (gauge_t) (total - avail)); swap_submit_gauge (NULL, "free", (gauge_t) avail); } sfree (s_paths); sfree (s); return (0); } /* }}} int swap_read */
pshkConfigStruct pshk_read_registry(void) { HKEY hk; CHAR szBuf[PSHK_REG_VALUE_MAX_LEN+1]; DWORD szBufSize = PSHK_REG_VALUE_MAX_LEN; pshkConfigStruct ret = {0}; DWORD readRetVal; memset(szBuf, 0, sizeof(szBuf)); if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, PSHK_REG_KEY, 0, KEY_QUERY_VALUE, &hk) != ERROR_SUCCESS ) { return ret; } /* Get the log level */ readRetVal = RegQueryValueEx( hk,"loglevel", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; ret.logLevel = strtol(szBuf, NULL, 10); memset(szBuf, 0, sizeof(szBuf)); szBufSize = PSHK_REG_VALUE_MAX_LEN; /* Get the priority */ readRetVal = RegQueryValueEx( hk,"priority", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; ret.priority = strtol(szBuf, NULL, 10); memset(szBuf, 0, sizeof(szBuf)); szBufSize = PSHK_REG_VALUE_MAX_LEN; /* Get the pre-change program wait time */ readRetVal = RegQueryValueEx( hk,"preChangeProgWait", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; ret.preChangeProgWait = strtol(szBuf, NULL, 10); memset(szBuf, 0, sizeof(szBuf)); szBufSize = PSHK_REG_VALUE_MAX_LEN; /* Get the post-change program wait time */ readRetVal = RegQueryValueEx( hk,"postChangeProgWait", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; ret.postChangeProgWait = strtol(szBuf, NULL, 10); memset(szBuf, 0, sizeof(szBuf)); szBufSize = PSHK_REG_VALUE_MAX_LEN; /* Get the working directory */ readRetVal = RegQueryValueEx(hk, "workingdir", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; ret.workingDir = escape_slashes(szBuf); memset(szBuf, 0, sizeof(szBuf)); szBufSize = PSHK_REG_VALUE_MAX_LEN; /* Get the log file */ readRetVal = RegQueryValueEx(hk, "logfile", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; ret.logFile = escape_slashes(szBuf); memset(szBuf, 0, sizeof(szBuf)); szBufSize = PSHK_REG_VALUE_MAX_LEN; /* Get the max log file size*/ readRetVal = RegQueryValueEx(hk, "maxlogsize", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; ret.maxLogSize = strtol(szBuf, NULL, 10); memset(szBuf, 0, sizeof(szBuf)); szBufSize = PSHK_REG_VALUE_MAX_LEN; /* Pre password change program file */ readRetVal = RegQueryValueEx(hk, "preChangeProg", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; ret.preChangeProg = escape_slashes(szBuf); memset(szBuf, 0, sizeof(szBuf)); szBufSize = PSHK_REG_VALUE_MAX_LEN; /* Pre password change program args */ readRetVal = RegQueryValueEx(hk, "preChangeProgArgs", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; ret.preChangeProgArgs = escape_slashes(szBuf); memset(szBuf, 0, sizeof(szBuf)); szBufSize = PSHK_REG_VALUE_MAX_LEN; /* Post password change program file */ readRetVal = RegQueryValueEx(hk, "postChangeProg", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; ret.postChangeProg = escape_slashes(szBuf); memset(szBuf, 0, sizeof(szBuf)); szBufSize = PSHK_REG_VALUE_MAX_LEN; /* Post password change program args */ readRetVal = RegQueryValueEx(hk, "postChangeProgArgs", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; ret.postChangeProgArgs = escape_slashes(szBuf); memset(szBuf, 0, sizeof(szBuf)); szBufSize = PSHK_REG_VALUE_MAX_LEN; /* Environment string */ readRetVal = RegQueryValueEx(hk, "environment", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; ret.environmentStr = strdup(szBuf); ret.environment = parse_env(szBuf); memset(szBuf, 0, sizeof(szBuf)); szBufSize = PSHK_REG_VALUE_MAX_LEN; /* Get wether to urlencode the password string */ readRetVal = RegQueryValueEx(hk, "urlencode", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; if(!_stricmp(szBuf, "true") || \ !_stricmp(szBuf, "yes") || \ !_stricmp(szBuf, "on") ) ret.urlencode = TRUE; memset(szBuf, 0, sizeof(szBuf)); szBufSize = PSHK_REG_VALUE_MAX_LEN; /* Get wether to output to logfile */ readRetVal = RegQueryValueEx(hk, "output2log", NULL, NULL, (LPBYTE)szBuf, &szBufSize); if( readRetVal != ERROR_SUCCESS ) return ret; if(!_stricmp(szBuf, "true") || \ !_stricmp(szBuf, "yes") || \ !_stricmp(szBuf, "on") ) ret.inheritParentHandles = TRUE; RegCloseKey(hk); ret.valid = 1; return ret; }