klishd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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/ini.h>
  23. #include <faux/log.h>
  24. #include <faux/sched.h>
  25. #include <faux/sysdb.h>
  26. #include <faux/net.h>
  27. #include <faux/list.h>
  28. #include <faux/conv.h>
  29. #include <faux/file.h>
  30. #include <faux/eloop.h>
  31. #include <faux/error.h>
  32. #include <klish/ktp.h>
  33. #include <klish/ktp_session.h>
  34. #include <klish/kscheme.h>
  35. #include <klish/ischeme.h>
  36. #include <klish/kcontext.h>
  37. #include "private.h"
  38. #include "sch.c"
  39. // Local static functions
  40. static int create_listen_unix_sock(const char *path);
  41. // Main loop events
  42. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  43. void *associated_data, void *user_data);
  44. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  45. void *associated_data, void *user_data);
  46. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  47. void *associated_data, void *user_data);
  48. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  49. void *associated_data, void *user_data);
  50. static bool_t sched_once(faux_eloop_t *eloop, faux_eloop_type_e type,
  51. void *associated_data, void *user_data);
  52. static bool_t sched_periodic(faux_eloop_t *eloop, faux_eloop_type_e type,
  53. void *associated_data, void *user_data);
  54. /** @brief Main function
  55. */
  56. int main(int argc, char **argv)
  57. {
  58. int retval = -1;
  59. struct options *opts = NULL;
  60. int pidfd = -1;
  61. int logoptions = 0;
  62. faux_eloop_t *eloop = NULL;
  63. int listen_unix_sock = -1;
  64. ktpd_clients_t *clients = NULL;
  65. kscheme_t *scheme = NULL;
  66. faux_error_t *error = faux_error_new();
  67. faux_ini_t *config = NULL;
  68. struct timespec delayed = { .tv_sec = 10, .tv_nsec = 0 };
  69. struct timespec period = { .tv_sec = 3, .tv_nsec = 0 };
  70. // Parse command line options
  71. opts = opts_init();
  72. if (opts_parse(argc, argv, opts))
  73. goto err;
  74. // Initialize syslog
  75. logoptions = LOG_CONS;
  76. if (opts->foreground)
  77. logoptions |= LOG_PERROR;
  78. openlog(LOG_NAME, logoptions, opts->log_facility);
  79. if (!opts->verbose)
  80. setlogmask(LOG_UPTO(LOG_INFO));
  81. // Parse config file
  82. syslog(LOG_DEBUG, "Parse config file: %s\n", opts->cfgfile);
  83. if (!access(opts->cfgfile, R_OK)) {
  84. if (!(config = config_parse(opts->cfgfile, opts)))
  85. goto err;
  86. } else if (opts->cfgfile_userdefined) {
  87. // User defined config must be found
  88. fprintf(stderr, "Error: Can't find config file %s\n",
  89. opts->cfgfile);
  90. goto err;
  91. }
  92. // DEBUG: Show options
  93. opts_show(opts);
  94. syslog(LOG_INFO, "Start daemon.\n");
  95. // Fork the daemon
  96. if (!opts->foreground) {
  97. // Daemonize
  98. syslog(LOG_DEBUG, "Daemonize\n");
  99. if (daemon(0, 0) < 0) {
  100. syslog(LOG_ERR, "Can't daemonize\n");
  101. goto err;
  102. }
  103. // Write pidfile
  104. syslog(LOG_DEBUG, "Write PID file: %s\n", opts->pidfile);
  105. if ((pidfd = open(opts->pidfile,
  106. O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
  107. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
  108. syslog(LOG_WARNING, "Can't open pidfile %s: %s\n",
  109. opts->pidfile, strerror(errno));
  110. } else {
  111. char str[20];
  112. snprintf(str, sizeof(str), "%u\n", getpid());
  113. str[sizeof(str) - 1] = '\0';
  114. if (write(pidfd, str, strlen(str)) < 0)
  115. syslog(LOG_WARNING, "Can't write to %s: %s\n",
  116. opts->pidfile, strerror(errno));
  117. close(pidfd);
  118. }
  119. }
  120. // Scheme
  121. scheme = kscheme_new();
  122. {
  123. char *txt = NULL;
  124. kcontext_t *context = NULL;
  125. bool_t prepare_retcode = BOOL_FALSE;
  126. // Load scheme
  127. if (!ischeme_load(&sch, scheme, error)) {
  128. fprintf(stderr, "Scheme errors:\n");
  129. goto err;
  130. }
  131. // Prepare scheme
  132. context = kcontext_new(KCONTEXT_PLUGIN_INIT);
  133. prepare_retcode = kscheme_prepare(scheme, context, error);
  134. kcontext_free(context);
  135. if (!prepare_retcode) {
  136. fprintf(stderr, "Scheme preparing errors:\n");
  137. goto err;
  138. }
  139. txt = ischeme_deploy(scheme, 0);
  140. // printf("%s\n", txt);
  141. faux_str_free(txt);
  142. goto err; // Test purposes
  143. }
  144. // Listen socket
  145. syslog(LOG_DEBUG, "Create listen UNIX socket: %s\n", opts->unix_socket_path);
  146. listen_unix_sock = create_listen_unix_sock(opts->unix_socket_path);
  147. if (listen_unix_sock < 0)
  148. goto err;
  149. syslog(LOG_DEBUG, "Listen socket %d", listen_unix_sock);
  150. // Clients sessions DB
  151. clients = ktpd_clients_new();
  152. assert(clients);
  153. if (!clients)
  154. goto err;
  155. // Event loop
  156. eloop = faux_eloop_new(NULL);
  157. // Signals
  158. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  159. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  160. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  161. faux_eloop_add_signal(eloop, SIGHUP, refresh_config_ev, opts);
  162. // Listen socket. Waiting for new connections
  163. faux_eloop_add_fd(eloop, listen_unix_sock, POLLIN, listen_socket_ev, clients);
  164. // Scheduled events
  165. faux_eloop_add_sched_once_delayed(eloop, &delayed, 1, sched_once, NULL);
  166. faux_eloop_add_sched_periodic_delayed(eloop, 2, sched_periodic, NULL, &period, FAUX_SCHED_INFINITE);
  167. // Main loop
  168. faux_eloop_loop(eloop);
  169. faux_eloop_free(eloop);
  170. /*
  171. // Non-blocking wait for all children
  172. while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) {
  173. syslog(LOG_DEBUG, "Exit child process %d\n", pid);
  174. }
  175. */
  176. retval = 0;
  177. err:
  178. syslog(LOG_DEBUG, "Cleanup.\n");
  179. ktpd_clients_free(clients);
  180. // Close listen socket
  181. if (listen_unix_sock >= 0)
  182. close(listen_unix_sock);
  183. // Remove pidfile
  184. if (pidfd >= 0) {
  185. if (unlink(opts->pidfile) < 0) {
  186. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  187. opts->pidfile, strerror(errno));
  188. }
  189. }
  190. // Free scheme
  191. {
  192. kcontext_t *context = kcontext_new(KCONTEXT_PLUGIN_FINI);
  193. kscheme_fini(scheme, context, error);
  194. kcontext_free(context);
  195. kscheme_free(scheme);
  196. }
  197. // Free command line options
  198. opts_free(opts);
  199. faux_ini_free(config);
  200. syslog(LOG_INFO, "Stop daemon.\n");
  201. if (faux_error_len(error) > 0)
  202. faux_error_show(error);
  203. faux_error_free(error);
  204. return retval;
  205. }
  206. /** @brief Create listen socket
  207. *
  208. * Previously removes old socket's file from filesystem. Note daemon must check
  209. * for already working daemon to don't duplicate.
  210. *
  211. * @param [in] path Socket path within filesystem.
  212. * @return Socket descriptor of < 0 on error.
  213. */
  214. static int create_listen_unix_sock(const char *path)
  215. {
  216. int sock = -1;
  217. int opt = 1;
  218. struct sockaddr_un laddr = {};
  219. assert(path);
  220. if (!path)
  221. return -1;
  222. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  223. syslog(LOG_ERR, "Can't create socket: %s\n", strerror(errno));
  224. goto err;
  225. }
  226. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
  227. syslog(LOG_ERR, "Can't set socket options: %s\n", strerror(errno));
  228. goto err;
  229. }
  230. // Remove old (lost) socket's file
  231. unlink(path);
  232. laddr.sun_family = AF_UNIX;
  233. strncpy(laddr.sun_path, path, USOCK_PATH_MAX);
  234. laddr.sun_path[USOCK_PATH_MAX - 1] = '\0';
  235. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  236. syslog(LOG_ERR, "Can't bind socket %s: %s\n", path, strerror(errno));
  237. goto err;
  238. }
  239. if (listen(sock, 128)) {
  240. unlink(path);
  241. syslog(LOG_ERR, "Can't listen on socket %s: %s\n", path, strerror(errno));
  242. goto err;
  243. }
  244. return sock;
  245. err:
  246. if (sock >= 0)
  247. close(sock);
  248. return -1;
  249. }
  250. /** @brief Stop main event loop.
  251. */
  252. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  253. void *associated_data, void *user_data)
  254. {
  255. // Happy compiler
  256. eloop = eloop;
  257. type = type;
  258. associated_data = associated_data;
  259. user_data = user_data;
  260. return BOOL_FALSE; // Stop Event Loop
  261. }
  262. /** @brief Re-read config file.
  263. *
  264. * This function can refresh klishd options but plugins (dbs for example) are
  265. * already inited and there is no way to re-init them on-the-fly.
  266. */
  267. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  268. void *associated_data, void *user_data)
  269. {
  270. struct options *opts = (struct options *)user_data;
  271. faux_ini_t *ini = NULL;
  272. if (access(opts->cfgfile, R_OK) == 0) {
  273. syslog(LOG_DEBUG, "Re-reading config file \"%s\"\n", opts->cfgfile);
  274. if (!(ini = config_parse(opts->cfgfile, opts)))
  275. syslog(LOG_ERR, "Error while config file parsing.\n");
  276. } else if (opts->cfgfile_userdefined) {
  277. syslog(LOG_ERR, "Can't find config file \"%s\"\n", opts->cfgfile);
  278. }
  279. faux_ini_free(ini); // No way to use it later
  280. // Happy compiler
  281. eloop = eloop;
  282. type = type;
  283. associated_data = associated_data;
  284. return BOOL_TRUE;
  285. }
  286. bool_t fd_stall_cb(ktpd_session_t *session, void *user_data)
  287. {
  288. faux_eloop_t *eloop = (faux_eloop_t *)user_data;
  289. assert(session);
  290. assert(eloop);
  291. faux_eloop_include_fd_event(eloop, ktpd_session_fd(session), POLLOUT);
  292. return BOOL_TRUE;
  293. }
  294. /** @brief Event on listen socket. New remote client.
  295. */
  296. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  297. void *associated_data, void *user_data)
  298. {
  299. int new_conn = -1;
  300. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  301. ktpd_clients_t *clients = (ktpd_clients_t *)user_data;
  302. ktpd_session_t *session = NULL;
  303. assert(clients);
  304. new_conn = accept(info->fd, NULL, NULL);
  305. if (new_conn < 0) {
  306. syslog(LOG_ERR, "Can't accept() new connection");
  307. return BOOL_TRUE;
  308. }
  309. session = ktpd_clients_add(clients, new_conn);
  310. if (!session) {
  311. syslog(LOG_ERR, "Duplicated client fd");
  312. close(new_conn);
  313. return BOOL_TRUE;
  314. }
  315. ktpd_session_set_stall_cb(session, fd_stall_cb, eloop);
  316. faux_eloop_add_fd(eloop, new_conn, POLLIN, client_ev, clients);
  317. syslog(LOG_DEBUG, "New connection %d", new_conn);
  318. type = type; // Happy compiler
  319. user_data = user_data; // Happy compiler
  320. return BOOL_TRUE;
  321. }
  322. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  323. void *associated_data, void *user_data)
  324. {
  325. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  326. ktpd_clients_t *clients = (ktpd_clients_t *)user_data;
  327. ktpd_session_t *session = NULL;
  328. assert(clients);
  329. // Find out session
  330. session = ktpd_clients_find(clients, info->fd);
  331. if (!session) { // Some strange case
  332. syslog(LOG_ERR, "Can't find client session for fd %d", info->fd);
  333. faux_eloop_del_fd(eloop, info->fd);
  334. close(info->fd);
  335. return BOOL_TRUE;
  336. }
  337. // Write data
  338. if (info->revents & POLLOUT) {
  339. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  340. if (!ktpd_session_async_out(session)) {
  341. // Someting went wrong
  342. faux_eloop_del_fd(eloop, info->fd);
  343. ktpd_clients_del(clients, info->fd);
  344. syslog(LOG_ERR, "Problem with async input");
  345. }
  346. }
  347. // Read data
  348. if (info->revents & POLLIN) {
  349. if (!ktpd_session_async_in(session)) {
  350. // Someting went wrong
  351. faux_eloop_del_fd(eloop, info->fd);
  352. ktpd_clients_del(clients, info->fd);
  353. syslog(LOG_ERR, "Problem with async input");
  354. }
  355. }
  356. // EOF
  357. if (info->revents & POLLHUP) {
  358. faux_eloop_del_fd(eloop, info->fd);
  359. ktpd_clients_del(clients, info->fd);
  360. syslog(LOG_DEBUG, "Close connection %d", info->fd);
  361. }
  362. type = type; // Happy compiler
  363. user_data = user_data; // Happy compiler
  364. return BOOL_TRUE;
  365. }
  366. static bool_t sched_once(faux_eloop_t *eloop, faux_eloop_type_e type,
  367. void *associated_data, void *user_data)
  368. {
  369. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  370. printf("Once %d\n", info->ev_id);
  371. // Happy compiler
  372. eloop = eloop;
  373. type = type;
  374. associated_data = associated_data;
  375. user_data = user_data;
  376. return BOOL_TRUE;
  377. }
  378. static bool_t sched_periodic(faux_eloop_t *eloop, faux_eloop_type_e type,
  379. void *associated_data, void *user_data)
  380. {
  381. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  382. printf("Periodic %d\n", info->ev_id);
  383. // Happy compiler
  384. eloop = eloop;
  385. type = type;
  386. associated_data = associated_data;
  387. user_data = user_data;
  388. return BOOL_TRUE;
  389. }