klishd.c 11 KB

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