A small USV for ALIX boards with i2c support
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.

alix-usvd.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /***************************************************************************
  2. * Copyright (C) 03/2009 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 <string.h>
  23. #include <getopt.h>
  24. #include <signal.h>
  25. #include "configfile.h"
  26. #include "event.h"
  27. #include "logging.h"
  28. #include "pidfile.h"
  29. #include "signals.h"
  30. #include "usvdevice.h"
  31. #include "usvstate.h"
  32. #define DEFAULT_CONFIG "alix-usvd.conf"
  33. #define DEFAULT_LOGFILE "alix-usvd.log"
  34. #define DEFAULT_PIDFILE "alix-usvd.pid"
  35. static struct option opts[] = {
  36. {"nofork", 0, 0, 'd'},
  37. {"config", 1, 0, 'c'},
  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, "dc:h", opts, &arg);
  48. switch (code) {
  49. case 'd': /* debug */
  50. debug = 1;
  51. break;
  52. case 'c': /* configfile path */
  53. config = optarg;
  54. break;
  55. case 'h': /* help */
  56. printf( "Usage: alix-usvd [-d] [-c config]\n"
  57. "Options: \n"
  58. " --debug -d do not fork and log to stderr\n"
  59. " --config -c configfile use this configfile\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. if (!debug) {
  75. /* check pidfile */
  76. const char *pidfile = config_get_string("global", "pidfile", DEFAULT_PIDFILE);
  77. if (pidfile_check(pidfile, 1) != 0) {
  78. log_print(LOG_ERROR, "alix-usvd already running");
  79. exit(1);
  80. }
  81. /* start logging */
  82. const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
  83. if (log_init(logfile) < 0)
  84. exit(1);
  85. /* zum daemon mutieren */
  86. daemon(-1, 0);
  87. /* create pidfile */
  88. if (pidfile_create(pidfile) < 0) {
  89. log_print(LOG_ERROR, "failed to create pidfile %s", pidfile);
  90. exit(1);
  91. }
  92. }
  93. log_print(LOG_INFO, "alix-usvd started (pid: %d)", getpid());
  94. signal_init();
  95. signal_set_callback(SIGHUP, event_loop_break);
  96. while (1) {
  97. if (usvdev_init() < 0)
  98. break;
  99. if (usvstate_init() < 0)
  100. break;
  101. event_loop();
  102. usvstate_close();
  103. log_close();
  104. config_free();
  105. if (config_parse(config) < 0)
  106. break;
  107. const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
  108. if (!debug && log_init(logfile) < 0)
  109. break;
  110. log_print(LOG_INFO, "alix-usvd restarted (pid: %d)", getpid());
  111. }
  112. return 0;
  113. }