| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef _GNU_SOURCE |
| 22 |
# define _GNU_SOURCE |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <sys/stat.h> |
| 26 |
#include <sys/types.h> |
| 27 |
#include <ctype.h> |
| 28 |
#include <errno.h> |
| 29 |
#include <fcntl.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <string.h> |
| 33 |
#include <unistd.h> |
| 34 |
|
| 35 |
#include "string-macros.h" |
| 36 |
#include "messages.h" |
| 37 |
#include "procparser.h" |
| 38 |
#include "xalloc.h" |
| 39 |
|
| 40 |
static int |
| 41 |
compare_proc_table_structs (const void *a, const void *b) |
| 42 |
{ |
| 43 |
return strcmp (((const proc_table_struct *) a)->name, |
| 44 |
((const proc_table_struct *) b)->name); |
| 45 |
} |
| 46 |
|
| 47 |
void |
| 48 |
procparser (const char *filename, const proc_table_struct *proc_table, |
| 49 |
int proc_table_count, char separator) |
| 50 |
{ |
| 51 |
char namebuf[32]; |
| 52 |
proc_table_struct findme = { namebuf, NULL }; |
| 53 |
proc_table_struct *found; |
| 54 |
char *line = NULL; |
| 55 |
FILE *fp; |
| 56 |
size_t len = 0; |
| 57 |
ssize_t chread; |
| 58 |
|
| 59 |
#if __SIZEOF_LONG__ == 4 |
| 60 |
unsigned long long slotll; |
| 61 |
#endif |
| 62 |
|
| 63 |
if ((fp = fopen (filename, "r")) == NULL) |
| 64 |
plugin_error (STATE_UNKNOWN, errno, "error: cannot read %s", filename); |
| 65 |
|
| 66 |
while ((chread = getline (&line, &len, fp)) != -1) |
| 67 |
{ |
| 68 |
char *head = line; |
| 69 |
char *tail = strchr (line, separator); |
| 70 |
if (!tail) |
| 71 |
continue; |
| 72 |
*tail = '\0'; |
| 73 |
if (strlen (head) >= sizeof (namebuf)) |
| 74 |
continue; |
| 75 |
strcpy (namebuf, head); |
| 76 |
found = bsearch (&findme, proc_table, proc_table_count, |
| 77 |
sizeof (proc_table_struct), |
| 78 |
compare_proc_table_structs); |
| 79 |
head = tail + 1; |
| 80 |
if (!found) |
| 81 |
continue; |
| 82 |
|
| 83 |
#if __SIZEOF_LONG__ == 4 |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
slotll = strtoull (head, &tail, 10); |
| 89 |
*(found->slot) = (unsigned long) slotll; |
| 90 |
#else |
| 91 |
*(found->slot) = strtoul (head, &tail, 10); |
| 92 |
#endif |
| 93 |
} |
| 94 |
|
| 95 |
free (line); |
| 96 |
} |
| 97 |
|
| 98 |
int |
| 99 |
linelookup (char *line, char *pattern, char **value) |
| 100 |
{ |
| 101 |
char *p, *v; |
| 102 |
int len = strlen (pattern); |
| 103 |
|
| 104 |
if (!*line) |
| 105 |
return 0; |
| 106 |
|
| 107 |
|
| 108 |
if (strncmp (line, pattern, len)) |
| 109 |
return 0; |
| 110 |
|
| 111 |
|
| 112 |
for (p = line + len; isspace (*p); p++); |
| 113 |
|
| 114 |
|
| 115 |
if (*p != ':') |
| 116 |
return 0; |
| 117 |
|
| 118 |
|
| 119 |
for (++p; isspace (*p); p++); |
| 120 |
|
| 121 |
|
| 122 |
if (!*p) |
| 123 |
return 0; |
| 124 |
v = p; |
| 125 |
|
| 126 |
|
| 127 |
len = strlen (line) - 1; |
| 128 |
for (p = line + len; isspace (*(p - 1)); p--); |
| 129 |
*p = '\0'; |
| 130 |
|
| 131 |
*value = xstrdup (v); |
| 132 |
return 1; |
| 133 |
} |