summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorGravatar Hector Martin2009-04-30 05:52:10 +0200
committerGravatar Hector Martin2009-04-30 05:52:10 +0200
commit49a8ef4fcbc8e76ca0f484e6b2d2d9eea70596b6 (patch)
treed42281bbc3b17af0f92daa5d35429f46bdd2130f /main.c
parent53fb582e7729d5b7ed40ff04d912fcf5add7ce1c (diff)
downloadusbmuxd-49a8ef4fcbc8e76ca0f484e6b2d2d9eea70596b6.tar.gz
usbmuxd-49a8ef4fcbc8e76ca0f484e6b2d2d9eea70596b6.tar.bz2
too much stuff and it WORKS
Diffstat (limited to 'main.c')
-rw-r--r--main.c61
1 files changed, 58 insertions, 3 deletions
diff --git a/main.c b/main.c
index 7bc8dbe..d39b416 100644
--- a/main.c
+++ b/main.c
@@ -28,6 +28,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <errno.h>
#include <string.h>
#include <stdlib.h>
+#include <signal.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
@@ -35,8 +36,12 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "log.h"
#include "usb.h"
#include "device.h"
+#include "client.h"
static const char *socket_path = "/tmp/usbmuxd"; //TODO: CHANGEME
+int should_exit;
+
+struct sigaction sa_old;
int create_socket(void) {
struct sockaddr_un bind_addr;
@@ -70,29 +75,61 @@ int create_socket(void) {
return listenfd;
}
+void handle_signal(int sig)
+{
+ if(sig == SIGINT) {
+ usbmuxd_log(LL_NOTICE,"Caught SIGINT");
+ } else {
+ usbmuxd_log(LL_NOTICE,"Caught unknown signal %d", sig);
+ }
+ should_exit = 1;
+ sigaction(SIGINT, &sa_old, NULL);
+}
+
+void set_signal_handlers(void)
+{
+ struct sigaction sa;
+ memset(&sa, 0, sizeof(struct sigaction));
+ sa.sa_handler = handle_signal;
+ sigaction(SIGINT, &sa, &sa_old);
+}
+
int main_loop(int listenfd)
{
- int to, cnt, i;
+ int to, cnt, i, dto;
struct fdlist pollfds;
- while(1) {
+ while(!should_exit) {
usbmuxd_log(LL_FLOOD, "main_loop iteration");
to = usb_get_timeout();
usbmuxd_log(LL_FLOOD, "USB timeout is %d ms", to);
+ dto = device_get_timeout();
+ usbmuxd_log(LL_FLOOD, "Device timeout is %d ms", to);
+ if(dto < to)
+ to = dto;
fdlist_create(&pollfds);
fdlist_add(&pollfds, FD_LISTEN, listenfd, POLLIN);
usb_get_fds(&pollfds);
+ client_get_fds(&pollfds);
usbmuxd_log(LL_FLOOD, "fd count is %d", pollfds.count);
cnt = poll(pollfds.fds, pollfds.count, to);
usbmuxd_log(LL_FLOOD, "poll() returned %d", cnt);
- if(cnt == 0) {
+ if(cnt == -1) {
+ if(errno == EINTR && should_exit) {
+ usbmuxd_log(LL_INFO, "event processing interrupted");
+ fdlist_free(&pollfds);
+ return 0;
+ }
+ } else if(cnt == 0) {
if(usb_process() < 0) {
usbmuxd_log(LL_FATAL, "usb_process() failed");
+ fdlist_free(&pollfds);
return -1;
}
+ device_check_timeouts();
} else {
int done_usb = 0;
for(i=0; i<pollfds.count; i++) {
@@ -100,15 +137,27 @@ int main_loop(int listenfd)
if(!done_usb && pollfds.owners[i] == FD_USB) {
if(usb_process() < 0) {
usbmuxd_log(LL_FATAL, "usb_process() failed");
+ fdlist_free(&pollfds);
return -1;
}
done_usb = 1;
}
+ if(pollfds.owners[i] == FD_LISTEN) {
+ if(client_accept(listenfd) < 0) {
+ usbmuxd_log(LL_FATAL, "client_accept() failed");
+ fdlist_free(&pollfds);
+ return -1;
+ }
+ }
+ if(pollfds.owners[i] == FD_CLIENT) {
+ client_process(pollfds.fds[i].fd, pollfds.fds[i].revents);
+ }
}
}
}
fdlist_free(&pollfds);
}
+ return 0;
}
int main(int argc, char *argv[])
@@ -117,12 +166,16 @@ int main(int argc, char *argv[])
int res;
usbmuxd_log(LL_NOTICE, "usbmux v0.1 starting up");
+ should_exit = 0;
+
+ set_signal_handlers();
usbmuxd_log(LL_INFO, "Creating socket");
listenfd = create_socket();
if(listenfd < 0)
return 1;
+ client_init();
device_init();
usbmuxd_log(LL_INFO, "Initializing USB");
if((res = usb_init()) < 0)
@@ -136,8 +189,10 @@ int main(int argc, char *argv[])
usbmuxd_log(LL_FATAL, "main_loop failed");
usbmuxd_log(LL_NOTICE, "usbmux shutting down");
+ device_kill_connections();
usb_shutdown();
device_shutdown();
+ client_shutdown();
usbmuxd_log(LL_NOTICE, "Shutdown complete");
if(res < 0)