diff options
| author | David M. Syzdek <syzdek@bindlebinaries.com> | 2013-12-06 13:22:38 -0900 |
|---|---|---|
| committer | David M. Syzdek <syzdek@bindlebinaries.com> | 2013-12-06 13:22:38 -0900 |
| commit | 576aa630d14ba12f04423b7d00bfb821b764a381 (patch) | |
| tree | aa95ae3134815c97f2934a25c217f0ccc304cfa5 | |
| parent | 6c5c360da8d01cab3f7fbc231daba50b29bf4e06 (diff) | |
| download | dmstools-576aa630d14ba12f04423b7d00bfb821b764a381.tar.gz dmstools-576aa630d14ba12f04423b7d00bfb821b764a381.tar.bz2 dmstools-576aa630d14ba12f04423b7d00bfb821b764a381.tar.xz dmstools-576aa630d14ba12f04423b7d00bfb821b764a381.zip | |
Updating suicide logic.
| -rw-r--r-- | src/suicide.c | 107 |
1 files changed, 32 insertions, 75 deletions
diff --git a/src/suicide.c b/src/suicide.c index 02ed91b..f8c1b4e 100644 --- a/src/suicide.c +++ b/src/suicide.c | |||
| @@ -132,17 +132,6 @@ struct suicide_signal_data | |||
| 132 | }; | 132 | }; |
| 133 | 133 | ||
| 134 | 134 | ||
| 135 | /// table of signal data | ||
| 136 | typedef struct suicide_signal_action SuicideAction; | ||
| 137 | struct suicide_signal_action | ||
| 138 | { | ||
| 139 | int number; | ||
| 140 | int pad0; | ||
| 141 | void * pad1; | ||
| 142 | const SuicideData * data; | ||
| 143 | }; | ||
| 144 | |||
| 145 | |||
| 146 | ////////////////// | 135 | ////////////////// |
| 147 | // // | 136 | // // |
| 148 | // Prototypes // | 137 | // Prototypes // |
| @@ -157,8 +146,7 @@ int suicide_cmp_val PARAMS((const SuicideData * p1, | |||
| 157 | const SuicideData * p2)); | 146 | const SuicideData * p2)); |
| 158 | 147 | ||
| 159 | // finds a signal from the table and returns action | 148 | // finds a signal from the table and returns action |
| 160 | int suicide_find_data PARAMS((SuicideAction *** list, size_t * count, | 149 | int suicide_find_data PARAMS((SuicideData ** data, const char * str, int val)); |
| 161 | const char * str, int val)); | ||
| 162 | 150 | ||
| 163 | // empty signal handler | 151 | // empty signal handler |
| 164 | void suicide_signal_handler PARAMS((int sig)); | 152 | void suicide_signal_handler PARAMS((int sig)); |
| @@ -182,7 +170,7 @@ void suicide_version PARAMS((void)); | |||
| 182 | // // | 170 | // // |
| 183 | ///////////////// | 171 | ///////////////// |
| 184 | 172 | ||
| 185 | SuicideData susv3_signals[] = | 173 | SuicideData susv3_signals[] = |
| 186 | { | 174 | { |
| 187 | #ifdef SIGABRT | 175 | #ifdef SIGABRT |
| 188 | { 'A', SIGABRT, "SIGABRT", "Process abort signal." }, | 176 | { 'A', SIGABRT, "SIGABRT", "Process abort signal." }, |
| @@ -325,12 +313,11 @@ int main(int argc, char * argv[]) | |||
| 325 | int verbose; | 313 | int verbose; |
| 326 | int attempt_catch; | 314 | int attempt_catch; |
| 327 | int opt_index; | 315 | int opt_index; |
| 316 | int sig; | ||
| 328 | char buff[16]; | 317 | char buff[16]; |
| 329 | pid_t p; // process ID | 318 | pid_t p; // process ID |
| 330 | pid_t pp; // parent process ID | 319 | pid_t pp; // parent process ID |
| 331 | size_t count; | 320 | SuicideData * data; |
| 332 | size_t action_size; | ||
| 333 | SuicideAction ** actions; | ||
| 334 | 321 | ||
| 335 | static char short_opt[] = "chin:s:SvV"; | 322 | static char short_opt[] = "chin:s:SvV"; |
| 336 | static struct option long_opt[] = | 323 | static struct option long_opt[] = |
| @@ -350,10 +337,8 @@ int main(int argc, char * argv[]) | |||
| 350 | 337 | ||
| 351 | p = getpid(); | 338 | p = getpid(); |
| 352 | pp = getppid(); | 339 | pp = getppid(); |
| 353 | actions = NULL; | ||
| 354 | verbose = 0; | 340 | verbose = 0; |
| 355 | opt_index = 0; | 341 | opt_index = 0; |
| 356 | action_size = 0; | ||
| 357 | attempt_catch = 0; | 342 | attempt_catch = 0; |
| 358 | 343 | ||
| 359 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) | 344 | while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1) |
| @@ -374,11 +359,11 @@ int main(int argc, char * argv[]) | |||
| 374 | return(0); | 359 | return(0); |
| 375 | case 'n': | 360 | case 'n': |
| 376 | val = (int)strtol(optarg, NULL, 0); | 361 | val = (int)strtol(optarg, NULL, 0); |
| 377 | if ((suicide_find_data(&actions, &action_size, NULL, val))) | 362 | if ((sig = suicide_find_data(&data, NULL, val)) == -1) |
| 378 | return(1); | 363 | return(1); |
| 379 | break; | 364 | break; |
| 380 | case 's': | 365 | case 's': |
| 381 | if ((suicide_find_data(&actions, &action_size, optarg, -1))) | 366 | if ((sig = suicide_find_data(&data, optarg, -1)) == -1) |
| 382 | return(1); | 367 | return(1); |
| 383 | break; | 368 | break; |
| 384 | case 'S': | 369 | case 'S': |
| @@ -404,32 +389,27 @@ int main(int argc, char * argv[]) | |||
| 404 | 389 | ||
| 405 | if (verbose) | 390 | if (verbose) |
| 406 | { | 391 | { |
| 407 | printf(">> getppid(): %i\n", pp); | 392 | if ((data)) |
| 408 | printf(">> getpid(): %i\n", p); | 393 | printf(">> %-9s == %i\n", data->name, sig); |
| 394 | printf(">> getppid() == %i\n", pp); | ||
| 395 | printf(">> getpid() == %i\n", p); | ||
| 409 | }; | 396 | }; |
| 410 | 397 | ||
| 398 | if ((data)) | ||
| 399 | snprintf(buff, 16L, "%s", data->name); | ||
| 400 | else | ||
| 401 | snprintf(buff, 16L, "%i", sig); | ||
| 411 | if (attempt_catch) | 402 | if (attempt_catch) |
| 412 | for(c = 0; c < 32; c++) | ||
| 413 | signal(c, suicide_signal_handler); | ||
| 414 | |||
| 415 | for(count = 0; count < action_size; count++) | ||
| 416 | { | 403 | { |
| 417 | if (actions[count]->data) | ||
| 418 | snprintf(buff, 16L, "%s", actions[count]->data->name); | ||
| 419 | else | ||
| 420 | snprintf(buff, 16L, "%i", actions[count]->number); | ||
| 421 | if (attempt_catch) | ||
| 422 | { | ||
| 423 | if (verbose) | ||
| 424 | printf(">> signal(%s)\n", buff); | ||
| 425 | signal(actions[count]->number, suicide_signal_handler); | ||
| 426 | }; | ||
| 427 | printf(">> kill(%i, %s)\n", p, buff); | ||
| 428 | kill(p, actions[count]->number); | ||
| 429 | if (verbose) | 404 | if (verbose) |
| 430 | printf(">> usleep(100)\n"); | 405 | printf(">> signal(%s)\n", buff); |
| 431 | usleep(100); | 406 | signal(sig, suicide_signal_handler); |
| 432 | }; | 407 | }; |
| 408 | printf(">> kill(%i, %s)\n", p, buff); | ||
| 409 | kill(p, sig); | ||
| 410 | if (verbose) | ||
| 411 | printf(">> usleep(100)\n"); | ||
| 412 | usleep(100); | ||
| 433 | 413 | ||
| 434 | if (verbose) | 414 | if (verbose) |
| 435 | printf(">> return(0)\n"); | 415 | printf(">> return(0)\n"); |
| @@ -460,28 +440,14 @@ void suicide_signal_handler(int sig) | |||
| 460 | /// finds a signal from the table and returns action | 440 | /// finds a signal from the table and returns action |
| 461 | /// @param[in] str | 441 | /// @param[in] str |
| 462 | /// @param[in] val | 442 | /// @param[in] val |
| 463 | int suicide_find_data(SuicideAction *** list, size_t * count, const char * str, | 443 | int suicide_find_data(SuicideData ** datap, const char * str, |
| 464 | int val) | 444 | int val) |
| 465 | { | 445 | { |
| 466 | int i; | 446 | int i; |
| 467 | size_t offset; | 447 | size_t offset; |
| 468 | SuicideAction * action; | ||
| 469 | SuicideAction ** ptr; | ||
| 470 | 448 | ||
| 471 | if (!(ptr = realloc(*list, (sizeof(SuicideAction *)*((*count)+1))))) | 449 | if ((datap)) |
| 472 | { | 450 | *datap = NULL; |
| 473 | fprintf(stderr, PROGRAM_NAME ": out of virtual memory\n"); | ||
| 474 | return(-1); | ||
| 475 | }; | ||
| 476 | *list = ptr; | ||
| 477 | |||
| 478 | if (!(action = malloc(sizeof(SuicideAction)))) | ||
| 479 | { | ||
| 480 | fprintf(stderr, PROGRAM_NAME ": out of virtual memory\n"); | ||
| 481 | return(-1); | ||
| 482 | }; | ||
| 483 | memset(action, 0, sizeof(SuicideAction)); | ||
| 484 | action->number = -1; | ||
| 485 | 451 | ||
| 486 | if (str) | 452 | if (str) |
| 487 | { | 453 | { |
| @@ -490,31 +456,22 @@ int suicide_find_data(SuicideAction *** list, size_t * count, const char * str, | |||
| 490 | { | 456 | { |
| 491 | if (!(strcasecmp(&susv3_signals[i].name[offset], str))) | 457 | if (!(strcasecmp(&susv3_signals[i].name[offset], str))) |
| 492 | { | 458 | { |
| 493 | action->number = susv3_signals[i].number; | 459 | if ((datap)) |
| 494 | action->data = &susv3_signals[i]; | 460 | *datap = &susv3_signals[i]; |
| 495 | (*list)[(*count)] = action; | 461 | return(susv3_signals[i].number); |
| 496 | (*count)++; | ||
| 497 | return(0); | ||
| 498 | }; | 462 | }; |
| 499 | }; | 463 | }; |
| 500 | free(action); | ||
| 501 | fprintf(stderr, PROGRAM_NAME ": unknown signal\n"); | 464 | fprintf(stderr, PROGRAM_NAME ": unknown signal\n"); |
| 502 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | 465 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); |
| 503 | return(-1); | 466 | return(-1); |
| 504 | }; | 467 | }; |
| 505 | 468 | ||
| 506 | (*list)[(*count)] = action; | 469 | if ((datap)) |
| 507 | (*count)++; | ||
| 508 | |||
| 509 | if (!(str)) | ||
| 510 | { | ||
| 511 | action->number = val; | ||
| 512 | for(i = 0; susv3_signals[i].name; i++) | 470 | for(i = 0; susv3_signals[i].name; i++) |
| 513 | if (susv3_signals[i].number == val) | 471 | if (susv3_signals[i].number == val) |
| 514 | action->data = &susv3_signals[i]; | 472 | *datap = &susv3_signals[i]; |
| 515 | }; | ||
| 516 | 473 | ||
| 517 | return(0); | 474 | return(val); |
| 518 | } | 475 | } |
| 519 | 476 | ||
| 520 | 477 | ||
