summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2009-04-14 13:15:10 +0200
committerGravatar Nikias Bassen2009-04-14 13:15:10 +0200
commita5245005b41cbae1ccee805fbef8d3e343c9b312 (patch)
treee0f33650370132efc5d909ca032887690f2cf302 /main.c
parent9932dadfc7ddc65ccb3d2988df1a6dcc9cf536c5 (diff)
downloadusbmuxd-a5245005b41cbae1ccee805fbef8d3e343c9b312.tar.gz
usbmuxd-a5245005b41cbae1ccee805fbef8d3e343c9b312.tar.bz2
Logging and debugging refined.
Diffstat (limited to 'main.c')
-rw-r--r--main.c190
1 files changed, 99 insertions, 91 deletions
diff --git a/main.c b/main.c
index eba154e..157ecbb 100644
--- a/main.c
+++ b/main.c
@@ -49,6 +49,8 @@
49 49
50#define LOCKFILE "/var/run/usbmuxd.lock" 50#define LOCKFILE "/var/run/usbmuxd.lock"
51 51
52#define THREAD (unsigned int)pthread_self()
53
52static int quit_flag = 0; 54static int quit_flag = 0;
53static int fsock = -1; 55static int fsock = -1;
54static int verbose = DEBUG_LEVEL; 56static int verbose = DEBUG_LEVEL;
@@ -86,6 +88,44 @@ static int device_count = 0;
86static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER; 88static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER;
87static pthread_mutex_t usb_mutex = PTHREAD_MUTEX_INITIALIZER; 89static pthread_mutex_t usb_mutex = PTHREAD_MUTEX_INITIALIZER;
88 90
91/**
92 * logs a message to syslog when running as daemon or to stdout/stderr when
93 * running in foreground.
94 * @param prio The logging priority.
95 * @param format The message to be printed.
96 */
97static void logmsg(int prio, char *format, ...)
98{
99 va_list args;
100 va_start(args, format);
101
102 if (!foreground) {
103 // daemon. log using syslog.
104 vsyslog(prio, format, args);
105 } else {
106 // running in foreground. log to stdout/stderr.
107 char msgbuf[256];
108 FILE *lfp = stdout;
109 switch(prio) {
110 case LOG_EMERG:
111 case LOG_ALERT:
112 case LOG_CRIT:
113 case LOG_ERR:
114 case LOG_WARNING:
115 lfp = stderr;
116 break;
117 default:
118 lfp = stdout;
119 }
120 strcpy(msgbuf, "usbmuxd: ");
121 vsnprintf(msgbuf+9, 244, format, args);
122 strcat(msgbuf, "\n");
123 fputs(msgbuf, lfp);
124 }
125
126 va_end(args);
127}
128
89#ifdef DEBUG 129#ifdef DEBUG
90/** 130/**
91 * for debugging purposes. 131 * for debugging purposes.
@@ -147,18 +187,18 @@ static int usbmuxd_get_request(int fd, void **data, size_t len)
147 *data = malloc(pktlen); 187 *data = malloc(pktlen);
148 } else if (len < pktlen) { 188 } else if (len < pktlen) {
149 // target buffer is to small to hold this packet! fix it! 189 // target buffer is to small to hold this packet! fix it!
150 if (verbose >= 2) fprintf(stderr, "%s: WARNING -- packet (%d) is larger than target buffer (%d)! Truncating.\n", __func__, pktlen, len); 190 if (verbose >= 2) logmsg(LOG_WARNING, "%s: WARNING -- packet (%d) is larger than target buffer (%d)! Truncating.", __func__, pktlen, len);
151 pktlen = len; 191 pktlen = len;
152 } 192 }
153 193
154 recv_len = recv_buf(fd, *data, pktlen); 194 recv_len = recv_buf(fd, *data, pktlen);
155 if ((recv_len > 0) && (recv_len < pktlen)) { 195 if ((recv_len > 0) && (recv_len < pktlen)) {
156 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); 196 if (verbose >= 2) logmsg(LOG_WARNING, "%s: Uh-oh, we got less than the packet's size, %d instead of %d...", __func__, recv_len, pktlen);
157 } 197 }
158 198
159#ifdef DEBUG 199#ifdef DEBUG
160 if (*data && (recv_len > 0) && verbose >= 4) { 200 if (*data && (recv_len > 0) && verbose >= 4) {
161 if (verbose >= 3) fprintf(stderr, "%s: received:\n", __func__); 201 fprintf(stderr, "%s: received:\n", __func__);
162 print_buffer(stderr, *data, recv_len); 202 print_buffer(stderr, *data, recv_len);
163 } 203 }
164#endif 204#endif
@@ -186,7 +226,7 @@ static int usbmuxd_send_result(int fd, uint32_t tag, uint32_t result_code)
186 res.header.tag = tag; 226 res.header.tag = tag;
187 res.result = result_code; 227 res.result = result_code;
188 228
189 if (verbose >= 4) fprintf(stderr, "%s: tag=%d result=%d\n", __func__, res.header.tag, res.result); 229 if (verbose >= 4) logmsg(LOG_NOTICE, "%s: tag=%d result=%d", __func__, res.header.tag, res.result);
190 230
191 ret = send_buf(fd, &res, sizeof(res)); 231 ret = send_buf(fd, &res, sizeof(res));
192 fsync(fd); // let's get it sent 232 fsync(fd); // let's get it sent
@@ -214,7 +254,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
214 int result; 254 int result;
215 255
216 if (!arg) { 256 if (!arg) {
217 if (verbose >= 2) fprintf(stderr, "%s: invalid client_data supplied!\n", __func__); 257 if (verbose >= 2) logmsg(LOG_ERR, "%s: invalid client_data supplied!", __func__);
218 cdata->reader_dead = 1; 258 cdata->reader_dead = 1;
219 return NULL; 259 return NULL;
220 } 260 }
@@ -223,13 +263,13 @@ static void *usbmuxd_client_reader_thread(void *arg)
223 263
224 cdata->reader_dead = 0; 264 cdata->reader_dead = 0;
225 265
226 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: started\n", __func__, cdata->dev->device_id, cdata->dev->use_count); 266 if (verbose >= 3) logmsg(LOG_NOTICE, "%s[%d:%d]: started", __func__, cdata->dev->device_id, cdata->dev->use_count);
227 267
228 while (!quit_flag && !cdata->reader_quit) { 268 while (!quit_flag && !cdata->reader_quit) {
229 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT); 269 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT);
230 if (result <= 0) { 270 if (result <= 0) {
231 if (result < 0) { 271 if (result < 0) {
232 if (verbose >= 2) fprintf(stderr, "%s: select error: %s\n", __func__, strerror(errno)); 272 if (verbose >= 2) logmsg(LOG_ERR, "%s: select error: %s", __func__, strerror(errno));
233 } 273 }
234 continue; 274 continue;
235 } 275 }
@@ -237,7 +277,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
237 rlen = 0; 277 rlen = 0;
238 err = iphone_mux_recv_timeout(cdata->muxclient, rbuffer, rbuffersize, &rlen, DEFAULT_TIMEOUT); 278 err = iphone_mux_recv_timeout(cdata->muxclient, rbuffer, rbuffersize, &rlen, DEFAULT_TIMEOUT);
239 if (err != 0) { 279 if (err != 0) {
240 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: encountered USB read error: %d\n", __func__, cdata->dev->device_id, cdata->dev->use_count, err); 280 if (verbose >= 2) logmsg(LOG_ERR, "%s[%d:%d]: encountered USB read error: %d", __func__, cdata->dev->device_id, cdata->dev->use_count, err);
241 break; 281 break;
242 } 282 }
243 283
@@ -245,7 +285,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
245 while (rlen > 0) { 285 while (rlen > 0) {
246 len = send_buf(cdata->socket, cursor, rlen); 286 len = send_buf(cdata->socket, cursor, rlen);
247 if (len <= 0) { 287 if (len <= 0) {
248 fprintf(stderr, "Error: send returned %d\n", len); 288 logmsg(LOG_ERR, "%s: Error: send returned %d", __func__, len);
249 err = 1; 289 err = 1;
250 break; 290 break;
251 } 291 }
@@ -255,13 +295,13 @@ static void *usbmuxd_client_reader_thread(void *arg)
255 cursor += len; 295 cursor += len;
256 } 296 }
257 if (err != 0) { 297 if (err != 0) {
258 fprintf(stderr, "Error when writing to client...\n"); 298 logmsg(LOG_ERR, "%s: Error when writing to client...", __func__);
259 break; 299 break;
260 } 300 }
261 fsync(cdata->socket); 301 fsync(cdata->socket);
262 } 302 }
263 303
264 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: terminated\n", __func__, cdata->dev->device_id, cdata->dev->use_count); 304 if (verbose >= 3) logmsg(LOG_NOTICE, "%s[%d:%d]: terminated", __func__, cdata->dev->device_id, cdata->dev->use_count);
265 305
266 cdata->reader_dead = 1; 306 cdata->reader_dead = 1;
267 307
@@ -289,21 +329,21 @@ static int usbmuxd_handleConnectResult(struct client_data *cdata)
289 iphone_error_t err; 329 iphone_error_t err;
290 330
291 if (!cdata) { 331 if (!cdata) {
292 if (verbose >= 2) fprintf(stderr, "%s: Invalid client_data provided!\n", __func__); 332 if (verbose >= 2) logmsg(LOG_ERR, "%s: Invalid client_data provided!", __func__);
293 return -EINVAL; 333 return -EINVAL;
294 } 334 }
295 335
296 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT); 336 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT);
297 if (result <= 0) { 337 if (result <= 0) {
298 if (result < 0) { 338 if (result < 0) {
299 if (verbose >= 2) fprintf(stderr, "%s: select error: %s\n", __func__, strerror(errno)); 339 if (verbose >= 2) logmsg(LOG_ERR, "%s: select error: %s", __func__, strerror(errno));
300 return result; 340 return result;
301 } 341 }
302 } else { 342 } else {
303 result = 0; 343 result = 0;
304 err = iphone_mux_recv_timeout(cdata->muxclient, buffer, maxlen, &rlen, 100); 344 err = iphone_mux_recv_timeout(cdata->muxclient, buffer, maxlen, &rlen, 100);
305 if (err != 0) { 345 if (err != 0) {
306 if (verbose >= 2) fprintf(stderr, "%s: encountered USB read error: %d\n", __func__, err); 346 if (verbose >= 2) logmsg(LOG_ERR, "%s: encountered USB read error: %d", __func__, err);
307 usbmuxd_send_result(cdata->socket, cdata->tag, -err); 347 usbmuxd_send_result(cdata->socket, cdata->tag, -err);
308 return err; 348 return err;
309 } else { 349 } else {
@@ -311,7 +351,7 @@ static int usbmuxd_handleConnectResult(struct client_data *cdata)
311 if ((buffer[0] == 1) && (rlen > 20) && !memcmp(buffer+1, "handleConnectResult:", 20)) { 351 if ((buffer[0] == 1) && (rlen > 20) && !memcmp(buffer+1, "handleConnectResult:", 20)) {
312 // hm... we got an error message! 352 // hm... we got an error message!
313 buffer[rlen] = 0; 353 buffer[rlen] = 0;
314 if (verbose >= 1) fprintf(stderr, "%s: %s\n", __func__, buffer+22); 354 if (verbose >= 1) logmsg(LOG_ERR, "%s: %s\n", __func__, buffer+22);
315 355
316 if (sscanf(buffer+22, "%s - %d\n", err_type, &err_code) == 2) { 356 if (sscanf(buffer+22, "%s - %d\n", err_type, &err_code) == 2) {
317 usbmuxd_send_result(cdata->socket, cdata->tag, err_code); 357 usbmuxd_send_result(cdata->socket, cdata->tag, err_code);
@@ -352,26 +392,26 @@ static void *usbmuxd_client_handler_thread(void *arg)
352 iphone_error_t err; 392 iphone_error_t err;
353 393
354 if (!arg) { 394 if (!arg) {
355 if (verbose >= 3) fprintf(stderr, "%s: invalid client_data provided!\n", __func__); 395 if (verbose >= 2) logmsg(LOG_ERR, "%s: invalid client_data provided!", __func__);
356 return NULL; 396 return NULL;
357 } 397 }
358 398
359 cdata = (struct client_data*)arg; 399 cdata = (struct client_data*)arg;
360 400
361 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: started\n", __func__, cdata->dev->device_id,cdata->dev->use_count); 401 if (verbose >= 3) logmsg(LOG_NOTICE, "%s[%d:%d]: started", __func__, cdata->dev->device_id,cdata->dev->use_count);
362 402
363 if (usbmuxd_handleConnectResult(cdata)) { 403 if (usbmuxd_handleConnectResult(cdata)) {
364 if (verbose >= 3) fprintf(stderr, "handleConnectResult: Error\n"); 404 if (verbose >= 3) logmsg(LOG_ERR, "handleConnectResult: Error");
365 goto leave; 405 goto leave;
366 } else { 406 } else {
367 if (verbose >= 3) fprintf(stderr, "handleConnectResult: Success\n"); 407 if (verbose >= 3) logmsg(LOG_NOTICE, "handleConnectResult: Success");
368 } 408 }
369 409
370 // starting mux reader thread 410 // starting mux reader thread
371 cdata->reader_quit = 0; 411 cdata->reader_quit = 0;
372 cdata->reader_dead = 0; 412 cdata->reader_dead = 0;
373 if (pthread_create(&cdata->reader, NULL, usbmuxd_client_reader_thread, cdata) != 0) { 413 if (pthread_create(&cdata->reader, NULL, usbmuxd_client_reader_thread, cdata) != 0) {
374 if (verbose >= 2) fprintf(stderr, "%s: could not start client_reader thread\n", __func__); 414 if (verbose >= 2) logmsg(LOG_ERR, "%s: could not start client_reader thread", __func__);
375 cdata->reader = 0; 415 cdata->reader = 0;
376 } 416 }
377 417
@@ -379,7 +419,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
379 result = check_fd(cdata->socket, FD_READ, DEFAULT_TIMEOUT); 419 result = check_fd(cdata->socket, FD_READ, DEFAULT_TIMEOUT);
380 if (result <= 0) { 420 if (result <= 0) {
381 if (result < 0) { 421 if (result < 0) {
382 if (verbose >= 3) fprintf(stderr, "%s: Error: checkfd: %s\n", __func__, strerror(errno)); 422 if (verbose >= 3) logmsg(LOG_ERR, "%s: Error: checkfd: %s", __func__, strerror(errno));
383 } 423 }
384 continue; 424 continue;
385 } 425 }
@@ -391,7 +431,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
391 break; 431 break;
392 } 432 }
393 if (len < 0) { 433 if (len < 0) {
394 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: Error: recv: %s\n", __func__, cdata->dev->device_id, cdata->dev->use_count, strerror(errno)); 434 if (verbose >= 2) logmsg(LOG_ERR, "%s[%d:%d]: Error: recv: %s", __func__, cdata->dev->device_id, cdata->dev->use_count, strerror(errno));
395 break; 435 break;
396 } 436 }
397 437
@@ -404,7 +444,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
404 if (err == IPHONE_E_TIMEOUT) { 444 if (err == IPHONE_E_TIMEOUT) {
405 // some kind of timeout... just be patient and retry. 445 // some kind of timeout... just be patient and retry.
406 } else if (err != IPHONE_E_SUCCESS) { 446 } else if (err != IPHONE_E_SUCCESS) {
407 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: USB write error: %d\n", __func__, cdata->dev->device_id, cdata->dev->use_count, err); 447 if (verbose >= 2) logmsg(LOG_ERR, "%s[%d:%d]: USB write error: %d", __func__, cdata->dev->device_id, cdata->dev->use_count, err);
408 len = -1; 448 len = -1;
409 break; 449 break;
410 } 450 }
@@ -422,7 +462,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
422 462
423leave: 463leave:
424 // cleanup 464 // cleanup
425 if (verbose >= 3) fprintf(stderr, "%s[%d:%d]: terminating\n", __func__, cdata->dev->device_id, cdata->dev->use_count); 465 if (verbose >= 3) logmsg(LOG_NOTICE, "%s[%d:%d]: terminating", __func__, cdata->dev->device_id, cdata->dev->use_count);
426 if (cdata->reader != 0) { 466 if (cdata->reader != 0) {
427 cdata->reader_quit = 1; 467 cdata->reader_quit = 1;
428 pthread_join(cdata->reader, NULL); 468 pthread_join(cdata->reader, NULL);
@@ -430,7 +470,7 @@ leave:
430 470
431 cdata->handler_dead = 1; 471 cdata->handler_dead = 1;
432 472
433 if (verbose >= 3) fprintf(stderr, "%s[%d:%d]: terminated\n", __func__, cdata->dev->device_id, cdata->dev->use_count); 473 if (verbose >= 3) logmsg(LOG_NOTICE, "%s[%d:%d]: terminated", __func__, cdata->dev->device_id, cdata->dev->use_count);
434 return NULL; 474 return NULL;
435} 475}
436 476
@@ -444,13 +484,13 @@ static void *usbmuxd_bulk_reader_thread(void *arg)
444 int err; 484 int err;
445 485
446 if (!arg) { 486 if (!arg) {
447 if (verbose >= 3) fprintf(stderr, "%s: Invalid client_data provided\n", __func__); 487 if (verbose >= 2) logmsg(LOG_ERR, "%s: Invalid client_data provided", __func__);
448 return NULL; 488 return NULL;
449 } 489 }
450 490
451 cur_dev = (struct device_info*)arg; 491 cur_dev = (struct device_info*)arg;
452 492
453 if (verbose >= 5) fprintf(stderr, "%s: started\n", __func__); 493 if (verbose >= 3) logmsg(LOG_NOTICE, "%s: started", __func__);
454 494
455 while (!quit_flag && cur_dev) { 495 while (!quit_flag && cur_dev) {
456 496
@@ -462,12 +502,12 @@ static void *usbmuxd_bulk_reader_thread(void *arg)
462 pthread_mutex_unlock(&cur_dev->mutex); 502 pthread_mutex_unlock(&cur_dev->mutex);
463 503
464 if ((err = iphone_mux_pullbulk(cur_dev->phone)) < 0) { 504 if ((err = iphone_mux_pullbulk(cur_dev->phone)) < 0) {
465 if (verbose >= 1) fprintf(stderr, "%s: error %d when reading from device\n", __func__, err); 505 if (verbose >= 1) logmsg(LOG_ERR, "%s: error %d when reading from device", __func__, err);
466 break; 506 break;
467 } 507 }
468 } 508 }
469 509
470 if (verbose >= 0) fprintf(stderr, "%s: terminated\n", __func__); 510 if (verbose >= 3) logmsg(LOG_NOTICE, "%s: terminated", __func__);
471 511
472 return NULL; 512 return NULL;
473} 513}
@@ -498,24 +538,24 @@ static void *usbmuxd_client_init_thread(void *arg)
498 struct device_info *cur_dev = NULL; 538 struct device_info *cur_dev = NULL;
499 539
500 if (!arg) { 540 if (!arg) {
501 if (verbose >= 1) fprintf(stderr, "%s: invalid client_data provided!\n", __func__); 541 if (verbose >= 1) logmsg(LOG_ERR, "%s[%x]: invalid client_data provided!", __func__, THREAD);
502 return NULL; 542 return NULL;
503 } 543 }
504 544
505 cdata = (struct client_data*)arg; 545 cdata = (struct client_data*)arg;
506 cdata->dead = 0; 546 cdata->dead = 0;
507 547
508 if (verbose >= 2) fprintf(stderr, "%s: started (fd=%d)\n", __func__, cdata->socket); 548 if (verbose >= 3) logmsg(LOG_NOTICE, "%s[%x]: started (fd=%d)", __func__, THREAD, cdata->socket);
509 549
510 if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&s_req, 0)) <= 0) { 550 if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&s_req, 0)) <= 0) {
511 if (verbose >= 2) fprintf(stderr, "%s: No scan packet received, error %s\n", __func__, strerror(errno)); 551 if (verbose >= 2) logmsg(LOG_ERR, "%s[%x]: No scan packet received, error %s", __func__, THREAD, strerror(errno));
512 goto leave; 552 goto leave;
513 } 553 }
514 554
515 if ((recv_len == sizeof(struct usbmuxd_scan_request)) && (s_req->header.length == sizeof(struct usbmuxd_scan_request)) 555 if ((recv_len == sizeof(struct usbmuxd_scan_request)) && (s_req->header.length == sizeof(struct usbmuxd_scan_request))
516 && (s_req->header.reserved == 0) && (s_req->header.type == USBMUXD_SCAN)) { 556 && (s_req->header.reserved == 0) && (s_req->header.type == USBMUXD_SCAN)) {
517 // send success response 557 // send success response
518 if (verbose >= 3) fprintf(stderr, "%s: Got scan packet!\n", __func__); 558 if (verbose >= 3) logmsg(LOG_NOTICE, "%s[%x]: Got scan packet!", __func__, THREAD);
519 usbmuxd_send_result(cdata->socket, s_req->header.tag, 0); 559 usbmuxd_send_result(cdata->socket, s_req->header.tag, 0);
520 } else if ((recv_len == sizeof(struct usbmuxd_connect_request)) && (s_req->header.type == USBMUXD_CONNECT)) { 560 } else if ((recv_len == sizeof(struct usbmuxd_connect_request)) && (s_req->header.type == USBMUXD_CONNECT)) {
521 c_req = (struct usbmuxd_connect_request*)s_req; 561 c_req = (struct usbmuxd_connect_request*)s_req;
@@ -523,7 +563,7 @@ static void *usbmuxd_client_init_thread(void *arg)
523 goto connect; 563 goto connect;
524 } else { 564 } else {
525 // send error response and exit 565 // send error response and exit
526 if (verbose >= 2) fprintf(stderr, "%s: Invalid scan packet received.\n", __func__); 566 if (verbose >= 2) logmsg(LOG_ERR, "%s[%x]: Invalid scan packet received.", __func__, THREAD);
527 // TODO is this required?! 567 // TODO is this required?!
528 usbmuxd_send_result(cdata->socket, s_req->header.tag, EINVAL); 568 usbmuxd_send_result(cdata->socket, s_req->header.tag, EINVAL);
529 goto leave; 569 goto leave;
@@ -532,14 +572,14 @@ static void *usbmuxd_client_init_thread(void *arg)
532 pthread_mutex_lock(&usb_mutex); 572 pthread_mutex_lock(&usb_mutex);
533 // gather data about all iPhones/iPods attached 573 // gather data about all iPhones/iPods attached
534 574
535 if (verbose >= 5) fprintf(stderr, "%s: usb init\n", __func__); 575 if (verbose >= 5) logmsg(LOG_DEBUG, "%s[%x]: usb init", __func__, THREAD);
536 usb_init(); 576 usb_init();
537 if (verbose >= 5) fprintf(stderr, "%s: usb find busses\n", __func__); 577 if (verbose >= 5) logmsg(LOG_DEBUG, "%s[%x]: usb find busses", __func__, THREAD);
538 usb_find_busses(); 578 usb_find_busses();
539 if (verbose >= 5) fprintf(stderr, "%s: usb find devices\n", __func__); 579 if (verbose >= 5) logmsg(LOG_DEBUG, "%s[%x]: usb find devices", __func__, THREAD);
540 usb_find_devices(); 580 usb_find_devices();
541 581
542 if (verbose >= 2) fprintf(stderr, "%s: Looking for attached devices...\n", __func__); 582 if (verbose >= 2) logmsg(LOG_NOTICE, "%s[%x]: Looking for attached devices...", __func__, THREAD);
543 583
544 for (bus = usb_get_busses(); bus; bus = bus->next) { 584 for (bus = usb_get_busses(); bus; bus = bus->next) {
545 for (dev = bus->devices; dev; dev = dev->next) { 585 for (dev = bus->devices; dev; dev = dev->next) {
@@ -547,7 +587,7 @@ static void *usbmuxd_client_init_thread(void *arg)
547 && dev->descriptor.idProduct >= 0x1290 587 && dev->descriptor.idProduct >= 0x1290
548 && dev->descriptor.idProduct <= 0x1293) 588 && dev->descriptor.idProduct <= 0x1293)
549 { 589 {
550 if (verbose >= 1) fprintf(stderr, "%s: Found device on bus %d, id %d\n", __func__, bus->location, dev->devnum); 590 if (verbose >= 1) logmsg(LOG_NOTICE, "%s[%x]: Found device on bus %d, id %d", __func__, THREAD, bus->location, dev->devnum);
551 found++; 591 found++;
552 592
553 // construct packet 593 // construct packet
@@ -573,7 +613,7 @@ static void *usbmuxd_client_init_thread(void *arg)
573 613
574 // send it 614 // send it
575 if (send_buf(cdata->socket, &dev_info_rec, sizeof(dev_info_rec)) <= 0) { 615 if (send_buf(cdata->socket, &dev_info_rec, sizeof(dev_info_rec)) <= 0) {
576 if (verbose >= 3) fprintf(stderr, "%s: Error: Could not send device info: %s\n", __func__, strerror(errno)); 616 if (verbose >= 3) logmsg(LOG_ERR, "%s[%x]: Error: Could not send device info: %s", __func__, THREAD, strerror(errno));
577 found--; 617 found--;
578 } 618 }
579 } 619 }
@@ -582,27 +622,27 @@ static void *usbmuxd_client_init_thread(void *arg)
582 pthread_mutex_unlock(&usb_mutex); 622 pthread_mutex_unlock(&usb_mutex);
583 623
584 if (found <= 0) { 624 if (found <= 0) {
585 if (verbose >= 1) fprintf(stderr, "%s: No attached iPhone/iPod devices found.\n", __func__); 625 if (verbose >= 1) logmsg(LOG_NOTICE, "%s[%x]: No attached iPhone/iPod devices found.", __func__, THREAD);
586 goto leave; 626 goto leave;
587 } 627 }
588 628
589 if (verbose >= 2) fprintf(stderr, "%s: Waiting for connect request\n", __func__); 629 if (verbose >= 3) logmsg(LOG_NOTICE, "%s[%x]: Waiting for connect request", __func__, THREAD);
590 630
591 // now wait for connect request 631 // now wait for connect request
592 //memset(&c_req, 0, sizeof(c_req)); 632 //memset(&c_req, 0, sizeof(c_req));
593 if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&c_req, 0)) <= 0) { 633 if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&c_req, 0)) <= 0) {
594 if (verbose >= 3) fprintf(stderr, "%s: Did not receive any connect request.\n", __func__); 634 if (verbose >= 3) logmsg(LOG_NOTICE, "%s[%x]: Did not receive any connect request.", __func__, THREAD);
595 goto leave; 635 goto leave;
596 } 636 }
597 637
598connect: 638connect:
599 639
600 if (c_req->header.type != USBMUXD_CONNECT) { 640 if (c_req->header.type != USBMUXD_CONNECT) {
601 if (verbose >= 2) fprintf(stderr, "%s: Unexpected packet of type %d received.\n", __func__, c_req->header.type); 641 if (verbose >= 2) logmsg(LOG_ERR, "%s[%x]: Unexpected packet of type %d received.", __func__, THREAD, c_req->header.type);
602 goto leave; 642 goto leave;
603 } 643 }
604 644
605 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)); 645 if (verbose >= 3) logmsg(LOG_NOTICE, "%s[%x]: Setting up connection to usb device #%d on port %d", __func__, THREAD, c_req->device_id, ntohs(c_req->tcp_dport));
606 646
607 // find the device, and open usb connection 647 // find the device, and open usb connection
608 pthread_mutex_lock(&usbmux_mutex); 648 pthread_mutex_lock(&usbmux_mutex);
@@ -623,20 +663,20 @@ connect:
623 } 663 }
624 if (!phone) { 664 if (!phone) {
625 // if not found, make a new connection 665 // if not found, make a new connection
626 if (verbose >= 2) fprintf(stderr, "%s: creating new usb connection, device_id=%d\n", __func__, c_req->device_id); 666 if (verbose >= 2) logmsg(LOG_NOTICE, "%s[%x]: creating new usb connection, device_id=%d", __func__, THREAD, c_req->device_id);
627 667
628 pthread_mutex_lock(&usb_mutex); 668 pthread_mutex_lock(&usb_mutex);
629 if (iphone_get_specific_device(0, c_req->device_id, &phone) != IPHONE_E_SUCCESS) { 669 if (iphone_get_specific_device(0, c_req->device_id, &phone) != IPHONE_E_SUCCESS) {
630 pthread_mutex_unlock(&usb_mutex); 670 pthread_mutex_unlock(&usb_mutex);
631 pthread_mutex_unlock(&usbmux_mutex); 671 pthread_mutex_unlock(&usbmux_mutex);
632 if (verbose >= 1) fprintf(stderr, "%s: device_id %d could not be opened\n", __func__, c_req->device_id); 672 if (verbose >= 1) logmsg(LOG_ERR, "%s[%x]: device_id %d could not be opened", __func__, THREAD, c_req->device_id);
633 usbmuxd_send_result(cdata->socket, c_req->header.tag, ENODEV); 673 usbmuxd_send_result(cdata->socket, c_req->header.tag, ENODEV);
634 goto leave; 674 goto leave;
635 } 675 }
636 pthread_mutex_unlock(&usb_mutex); 676 pthread_mutex_unlock(&usb_mutex);
637 677
638 // create device object 678 // create device object
639 if (verbose >= 3) fprintf(stderr, "%s: add to device list\n", __func__); 679 if (verbose >= 3) logmsg(LOG_DEBUG, "%s[%x]: add to device list", __func__, THREAD);
640 cur_dev = (struct device_info*)malloc(sizeof(struct device_info)); 680 cur_dev = (struct device_info*)malloc(sizeof(struct device_info));
641 memset(cur_dev, 0, sizeof(struct device_info)); 681 memset(cur_dev, 0, sizeof(struct device_info));
642 cur_dev->use_count = 1; 682 cur_dev->use_count = 1;
@@ -646,7 +686,7 @@ connect:
646 pthread_mutex_init(&cur_dev->mutex, NULL); 686 pthread_mutex_init(&cur_dev->mutex, NULL);
647 pthread_mutex_init(&cur_dev->writer_mutex, NULL); 687 pthread_mutex_init(&cur_dev->writer_mutex, NULL);
648 688
649 if (verbose >= 3) fprintf(stderr, "%s: device_count = %d\n", __func__, device_count); 689 if (verbose >= 3) logmsg(LOG_DEBUG, "%s[%x]: device_count = %d", __func__, THREAD, device_count);
650 690
651 // add to list of devices 691 // add to list of devices
652 devices = (struct device_info**)realloc(devices, sizeof(struct device_info*) * (device_count+1)); 692 devices = (struct device_info**)realloc(devices, sizeof(struct device_info*) * (device_count+1));
@@ -655,7 +695,7 @@ connect:
655 device_count++; 695 device_count++;
656 } 696 }
657 } else { 697 } else {
658 if (verbose >= 3) fprintf(stderr, "%s: reusing usb connection, device_id=%d\n", __func__, c_req->device_id); 698 if (verbose >= 2) logmsg(LOG_NOTICE, "%s[%x]: reusing usb connection, device_id=%d", __func__, THREAD, c_req->device_id);
659 } 699 }
660 pthread_mutex_unlock(&usbmux_mutex); 700 pthread_mutex_unlock(&usbmux_mutex);
661 701
@@ -666,7 +706,7 @@ connect:
666 706
667 if (res != 0) { 707 if (res != 0) {
668 usbmuxd_send_result(cdata->socket, c_req->header.tag, res); 708 usbmuxd_send_result(cdata->socket, c_req->header.tag, res);
669 if (verbose >= 1) fprintf(stderr, "%s: mux_new_client returned %d, aborting.\n", __func__, res); 709 if (verbose >= 1) logmsg(LOG_ERR, "%s[%x]: mux_new_client returned %d, aborting.", __func__, THREAD, res);
670 goto leave; 710 goto leave;
671 } 711 }
672 712
@@ -682,7 +722,7 @@ connect:
682 cdata->tag = c_req->header.tag; 722 cdata->tag = c_req->header.tag;
683 cdata->dev = cur_dev; 723 cdata->dev = cur_dev;
684 if (pthread_create(&cdata->handler, NULL, usbmuxd_client_handler_thread, cdata) != 0) { 724 if (pthread_create(&cdata->handler, NULL, usbmuxd_client_handler_thread, cdata) != 0) {
685 if (verbose >= 3) fprintf(stderr, "%s: could not create usbmuxd_client_handler_thread!\n", __func__); 725 if (verbose >= 1) logmsg(LOG_ERR, "%s[%x]: could not create usbmuxd_client_handler_thread!", __func__, THREAD);
686 cdata->handler = 0; 726 cdata->handler = 0;
687 goto leave; 727 goto leave;
688 } 728 }
@@ -692,7 +732,7 @@ connect:
692 pthread_join(cdata->handler, NULL); 732 pthread_join(cdata->handler, NULL);
693 } 733 }
694 734
695 if (verbose >= 2) fprintf(stderr, "%s: closing connection\n", __func__); 735 if (verbose >= 2) logmsg(LOG_NOTICE, "%s[%x]: closing connection", __func__, THREAD);
696 736
697 // time to clean up 737 // time to clean up
698 if (cdata && cdata->muxclient) { // should be non-NULL 738 if (cdata && cdata->muxclient) { // should be non-NULL
@@ -700,7 +740,7 @@ connect:
700 } 740 }
701 741
702leave: 742leave:
703 if (verbose >= 2) fprintf(stderr, "%s: terminating\n", __func__); 743 if (verbose >= 3) logmsg(LOG_NOTICE, "%s[%x]: terminating", __func__, THREAD);
704 744
705 if (s_req) { 745 if (s_req) {
706 free(s_req); 746 free(s_req);
@@ -715,15 +755,15 @@ leave:
715 if (cur_dev) { 755 if (cur_dev) {
716 pthread_mutex_lock(&cur_dev->mutex); 756 pthread_mutex_lock(&cur_dev->mutex);
717 if (cur_dev->use_count > 1) { 757 if (cur_dev->use_count > 1) {
718 if (verbose >= 0) fprintf(stderr, "%s: decreasing device use count (from %d to %d)\n", __func__, cur_dev->use_count, cur_dev->use_count-1); 758 if (verbose >= 2) logmsg(LOG_NOTICE, "%s[%x]: decreasing device use count (from %d to %d)", __func__, THREAD, cur_dev->use_count, cur_dev->use_count-1);
719 cur_dev->use_count--; 759 cur_dev->use_count--;
720 pthread_mutex_unlock(&cur_dev->mutex); 760 pthread_mutex_unlock(&cur_dev->mutex);
721 } else { 761 } else {
722 if (verbose >= 3) fprintf(stderr, "%s: last client disconnected, cleaning up\n", __func__); 762 if (verbose >= 2) logmsg(LOG_NOTICE, "%s[%x]: last client disconnected, cleaning up", __func__, THREAD);
723 cur_dev->use_count = 0; 763 cur_dev->use_count = 0;
724 pthread_mutex_unlock(&cur_dev->mutex); 764 pthread_mutex_unlock(&cur_dev->mutex);
725 if (cur_dev->bulk_reader != 0) { 765 if (cur_dev->bulk_reader != 0) {
726 fprintf(stderr, "%s: joining bulk_reader...\n", __func__); 766 if (verbose >= 3) logmsg(LOG_NOTICE, "%s[%x]: joining bulk_reader...", __func__, THREAD);
727 pthread_join(cur_dev->bulk_reader, NULL); 767 pthread_join(cur_dev->bulk_reader, NULL);
728 } 768 }
729 pthread_mutex_lock(&usb_mutex); 769 pthread_mutex_lock(&usb_mutex);
@@ -758,7 +798,7 @@ leave:
758 cdata->dead = 1; 798 cdata->dead = 1;
759 close(cdata->socket); 799 close(cdata->socket);
760 800
761 if (verbose >= 5) fprintf(stderr, "%s: terminated\n", __func__); 801 if (verbose >= 3) logmsg(LOG_NOTICE, "%s[%x]: terminated", __func__, THREAD);
762 802
763 return NULL; 803 return NULL;
764} 804}
@@ -819,38 +859,6 @@ static void clean_exit(int sig)
819 quit_flag = 1; 859 quit_flag = 1;
820} 860}
821 861
822static void logmsg(int prio, char *format, ...)
823{
824 va_list args;
825 va_start(args, format);
826
827 if (!foreground) {
828 // daemon. log using syslog.
829 vsyslog(prio, format, args);
830 } else {
831 // running in foreground. log to stdout/stderr.
832 char msgbuf[256];
833 FILE *lfp = stdout;
834 switch(prio) {
835 case LOG_EMERG:
836 case LOG_ALERT:
837 case LOG_CRIT:
838 case LOG_ERR:
839 case LOG_WARNING:
840 lfp = stderr;
841 break;
842 default:
843 lfp = stdout;
844 }
845 strcpy(msgbuf, "usbmuxd: ");
846 vsnprintf(msgbuf+9, 244, format, args);
847 strcat(msgbuf, "\n");
848 fputs(msgbuf, lfp);
849 }
850
851 va_end(args);
852}
853
854static void usage() 862static void usage()
855{ 863{
856 printf("usage: usbmuxd [options]\n"); 864 printf("usage: usbmuxd [options]\n");
@@ -1107,7 +1115,7 @@ int main(int argc, char **argv)
1107 if (verbose >= 3) logmsg(LOG_NOTICE, "terminating"); 1115 if (verbose >= 3) logmsg(LOG_NOTICE, "terminating");
1108 1116
1109 // preparing for shutdown: wait for child threads to terminate (if any) 1117 // preparing for shutdown: wait for child threads to terminate (if any)
1110 if (verbose >= 2) fprintf(stderr, "usbmuxd: waiting for child threads to terminate...\n"); 1118 if (verbose >= 2) logmsg(LOG_NOTICE, "waiting for child threads to terminate...");
1111 for (i = 0; i < children_capacity; i++) { 1119 for (i = 0; i < children_capacity; i++) {
1112 if (children[i] != NULL) { 1120 if (children[i] != NULL) {
1113 pthread_join(children[i]->thread, NULL); 1121 pthread_join(children[i]->thread, NULL);