QNAP-TS419p system daemon
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.

209 lines
5.0 KiB

  1. /***************************************************************************
  2. * Copyright (C) 05/2011 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 <signal.h>
  26. #include "configfile.h"
  27. #include "diskwatch.h"
  28. #include "event.h"
  29. #include "lcd.h"
  30. #include "lcdpage.h"
  31. #include "logging.h"
  32. #include "pic.h"
  33. #include "pidfile.h"
  34. #include "signals.h"
  35. #define DEFAULT_CONFIG "qnapd.conf"
  36. #define DEFAULT_LOGFILE "qnapd.log"
  37. #define DEFAULT_PIDFILE "qnapd.pid"
  38. #define LCD_TIMEOUT 15
  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. restart_var = 1;
  49. }
  50. int check_restart(int *maxfd, void *readfds, void *writefds, struct timeval *timeout, void *privdata)
  51. {
  52. if (restart_var == 1) {
  53. restart_var = 0;
  54. return 1;
  55. }
  56. return 0;
  57. }
  58. int main(int argc, char *argv[])
  59. {
  60. char *config = DEFAULT_CONFIG;
  61. int code, arg = 0, debug = 0;
  62. do {
  63. code = getopt_long(argc, argv, "c:dh", opts, &arg);
  64. switch (code) {
  65. case 'c': /* config */
  66. config = optarg;
  67. break;
  68. case 'd': /* debug */
  69. debug = 1;
  70. break;
  71. case 'h': /* help */
  72. printf("Usage: qnapd [options]\n"
  73. "Options: \n"
  74. " --config -c configfile use this configfile\n"
  75. " --debug -d do not fork and log to stderr\n"
  76. " --help -h this help\n"
  77. "\n");
  78. exit(0);
  79. break;
  80. case '?': /* error */
  81. exit(1);
  82. break;
  83. default: /* unknown / all options parsed */
  84. break;
  85. }
  86. } while (code != -1);
  87. /* parse config file */
  88. if (config_parse(config) < 0)
  89. exit(1);
  90. if (!debug) {
  91. /* check pidfile */
  92. const char *pidfile = config_get_string("global", "pidfile", DEFAULT_PIDFILE);
  93. if (pidfile_check(pidfile, 1) != 0) {
  94. log_print(LOG_ERROR, "qnapd already running");
  95. exit(1);
  96. }
  97. /* start logging */
  98. const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
  99. if (log_init(logfile) < 0)
  100. exit(1);
  101. /* mutate to daemon */
  102. if (daemon(-1, 0) < 0) {
  103. log_print(LOG_ERROR, "failed to daemonize");
  104. exit(1);
  105. }
  106. /* create pidfile */
  107. if (pidfile_create(pidfile) < 0) {
  108. log_print(LOG_ERROR, "failed to create pidfile %s", pidfile);
  109. exit(1);
  110. }
  111. }
  112. signal_init();
  113. signal_add_callback(SIGHUP, trigger_restart, NULL);
  114. log_print(LOG_EVERYTIME, "qnapd started (pid:%d)", getpid());
  115. while (1) {
  116. struct lcddev *lcd = NULL;
  117. struct picdev *pic = NULL;
  118. int abort = 0;
  119. do {
  120. const char *devicename = config_get_string("global", "lcddevice", NULL);
  121. if (devicename != NULL) {
  122. int lcdtimeout;
  123. config_get_int("global", "lcdtimeout", &lcdtimeout, LCD_TIMEOUT);
  124. lcd = lcd_open(devicename, lcdtimeout);
  125. if (lcd == NULL) {
  126. abort = 1;
  127. break;
  128. }
  129. if (lcdpage_init(lcd) < 0) {
  130. abort = 1;
  131. break;
  132. }
  133. }
  134. devicename = config_get_string("global", "picdevice", NULL);
  135. if (devicename != NULL) {
  136. pic = pic_open(devicename, lcd);
  137. if (pic == NULL) {
  138. abort = 1;
  139. break;
  140. }
  141. }
  142. if (diskwatch_init(lcd) < 0) {
  143. abort = 1;
  144. break;
  145. }
  146. /* exited on restart / SIGUSR1 */
  147. event_loop(check_restart, NULL, NULL);
  148. } while (0);
  149. diskwatch_exit();
  150. if (pic != NULL) {
  151. pic_close(pic);
  152. }
  153. if (lcd != NULL) {
  154. lcdpage_free();
  155. lcd_close(lcd);
  156. }
  157. config_free();
  158. if (abort)
  159. break;
  160. if (config_parse(config) < 0)
  161. break;
  162. const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
  163. if (!debug && log_init(logfile) < 0)
  164. break;
  165. log_print(LOG_EVERYTIME, "qnapd restarted (pid:%d) ", getpid());
  166. }
  167. return 0;
  168. }