Linux HLSW LAN Master
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.

140 lines
3.7 KiB

  1. /***************************************************************************
  2. * Copyright (C) 03/2005 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 <pwd.h>
  27. #include "configfile.h"
  28. #include "event.h"
  29. #include "logging.h"
  30. #include "gamelist.h"
  31. #include "scanner.h"
  32. #include "server.h"
  33. #include "plugin.h"
  34. #define DEFAULT_CONFIG "hlswmaster.conf"
  35. #define DEFAULT_LOGFILE "hlswmaster.log"
  36. static struct option opts[] = {
  37. {"config", 1, 0, 'c'},
  38. {"user", 1, 0, 'u'},
  39. {"debug", 0, 0, 'd'},
  40. {"help", 0, 0, 'h'},
  41. {0, 0, 0, 0}
  42. };
  43. int main(int argc, char *argv[])
  44. {
  45. int arg = 0, code = 0, debug = 0;
  46. char *config = DEFAULT_CONFIG, *user = NULL, *logfile;
  47. while (code != -1) {
  48. code = getopt_long(argc, argv, "c:u:dh", opts, &arg);
  49. switch (code) {
  50. case 'c': /* config */
  51. config = optarg;
  52. break;
  53. case 'u': /* user */
  54. user = optarg;
  55. break;
  56. case 'd': /* debug */
  57. debug = 1;
  58. break;
  59. case 'h': /* help */
  60. printf("Usage: hlsw-master [options]\n"
  61. "Options: \n"
  62. " --config -c configfile use this configfile\n"
  63. " --user -u username change uid to username\n"
  64. " --debug -d do not fork and log to stderr\n"
  65. " --help -h this help\n"
  66. "\n");
  67. exit(0);
  68. break;
  69. case '?': /* error */
  70. exit(-1);
  71. break;
  72. default: /* unknown / all options parsed */
  73. break;
  74. }
  75. }
  76. /* userwechsel */
  77. if (user) {
  78. struct passwd *pwl;
  79. if (!(pwl = getpwnam(user))) {
  80. log_print(LOG_ERROR, "unknown user: %s", user);
  81. exit(-1);
  82. }
  83. if (setgid(pwl->pw_gid) || setuid(pwl->pw_uid)) {
  84. log_print(LOG_ERROR, "setgid/setuid");
  85. exit(-1);
  86. }
  87. }
  88. /* parse config file */
  89. if (config_parse(config) == -1)
  90. exit(-1);
  91. /* check logfile */
  92. logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
  93. if (logfile && !debug) {
  94. /* start logging */
  95. if (!log_init(logfile))
  96. exit(-1);
  97. /* zum daemon mutieren */
  98. daemon(-1, 0);
  99. }
  100. log_print(LOG_INFO, "hlswmaster started (user: %s, pid: %d)", getpwuid(getuid())->pw_name, getpid());
  101. /* init plugins */
  102. if (plugin_init() == -1)
  103. exit(-1);
  104. /* init gamelist */
  105. if (gamelist_init() == -1)
  106. exit(-1);
  107. /* init server */
  108. if (server_init() == -1)
  109. exit(-1);
  110. /* init scanner */
  111. if (scanner_init() == -1)
  112. exit(-1);
  113. event_loop();
  114. return 0;
  115. }