ctorrent stat collector
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.

144 lines
3.8KB

  1. /***************************************************************************
  2. * Copyright (C) 07/2007 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <getopt.h>
  23. #include <signal.h>
  24. #include "configfile.h"
  25. #include "connection.h"
  26. #include "event.h"
  27. #include "logging.h"
  28. #include "pidfile.h"
  29. #include "process.h"
  30. #include "signals.h"
  31. #include "torrentfile.h"
  32. #define DEFAULT_CONFIG "torrent-stats.conf"
  33. #define DEFAULT_LOGFILE "torrent-stats.log"
  34. #define DEFAULT_PIDFILE "torrent-stats.pid"
  35. static struct option opts[] = {
  36. {"config", 1, 0, 'c'},
  37. {"debug", 0, 0, 'd'},
  38. {"help", 0, 0, 'h'},
  39. {0, 0, 0, 0}
  40. };
  41. int main(int argc, char *argv[])
  42. {
  43. char *config = DEFAULT_CONFIG;
  44. int debug = 0;
  45. int code, arg = 0;
  46. do {
  47. code = getopt_long(argc, argv, "c:u:dh", opts, &arg);
  48. switch (code) {
  49. case 'c': /* config */
  50. config = optarg;
  51. break;
  52. case 'd': /* debug */
  53. debug = 1;
  54. break;
  55. case 'h': /* help */
  56. printf("Usage: torrent-stats [options]\n"
  57. "Options: \n"
  58. " --config -c configfile use this configfile\n"
  59. " --debug -d do not fork and log to stderr\n"
  60. " --help -h this help\n"
  61. "\n");
  62. exit(0);
  63. break;
  64. case '?': /* error */
  65. exit(1);
  66. break;
  67. default: /* unknown / all options parsed */
  68. break;
  69. }
  70. } while (code != -1);
  71. /* parse config file */
  72. if (config_parse(config) < 0)
  73. exit(1);
  74. /* check logfile */
  75. const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
  76. if (!debug) {
  77. /* check pidfile */
  78. const char *pidfile = config_get_string("global", "pidfile", DEFAULT_PIDFILE);
  79. if (pidfile_check(pidfile, 1) != 0) {
  80. log_print(LOG_ERROR, "torrent-stats already running");
  81. exit(1);
  82. }
  83. /* start logging */
  84. if (log_init(logfile) < 0)
  85. exit(1);
  86. /* zum daemon mutieren */
  87. daemon(-1, 0);
  88. /* create pidfile */
  89. if (pidfile_create(pidfile) < 0) {
  90. log_print(LOG_ERROR, "failed to create pidfile %s", pidfile);
  91. exit(1);
  92. }
  93. }
  94. log_print(LOG_INFO, "torrent-stats started (pid: %d)", getpid());
  95. signal_init();
  96. signal_set_callback(SIGCHLD, childproc_cleanup);
  97. signal_set_callback(SIGHUP, event_loop_break);
  98. while (1) {
  99. if (torrentfile_init() < 0)
  100. break;
  101. if (ctcs_init() < 0)
  102. break;
  103. event_loop();
  104. ctcs_free();
  105. log_close();
  106. config_free();
  107. if (config_parse(config) < 0)
  108. break;
  109. logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
  110. if (!debug && log_init(logfile) < 0)
  111. break;
  112. log_print(LOG_INFO, "torrent-stats restarted (pid: %d)", getpid());
  113. }
  114. return 0;
  115. }