A clone of btpd with my configuration changes.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

120 lignes
2.3 KiB

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