A clone of btpd with my configuration changes.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 line
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 timeout m_heartbeat;
  6. static int m_signal;
  7. static int m_shutdown;
  8. static int m_ghost;
  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. extern int pidfd;
  17. void ipc_shutdown(void);
  18. void net_shutdown(void);
  19. static void
  20. death_procedure(void)
  21. {
  22. assert(m_shutdown);
  23. if (torrent_count() == 0)
  24. btpd_exit(0);
  25. if (!m_ghost && torrent_count() == torrent_ghosts()) {
  26. btpd_log(BTPD_L_BTPD, "Entering pre exit mode. Bye!\n");
  27. fclose(stderr);
  28. fclose(stdout);
  29. net_shutdown();
  30. ipc_shutdown();
  31. close(pidfd);
  32. m_ghost = 1;
  33. }
  34. }
  35. void
  36. btpd_shutdown(void)
  37. {
  38. m_shutdown = 1;
  39. struct torrent *tp, *next;
  40. BTPDQ_FOREACH_MUTABLE(tp, torrent_get_all(), entry, next)
  41. torrent_stop(tp, 0);
  42. death_procedure();
  43. }
  44. int btpd_is_stopping(void)
  45. {
  46. return m_shutdown;
  47. }
  48. const uint8_t *
  49. btpd_get_peer_id(void)
  50. {
  51. return m_peer_id;
  52. }
  53. static void
  54. signal_handler(int signal)
  55. {
  56. m_signal = signal;
  57. }
  58. static void
  59. heartbeat_cb(int fd, short type, void *arg)
  60. {
  61. btpd_timer_add(&m_heartbeat, (& (struct timespec) { 1, 0 }));
  62. btpd_seconds++;
  63. net_on_tick();
  64. torrent_on_tick_all();
  65. if (m_signal) {
  66. btpd_log(BTPD_L_BTPD, "Got signal %d.\n", m_signal);
  67. m_signal = 0;
  68. if (!m_shutdown)
  69. btpd_shutdown();
  70. }
  71. if (m_shutdown)
  72. death_procedure();
  73. }
  74. void tr_init(void);
  75. void ipc_init(void);
  76. void td_init(void);
  77. void addrinfo_init(void);
  78. void
  79. btpd_init(void)
  80. {
  81. struct sigaction sa;
  82. unsigned long seed;
  83. uint8_t idcon[1024];
  84. struct timeval now;
  85. int n;
  86. bzero(&sa, sizeof(sa));
  87. sa.sa_handler = SIG_IGN;
  88. sa.sa_flags = SA_RESTART;
  89. sigfillset(&sa.sa_mask);
  90. sigaction(SIGPIPE, &sa, NULL);
  91. sa.sa_handler = signal_handler;
  92. sigaction(SIGTERM, &sa, NULL);
  93. sigaction(SIGINT, &sa, NULL);
  94. gettimeofday(&now, NULL);
  95. n = snprintf(idcon, sizeof(idcon), "%ld%ld%d", (long)now.tv_sec,
  96. (long)now.tv_usec, net_port);
  97. if (n < sizeof(idcon))
  98. gethostname(idcon + n, sizeof(idcon) - n);
  99. idcon[sizeof(idcon) - 1] = '\0';
  100. n = strlen(idcon);
  101. SHA1(idcon, n, m_peer_id);
  102. bcopy(m_peer_id, &seed, sizeof(seed));
  103. bcopy(BTPD_VERSION, m_peer_id, sizeof(BTPD_VERSION) - 1);
  104. m_peer_id[sizeof(BTPD_VERSION) - 1] = '|';
  105. srandom(seed);
  106. td_init();
  107. addrinfo_init();
  108. net_init();
  109. ipc_init();
  110. ul_init();
  111. cm_init();
  112. tr_init();
  113. tlib_init();
  114. evtimer_init(&m_heartbeat, heartbeat_cb, NULL);
  115. btpd_timer_add(&m_heartbeat, (& (struct timespec) { 1, 0 }));
  116. }