Broadcast forwarder for ip-bound gameservers
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.

main.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /***************************************************************************
  2. * Copyright (C) 12/2010 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; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <getopt.h>
  25. #include <sys/stat.h>
  26. #include <sys/time.h>
  27. #include <sys/types.h>
  28. #include <signal.h>
  29. #include "configfile.h"
  30. #include "event.h"
  31. #include "logging.h"
  32. #include "pidfile.h"
  33. #include "signals.h"
  34. #define DEFAULT_CONFIG "bcastfwd.conf"
  35. #define DEFAULT_LOGFILE "bcastfwd.log"
  36. #define DEFAULT_PIDFILE "bcastfwd.pid"
  37. int bcast_init(void);
  38. int bcast_close(void);
  39. static struct option opts[] = {
  40. {"config", 1, 0, 'c'},
  41. {"debug", 0, 0, 'd'},
  42. {"help", 0, 0, 'h'},
  43. {0, 0, 0, 0}
  44. };
  45. static int restart_var;
  46. static void trigger_restart(void *privdata)
  47. {
  48. int *restart = (int *)privdata;
  49. *restart = 1;
  50. }
  51. int check_restart(int *maxfd, void *readfds, void *writefds, struct timeval *timeout, void *privdata)
  52. {
  53. int *restart = (int *)privdata;
  54. if (*restart == 1) {
  55. *restart = 0;
  56. return 1;
  57. }
  58. return 0;
  59. }
  60. int main(int argc, char *argv[])
  61. {
  62. char *config = DEFAULT_CONFIG;
  63. int code, arg = 0, debug = 0;
  64. do {
  65. code = getopt_long(argc, argv, "c:dh", opts, &arg);
  66. switch (code) {
  67. case 'c': /* config */
  68. config = optarg;
  69. break;
  70. case 'd': /* debug */
  71. debug = 1;
  72. break;
  73. case 'h': /* help */
  74. printf("Usage: bcastfwd [options]\n"
  75. "Options: \n"
  76. " --config -c configfile use this configfile\n"
  77. " --debug -d do not fork and log to stderr\n"
  78. " --help -h this help\n"
  79. "\n");
  80. exit(0);
  81. break;
  82. case '?': /* error */
  83. exit(1);
  84. break;
  85. default: /* unknown / all options parsed */
  86. break;
  87. }
  88. } while (code != -1);
  89. /* parse config file */
  90. if (config_parse(config) < 0)
  91. exit(1);
  92. if (!debug) {
  93. /* check pidfile */
  94. const char *pidfile = config_get_string("global", "pidfile", DEFAULT_PIDFILE);
  95. if (pidfile_check(pidfile, 1) != 0) {
  96. log_print(LOG_ERROR, "bcastfwd already running");
  97. exit(1);
  98. }
  99. /* start logging */
  100. const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
  101. if (log_init(logfile) < 0)
  102. exit(1);
  103. /* zum daemon mutieren */
  104. if (daemon(-1, 0) < 0) {
  105. log_print(LOG_ERROR, "failed to daemonize");
  106. exit(1);
  107. }
  108. /* create pidfile */
  109. if (pidfile_create(pidfile) < 0) {
  110. log_print(LOG_ERROR, "failed to create pidfile %s", pidfile);
  111. exit(1);
  112. }
  113. }
  114. signal_init();
  115. signal_add_callback(SIGHUP, trigger_restart, (void *)&restart_var);
  116. log_print(LOG_EVERYTIME, "bcastfwd started (pid:%d)", getpid());
  117. while (1) {
  118. if (bcast_init())
  119. break;
  120. /* exited on restart / SIGUSR1 */
  121. event_loop(check_restart, NULL, (void *)&restart_var);
  122. bcast_close();
  123. config_free();
  124. if (config_parse(config) < 0)
  125. break;
  126. const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
  127. if (!debug && log_init(logfile) < 0)
  128. break;
  129. log_print(LOG_EVERYTIME, "bcastfwd restarted (pid:%d) ", getpid());
  130. }
  131. return 0;
  132. }