summaryrefslogtreecommitdiffstats
path: root/usbmuxd/main.c
diff options
context:
space:
mode:
authorGravatar Hector Martin2009-05-05 00:38:06 +0200
committerGravatar Hector Martin2009-05-05 00:38:06 +0200
commitbd335df6954fe5e9419499820cd082c27de111f1 (patch)
tree4d7ccd2e767083baa2c41880081780de57665cf7 /usbmuxd/main.c
parent9bf93e406de3a06cee0a1452bf1da3c6f697ee31 (diff)
downloadusbmuxd-bd335df6954fe5e9419499820cd082c27de111f1.tar.gz
usbmuxd-bd335df6954fe5e9419499820cd082c27de111f1.tar.bz2
Move usbmuxd to its own folder
Diffstat (limited to 'usbmuxd/main.c')
-rw-r--r--usbmuxd/main.c201
1 files changed, 201 insertions, 0 deletions
diff --git a/usbmuxd/main.c b/usbmuxd/main.c
new file mode 100644
index 0000000..d39b416
--- /dev/null
+++ b/usbmuxd/main.c
@@ -0,0 +1,201 @@
1/*
2 usbmuxd - iPhone/iPod Touch USB multiplex server daemon
3
4Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com>
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 or version 3.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19*/
20
21#define _BSD_SOURCE
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include <stdio.h>
28#include <errno.h>
29#include <string.h>
30#include <stdlib.h>
31#include <signal.h>
32#include <unistd.h>
33#include <sys/socket.h>
34#include <sys/un.h>
35
36#include "log.h"
37#include "usb.h"
38#include "device.h"
39#include "client.h"
40
41static const char *socket_path = "/tmp/usbmuxd"; //TODO: CHANGEME
42int should_exit;
43
44struct sigaction sa_old;
45
46int create_socket(void) {
47 struct sockaddr_un bind_addr;
48 int listenfd;
49
50 if(unlink(socket_path) == -1 && errno != ENOENT) {
51 usbmuxd_log(LL_FATAL, "unlink(%s) failed: %s", socket_path, strerror(errno));
52 return -1;
53 }
54
55 listenfd = socket(AF_UNIX, SOCK_STREAM, 0);
56 if (listenfd == -1) {
57 usbmuxd_log(LL_FATAL, "socket() failed: %s", strerror(errno));
58 return -1;
59 }
60
61 bzero(&bind_addr, sizeof(bind_addr));
62 bind_addr.sun_family = AF_UNIX;
63 strcpy(bind_addr.sun_path, socket_path);
64 if (bind(listenfd, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) != 0) {
65 usbmuxd_log(LL_FATAL, "bind() failed: %s", strerror(errno));
66 return -1;
67 }
68
69 // Start listening
70 if (listen(listenfd, 5) != 0) {
71 usbmuxd_log(LL_FATAL, "listen() failed: %s", strerror(errno));
72 return -1;
73 }
74
75 return listenfd;
76}
77
78void 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
89void 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
97int main_loop(int listenfd)
98{
99 int to, cnt, i, dto;
100 struct fdlist pollfds;
101
102 while(!should_exit) {
103 usbmuxd_log(LL_FLOOD, "main_loop iteration");
104 to = usb_get_timeout();
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;
110
111 fdlist_create(&pollfds);
112 fdlist_add(&pollfds, FD_LISTEN, listenfd, POLLIN);
113 usb_get_fds(&pollfds);
114 client_get_fds(&pollfds);
115 usbmuxd_log(LL_FLOOD, "fd count is %d", pollfds.count);
116
117 cnt = poll(pollfds.fds, pollfds.count, to);
118 usbmuxd_log(LL_FLOOD, "poll() returned %d", cnt);
119
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) {
127 if(usb_process() < 0) {
128 usbmuxd_log(LL_FATAL, "usb_process() failed");
129 fdlist_free(&pollfds);
130 return -1;
131 }
132 device_check_timeouts();
133 } else {
134 int done_usb = 0;
135 for(i=0; i<pollfds.count; i++) {
136 if(pollfds.fds[i].revents) {
137 if(!done_usb && pollfds.owners[i] == FD_USB) {
138 if(usb_process() < 0) {
139 usbmuxd_log(LL_FATAL, "usb_process() failed");
140 fdlist_free(&pollfds);
141 return -1;
142 }
143 done_usb = 1;
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 }
155 }
156 }
157 }
158 fdlist_free(&pollfds);
159 }
160 return 0;
161}
162
163int main(int argc, char *argv[])
164{
165 int listenfd;
166 int res;
167
168 usbmuxd_log(LL_NOTICE, "usbmux v0.1 starting up");
169 should_exit = 0;
170
171 set_signal_handlers();
172
173 usbmuxd_log(LL_INFO, "Creating socket");
174 listenfd = create_socket();
175 if(listenfd < 0)
176 return 1;
177
178 client_init();
179 device_init();
180 usbmuxd_log(LL_INFO, "Initializing USB");
181 if((res = usb_init()) < 0)
182 return 2;
183 usbmuxd_log(LL_INFO, "%d device%s detected", res, (res==1)?"":"s");
184
185 usbmuxd_log(LL_NOTICE, "Initialization complete");
186
187 res = main_loop(listenfd);
188 if(res < 0)
189 usbmuxd_log(LL_FATAL, "main_loop failed");
190
191 usbmuxd_log(LL_NOTICE, "usbmux shutting down");
192 device_kill_connections();
193 usb_shutdown();
194 device_shutdown();
195 client_shutdown();
196 usbmuxd_log(LL_NOTICE, "Shutdown complete");
197
198 if(res < 0)
199 return -res;
200 return 0;
201}