| 1 |
#include <net-snmp/net-snmp-config.h> |
| 2 |
#include <net-snmp/net-snmp-features.h> |
| 3 |
|
| 4 |
#ifdef HAVE_IO_H |
| 5 |
#include <io.h> |
| 6 |
#endif |
| 7 |
#ifdef HAVE_STDLIB_H |
| 8 |
#include <stdlib.h> |
| 9 |
#endif |
| 10 |
#ifdef HAVE_STRING_H |
| 11 |
#include <string.h> |
| 12 |
#else |
| 13 |
#include <strings.h> |
| 14 |
#endif |
| 15 |
#ifdef HAVE_UNISTD_H |
| 16 |
#include <unistd.h> |
| 17 |
#endif |
| 18 |
#include <ctype.h> |
| 19 |
#include <sys/types.h> |
| 20 |
#ifdef HAVE_NETINET_IN_H |
| 21 |
#include <netinet/in.h> |
| 22 |
#endif |
| 23 |
#ifdef HAVE_SYS_WAIT_H |
| 24 |
# include <sys/wait.h> |
| 25 |
#endif |
| 26 |
#ifdef HAVE_LIMITS_H |
| 27 |
#include <limits.h> |
| 28 |
#endif |
| 29 |
#ifdef WIN32 |
| 30 |
#include <limits.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
#include <signal.h> |
| 34 |
#include <errno.h> |
| 35 |
|
| 36 |
#include <net-snmp/net-snmp-includes.h> |
| 37 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 38 |
|
| 39 |
#include "struct.h" |
| 40 |
#include "pass_persist.h" |
| 41 |
#include "pass_common.h" |
| 42 |
#include "extensible.h" |
| 43 |
#include "util_funcs.h" |
| 44 |
|
| 45 |
netsnmp_feature_require(get_exten_instance); |
| 46 |
netsnmp_feature_require(parse_miboid); |
| 47 |
|
| 48 |
struct extensible *persistpassthrus = NULL; |
| 49 |
int numpersistpassthrus = 0; |
| 50 |
struct persist_pipe_type { |
| 51 |
FILE *fIn; |
| 52 |
int fdOut; |
| 53 |
netsnmp_pid_t pid; |
| 54 |
} *persist_pipes = (struct persist_pipe_type *) NULL; |
| 55 |
static unsigned pipe_check_alarm_id; |
| 56 |
static int init_persist_pipes(void); |
| 57 |
static void close_persist_pipe(int iindex); |
| 58 |
static int open_persist_pipe(int iindex, char *command); |
| 59 |
static void check_persist_pipes(unsigned clientreg, void *clientarg); |
| 60 |
static void destruct_persist_pipes(void); |
| 61 |
static int write_persist_pipe(int iindex, const char *data); |
| 62 |
|
| 63 |
/* |
| 64 |
* the relocatable extensible commands variables |
| 65 |
*/ |
| 66 |
struct variable2 extensible_persist_passthru_variables[] = { |
| 67 |
/* |
| 68 |
* bogus entry. Only some of it is actually used. |
| 69 |
*/ |
| 70 |
{MIBINDEX, ASN_INTEGER, NETSNMP_OLDAPI_RWRITE, |
| 71 |
var_extensible_pass_persist, 0, {MIBINDEX}}, |
| 72 |
}; |
| 73 |
|
| 74 |
void |
| 75 |
init_pass_persist(void) |
| 76 |
{ |
| 77 |
snmpd_register_config_handler("pass_persist", |
| 78 |
pass_persist_parse_config, |
| 79 |
pass_persist_free_config, |
| 80 |
"miboid program"); |
| 81 |
pipe_check_alarm_id = snmp_alarm_register(10, SA_REPEAT, check_persist_pipes, NULL); |
| 82 |
} |
| 83 |
|
| 84 |
void |
| 85 |
shutdown_pass_persist(void) |
| 86 |
{ |
| 87 |
if (pipe_check_alarm_id) { |
| 88 |
snmp_alarm_unregister(pipe_check_alarm_id); |
| 89 |
pipe_check_alarm_id = 0; |
| 90 |
} |
| 91 |
|
| 92 |
/* Close any open pipes. */ |
| 93 |
destruct_persist_pipes(); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
#ifdef USING_SINGLE_COMMON_PASSPERSIST_INSTANCE |
| 98 |
void |
| 99 |
pass_persist_group(struct extensible *persistpassthrus) |
| 100 |
{ |
| 101 |
struct extensible *ptmp, *ptmp1; |
| 102 |
|
| 103 |
/* |
| 104 |
* reset groupping |
| 105 |
*/ |
| 106 |
for (ptmp = persistpassthrus; ptmp != NULL; ptmp = ptmp->next) { |
| 107 |
ptmp->passpersist_inst = NULL; |
| 108 |
} |
| 109 |
|
| 110 |
/* |
| 111 |
* group |
| 112 |
*/ |
| 113 |
for (ptmp = persistpassthrus; ptmp != NULL; ptmp = ptmp->next) { |
| 114 |
/* skip already groupped items */ |
| 115 |
if (ptmp->passpersist_inst != NULL) { |
| 116 |
continue; |
| 117 |
} |
| 118 |
for (ptmp1 = persistpassthrus; ptmp1 != NULL; ptmp1 = ptmp1->next) { |
| 119 |
if (ptmp1 == ptmp) { |
| 120 |
continue; |
| 121 |
} |
| 122 |
|
| 123 |
if (strcmp(ptmp->command, ptmp1->command) == 0) { |
| 124 |
ptmp1->passpersist_inst = ptmp; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
#endif /* USING_SINGLE_COMMON_PASSPERSIST_INSTANCE */ |
| 130 |
|
| 131 |
void |
| 132 |
pass_persist_parse_config(const char *token, char *cptr) |
| 133 |
{ |
| 134 |
struct extensible **ppass = &persistpassthrus, **etmp, *ptmp; |
| 135 |
char *tcptr, *endopt; |
| 136 |
int i; |
| 137 |
long int priority; |
| 138 |
|
| 139 |
/* |
| 140 |
* options |
| 141 |
*/ |
| 142 |
priority = DEFAULT_MIB_PRIORITY; |
| 143 |
while (*cptr == '-') { |
| 144 |
cptr++; |
| 145 |
switch (*cptr) { |
| 146 |
case 'p': |
| 147 |
/* change priority level */ |
| 148 |
cptr++; |
| 149 |
cptr = skip_white(cptr); |
| 150 |
if (! isdigit((unsigned char)(*cptr))) { |
| 151 |
config_perror("priority must be an integer"); |
| 152 |
return; |
| 153 |
} |
| 154 |
priority = strtol((const char*) cptr, &endopt, 0); |
| 155 |
if ((priority == LONG_MIN) || (priority == LONG_MAX)) { |
| 156 |
config_perror("priority under/overflow"); |
| 157 |
return; |
| 158 |
} |
| 159 |
cptr = endopt; |
| 160 |
cptr = skip_white(cptr); |
| 161 |
break; |
| 162 |
default: |
| 163 |
config_perror("unknown option for pass directive"); |
| 164 |
return; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/* |
| 169 |
* MIB |
| 170 |
*/ |
| 171 |
if (*cptr == '.') |
| 172 |
cptr++; |
| 173 |
if (!isdigit((unsigned char)(*cptr))) { |
| 174 |
config_perror("second token is not a OID"); |
| 175 |
return; |
| 176 |
} |
| 177 |
numpersistpassthrus++; |
| 178 |
|
| 179 |
while (*ppass != NULL) |
| 180 |
ppass = &((*ppass)->next); |
| 181 |
*ppass = calloc(1, sizeof(**ppass)); |
| 182 |
if (*ppass == NULL) |
| 183 |
return; |
| 184 |
(*ppass)->type = PASSTHRU_PERSIST; |
| 185 |
(*ppass)->mibpriority = priority; |
| 186 |
|
| 187 |
(*ppass)->miblen = parse_miboid(cptr, (*ppass)->miboid); |
| 188 |
while (isdigit((unsigned char)(*cptr)) || *cptr == '.') |
| 189 |
cptr++; |
| 190 |
/* |
| 191 |
* path |
| 192 |
*/ |
| 193 |
free((*ppass)->command); |
| 194 |
(*ppass)->command = NULL; |
| 195 |
cptr = skip_white(cptr); |
| 196 |
if (cptr == NULL) { |
| 197 |
config_perror("No command specified on pass_persist line"); |
| 198 |
if (asprintf(&(*ppass)->command, "%s", "") < 0) { |
| 199 |
} |
| 200 |
} else { |
| 201 |
for (tcptr = cptr; *tcptr != 0 && *tcptr != '#' && *tcptr != ';'; |
| 202 |
tcptr++); |
| 203 |
if (asprintf(&(*ppass)->command, "%.*s", (int)(tcptr - cptr), cptr) |
| 204 |
< 0) { |
| 205 |
} |
| 206 |
} |
| 207 |
strlcpy((*ppass)->name, (*ppass)->command, sizeof((*ppass)->name)); |
| 208 |
(*ppass)->next = NULL; |
| 209 |
|
| 210 |
register_mib_priority("pass_persist", |
| 211 |
(struct variable *) extensible_persist_passthru_variables, |
| 212 |
sizeof(struct variable2), 1, (*ppass)->miboid, |
| 213 |
(*ppass)->miblen, (*ppass)->mibpriority); |
| 214 |
|
| 215 |
/* |
| 216 |
* argggg -- pasthrus must be sorted |
| 217 |
*/ |
| 218 |
if (numpersistpassthrus > 1) { |
| 219 |
etmp = (struct extensible **) |
| 220 |
malloc(((sizeof(struct extensible *)) * numpersistpassthrus)); |
| 221 |
if (etmp == NULL) |
| 222 |
return; |
| 223 |
for (i = 0, ptmp = (struct extensible *) persistpassthrus; |
| 224 |
i < numpersistpassthrus && ptmp != NULL; i++, ptmp = ptmp->next) |
| 225 |
etmp[i] = ptmp; |
| 226 |
qsort(etmp, numpersistpassthrus, sizeof(struct extensible *), |
| 227 |
pass_persist_compare); |
| 228 |
persistpassthrus = (struct extensible *) etmp[0]; |
| 229 |
ptmp = (struct extensible *) etmp[0]; |
| 230 |
|
| 231 |
for (i = 0; i < numpersistpassthrus - 1; i++) { |
| 232 |
ptmp->next = etmp[i + 1]; |
| 233 |
ptmp = ptmp->next; |
| 234 |
} |
| 235 |
ptmp->next = NULL; |
| 236 |
free(etmp); |
| 237 |
} |
| 238 |
|
| 239 |
#ifdef USING_SINGLE_COMMON_PASSPERSIST_INSTANCE |
| 240 |
pass_persist_group(persistpassthrus); |
| 241 |
#endif /* USING_SINGLE_COMMON_PASSPERSIST_INSTANCE */ |
| 242 |
} |
| 243 |
|
| 244 |
void |
| 245 |
pass_persist_free_config(void) |
| 246 |
{ |
| 247 |
struct extensible *etmp, *etmp2; |
| 248 |
int i; |
| 249 |
|
| 250 |
for (etmp = persistpassthrus; etmp != NULL;) { |
| 251 |
etmp2 = etmp; |
| 252 |
etmp = etmp->next; |
| 253 |
unregister_mib_priority(etmp2->miboid, etmp2->miblen, etmp2->mibpriority); |
| 254 |
free(etmp2); |
| 255 |
} |
| 256 |
if (persist_pipes) { |
| 257 |
for (i = 0; i <= numpersistpassthrus; i++) { |
| 258 |
close_persist_pipe(i); |
| 259 |
} |
| 260 |
} |
| 261 |
persistpassthrus = NULL; |
| 262 |
numpersistpassthrus = 0; |
| 263 |
} |
| 264 |
|
| 265 |
#ifdef USING_SINGLE_COMMON_PASSPERSIST_INSTANCE |
| 266 |
int get_exten_group_id(struct extensible *persistpassthru, |
| 267 |
int current_id) |
| 268 |
{ |
| 269 |
struct extensible *ptmp; |
| 270 |
int idx; |
| 271 |
|
| 272 |
if (persistpassthru == NULL) |
| 273 |
return current_id; |
| 274 |
|
| 275 |
for (idx = 1, ptmp = persistpassthrus; |
| 276 |
ptmp != NULL; ptmp = ptmp->next, idx++) { |
| 277 |
if (ptmp == persistpassthru) { |
| 278 |
return idx; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
/* should never really come here, but safety doesn't hurt */ |
| 283 |
return current_id; |
| 284 |
} |
| 285 |
#endif /* USING_SINGLE_COMMON_PASSPERSIST_INSTANCE */ |
| 286 |
|
| 287 |
u_char * |
| 288 |
var_extensible_pass_persist(struct variable *vp, |
| 289 |
oid * name, |
| 290 |
size_t * length, |
| 291 |
int exact, |
| 292 |
size_t * var_len, WriteMethod ** write_method) |
| 293 |
{ |
| 294 |
oid newname[MAX_OID_LEN]; |
| 295 |
int i, rtest, newlen; |
| 296 |
char buf[SNMP_MAXBUF]; |
| 297 |
static char buf2[SNMP_MAXBUF]; |
| 298 |
struct extensible *persistpassthru; |
| 299 |
FILE *file; |
| 300 |
int pipe_idx; |
| 301 |
|
| 302 |
/* |
| 303 |
* Make sure that our basic pipe structure is malloced |
| 304 |
*/ |
| 305 |
init_persist_pipes(); |
| 306 |
|
| 307 |
for (i = 1; i <= numpersistpassthrus; i++) { |
| 308 |
persistpassthru = get_exten_instance(persistpassthrus, i); |
| 309 |
rtest = snmp_oidtree_compare(name, *length, |
| 310 |
persistpassthru->miboid, |
| 311 |
persistpassthru->miblen); |
| 312 |
if ((exact && rtest == 0) || (!exact && rtest <= 0)) { |
| 313 |
/* |
| 314 |
* setup args |
| 315 |
*/ |
| 316 |
if (persistpassthru->miblen >= *length || rtest < 0) |
| 317 |
sprint_mib_oid(buf, persistpassthru->miboid, |
| 318 |
persistpassthru->miblen); |
| 319 |
else |
| 320 |
sprint_mib_oid(buf, name, *length); |
| 321 |
|
| 322 |
pipe_idx = i; |
| 323 |
#ifdef USING_SINGLE_COMMON_PASSPERSIST_INSTANCE |
| 324 |
pipe_idx = |
| 325 |
get_exten_group_id(persistpassthru->passpersist_inst, i); |
| 326 |
|
| 327 |
if (pipe_idx != i) { |
| 328 |
persistpassthru = persistpassthru->passpersist_inst; |
| 329 |
} |
| 330 |
#endif /* USING_SINGLE_COMMON_PASSPERSIST_INSTANCE */ |
| 331 |
/* |
| 332 |
* Open our pipe if necessary |
| 333 |
*/ |
| 334 |
if (!open_persist_pipe(pipe_idx, persistpassthru->name)) { |
| 335 |
return (NULL); |
| 336 |
} |
| 337 |
|
| 338 |
free(persistpassthru->command); |
| 339 |
if (asprintf(&persistpassthru->command, "%s\n%s\n", |
| 340 |
exact ? "get" : "getnext", buf) < 0) { |
| 341 |
persistpassthru->command = NULL; |
| 342 |
*var_len = 0; |
| 343 |
return NULL; |
| 344 |
} |
| 345 |
|
| 346 |
DEBUGMSGTL(("ucd-snmp/pass_persist", |
| 347 |
"persistpass-sending:\n%s", |
| 348 |
persistpassthru->command)); |
| 349 |
if (!write_persist_pipe(pipe_idx, persistpassthru->command)) { |
| 350 |
*var_len = 0; |
| 351 |
/* |
| 352 |
* close_persist_pipes is called in write_persist_pipe |
| 353 |
*/ |
| 354 |
return (NULL); |
| 355 |
} |
| 356 |
|
| 357 |
/* |
| 358 |
* valid call. Exec and get output |
| 359 |
*/ |
| 360 |
|
| 361 |
if ((file = persist_pipes[pipe_idx].fIn)) { |
| 362 |
if (fgets(buf, sizeof(buf), file) == NULL) { |
| 363 |
*var_len = 0; |
| 364 |
close_persist_pipe(pipe_idx); |
| 365 |
return (NULL); |
| 366 |
} |
| 367 |
/* |
| 368 |
* persistent scripts return "NONE\n" on invalid items |
| 369 |
*/ |
| 370 |
if (!strncmp(buf, "NONE", 4)) { |
| 371 |
if (exact) { |
| 372 |
*var_len = 0; |
| 373 |
return (NULL); |
| 374 |
} |
| 375 |
continue; |
| 376 |
} |
| 377 |
newlen = parse_miboid(buf, newname); |
| 378 |
|
| 379 |
/* |
| 380 |
* its good, so copy onto name/length |
| 381 |
*/ |
| 382 |
memcpy((char *) name, (char *) newname, |
| 383 |
(int) newlen * sizeof(oid)); |
| 384 |
*length = newlen; |
| 385 |
|
| 386 |
/* |
| 387 |
* set up return pointer for setable stuff |
| 388 |
*/ |
| 389 |
*write_method = setPassPersist; |
| 390 |
|
| 391 |
if (newlen == 0 || fgets(buf, sizeof(buf), file) == NULL |
| 392 |
|| fgets(buf2, sizeof(buf2), file) == NULL) { |
| 393 |
*var_len = 0; |
| 394 |
close_persist_pipe(pipe_idx); |
| 395 |
return (NULL); |
| 396 |
} |
| 397 |
return netsnmp_internal_pass_parse(buf, buf2, var_len, vp); |
| 398 |
} |
| 399 |
*var_len = 0; |
| 400 |
return (NULL); |
| 401 |
} |
| 402 |
} |
| 403 |
if (var_len) |
| 404 |
*var_len = 0; |
| 405 |
*write_method = NULL; |
| 406 |
return (NULL); |
| 407 |
} |
| 408 |
|
| 409 |
int |
| 410 |
setPassPersist(int action, |
| 411 |
u_char * var_val, |
| 412 |
u_char var_val_type, |
| 413 |
size_t var_val_len, |
| 414 |
u_char * statP, oid * name, size_t name_len) |
| 415 |
{ |
| 416 |
int i, rtest; |
| 417 |
struct extensible *persistpassthru; |
| 418 |
|
| 419 |
char buf[SNMP_MAXBUF], buf2[SNMP_MAXBUF]; |
| 420 |
int pipe_idx; |
| 421 |
|
| 422 |
/* |
| 423 |
* Make sure that our basic pipe structure is malloced |
| 424 |
*/ |
| 425 |
init_persist_pipes(); |
| 426 |
|
| 427 |
for (i = 1; i <= numpersistpassthrus; i++) { |
| 428 |
persistpassthru = get_exten_instance(persistpassthrus, i); |
| 429 |
rtest = snmp_oidtree_compare(name, name_len, |
| 430 |
persistpassthru->miboid, |
| 431 |
persistpassthru->miblen); |
| 432 |
pipe_idx = i; |
| 433 |
#ifdef USING_SINGLE_COMMON_PASSPERSIST_INSTANCE |
| 434 |
pipe_idx = |
| 435 |
get_exten_group_id(persistpassthru->passpersist_inst, i); |
| 436 |
|
| 437 |
if (pipe_idx != i) { |
| 438 |
persistpassthru = persistpassthru->passpersist_inst; |
| 439 |
} |
| 440 |
#endif /* USING_SINGLE_COMMON_PASSPERSIST_INSTANCE */ |
| 441 |
if (rtest <= 0) { |
| 442 |
if (action != ACTION) |
| 443 |
return SNMP_ERR_NOERROR; |
| 444 |
/* |
| 445 |
* setup args |
| 446 |
*/ |
| 447 |
if (persistpassthru->miblen >= name_len || rtest < 0) |
| 448 |
sprint_mib_oid(buf, persistpassthru->miboid, |
| 449 |
persistpassthru->miblen); |
| 450 |
else |
| 451 |
sprint_mib_oid(buf, name, name_len); |
| 452 |
netsnmp_internal_pass_set_format(buf2, var_val, var_val_type, |
| 453 |
var_val_len); |
| 454 |
free(persistpassthru->command); |
| 455 |
if (asprintf(&persistpassthru->command, "set\n%s\n%s", buf, |
| 456 |
buf2) < 0) { |
| 457 |
persistpassthru->command = NULL; |
| 458 |
return SNMP_ERR_GENERR; |
| 459 |
} |
| 460 |
|
| 461 |
if (!open_persist_pipe(pipe_idx, persistpassthru->name)) { |
| 462 |
return SNMP_ERR_NOTWRITABLE; |
| 463 |
} |
| 464 |
|
| 465 |
DEBUGMSGTL(("ucd-snmp/pass_persist", |
| 466 |
"persistpass-writing: %s\n", |
| 467 |
persistpassthru->command)); |
| 468 |
if (!write_persist_pipe(pipe_idx, persistpassthru->command)) { |
| 469 |
close_persist_pipe(pipe_idx); |
| 470 |
return SNMP_ERR_NOTWRITABLE; |
| 471 |
} |
| 472 |
|
| 473 |
if (fgets(buf, sizeof(buf), persist_pipes[pipe_idx].fIn) == NULL) { |
| 474 |
close_persist_pipe(pipe_idx); |
| 475 |
return SNMP_ERR_NOTWRITABLE; |
| 476 |
} |
| 477 |
|
| 478 |
return netsnmp_internal_pass_str_to_errno(buf); |
| 479 |
} |
| 480 |
} |
| 481 |
if (snmp_get_do_debugging()) { |
| 482 |
sprint_mib_oid(buf2, name, name_len); |
| 483 |
DEBUGMSGTL(("ucd-snmp/pass_persist", "persistpass-notfound: %s\n", |
| 484 |
buf2)); |
| 485 |
} |
| 486 |
return SNMP_ERR_NOSUCHNAME; |
| 487 |
} |
| 488 |
|
| 489 |
int |
| 490 |
pass_persist_compare(const void *a, const void *b) |
| 491 |
{ |
| 492 |
const struct extensible *const *ap, *const *bp; |
| 493 |
ap = (const struct extensible * const *) a; |
| 494 |
bp = (const struct extensible * const *) b; |
| 495 |
return snmp_oid_compare((*ap)->miboid, (*ap)->miblen, (*bp)->miboid, |
| 496 |
(*bp)->miblen); |
| 497 |
} |
| 498 |
|
| 499 |
/* |
| 500 |
* Initialize our persistent pipes |
| 501 |
* - Returns 1 on success, 0 on failure. |
| 502 |
* - Initializes all FILE pointers to NULL to indicate "closed" |
| 503 |
*/ |
| 504 |
static int |
| 505 |
init_persist_pipes(void) |
| 506 |
{ |
| 507 |
int i; |
| 508 |
|
| 509 |
/* |
| 510 |
* if we are already taken care of, just return |
| 511 |
*/ |
| 512 |
if (persist_pipes) |
| 513 |
return 1; |
| 514 |
|
| 515 |
/* |
| 516 |
* Otherwise malloc and initialize |
| 517 |
*/ |
| 518 |
persist_pipes = malloc(sizeof(persist_pipes[0]) * |
| 519 |
(numpersistpassthrus + 1)); |
| 520 |
if (!persist_pipes) |
| 521 |
return 0; |
| 522 |
for (i = 0; i <= numpersistpassthrus; i++) { |
| 523 |
persist_pipes[i].fIn = NULL; |
| 524 |
persist_pipes[i].fdOut = -1; |
| 525 |
persist_pipes[i].pid = NETSNMP_NO_SUCH_PROCESS; |
| 526 |
} |
| 527 |
return 1; |
| 528 |
} |
| 529 |
|
| 530 |
/** |
| 531 |
* Return true if and only if the process associated with the persistent |
| 532 |
* pipe has stopped. |
| 533 |
* |
| 534 |
* @param[in] idx Persistent pipe index. |
| 535 |
*/ |
| 536 |
static int process_stopped(int idx) |
| 537 |
{ |
| 538 |
if (persist_pipes[idx].pid != NETSNMP_NO_SUCH_PROCESS) { |
| 539 |
#ifdef HAVE_WAITPID |
| 540 |
return waitpid(persist_pipes[idx].pid, NULL, WNOHANG) > 0; |
| 541 |
#elif defined(WIN32) && !defined (mingw32) && !defined(HAVE_SIGNAL) |
| 542 |
return WaitForSingleObject(persist_pipes[idx].pid, 0) == WAIT_OBJECT_0; |
| 543 |
#endif |
| 544 |
} |
| 545 |
return 0; |
| 546 |
} |
| 547 |
|
| 548 |
/** |
| 549 |
* Iterate over all persistent pipes and close those pipes of which the |
| 550 |
* associated process has stopped. |
| 551 |
*/ |
| 552 |
static void check_persist_pipes(unsigned clientreg, void *clientarg) |
| 553 |
{ |
| 554 |
int i; |
| 555 |
|
| 556 |
if (!persist_pipes) |
| 557 |
return; |
| 558 |
|
| 559 |
for (i = 0; i <= numpersistpassthrus; i++) { |
| 560 |
if (process_stopped(i)) { |
| 561 |
snmp_log(LOG_INFO, "pass_persist[%d]: child process stopped - closing pipe\n", i); |
| 562 |
close_persist_pipe(i); |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
/* |
| 568 |
* Destruct our persistent pipes |
| 569 |
* |
| 570 |
*/ |
| 571 |
static void |
| 572 |
destruct_persist_pipes(void) |
| 573 |
{ |
| 574 |
int i; |
| 575 |
|
| 576 |
/* |
| 577 |
* Return if there are no pipes |
| 578 |
*/ |
| 579 |
if (!persist_pipes) { |
| 580 |
return; |
| 581 |
} |
| 582 |
|
| 583 |
for (i = 0; i <= numpersistpassthrus; i++) { |
| 584 |
close_persist_pipe(i); |
| 585 |
} |
| 586 |
|
| 587 |
free(persist_pipes); |
| 588 |
persist_pipes = (struct persist_pipe_type *) 0; |
| 589 |
} |
| 590 |
|
| 591 |
/* |
| 592 |
* returns 0 on failure, 1 on success |
| 593 |
*/ |
| 594 |
static int |
| 595 |
open_persist_pipe(int iindex, char *command) |
| 596 |
{ |
| 597 |
static int recurse = 0; /* used to allow one level of recursion */ |
| 598 |
|
| 599 |
DEBUGMSGTL(("ucd-snmp/pass_persist", "open_persist_pipe(%d,'%s') recurse=%d\n", |
| 600 |
iindex, command, recurse)); |
| 601 |
/* |
| 602 |
* Open if it's not already open |
| 603 |
*/ |
| 604 |
if (persist_pipes[iindex].pid == NETSNMP_NO_SUCH_PROCESS) { |
| 605 |
int fdIn, fdOut; |
| 606 |
netsnmp_pid_t pid; |
| 607 |
|
| 608 |
/* |
| 609 |
* Did we fail? |
| 610 |
*/ |
| 611 |
if ((0 == get_exec_pipes(command, &fdIn, &fdOut, &pid)) || |
| 612 |
(pid == NETSNMP_NO_SUCH_PROCESS)) { |
| 613 |
DEBUGMSGTL(("ucd-snmp/pass_persist", |
| 614 |
"open_persist_pipe: pid == -1\n")); |
| 615 |
recurse = 0; |
| 616 |
return 0; |
| 617 |
} |
| 618 |
|
| 619 |
/* |
| 620 |
* If not, fill out our structure |
| 621 |
*/ |
| 622 |
persist_pipes[iindex].pid = pid; |
| 623 |
persist_pipes[iindex].fdOut = fdOut; |
| 624 |
persist_pipes[iindex].fIn = fdopen(fdIn, "r"); |
| 625 |
|
| 626 |
DEBUGMSGTL(("ucd-snmp/pass_persist", "open_persist_pipe: opened the pipes\n")); |
| 627 |
} |
| 628 |
|
| 629 |
/* |
| 630 |
* Send test packet always so we can self-catch |
| 631 |
*/ |
| 632 |
{ |
| 633 |
char buf[SNMP_MAXBUF]; |
| 634 |
/* |
| 635 |
* Should catch SIGPIPE around this call! |
| 636 |
*/ |
| 637 |
if (!write_persist_pipe(iindex, "PING\n")) { |
| 638 |
DEBUGMSGTL(("ucd-snmp/pass_persist", |
| 639 |
"open_persist_pipe: Error writing PING\n")); |
| 640 |
close_persist_pipe(iindex); |
| 641 |
|
| 642 |
/* |
| 643 |
* Recurse one time if we get a SIGPIPE |
| 644 |
*/ |
| 645 |
if (!recurse) { |
| 646 |
DEBUGMSGTL(("ucd-snmp/pass_persist", "open_persist_pipe: recursing to reopen\n")); |
| 647 |
recurse = 1; |
| 648 |
return open_persist_pipe(iindex, command); |
| 649 |
} |
| 650 |
recurse = 0; |
| 651 |
return 0; |
| 652 |
} |
| 653 |
if (fgets(buf, sizeof(buf), persist_pipes[iindex].fIn) == NULL) { |
| 654 |
DEBUGMSGTL(("ucd-snmp/pass_persist", |
| 655 |
"open_persist_pipe: Error reading for PONG\n")); |
| 656 |
close_persist_pipe(iindex); |
| 657 |
recurse = 0; |
| 658 |
return 0; |
| 659 |
} |
| 660 |
|
| 661 |
if (strncmp(buf, "PONG", 4)) { |
| 662 |
DEBUGMSGTL(("ucd-snmp/pass_persist", |
| 663 |
"open_persist_pipe: Got %s instead of PONG!\n", buf)); |
| 664 |
close_persist_pipe(iindex); |
| 665 |
recurse = 0; |
| 666 |
return 0; |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
recurse = 0; |
| 671 |
return 1; |
| 672 |
} |
| 673 |
|
| 674 |
static int |
| 675 |
write_persist_pipe(int iindex, const char *data) |
| 676 |
{ |
| 677 |
int len, wret; |
| 678 |
|
| 679 |
/* |
| 680 |
* Don't write to a non-existent process |
| 681 |
*/ |
| 682 |
if (persist_pipes[iindex].pid == NETSNMP_NO_SUCH_PROCESS) { |
| 683 |
DEBUGMSGTL(("ucd-snmp/pass_persist", |
| 684 |
"write_persist_pipe: not writing %s, process is non-existent", |
| 685 |
data)); |
| 686 |
return 0; |
| 687 |
} |
| 688 |
|
| 689 |
/* |
| 690 |
* Do the write |
| 691 |
*/ |
| 692 |
len = strlen(data); |
| 693 |
wret = write(persist_pipes[iindex].fdOut, data, len); |
| 694 |
if (wret == len) |
| 695 |
return 1; |
| 696 |
if (wret < 0) { |
| 697 |
int werrno = errno; |
| 698 |
|
| 699 |
if (werrno != EPIPE) { |
| 700 |
DEBUGMSGTL(("ucd-snmp/pass_persist", |
| 701 |
"write_persist_pipe: write returned unexpected error %d (%s)\n", |
| 702 |
werrno, strerror(werrno))); |
| 703 |
} |
| 704 |
close_persist_pipe(iindex); |
| 705 |
} else { |
| 706 |
DEBUGMSGTL(("ucd-snmp/pass_persist", |
| 707 |
"write_persist_pipe: short write (%d < %d)\n", wret, len)); |
| 708 |
} |
| 709 |
return 0; |
| 710 |
} |
| 711 |
|
| 712 |
static void |
| 713 |
close_persist_pipe(int iindex) |
| 714 |
{ |
| 715 |
/* |
| 716 |
* Check and nix every item |
| 717 |
*/ |
| 718 |
if (persist_pipes[iindex].fdOut != -1) { |
| 719 |
close(persist_pipes[iindex].fdOut); |
| 720 |
persist_pipes[iindex].fdOut = -1; |
| 721 |
} |
| 722 |
if (persist_pipes[iindex].fIn) { |
| 723 |
fclose(persist_pipes[iindex].fIn); |
| 724 |
persist_pipes[iindex].fIn = (FILE *) 0; |
| 725 |
} |
| 726 |
|
| 727 |
if (persist_pipes[iindex].pid != NETSNMP_NO_SUCH_PROCESS) { |
| 728 |
/* |
| 729 |
* kill the child, in case we got an error and the child is not |
| 730 |
* cooperating. Ignore the return code. |
| 731 |
*/ |
| 732 |
#ifdef HAVE_SIGNAL |
| 733 |
(void)kill(persist_pipes[iindex].pid, SIGKILL); |
| 734 |
#endif |
| 735 |
#ifdef HAVE_WAITPID |
| 736 |
waitpid(persist_pipes[iindex].pid, NULL, 0); |
| 737 |
#elif defined(WIN32) && !defined (mingw32) && !defined (HAVE_SIGNAL) |
| 738 |
if (!CloseHandle(persist_pipes[iindex].pid)) { |
| 739 |
DEBUGMSGTL(("ucd-snmp/pass_persist", |
| 740 |
"close_persist_pipe pid: close error\n")); |
| 741 |
} |
| 742 |
#endif |
| 743 |
persist_pipes[iindex].pid = NETSNMP_NO_SUCH_PROCESS; |
| 744 |
} |
| 745 |
|
| 746 |
} |