summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main.c96
1 files changed, 59 insertions, 37 deletions
diff --git a/src/main.c b/src/main.c
index c1d1edf..8702a4b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -54,7 +54,8 @@
54#include "conf.h" 54#include "conf.h"
55 55
56static const char *socket_path = "/var/run/usbmuxd"; 56static const char *socket_path = "/var/run/usbmuxd";
57static const char *lockfile = "/var/run/usbmuxd.pid"; 57#define DEFAULT_LOCKFILE "/var/run/usbmuxd.pid"
58static const char *lockfile = DEFAULT_LOCKFILE;
58 59
59// Global state used in other files 60// Global state used in other files
60int should_exit; 61int should_exit;
@@ -519,6 +520,8 @@ static void usage()
519 printf(" -S, --socket ADDR:PORT | PATH Specify source ADDR and PORT or a UNIX\n"); 520 printf(" -S, --socket ADDR:PORT | PATH Specify source ADDR and PORT or a UNIX\n");
520 printf(" \t\tsocket PATH to use for the listening socket.\n"); 521 printf(" \t\tsocket PATH to use for the listening socket.\n");
521 printf(" \t\tDefault: %s\n", socket_path); 522 printf(" \t\tDefault: %s\n", socket_path);
523 printf(" -P, --pidfile PATH\tSpecify a different location for the pid file, or pass\n");
524 printf(" \t\tNONE to disable. Default: %s\n", DEFAULT_LOCKFILE);
522 printf(" -x, --exit\t\tNotify a running instance to exit if there are no devices\n"); 525 printf(" -x, --exit\t\tNotify a running instance to exit if there are no devices\n");
523 printf(" \t\tconnected (sends SIGUSR1 to running instance) and exit.\n"); 526 printf(" \t\tconnected (sends SIGUSR1 to running instance) and exit.\n");
524 printf(" -X, --force-exit\tNotify a running instance to exit even if there are still\n"); 527 printf(" -X, --force-exit\tNotify a running instance to exit even if there are still\n");
@@ -547,6 +550,7 @@ static void parse_opts(int argc, char **argv)
547 {"systemd", no_argument, NULL, 's'}, 550 {"systemd", no_argument, NULL, 's'},
548#endif 551#endif
549 {"socket", required_argument, NULL, 'S'}, 552 {"socket", required_argument, NULL, 'S'},
553 {"pidfile", required_argument, NULL, 'P'},
550 {"exit", no_argument, NULL, 'x'}, 554 {"exit", no_argument, NULL, 'x'},
551 {"force-exit", no_argument, NULL, 'X'}, 555 {"force-exit", no_argument, NULL, 'X'},
552 {"logfile", required_argument, NULL, 'l'}, 556 {"logfile", required_argument, NULL, 'l'},
@@ -556,11 +560,11 @@ static void parse_opts(int argc, char **argv)
556 int c; 560 int c;
557 561
558#ifdef HAVE_SYSTEMD 562#ifdef HAVE_SYSTEMD
559 const char* opts_spec = "hfvVuU:xXsnzl:pS:"; 563 const char* opts_spec = "hfvVuU:xXsnzl:pS:P:";
560#elif HAVE_UDEV 564#elif HAVE_UDEV
561 const char* opts_spec = "hfvVuU:xXnzl:pS:"; 565 const char* opts_spec = "hfvVuU:xXnzl:pS:P:";
562#else 566#else
563 const char* opts_spec = "hfvVU:xXnzl:pS:"; 567 const char* opts_spec = "hfvVU:xXnzl:pS:P:";
564#endif 568#endif
565 569
566 while (1) { 570 while (1) {
@@ -615,6 +619,18 @@ static void parse_opts(int argc, char **argv)
615 } 619 }
616 listen_addr = optarg; 620 listen_addr = optarg;
617 break; 621 break;
622 case 'P':
623 if (!*optarg || *optarg == '-') {
624 usbmuxd_log(LL_FATAL, "ERROR: --pidfile requires an argument");
625 usage();
626 exit(2);
627 }
628 if (!strcmp(optarg, "NONE")) {
629 lockfile = NULL;
630 } else {
631 lockfile = optarg;
632 }
633 break;
618 case 'x': 634 case 'x':
619 opt_exit = 1; 635 opt_exit = 1;
620 exit_signal = SIGUSR1; 636 exit_signal = SIGUSR1;
@@ -676,19 +692,21 @@ int main(int argc, char *argv[])
676 set_signal_handlers(); 692 set_signal_handlers();
677 signal(SIGPIPE, SIG_IGN); 693 signal(SIGPIPE, SIG_IGN);
678 694
679 res = lfd = open(lockfile, O_WRONLY|O_CREAT, 0644); 695 if (lockfile) {
680 if(res == -1) { 696 res = lfd = open(lockfile, O_WRONLY|O_CREAT, 0644);
681 usbmuxd_log(LL_FATAL, "Could not open lockfile"); 697 if(res == -1) {
682 goto terminate; 698 usbmuxd_log(LL_FATAL, "Could not open lockfile");
699 goto terminate;
700 }
701 lock.l_type = F_WRLCK;
702 lock.l_whence = SEEK_SET;
703 lock.l_start = 0;
704 lock.l_len = 0;
705 lock.l_pid = 0;
706 fcntl(lfd, F_GETLK, &lock);
707 close(lfd);
683 } 708 }
684 lock.l_type = F_WRLCK; 709 if (lockfile && lock.l_type != F_UNLCK) {
685 lock.l_whence = SEEK_SET;
686 lock.l_start = 0;
687 lock.l_len = 0;
688 lock.l_pid = 0;
689 fcntl(lfd, F_GETLK, &lock);
690 close(lfd);
691 if (lock.l_type != F_UNLCK) {
692 if (opt_exit) { 710 if (opt_exit) {
693 if (lock.l_pid && !kill(lock.l_pid, 0)) { 711 if (lock.l_pid && !kill(lock.l_pid, 0)) {
694 usbmuxd_log(LL_NOTICE, "Sending signal %d to instance with pid %d", exit_signal, lock.l_pid); 712 usbmuxd_log(LL_NOTICE, "Sending signal %d to instance with pid %d", exit_signal, lock.l_pid);
@@ -724,7 +742,9 @@ int main(int argc, char *argv[])
724 goto terminate; 742 goto terminate;
725 } 743 }
726 } 744 }
727 unlink(lockfile); 745 if (lockfile) {
746 unlink(lockfile);
747 }
728 748
729 if (opt_exit) { 749 if (opt_exit) {
730 usbmuxd_log(LL_NOTICE, "No running instance found, none killed. Exiting."); 750 usbmuxd_log(LL_NOTICE, "No running instance found, none killed. Exiting.");
@@ -739,26 +759,28 @@ int main(int argc, char *argv[])
739 } 759 }
740 } 760 }
741 761
742 // now open the lockfile and place the lock 762 if (lockfile) {
743 res = lfd = open(lockfile, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644); 763 // now open the lockfile and place the lock
744 if(res < 0) { 764 res = lfd = open(lockfile, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
745 usbmuxd_log(LL_FATAL, "Could not open lockfile"); 765 if(res < 0) {
746 goto terminate; 766 usbmuxd_log(LL_FATAL, "Could not open pidfile '%s'", lockfile);
747 } 767 goto terminate;
748 lock.l_type = F_WRLCK; 768 }
749 lock.l_whence = SEEK_SET; 769 lock.l_type = F_WRLCK;
750 lock.l_start = 0; 770 lock.l_whence = SEEK_SET;
751 lock.l_len = 0; 771 lock.l_start = 0;
752 if ((res = fcntl(lfd, F_SETLK, &lock)) < 0) { 772 lock.l_len = 0;
753 usbmuxd_log(LL_FATAL, "Lockfile locking failed!"); 773 if ((res = fcntl(lfd, F_SETLK, &lock)) < 0) {
754 goto terminate; 774 usbmuxd_log(LL_FATAL, "Locking pidfile '%s' failed!", lockfile);
755 } 775 goto terminate;
756 sprintf(pids, "%d", getpid()); 776 }
757 if ((size_t)(res = write(lfd, pids, strlen(pids))) != strlen(pids)) { 777 sprintf(pids, "%d", getpid());
758 usbmuxd_log(LL_FATAL, "Could not write pidfile!"); 778 if ((size_t)(res = write(lfd, pids, strlen(pids))) != strlen(pids)) {
759 if(res >= 0) 779 usbmuxd_log(LL_FATAL, "Could not write pidfile!");
760 res = -2; 780 if(res >= 0)
761 goto terminate; 781 res = -2;
782 goto terminate;
783 }
762 } 784 }
763 785
764 // set number of file descriptors to higher value 786 // set number of file descriptors to higher value