summaryrefslogtreecommitdiffstats
path: root/daemon/main.c
diff options
context:
space:
mode:
authorGravatar Hector Martin2009-10-10 16:01:26 +0200
committerGravatar Hector Martin2009-10-10 16:01:26 +0200
commite855c861acde634662957131bbfb367260f8daf0 (patch)
tree4319250c5429d7ab20eccabc47b4b0893164986b /daemon/main.c
parent94033a862cb907c24cb2aaed1d4f1fb75652bfcd (diff)
downloadusbmuxd-e855c861acde634662957131bbfb367260f8daf0.tar.gz
usbmuxd-e855c861acde634662957131bbfb367260f8daf0.tar.bz2
Disable polling in udev mode, use udev to signal device discovery
Diffstat (limited to 'daemon/main.c')
-rw-r--r--daemon/main.c58
1 files changed, 43 insertions, 15 deletions
diff --git a/daemon/main.c b/daemon/main.c
index ef98e75..893f26d 100644
--- a/daemon/main.c
+++ b/daemon/main.c
@@ -48,6 +48,7 @@ static const char *socket_path = "/var/run/usbmuxd";
48static const char *lockfile = "/var/run/usbmuxd.pid"; 48static const char *lockfile = "/var/run/usbmuxd.pid";
49 49
50int should_exit; 50int should_exit;
51int should_discover;
51 52
52static int verbose = 0; 53static int verbose = 0;
53static int foreground = 0; 54static int foreground = 0;
@@ -96,21 +97,26 @@ int create_socket(void) {
96 97
97void handle_signal(int sig) 98void handle_signal(int sig)
98{ 99{
99 if (sig != SIGUSR1) { 100 if (sig != SIGUSR1 && sig != SIGUSR2) {
100 usbmuxd_log(LL_NOTICE,"Caught signal %d, exiting", sig); 101 usbmuxd_log(LL_NOTICE,"Caught signal %d, exiting", sig);
101 should_exit = 1; 102 should_exit = 1;
102 } else { 103 } else {
103 if(opt_udev) { 104 if(opt_udev) {
104 usbmuxd_log(LL_INFO, "Caught SIGUSR1, checking if we can terminate (no more devices attached)..."); 105 if (sig == SIGUSR1) {
105 if (device_get_count() > 0) { 106 usbmuxd_log(LL_INFO, "Caught SIGUSR1, checking if we can terminate (no more devices attached)...");
106 // we can't quit, there are still devices attached. 107 if (device_get_count() > 0) {
107 usbmuxd_log(LL_NOTICE, "Refusing to terminate, there are still devices attached. Kill me with signal 15 (TERM) to force quit."); 108 // we can't quit, there are still devices attached.
108 } else { 109 usbmuxd_log(LL_NOTICE, "Refusing to terminate, there are still devices attached. Kill me with signal 15 (TERM) to force quit.");
109 // it's safe to quit 110 } else {
110 should_exit = 1; 111 // it's safe to quit
112 should_exit = 1;
113 }
114 } else if (sig == SIGUSR2) {
115 usbmuxd_log(LL_INFO, "Caught SIGUSR2, scheduling device discovery");
116 should_discover = 1;
111 } 117 }
112 } else { 118 } else {
113 usbmuxd_log(LL_INFO, "Caught SIGUSR1 but we weren't started in --udev mode, ignoring"); 119 usbmuxd_log(LL_INFO, "Caught SIGUSR1/2 but we weren't started in --udev mode, ignoring");
114 } 120 }
115 } 121 }
116} 122}
@@ -124,6 +130,7 @@ void set_signal_handlers(void)
124 sigaction(SIGQUIT, &sa, NULL); 130 sigaction(SIGQUIT, &sa, NULL);
125 sigaction(SIGTERM, &sa, NULL); 131 sigaction(SIGTERM, &sa, NULL);
126 sigaction(SIGUSR1, &sa, NULL); 132 sigaction(SIGUSR1, &sa, NULL);
133 sigaction(SIGUSR2, &sa, NULL);
127} 134}
128 135
129int main_loop(int listenfd) 136int main_loop(int listenfd)
@@ -150,10 +157,17 @@ int main_loop(int listenfd)
150 usbmuxd_log(LL_FLOOD, "poll() returned %d", cnt); 157 usbmuxd_log(LL_FLOOD, "poll() returned %d", cnt);
151 158
152 if(cnt == -1) { 159 if(cnt == -1) {
153 if(errno == EINTR && should_exit) { 160 if(errno == EINTR) {
154 usbmuxd_log(LL_INFO, "event processing interrupted"); 161 if(should_exit) {
155 fdlist_free(&pollfds); 162 usbmuxd_log(LL_INFO, "event processing interrupted");
156 return 0; 163 fdlist_free(&pollfds);
164 return 0;
165 }
166 if(should_discover) {
167 should_discover = 0;
168 usbmuxd_log(LL_INFO, "device discovery triggered by udev");
169 usb_discover();
170 }
157 } 171 }
158 } else if(cnt == 0) { 172 } else if(cnt == 0) {
159 if(usb_process() < 0) { 173 if(usb_process() < 0) {
@@ -391,6 +405,7 @@ int main(int argc, char *argv[])
391 405
392 usbmuxd_log(LL_NOTICE, "usbmux v%s starting up", USBMUXD_VERSION); 406 usbmuxd_log(LL_NOTICE, "usbmux v%s starting up", USBMUXD_VERSION);
393 should_exit = 0; 407 should_exit = 0;
408 should_discover = 0;
394 409
395 set_signal_handlers(); 410 set_signal_handlers();
396 411
@@ -425,8 +440,18 @@ int main(int argc, char *argv[])
425 usbmuxd_log(LL_ERROR, "Another instance is already running (pid %d). exiting.", lock.l_pid); 440 usbmuxd_log(LL_ERROR, "Another instance is already running (pid %d). exiting.", lock.l_pid);
426 res = -1; 441 res = -1;
427 } else { 442 } else {
428 usbmuxd_log(LL_NOTICE, "Another instance is already running (pid %d). exiting.", lock.l_pid); 443 usbmuxd_log(LL_NOTICE, "Another instance is already running (pid %d). Telling it to check for devices.", lock.l_pid);
429 res = 0; 444 if (lock.l_pid && !kill(lock.l_pid, 0)) {
445 usbmuxd_log(LL_NOTICE, "Sending signal SIGUSR2 to instance with pid %d", lock.l_pid);
446 res = 0;
447 if (kill(lock.l_pid, SIGUSR2) < 0) {
448 usbmuxd_log(LL_FATAL, "Could not deliver SIGUSR2 to pid %d", lock.l_pid);
449 res = -1;
450 }
451 } else {
452 usbmuxd_log(LL_ERROR, "Could not determine pid of the other running instance!");
453 res = -1;
454 }
430 } 455 }
431 goto terminate; 456 goto terminate;
432 } 457 }
@@ -523,6 +548,9 @@ int main(int argc, char *argv[])
523 if((res = notify_parent(0)) < 0) 548 if((res = notify_parent(0)) < 0)
524 goto terminate; 549 goto terminate;
525 550
551 if(opt_udev)
552 usb_autodiscover(0); // discovery triggered by udev
553
526 res = main_loop(listenfd); 554 res = main_loop(listenfd);
527 if(res < 0) 555 if(res < 0)
528 usbmuxd_log(LL_FATAL, "main_loop failed"); 556 usbmuxd_log(LL_FATAL, "main_loop failed");