klishd.c 11 KB

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