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.

183 lines
4.2 KiB

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <assert.h>
  4. #include <err.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <getopt.h>
  8. #include <locale.h>
  9. #include <pwd.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include "btpd.h"
  15. static void
  16. writepid(int pidfd)
  17. {
  18. FILE *fp = fdopen(dup(pidfd), "w");
  19. fprintf(fp, "%d", getpid());
  20. fclose(fp);
  21. }
  22. static char *
  23. find_homedir(void)
  24. {
  25. char *res = getenv("BTPD_HOME");
  26. if (res == NULL) {
  27. char *home = getenv("HOME");
  28. if (home == NULL) {
  29. struct passwd *pwent = getpwuid(getuid());
  30. if (pwent == NULL)
  31. errx(1, "Can't find my home directory.\n");
  32. home = pwent->pw_dir;
  33. endpwent();
  34. }
  35. asprintf(&res, "%s/.btpd", home);
  36. }
  37. return res;
  38. }
  39. static void
  40. setup_daemon(const char *dir)
  41. {
  42. int pidfd;
  43. if (dir == NULL)
  44. dir = find_homedir();
  45. btpd_dir = dir;
  46. if (mkdir(dir, 0777) == -1 && errno != EEXIST)
  47. err(1, "Couldn't create home '%s'", dir);
  48. if (chdir(dir) != 0)
  49. err(1, "Couldn't change working directory to '%s'", dir);
  50. if (mkdir("library", 0777) == -1 && errno != EEXIST)
  51. err(1, "Couldn't create library");
  52. pidfd = open("pid", O_CREAT|O_WRONLY|O_NONBLOCK|O_EXLOCK, 0666);
  53. if (pidfd == -1)
  54. err(1, "Couldn't open 'pid'");
  55. if (btpd_daemon) {
  56. if (daemon(1, 1) != 0)
  57. err(1, "Failed to daemonize");
  58. freopen("/dev/null", "r", stdin);
  59. if (freopen("log", "a", stdout) == NULL)
  60. err(1, "Couldn't open 'log'");
  61. dup2(fileno(stdout), fileno(stderr));
  62. setlinebuf(stdout);
  63. setlinebuf(stderr);
  64. }
  65. writepid(pidfd);
  66. }
  67. static void
  68. usage(void)
  69. {
  70. printf("Usage: btpd [options]\n"
  71. "\n"
  72. "Options:\n"
  73. "\n"
  74. "--bw-hz n\n"
  75. "\tRun the bandwidth limiter at n hz.\n"
  76. "\tDefault is 8 hz.\n"
  77. "\n"
  78. "--bw-in n\n"
  79. "\tLimit incoming BitTorrent traffic to n kB/s.\n"
  80. "\tDefault is 0 which means unlimited.\n"
  81. "\n"
  82. "--bw-out n\n"
  83. "\tLimit outgoing BitTorrent traffic to n kB/s.\n"
  84. "\tDefault is 0 which means unlimited.\n"
  85. "\n"
  86. "-d\n"
  87. "\tKeep the btpd process in the foregorund and log to std{out,err}.\n"
  88. "\tThis option is intended for debugging purposes.\n"
  89. "\n"
  90. "--ipc key\n"
  91. "\tThe same key must be used by the cli to talk to this\n"
  92. "\tbtpd instance. You shouldn't need to use this option.\n"
  93. "\n"
  94. "--logfile file\n"
  95. "\tLog to the given file. By default btpd logs to ./btpd.log.\n"
  96. "\n"
  97. "-p n, --port n\n"
  98. "\tListen at port n. Default is 6881.\n"
  99. "\n"
  100. "--help\n"
  101. "\tShow this help.\n"
  102. "\n");
  103. exit(1);
  104. }
  105. static int longval = 0;
  106. static struct option longopts[] = {
  107. { "port", required_argument, NULL, 'p' },
  108. { "bw-in", required_argument, &longval, 1 },
  109. { "bw-out", required_argument, &longval, 2 },
  110. { "help", no_argument, &longval, 5 },
  111. { NULL, 0, NULL, 0 }
  112. };
  113. int
  114. main(int argc, char **argv)
  115. {
  116. char *dir = NULL;
  117. setlocale(LC_ALL, "");
  118. for (;;) {
  119. switch (getopt_long(argc, argv, "dp:", longopts, NULL)) {
  120. case -1:
  121. goto args_done;
  122. case 'd':
  123. btpd_daemon = 0;
  124. break;
  125. case 'p':
  126. net_port = atoi(optarg);
  127. break;
  128. case 0:
  129. switch (longval) {
  130. case 1:
  131. net_bw_limit_in = atoi(optarg) * 1024;
  132. break;
  133. case 2:
  134. net_bw_limit_out = atoi(optarg) * 1024;
  135. break;
  136. default:
  137. usage();
  138. }
  139. break;
  140. case '?':
  141. default:
  142. usage();
  143. }
  144. }
  145. args_done:
  146. argc -= optind;
  147. argv += optind;
  148. if (argc != 0)
  149. usage();
  150. setup_daemon(dir);
  151. event_init();
  152. btpd_init();
  153. event_dispatch();
  154. btpd_err("Unexpected exit from libevent.\n");
  155. return 1;
  156. }