klishd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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/argv.h>
  23. #include <faux/ini.h>
  24. #include <faux/log.h>
  25. #include <faux/sched.h>
  26. #include <faux/sysdb.h>
  27. #include <faux/net.h>
  28. #include <faux/list.h>
  29. #include <faux/conv.h>
  30. #include <faux/file.h>
  31. #include <faux/eloop.h>
  32. #include <faux/error.h>
  33. #include <klish/ktp.h>
  34. #include <klish/ktp_session.h>
  35. #include <klish/kscheme.h>
  36. #include <klish/ischeme.h>
  37. #include <klish/kcontext.h>
  38. #include <klish/ksession.h>
  39. #include <klish/kdb.h>
  40. #include <klish/kpargv.h>
  41. #include "private.h"
  42. // Local static functions
  43. static int create_listen_unix_sock(const char *path);
  44. static kscheme_t *load_all_dbs(const char *dbs,
  45. faux_ini_t *global_config, faux_error_t *error);
  46. static bool_t clear_scheme(kscheme_t *scheme, faux_error_t *error);
  47. // Main loop events
  48. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  49. void *associated_data, void *user_data);
  50. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  51. void *associated_data, void *user_data);
  52. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  53. void *associated_data, void *user_data);
  54. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  55. void *associated_data, void *user_data);
  56. static bool_t wait_for_child_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  57. void *associated_data, void *user_data);
  58. //static bool_t sched_once(faux_eloop_t *eloop, faux_eloop_type_e type,
  59. // void *associated_data, void *user_data);
  60. //static bool_t sched_periodic(faux_eloop_t *eloop, faux_eloop_type_e type,
  61. // void *associated_data, void *user_data);
  62. static bool_t fd_stall_cb(ktpd_session_t *session, void *user_data);
  63. /** @brief Main function
  64. */
  65. int main(int argc, char **argv)
  66. {
  67. int retval = -1;
  68. struct options *opts = NULL;
  69. int pidfd = -1;
  70. int logoptions = 0;
  71. faux_eloop_t *eloop = NULL;
  72. int listen_unix_sock = -1;
  73. ktpd_session_t *ktpd_session = NULL;
  74. kscheme_t *scheme = NULL;
  75. ksession_t *session = NULL;
  76. faux_error_t *error = faux_error_new();
  77. faux_ini_t *config = NULL;
  78. int client_fd = -1;
  79. // struct timespec delayed = { .tv_sec = 10, .tv_nsec = 0 };
  80. // struct timespec period = { .tv_sec = 3, .tv_nsec = 0 };
  81. // Parse command line options
  82. opts = opts_init();
  83. if (opts_parse(argc, argv, opts))
  84. goto err;
  85. // Initialize syslog
  86. logoptions = LOG_CONS;
  87. if (opts->foreground)
  88. logoptions |= LOG_PERROR;
  89. openlog(LOG_NAME, logoptions, opts->log_facility);
  90. if (!opts->verbose)
  91. setlogmask(LOG_UPTO(LOG_INFO));
  92. // Parse config file
  93. syslog(LOG_DEBUG, "Parse config file: %s\n", opts->cfgfile);
  94. if (!access(opts->cfgfile, R_OK)) {
  95. if (!(config = config_parse(opts->cfgfile, opts)))
  96. goto err;
  97. } else if (opts->cfgfile_userdefined) {
  98. // User defined config must be found
  99. fprintf(stderr, "Error: Can't find config file %s\n",
  100. opts->cfgfile);
  101. goto err;
  102. }
  103. // DEBUG: Show options
  104. opts_show(opts);
  105. syslog(LOG_INFO, "Start daemon.\n");
  106. // Fork the daemon
  107. if (!opts->foreground) {
  108. // Daemonize
  109. syslog(LOG_DEBUG, "Daemonize\n");
  110. if (daemon(0, 0) < 0) {
  111. syslog(LOG_ERR, "Can't daemonize\n");
  112. goto err;
  113. }
  114. // Write pidfile
  115. syslog(LOG_DEBUG, "Write PID file: %s\n", opts->pidfile);
  116. if ((pidfd = open(opts->pidfile,
  117. O_WRONLY | O_CREAT | O_EXCL | O_TRUNC,
  118. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
  119. syslog(LOG_WARNING, "Can't open pidfile %s: %s\n",
  120. opts->pidfile, strerror(errno));
  121. } else {
  122. char str[20];
  123. snprintf(str, sizeof(str), "%u\n", getpid());
  124. str[sizeof(str) - 1] = '\0';
  125. if (write(pidfd, str, strlen(str)) < 0)
  126. syslog(LOG_WARNING, "Can't write to %s: %s\n",
  127. opts->pidfile, strerror(errno));
  128. close(pidfd);
  129. }
  130. }
  131. // Load scheme
  132. if (!(scheme = load_all_dbs(opts->dbs, config, error))) {
  133. fprintf(stderr, "Scheme errors:\n");
  134. goto err;
  135. }
  136. // Parsing
  137. {
  138. // const char *line = "cmd o4 m7 o2 e1";
  139. const char *line = "cmd o4 o4 o4 m3 o2";
  140. kpargv_t *pargv = NULL;
  141. kpargv_pargs_node_t *p_iter = NULL;
  142. session = ksession_new(scheme, "/lowview");
  143. kpath_push(ksession_path(session), klevel_new(kscheme_find_entry_by_path(scheme, "/main")));
  144. pargv = ksession_parse_line(session, line, KPURPOSE_COMPLETION);
  145. if (pargv) {
  146. printf("Level: %lu, Command: %s, Line '%s': %s\n",
  147. kpargv_level(pargv),
  148. kpargv_command(pargv) ? kentry_name(kpargv_command(pargv)) : "<none>",
  149. line,
  150. kpargv_status_str(pargv));
  151. kparg_t *parg = NULL;
  152. p_iter = kpargv_pargs_iter(pargv);
  153. if (kpargv_pargs_len(pargv) > 0) {
  154. while ((parg = kpargv_pargs_each(&p_iter))) {
  155. printf("%s(%s) ", kparg_value(parg), kentry_name(kparg_entry(parg)));
  156. }
  157. printf("\n");
  158. }
  159. // Completions
  160. if (!kpargv_completions_is_empty(pargv)) {
  161. kentry_t *completion = NULL;
  162. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  163. printf("Completions (%s):\n", kpargv_last_arg(pargv));
  164. while ((completion = kpargv_completions_each(&citer)))
  165. printf("* %s\n", kentry_name(completion));
  166. }
  167. }
  168. kpargv_free(pargv);
  169. ksession_free(session);
  170. }
  171. // Listen socket
  172. syslog(LOG_DEBUG, "Create listen UNIX socket: %s\n", opts->unix_socket_path);
  173. listen_unix_sock = create_listen_unix_sock(opts->unix_socket_path);
  174. if (listen_unix_sock < 0)
  175. goto err;
  176. syslog(LOG_DEBUG, "Listen socket %d", listen_unix_sock);
  177. // Event loop
  178. eloop = faux_eloop_new(NULL);
  179. // Signals
  180. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  181. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  182. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  183. faux_eloop_add_signal(eloop, SIGHUP, refresh_config_ev, opts);
  184. faux_eloop_add_signal(eloop, SIGCHLD, wait_for_child_ev, NULL);
  185. // Listen socket. Waiting for new connections
  186. faux_eloop_add_fd(eloop, listen_unix_sock, POLLIN,
  187. listen_socket_ev, &client_fd);
  188. // Scheduled events
  189. // faux_eloop_add_sched_once_delayed(eloop, &delayed, 1, sched_once, NULL);
  190. // faux_eloop_add_sched_periodic_delayed(eloop, 2, sched_periodic, NULL, &period, FAUX_SCHED_INFINITE);
  191. // Main loop
  192. faux_eloop_loop(eloop);
  193. faux_eloop_free(eloop);
  194. retval = 0;
  195. err:
  196. // Print errors
  197. if (faux_error_len(error) > 0)
  198. faux_error_show(error);
  199. faux_error_free(error);
  200. // Close listen socket
  201. if (listen_unix_sock >= 0)
  202. close(listen_unix_sock);
  203. // Finish listen daemon if it's not forked service process.
  204. if (client_fd < 0) {
  205. // Free scheme
  206. clear_scheme(scheme, error);
  207. // Free command line options
  208. opts_free(opts);
  209. faux_ini_free(config);
  210. // Remove pidfile
  211. if (pidfd >= 0) {
  212. if (unlink(opts->pidfile) < 0) {
  213. syslog(LOG_ERR, "Can't remove pid-file %s: %s\n",
  214. opts->pidfile, strerror(errno));
  215. }
  216. }
  217. syslog(LOG_INFO, "Stop daemon.\n");
  218. return retval;
  219. }
  220. // ATTENTION: It's a forked service process
  221. retval = -1; // Pessimism for service process
  222. // Re-Initialize syslog
  223. openlog(LOG_SERVICE_NAME, logoptions, opts->log_facility);
  224. if (!opts->verbose)
  225. setlogmask(LOG_UPTO(LOG_INFO));
  226. ktpd_session = ktpd_session_new(client_fd, scheme, NULL);
  227. assert(ktpd_session);
  228. if (!ktpd_session) {
  229. syslog(LOG_ERR, "Can't create KTPd session\n");
  230. close(client_fd);
  231. goto err_client;
  232. }
  233. // ktpd_session_set_stall_cb(ktpd_session, fd_stall_cb, eloop);
  234. // faux_eloop_add_fd(eloop, new_conn, POLLIN, client_ev, clients);
  235. syslog(LOG_DEBUG, "New connection %d\n", client_fd);
  236. // Event loop
  237. eloop = faux_eloop_new(NULL);
  238. // Signals
  239. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  240. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  241. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  242. // FDs
  243. ktpd_session_set_stall_cb(ktpd_session, fd_stall_cb, eloop);
  244. faux_eloop_add_fd(eloop, client_fd, POLLIN, client_ev, ktpd_session);
  245. // Main service loop
  246. faux_eloop_loop(eloop);
  247. faux_eloop_free(eloop);
  248. retval = 0;
  249. err_client:
  250. ktpd_session_free(ktpd_session);
  251. // Free scheme
  252. clear_scheme(scheme, error);
  253. // Free command line options
  254. opts_free(opts);
  255. faux_ini_free(config);
  256. return retval;
  257. }
  258. static bool_t load_db(kscheme_t *scheme, const char *db_name,
  259. faux_ini_t *config, faux_error_t *error)
  260. {
  261. kdb_t *db = NULL;
  262. const char *sofile = NULL;
  263. assert(scheme);
  264. if (!scheme)
  265. return BOOL_FALSE;
  266. assert(db_name);
  267. if (!db_name)
  268. return BOOL_FALSE;
  269. // DB.libxml2.so = <so filename>
  270. if (config)
  271. sofile = faux_ini_find(config, "so");
  272. db = kdb_new(db_name, sofile);
  273. assert(db);
  274. if (!db)
  275. return BOOL_FALSE;
  276. kdb_set_ini(db, config);
  277. kdb_set_error(db, error);
  278. // Load DB plugin
  279. if (!kdb_load_plugin(db)) {
  280. faux_error_sprintf(error,
  281. "DB \"%s\": Can't load DB plugin", db_name);
  282. kdb_free(db);
  283. return BOOL_FALSE;
  284. }
  285. // Check plugin API version
  286. if ((kdb_major(db) != KDB_MAJOR) ||
  287. (kdb_minor(db) != KDB_MINOR)) {
  288. faux_error_sprintf(error,
  289. "DB \"%s\": Plugin's API version is %u.%u, need %u.%u",
  290. db_name,
  291. kdb_major(db), kdb_minor(db),
  292. KDB_MAJOR, KDB_MINOR);
  293. kdb_free(db);
  294. return BOOL_FALSE;
  295. }
  296. // Init plugin
  297. if (kdb_has_init_fn(db) && !kdb_init(db)) {
  298. faux_error_sprintf(error,
  299. "DB \"%s\": Can't init DB plugin", db_name);
  300. kdb_free(db);
  301. return BOOL_FALSE;
  302. }
  303. // Load scheme
  304. if (!kdb_has_load_fn(db) || !kdb_load_scheme(db, scheme)) {
  305. faux_error_sprintf(error,
  306. "DB \"%s\": Can't load scheme from DB plugin", db_name);
  307. kdb_fini(db);
  308. kdb_free(db);
  309. return BOOL_FALSE;
  310. }
  311. // Fini plugin
  312. if (kdb_has_fini_fn(db) && !kdb_fini(db)) {
  313. faux_error_sprintf(error,
  314. "DB \"%s\": Can't fini DB plugin", db_name);
  315. kdb_free(db);
  316. return BOOL_FALSE;
  317. }
  318. kdb_free(db);
  319. return BOOL_TRUE;
  320. }
  321. static kscheme_t *load_all_dbs(const char *dbs,
  322. faux_ini_t *global_config, faux_error_t *error)
  323. {
  324. kscheme_t *scheme = NULL;
  325. faux_argv_t *dbs_argv = NULL;
  326. faux_argv_node_t *iter = NULL;
  327. const char *db_name = NULL;
  328. bool_t retcode = BOOL_TRUE;
  329. kcontext_t *context = NULL;
  330. assert(dbs);
  331. if (!dbs)
  332. return NULL;
  333. scheme = kscheme_new();
  334. assert(scheme);
  335. if (!scheme)
  336. return NULL;
  337. dbs_argv = faux_argv_new();
  338. assert(dbs_argv);
  339. if (!dbs_argv) {
  340. kscheme_free(scheme);
  341. return NULL;
  342. }
  343. if (faux_argv_parse(dbs_argv, dbs) <= 0) {
  344. kscheme_free(scheme);
  345. faux_argv_free(dbs_argv);
  346. return NULL;
  347. }
  348. // For each DB
  349. iter = faux_argv_iter(dbs_argv);
  350. while ((db_name = faux_argv_each(&iter))) {
  351. faux_ini_t *config = NULL; // Sub-config for current DB
  352. char *prefix = NULL;
  353. prefix = faux_str_mcat(&prefix, "DB.", db_name, ".", NULL);
  354. if (config)
  355. config = faux_ini_extract_subini(global_config, prefix);
  356. if (!load_db(scheme, db_name, config, error))
  357. retcode = BOOL_FALSE;
  358. faux_ini_free(config);
  359. faux_str_free(prefix);
  360. }
  361. faux_argv_free(dbs_argv);
  362. // Something went wrong while loading DBs
  363. if (!retcode) {
  364. kscheme_free(scheme);
  365. return NULL;
  366. }
  367. // Prepare scheme
  368. context = kcontext_new(KCONTEXT_PLUGIN_INIT);
  369. retcode = kscheme_prepare(scheme, context, error);
  370. kcontext_free(context);
  371. if (!retcode) {
  372. kscheme_free(scheme);
  373. faux_error_sprintf(error, "Scheme preparing errors.\n");
  374. return NULL;
  375. }
  376. /*
  377. // Debug
  378. {
  379. kdb_t *deploy_db = NULL;
  380. // Deploy (for testing purposes)
  381. deploy_db = kdb_new("ischeme", NULL);
  382. kdb_load_plugin(deploy_db);
  383. kdb_init(deploy_db);
  384. kdb_deploy_scheme(deploy_db, scheme);
  385. kdb_fini(deploy_db);
  386. kdb_free(deploy_db);
  387. }
  388. */
  389. return scheme;
  390. }
  391. static bool_t clear_scheme(kscheme_t *scheme, faux_error_t *error)
  392. {
  393. kcontext_t *context = NULL;
  394. if (!scheme)
  395. return BOOL_TRUE; // It's not an error
  396. context = kcontext_new(KCONTEXT_PLUGIN_FINI);
  397. kscheme_fini(scheme, context, error);
  398. kcontext_free(context);
  399. kscheme_free(scheme);
  400. return BOOL_TRUE;
  401. }
  402. /** @brief Create listen socket
  403. *
  404. * Previously removes old socket's file from filesystem. Note daemon must check
  405. * for already working daemon to don't duplicate.
  406. *
  407. * @param [in] path Socket path within filesystem.
  408. * @return Socket descriptor of < 0 on error.
  409. */
  410. static int create_listen_unix_sock(const char *path)
  411. {
  412. int sock = -1;
  413. int opt = 1;
  414. struct sockaddr_un laddr = {};
  415. assert(path);
  416. if (!path)
  417. return -1;
  418. if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  419. syslog(LOG_ERR, "Can't create socket: %s\n", strerror(errno));
  420. goto err;
  421. }
  422. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
  423. syslog(LOG_ERR, "Can't set socket options: %s\n", strerror(errno));
  424. goto err;
  425. }
  426. // Remove old (lost) socket's file
  427. unlink(path);
  428. laddr.sun_family = AF_UNIX;
  429. strncpy(laddr.sun_path, path, USOCK_PATH_MAX);
  430. laddr.sun_path[USOCK_PATH_MAX - 1] = '\0';
  431. if (bind(sock, (struct sockaddr *)&laddr, sizeof(laddr))) {
  432. syslog(LOG_ERR, "Can't bind socket %s: %s\n", path, strerror(errno));
  433. goto err;
  434. }
  435. if (listen(sock, 128)) {
  436. unlink(path);
  437. syslog(LOG_ERR, "Can't listen on socket %s: %s\n", path, strerror(errno));
  438. goto err;
  439. }
  440. return sock;
  441. err:
  442. if (sock >= 0)
  443. close(sock);
  444. return -1;
  445. }
  446. /** @brief Stop main event loop.
  447. */
  448. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  449. void *associated_data, void *user_data)
  450. {
  451. // Happy compiler
  452. eloop = eloop;
  453. type = type;
  454. associated_data = associated_data;
  455. user_data = user_data;
  456. return BOOL_FALSE; // Stop Event Loop
  457. }
  458. /** @brief Wait for child processes (service processes).
  459. */
  460. static bool_t wait_for_child_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  461. void *associated_data, void *user_data)
  462. {
  463. int wstatus = 0;
  464. pid_t child_pid = -1;
  465. // Wait for any child process. Doesn't block.
  466. while ((child_pid = waitpid(-1, &wstatus, WNOHANG)) > 0) {
  467. if (WIFSIGNALED(wstatus)) {
  468. syslog(LOG_ERR, "Service process %d was terminated "
  469. "by signal: %d",
  470. child_pid, WTERMSIG(wstatus));
  471. } else {
  472. syslog(LOG_ERR, "Service process %d was terminated: %d",
  473. child_pid, WEXITSTATUS(wstatus));
  474. }
  475. }
  476. // Happy compiler
  477. eloop = eloop;
  478. type = type;
  479. associated_data = associated_data;
  480. user_data = user_data;
  481. return BOOL_TRUE;
  482. }
  483. /** @brief Re-read config file.
  484. *
  485. * This function can refresh klishd options but plugins (dbs for example) are
  486. * already inited and there is no way to re-init them on-the-fly.
  487. */
  488. static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  489. void *associated_data, void *user_data)
  490. {
  491. struct options *opts = (struct options *)user_data;
  492. faux_ini_t *ini = NULL;
  493. if (access(opts->cfgfile, R_OK) == 0) {
  494. syslog(LOG_DEBUG, "Re-reading config file \"%s\"\n", opts->cfgfile);
  495. if (!(ini = config_parse(opts->cfgfile, opts)))
  496. syslog(LOG_ERR, "Error while config file parsing.\n");
  497. } else if (opts->cfgfile_userdefined) {
  498. syslog(LOG_ERR, "Can't find config file \"%s\"\n", opts->cfgfile);
  499. }
  500. faux_ini_free(ini); // No way to use it later
  501. // Happy compiler
  502. eloop = eloop;
  503. type = type;
  504. associated_data = associated_data;
  505. return BOOL_TRUE;
  506. }
  507. static bool_t fd_stall_cb(ktpd_session_t *session, void *user_data)
  508. {
  509. faux_eloop_t *eloop = (faux_eloop_t *)user_data;
  510. assert(session);
  511. assert(eloop);
  512. faux_eloop_include_fd_event(eloop, ktpd_session_fd(session), POLLOUT);
  513. return BOOL_TRUE;
  514. }
  515. /** @brief Event on listen socket. New remote client.
  516. */
  517. static bool_t listen_socket_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  518. void *associated_data, void *user_data)
  519. {
  520. int new_conn = -1;
  521. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  522. pid_t child_pid = -1;
  523. assert(user_data);
  524. new_conn = accept(info->fd, NULL, NULL);
  525. if (new_conn < 0) {
  526. syslog(LOG_ERR, "Can't accept() new connection");
  527. return BOOL_TRUE;
  528. }
  529. // Fork new instance for newly connected client
  530. child_pid = fork();
  531. if (child_pid < 0) {
  532. close(new_conn);
  533. syslog(LOG_ERR, "Can't fork service process for client");
  534. return BOOL_TRUE;
  535. }
  536. // Parent
  537. if (child_pid > 0) {
  538. close(new_conn); // It's needed by child but not for parent
  539. syslog(LOG_ERR, "Service process for client was forked: %d",
  540. child_pid);
  541. return BOOL_TRUE;
  542. }
  543. // Child (forked service process)
  544. // Pass new ktpd_session to main programm
  545. *((int *)user_data) = new_conn;
  546. type = type; // Happy compiler
  547. eloop = eloop;
  548. // Return BOOL_FALSE to break listen parent loop. Child will create its
  549. // own loop then.
  550. return BOOL_FALSE;
  551. }
  552. static bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  553. void *associated_data, void *user_data)
  554. {
  555. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  556. ktpd_session_t *ktpd_session = (ktpd_session_t *)user_data;
  557. assert(ktpd_session);
  558. // Write data
  559. if (info->revents & POLLOUT) {
  560. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  561. if (!ktpd_session_async_out(ktpd_session)) {
  562. // Someting went wrong
  563. faux_eloop_del_fd(eloop, info->fd);
  564. syslog(LOG_ERR, "Problem with async output");
  565. }
  566. }
  567. // Read data
  568. if (info->revents & POLLIN) {
  569. if (!ktpd_session_async_in(ktpd_session)) {
  570. // Someting went wrong
  571. faux_eloop_del_fd(eloop, info->fd);
  572. syslog(LOG_ERR, "Problem with async input");
  573. }
  574. }
  575. // EOF
  576. if (info->revents & POLLHUP) {
  577. faux_eloop_del_fd(eloop, info->fd);
  578. syslog(LOG_DEBUG, "Close connection %d", info->fd);
  579. return BOOL_FALSE; // Stop event loop
  580. }
  581. type = type; // Happy compiler
  582. return BOOL_TRUE;
  583. }
  584. /*
  585. static bool_t sched_once(faux_eloop_t *eloop, faux_eloop_type_e type,
  586. void *associated_data, void *user_data)
  587. {
  588. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  589. printf("Once %d\n", info->ev_id);
  590. // Happy compiler
  591. eloop = eloop;
  592. type = type;
  593. associated_data = associated_data;
  594. user_data = user_data;
  595. return BOOL_TRUE;
  596. }
  597. */
  598. /*
  599. static bool_t sched_periodic(faux_eloop_t *eloop, faux_eloop_type_e type,
  600. void *associated_data, void *user_data)
  601. {
  602. faux_eloop_info_sched_t *info = (faux_eloop_info_sched_t *)associated_data;
  603. printf("Periodic %d\n", info->ev_id);
  604. // Happy compiler
  605. eloop = eloop;
  606. type = type;
  607. associated_data = associated_data;
  608. user_data = user_data;
  609. return BOOL_TRUE;
  610. }
  611. */