A clone of btpd with my configuration changes.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

128 рядки
2.7 KiB

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