diff options
| author | David M. Syzdek <david.syzdek@acsalaska.com> | 2020-01-30 21:24:58 -0900 |
|---|---|---|
| committer | David M. Syzdek <david.syzdek@acsalaska.com> | 2020-01-30 21:24:58 -0900 |
| commit | 25ac7830bcfcc8e162424ecf9bfc4ce2bc0b7b17 (patch) | |
| tree | 768880bb3ca447731c95f31d93fe7d932d9c216c | |
| parent | 952daf64606da4fc44d88346efaf12474f373c07 (diff) | |
| download | akcom-udpecho-25ac7830bcfcc8e162424ecf9bfc4ce2bc0b7b17.tar.gz akcom-udpecho-25ac7830bcfcc8e162424ecf9bfc4ce2bc0b7b17.tar.bz2 akcom-udpecho-25ac7830bcfcc8e162424ecf9bfc4ce2bc0b7b17.tar.xz akcom-udpecho-25ac7830bcfcc8e162424ecf9bfc4ce2bc0b7b17.zip | |
updating verbose output
| -rw-r--r-- | src/akcom-udpechod.c | 171 |
1 files changed, 100 insertions, 71 deletions
diff --git a/src/akcom-udpechod.c b/src/akcom-udpechod.c index 30845a6..af34ae8 100644 --- a/src/akcom-udpechod.c +++ b/src/akcom-udpechod.c | |||
| @@ -187,6 +187,12 @@ int main(int argc, char * argv[]); | |||
| 187 | // daemonize process | 187 | // daemonize process |
| 188 | int my_daemonize(void); | 188 | int my_daemonize(void); |
| 189 | 189 | ||
| 190 | // display debug message | ||
| 191 | void my_debug(const char * fmt, ...); | ||
| 192 | |||
| 193 | // display error message | ||
| 194 | void my_error(const char * fmt, ...); | ||
| 195 | |||
| 190 | // log connection | 196 | // log connection |
| 191 | int my_log_conn(int mode, uint64_t * connp, union my_sa * sap, | 197 | int my_log_conn(int mode, uint64_t * connp, union my_sa * sap, |
| 192 | struct udp_echo_plus * msgp, ssize_t ssize, struct timespec * tsp, | 198 | struct udp_echo_plus * msgp, ssize_t ssize, struct timespec * tsp, |
| @@ -217,7 +223,6 @@ int main(int argc, char * argv[]) | |||
| 217 | { | 223 | { |
| 218 | char * ptr; | 224 | char * ptr; |
| 219 | int c; | 225 | int c; |
| 220 | int opts; | ||
| 221 | int s; | 226 | int s; |
| 222 | unsigned seed; | 227 | unsigned seed; |
| 223 | struct timespec ts; | 228 | struct timespec ts; |
| @@ -327,8 +332,6 @@ int main(int argc, char * argv[]) | |||
| 327 | 332 | ||
| 328 | case 'n': | 333 | case 'n': |
| 329 | cnf.dont_fork = 1; | 334 | cnf.dont_fork = 1; |
| 330 | if (!(cnf.verbose)) | ||
| 331 | cnf.verbose++; | ||
| 332 | break; | 335 | break; |
| 333 | 336 | ||
| 334 | case 'p': | 337 | case 'p': |
| @@ -388,18 +391,8 @@ int main(int argc, char * argv[]) | |||
| 388 | return(1); | 391 | return(1); |
| 389 | }; | 392 | }; |
| 390 | 393 | ||
| 391 | // open syslog | ||
| 392 | opts = LOG_PID; | ||
| 393 | if ((cnf.verbose)) | ||
| 394 | opts |= LOG_PERROR; | ||
| 395 | openlog(cnf.prog_name, opts, cnf.facility); | ||
| 396 | syslog(LOG_NOTICE, "%s v%s starting", PROGRAM_NAME, PACKAGE_VERSION); | ||
| 397 | syslog(LOG_NOTICE, "echo plus enabled: %s", ((cnf.echoplus)) ? "yes" : "no"); | ||
| 398 | syslog(LOG_NOTICE, "random delay: %u ms", cnf.delay); | ||
| 399 | syslog(LOG_NOTICE, "drop probability: %u%%", cnf.drop_perct); | ||
| 400 | |||
| 401 | // configure signals | 394 | // configure signals |
| 402 | syslog(LOG_DEBUG, "configuring signal handling"); | 395 | my_debug("configuring signal handling"); |
| 403 | signal(SIGHUP, SIG_IGN); | 396 | signal(SIGHUP, SIG_IGN); |
| 404 | signal(SIGPIPE, SIG_IGN); | 397 | signal(SIGPIPE, SIG_IGN); |
| 405 | signal(SIGINT, my_sighandler); | 398 | signal(SIGINT, my_sighandler); |
| @@ -407,7 +400,7 @@ int main(int argc, char * argv[]) | |||
| 407 | signal(SIGTERM, my_sighandler); | 400 | signal(SIGTERM, my_sighandler); |
| 408 | 401 | ||
| 409 | // seed psuedo random number generator | 402 | // seed psuedo random number generator |
| 410 | syslog(LOG_DEBUG, "seeding psuedo random number generator"); | 403 | my_debug("seeding psuedo random number generator"); |
| 411 | clock_gettime(CLOCK_REALTIME, &ts); | 404 | clock_gettime(CLOCK_REALTIME, &ts); |
| 412 | seed = (int)ts.tv_sec; | 405 | seed = (int)ts.tv_sec; |
| 413 | seed += (int)ts.tv_nsec; | 406 | seed += (int)ts.tv_nsec; |
| @@ -455,9 +448,9 @@ int my_daemonize(void) | |||
| 455 | int opt; | 448 | int opt; |
| 456 | socklen_t socklen; | 449 | socklen_t socklen; |
| 457 | char pidfile[512]; | 450 | char pidfile[512]; |
| 458 | char buff[512]; | 451 | char buff[16]; |
| 452 | char addr_str[512]; | ||
| 459 | pid_t pid; | 453 | pid_t pid; |
| 460 | pid_t oldpid; | ||
| 461 | short port; | 454 | short port; |
| 462 | FILE * fs; | 455 | FILE * fs; |
| 463 | struct stat sb; | 456 | struct stat sb; |
| @@ -471,19 +464,19 @@ int my_daemonize(void) | |||
| 471 | 464 | ||
| 472 | // check for existing instance | 465 | // check for existing instance |
| 473 | fs = NULL; | 466 | fs = NULL; |
| 474 | syslog(LOG_DEBUG, "checking for existing PID file (%s)", cnf.pidfile); | 467 | my_debug("checking for existing PID file (%s)", cnf.pidfile); |
| 475 | if ((rc = stat(cnf.pidfile, &sb)) == -1) | 468 | if ((rc = stat(cnf.pidfile, &sb)) == -1) |
| 476 | { | 469 | { |
| 477 | if (errno != ENOENT) | 470 | if (errno != ENOENT) |
| 478 | { | 471 | { |
| 479 | syslog(LOG_ERR, "error: stat(): %s", strerror(errno)); | 472 | my_error("stat(): %s", strerror(errno)); |
| 480 | return(-1); | 473 | return(-1); |
| 481 | }; | 474 | }; |
| 482 | } else if ((fs = fopen(cnf.pidfile, "r")) == NULL) | 475 | } else if ((fs = fopen(cnf.pidfile, "r")) == NULL) |
| 483 | { | 476 | { |
| 484 | if (errno != ENOENT) | 477 | if (errno != ENOENT) |
| 485 | { | 478 | { |
| 486 | syslog(LOG_ERR, "error: fopen(): %s", strerror(errno)); | 479 | my_error("fopen(): %s", strerror(errno)); |
| 487 | return(-1); | 480 | return(-1); |
| 488 | }; | 481 | }; |
| 489 | } else | 482 | } else |
| @@ -494,33 +487,33 @@ int my_daemonize(void) | |||
| 494 | { | 487 | { |
| 495 | if (errno == ESRCH) | 488 | if (errno == ESRCH) |
| 496 | { | 489 | { |
| 497 | syslog(LOG_DEBUG, "removing stale PID file"); | 490 | my_debug("removing stale PID file"); |
| 498 | unlink(cnf.pidfile); | 491 | unlink(cnf.pidfile); |
| 499 | } else | 492 | } else |
| 500 | { | 493 | { |
| 501 | syslog(LOG_ERR, "error: kill(): %s", strerror(errno)); | 494 | my_error("kill(): %s", strerror(errno)); |
| 502 | return(-1); | 495 | return(-1); |
| 503 | }; | 496 | }; |
| 504 | } else | 497 | } else |
| 505 | { | 498 | { |
| 506 | syslog(LOG_ERR, "daemon already running"); | 499 | my_error("daemon already running"); |
| 507 | return(-1); | 500 | return(-1); |
| 508 | }; | 501 | }; |
| 509 | }; | 502 | }; |
| 510 | 503 | ||
| 511 | // create PID file | 504 | // create PID file |
| 512 | syslog(LOG_DEBUG, "creating PID file"); | 505 | my_debug("creating PID file"); |
| 513 | strncpy(pidfile, cnf.pidfile, sizeof(pidfile)-7); | 506 | strncpy(pidfile, cnf.pidfile, sizeof(pidfile)-7); |
| 514 | strcat(pidfile, "XXXXXX"); | 507 | strcat(pidfile, "XXXXXX"); |
| 515 | if ((fd = mkstemp(pidfile)) == -1) | 508 | if ((fd = mkstemp(pidfile)) == -1) |
| 516 | { | 509 | { |
| 517 | syslog(LOG_ERR, "error: mkstemp(): %s", strerror(errno)); | 510 | my_error("mkstemp(): %s", strerror(errno)); |
| 518 | return(-1); | 511 | return(-1); |
| 519 | }; | 512 | }; |
| 520 | syslog(LOG_DEBUG, "temp pidfile: %s", pidfile); | 513 | my_debug("temp pidfile: %s", pidfile); |
| 521 | if ((rc = link(pidfile, cnf.pidfile)) == -1) | 514 | if ((rc = link(pidfile, cnf.pidfile)) == -1) |
| 522 | { | 515 | { |
| 523 | syslog(LOG_ERR, "error: mkstemp(): %s", strerror(errno)); | 516 | my_error("mkstemp(): %s", strerror(errno)); |
| 524 | close(fd); | 517 | close(fd); |
| 525 | unlink(pidfile); | 518 | unlink(pidfile); |
| 526 | return(-1); | 519 | return(-1); |
| @@ -528,7 +521,7 @@ int my_daemonize(void) | |||
| 528 | unlink(pidfile); | 521 | unlink(pidfile); |
| 529 | if ((rc = fchmod(fd, 0644)) == -1) | 522 | if ((rc = fchmod(fd, 0644)) == -1) |
| 530 | { | 523 | { |
| 531 | syslog(LOG_ERR, "error: fchmod(): %s", strerror(errno)); | 524 | my_error("fchmod(): %s", strerror(errno)); |
| 532 | close(fd); | 525 | close(fd); |
| 533 | return(-1); | 526 | return(-1); |
| 534 | }; | 527 | }; |
| @@ -537,12 +530,12 @@ int my_daemonize(void) | |||
| 537 | { | 530 | { |
| 538 | if ((rc = fchown(fd, cnf.uid, cnf.gid)) == -1) | 531 | if ((rc = fchown(fd, cnf.uid, cnf.gid)) == -1) |
| 539 | { | 532 | { |
| 540 | syslog(LOG_ERR, "error: fchown(): %s", strerror(errno)); | 533 | my_error("fchown(): %s", strerror(errno)); |
| 541 | close(fd); | 534 | close(fd); |
| 542 | return(-1); | 535 | return(-1); |
| 543 | }; | 536 | }; |
| 544 | }; | 537 | }; |
| 545 | syslog(LOG_DEBUG, "pidfile: %s", cnf.pidfile); | 538 | my_debug("pidfile: %s", cnf.pidfile); |
| 546 | 539 | ||
| 547 | // determines interface on which to listen | 540 | // determines interface on which to listen |
| 548 | bzero(&sa, sizeof(sa)); | 541 | bzero(&sa, sizeof(sa)); |
| @@ -567,9 +560,9 @@ int my_daemonize(void) | |||
| 567 | } else | 560 | } else |
| 568 | { | 561 | { |
| 569 | if (rc == -1) | 562 | if (rc == -1) |
| 570 | syslog(LOG_ERR, "error: inet_pton(): %s", strerror(errno)); | 563 | my_error("inet_pton(): %s", strerror(errno)); |
| 571 | else | 564 | else |
| 572 | syslog(LOG_ERR, "error: invalid address specified with `-l'"); | 565 | my_error("invalid address specified with `-l'"); |
| 573 | close(fd); | 566 | close(fd); |
| 574 | unlink(cnf.pidfile); | 567 | unlink(cnf.pidfile); |
| 575 | return(-1); | 568 | return(-1); |
| @@ -577,21 +570,21 @@ int my_daemonize(void) | |||
| 577 | }; | 570 | }; |
| 578 | 571 | ||
| 579 | // creates socket | 572 | // creates socket |
| 580 | syslog(LOG_DEBUG, "creating UDP socket"); | 573 | my_debug("creating UDP socket"); |
| 581 | if ((s = socket(sa.sa.sa_family, SOCK_DGRAM, 0)) == -1) | 574 | if ((s = socket(sa.sa.sa_family, SOCK_DGRAM, 0)) == -1) |
| 582 | { | 575 | { |
| 583 | syslog(LOG_ERR, "socket error: %s", strerror(errno)); | 576 | my_error("socket(): %s", strerror(errno)); |
| 584 | close(fd); | 577 | close(fd); |
| 585 | unlink(cnf.pidfile); | 578 | unlink(cnf.pidfile); |
| 586 | return(-1); | 579 | return(-1); |
| 587 | }; | 580 | }; |
| 588 | 581 | ||
| 589 | // set socket options | 582 | // set socket options |
| 590 | syslog(LOG_DEBUG, "setting socket options"); | 583 | my_debug("setting socket options"); |
| 591 | opt = 1; | 584 | opt = 1; |
| 592 | if ((rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(int))) == -1) | 585 | if ((rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(int))) == -1) |
| 593 | { | 586 | { |
| 594 | syslog(LOG_ERR, "error: setsockopt(SO_REUSEADDR): %s", strerror(errno)); | 587 | my_error("setsockopt(SO_REUSEADDR): %s", strerror(errno)); |
| 595 | close(s); | 588 | close(s); |
| 596 | close(fd); | 589 | close(fd); |
| 597 | unlink(cnf.pidfile); | 590 | unlink(cnf.pidfile); |
| @@ -599,10 +592,10 @@ int my_daemonize(void) | |||
| 599 | }; | 592 | }; |
| 600 | 593 | ||
| 601 | // bind socket to interface | 594 | // bind socket to interface |
| 602 | syslog(LOG_DEBUG, "binding socket"); | 595 | my_debug("binding socket"); |
| 603 | if ((rc = bind(s, &sa.sa, socklen)) == -1) | 596 | if ((rc = bind(s, &sa.sa, socklen)) == -1) |
| 604 | { | 597 | { |
| 605 | syslog(LOG_ERR, "error: bind(): %s", strerror(errno)); | 598 | my_error("bind(): %s", strerror(errno)); |
| 606 | close(s); | 599 | close(s); |
| 607 | close(fd); | 600 | close(fd); |
| 608 | unlink(cnf.pidfile); | 601 | unlink(cnf.pidfile); |
| @@ -613,7 +606,7 @@ int my_daemonize(void) | |||
| 613 | socklen = sizeof(struct sockaddr_storage); | 606 | socklen = sizeof(struct sockaddr_storage); |
| 614 | if ((rc = getsockname(s, &sa.sa, &socklen)) == -1) | 607 | if ((rc = getsockname(s, &sa.sa, &socklen)) == -1) |
| 615 | { | 608 | { |
| 616 | syslog(LOG_ERR, "error: getsockname(): %s", strerror(errno)); | 609 | my_error("getsockname(): %s", strerror(errno)); |
| 617 | close(s); | 610 | close(s); |
| 618 | close(fd); | 611 | close(fd); |
| 619 | unlink(cnf.pidfile); | 612 | unlink(cnf.pidfile); |
| @@ -622,28 +615,27 @@ int my_daemonize(void) | |||
| 622 | switch(sa.ss.ss_family) | 615 | switch(sa.ss.ss_family) |
| 623 | { | 616 | { |
| 624 | case AF_INET: | 617 | case AF_INET: |
| 625 | inet_ntop(sa.sin.sin_family, &sa.sin.sin_addr, buff, sizeof(buff)); | 618 | inet_ntop(sa.sin.sin_family, &sa.sin.sin_addr, addr_str, sizeof(addr_str)); |
| 626 | port = ntohs(sa.sin.sin_port); | 619 | port = ntohs(sa.sin.sin_port); |
| 627 | break; | 620 | break; |
| 628 | 621 | ||
| 629 | case AF_INET6: | 622 | case AF_INET6: |
| 630 | inet_ntop(sa.sin6.sin6_family, &sa.sin6.sin6_addr, buff, sizeof(buff)); | 623 | inet_ntop(sa.sin6.sin6_family, &sa.sin6.sin6_addr, addr_str, sizeof(addr_str)); |
| 631 | port = ntohs(sa.sin6.sin6_port); | 624 | port = ntohs(sa.sin6.sin6_port); |
| 632 | break; | 625 | break; |
| 633 | 626 | ||
| 634 | default: | 627 | default: |
| 635 | syslog(LOG_ERR, "listening socket has invalid address family: %i\n", sa.sa.sa_family); | 628 | my_error("listening socket has invalid address family: %i\n", sa.sa.sa_family); |
| 636 | close(s); | 629 | close(s); |
| 637 | close(fd); | 630 | close(fd); |
| 638 | unlink(cnf.pidfile); | 631 | unlink(cnf.pidfile); |
| 639 | return(-1); | 632 | return(-1); |
| 640 | }; | 633 | }; |
| 641 | syslog(LOG_INFO, "listening on [%s]:%hu", buff, port); | ||
| 642 | 634 | ||
| 643 | // change ownership | 635 | // change ownership |
| 644 | if ( (getgid() != cnf.gid) && ((rc = setregid(cnf.gid, cnf.gid)) == -1) ) | 636 | if ( (getgid() != cnf.gid) && ((rc = setregid(cnf.gid, cnf.gid)) == -1) ) |
| 645 | { | 637 | { |
| 646 | syslog(LOG_ERR, "error: getgid(): %s", strerror(errno)); | 638 | my_error("getgid(): %s", strerror(errno)); |
| 647 | close(s); | 639 | close(s); |
| 648 | close(fd); | 640 | close(fd); |
| 649 | unlink(cnf.pidfile); | 641 | unlink(cnf.pidfile); |
| @@ -651,7 +643,7 @@ int my_daemonize(void) | |||
| 651 | }; | 643 | }; |
| 652 | if ( (getuid() != cnf.uid) && ((rc = setreuid(cnf.uid, cnf.uid)) == -1) ) | 644 | if ( (getuid() != cnf.uid) && ((rc = setreuid(cnf.uid, cnf.uid)) == -1) ) |
| 653 | { | 645 | { |
| 654 | syslog(LOG_ERR, "error: getuid(): %s", strerror(errno)); | 646 | my_error("getuid(): %s", strerror(errno)); |
| 655 | close(s); | 647 | close(s); |
| 656 | close(fd); | 648 | close(fd); |
| 657 | unlink(cnf.pidfile); | 649 | unlink(cnf.pidfile); |
| @@ -659,46 +651,83 @@ int my_daemonize(void) | |||
| 659 | }; | 651 | }; |
| 660 | 652 | ||
| 661 | // fork process | 653 | // fork process |
| 662 | if ((cnf.dont_fork)) | 654 | if (!(cnf.dont_fork)) |
| 663 | { | 655 | { |
| 664 | syslog(LOG_DEBUG, "writing PID to pidfile"); | 656 | my_debug("forking process"); |
| 665 | snprintf(buff, sizeof(buff), "%i", getpid()); | 657 | switch(pid = fork()) |
| 666 | write(fd, buff, strlen(buff)); | 658 | { |
| 667 | close(fd); | 659 | case -1: |
| 668 | return(s); | 660 | my_error("fork(): %s", strerror(errno)); |
| 669 | }; | 661 | break; |
| 670 | syslog(LOG_DEBUG, "forking process"); | ||
| 671 | oldpid = getpid(); | ||
| 672 | switch(pid = fork()) | ||
| 673 | { | ||
| 674 | case -1: | ||
| 675 | syslog(LOG_ERR, "error: fork(): %s", strerror(errno)); | ||
| 676 | break; | ||
| 677 | 662 | ||
| 678 | case 0: | 663 | case 0: |
| 679 | closelog(); | 664 | break; |
| 680 | openlog(cnf.prog_name, LOG_PID, cnf.facility); | ||
| 681 | syslog(LOG_INFO, "forked from %i", oldpid); | ||
| 682 | break; | ||
| 683 | 665 | ||
| 684 | default: | 666 | default: |
| 685 | syslog(LOG_INFO, "forking to %i", pid); | 667 | my_debug("forking to %i", pid); |
| 686 | closelog(); | 668 | closelog(); |
| 687 | close(fd); | 669 | close(fd); |
| 688 | close(s); | 670 | close(s); |
| 689 | return(0); | 671 | return(0); |
| 672 | }; | ||
| 690 | }; | 673 | }; |
| 691 | 674 | ||
| 692 | // record PID in pidfile | 675 | // record PID in pidfile |
| 693 | syslog(LOG_DEBUG, "writing forked PID to pidfile"); | ||
| 694 | snprintf(buff, sizeof(buff), "%i", pid); | 676 | snprintf(buff, sizeof(buff), "%i", pid); |
| 695 | write(fd, buff, strlen(buff)); | 677 | write(fd, buff, strlen(buff)); |
| 696 | close(fd); | 678 | close(fd); |
| 697 | 679 | ||
| 680 | // opens syslog | ||
| 681 | openlog(cnf.prog_name, LOG_PID | (((cnf.dont_fork)) ? LOG_PERROR : 0), cnf.facility); | ||
| 682 | syslog(LOG_NOTICE, "%s v%s", PROGRAM_NAME, PACKAGE_VERSION); | ||
| 683 | syslog(LOG_NOTICE, "echo plus enabled: %s", ((cnf.echoplus)) ? "yes" : "no"); | ||
| 684 | syslog(LOG_NOTICE, "random delay: %u ms", cnf.delay); | ||
| 685 | syslog(LOG_NOTICE, "drop probability: %u%%", cnf.drop_perct); | ||
| 686 | syslog(LOG_NOTICE, "running as UID: %u", getuid()); | ||
| 687 | syslog(LOG_NOTICE, "running as GID: %u", getgid()); | ||
| 688 | syslog(LOG_NOTICE, "listening on [%s]:%hu", addr_str, port); | ||
| 689 | |||
| 698 | return(s); | 690 | return(s); |
| 699 | } | 691 | } |
| 700 | 692 | ||
| 701 | 693 | ||
| 694 | // display debug message | ||
| 695 | void my_debug(const char * fmt, ...) | ||
| 696 | { | ||
| 697 | va_list args; | ||
| 698 | |||
| 699 | if (!(cnf.verbose)) | ||
| 700 | return; | ||
| 701 | |||
| 702 | printf("%s: ", cnf.prog_name); | ||
| 703 | |||
| 704 | va_start(args, fmt); | ||
| 705 | vprintf(fmt, args); | ||
| 706 | va_end(args); | ||
| 707 | |||
| 708 | printf("\n"); | ||
| 709 | |||
| 710 | return; | ||
| 711 | } | ||
| 712 | |||
| 713 | |||
| 714 | // display error message | ||
| 715 | void my_error(const char * fmt, ...) | ||
| 716 | { | ||
| 717 | va_list args; | ||
| 718 | |||
| 719 | fprintf(stderr, "%s: error: ", cnf.prog_name); | ||
| 720 | |||
| 721 | va_start(args, fmt); | ||
| 722 | vfprintf(stderr, fmt, args); | ||
| 723 | va_end(args); | ||
| 724 | |||
| 725 | fprintf(stderr, "\n"); | ||
| 726 | |||
| 727 | return; | ||
| 728 | } | ||
| 729 | |||
| 730 | |||
| 702 | // log connection | 731 | // log connection |
| 703 | int my_log_conn(int mode, uint64_t * connp, union my_sa * sap, | 732 | int my_log_conn(int mode, uint64_t * connp, union my_sa * sap, |
| 704 | struct udp_echo_plus * msgp, ssize_t ssize, struct timespec * tsp, | 733 | struct udp_echo_plus * msgp, ssize_t ssize, struct timespec * tsp, |
