aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid M. Syzdek <david.syzdek@acsalaska.com>2020-02-10 12:44:05 -0900
committerDavid M. Syzdek <david.syzdek@acsalaska.com>2020-02-10 12:44:05 -0900
commitec6a997e575c4c414921ce4fce29666b61f08c43 (patch)
treefade2e92614c5be3db90424f5a68abb384736aff
parent9d24dcf7680545543e21d613f7bc1d89163175d0 (diff)
downloadakcom-udpecho-ec6a997e575c4c414921ce4fce29666b61f08c43.tar.gz
akcom-udpecho-ec6a997e575c4c414921ce4fce29666b61f08c43.tar.bz2
akcom-udpecho-ec6a997e575c4c414921ce4fce29666b61f08c43.tar.xz
akcom-udpecho-ec6a997e575c4c414921ce4fce29666b61f08c43.zip
akcom-udpechod: replacing pidfile struct member with static variable
-rw-r--r--src/akcom-udpechod.c39
1 files changed, 19 insertions, 20 deletions
diff --git a/src/akcom-udpechod.c b/src/akcom-udpechod.c
index 8012281..f3fad3b 100644
--- a/src/akcom-udpechod.c
+++ b/src/akcom-udpechod.c
@@ -148,7 +148,6 @@ static int should_stop = 0;
148 148
149struct app_config 149struct app_config
150{ 150{
151 const char * pidfile;
152 uint16_t port; // UDP port number 151 uint16_t port; // UDP port number
153 uint16_t echoplus; // enable echo plus 152 uint16_t echoplus; // enable echo plus
154 int drop_perct; // drop percentage 153 int drop_perct; // drop percentage
@@ -162,7 +161,6 @@ struct app_config
162}; 161};
163static struct app_config cnf = 162static struct app_config cnf =
164{ 163{
165 .pidfile = "/var/run/" PROGRAM_NAME ".pid",
166 .port = 30006, 164 .port = 30006,
167 .echoplus = 0, 165 .echoplus = 0,
168 .drop_perct = 0, 166 .drop_perct = 0,
@@ -175,6 +173,7 @@ static struct app_config cnf =
175 .gid = 0, 173 .gid = 0,
176}; 174};
177static const char * prog_name = "a.out"; 175static const char * prog_name = "a.out";
176static const char * cnf_pidfile = "/var/run/" PROGRAM_NAME ".pid";
178 177
179 178
180////////////////// 179//////////////////
@@ -342,7 +341,7 @@ int main(int argc, char * argv[])
342 break; 341 break;
343 342
344 case 'P': 343 case 'P':
345 cnf.pidfile = optarg; 344 cnf_pidfile = optarg;
346 break; 345 break;
347 346
348 case 'r': 347 case 'r':
@@ -434,7 +433,7 @@ int main(int argc, char * argv[])
434 // close syslog 433 // close syslog
435 syslog(LOG_NOTICE, "daemon stopping"); 434 syslog(LOG_NOTICE, "daemon stopping");
436 close(s); 435 close(s);
437 unlink(cnf.pidfile); 436 unlink(cnf_pidfile);
438 closelog(); 437 closelog();
439 438
440 return(0); 439 return(0);
@@ -466,15 +465,15 @@ int my_daemonize(void)
466 465
467 // check for existing instance 466 // check for existing instance
468 fs = NULL; 467 fs = NULL;
469 my_debug("checking for existing PID file (%s)", cnf.pidfile); 468 my_debug("checking for existing PID file (%s)", cnf_pidfile);
470 if ((rc = stat(cnf.pidfile, &sb)) == -1) 469 if ((rc = stat(cnf_pidfile, &sb)) == -1)
471 { 470 {
472 if (errno != ENOENT) 471 if (errno != ENOENT)
473 { 472 {
474 my_error("stat(): %s", strerror(errno)); 473 my_error("stat(): %s", strerror(errno));
475 return(-1); 474 return(-1);
476 }; 475 };
477 } else if ((fs = fopen(cnf.pidfile, "r")) == NULL) 476 } else if ((fs = fopen(cnf_pidfile, "r")) == NULL)
478 { 477 {
479 if (errno != ENOENT) 478 if (errno != ENOENT)
480 { 479 {
@@ -490,7 +489,7 @@ int my_daemonize(void)
490 if (errno == ESRCH) 489 if (errno == ESRCH)
491 { 490 {
492 my_debug("removing stale PID file"); 491 my_debug("removing stale PID file");
493 unlink(cnf.pidfile); 492 unlink(cnf_pidfile);
494 } else 493 } else
495 { 494 {
496 my_error("kill(): %s", strerror(errno)); 495 my_error("kill(): %s", strerror(errno));
@@ -505,7 +504,7 @@ int my_daemonize(void)
505 504
506 // create PID file 505 // create PID file
507 my_debug("creating PID file"); 506 my_debug("creating PID file");
508 strncpy(pidfile, cnf.pidfile, sizeof(pidfile)-7); 507 strncpy(pidfile, cnf_pidfile, sizeof(pidfile)-7);
509 strcat(pidfile, "XXXXXX"); 508 strcat(pidfile, "XXXXXX");
510 if ((fd = mkstemp(pidfile)) == -1) 509 if ((fd = mkstemp(pidfile)) == -1)
511 { 510 {
@@ -513,7 +512,7 @@ int my_daemonize(void)
513 return(-1); 512 return(-1);
514 }; 513 };
515 my_debug("temp pidfile: %s", pidfile); 514 my_debug("temp pidfile: %s", pidfile);
516 if ((rc = link(pidfile, cnf.pidfile)) == -1) 515 if ((rc = link(pidfile, cnf_pidfile)) == -1)
517 { 516 {
518 my_error("mkstemp(): %s", strerror(errno)); 517 my_error("mkstemp(): %s", strerror(errno));
519 close(fd); 518 close(fd);
@@ -537,7 +536,7 @@ int my_daemonize(void)
537 return(-1); 536 return(-1);
538 }; 537 };
539 }; 538 };
540 my_debug("pidfile: %s", cnf.pidfile); 539 my_debug("pidfile: %s", cnf_pidfile);
541 540
542 // determines interface on which to listen 541 // determines interface on which to listen
543 bzero(&sa, sizeof(sa)); 542 bzero(&sa, sizeof(sa));
@@ -566,7 +565,7 @@ int my_daemonize(void)
566 else 565 else
567 my_error("invalid address specified with `-l'"); 566 my_error("invalid address specified with `-l'");
568 close(fd); 567 close(fd);
569 unlink(cnf.pidfile); 568 unlink(cnf_pidfile);
570 return(-1); 569 return(-1);
571 }; 570 };
572 }; 571 };
@@ -577,7 +576,7 @@ int my_daemonize(void)
577 { 576 {
578 my_error("socket(): %s", strerror(errno)); 577 my_error("socket(): %s", strerror(errno));
579 close(fd); 578 close(fd);
580 unlink(cnf.pidfile); 579 unlink(cnf_pidfile);
581 return(-1); 580 return(-1);
582 }; 581 };
583 582
@@ -589,7 +588,7 @@ int my_daemonize(void)
589 my_error("setsockopt(SO_REUSEADDR): %s", strerror(errno)); 588 my_error("setsockopt(SO_REUSEADDR): %s", strerror(errno));
590 close(s); 589 close(s);
591 close(fd); 590 close(fd);
592 unlink(cnf.pidfile); 591 unlink(cnf_pidfile);
593 return(-1); 592 return(-1);
594 }; 593 };
595 594
@@ -600,7 +599,7 @@ int my_daemonize(void)
600 my_error("bind(): %s", strerror(errno)); 599 my_error("bind(): %s", strerror(errno));
601 close(s); 600 close(s);
602 close(fd); 601 close(fd);
603 unlink(cnf.pidfile); 602 unlink(cnf_pidfile);
604 return(-1); 603 return(-1);
605 }; 604 };
606 605
@@ -611,7 +610,7 @@ int my_daemonize(void)
611 my_error("getsockname(): %s", strerror(errno)); 610 my_error("getsockname(): %s", strerror(errno));
612 close(s); 611 close(s);
613 close(fd); 612 close(fd);
614 unlink(cnf.pidfile); 613 unlink(cnf_pidfile);
615 return(-1); 614 return(-1);
616 }; 615 };
617 switch(sa.ss.ss_family) 616 switch(sa.ss.ss_family)
@@ -630,7 +629,7 @@ int my_daemonize(void)
630 my_error("listening socket has invalid address family: %i\n", sa.sa.sa_family); 629 my_error("listening socket has invalid address family: %i\n", sa.sa.sa_family);
631 close(s); 630 close(s);
632 close(fd); 631 close(fd);
633 unlink(cnf.pidfile); 632 unlink(cnf_pidfile);
634 return(-1); 633 return(-1);
635 }; 634 };
636 635
@@ -640,7 +639,7 @@ int my_daemonize(void)
640 my_error("getgid(): %s", strerror(errno)); 639 my_error("getgid(): %s", strerror(errno));
641 close(s); 640 close(s);
642 close(fd); 641 close(fd);
643 unlink(cnf.pidfile); 642 unlink(cnf_pidfile);
644 return(-1); 643 return(-1);
645 }; 644 };
646 if ( (getuid() != cnf.uid) && ((rc = setreuid(cnf.uid, cnf.uid)) == -1) ) 645 if ( (getuid() != cnf.uid) && ((rc = setreuid(cnf.uid, cnf.uid)) == -1) )
@@ -648,7 +647,7 @@ int my_daemonize(void)
648 my_error("getuid(): %s", strerror(errno)); 647 my_error("getuid(): %s", strerror(errno));
649 close(s); 648 close(s);
650 close(fd); 649 close(fd);
651 unlink(cnf.pidfile); 650 unlink(cnf_pidfile);
652 return(-1); 651 return(-1);
653 }; 652 };
654 653
@@ -923,7 +922,7 @@ void my_usage(void)
923 printf(" -l addr, --listen=addr bind to IP address (default: all)\n"); 922 printf(" -l addr, --listen=addr bind to IP address (default: all)\n");
924 printf(" -n, --foreground do not fork\n"); 923 printf(" -n, --foreground do not fork\n");
925 printf(" -p port, --port=port list on port number (default: %u)\n", cnf.port); 924 printf(" -p port, --port=port list on port number (default: %u)\n", cnf.port);
926 printf(" -P file, --pidfile=file PID file (default: %s)\n", cnf.pidfile); 925 printf(" -P file, --pidfile=file PID file (default: %s)\n", cnf_pidfile);
927 printf(" -r, --rfc RFC compliant echo protocol%s\n", (!(cnf.echoplus)) ? " (default)" : ""); 926 printf(" -r, --rfc RFC compliant echo protocol%s\n", (!(cnf.echoplus)) ? " (default)" : "");
928 printf(" -u uid, --user=uid setuid to uid (default: none)\n"); 927 printf(" -u uid, --user=uid setuid to uid (default: none)\n");
929 printf(" -v, --verbose enable verbose output\n"); 928 printf(" -v, --verbose enable verbose output\n");