Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 1 | /* |
Nikias Bassen | 96101a1 | 2010-01-28 22:18:41 +0100 | [diff] [blame] | 2 | * idevicesyslog.c |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 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> |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 25 | #include <signal.h> |
Martin Szulecki | 0727ae7 | 2009-06-21 23:02:22 +0200 | [diff] [blame] | 26 | #include <stdlib.h> |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
| 29 | #ifdef WIN32 |
| 30 | #include <windows.h> |
| 31 | #define sleep(x) Sleep(x*1000) |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 32 | #endif |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 33 | |
Nikias Bassen | 96101a1 | 2010-01-28 22:18:41 +0100 | [diff] [blame] | 34 | #include <libimobiledevice/libimobiledevice.h> |
| 35 | #include <libimobiledevice/lockdown.h> |
Martin Szulecki | abad8cb | 2013-05-23 14:15:40 +0200 | [diff] [blame^] | 36 | #include <libimobiledevice/syslog_relay.h> |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 37 | |
| 38 | static int quit_flag = 0; |
| 39 | |
| 40 | void print_usage(int argc, char **argv); |
| 41 | |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 42 | static char* udid = NULL; |
| 43 | |
| 44 | static idevice_t device = NULL; |
Martin Szulecki | abad8cb | 2013-05-23 14:15:40 +0200 | [diff] [blame^] | 45 | static syslog_relay_client_t syslog = NULL; |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 46 | |
Martin Szulecki | abad8cb | 2013-05-23 14:15:40 +0200 | [diff] [blame^] | 47 | static void syslog_callback(char c, void *user_data) |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 48 | { |
Martin Szulecki | abad8cb | 2013-05-23 14:15:40 +0200 | [diff] [blame^] | 49 | putchar(c); |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static int start_logging() |
| 53 | { |
| 54 | idevice_error_t ret = idevice_new(&device, udid); |
| 55 | if (ret != IDEVICE_E_SUCCESS) { |
| 56 | fprintf(stderr, "Device with udid %s not found!?\n", udid); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | /* start and connect to syslog_relay service */ |
Martin Szulecki | abad8cb | 2013-05-23 14:15:40 +0200 | [diff] [blame^] | 61 | syslog_relay_error_t serr = SYSLOG_RELAY_E_UNKNOWN_ERROR; |
| 62 | serr = syslog_relay_client_start_service(device, &syslog, "idevicesyslog"); |
| 63 | if (serr != SYSLOG_RELAY_E_SUCCESS) { |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 64 | fprintf(stderr, "ERROR: Could not start service com.apple.syslog_relay.\n"); |
| 65 | idevice_free(device); |
| 66 | device = NULL; |
| 67 | return -1; |
| 68 | } |
| 69 | |
Martin Szulecki | abad8cb | 2013-05-23 14:15:40 +0200 | [diff] [blame^] | 70 | /* start capturing syslog */ |
| 71 | serr = syslog_relay_start_capture(syslog, syslog_callback, NULL); |
| 72 | if (serr != SYSLOG_RELAY_E_SUCCESS) { |
| 73 | fprintf(stderr, "ERROR: Unable tot start capturing syslog.\n"); |
| 74 | syslog_relay_client_free(syslog); |
| 75 | syslog = NULL; |
| 76 | idevice_free(device); |
| 77 | device = NULL; |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 78 | return -1; |
| 79 | } |
Martin Szulecki | abad8cb | 2013-05-23 14:15:40 +0200 | [diff] [blame^] | 80 | |
| 81 | fprintf(stdout, "[connected]\n"); |
| 82 | fflush(stdout); |
| 83 | |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static void stop_logging() |
| 88 | { |
Martin Szulecki | abad8cb | 2013-05-23 14:15:40 +0200 | [diff] [blame^] | 89 | fflush(stdout); |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 90 | |
Martin Szulecki | abad8cb | 2013-05-23 14:15:40 +0200 | [diff] [blame^] | 91 | if (syslog) { |
| 92 | syslog_relay_client_free(syslog); |
| 93 | syslog = NULL; |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | if (device) { |
| 97 | idevice_free(device); |
| 98 | device = NULL; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | static void device_event_cb(const idevice_event_t* event, void* userdata) |
| 103 | { |
| 104 | if (event->event == IDEVICE_DEVICE_ADD) { |
Martin Szulecki | abad8cb | 2013-05-23 14:15:40 +0200 | [diff] [blame^] | 105 | if (!syslog) { |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 106 | if (!udid) { |
| 107 | udid = strdup(event->udid); |
| 108 | } |
| 109 | if (strcmp(udid, event->udid) == 0) { |
| 110 | if (start_logging() != 0) { |
| 111 | fprintf(stderr, "Could not start logger for udid %s\n", udid); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } else if (event->event == IDEVICE_DEVICE_REMOVE) { |
Martin Szulecki | abad8cb | 2013-05-23 14:15:40 +0200 | [diff] [blame^] | 116 | if (syslog && (strcmp(udid, event->udid) == 0)) { |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 117 | stop_logging(); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 122 | /** |
| 123 | * signal handler function for cleaning up properly |
| 124 | */ |
| 125 | static void clean_exit(int sig) |
| 126 | { |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 127 | fprintf(stderr, "\nExiting...\n"); |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 128 | quit_flag++; |
| 129 | } |
| 130 | |
| 131 | int main(int argc, char *argv[]) |
| 132 | { |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 133 | int i; |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 134 | |
| 135 | signal(SIGINT, clean_exit); |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 136 | signal(SIGTERM, clean_exit); |
Nikias Bassen | 7acb8ec | 2011-09-15 00:44:51 +0200 | [diff] [blame] | 137 | #ifndef WIN32 |
| 138 | signal(SIGQUIT, clean_exit); |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 139 | signal(SIGPIPE, SIG_IGN); |
Nikias Bassen | 7acb8ec | 2011-09-15 00:44:51 +0200 | [diff] [blame] | 140 | #endif |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 141 | |
| 142 | /* parse cmdline args */ |
| 143 | for (i = 1; i < argc; i++) { |
| 144 | if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { |
Nikias Bassen | 96101a1 | 2010-01-28 22:18:41 +0100 | [diff] [blame] | 145 | idevice_set_debug_level(1); |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 146 | continue; |
| 147 | } |
Martin Szulecki | 7457346 | 2012-03-22 16:07:07 +0100 | [diff] [blame] | 148 | else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--udid")) { |
Nikias Bassen | b61667e | 2009-05-19 13:15:11 +0200 | [diff] [blame] | 149 | i++; |
| 150 | if (!argv[i] || (strlen(argv[i]) != 40)) { |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 151 | print_usage(argc, argv); |
| 152 | return 0; |
| 153 | } |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 154 | udid = strdup(argv[i]); |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 155 | continue; |
| 156 | } |
| 157 | else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { |
| 158 | print_usage(argc, argv); |
| 159 | return 0; |
| 160 | } |
| 161 | else { |
| 162 | print_usage(argc, argv); |
| 163 | return 0; |
| 164 | } |
| 165 | } |
| 166 | |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 167 | int num = 0; |
| 168 | char **devices = NULL; |
| 169 | idevice_get_device_list(&devices, &num); |
| 170 | idevice_device_list_free(devices); |
| 171 | if (num == 0) { |
| 172 | if (!udid) { |
| 173 | fprintf(stderr, "No device found. Plug in a device or pass UDID with -u to wait for device to be available.\n"); |
| 174 | return -1; |
Nikias Bassen | 36c0192 | 2012-11-29 03:42:06 +0100 | [diff] [blame] | 175 | } else { |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 176 | fprintf(stderr, "Waiting for device with UDID %s to become available...\n", udid); |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 177 | } |
Nikias Bassen | b61667e | 2009-05-19 13:15:11 +0200 | [diff] [blame] | 178 | } |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 179 | |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 180 | idevice_event_subscribe(device_event_cb, NULL); |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 181 | |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 182 | while (!quit_flag) { |
| 183 | sleep(1); |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 184 | } |
Nikias Bassen | b5a7434 | 2013-02-28 11:04:17 +0100 | [diff] [blame] | 185 | idevice_event_unsubscribe(); |
| 186 | stop_logging(); |
| 187 | |
| 188 | if (udid) { |
| 189 | free(udid); |
| 190 | } |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | void print_usage(int argc, char **argv) |
| 196 | { |
Martin Szulecki | 9223396 | 2009-07-01 18:17:16 +0200 | [diff] [blame] | 197 | char *name = NULL; |
| 198 | |
| 199 | name = strrchr(argv[0], '/'); |
| 200 | printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); |
Martin Szulecki | 1379f42 | 2012-04-07 15:50:04 +0200 | [diff] [blame] | 201 | printf("Relay syslog of a connected device.\n\n"); |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 202 | printf(" -d, --debug\t\tenable communication debugging\n"); |
Martin Szulecki | 7457346 | 2012-03-22 16:07:07 +0100 | [diff] [blame] | 203 | printf(" -u, --udid UDID\ttarget specific device by its 40-digit device UDID\n"); |
Martin Szulecki | f104eb4 | 2009-04-16 17:12:45 +0200 | [diff] [blame] | 204 | printf(" -h, --help\t\tprints usage information\n"); |
| 205 | printf("\n"); |
| 206 | } |
| 207 | |