diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 61 |
1 files changed, 58 insertions, 3 deletions
| @@ -28,6 +28,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 28 | #include <errno.h> | 28 | #include <errno.h> |
| 29 | #include <string.h> | 29 | #include <string.h> |
| 30 | #include <stdlib.h> | 30 | #include <stdlib.h> |
| 31 | #include <signal.h> | ||
| 31 | #include <unistd.h> | 32 | #include <unistd.h> |
| 32 | #include <sys/socket.h> | 33 | #include <sys/socket.h> |
| 33 | #include <sys/un.h> | 34 | #include <sys/un.h> |
| @@ -35,8 +36,12 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
| 35 | #include "log.h" | 36 | #include "log.h" |
| 36 | #include "usb.h" | 37 | #include "usb.h" |
| 37 | #include "device.h" | 38 | #include "device.h" |
| 39 | #include "client.h" | ||
| 38 | 40 | ||
| 39 | static const char *socket_path = "/tmp/usbmuxd"; //TODO: CHANGEME | 41 | static const char *socket_path = "/tmp/usbmuxd"; //TODO: CHANGEME |
| 42 | int should_exit; | ||
| 43 | |||
| 44 | struct sigaction sa_old; | ||
| 40 | 45 | ||
| 41 | int create_socket(void) { | 46 | int create_socket(void) { |
| 42 | struct sockaddr_un bind_addr; | 47 | struct sockaddr_un bind_addr; |
| @@ -70,29 +75,61 @@ int create_socket(void) { | |||
| 70 | return listenfd; | 75 | return listenfd; |
| 71 | } | 76 | } |
| 72 | 77 | ||
| 78 | void handle_signal(int sig) | ||
| 79 | { | ||
| 80 | if(sig == SIGINT) { | ||
| 81 | usbmuxd_log(LL_NOTICE,"Caught SIGINT"); | ||
| 82 | } else { | ||
| 83 | usbmuxd_log(LL_NOTICE,"Caught unknown signal %d", sig); | ||
| 84 | } | ||
| 85 | should_exit = 1; | ||
| 86 | sigaction(SIGINT, &sa_old, NULL); | ||
| 87 | } | ||
| 88 | |||
| 89 | void set_signal_handlers(void) | ||
| 90 | { | ||
| 91 | struct sigaction sa; | ||
| 92 | memset(&sa, 0, sizeof(struct sigaction)); | ||
| 93 | sa.sa_handler = handle_signal; | ||
| 94 | sigaction(SIGINT, &sa, &sa_old); | ||
| 95 | } | ||
| 96 | |||
| 73 | int main_loop(int listenfd) | 97 | int main_loop(int listenfd) |
| 74 | { | 98 | { |
| 75 | int to, cnt, i; | 99 | int to, cnt, i, dto; |
| 76 | struct fdlist pollfds; | 100 | struct fdlist pollfds; |
| 77 | 101 | ||
| 78 | while(1) { | 102 | while(!should_exit) { |
| 79 | usbmuxd_log(LL_FLOOD, "main_loop iteration"); | 103 | usbmuxd_log(LL_FLOOD, "main_loop iteration"); |
| 80 | to = usb_get_timeout(); | 104 | to = usb_get_timeout(); |
| 81 | usbmuxd_log(LL_FLOOD, "USB timeout is %d ms", to); | 105 | usbmuxd_log(LL_FLOOD, "USB timeout is %d ms", to); |
| 106 | dto = device_get_timeout(); | ||
| 107 | usbmuxd_log(LL_FLOOD, "Device timeout is %d ms", to); | ||
| 108 | if(dto < to) | ||
| 109 | to = dto; | ||
| 82 | 110 | ||
| 83 | fdlist_create(&pollfds); | 111 | fdlist_create(&pollfds); |
| 84 | fdlist_add(&pollfds, FD_LISTEN, listenfd, POLLIN); | 112 | fdlist_add(&pollfds, FD_LISTEN, listenfd, POLLIN); |
| 85 | usb_get_fds(&pollfds); | 113 | usb_get_fds(&pollfds); |
| 114 | client_get_fds(&pollfds); | ||
| 86 | usbmuxd_log(LL_FLOOD, "fd count is %d", pollfds.count); | 115 | usbmuxd_log(LL_FLOOD, "fd count is %d", pollfds.count); |
| 87 | 116 | ||
| 88 | cnt = poll(pollfds.fds, pollfds.count, to); | 117 | cnt = poll(pollfds.fds, pollfds.count, to); |
| 89 | usbmuxd_log(LL_FLOOD, "poll() returned %d", cnt); | 118 | usbmuxd_log(LL_FLOOD, "poll() returned %d", cnt); |
| 90 | 119 | ||
| 91 | if(cnt == 0) { | 120 | if(cnt == -1) { |
| 121 | if(errno == EINTR && should_exit) { | ||
| 122 | usbmuxd_log(LL_INFO, "event processing interrupted"); | ||
| 123 | fdlist_free(&pollfds); | ||
| 124 | return 0; | ||
| 125 | } | ||
| 126 | } else if(cnt == 0) { | ||
| 92 | if(usb_process() < 0) { | 127 | if(usb_process() < 0) { |
| 93 | usbmuxd_log(LL_FATAL, "usb_process() failed"); | 128 | usbmuxd_log(LL_FATAL, "usb_process() failed"); |
| 129 | fdlist_free(&pollfds); | ||
| 94 | return -1; | 130 | return -1; |
| 95 | } | 131 | } |
| 132 | device_check_timeouts(); | ||
| 96 | } else { | 133 | } else { |
| 97 | int done_usb = 0; | 134 | int done_usb = 0; |
| 98 | for(i=0; i<pollfds.count; i++) { | 135 | for(i=0; i<pollfds.count; i++) { |
| @@ -100,15 +137,27 @@ int main_loop(int listenfd) | |||
| 100 | if(!done_usb && pollfds.owners[i] == FD_USB) { | 137 | if(!done_usb && pollfds.owners[i] == FD_USB) { |
| 101 | if(usb_process() < 0) { | 138 | if(usb_process() < 0) { |
| 102 | usbmuxd_log(LL_FATAL, "usb_process() failed"); | 139 | usbmuxd_log(LL_FATAL, "usb_process() failed"); |
| 140 | fdlist_free(&pollfds); | ||
| 103 | return -1; | 141 | return -1; |
| 104 | } | 142 | } |
| 105 | done_usb = 1; | 143 | done_usb = 1; |
| 106 | } | 144 | } |
| 145 | if(pollfds.owners[i] == FD_LISTEN) { | ||
| 146 | if(client_accept(listenfd) < 0) { | ||
| 147 | usbmuxd_log(LL_FATAL, "client_accept() failed"); | ||
| 148 | fdlist_free(&pollfds); | ||
| 149 | return -1; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | if(pollfds.owners[i] == FD_CLIENT) { | ||
| 153 | client_process(pollfds.fds[i].fd, pollfds.fds[i].revents); | ||
| 154 | } | ||
| 107 | } | 155 | } |
| 108 | } | 156 | } |
| 109 | } | 157 | } |
| 110 | fdlist_free(&pollfds); | 158 | fdlist_free(&pollfds); |
| 111 | } | 159 | } |
| 160 | return 0; | ||
| 112 | } | 161 | } |
| 113 | 162 | ||
| 114 | int main(int argc, char *argv[]) | 163 | int main(int argc, char *argv[]) |
| @@ -117,12 +166,16 @@ int main(int argc, char *argv[]) | |||
| 117 | int res; | 166 | int res; |
| 118 | 167 | ||
| 119 | usbmuxd_log(LL_NOTICE, "usbmux v0.1 starting up"); | 168 | usbmuxd_log(LL_NOTICE, "usbmux v0.1 starting up"); |
| 169 | should_exit = 0; | ||
| 170 | |||
| 171 | set_signal_handlers(); | ||
| 120 | 172 | ||
| 121 | usbmuxd_log(LL_INFO, "Creating socket"); | 173 | usbmuxd_log(LL_INFO, "Creating socket"); |
| 122 | listenfd = create_socket(); | 174 | listenfd = create_socket(); |
| 123 | if(listenfd < 0) | 175 | if(listenfd < 0) |
| 124 | return 1; | 176 | return 1; |
| 125 | 177 | ||
| 178 | client_init(); | ||
| 126 | device_init(); | 179 | device_init(); |
| 127 | usbmuxd_log(LL_INFO, "Initializing USB"); | 180 | usbmuxd_log(LL_INFO, "Initializing USB"); |
| 128 | if((res = usb_init()) < 0) | 181 | if((res = usb_init()) < 0) |
| @@ -136,8 +189,10 @@ int main(int argc, char *argv[]) | |||
| 136 | usbmuxd_log(LL_FATAL, "main_loop failed"); | 189 | usbmuxd_log(LL_FATAL, "main_loop failed"); |
| 137 | 190 | ||
| 138 | usbmuxd_log(LL_NOTICE, "usbmux shutting down"); | 191 | usbmuxd_log(LL_NOTICE, "usbmux shutting down"); |
| 192 | device_kill_connections(); | ||
| 139 | usb_shutdown(); | 193 | usb_shutdown(); |
| 140 | device_shutdown(); | 194 | device_shutdown(); |
| 195 | client_shutdown(); | ||
| 141 | usbmuxd_log(LL_NOTICE, "Shutdown complete"); | 196 | usbmuxd_log(LL_NOTICE, "Shutdown complete"); |
| 142 | 197 | ||
| 143 | if(res < 0) | 198 | if(res < 0) |
