klishd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 <signal.h>
  8. #include <syslog.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <sys/socket.h>
  15. #include <sys/un.h>
  16. #include <sys/fsuid.h>
  17. #include <sys/wait.h>
  18. #include <poll.h>
  19. #include <time.h>
  20. #include <faux/faux.h>
  21. #include <faux/str.h>
  22. #include <faux/argv.h>
  23. #include <faux/ini.h>
  24. #include <faux/log.h>
  25. #include <faux/sched.h>
  26. #include <faux/sysdb.h>
  27. #include <faux/net.h>
  28. #include <faux/list.h>
  29. #include <faux/conv.h>
  30. #include <faux/file.h>
  31. #include <faux/eloop.h>
  32. #include <faux/error.h>
  33. #include <klish/ktp.h>
  34. #include <klish/ktp_session.h>
  35. #include <klish/kscheme.h>
  36. #include <klish/ischeme.h>
  37. #include <klish/kcontext.h>
  38. #include <klish/ksession.h>
  39. #include <klish/ksession_parse.h>
  40. #include <klish/kdb.h>
  41. #include <klish/kpargv.h>
  42. #include "private.h"
  43. // Local static functions
  44. bool_t daemonize(const char *pidfile);
  45. bool_t kentry_entrys_is_empty(const kentry_t *entry);
  46. static int create_listen_unix_sock(const char *path);
  47. static kscheme_t *load_all_dbs(const char *dbs,
  48. faux_ini_t *global_config, faux_error_t *error);
  49. static bool_t clear_scheme(kscheme_t *scheme, faux_error_t *error);
  50. // Main loop events
  51. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  52. void *associated_data, void *user_data);
  53. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  54. void *associated_data, void *user_data);
  55. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  56. void *associated_data, void *user_data);
  57. static bool_t wait_for_child_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  58. void *associated_data, void *user_data);
  59. /** @brief Main function
  60. */
  61. int main(int argc, char **argv)
  62. {
  63. int retval = -1;
  64. struct options *opts = NULL;
  65. int pidfd = -1;
  66. int logoptions = 0;
  67. faux_eloop_t *eloop = NULL;
  68. int listen_unix_sock = -1;
  69. ktpd_session_t *ktpd_session = NULL;
  70. kscheme_t *scheme = NULL;
  71. faux_error_t *error = faux_error_new();
  72. faux_ini_t *config = NULL;
  73. int client_fd = -1;
  74. // Parse command line options
  75. opts = opts_init();
  76. if (opts_parse(argc, argv, opts))
  77. goto err;
  78. // Initialize syslog
  79. logoptions = LOG_CONS;
  80. if (opts->foreground)
  81. logoptions |= LOG_PERROR;
  82. openlog(LOG_NAME, logoptions, opts->log_facility);
  83. if (!opts->verbose)
  84. setlogmask(LOG_UPTO(LOG_INFO));
  85. // Parse config file
  86. syslog(LOG_DEBUG, "Parse config file: %s\n", opts->cfgfile);
  87. if (!access(opts->cfgfile, R_OK)) {
  88. if (!(config = config_parse(opts->cfgfile, opts)))
  89. goto err;
  90. } else if (opts->cfgfile_userdefined) {
  91. // User defined config must be found
  92. fprintf(stderr, "Error: Can't find config file %s\n",
  93. opts->cfgfile);
  94. goto err;
  95. }
  96. // DEBUG: Show options
  97. opts_show(opts);
  98. syslog(LOG_INFO, "Start daemon.\n");
  99. // Fork the daemon if needed
  100. if (!opts->foreground && !daemonize(opts->pidfile))
  101. goto err;
  102. // Load scheme
  103. if (!(scheme = load_all_dbs(opts->dbs, config, error))) {
  104. fprintf(stderr, "Scheme errors:\n");
  105. goto err;
  106. }
  107. // Listen socket
  108. syslog(LOG_DEBUG, "Create listen UNIX socket: %s\n", opts->unix_socket_path);
  109. listen_unix_sock = create_listen_unix_sock(opts->unix_socket_path);
  110. if (listen_unix_sock < 0)
  111. goto err;
  112. syslog(LOG_DEBUG, "Listen socket %d", listen_unix_sock);
  113. // Event loop
  114. eloop = faux_eloop_new(NULL);
  115. // Signals
  116. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  117. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  118. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  119. faux_eloop_add_signal(eloop, SIGHUP, refresh_config_ev, opts);
  120. faux_eloop_add_signal(eloop, SIGCHLD, wait_for_child_ev, NULL);
  121. // Listen socket. Waiting for new connections
  122. faux_eloop_add_fd(eloop, listen_unix_sock, POLLIN,
  123. listen_socket_ev, &client_fd);
  124. // Scheduled events
  125. // faux_eloop_add_sched_once_delayed(eloop, &delayed, 1, sched_once, NULL);
  126. // faux_eloop_add_sched_periodic_delayed(eloop, 2, sched_periodic, NULL, &period, FAUX_SCHED_INFINITE);
  127. // Main loop
  128. faux_eloop_loop(eloop);
  129. faux_eloop_free(eloop);
  130. retval = 0;
  131. err: // For listen daemon
  132. // Print errors
  133. if (faux_error_len(error) > 0)
  134. faux_error_show(error);
  135. faux_error_free(error);
  136. // Close listen socket
  137. if (listen_unix_sock >= 0)
  138. close(listen_unix_sock);
  139. // Finish listen daemon if it's not forked service process.
  140. if (client_fd < 0) {
  141. // Free scheme
  142. clear_scheme(scheme, error);
  143. // Free command line options
  144. opts_free(opts);
  145. faux_ini_free(config);
  146. // Remove pidfile
  147. if (pidfd >= 0) {
  148. if (unlink(opts->pidfile) < 0) {
  149. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  150. opts->pidfile, strerror(errno));
  151. }
  152. }
  153. syslog(LOG_INFO, "Stop daemon.\n");
  154. return retval;
  155. }
  156. // ATTENTION: It's a forked service process
  157. retval = -1; // Pessimism for service process
  158. eloop = NULL;
  159. // Re-Initialize syslog
  160. openlog(LOG_SERVICE_NAME, logoptions, opts->log_facility);
  161. if (!opts->verbose)
  162. setlogmask(LOG_UPTO(LOG_INFO));
  163. // Create event loop
  164. eloop = faux_eloop_new(NULL);
  165. // Create KTP session
  166. // Function ktpd_session_new() will add new events to eloop itself.
  167. ktpd_session = ktpd_session_new(client_fd, scheme, NULL, eloop);
  168. assert(ktpd_session);
  169. if (!ktpd_session) {
  170. syslog(LOG_ERR, "Can't create KTPd session\n");
  171. goto err_client;
  172. }
  173. syslog(LOG_DEBUG, "New connection %d\n", client_fd);
  174. // Signals
  175. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  176. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  177. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  178. // Ignore SIGPIPE from client
  179. signal(SIGPIPE, SIG_IGN);
  180. // Main service loop
  181. faux_eloop_loop(eloop);
  182. retval = 0;
  183. err_client:
  184. ktpd_session_free(ktpd_session);
  185. faux_eloop_free(eloop);
  186. syslog(LOG_DEBUG, "Close connection %d", client_fd);
  187. close(client_fd);
  188. // Free scheme
  189. clear_scheme(scheme, error);
  190. // Free command line options
  191. opts_free(opts);
  192. faux_ini_free(config);
  193. return retval;
  194. }
  195. bool_t daemonize(const char *pidfile)
  196. {
  197. // Daemonize
  198. syslog(LOG_DEBUG, "Daemonize\n");
  199. if (!faux_daemon(0, 0, pidfile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) {
  200. syslog(LOG_ERR, "Can't daemonize\n");
  201. return BOOL_FALSE;
  202. }
  203. syslog(LOG_DEBUG, "PID file: %s\n", pidfile);
  204. return BOOL_TRUE;
  205. }
  206. static bool_t load_db(kscheme_t *scheme, const char *db_name,
  207. faux_ini_t *config, faux_error_t *error)
  208. {
  209. kdb_t *db = NULL;
  210. const char *sofile = NULL;
  211. assert(scheme);
  212. if (!scheme)
  213. return BOOL_FALSE;
  214. assert(db_name);
  215. if (!db_name)
  216. return BOOL_FALSE;
  217. // DB.libxml2.so = <so filename>
  218. if (config)
  219. sofile = faux_ini_find(config, "so");
  220. db = kdb_new(db_name, sofile);
  221. assert(db);
  222. if (!db) {
  223. faux_ini_free(config);
  224. return BOOL_FALSE;
  225. }
  226. // Now kdb owns config
  227. kdb_set_ini(db, config);
  228. kdb_set_error(db, error);
  229. // Load DB plugin
  230. if (!kdb_load_plugin(db)) {
  231. faux_error_sprintf(error,
  232. "DB \"%s\": Can't load DB plugin", db_name);
  233. kdb_free(db);
  234. return BOOL_FALSE;
  235. }
  236. // Check plugin API version
  237. if ((kdb_major(db) != KDB_MAJOR) ||
  238. (kdb_minor(db) != KDB_MINOR)) {
  239. faux_error_sprintf(error,
  240. "DB \"%s\": Plugin's API version is %u.%u, need %u.%u",
  241. db_name,
  242. kdb_major(db), kdb_minor(db),
  243. KDB_MAJOR, KDB_MINOR);
  244. kdb_free(db);
  245. return BOOL_FALSE;
  246. }
  247. // Init plugin
  248. if (kdb_has_init_fn(db) && !kdb_init(db)) {
  249. faux_error_sprintf(error,
  250. "DB \"%s\": Can't init DB plugin", db_name);
  251. kdb_free(db);
  252. return BOOL_FALSE;
  253. }
  254. // Load scheme
  255. if (!kdb_has_load_fn(db) || !kdb_load_scheme(db, scheme)) {
  256. faux_error_sprintf(error,
  257. "DB \"%s\": Can't load scheme from DB plugin", db_name);
  258. kdb_fini(db);
  259. kdb_free(db);
  260. return BOOL_FALSE;
  261. }
  262. // Fini plugin
  263. if (kdb_has_fini_fn(db) && !kdb_fini(db)) {
  264. faux_error_sprintf(error,
  265. "DB \"%s\": Can't fini DB plugin", db_name);
  266. kdb_free(db);
  267. return BOOL_FALSE;
  268. }
  269. kdb_free(db);
  270. return BOOL_TRUE;
  271. }
  272. static kscheme_t *load_all_dbs(const char *dbs,
  273. faux_ini_t *global_config, faux_error_t *error)
  274. {
  275. kscheme_t *scheme = NULL;
  276. faux_argv_t *dbs_argv = NULL;
  277. faux_argv_node_t *iter = NULL;
  278. const char *db_name = NULL;
  279. bool_t retcode = BOOL_TRUE;
  280. kcontext_t *context = NULL;
  281. assert(dbs);
  282. if (!dbs)
  283. return NULL;
  284. scheme = kscheme_new();
  285. assert(scheme);
  286. if (!scheme)
  287. return NULL;
  288. dbs_argv = faux_argv_new();
  289. assert(dbs_argv);
  290. if (!dbs_argv) {
  291. kscheme_free(scheme);
  292. return NULL;
  293. }
  294. if (faux_argv_parse(dbs_argv, dbs) <= 0) {
  295. kscheme_free(scheme);
  296. faux_argv_free(dbs_argv);
  297. return NULL;
  298. }
  299. // For each DB
  300. iter = faux_argv_iter(dbs_argv);
  301. while ((db_name = faux_argv_each(&iter))) {
  302. faux_ini_t *config = NULL; // Sub-config for current DB
  303. if (global_config) {
  304. char *prefix = NULL;
  305. prefix = faux_str_mcat(&prefix, "DB.", db_name, ".", NULL);
  306. config = faux_ini_extract_subini(global_config, prefix);
  307. faux_str_free(prefix);
  308. }
  309. if (!load_db(scheme, db_name, config, error))
  310. retcode = BOOL_FALSE;
  311. }
  312. faux_argv_free(dbs_argv);
  313. // Something went wrong while loading DBs
  314. if (!retcode) {
  315. kscheme_free(scheme);
  316. return NULL;
  317. }
  318. // Prepare scheme
  319. context = kcontext_new(KCONTEXT_TYPE_PLUGIN_INIT);
  320. kcontext_set_scheme(context, scheme);
  321. retcode = kscheme_prepare(scheme, context, error);
  322. kcontext_free(context);
  323. if (!retcode) {
  324. kscheme_free(scheme);
  325. faux_error_sprintf(error, "Scheme preparing errors.\n");
  326. return NULL;
  327. }
  328. // Debug
  329. /* {
  330. kdb_t *deploy_db = NULL;
  331. // Deploy (for testing purposes)
  332. deploy_db = kdb_new("ischeme", NULL);
  333. kdb_load_plugin(deploy_db);
  334. kdb_init(deploy_db);
  335. kdb_deploy_scheme(deploy_db, scheme);
  336. kdb_fini(deploy_db);
  337. kdb_free(deploy_db);
  338. }
  339. */
  340. return scheme;
  341. }
  342. static bool_t clear_scheme(kscheme_t *scheme, faux_error_t *error)
  343. {
  344. kcontext_t *context = NULL;
  345. if (!scheme)
  346. return BOOL_TRUE; // It's not an error
  347. context = kcontext_new(KCONTEXT_TYPE_PLUGIN_FINI);
  348. kcontext_set_scheme(context, scheme);
  349. kscheme_fini(scheme, context, error);
  350. kcontext_free(context);
  351. kscheme_free(scheme);
  352. return BOOL_TRUE;
  353. }
  354. /** @brief Create listen socket
  355. *
  356. * Previously removes old socket's file from filesystem. Note daemon must check
  357. * for already working daemon to don't duplicate.
  358. *
  359. * @param [in] path Socket path within filesystem.
  360. * @return Socket descriptor of < 0 on error.
  361. */
  362. static int create_listen_unix_sock(const char *path)
  363. {
  364. int sock = -1;
  365. int opt = 1;
  366. struct sockaddr_un laddr = {};
  367. assert(path);
  368. if (!path)
  369. return -1;
  370. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  371. syslog(LOG_ERR, "Can't create socket: %s\n", strerror(errno));
  372. goto err;
  373. }
  374. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
  375. syslog(LOG_ERR, "Can't set socket options: %s\n", strerror(errno));
  376. goto err;
  377. }
  378. // Remove old (lost) socket's file
  379. unlink(path);
  380. laddr.sun_family = AF_UNIX;
  381. strncpy(laddr.sun_path, path, USOCK_PATH_MAX);
  382. laddr.sun_path[USOCK_PATH_MAX - 1] = '\0';
  383. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  384. syslog(LOG_ERR, "Can't bind socket %s: %s\n", path, strerror(errno));
  385. goto err;
  386. }
  387. if (listen(sock, 128)) {
  388. unlink(path);
  389. syslog(LOG_ERR, "Can't listen on socket %s: %s\n", path, strerror(errno));
  390. goto err;
  391. }
  392. return sock;
  393. err:
  394. if (sock >= 0)
  395. close(sock);
  396. return -1;
  397. }
  398. /** @brief Stop main event loop.
  399. */
  400. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  401. void *associated_data, void *user_data)
  402. {
  403. // Happy compiler
  404. eloop = eloop;
  405. type = type;
  406. associated_data = associated_data;
  407. user_data = user_data;
  408. return BOOL_FALSE; // Stop Event Loop
  409. }
  410. /** @brief Wait for child processes (service processes).
  411. */
  412. static bool_t wait_for_child_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  413. void *associated_data, void *user_data)
  414. {
  415. int wstatus = 0;
  416. pid_t child_pid = -1;
  417. // Wait for any child process. Doesn't block.
  418. while ((child_pid = waitpid(-1, &wstatus, WNOHANG)) > 0) {
  419. if (WIFSIGNALED(wstatus)) {
  420. syslog(LOG_ERR, "Service process %d was terminated "
  421. "by signal: %d",
  422. child_pid, WTERMSIG(wstatus));
  423. } else {
  424. syslog(LOG_ERR, "Service process %d was terminated: %d",
  425. child_pid, WEXITSTATUS(wstatus));
  426. }
  427. }
  428. // Happy compiler
  429. eloop = eloop;
  430. type = type;
  431. associated_data = associated_data;
  432. user_data = user_data;
  433. return BOOL_TRUE;
  434. }
  435. /** @brief Re-read config file.
  436. *
  437. * This function can refresh klishd options but plugins (dbs for example) are
  438. * already inited and there is no way to re-init them on-the-fly.
  439. */
  440. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  441. void *associated_data, void *user_data)
  442. {
  443. struct options *opts = (struct options *)user_data;
  444. faux_ini_t *ini = NULL;
  445. if (access(opts->cfgfile, R_OK) == 0) {
  446. syslog(LOG_DEBUG, "Re-reading config file \"%s\"\n", opts->cfgfile);
  447. if (!(ini = config_parse(opts->cfgfile, opts)))
  448. syslog(LOG_ERR, "Error while config file parsing.\n");
  449. } else if (opts->cfgfile_userdefined) {
  450. syslog(LOG_ERR, "Can't find config file \"%s\"\n", opts->cfgfile);
  451. }
  452. faux_ini_free(ini); // No way to use it later
  453. // Happy compiler
  454. eloop = eloop;
  455. type = type;
  456. associated_data = associated_data;
  457. return BOOL_TRUE;
  458. }
  459. /** @brief Event on listen socket. New remote client.
  460. */
  461. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  462. void *associated_data, void *user_data)
  463. {
  464. int new_conn = -1;
  465. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  466. pid_t child_pid = -1;
  467. assert(user_data);
  468. new_conn = accept(info->fd, NULL, NULL);
  469. if (new_conn < 0) {
  470. syslog(LOG_ERR, "Can't accept() new connection");
  471. return BOOL_TRUE;
  472. }
  473. // Fork new instance for newly connected client
  474. child_pid = fork();
  475. if (child_pid < 0) {
  476. close(new_conn);
  477. syslog(LOG_ERR, "Can't fork service process for client");
  478. return BOOL_TRUE;
  479. }
  480. // Parent
  481. if (child_pid > 0) {
  482. close(new_conn); // It's needed by child but not for parent
  483. syslog(LOG_ERR, "Service process for client was forked: %d",
  484. child_pid);
  485. return BOOL_TRUE;
  486. }
  487. // Child (forked service process)
  488. // Pass new ktpd_session to main programm
  489. *((int *)user_data) = new_conn;
  490. type = type; // Happy compiler
  491. eloop = eloop;
  492. // Return BOOL_FALSE to break listen parent loop. Child will create its
  493. // own loop then.
  494. return BOOL_FALSE;
  495. }