summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..c2467a2
--- /dev/null
+++ b/main.c
@@ -0,0 +1,92 @@
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 <unistd.h>
32#include <sys/socket.h>
33#include <sys/un.h>
34
35#include "log.h"
36#include "usb.h"
37
38const char *socket_path = "/tmp/usbmuxd"; //TODO: CHANGEME
39
40int create_socket(void) {
41 struct sockaddr_un bind_addr;
42 int listenfd;
43
44 if(unlink(socket_path) == -1 && errno != ENOENT) {
45 usbmuxd_log(LOG_FATAL, "unlink(%s) failed: %s", socket_path, strerror(errno));
46 return -1;
47 }
48
49 listenfd = socket(AF_UNIX, SOCK_STREAM, 0);
50 if (listenfd == -1) {
51 usbmuxd_log(LOG_FATAL, "socket() failed: %s", strerror(errno));
52 return -1;
53 }
54
55 bzero(&bind_addr, sizeof(bind_addr));
56 bind_addr.sun_family = AF_UNIX;
57 strcpy(bind_addr.sun_path, socket_path);
58 if (bind(listenfd, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) != 0) {
59 usbmuxd_log(LOG_FATAL, "bind() failed: %s", strerror(errno));
60 return -1;
61 }
62
63 // Start listening
64 if (listen(listenfd, 5) != 0) {
65 usbmuxd_log(LOG_FATAL, "listen() failed: %s", strerror(errno));
66 return -1;
67 }
68
69 return listenfd;
70}
71
72int main(int argc, char *argv[])
73{
74 int listenfd;
75 int res;
76
77 usbmuxd_log(LOG_NOTICE, "usbmux v0.1 starting up");
78
79 usbmuxd_log(LOG_INFO, "Creating socket");
80 listenfd = create_socket();
81 if(listenfd < 0)
82 return 1;
83
84 usbmuxd_log(LOG_INFO, "Initializing USB");
85 if((res = usb_init()) < 0)
86 return 2;
87 usbmuxd_log(LOG_INFO, "%d device%s detected", res, (res==1)?"":"s");
88
89 usbmuxd_log(LOG_NOTICE, "initialization complete");
90
91 return 0;
92}