A clone of btpd with my configuration changes.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

118 行
2.2 KiB

  1. #include <signal.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include "btpd.h"
  5. #include "active.h"
  6. static uint8_t m_peer_id[20];
  7. static struct event m_sigint;
  8. static struct event m_sigterm;
  9. static struct event m_heartbeat;
  10. static int m_shutdown;
  11. long btpd_seconds;
  12. void
  13. btpd_exit(int code)
  14. {
  15. btpd_log(BTPD_L_BTPD, "Exiting.\n");
  16. exit(code);
  17. }
  18. static void
  19. grace_cb(int fd, short type, void *arg)
  20. {
  21. struct torrent *tp;
  22. BTPDQ_FOREACH(tp, torrent_get_all(), entry)
  23. torrent_stop(tp);
  24. }
  25. void
  26. btpd_shutdown(int grace_seconds)
  27. {
  28. if (torrent_count() == 0)
  29. btpd_exit(0);
  30. else {
  31. struct torrent *tp;
  32. m_shutdown = 1;
  33. BTPDQ_FOREACH(tp, torrent_get_all(), entry)
  34. if (tp->state != T_STOPPING)
  35. torrent_stop(tp);
  36. if (grace_seconds >= 0) {
  37. event_once(-1, EV_TIMEOUT, grace_cb, NULL,
  38. (& (struct timeval) { grace_seconds, 0 }));
  39. }
  40. }
  41. }
  42. int btpd_is_stopping(void)
  43. {
  44. return m_shutdown;
  45. }
  46. const uint8_t *
  47. btpd_get_peer_id(void)
  48. {
  49. return m_peer_id;
  50. }
  51. void
  52. btpd_on_no_torrents(void)
  53. {
  54. if (m_shutdown)
  55. btpd_exit(0);
  56. }
  57. static void
  58. signal_cb(int signal, short type, void *arg)
  59. {
  60. btpd_log(BTPD_L_BTPD, "Got signal %d.\n", signal);
  61. btpd_shutdown(30);
  62. }
  63. static void
  64. heartbeat_cb(int fd, short type, void *arg)
  65. {
  66. btpd_ev_add(&m_heartbeat, (& (struct timeval) { 1, 0 }));
  67. btpd_seconds++;
  68. net_on_tick();
  69. }
  70. void td_init(void);
  71. void tr_init(void);
  72. void ipc_init(void);
  73. void
  74. btpd_init(void)
  75. {
  76. srandom(time(NULL));
  77. bcopy(BTPD_VERSION, m_peer_id, sizeof(BTPD_VERSION) - 1);
  78. m_peer_id[sizeof(BTPD_VERSION) - 1] = '|';
  79. for (int i = sizeof(BTPD_VERSION); i < 20; i++)
  80. m_peer_id[i] = rand_between(0, 255);
  81. td_init();
  82. net_init();
  83. ipc_init();
  84. ul_init();
  85. cm_init();
  86. tr_init();
  87. tlib_init();
  88. signal(SIGPIPE, SIG_IGN);
  89. signal_set(&m_sigint, SIGINT, signal_cb, NULL);
  90. btpd_ev_add(&m_sigint, NULL);
  91. signal_set(&m_sigterm, SIGTERM, signal_cb, NULL);
  92. btpd_ev_add(&m_sigterm, NULL);
  93. evtimer_set(&m_heartbeat, heartbeat_cb, NULL);
  94. btpd_ev_add(&m_heartbeat, (& (struct timeval) { 1, 0 }));
  95. if (!empty_start)
  96. active_start();
  97. else
  98. active_clear();
  99. }