diff options
| author | 2009-04-16 17:12:45 +0200 | |
|---|---|---|
| committer | 2009-04-19 15:16:19 -0700 | |
| commit | f104eb4371c2da6bac81f283d9943cdb800a6f95 (patch) | |
| tree | 3ce8c59cbd70c3002216831b0cfd7394878ff415 /dev/syslog_relay.c | |
| parent | 6b1c3e363adff39ce15e57217ab97ae2d692247c (diff) | |
| download | libimobiledevice-f104eb4371c2da6bac81f283d9943cdb800a6f95.tar.gz libimobiledevice-f104eb4371c2da6bac81f283d9943cdb800a6f95.tar.bz2 | |
Add tool to relay the syslog from a device to stdout
[#30]
Signed-off-by: Matt Colyer <matt@colyer.name>
Diffstat (limited to 'dev/syslog_relay.c')
| -rw-r--r-- | dev/syslog_relay.c | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/dev/syslog_relay.c b/dev/syslog_relay.c new file mode 100644 index 0000000..56cf56c --- /dev/null +++ b/dev/syslog_relay.c | |||
| @@ -0,0 +1,159 @@ | |||
| 1 | /* | ||
| 2 | * syslog_relay.c | ||
| 3 | * Relay the syslog of a device to stdout | ||
| 4 | * | ||
| 5 | * Copyright (c) 2009 Martin Szulecki All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include <stdio.h> | ||
| 23 | #include <string.h> | ||
| 24 | #include <errno.h> | ||
| 25 | #include <netinet/in.h> | ||
| 26 | #include <signal.h> | ||
| 27 | #include <usb.h> | ||
| 28 | |||
| 29 | #include <libiphone/libiphone.h> | ||
| 30 | |||
| 31 | static int quit_flag = 0; | ||
| 32 | |||
| 33 | void print_usage(int argc, char **argv); | ||
| 34 | |||
| 35 | /** | ||
| 36 | * signal handler function for cleaning up properly | ||
| 37 | */ | ||
| 38 | static void clean_exit(int sig) | ||
| 39 | { | ||
| 40 | fprintf(stderr, "Exiting...\n"); | ||
| 41 | quit_flag++; | ||
| 42 | } | ||
| 43 | |||
| 44 | int main(int argc, char *argv[]) | ||
| 45 | { | ||
| 46 | iphone_lckd_client_t control = NULL; | ||
| 47 | iphone_device_t phone = NULL; | ||
| 48 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | ||
| 49 | int i; | ||
| 50 | int bus_n = -1, dev_n = -1; | ||
| 51 | int port = 0; | ||
| 52 | |||
| 53 | signal(SIGINT, clean_exit); | ||
| 54 | signal(SIGQUIT, clean_exit); | ||
| 55 | signal(SIGTERM, clean_exit); | ||
| 56 | signal(SIGPIPE, SIG_IGN); | ||
| 57 | |||
| 58 | /* parse cmdline args */ | ||
| 59 | for (i = 1; i < argc; i++) { | ||
| 60 | if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { | ||
| 61 | iphone_set_debug_mask(DBGMASK_ALL); | ||
| 62 | continue; | ||
| 63 | } | ||
| 64 | else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--usb")) { | ||
| 65 | if (sscanf(argv[++i], "%d,%d", &bus_n, &dev_n) < 2) { | ||
| 66 | print_usage(argc, argv); | ||
| 67 | return 0; | ||
| 68 | } | ||
| 69 | continue; | ||
| 70 | } | ||
| 71 | else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { | ||
| 72 | print_usage(argc, argv); | ||
| 73 | return 0; | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | print_usage(argc, argv); | ||
| 77 | return 0; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | if (bus_n != -1) { | ||
| 82 | ret = iphone_get_specific_device(bus_n, dev_n, &phone); | ||
| 83 | if (ret != IPHONE_E_SUCCESS) { | ||
| 84 | printf("No device found for usb bus %d and dev %d, is it plugged in?\n", bus_n, dev_n); | ||
| 85 | return -1; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | else | ||
| 89 | { | ||
| 90 | ret = iphone_get_device(&phone); | ||
| 91 | if (ret != IPHONE_E_SUCCESS) { | ||
| 92 | printf("No device found, is it plugged in?\n"); | ||
| 93 | return -1; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) { | ||
| 98 | iphone_free_device(phone); | ||
| 99 | return -1; | ||
| 100 | } | ||
| 101 | |||
| 102 | /* start syslog_relay service and retrieve port */ | ||
| 103 | ret = iphone_lckd_start_service(control, "com.apple.syslog_relay", &port); | ||
| 104 | if ((ret == IPHONE_E_SUCCESS) && port) { | ||
| 105 | /* connect to socket relay messages */ | ||
| 106 | iphone_umux_client_t syslog_client = NULL; | ||
| 107 | |||
| 108 | ret = iphone_mux_new_client(phone, 514, port, &syslog_client); | ||
| 109 | if (ret == IPHONE_E_SUCCESS) { | ||
| 110 | while (!quit_flag) { | ||
| 111 | char *receive = NULL; | ||
| 112 | uint32_t datalen = 0, bytes = 0, recv_bytes = 0; | ||
| 113 | |||
| 114 | ret = iphone_mux_recv(syslog_client, (char *) &datalen, sizeof(datalen), &bytes); | ||
| 115 | datalen = ntohl(datalen); | ||
| 116 | |||
| 117 | if (datalen == 0) | ||
| 118 | continue; | ||
| 119 | |||
| 120 | recv_bytes += bytes; | ||
| 121 | receive = (char *) malloc(sizeof(char) * datalen); | ||
| 122 | |||
| 123 | while (!quit_flag && (recv_bytes <= datalen)) { | ||
| 124 | ret = iphone_mux_recv(syslog_client, receive, datalen, &bytes); | ||
| 125 | |||
| 126 | if (bytes == 0) | ||
| 127 | break; | ||
| 128 | |||
| 129 | recv_bytes += bytes; | ||
| 130 | |||
| 131 | fwrite(receive, sizeof(char), bytes, stdout); | ||
| 132 | } | ||
| 133 | |||
| 134 | free(receive); | ||
| 135 | } | ||
| 136 | } else { | ||
| 137 | printf("ERROR: Could not open usbmux connection.\n"); | ||
| 138 | } | ||
| 139 | iphone_mux_free_client(syslog_client); | ||
| 140 | } else { | ||
| 141 | printf("ERROR: Could not start service com.apple.syslog_relay.\n"); | ||
| 142 | } | ||
| 143 | |||
| 144 | iphone_lckd_free_client(control); | ||
| 145 | iphone_free_device(phone); | ||
| 146 | |||
| 147 | return 0; | ||
| 148 | } | ||
| 149 | |||
| 150 | void print_usage(int argc, char **argv) | ||
| 151 | { | ||
| 152 | printf("Usage: %s [OPTIONS]\n", (strrchr(argv[0], '/') + 1)); | ||
| 153 | printf("Relay syslog of a connected iPhone/iPod Touch.\n\n"); | ||
| 154 | printf(" -d, --debug\t\tenable communication debugging\n"); | ||
| 155 | printf(" -u, --usb=BUS,DEV\ttarget specific device by usb bus/dev number\n"); | ||
| 156 | printf(" -h, --help\t\tprints usage information\n"); | ||
| 157 | printf("\n"); | ||
| 158 | } | ||
| 159 | |||
