daemon.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2020 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * T I N T I N + + *
  21. * *
  22. * coded by Igor van den Hoven 2019 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. #ifdef HAVE_PTY_H
  26. #include <pty.h>
  27. #else
  28. #ifdef HAVE_UTIL_H
  29. #include <util.h>
  30. #endif
  31. #endif
  32. #include <fcntl.h>
  33. #include <dirent.h>
  34. #include <termios.h>
  35. #include <sys/un.h>
  36. int get_daemon_dir(struct session *ses, char *filename);
  37. DO_COMMAND(do_daemon)
  38. {
  39. char arg1[BUFFER_SIZE];
  40. int cnt;
  41. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  42. if (*arg1 == 0)
  43. {
  44. tintin_header(ses, " DAEMON OPTIONS ");
  45. for (cnt = 0 ; *daemon_table[cnt].fun != NULL ; cnt++)
  46. {
  47. if (*daemon_table[cnt].desc)
  48. {
  49. tintin_printf2(ses, " [%-13s] %s", daemon_table[cnt].name, daemon_table[cnt].desc);
  50. }
  51. }
  52. tintin_header(ses, "");
  53. return ses;
  54. }
  55. else
  56. {
  57. for (cnt = 0 ; *daemon_table[cnt].name ; cnt++)
  58. {
  59. if (is_abbrev(arg1, daemon_table[cnt].name))
  60. {
  61. break;
  62. }
  63. }
  64. if (*daemon_table[cnt].name == 0)
  65. {
  66. do_daemon(ses, "");
  67. }
  68. else
  69. {
  70. daemon_table[cnt].fun(ses, arg);
  71. }
  72. }
  73. return ses;
  74. }
  75. DO_DAEMON(daemon_attach)
  76. {
  77. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE], filename[BUFFER_SIZE], sock_file[BUFFER_SIZE];
  78. struct dirent **dirlist;
  79. struct sockaddr_un addr_un;
  80. int size, index, pid, error, repeat = 0;
  81. struct timeval timeout;
  82. fd_set wds, rds;
  83. timeout.tv_sec = 0;
  84. timeout.tv_usec = 100000;
  85. if (gtd->attach_sock)
  86. {
  87. show_error(ses, LIST_COMMAND, "#DAEMON ATTACH: YOU ARE ALREADY ATTACHED TO {%s}.", gtd->attach_file);
  88. return;
  89. }
  90. sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_VAR|SUB_FUN);
  91. if (!get_daemon_dir(ses, filename))
  92. {
  93. return;
  94. }
  95. start:
  96. size = scandir(filename, &dirlist, 0, alphasort);
  97. if (size == -1)
  98. {
  99. syserr_printf(ses, "do_attach: scandir:");
  100. return;
  101. }
  102. for (*arg2 = index = pid = 0 ; index < size ; index++)
  103. {
  104. if (strlen(dirlist[index]->d_name) > 2)
  105. {
  106. if (*arg1)
  107. {
  108. if (!strstr(dirlist[index]->d_name, arg1))
  109. {
  110. continue;
  111. }
  112. }
  113. arg = strchr(dirlist[index]->d_name, '.');
  114. if (arg)
  115. {
  116. *arg = 0;
  117. strcpy(arg2, dirlist[index]->d_name);
  118. arg = strchr(dirlist[index]->d_name, '_');
  119. if (arg)
  120. {
  121. pid = atoi(arg + 1);
  122. }
  123. break;
  124. }
  125. }
  126. }
  127. for (index = 0 ; index < size ; index++)
  128. {
  129. free(dirlist[index]);
  130. }
  131. free(dirlist);
  132. if (pid == 0)
  133. {
  134. if (HAS_BIT(gtd->flags, TINTIN_FLAG_DAEMONIZE))
  135. {
  136. daemon_detach(ses, arg1);
  137. return;
  138. }
  139. if (*arg1 && ++repeat < 10)
  140. {
  141. usleep(2000);
  142. goto start;
  143. }
  144. if (*arg1)
  145. {
  146. show_message(ses, LIST_COMMAND, "#DAEMON ATTACH: UNABLE TO FIND DAEMON FILE {%s} IN {%s}.", arg1, filename);
  147. }
  148. else
  149. {
  150. show_message(ses, LIST_COMMAND, "#DAEMON ATTACH: NO AVAILABLE DAEMON FILES FOUND IN {%s}.", filename);
  151. }
  152. return;
  153. }
  154. sprintf(sock_file, "%s/%s.s", filename, arg2);
  155. if (access(sock_file, F_OK) == -1)
  156. {
  157. show_error(ses, LIST_COMMAND, "#ERROR: DAEMON ATTACH: FILE {%s} CANNOT BE ACCESSED.", sock_file);
  158. return;
  159. }
  160. if (kill((pid_t) pid, 0) == -1)
  161. {
  162. show_error(ses, LIST_COMMAND, "#ERROR: DAEMON ATTACH: REMOVING INVALID DAEMON FILE {%s}.", sock_file);
  163. remove(sock_file);
  164. if (HAS_BIT(gtd->flags, TINTIN_FLAG_DAEMONIZE) || *arg1 == 0)
  165. {
  166. goto start;
  167. }
  168. return;
  169. }
  170. DEL_BIT(gtd->flags, TINTIN_FLAG_DAEMONIZE);
  171. memset(&addr_un, 0, sizeof(addr_un));
  172. if (strlen(sock_file) >= sizeof(addr_un.sun_path))
  173. {
  174. show_error(ses, LIST_COMMAND, "#ERROR: #DAEMON ATTACH: {%s} FILENAME EXCEEDS MAXIMUM LENGTH OF %d.", filename, sizeof(addr_un.sun_path));
  175. return;
  176. }
  177. if (pid == getpid())
  178. {
  179. show_error(ses, LIST_COMMAND, "#ERROR: #DAEMON ATTACH: {%s} CANNOT ATTACH TO ITSELF.", filename);
  180. return;
  181. }
  182. gtd->attach_file = restringf(gtd->attach_file, "%s", sock_file);
  183. gtd->attach_pid = pid;
  184. gtd->attach_sock = socket(AF_UNIX, SOCK_STREAM, 0);
  185. if (gtd->attach_sock == -1)
  186. {
  187. syserr_printf(ses, "do_attach: %s: socket:");
  188. gtd->attach_sock = 0;
  189. return;
  190. }
  191. strcpy(addr_un.sun_path, sock_file);
  192. addr_un.sun_family = AF_UNIX;
  193. show_message(ses, LIST_COMMAND, "#DAEMON ATTACH: CONNECTING {%d} TO {%d} {%s}", getpid(), gtd->attach_pid, sock_file);
  194. /*
  195. error = select(gtd->attach_sock, NULL, &wds, NULL, &timeout);
  196. if (error == -1)
  197. {
  198. syserr_printf(ses, "do_attach: %s: select:", sock_file);
  199. return;
  200. }
  201. */
  202. if (connect(gtd->attach_sock, (struct sockaddr *)&addr_un, sizeof(addr_un)) == -1)
  203. {
  204. syserr_printf(ses, "do_attach: %s: connect:", sock_file);
  205. gtd->attach_sock = close(gtd->attach_sock);
  206. return;
  207. }
  208. FD_ZERO(&wds);
  209. FD_SET(gtd->attach_sock, &wds);
  210. error = select(FD_SETSIZE, NULL, &wds, NULL, &timeout);
  211. if (error < 0)
  212. {
  213. syserr_printf(ses, "do_attach: select wds:");
  214. show_error(ses, LIST_COMMAND, "#ERROR: #DAEMON ATTACH: UNABLE TO WRITE TO {%s}.", sock_file);
  215. gtd->attach_sock = close(gtd->attach_sock);
  216. return;
  217. }
  218. if (!FD_ISSET(gtd->attach_sock, &wds))
  219. {
  220. show_error(ses, LIST_COMMAND, "#ERROR: #DAEMON ATTACH: UNABLE TO WRITE TO {%s}.", sock_file);
  221. gtd->attach_sock = close(gtd->attach_sock);
  222. return;
  223. }
  224. FD_ZERO(&rds);
  225. FD_SET(gtd->attach_sock, &rds);
  226. error = select(FD_SETSIZE, &rds, NULL, NULL, &timeout);
  227. if (error < 0)
  228. {
  229. syserr_printf(ses, "do_attach: select rds:");
  230. gtd->attach_sock = close(gtd->attach_sock);
  231. return;
  232. }
  233. if (error == 0)
  234. {
  235. tintin_printf2(ses, "do_attach: select rds: timeout");
  236. gtd->attach_sock = close(gtd->attach_sock);
  237. return;
  238. }
  239. return;
  240. }
  241. DO_DAEMON(daemon_detach)
  242. {
  243. char arg1[BUFFER_SIZE], filename[BUFFER_SIZE];
  244. struct sockaddr_un addr_un;
  245. pid_t pid, sid;
  246. int dev_null;
  247. sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_VAR|SUB_FUN);
  248. if (gtd->detach_port)
  249. {
  250. if (gtd->detach_sock)
  251. {
  252. show_message(gtd->ses, LIST_COMMAND, "#DAEMON DETACH: DETACHING FROM {%s}", gtd->detach_file);
  253. // kill((pid_t) gtd->detach_sock, SIGTSTP);
  254. // print_stdout("%c", (char) 255);
  255. gtd->detach_sock = close(gtd->detach_sock);
  256. }
  257. else
  258. {
  259. show_error(gtd->ses, LIST_COMMAND, "#DAEMON DETACH: ALREADY FULLY DETACHED.");
  260. }
  261. return;
  262. }
  263. if (!get_daemon_dir(ses, filename))
  264. {
  265. return;
  266. }
  267. pid = fork();
  268. if (pid < 0)
  269. {
  270. syserr_printf(ses, "do_detatch: fork:");
  271. return;
  272. }
  273. if (pid > 0)
  274. {
  275. if (HAS_BIT(gtd->flags, TINTIN_FLAG_DAEMONIZE))
  276. {
  277. DEL_BIT(gtd->flags, TINTIN_FLAG_DAEMONIZE);
  278. usleep(2000);
  279. daemon_attach(ses, *arg1 ? arg1 : "pid");
  280. return;
  281. }
  282. reset_terminal(gtd->ses);
  283. print_stdout("\e[r\e[%d;%dH", gtd->screen->rows, 1);
  284. _exit(0);
  285. }
  286. DEL_BIT(gtd->flags, TINTIN_FLAG_DAEMONIZE);
  287. sid = setsid();
  288. if (sid < 0)
  289. {
  290. syserr_printf(ses, "do_detach: setsid:");
  291. return;
  292. }
  293. if (!get_daemon_dir(ses, filename))
  294. {
  295. return;
  296. }
  297. memset(&addr_un, 0, sizeof(addr_un));
  298. cat_sprintf(filename, "/%s_%d.s", *arg1 ? arg1 : "pid", getpid());
  299. if (strlen(filename) >= sizeof(addr_un.sun_path))
  300. {
  301. tintin_printf(ses, "#DAEMON DETACH: FILE NAME LENGTH OF {%s} EXCEEDS MAXIMUM OF %d.", filename, sizeof(addr_un.sun_path));
  302. return;
  303. }
  304. strcpy(addr_un.sun_path, filename);
  305. addr_un.sun_family = AF_UNIX;
  306. show_message(ses, LIST_COMMAND, "#DAEMON DETACH: DAEMONIZING PROCESS %d AS {%s}", getpid(), filename);
  307. gtd->detach_port = socket(AF_UNIX, SOCK_STREAM, 0);
  308. if (gtd->detach_port <= 0)
  309. {
  310. syserr_printf(ses, "do_detach: socket:");
  311. return;
  312. }
  313. if (bind(gtd->detach_port, (struct sockaddr *) &addr_un, sizeof(struct sockaddr_un)) < 0)
  314. {
  315. syserr_printf(ses, "do_detach: bind:");
  316. gtd->detach_port = close(gtd->detach_port);
  317. return;
  318. }
  319. if (listen(gtd->detach_port, 32) < 0)
  320. {
  321. syserr_printf(ses, "do_detach: listen:");
  322. gtd->detach_port = close(gtd->detach_port);
  323. return;
  324. }
  325. dev_null = open("/dev/null", O_RDWR, 0);
  326. if (dev_null == -1)
  327. {
  328. syserr_printf(ses, "daemon_detach: dev_null open:");
  329. return;
  330. }
  331. if (dup2(dev_null, STDIN_FILENO) == -1)
  332. {
  333. syserr_printf(ses, "daemon_detach: dup2 STDIN-fileno:");
  334. }
  335. // dup2(dev_null, STDOUT_FILENO);
  336. // dup2(dev_null, STDERR_FILENO);
  337. close(dev_null);
  338. gtd->detach_file = restringf(gtd->detach_file, "%s", filename);
  339. return;
  340. }
  341. DO_DAEMON(daemon_input)
  342. {
  343. char arg1[BUFFER_SIZE], out[BUFFER_SIZE];
  344. int size;
  345. if (*arg == 0)
  346. {
  347. show_error(ses, LIST_COMMAND, "#SYNTAX: #DAEMON INPUT <TEXT>");
  348. return;
  349. }
  350. if (gtd->attach_sock <= 0)
  351. {
  352. show_error(ses, LIST_COMMAND, "#DAEMON INPUT: YOU MUST BE ATTACHED TO A DAEMON TO SEND INPUT.");
  353. return;
  354. }
  355. get_arg_in_braces(ses, arg, arg1, GET_ALL);
  356. size = substitute(ses, arg1, out, SUB_VAR|SUB_FUN|SUB_COL|SUB_ESC|SUB_EOL);
  357. if (write(gtd->attach_sock, out, size) < 0)
  358. {
  359. gtd->attach_sock = close(gtd->attach_sock);
  360. show_message(gtd->ses, LIST_COMMAND, "#DAEMON INPUT: WRITE ERROR: UNATTACHING.");
  361. }
  362. }
  363. DO_DAEMON(daemon_kill)
  364. {
  365. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE], filename[BUFFER_SIZE], sock_file[BUFFER_SIZE];
  366. struct dirent **dirlist;
  367. int size, index, pid;
  368. sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_VAR|SUB_FUN);
  369. if (!get_daemon_dir(ses, filename))
  370. {
  371. return;
  372. }
  373. size = scandir(filename, &dirlist, 0, alphasort);
  374. if (size == -1)
  375. {
  376. syserr_printf(ses, "do_attach: scandir:");
  377. return;
  378. }
  379. for (*arg2 = index = pid = 0 ; index < size ; index++)
  380. {
  381. if (strlen(dirlist[index]->d_name) > 2)
  382. {
  383. tintin_printf2(ses, "#DAEMON KILL: CHECKING FILE {%s}", dirlist[index]->d_name);
  384. if (*arg1)
  385. {
  386. if (!strstr(dirlist[index]->d_name, arg1))
  387. {
  388. continue;
  389. }
  390. }
  391. arg = strchr(dirlist[index]->d_name, '.');
  392. if (arg)
  393. {
  394. *arg = 0;
  395. strcpy(arg2, dirlist[index]->d_name);
  396. arg = strchr(dirlist[index]->d_name, '_');
  397. if (arg)
  398. {
  399. pid = atoi(arg + 1);
  400. sprintf(sock_file, "%s/%s.s", filename, arg2);
  401. show_message(ses, LIST_COMMAND, "#DAEMON {%s} KILLED.", sock_file, pid);
  402. kill((pid_t) pid, SIGKILL);
  403. remove(sock_file);
  404. }
  405. }
  406. }
  407. }
  408. for (index = 0 ; index < size ; index++)
  409. {
  410. free(dirlist[index]);
  411. }
  412. free(dirlist);
  413. return;
  414. }
  415. DO_DAEMON(daemon_list)
  416. {
  417. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE], filename[BUFFER_SIZE], sock_file[BUFFER_SIZE];
  418. struct dirent **dirlist;
  419. int size, index, pid;
  420. sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_VAR|SUB_FUN);
  421. if (!get_daemon_dir(ses, filename))
  422. {
  423. return;
  424. }
  425. size = scandir(filename, &dirlist, 0, alphasort);
  426. if (size == -1)
  427. {
  428. syserr_printf(ses, "do_attach: scandir:");
  429. return;
  430. }
  431. tintin_printf2(ses, "#THESE DAEMONS HAVE BEEN DEFINED:");
  432. for (*arg2 = index = pid = 0 ; index < size ; index++)
  433. {
  434. if (strlen(dirlist[index]->d_name) > 2)
  435. {
  436. if (*arg1)
  437. {
  438. if (!strstr(dirlist[index]->d_name, arg1))
  439. {
  440. continue;
  441. }
  442. }
  443. arg = strchr(dirlist[index]->d_name, '.');
  444. if (arg)
  445. {
  446. *arg = 0;
  447. strcpy(arg2, dirlist[index]->d_name);
  448. arg = strchr(dirlist[index]->d_name, '_');
  449. if (arg)
  450. {
  451. pid = atoi(arg + 1);
  452. sprintf(sock_file, "%s/%s.s", filename, arg2);
  453. tintin_printf2(ses, "%-40s [%6d]", sock_file, pid);
  454. }
  455. }
  456. }
  457. }
  458. for (index = 0 ; index < size ; index++)
  459. {
  460. free(dirlist[index]);
  461. }
  462. free(dirlist);
  463. return;
  464. }
  465. int get_daemon_dir(struct session *ses, char *filename)
  466. {
  467. sprintf(filename, "%s/%s", gtd->home, TINTIN_DIR);
  468. if (mkdir(filename, 0755) && errno != EEXIST)
  469. {
  470. show_error(ses, LIST_COMMAND, "#DAEMON CHECK DIR: FAILED TO CREATE TINTIN DIR %s (%s)", filename, strerror(errno));
  471. return 0;
  472. }
  473. sprintf(filename, "%s/%s/%s", gtd->home, TINTIN_DIR, DAEMON_DIR);
  474. if (mkdir(filename, 0755) && errno != EEXIST)
  475. {
  476. show_error(ses, LIST_COMMAND, "#DAEMON CHECK DIR: CANNOT CREATE DAEMON DIR %s (%s)", filename, strerror(errno));
  477. return 0;
  478. }
  479. return 1;
  480. }
  481. void reset_daemon()
  482. {
  483. if (gtd->detach_sock > 0)
  484. {
  485. print_stdout("removing(%s)\n", gtd->detach_file);
  486. remove(gtd->detach_file);
  487. }
  488. /*
  489. if (gtd->attach_sock > 0)
  490. {
  491. print_stdout("unlinking(%s)\n", gtd->attach_file);
  492. unlink(gtd->attach_file);
  493. }
  494. */
  495. }
  496. void winch_daemon()
  497. {
  498. if (gtd->attach_sock)
  499. {
  500. kill((pid_t) gtd->attach_pid, SIGWINCH);
  501. }
  502. }