~ubuntu-branches/ubuntu/lucid/libimobiledevice/lucid

« back to all changes in this revision

Viewing changes to tools/idevicesyslog.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lavergne
  • Date: 2010-02-02 21:19:51 UTC
  • Revision ID: james.westby@ubuntu.com-20100202211951-qv87ejwmucigqezf
Tags: upstream-0.9.7
ImportĀ upstreamĀ versionĀ 0.9.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * idevicesyslog.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 <stdlib.h>
 
28
 
 
29
#include <libimobiledevice/libimobiledevice.h>
 
30
#include <libimobiledevice/lockdown.h>
 
31
 
 
32
static int quit_flag = 0;
 
33
 
 
34
void print_usage(int argc, char **argv);
 
35
 
 
36
/**
 
37
 * signal handler function for cleaning up properly
 
38
 */
 
39
static void clean_exit(int sig)
 
40
{
 
41
        fprintf(stderr, "Exiting...\n");
 
42
        quit_flag++;
 
43
}
 
44
 
 
45
int main(int argc, char *argv[])
 
46
{
 
47
        lockdownd_client_t client = NULL;
 
48
        idevice_t phone = NULL;
 
49
        idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR;
 
50
        int i;
 
51
        char uuid[41];
 
52
        uint16_t port = 0;
 
53
        uuid[0] = 0;
 
54
 
 
55
        signal(SIGINT, clean_exit);
 
56
        signal(SIGQUIT, clean_exit);
 
57
        signal(SIGTERM, clean_exit);
 
58
        signal(SIGPIPE, SIG_IGN);
 
59
 
 
60
        /* parse cmdline args */
 
61
        for (i = 1; i < argc; i++) {
 
62
                if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
 
63
                        idevice_set_debug_level(1);
 
64
                        continue;
 
65
                }
 
66
                else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) {
 
67
                        i++;
 
68
                        if (!argv[i] || (strlen(argv[i]) != 40)) {
 
69
                                print_usage(argc, argv);
 
70
                                return 0;
 
71
                        }
 
72
                        strcpy(uuid, argv[i]);
 
73
                        continue;
 
74
                }
 
75
                else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
 
76
                        print_usage(argc, argv);
 
77
                        return 0;
 
78
                }
 
79
                else {
 
80
                        print_usage(argc, argv);
 
81
                        return 0;
 
82
                }
 
83
        }
 
84
 
 
85
        if (uuid[0] != 0) {
 
86
                ret = idevice_new(&phone, uuid);
 
87
                if (ret != IDEVICE_E_SUCCESS) {
 
88
                        printf("No device found with uuid %s, is it plugged in?\n", uuid);
 
89
                        return -1;
 
90
                }
 
91
        }
 
92
        else
 
93
        {
 
94
                ret = idevice_new(&phone, NULL);
 
95
                if (ret != IDEVICE_E_SUCCESS) {
 
96
                        printf("No device found, is it plugged in?\n");
 
97
                        return -1;
 
98
                }
 
99
        }
 
100
 
 
101
        if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(phone, &client, "idevicesyslog")) {
 
102
                idevice_free(phone);
 
103
                return -1;
 
104
        }
 
105
 
 
106
        /* start syslog_relay service and retrieve port */
 
107
        ret = lockdownd_start_service(client, "com.apple.syslog_relay", &port);
 
108
        if ((ret == LOCKDOWN_E_SUCCESS) && port) {
 
109
                lockdownd_client_free(client);
 
110
                
 
111
                /* connect to socket relay messages */
 
112
                idevice_connection_t conn = NULL;
 
113
                if ((idevice_connect(phone, port, &conn) != IDEVICE_E_SUCCESS) || !conn) {
 
114
                        printf("ERROR: Could not open usbmux connection.\n");
 
115
                } else {
 
116
                        while (!quit_flag) {
 
117
                                char *receive = NULL;
 
118
                                uint32_t datalen = 0, bytes = 0, recv_bytes = 0;
 
119
 
 
120
                                ret = idevice_connection_receive(conn, (char *) &datalen, sizeof(datalen), &bytes);
 
121
                                datalen = ntohl(datalen);
 
122
 
 
123
                                if (datalen == 0)
 
124
                                        continue;
 
125
 
 
126
                                recv_bytes += bytes;
 
127
                                receive = (char *) malloc(sizeof(char) * datalen);
 
128
 
 
129
                                while (!quit_flag && (recv_bytes <= datalen)) {
 
130
                                        ret = idevice_connection_receive(conn, receive, datalen, &bytes);
 
131
 
 
132
                                        if (bytes == 0)
 
133
                                                break;
 
134
 
 
135
                                        recv_bytes += bytes;
 
136
 
 
137
                                        fwrite(receive, sizeof(char), bytes, stdout);
 
138
                                }
 
139
 
 
140
                                free(receive);
 
141
                        }
 
142
                }
 
143
                idevice_disconnect(conn);
 
144
        } else {
 
145
                printf("ERROR: Could not start service com.apple.syslog_relay.\n");
 
146
        }
 
147
 
 
148
        idevice_free(phone);
 
149
 
 
150
        return 0;
 
151
}
 
152
 
 
153
void print_usage(int argc, char **argv)
 
154
{
 
155
        char *name = NULL;
 
156
        
 
157
        name = strrchr(argv[0], '/');
 
158
        printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0]));
 
159
        printf("Relay syslog of a connected iPhone/iPod Touch.\n\n");
 
160
        printf("  -d, --debug\t\tenable communication debugging\n");
 
161
        printf("  -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n");
 
162
        printf("  -h, --help\t\tprints usage information\n");
 
163
        printf("\n");
 
164
}
 
165