summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorGravatar Paul Sladen2009-03-27 01:35:26 +0100
committerGravatar Nikias Bassen2009-03-27 01:35:26 +0100
commit9aec772bf67aa4817f0f5f6f9a262ec1eff4d986 (patch)
treea8da5c5583fb112c9ba43e2cffcae79fe3ab4536 /main.c
parenta8d088cf480f306fc5724a7590a59d61d62af382 (diff)
downloadusbmuxd-9aec772bf67aa4817f0f5f6f9a262ec1eff4d986.tar.gz
usbmuxd-9aec772bf67aa4817f0f5f6f9a262ec1eff4d986.tar.bz2
Make all fprintf(stderr) be >= verbose level
Signed-off-by: Nikias Bassen <nikias@gmx.li>
Diffstat (limited to 'main.c')
-rw-r--r--main.c169
1 files changed, 89 insertions, 80 deletions
diff --git a/main.c b/main.c
index 76f5c20..e196b8c 100644
--- a/main.c
+++ b/main.c
@@ -41,9 +41,11 @@
41 41
42#define DEFAULT_TIMEOUT 4000 42#define DEFAULT_TIMEOUT 4000
43#define DEFAULT_CHILDREN_CAPACITY 10 43#define DEFAULT_CHILDREN_CAPACITY 10
44#define DEBUG_LEVEL 0
44 45
45static int quit_flag = 0; 46static int quit_flag = 0;
46static int fsock = -1; 47static int fsock = -1;
48static int verbose = DEBUG_LEVEL;
47 49
48struct device_use_info { 50struct device_use_info {
49 uint32_t device_id; 51 uint32_t device_id;
@@ -80,22 +82,22 @@ static pthread_mutex_t usb_mutex = PTHREAD_MUTEX_INITIALIZER;
80/** 82/**
81 * for debugging purposes. 83 * for debugging purposes.
82 */ 84 */
83static void print_buffer(const char *data, const int length) 85static void print_buffer(FILE *fp, const char *data, const int length)
84{ 86{
85 int i; 87 int i;
86 int j; 88 int j;
87 unsigned char c; 89 unsigned char c;
88 90
89 for(i=0; i<length; i+=16) { 91 for(i=0; i<length; i+=16) {
90 printf("%04x: ", i); 92 if (verbose >= 4) fprintf(fp, "%04x: ", i);
91 for (j=0;j<16;j++) { 93 for (j=0;j<16;j++) {
92 if (i+j >= length) { 94 if (i+j >= length) {
93 printf(" "); 95 printf(" ");
94 continue; 96 continue;
95 } 97 }
96 printf("%02hhx ", *(data+i+j)); 98 if (verbose >= 4) printf("%02hhx ", *(data+i+j));
97 } 99 }
98 printf(" | "); 100 if (verbose >= 4) fprintf(fp, " | ");
99 for(j=0;j<16;j++) { 101 for(j=0;j<16;j++) {
100 if (i+j >= length) 102 if (i+j >= length)
101 break; 103 break;
@@ -104,11 +106,11 @@ static void print_buffer(const char *data, const int length)
104 printf("."); 106 printf(".");
105 continue; 107 continue;
106 } 108 }
107 printf("%c", c); 109 if (verbose >= 4) fprintf(fp, "%c", c);
108 } 110 }
109 printf("\n"); 111 if (verbose >= 4) fprintf(fp, "\n");
110 } 112 }
111 printf("\n"); 113 if (verbose >= 4) fprintf(fp, "\n");
112} 114}
113#endif 115#endif
114 116
@@ -137,19 +139,19 @@ static int usbmuxd_get_request(int fd, void **data, size_t len)
137 *data = malloc(pktlen); 139 *data = malloc(pktlen);
138 } else if (len < pktlen) { 140 } else if (len < pktlen) {
139 // target buffer is to small to hold this packet! fix it! 141 // target buffer is to small to hold this packet! fix it!
140 fprintf(stderr, "%s: WARNING -- packet (%d) is larger than target buffer (%d)! Truncating.\n", __func__, pktlen, len); 142 if (verbose >= 2) fprintf(stderr, "%s: WARNING -- packet (%d) is larger than target buffer (%d)! Truncating.\n", __func__, pktlen, len);
141 pktlen = len; 143 pktlen = len;
142 } 144 }
143 145
144 recv_len = recv_buf(fd, *data, pktlen); 146 recv_len = recv_buf(fd, *data, pktlen);
145 if ((recv_len > 0) && (recv_len < pktlen)) { 147 if ((recv_len > 0) && (recv_len < pktlen)) {
146 fprintf(stderr, "%s: Uh-oh, we got less than the packet's size, %d instead of %d...\n", __func__, recv_len, pktlen); 148 if (verbose >= 2) fprintf(stderr, "%s: Uh-oh, we got less than the packet's size, %d instead of %d...\n", __func__, recv_len, pktlen);
147 } 149 }
148 150
149#ifdef DEBUG 151#ifdef DEBUG
150 if (*data && (recv_len > 0)) { 152 if (*data && (recv_len > 0) && verbose >= 4) {
151 fprintf(stderr, "%s: received:\n", __func__); 153 if (verbose >= 3) fprintf(stderr, "%s: received:\n", __func__);
152 print_buffer(*data,recv_len); 154 print_buffer(stderr, *data, recv_len);
153 } 155 }
154#endif 156#endif
155 157
@@ -176,7 +178,7 @@ static int usbmuxd_send_result(int fd, uint32_t tag, uint32_t result_code)
176 res.header.tag = tag; 178 res.header.tag = tag;
177 res.result = result_code; 179 res.result = result_code;
178 180
179 fprintf(stderr, "%s: tag=%d result=%d\n", __func__, res.header.tag, res.result); 181 if (verbose >= 4) fprintf(stderr, "%s: tag=%d result=%d\n", __func__, res.header.tag, res.result);
180 182
181 ret = send_buf(fd, &res, sizeof(res)); 183 ret = send_buf(fd, &res, sizeof(res));
182 fsync(fd); // let's get it sent 184 fsync(fd); // let's get it sent
@@ -204,7 +206,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
204 int result; 206 int result;
205 207
206 if (!arg) { 208 if (!arg) {
207 fprintf(stderr, "%s: invalid client_data supplied!\n", __func__); 209 if (verbose >= 2) fprintf(stderr, "%s: invalid client_data supplied!\n", __func__);
208 cdata->reader_dead = 1; 210 cdata->reader_dead = 1;
209 return NULL; 211 return NULL;
210 } 212 }
@@ -213,13 +215,13 @@ static void *usbmuxd_client_reader_thread(void *arg)
213 215
214 cdata->reader_dead = 0; 216 cdata->reader_dead = 0;
215 217
216 fprintf(stderr, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); 218 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count);
217 219
218 while (!quit_flag && !cdata->reader_quit) { 220 while (!quit_flag && !cdata->reader_quit) {
219 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT); 221 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT);
220 if (result <= 0) { 222 if (result <= 0) {
221 if (result < 0) { 223 if (result < 0) {
222 fprintf(stderr, "%s: select error: %s\n", __func__, strerror(errno)); 224 if (verbose >= 2) fprintf(stderr, "%s: select error: %s\n", __func__, strerror(errno));
223 } 225 }
224 continue; 226 continue;
225 } 227 }
@@ -227,7 +229,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
227 rlen = 0; 229 rlen = 0;
228 err = iphone_mux_recv_timeout(cdata->muxclient, rbuffer, rbuffersize, &rlen, DEFAULT_TIMEOUT); 230 err = iphone_mux_recv_timeout(cdata->muxclient, rbuffer, rbuffersize, &rlen, DEFAULT_TIMEOUT);
229 if (err != 0) { 231 if (err != 0) {
230 fprintf(stderr, "%s[%d:%d]: encountered USB read error: %d\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, err); 232 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: encountered USB read error: %d\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, err);
231 break; 233 break;
232 } 234 }
233 235
@@ -242,7 +244,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
242 fsync(cdata->socket); 244 fsync(cdata->socket);
243 } 245 }
244 246
245 fprintf(stderr, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); 247 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count);
246 248
247 cdata->reader_dead = 1; 249 cdata->reader_dead = 1;
248 250
@@ -270,21 +272,21 @@ static int usbmuxd_handleConnectResult(struct client_data *cdata)
270 iphone_error_t err; 272 iphone_error_t err;
271 273
272 if (!cdata) { 274 if (!cdata) {
273 fprintf(stderr, "%s: Invalid client_data provided!\n", __func__); 275 if (verbose >= 2) fprintf(stderr, "%s: Invalid client_data provided!\n", __func__);
274 return -EINVAL; 276 return -EINVAL;
275 } 277 }
276 278
277 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT); 279 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT);
278 if (result <= 0) { 280 if (result <= 0) {
279 if (result < 0) { 281 if (result < 0) {
280 fprintf(stderr, "%s: select error: %s\n", __func__, strerror(errno)); 282 if (verbose >= 2) fprintf(stderr, "%s: select error: %s\n", __func__, strerror(errno));
281 return result; 283 return result;
282 } 284 }
283 } else { 285 } else {
284 result = 0; 286 result = 0;
285 err = iphone_mux_recv_timeout(cdata->muxclient, buffer, maxlen, &rlen, 100); 287 err = iphone_mux_recv_timeout(cdata->muxclient, buffer, maxlen, &rlen, 100);
286 if (err != 0) { 288 if (err != 0) {
287 fprintf(stderr, "%s: encountered USB read error: %d\n", __func__, err); 289 if (verbose >= 2) fprintf(stderr, "%s: encountered USB read error: %d\n", __func__, err);
288 usbmuxd_send_result(cdata->socket, cdata->tag, -err); 290 usbmuxd_send_result(cdata->socket, cdata->tag, -err);
289 return err; 291 return err;
290 } else { 292 } else {
@@ -292,7 +294,7 @@ static int usbmuxd_handleConnectResult(struct client_data *cdata)
292 if ((buffer[0] == 1) && (rlen > 20) && !memcmp(buffer+1, "handleConnectResult:", 20)) { 294 if ((buffer[0] == 1) && (rlen > 20) && !memcmp(buffer+1, "handleConnectResult:", 20)) {
293 // hm... we got an error message! 295 // hm... we got an error message!
294 buffer[rlen] = 0; 296 buffer[rlen] = 0;
295 fprintf(stderr, "%s: %s\n", __func__, buffer+22); 297 if (verbose >= 1) fprintf(stderr, "%s: %s\n", __func__, buffer+22);
296 298
297 if (sscanf(buffer+22, "%s - %d\n", err_type, &err_code) == 2) { 299 if (sscanf(buffer+22, "%s - %d\n", err_type, &err_code) == 2) {
298 usbmuxd_send_result(cdata->socket, cdata->tag, err_code); 300 usbmuxd_send_result(cdata->socket, cdata->tag, err_code);
@@ -333,25 +335,26 @@ static void *usbmuxd_client_handler_thread(void *arg)
333 iphone_error_t err; 335 iphone_error_t err;
334 336
335 if (!arg) { 337 if (!arg) {
336 fprintf(stderr, "%s: invalid client_data provided!\n", __func__); 338 if (verbose >= 3) fprintf(stderr, "%s: invalid client_data provided!\n", __func__);
337 return NULL; 339 return NULL;
338 } 340 }
339 341
340 cdata = (struct client_data*)arg; 342 cdata = (struct client_data*)arg;
341 343
342 fprintf(stderr, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id,cdata->duinfo->use_count); 344 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id,cdata->duinfo->use_count);
343 345
344 if (usbmuxd_handleConnectResult(cdata)) { 346 if (usbmuxd_handleConnectResult(cdata)) {
345 fprintf(stderr, "handleConnectResult: Error\n"); 347 if (verbose >= 3) fprintf(stderr, "handleConnectResult: Error\n");
346 goto leave; 348 goto leave;
349 } else {
350 if (verbose >= 3) fprintf(stderr, "handleConnectResult: Success\n");
347 } 351 }
348 fprintf(stderr, "handleConnectResult: Success\n");
349 352
350 // starting mux reader thread 353 // starting mux reader thread
351 cdata->reader_quit = 0; 354 cdata->reader_quit = 0;
352 cdata->reader_dead = 0; 355 cdata->reader_dead = 0;
353 if (pthread_create(&cdata->reader, NULL, usbmuxd_client_reader_thread, cdata) != 0) { 356 if (pthread_create(&cdata->reader, NULL, usbmuxd_client_reader_thread, cdata) != 0) {
354 fprintf(stderr, "%s: could not start client_reader thread\n", __func__); 357 if (verbose >= 2) fprintf(stderr, "%s: could not start client_reader thread\n", __func__);
355 cdata->reader = 0; 358 cdata->reader = 0;
356 } 359 }
357 360
@@ -359,7 +362,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
359 result = check_fd(cdata->socket, FD_READ, DEFAULT_TIMEOUT); 362 result = check_fd(cdata->socket, FD_READ, DEFAULT_TIMEOUT);
360 if (result <= 0) { 363 if (result <= 0) {
361 if (result < 0) { 364 if (result < 0) {
362 fprintf(stderr, "%s: Error: checkfd: %s\n", __func__, strerror(errno)); 365 if (verbose >= 3) fprintf(stderr, "%s: Error: checkfd: %s\n", __func__, strerror(errno));
363 } 366 }
364 continue; 367 continue;
365 } 368 }
@@ -371,7 +374,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
371 break; 374 break;
372 } 375 }
373 if (len < 0) { 376 if (len < 0) {
374 fprintf(stderr, "%s[%d:%d]: Error: recv: %s\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, strerror(errno)); 377 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: Error: recv: %s\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, strerror(errno));
375 break; 378 break;
376 } 379 }
377 380
@@ -384,7 +387,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
384 if (err == IPHONE_E_TIMEOUT) { 387 if (err == IPHONE_E_TIMEOUT) {
385 // some kind of timeout... just be patient and retry. 388 // some kind of timeout... just be patient and retry.
386 } else if (err != IPHONE_E_SUCCESS) { 389 } else if (err != IPHONE_E_SUCCESS) {
387 fprintf(stderr, "%s[%d:%d]: USB write error: %d\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, err); 390 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: USB write error: %d\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, err);
388 len = -1; 391 len = -1;
389 break; 392 break;
390 } 393 }
@@ -402,7 +405,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
402 405
403leave: 406leave:
404 // cleanup 407 // cleanup
405 fprintf(stderr, "%s[%d:%d]: terminating\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); 408 if (verbose >= 3) fprintf(stderr, "%s[%d:%d]: terminating\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count);
406 if (cdata->reader != 0) { 409 if (cdata->reader != 0) {
407 cdata->reader_quit = 1; 410 cdata->reader_quit = 1;
408 pthread_join(cdata->reader, NULL); 411 pthread_join(cdata->reader, NULL);
@@ -410,7 +413,7 @@ leave:
410 413
411 cdata->handler_dead = 1; 414 cdata->handler_dead = 1;
412 415
413 fprintf(stderr, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); 416 if (verbose >= 3) fprintf(stderr, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count);
414 return NULL; 417 return NULL;
415} 418}
416 419
@@ -423,13 +426,13 @@ static void *usbmuxd_bulk_reader_thread(void *arg)
423 struct device_use_info *cur_dev; 426 struct device_use_info *cur_dev;
424 427
425 if (!arg) { 428 if (!arg) {
426 fprintf(stderr, "%s: Invalid client_data provided\n", __func__); 429 if (verbose >= 3) fprintf(stderr, "%s: Invalid client_data provided\n", __func__);
427 return NULL; 430 return NULL;
428 } 431 }
429 432
430 cur_dev = (struct device_use_info*)arg; 433 cur_dev = (struct device_use_info*)arg;
431 434
432 printf("%s: started\n", __func__); 435 if (verbose >= 5) fprintf(stderr, "%s: started\n", __func__);
433 436
434 while (!quit_flag && cur_dev) { 437 while (!quit_flag && cur_dev) {
435 438
@@ -447,7 +450,7 @@ static void *usbmuxd_bulk_reader_thread(void *arg)
447 //} 450 //}
448 } 451 }
449 452
450 printf("%s: terminated\n", __func__); 453 if (verbose >= 5) fprintf(stderr, "%s: terminated\n", __func__);
451 454
452 return NULL; 455 return NULL;
453} 456}
@@ -478,24 +481,24 @@ static void *usbmuxd_client_init_thread(void *arg)
478 struct device_use_info *cur_dev = NULL; 481 struct device_use_info *cur_dev = NULL;
479 482
480 if (!arg) { 483 if (!arg) {
481 fprintf(stderr, "%s: invalid client_data provided!\n", __func__); 484 if (verbose >= 1) fprintf(stderr, "%s: invalid client_data provided!\n", __func__);
482 return NULL; 485 return NULL;
483 } 486 }
484 487
485 cdata = (struct client_data*)arg; 488 cdata = (struct client_data*)arg;
486 cdata->dead = 0; 489 cdata->dead = 0;
487 490
488 fprintf(stderr, "%s: started (fd=%d)\n", __func__, cdata->socket); 491 if (verbose >= 2) fprintf(stderr, "%s: started (fd=%d)\n", __func__, cdata->socket);
489 492
490 if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&s_req, 0)) <= 0) { 493 if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&s_req, 0)) <= 0) {
491 fprintf(stderr, "%s: No Hello packet received, error %s\n", __func__, strerror(errno)); 494 if (verbose >= 2) fprintf(stderr, "%s: No Hello packet received, error %s\n", __func__, strerror(errno));
492 goto leave; 495 goto leave;
493 } 496 }
494 497
495 if ((recv_len == sizeof(struct usbmuxd_scan_request)) && (s_req->header.length == sizeof(struct usbmuxd_scan_request)) 498 if ((recv_len == sizeof(struct usbmuxd_scan_request)) && (s_req->header.length == sizeof(struct usbmuxd_scan_request))
496 && (s_req->header.reserved == 0) && (s_req->header.type == USBMUXD_SCAN)) { 499 && (s_req->header.reserved == 0) && (s_req->header.type == USBMUXD_SCAN)) {
497 // send success response 500 // send success response
498 fprintf(stderr, "%s: Got Hello packet!\n", __func__); 501 if (verbose >= 3) fprintf(stderr, "%s: Got Hello packet!\n", __func__);
499 usbmuxd_send_result(cdata->socket, s_req->header.tag, 0); 502 usbmuxd_send_result(cdata->socket, s_req->header.tag, 0);
500 } else if ((recv_len == sizeof(struct usbmuxd_connect_request)) && (s_req->header.type == USBMUXD_CONNECT)) { 503 } else if ((recv_len == sizeof(struct usbmuxd_connect_request)) && (s_req->header.type == USBMUXD_CONNECT)) {
501 c_req = (struct usbmuxd_connect_request*)s_req; 504 c_req = (struct usbmuxd_connect_request*)s_req;
@@ -503,7 +506,7 @@ static void *usbmuxd_client_init_thread(void *arg)
503 goto connect; 506 goto connect;
504 } else { 507 } else {
505 // send error response and exit 508 // send error response and exit
506 fprintf(stderr, "%s: Invalid Hello packet received.\n", __func__); 509 if (verbose >= 2) fprintf(stderr, "%s: Invalid Hello packet received.\n", __func__);
507 // TODO is this required?! 510 // TODO is this required?!
508 usbmuxd_send_result(cdata->socket, s_req->header.tag, EINVAL); 511 usbmuxd_send_result(cdata->socket, s_req->header.tag, EINVAL);
509 goto leave; 512 goto leave;
@@ -512,14 +515,14 @@ static void *usbmuxd_client_init_thread(void *arg)
512 pthread_mutex_lock(&usb_mutex); 515 pthread_mutex_lock(&usb_mutex);
513 // gather data about all iPhones/iPods attached 516 // gather data about all iPhones/iPods attached
514 517
515 fprintf(stderr, "%s: usb init\n", __func__); 518 if (verbose >= 5) fprintf(stderr, "%s: usb init\n", __func__);
516 usb_init(); 519 usb_init();
517 fprintf(stderr, "%s: usb find busses\n", __func__); 520 if (verbose >= 5) fprintf(stderr, "%s: usb find busses\n", __func__);
518 usb_find_busses(); 521 usb_find_busses();
519 fprintf(stderr, "%s: usb find devices\n", __func__); 522 if (verbose >= 5) fprintf(stderr, "%s: usb find devices\n", __func__);
520 usb_find_devices(); 523 usb_find_devices();
521 524
522 fprintf(stderr, "%s: Looking for attached devices...\n", __func__); 525 if (verbose >= 2) fprintf(stderr, "%s: Looking for attached devices...\n", __func__);
523 526
524 for (bus = usb_get_busses(); bus; bus = bus->next) { 527 for (bus = usb_get_busses(); bus; bus = bus->next) {
525 for (dev = bus->devices; dev; dev = dev->next) { 528 for (dev = bus->devices; dev; dev = dev->next) {
@@ -527,7 +530,7 @@ static void *usbmuxd_client_init_thread(void *arg)
527 && dev->descriptor.idProduct >= 0x1290 530 && dev->descriptor.idProduct >= 0x1290
528 && dev->descriptor.idProduct <= 0x1293) 531 && dev->descriptor.idProduct <= 0x1293)
529 { 532 {
530 fprintf(stderr, "%s: Found device on bus %d, id %d\n", __func__, bus->location, dev->devnum); 533 if (verbose >= 1) fprintf(stderr, "%s: Found device on bus %d, id %d\n", __func__, bus->location, dev->devnum);
531 found++; 534 found++;
532 535
533 // construct packet 536 // construct packet
@@ -548,12 +551,12 @@ static void *usbmuxd_client_init_thread(void *arg)
548 } 551 }
549 552
550#ifdef DEBUG 553#ifdef DEBUG
551 print_buffer((char*)&dev_info_req, sizeof(dev_info_req)); 554 if (verbose >= 4) print_buffer(stderr, (char*)&dev_info_req, sizeof(dev_info_req));
552#endif 555#endif
553 556
554 // send it 557 // send it
555 if (send_buf(cdata->socket, &dev_info_req, sizeof(dev_info_req)) <= 0) { 558 if (send_buf(cdata->socket, &dev_info_req, sizeof(dev_info_req)) <= 0) {
556 fprintf(stderr, "%s: Error: Could not send device info: %s\n", __func__, strerror(errno)); 559 if (verbose >= 3) fprintf(stderr, "%s: Error: Could not send device info: %s\n", __func__, strerror(errno));
557 found--; 560 found--;
558 } 561 }
559 } 562 }
@@ -562,27 +565,27 @@ static void *usbmuxd_client_init_thread(void *arg)
562 pthread_mutex_unlock(&usb_mutex); 565 pthread_mutex_unlock(&usb_mutex);
563 566
564 if (found <= 0) { 567 if (found <= 0) {
565 fprintf(stderr, "%s: No attached iPhone/iPod devices found.\n", __func__); 568 if (verbose >= 1) fprintf(stderr, "%s: No attached iPhone/iPod devices found.\n", __func__);
566 goto leave; 569 goto leave;
567 } 570 }
568 571
569 fprintf(stderr, "%s: Waiting for connect request\n", __func__); 572 if (verbose >= 2) fprintf(stderr, "%s: Waiting for connect request\n", __func__);
570 573
571 // now wait for connect request 574 // now wait for connect request
572 //memset(&c_req, 0, sizeof(c_req)); 575 //memset(&c_req, 0, sizeof(c_req));
573 if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&c_req, 0)) <= 0) { 576 if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&c_req, 0)) <= 0) {
574 fprintf(stderr, "%s: Did not receive any connect request.\n", __func__); 577 if (verbose >= 3) fprintf(stderr, "%s: Did not receive any connect request.\n", __func__);
575 goto leave; 578 goto leave;
576 } 579 }
577 580
578connect: 581connect:
579 582
580 if (c_req->header.type != USBMUXD_CONNECT) { 583 if (c_req->header.type != USBMUXD_CONNECT) {
581 fprintf(stderr, "%s: Unexpected packet of type %d received.\n", __func__, c_req->header.type); 584 if (verbose >= 2) fprintf(stderr, "%s: Unexpected packet of type %d received.\n", __func__, c_req->header.type);
582 goto leave; 585 goto leave;
583 } 586 }
584 587
585 fprintf(stderr, "%s: Setting up connection to usb device #%d on port %d\n", __func__, c_req->device_id, ntohs(c_req->tcp_dport)); 588 if (verbose >= 3) fprintf(stderr, "%s: Setting up connection to usb device #%d on port %d\n", __func__, c_req->device_id, ntohs(c_req->tcp_dport));
586 589
587 // find the device, and open usb connection 590 // find the device, and open usb connection
588 phone = NULL; 591 phone = NULL;
@@ -604,19 +607,19 @@ connect:
604 } 607 }
605 if (!phone) { 608 if (!phone) {
606 // if not found, make a new connection 609 // if not found, make a new connection
607 fprintf(stderr, "%s: creating new usb connection, device_id=%d\n", __func__, c_req->device_id); 610 if (verbose >= 2) fprintf(stderr, "%s: creating new usb connection, device_id=%d\n", __func__, c_req->device_id);
608 611
609 pthread_mutex_lock(&usb_mutex); 612 pthread_mutex_lock(&usb_mutex);
610 if (iphone_get_specific_device(0, c_req->device_id, &phone) != IPHONE_E_SUCCESS) { 613 if (iphone_get_specific_device(0, c_req->device_id, &phone) != IPHONE_E_SUCCESS) {
611 pthread_mutex_unlock(&usb_mutex); 614 pthread_mutex_unlock(&usb_mutex);
612 fprintf(stderr, "%s: device_id %d could not be opened\n", __func__, c_req->device_id); 615 if (verbose >= 1) fprintf(stderr, "%s: device_id %d could not be opened\n", __func__, c_req->device_id);
613 usbmuxd_send_result(cdata->socket, c_req->header.tag, ENODEV); 616 usbmuxd_send_result(cdata->socket, c_req->header.tag, ENODEV);
614 goto leave; 617 goto leave;
615 } 618 }
616 pthread_mutex_unlock(&usb_mutex); 619 pthread_mutex_unlock(&usb_mutex);
617 620
618 // add to device list 621 // add to device list
619 fprintf(stderr, "%s: add to device list\n", __func__); 622 if (verbose >= 3) fprintf(stderr, "%s: add to device list\n", __func__);
620 cur_dev = (struct device_use_info*)malloc(sizeof(struct device_use_info)); 623 cur_dev = (struct device_use_info*)malloc(sizeof(struct device_use_info));
621 memset(cur_dev, 0, sizeof(struct device_use_info)); 624 memset(cur_dev, 0, sizeof(struct device_use_info));
622 cur_dev->use_count = 1; 625 cur_dev->use_count = 1;
@@ -625,7 +628,7 @@ connect:
625 pthread_mutex_init(&cur_dev->mutex, NULL); 628 pthread_mutex_init(&cur_dev->mutex, NULL);
626 pthread_mutex_init(&cur_dev->writer_mutex, NULL); 629 pthread_mutex_init(&cur_dev->writer_mutex, NULL);
627 630
628 fprintf(stderr, "%s: device_use_count = %d\n", __func__, device_use_count); 631 if (verbose >= 3) fprintf(stderr, "%s: device_use_count = %d\n", __func__, device_use_count);
629 pthread_create(&cur_dev->bulk_reader, NULL, usbmuxd_bulk_reader_thread, cur_dev); 632 pthread_create(&cur_dev->bulk_reader, NULL, usbmuxd_bulk_reader_thread, cur_dev);
630 633
631 pthread_mutex_lock(&usbmux_mutex); 634 pthread_mutex_lock(&usbmux_mutex);
@@ -636,7 +639,7 @@ connect:
636 } 639 }
637 pthread_mutex_unlock(&usbmux_mutex); 640 pthread_mutex_unlock(&usbmux_mutex);
638 } else { 641 } else {
639 fprintf(stderr, "%s: reusing usb connection, device_id=%d\n", __func__, c_req->device_id); 642 if (verbose >= 3) fprintf(stderr, "%s: reusing usb connection, device_id=%d\n", __func__, c_req->device_id);
640 } 643 }
641 644
642 // setup connection to iPhone/iPod 645 // setup connection to iPhone/iPod
@@ -646,7 +649,7 @@ connect:
646 649
647 if (res != 0) { 650 if (res != 0) {
648 usbmuxd_send_result(cdata->socket, c_req->header.tag, res); 651 usbmuxd_send_result(cdata->socket, c_req->header.tag, res);
649 fprintf(stderr, "%s: mux_new_client returned %d, aborting.\n", __func__, res); 652 if (verbose >= 1) fprintf(stderr, "%s: mux_new_client returned %d, aborting.\n", __func__, res);
650 goto leave; 653 goto leave;
651 } 654 }
652 655
@@ -655,7 +658,7 @@ connect:
655 cdata->tag = c_req->header.tag; 658 cdata->tag = c_req->header.tag;
656 cdata->duinfo = cur_dev; 659 cdata->duinfo = cur_dev;
657 if (pthread_create(&cdata->handler, NULL, usbmuxd_client_handler_thread, cdata) != 0) { 660 if (pthread_create(&cdata->handler, NULL, usbmuxd_client_handler_thread, cdata) != 0) {
658 fprintf(stderr, "%s: could not create usbmuxd_client_handler_thread!\n", __func__); 661 if (verbose >= 3) fprintf(stderr, "%s: could not create usbmuxd_client_handler_thread!\n", __func__);
659 cdata->handler = 0; 662 cdata->handler = 0;
660 goto leave; 663 goto leave;
661 } 664 }
@@ -688,7 +691,7 @@ connect:
688 pthread_join(cdata->handler, NULL); 691 pthread_join(cdata->handler, NULL);
689 } 692 }
690 693
691 fprintf(stderr, "%s: closing connection\n", __func__); 694 if (verbose >= 2) fprintf(stderr, "%s: closing connection\n", __func__);
692 695
693 // time to clean up 696 // time to clean up
694 if (cdata && cdata->muxclient) { // should be non-NULL 697 if (cdata && cdata->muxclient) { // should be non-NULL
@@ -696,7 +699,7 @@ connect:
696 } 699 }
697 700
698leave: 701leave:
699 fprintf(stderr, "%s: terminating\n", __func__); 702 if (verbose >= 2) fprintf(stderr, "%s: terminating\n", __func__);
700 703
701 if (s_req) { 704 if (s_req) {
702 free(s_req); 705 free(s_req);
@@ -710,11 +713,11 @@ leave:
710 if (cur_dev) { 713 if (cur_dev) {
711 pthread_mutex_lock(&cur_dev->mutex); 714 pthread_mutex_lock(&cur_dev->mutex);
712 if (cur_dev->use_count > 1) { 715 if (cur_dev->use_count > 1) {
713 printf("%s: decreasing device use count\n", __func__); 716 if (verbose >= 5) fprintf(stderr, "%s: decreasing device use count\n", __func__);
714 cur_dev->use_count--; 717 cur_dev->use_count--;
715 pthread_mutex_unlock(&cur_dev->mutex); 718 pthread_mutex_unlock(&cur_dev->mutex);
716 } else { 719 } else {
717 printf("%s: last client disconnected, cleaning up\n", __func__); 720 if (verbose >= 3) fprintf(stderr, "%s: last client disconnected, cleaning up\n", __func__);
718 cur_dev->use_count = 0; 721 cur_dev->use_count = 0;
719 pthread_mutex_unlock(&cur_dev->mutex); 722 pthread_mutex_unlock(&cur_dev->mutex);
720 pthread_join(cur_dev->bulk_reader, NULL); 723 pthread_join(cur_dev->bulk_reader, NULL);
@@ -751,7 +754,7 @@ leave:
751 cdata->dead = 1; 754 cdata->dead = 1;
752 close(cdata->socket); 755 close(cdata->socket);
753 756
754 fprintf(stderr, "%s: terminated\n", __func__); 757 if (verbose >= 5) fprintf(stderr, "%s: terminated\n", __func__);
755 758
756 return NULL; 759 return NULL;
757} 760}
@@ -771,7 +774,7 @@ static int daemonize()
771static void clean_exit(int sig) 774static void clean_exit(int sig)
772{ 775{
773 if (sig == SIGINT) { 776 if (sig == SIGINT) {
774 fprintf(stderr, "CTRL+C pressed\n"); 777 if (verbose >= 1) fprintf(stderr, "CTRL+C pressed\n");
775 } 778 }
776 quit_flag = 1; 779 quit_flag = 1;
777} 780}
@@ -787,17 +790,23 @@ int main(int argc, char **argv)
787 struct client_data *cdata = NULL; 790 struct client_data *cdata = NULL;
788 struct client_data **children = NULL; 791 struct client_data **children = NULL;
789 int children_capacity = DEFAULT_CHILDREN_CAPACITY; 792 int children_capacity = DEFAULT_CHILDREN_CAPACITY;
790 int i = 0; 793 int i;
791 int result = 0; 794 int result = 0;
792 int cnt = 0; 795 int cnt = 0;
793 796
794 fprintf(stderr, "usbmuxd: starting\n"); 797 for (i = 1; i < argc; i++) {
798 if (argv[i] != NULL && (!strncmp("-v", argv[i], 2) || !strncmp("--verbose", argv[i], 10))) {
799 sock_stuff_set_verbose(++verbose);
800 }
801 }
802
803 if (verbose >= 2) fprintf(stderr, "usbmuxd: starting\n");
795 804
796 // TODO: Parameter checking. 805 // TODO: Parameter checking.
797 806
798 fsock = create_unix_socket(USBMUXD_SOCKET_FILE); 807 fsock = create_unix_socket(USBMUXD_SOCKET_FILE);
799 if (fsock < 0) { 808 if (fsock < 0) {
800 fprintf(stderr, "Could not create socket, exiting\n"); 809 if (verbose >= 1) fprintf(stderr, "Could not create socket, exiting\n");
801 return -1; 810 return -1;
802 } 811 }
803 812
@@ -819,12 +828,12 @@ int main(int argc, char **argv)
819 // buffer gets enlarged later. 828 // buffer gets enlarged later.
820 children = (struct client_data**)malloc(sizeof(struct client_data*) * children_capacity); 829 children = (struct client_data**)malloc(sizeof(struct client_data*) * children_capacity);
821 if (!children) { 830 if (!children) {
822 fprintf(stderr, "usbmuxd: Out of memory when allocating memory for child threads. Terminating.\n"); 831 if (verbose >= 2) fprintf(stderr, "usbmuxd: Out of memory when allocating memory for child threads. Terminating.\n");
823 exit(EXIT_FAILURE); 832 exit(EXIT_FAILURE);
824 } 833 }
825 memset(children, 0, sizeof(struct client_data*) * children_capacity); 834 memset(children, 0, sizeof(struct client_data*) * children_capacity);
826 835
827 fprintf(stderr, "usbmuxd: waiting for connection\n"); 836 if (verbose >= 2) fprintf(stderr, "usbmuxd: waiting for connection\n");
828 while (!quit_flag) { 837 while (!quit_flag) {
829 // Check the file descriptor before accepting a connection. 838 // Check the file descriptor before accepting a connection.
830 // If no connection attempt is made, just repeat... 839 // If no connection attempt is made, just repeat...
@@ -836,7 +845,7 @@ int main(int argc, char **argv)
836 if (children[i]) { 845 if (children[i]) {
837 if (children[i]->dead != 0) { 846 if (children[i]->dead != 0) {
838 pthread_join(children[i]->thread, NULL); 847 pthread_join(children[i]->thread, NULL);
839 fprintf(stderr, "usbmuxd: reclaimed client thread (fd=%d)\n", children[i]->socket); 848 if (verbose >= 3) fprintf(stderr, "usbmuxd: reclaimed client thread (fd=%d)\n", children[i]->socket);
840 free(children[i]); 849 free(children[i]);
841 children[i] = NULL; 850 children[i] = NULL;
842 cnt++; 851 cnt++;
@@ -855,7 +864,7 @@ int main(int argc, char **argv)
855 } 864 }
856 continue; 865 continue;
857 } else { 866 } else {
858 fprintf(stderr, "usbmuxd: select error: %s\n", strerror(errno)); 867 if (verbose >= 3) fprintf(stderr, "usbmuxd: select error: %s\n", strerror(errno));
859 continue; 868 continue;
860 } 869 }
861 } 870 }
@@ -864,7 +873,7 @@ int main(int argc, char **argv)
864 memset(cdata, 0, sizeof(struct client_data)); 873 memset(cdata, 0, sizeof(struct client_data));
865 if (!cdata) { 874 if (!cdata) {
866 quit_flag = 1; 875 quit_flag = 1;
867 fprintf(stderr, "usbmuxd: Error: Out of memory! Terminating.\n"); 876 if (verbose >= 1) fprintf(stderr, "usbmuxd: Error: Out of memory! Terminating.\n");
868 break; 877 break;
869 } 878 }
870 879
@@ -874,12 +883,12 @@ int main(int argc, char **argv)
874 if (errno == EINTR) { 883 if (errno == EINTR) {
875 continue; 884 continue;
876 } else { 885 } else {
877 fprintf(stderr, "usbmuxd: Error in accept: %s\n", strerror(errno)); 886 if (verbose >= 3) fprintf(stderr, "usbmuxd: Error in accept: %s\n", strerror(errno));
878 continue; 887 continue;
879 } 888 }
880 } 889 }
881 890
882 fprintf(stderr, "usbmuxd: new client connected (fd=%d)\n", cdata->socket); 891 if (verbose >= 1) fprintf(stderr, "usbmuxd: new client connected (fd=%d)\n", cdata->socket);
883 892
884 // create client thread: 893 // create client thread:
885 if (pthread_create(&cdata->thread, NULL, usbmuxd_client_init_thread, cdata) == 0) { 894 if (pthread_create(&cdata->thread, NULL, usbmuxd_client_init_thread, cdata) == 0) {
@@ -891,22 +900,22 @@ int main(int argc, char **argv)
891 children_capacity++; 900 children_capacity++;
892 children = realloc(children, sizeof(struct client_data*) * children_capacity); 901 children = realloc(children, sizeof(struct client_data*) * children_capacity);
893 if (!children) { 902 if (!children) {
894 fprintf(stderr, "usbmuxd: Out of memory when enlarging child thread buffer\n"); 903 if (verbose >= 1) fprintf(stderr, "usbmuxd: Out of memory when enlarging child thread buffer\n");
895 } 904 }
896 } 905 }
897 children[i] = cdata; 906 children[i] = cdata;
898 } else { 907 } else {
899 fprintf(stderr, "usbmuxd: Failed to create client_init_thread.\n"); 908 if (verbose >= 3) fprintf(stderr, "usbmuxd: Failed to create client_init_thread.\n");
900 close(cdata->socket); 909 close(cdata->socket);
901 free(cdata); 910 free(cdata);
902 cdata = NULL; 911 cdata = NULL;
903 } 912 }
904 } 913 }
905 914
906 fprintf(stderr, "usbmuxd: terminating\n"); 915 if (verbose >= 3) fprintf(stderr, "usbmuxd: terminating\n");
907 916
908 // preparing for shutdown: wait for child threads to terminate (if any) 917 // preparing for shutdown: wait for child threads to terminate (if any)
909 fprintf(stderr, "usbmuxd: waiting for child threads to terminate...\n"); 918 if (verbose >= 2) fprintf(stderr, "usbmuxd: waiting for child threads to terminate...\n");
910 for (i = 0; i < children_capacity; i++) { 919 for (i = 0; i < children_capacity; i++) {
911 if (children[i] != NULL) { 920 if (children[i] != NULL) {
912 pthread_join(children[i]->thread, NULL); 921 pthread_join(children[i]->thread, NULL);
@@ -925,7 +934,7 @@ int main(int argc, char **argv)
925 934
926 unlink(USBMUXD_SOCKET_FILE); 935 unlink(USBMUXD_SOCKET_FILE);
927 936
928 fprintf(stderr, "usbmuxd: terminated\n"); 937 if (verbose >= 1) fprintf(stderr, "usbmuxd: terminated\n");
929 938
930 return 0; 939 return 0;
931} 940}