Changeset 14265 for trunk/daemon/daemon.c
- Timestamp:
- Apr 27, 2014, 10:01:51 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/daemon/daemon.c
r14241 r14265 19 19 #include <unistd.h> /* daemon */ 20 20 21 #include <event.h> 21 22 #include <event2/buffer.h> 23 #include <event2/event.h> 22 24 23 25 #include <libtransmission/transmission.h> … … 62 64 63 65 static bool paused = false; 64 static bool closing = false;65 66 static bool seenHUP = false; 66 67 static const char *logfileName = NULL; … … 68 69 static tr_session * mySession = NULL; 69 70 static tr_quark key_pidfile = 0; 71 static struct event_base *ev_base = NULL; 70 72 71 73 /*** … … 184 186 case SIGINT: 185 187 case SIGTERM: 186 closing = true;188 event_base_loopexit(ev_base, NULL); 187 189 break; 188 190 } … … 345 347 } 346 348 349 static void 350 reportStatus (void) 351 { 352 const double up = tr_sessionGetRawSpeed_KBps (mySession, TR_UP); 353 const double dn = tr_sessionGetRawSpeed_KBps (mySession, TR_DOWN); 354 355 if (up>0 || dn>0) 356 sd_notifyf (0, "STATUS=Uploading %.2f KBps, Downloading %.2f KBps.\n", up, dn); 357 else 358 sd_notify (0, "STATUS=Idle.\n"); 359 } 360 361 static void 362 periodicUpdate (evutil_socket_t fd UNUSED, short what UNUSED, void *watchdir) 363 { 364 dtr_watchdir_update (watchdir); 365 366 pumpLogMessages (logfile); 367 368 reportStatus (); 369 } 370 347 371 static tr_rpc_callback_status 348 372 on_rpc_callback (tr_session * session UNUSED, … … 352 376 { 353 377 if (type == TR_RPC_SESSION_CLOSE) 354 closing = true;378 event_base_loopexit(ev_base, NULL); 355 379 return TR_RPC_OK; 356 380 } … … 371 395 bool pidfile_created = false; 372 396 tr_session * session = NULL; 397 struct event *status_ev; 373 398 374 399 key_pidfile = tr_quark_new ("pidfile", 7); … … 514 539 515 540 sd_notifyf (0, "MAINPID=%d\n", (int)getpid()); 541 542 /* setup event state */ 543 ev_base = event_base_new(); 544 if (ev_base == NULL) 545 { 546 char buf[256]; 547 tr_snprintf(buf, sizeof(buf), "Failed to init daemon event state: %s", tr_strerror(errno)); 548 printMessage (logfile, TR_LOG_ERROR, MY_NAME, buf, __FILE__, __LINE__); 549 exit (1); 550 } 516 551 517 552 /* start the session */ … … 580 615 #endif 581 616 582 sd_notify( 0, "READY=1\n" ); 583 584 while (!closing) 585 { 586 double up; 587 double dn; 588 589 tr_wait_msec (1000); /* sleep one second */ 590 591 dtr_watchdir_update (watchdir); 592 pumpLogMessages (logfile); 593 594 up = tr_sessionGetRawSpeed_KBps (mySession, TR_UP); 595 dn = tr_sessionGetRawSpeed_KBps (mySession, TR_DOWN); 596 if (up>0 || dn>0) 597 sd_notifyf (0, "STATUS=Uploading %.2f KBps, Downloading %.2f KBps.\n", up, dn); 598 else 599 sd_notify (0, "STATUS=Idle.\n"); 600 } 601 617 /* Create new timer event to report daemon status */ 618 { 619 struct timeval one_sec = { 1, 0 }; 620 status_ev = event_new(ev_base, -1, EV_PERSIST, &periodicUpdate, watchdir); 621 if (status_ev == NULL) 622 { 623 tr_logAddError("Failed to create status event %s", tr_strerror(errno)); 624 goto cleanup; 625 } 626 if (event_add(status_ev, &one_sec) == -1) 627 { 628 tr_logAddError("Failed to add status event %s", tr_strerror(errno)); 629 goto cleanup; 630 } 631 } 632 633 sd_notify( 0, "READY=1\n" ); 634 635 /* Run daemon event loop */ 636 if (event_base_dispatch(ev_base) == -1) 637 { 638 tr_logAddError("Failed to launch daemon event loop: %s", tr_strerror(errno)); 639 goto cleanup; 640 } 641 642 cleanup: 602 643 sd_notify( 0, "STATUS=Closing transmission session...\n" ); 603 644 printf ("Closing transmission session..."); 645 646 if (status_ev) 647 { 648 event_del(status_ev); 649 event_free(status_ev); 650 } 651 event_base_free(ev_base); 652 604 653 tr_sessionSaveSettings (mySession, configDir, &settings); 605 654 dtr_watchdir_free (watchdir);
Note: See TracChangeset
for help on using the changeset viewer.