diff options
| author | 2009-04-27 01:37:05 +0200 | |
|---|---|---|
| committer | 2009-04-27 01:37:05 +0200 | |
| commit | d982007a7350df35c5aeba820a520779694514a7 (patch) | |
| tree | 62d16ab56c7f92b12999bbcdbe7ce0f9293fe069 /main.c | |
| parent | 677fa91e376b98d871bb238ecb84685dd4f33ebd (diff) | |
| download | usbmuxd-d982007a7350df35c5aeba820a520779694514a7.tar.gz usbmuxd-d982007a7350df35c5aeba820a520779694514a7.tar.bz2 | |
general framework, unix socket listen, utils
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 92 |
1 files changed, 92 insertions, 0 deletions
| @@ -0,0 +1,92 @@ | |||
| 1 | /* | ||
| 2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
| 3 | |||
| 4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
| 5 | |||
| 6 | This program is free software; you can redistribute it and/or modify | ||
| 7 | it under the terms of the GNU General Public License as published by | ||
| 8 | the Free Software Foundation, either version 2 or version 3. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program; if not, write to the Free Software | ||
| 17 | Foundation, 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 | |||
| 38 | const char *socket_path = "/tmp/usbmuxd"; //TODO: CHANGEME | ||
| 39 | |||
| 40 | int 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 | |||
| 72 | int 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 | } | ||
