summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--daemon/main.c58
-rw-r--r--daemon/usb-linux.c12
-rw-r--r--daemon/usb.h2
3 files changed, 56 insertions, 16 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");
diff --git a/daemon/usb-linux.c b/daemon/usb-linux.c
index 0edc557..1f70f46 100644
--- a/daemon/usb-linux.c
+++ b/daemon/usb-linux.c
@@ -53,6 +53,7 @@ static struct collection device_list;
53static struct timeval next_dev_poll_time; 53static struct timeval next_dev_poll_time;
54 54
55static int devlist_failures; 55static int devlist_failures;
56static int device_polling;
56 57
57static void usb_disconnect(struct usb_device *dev) 58static void usb_disconnect(struct usb_device *dev)
58{ 59{
@@ -214,7 +215,7 @@ static int start_rx(struct usb_device *dev)
214 return 0; 215 return 0;
215} 216}
216 217
217static int usb_discover(void) 218int usb_discover(void)
218{ 219{
219 int cnt, i, res; 220 int cnt, i, res;
220 int valid_count = 0; 221 int valid_count = 0;
@@ -393,10 +394,18 @@ void usb_get_fds(struct fdlist *list)
393 free(usbfds); 394 free(usbfds);
394} 395}
395 396
397void usb_autodiscover(int enable)
398{
399 usbmuxd_log(LL_DEBUG, "usb polling enable: %d", enable);
400 device_polling = enable;
401}
402
396static int dev_poll_remain_ms(void) 403static int dev_poll_remain_ms(void)
397{ 404{
398 int msecs; 405 int msecs;
399 struct timeval tv; 406 struct timeval tv;
407 if(!device_polling)
408 return 100000; // devices will never be polled if this is > 0
400 gettimeofday(&tv, NULL); 409 gettimeofday(&tv, NULL);
401 msecs = (next_dev_poll_time.tv_sec - tv.tv_sec) * 1000; 410 msecs = (next_dev_poll_time.tv_sec - tv.tv_sec) * 1000;
402 msecs += (next_dev_poll_time.tv_usec - tv.tv_usec) / 1000; 411 msecs += (next_dev_poll_time.tv_usec - tv.tv_usec) / 1000;
@@ -493,6 +502,7 @@ int usb_init(void)
493 usbmuxd_log(LL_DEBUG, "usb_init for linux / libusb 1.0"); 502 usbmuxd_log(LL_DEBUG, "usb_init for linux / libusb 1.0");
494 503
495 devlist_failures = 0; 504 devlist_failures = 0;
505 device_polling = 1;
496 res = libusb_init(NULL); 506 res = libusb_init(NULL);
497 //libusb_set_debug(NULL, 3); 507 //libusb_set_debug(NULL, 3);
498 if(res != 0) { 508 if(res != 0) {
diff --git a/daemon/usb.h b/daemon/usb.h
index 7e20dce..9b2cb1a 100644
--- a/daemon/usb.h
+++ b/daemon/usb.h
@@ -57,6 +57,8 @@ uint16_t usb_get_pid(struct usb_device *dev);
57void usb_get_fds(struct fdlist *list); 57void usb_get_fds(struct fdlist *list);
58int usb_get_timeout(void); 58int usb_get_timeout(void);
59int usb_send(struct usb_device *dev, const unsigned char *buf, int length); 59int usb_send(struct usb_device *dev, const unsigned char *buf, int length);
60int usb_discover(void);
61void usb_autodiscover(int enable);
60int usb_process(void); 62int usb_process(void);
61int usb_process_timeout(int msec); 63int usb_process_timeout(int msec);
62 64