opts.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #define _GNU_SOURCE
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <syslog.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <getopt.h>
  14. #include <time.h>
  15. #include <faux/faux.h>
  16. #include <faux/str.h>
  17. #include <faux/ini.h>
  18. #include <faux/log.h>
  19. #include <faux/conv.h>
  20. #include <klish/ktp_session.h>
  21. #include "private.h"
  22. /** @brief Initialize option structure by defaults
  23. */
  24. struct options *opts_init(void)
  25. {
  26. struct options *opts = NULL;
  27. opts = faux_zmalloc(sizeof(*opts));
  28. assert(opts);
  29. // Initialize
  30. opts->pidfile = faux_str_dup(DEFAULT_PIDFILE);
  31. opts->cfgfile = faux_str_dup(DEFAULT_CFGFILE);
  32. opts->unix_socket_path = faux_str_dup(KLISH_DEFAULT_UNIX_SOCKET_PATH);
  33. opts->cfgfile_userdefined = BOOL_FALSE;
  34. opts->foreground = BOOL_FALSE; // Daemonize by default
  35. opts->verbose = BOOL_FALSE;
  36. opts->log_facility = LOG_DAEMON;
  37. opts->dbs = faux_str_dup(DEFAULT_DBS);
  38. return opts;
  39. }
  40. /** @brief Free options structure
  41. */
  42. void opts_free(struct options *opts)
  43. {
  44. faux_str_free(opts->pidfile);
  45. faux_str_free(opts->cfgfile);
  46. faux_str_free(opts->unix_socket_path);
  47. faux_str_free(opts->dbs);
  48. faux_free(opts);
  49. }
  50. /** @brief Parse command line options
  51. */
  52. int opts_parse(int argc, char *argv[], struct options *opts)
  53. {
  54. static const char *shortopts = "hp:f:dl:v";
  55. static const struct option longopts[] = {
  56. {"help", 0, NULL, 'h'},
  57. {"pid", 1, NULL, 'p'},
  58. {"conf", 1, NULL, 'f'},
  59. {"foreground", 0, NULL, 'd'},
  60. {"verbose", 0, NULL, 'v'},
  61. {"facility", 1, NULL, 'l'},
  62. {NULL, 0, NULL, 0}
  63. };
  64. optind = 1;
  65. while(1) {
  66. int opt = 0;
  67. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  68. if (-1 == opt)
  69. break;
  70. switch (opt) {
  71. case 'p':
  72. faux_str_free(opts->pidfile);
  73. opts->pidfile = faux_str_dup(optarg);
  74. break;
  75. case 'f':
  76. faux_str_free(opts->cfgfile);
  77. opts->cfgfile = faux_str_dup(optarg);
  78. opts->cfgfile_userdefined = BOOL_TRUE;
  79. break;
  80. case 'd':
  81. opts->foreground = BOOL_TRUE;
  82. break;
  83. case 'v':
  84. opts->verbose = BOOL_TRUE;
  85. break;
  86. case 'l':
  87. if (faux_log_facility_id(optarg, &(opts->log_facility))) {
  88. fprintf(stderr, "Error: Illegal syslog facility %s.\n", optarg);
  89. _exit(-1);
  90. }
  91. break;
  92. case 'h':
  93. help(0, argv[0]);
  94. _exit(0);
  95. break;
  96. default:
  97. help(-1, argv[0]);
  98. _exit(-1);
  99. break;
  100. }
  101. }
  102. return 0;
  103. }
  104. /** @brief Print help message
  105. */
  106. void help(int status, const char *argv0)
  107. {
  108. const char *name = NULL;
  109. if (!argv0)
  110. return;
  111. // Find the basename
  112. name = strrchr(argv0, '/');
  113. if (name)
  114. name++;
  115. else
  116. name = argv0;
  117. if (status != 0) {
  118. fprintf(stderr, "Try `%s -h' for more information.\n",
  119. name);
  120. } else {
  121. printf("Version : %s\n", VERSION);
  122. printf("Usage : %s [options]\n", name);
  123. printf("Klish daemon\n");
  124. printf("Options :\n");
  125. printf("\t-h, --help Print this help.\n");
  126. printf("\t-d, --foreground Don't daemonize.\n");
  127. printf("\t-v, --verbose Be verbose.\n");
  128. printf("\t-p <path>, --pid=<path> File to save daemon's PID to ("
  129. DEFAULT_PIDFILE ").\n");
  130. printf("\t-f <path>, --conf=<path> Config file ("
  131. DEFAULT_CFGFILE ").\n");
  132. printf("\t-l, --facility Syslog facility (DAEMON).\n");
  133. }
  134. }
  135. /** @brief Parse config file
  136. *
  137. * It parses known options and put them to opts arg. But also it returns
  138. * parsed config file. The parts of config file can be unknown to klish
  139. * itself but can be known to third party plugins. For example DBs
  140. * subsystem get settings from config file.
  141. */
  142. faux_ini_t *config_parse(const char *cfgfile, struct options *opts)
  143. {
  144. faux_ini_t *ini = NULL;
  145. const char *tmp = NULL;
  146. ini = faux_ini_new();
  147. assert(ini);
  148. if (!ini)
  149. NULL;
  150. if (!faux_ini_parse_file(ini, cfgfile)) {
  151. syslog(LOG_ERR, "Can't parse config file: %s\n", cfgfile);
  152. faux_ini_free(ini);
  153. return NULL;
  154. }
  155. // UnixSocketPath
  156. if ((tmp = faux_ini_find(ini, "UnixSocketPath"))) {
  157. faux_str_free(opts->unix_socket_path);
  158. opts->unix_socket_path = faux_str_dup(tmp);
  159. }
  160. // DBs
  161. if ((tmp = faux_ini_find(ini, "DBs"))) {
  162. faux_str_free(opts->dbs);
  163. opts->dbs = faux_str_dup(tmp);
  164. }
  165. return ini;
  166. }
  167. /** @brief Show options. For debug purposes.
  168. */
  169. int opts_show(struct options *opts)
  170. {
  171. assert(opts);
  172. if (!opts)
  173. return -1;
  174. syslog(LOG_DEBUG, "opts: Foreground = %s\n", opts->foreground ? "true" : "false");
  175. syslog(LOG_DEBUG, "opts: Verbose = %s\n", opts->verbose ? "true" : "false");
  176. syslog(LOG_DEBUG, "opts: LogFacility = %s\n", faux_log_facility_str(opts->log_facility));
  177. syslog(LOG_DEBUG, "opts: PIDPath = %s\n", opts->pidfile);
  178. syslog(LOG_DEBUG, "opts: ConfigPath = %s\n", opts->cfgfile);
  179. syslog(LOG_DEBUG, "opts: UnixSocketPath = %s\n", opts->unix_socket_path);
  180. syslog(LOG_DEBUG, "opts: DBs = %s\n", opts->dbs);
  181. return 0;
  182. }