klishd.c 11 KB

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