klishd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. faux_error_t *error = faux_error_new();
  165. scheme = kscheme_from_ischeme(&sch, error);
  166. if (!scheme) {
  167. fprintf(stderr, "Scheme errors:\n");
  168. faux_error_print(error);
  169. goto err;
  170. }
  171. }
  172. // Listen socket
  173. syslog(LOG_DEBUG, "Create listen UNIX socket: %s\n", opts->unix_socket_path);
  174. listen_unix_sock = create_listen_unix_sock(opts->unix_socket_path);
  175. if (listen_unix_sock < 0)
  176. goto err;
  177. syslog(LOG_DEBUG, "Listen socket %d", listen_unix_sock);
  178. // Clients sessions DB
  179. clients = ktpd_clients_new();
  180. assert(clients);
  181. if (!clients)
  182. goto err;
  183. // Event loop
  184. eloop = faux_eloop_new(NULL);
  185. // Signals
  186. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  187. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  188. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  189. faux_eloop_add_signal(eloop, SIGHUP, refresh_config_ev, opts);
  190. // Listen socket. Waiting for new connections
  191. faux_eloop_add_fd(eloop, listen_unix_sock, POLLIN, listen_socket_ev, clients);
  192. // Scheduled events
  193. faux_eloop_add_sched_once_delayed(eloop, &delayed, 1, sched_once, NULL);
  194. faux_eloop_add_sched_periodic_delayed(eloop, 2, sched_periodic, NULL, &period, FAUX_SCHED_INFINITE);
  195. // Main loop
  196. faux_eloop_loop(eloop);
  197. faux_eloop_free(eloop);
  198. /*
  199. // Non-blocking wait for all children
  200. while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) {
  201. syslog(LOG_DEBUG, "Exit child process %d\n", pid);
  202. }
  203. */
  204. retval = 0;
  205. err:
  206. syslog(LOG_DEBUG, "Cleanup.\n");
  207. ktpd_clients_free(clients);
  208. // Close listen socket
  209. if (listen_unix_sock >= 0)
  210. close(listen_unix_sock);
  211. // Remove pidfile
  212. if (pidfd >= 0) {
  213. if (unlink(opts->pidfile) < 0) {
  214. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  215. opts->pidfile, strerror(errno));
  216. }
  217. }
  218. // Free scheme
  219. kscheme_free(scheme);
  220. // Free command line options
  221. opts_free(opts);
  222. syslog(LOG_INFO, "Stop daemon.\n");
  223. return retval;
  224. }
  225. /** @brief Create listen socket
  226. *
  227. * Previously removes old socket's file from filesystem. Note daemon must check
  228. * for already working daemon to don't duplicate.
  229. *
  230. * @param [in] path Socket path within filesystem.
  231. * @return Socket descriptor of < 0 on error.
  232. */
  233. static int create_listen_unix_sock(const char *path)
  234. {
  235. int sock = -1;
  236. int opt = 1;
  237. struct sockaddr_un laddr = {};
  238. assert(path);
  239. if (!path)
  240. return -1;
  241. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  242. syslog(LOG_ERR, "Can't create socket: %s\n", strerror(errno));
  243. goto err;
  244. }
  245. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
  246. syslog(LOG_ERR, "Can't set socket options: %s\n", strerror(errno));
  247. goto err;
  248. }
  249. // Remove old (lost) socket's file
  250. unlink(path);
  251. laddr.sun_family = AF_UNIX;
  252. strncpy(laddr.sun_path, path, USOCK_PATH_MAX);
  253. laddr.sun_path[USOCK_PATH_MAX - 1] = '\0';
  254. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  255. syslog(LOG_ERR, "Can't bind socket %s: %s\n", path, strerror(errno));
  256. goto err;
  257. }
  258. if (listen(sock, 128)) {
  259. unlink(path);
  260. syslog(LOG_ERR, "Can't listen on socket %s: %s\n", path, strerror(errno));
  261. goto err;
  262. }
  263. return sock;
  264. err:
  265. if (sock >= 0)
  266. close(sock);
  267. return -1;
  268. }
  269. /** @brief Stop main event loop.
  270. */
  271. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  272. void *associated_data, void *user_data)
  273. {
  274. // Happy compiler
  275. eloop = eloop;
  276. type = type;
  277. associated_data = associated_data;
  278. user_data = user_data;
  279. return BOOL_FALSE; // Stop Event Loop
  280. }
  281. /** @brief Re-read config file.
  282. */
  283. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  284. void *associated_data, void *user_data)
  285. {
  286. struct options *opts = (struct options *)user_data;
  287. if (access(opts->cfgfile, R_OK) == 0) {
  288. syslog(LOG_DEBUG, "Re-reading config file \"%s\"\n", opts->cfgfile);
  289. if (config_parse(opts->cfgfile, opts) < 0)
  290. syslog(LOG_ERR, "Error while config file parsing.\n");
  291. } else if (opts->cfgfile_userdefined) {
  292. syslog(LOG_ERR, "Can't find config file \"%s\"\n", opts->cfgfile);
  293. }
  294. // Happy compiler
  295. eloop = eloop;
  296. type = type;
  297. associated_data = associated_data;
  298. return BOOL_TRUE;
  299. }
  300. bool_t fd_stall_cb(ktpd_session_t *session, void *user_data)
  301. {
  302. faux_eloop_t *eloop = (faux_eloop_t *)user_data;
  303. assert(session);
  304. assert(eloop);
  305. faux_eloop_include_fd_event(eloop, ktpd_session_fd(session), POLLOUT);
  306. return BOOL_TRUE;
  307. }
  308. /** @brief Event on listen socket. New remote client.
  309. */
  310. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  311. void *associated_data, void *user_data)
  312. {
  313. int new_conn = -1;
  314. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  315. ktpd_clients_t *clients = (ktpd_clients_t *)user_data;
  316. ktpd_session_t *session = NULL;
  317. assert(clients);
  318. new_conn = accept(info->fd, NULL, NULL);
  319. if (new_conn < 0) {
  320. syslog(LOG_ERR, "Can't accept() new connection");
  321. return BOOL_TRUE;
  322. }
  323. session = ktpd_clients_add(clients, new_conn);
  324. if (!session) {
  325. syslog(LOG_ERR, "Duplicated client fd");
  326. close(new_conn);
  327. return BOOL_TRUE;
  328. }
  329. ktpd_session_set_stall_cb(session, fd_stall_cb, eloop);
  330. faux_eloop_add_fd(eloop, new_conn, POLLIN, client_ev, clients);
  331. syslog(LOG_DEBUG, "New connection %d", new_conn);
  332. type = type; // Happy compiler
  333. user_data = user_data; // Happy compiler
  334. return BOOL_TRUE;
  335. }
  336. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  337. void *associated_data, void *user_data)
  338. {
  339. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  340. ktpd_clients_t *clients = (ktpd_clients_t *)user_data;
  341. ktpd_session_t *session = NULL;
  342. assert(clients);
  343. // Find out session
  344. session = ktpd_clients_find(clients, info->fd);
  345. if (!session) { // Some strange case
  346. syslog(LOG_ERR, "Can't find client session for fd %d", info->fd);
  347. faux_eloop_del_fd(eloop, info->fd);
  348. close(info->fd);
  349. return BOOL_TRUE;
  350. }
  351. // Write data
  352. if (info->revents & POLLOUT) {
  353. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  354. if (!ktpd_session_async_out(session)) {
  355. // Someting went wrong
  356. faux_eloop_del_fd(eloop, info->fd);
  357. ktpd_clients_del(clients, info->fd);
  358. syslog(LOG_ERR, "Problem with async input");
  359. }
  360. }
  361. // Read data
  362. if (info->revents & POLLIN) {
  363. if (!ktpd_session_async_in(session)) {
  364. // Someting went wrong
  365. faux_eloop_del_fd(eloop, info->fd);
  366. ktpd_clients_del(clients, info->fd);
  367. syslog(LOG_ERR, "Problem with async input");
  368. }
  369. }
  370. // EOF
  371. if (info->revents & POLLHUP) {
  372. faux_eloop_del_fd(eloop, info->fd);
  373. ktpd_clients_del(clients, info->fd);
  374. syslog(LOG_DEBUG, "Close connection %d", info->fd);
  375. }
  376. type = type; // Happy compiler
  377. user_data = user_data; // Happy compiler
  378. return BOOL_TRUE;
  379. }
  380. static bool_t sched_once(faux_eloop_t *eloop, faux_eloop_type_e type,
  381. void *associated_data, void *user_data)
  382. {
  383. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  384. printf("Once %d\n", info->ev_id);
  385. // Happy compiler
  386. eloop = eloop;
  387. type = type;
  388. associated_data = associated_data;
  389. user_data = user_data;
  390. return BOOL_TRUE;
  391. }
  392. static bool_t sched_periodic(faux_eloop_t *eloop, faux_eloop_type_e type,
  393. void *associated_data, void *user_data)
  394. {
  395. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  396. printf("Periodic %d\n", info->ev_id);
  397. // Happy compiler
  398. eloop = eloop;
  399. type = type;
  400. associated_data = associated_data;
  401. user_data = user_data;
  402. return BOOL_TRUE;
  403. }