update.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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 2006 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. #include <sys/types.h>
  26. #include <sys/time.h>
  27. #include <fcntl.h>
  28. #include <termios.h>
  29. #include <sys/un.h>
  30. #include <sys/socket.h>
  31. #include <signal.h>
  32. #define TIMER_UPDATE_INPUT 0
  33. #define TIMER_UPDATE_SESSIONS 1
  34. #define TIMER_UPDATE_DELAYS 2
  35. #define TIMER_UPDATE_DAEMON 3
  36. #define TIMER_UPDATE_CHAT 4
  37. #define TIMER_UPDATE_PORT 5
  38. #define TIMER_UPDATE_TICKS 6
  39. #define TIMER_UPDATE_PATHS 7
  40. #define TIMER_UPDATE_PACKETS 8
  41. #define TIMER_UPDATE_TERMINAL 9
  42. #define TIMER_UPDATE_TIME 10
  43. #define TIMER_UPDATE_MEMORY 11
  44. #define TIMER_STALL_PROGRAM 12
  45. #define TIMER_CPU 13
  46. #define PULSE_PER_SECOND 100
  47. #define PULSE_UPDATE_INPUT 1
  48. #define PULSE_UPDATE_SESSIONS 1
  49. #define PULSE_UPDATE_DELAYS 1
  50. #define PULSE_UPDATE_DAEMON 1
  51. #define PULSE_UPDATE_CHAT 10
  52. #define PULSE_UPDATE_PORT 10
  53. #define PULSE_UPDATE_TICKS 10
  54. #define PULSE_UPDATE_PATHS 10
  55. #define PULSE_UPDATE_PACKETS 10
  56. #define PULSE_UPDATE_TERMINAL 10
  57. #define PULSE_UPDATE_MEMORY 10
  58. #define PULSE_UPDATE_TIME 10
  59. #define TIMER_UPDATE_INPUT 0
  60. #define TIMER_UPDATE_SESSIONS 1
  61. #define TIMER_UPDATE_DELAYS 2
  62. #define TIMER_UPDATE_DAEMON 3
  63. #define TIMER_UPDATE_CHAT 4
  64. #define TIMER_UPDATE_PORT 5
  65. #define TIMER_UPDATE_TICKS 6
  66. #define TIMER_UPDATE_PATHS 7
  67. #define TIMER_UPDATE_PACKETS 8
  68. #define TIMER_UPDATE_TERMINAL 9
  69. #define TIMER_UPDATE_TIME 10
  70. #define TIMER_UPDATE_MEMORY 11
  71. #define TIMER_STALL_PROGRAM 12
  72. #define TIMER_CPU 13
  73. long long cpu_timer[TIMER_CPU][5];
  74. extern void update_input(void);
  75. extern void update_sessions(void);
  76. extern void update_daemon(void);
  77. extern void update_chat(void);
  78. extern void update_port(void);
  79. extern void tick_update(void);
  80. extern void delay_update(void);
  81. extern void path_update(void);
  82. extern void packet_update(void);
  83. extern void terminal_update(void);
  84. extern void memory_update(void);
  85. extern void time_update(void);
  86. extern long long display_timer(struct session *ses, int timer);
  87. extern void open_timer(int timer);
  88. extern void close_timer(int timer);
  89. void mainloop(void)
  90. {
  91. static struct timeval wait_time;
  92. static struct pulse_type pulse;
  93. static int wait_time_val, span_time_val;
  94. static unsigned long long start_utime, end_utime;
  95. pulse.update_input = 0 + PULSE_UPDATE_INPUT;
  96. pulse.update_sessions = 0 + PULSE_UPDATE_SESSIONS;
  97. pulse.update_delays = 0 + PULSE_UPDATE_DELAYS;
  98. pulse.update_daemon = 0 + PULSE_UPDATE_DAEMON;
  99. pulse.update_chat = 2 + PULSE_UPDATE_CHAT;
  100. pulse.update_port = 2 + PULSE_UPDATE_PORT;
  101. pulse.update_ticks = 3 + PULSE_UPDATE_TICKS;
  102. pulse.update_paths = 3 + PULSE_UPDATE_PATHS;
  103. pulse.update_packets = 4 + PULSE_UPDATE_PACKETS;
  104. pulse.update_terminal = 6 + PULSE_UPDATE_TERMINAL;
  105. pulse.update_memory = 7 + PULSE_UPDATE_MEMORY;
  106. pulse.update_time = 8 + PULSE_UPDATE_TIME;
  107. push_call("mainloop()");
  108. while (TRUE)
  109. {
  110. start_utime = utime();
  111. gtd->total_io_exec += span_time_val;
  112. gtd->total_io_delay += wait_time_val;
  113. if (gtd->memory->stack_len > 0)
  114. {
  115. tintin_printf2(NULL, "\e[1;31merror: memory_stack leak detected.\n");
  116. gtd->memory->debug_len = gtd->memory->debug_max;
  117. dump_stack();
  118. gtd->memory->debug_len = 1;
  119. gtd->memory->stack_len = 0;
  120. }
  121. if (gtd->memory->debug_len > 1)
  122. {
  123. tintin_printf2(NULL, "\e[1;31merror: debug_stack leak detected.\n");
  124. gtd->memory->debug_len = gtd->memory->debug_max;
  125. dump_stack();
  126. gtd->memory->debug_len = 1;
  127. }
  128. if (--pulse.update_delays == 0)
  129. {
  130. open_timer(TIMER_UPDATE_DELAYS);
  131. pulse.update_delays = PULSE_UPDATE_DELAYS;
  132. delay_update();
  133. close_timer(TIMER_UPDATE_DELAYS);
  134. }
  135. if (--pulse.update_input == 0)
  136. {
  137. open_timer(TIMER_UPDATE_INPUT);
  138. pulse.update_input = PULSE_UPDATE_INPUT;
  139. update_input();
  140. close_timer(TIMER_UPDATE_INPUT);
  141. }
  142. if (--pulse.update_sessions == 0)
  143. {
  144. open_timer(TIMER_UPDATE_SESSIONS);
  145. pulse.update_sessions = PULSE_UPDATE_SESSIONS;
  146. update_sessions();
  147. close_timer(TIMER_UPDATE_SESSIONS);
  148. }
  149. if (--pulse.update_daemon == 0)
  150. {
  151. open_timer(TIMER_UPDATE_DAEMON);
  152. pulse.update_daemon = PULSE_UPDATE_DAEMON;
  153. update_daemon();
  154. close_timer(TIMER_UPDATE_DAEMON);
  155. }
  156. if (--pulse.update_chat == 0)
  157. {
  158. open_timer(TIMER_UPDATE_CHAT);
  159. pulse.update_chat = PULSE_UPDATE_CHAT;
  160. update_chat();
  161. close_timer(TIMER_UPDATE_CHAT);
  162. }
  163. if (--pulse.update_port == 0)
  164. {
  165. open_timer(TIMER_UPDATE_PORT);
  166. pulse.update_port = PULSE_UPDATE_PORT;
  167. update_port();
  168. close_timer(TIMER_UPDATE_PORT);
  169. }
  170. if (--pulse.update_ticks == 0)
  171. {
  172. open_timer(TIMER_UPDATE_TICKS);
  173. pulse.update_ticks = PULSE_UPDATE_TICKS;
  174. tick_update();
  175. close_timer(TIMER_UPDATE_TICKS);
  176. }
  177. if (--pulse.update_paths == 0)
  178. {
  179. open_timer(TIMER_UPDATE_PATHS);
  180. pulse.update_paths = PULSE_UPDATE_PATHS;
  181. path_update();
  182. close_timer(TIMER_UPDATE_PATHS);
  183. }
  184. if (--pulse.update_packets == 0)
  185. {
  186. open_timer(TIMER_UPDATE_PACKETS);
  187. pulse.update_packets = PULSE_UPDATE_PACKETS;
  188. packet_update();
  189. close_timer(TIMER_UPDATE_PACKETS);
  190. }
  191. if (--pulse.update_terminal == 0)
  192. {
  193. open_timer(TIMER_UPDATE_TERMINAL);
  194. pulse.update_terminal = PULSE_UPDATE_TERMINAL;
  195. terminal_update();
  196. close_timer(TIMER_UPDATE_TERMINAL);
  197. }
  198. if (--pulse.update_memory == 0)
  199. {
  200. open_timer(TIMER_UPDATE_MEMORY);
  201. pulse.update_memory = PULSE_UPDATE_MEMORY;
  202. memory_update();
  203. close_timer(TIMER_UPDATE_MEMORY);
  204. }
  205. if (--pulse.update_time == 0)
  206. {
  207. open_timer(TIMER_UPDATE_TIME);
  208. pulse.update_time = PULSE_UPDATE_TIME;
  209. time_update();
  210. close_timer(TIMER_UPDATE_TIME);
  211. }
  212. end_utime = utime();
  213. span_time_val = end_utime - start_utime;
  214. wait_time_val = 1000000 / PULSE_PER_SECOND - span_time_val;
  215. if (wait_time_val > 0)
  216. {
  217. wait_time.tv_usec = wait_time_val;
  218. select(0, NULL, NULL, NULL, &wait_time);
  219. }
  220. else
  221. {
  222. wait_time_val = 0;
  223. }
  224. }
  225. pop_call();
  226. return;
  227. }
  228. void update_input(void)
  229. {
  230. fd_set read_fd;
  231. static struct timeval timeout;
  232. static unsigned char sleep;
  233. if (gtd->time_input + 60 < gtd->time)
  234. {
  235. if (sleep < 10)
  236. {
  237. sleep++;
  238. return;
  239. }
  240. sleep = 0;
  241. }
  242. if (gtd->detach_port)
  243. {
  244. return;
  245. }
  246. while (TRUE)
  247. {
  248. FD_ZERO(&read_fd);
  249. FD_SET(STDIN_FILENO, &read_fd);
  250. if (select(FD_SETSIZE, &read_fd, NULL, NULL, &timeout) <= 0)
  251. {
  252. break;
  253. }
  254. if (!FD_ISSET(STDIN_FILENO, &read_fd))
  255. {
  256. break;
  257. }
  258. gtd->time_input = gtd->time;
  259. process_input();
  260. fflush(stdout);
  261. if (gtd->detach_port)
  262. {
  263. return;
  264. }
  265. }
  266. return;
  267. }
  268. void update_sessions(void)
  269. {
  270. fd_set read_fd, error_fd;
  271. static struct timeval timeout;
  272. static unsigned char sleep;
  273. struct session *ses;
  274. int rv;
  275. if (gtd->time_session + 10 < gtd->time)
  276. {
  277. if (sleep < 10)
  278. {
  279. sleep++;
  280. return;
  281. }
  282. sleep = 0;
  283. }
  284. if (HAS_BIT(gtd->flags, TINTIN_FLAG_WINCHUPDATE))
  285. {
  286. DEL_BIT(gtd->flags, TINTIN_FLAG_WINCHUPDATE);
  287. init_terminal_size(gts);
  288. for (ses = gts->next ; ses ; ses = ses->next)
  289. {
  290. init_terminal_size(ses);
  291. if (HAS_BIT(ses->telopts, TELOPT_FLAG_NAWS))
  292. {
  293. SET_BIT(ses->telopts, TELOPT_FLAG_UPDATENAWS);
  294. }
  295. }
  296. winch_daemon();
  297. }
  298. if (gts->next)
  299. {
  300. FD_ZERO(&read_fd);
  301. FD_ZERO(&error_fd);
  302. for (ses = gts->next ; ses ; ses = gtd->update)
  303. {
  304. gtd->update = ses->next;
  305. if (HAS_BIT(ses->flags, SES_FLAG_CONNECTED))
  306. {
  307. while (TRUE)
  308. {
  309. FD_SET(ses->socket, &read_fd);
  310. FD_SET(ses->socket, &error_fd);
  311. rv = select(FD_SETSIZE, &read_fd, NULL, &error_fd, &timeout);
  312. if (rv < 0)
  313. {
  314. break; // bug report after removal.
  315. syserr_printf(ses, "update_sessions: select:");
  316. cleanup_session(ses);
  317. gtd->mud_output_len = 0;
  318. break;
  319. }
  320. if (rv == 0)
  321. {
  322. break;
  323. }
  324. if (FD_ISSET(ses->socket, &read_fd))
  325. {
  326. if (read_buffer_mud(ses) == FALSE)
  327. {
  328. readmud(ses);
  329. cleanup_session(ses);
  330. gtd->mud_output_len = 0;
  331. break;
  332. }
  333. }
  334. if (FD_ISSET(ses->socket, &error_fd))
  335. {
  336. FD_CLR(ses->socket, &read_fd);
  337. cleanup_session(ses);
  338. gtd->mud_output_len = 0;
  339. break;
  340. }
  341. }
  342. gtd->time_session = gtd->time;
  343. if (gtd->mud_output_len)
  344. {
  345. readmud(ses);
  346. }
  347. }
  348. }
  349. }
  350. if (HAS_BIT(gtd->flags, TINTIN_FLAG_SESSIONUPDATE))
  351. {
  352. DEL_BIT(gtd->flags, TINTIN_FLAG_SESSIONUPDATE);
  353. for (ses = gts ; ses ; ses = gtd->update)
  354. {
  355. gtd->update = ses->next;
  356. if (HAS_BIT(ses->flags, SES_FLAG_PRINTLINE) && ses->check_output == 0)
  357. {
  358. DEL_BIT(ses->flags, SES_FLAG_PRINTLINE);
  359. SET_BIT(ses->flags, SES_FLAG_PRINTBUFFER);
  360. if (ses == gtd->ses)
  361. {
  362. if (HAS_BIT(ses->scroll->flags, SCROLL_FLAG_RESIZE))
  363. {
  364. buffer_refresh(ses, "", "", "");
  365. }
  366. else
  367. {
  368. print_scroll_region(ses);
  369. }
  370. }
  371. else
  372. {
  373. if (HAS_BIT(ses->flags, SES_FLAG_SNOOPSCROLL))
  374. {
  375. if (HAS_BIT(ses->scroll->flags, SCROLL_FLAG_RESIZE))
  376. {
  377. buffer_refresh(ses, "", "", "");
  378. }
  379. else
  380. {
  381. print_scroll_region(ses);
  382. }
  383. }
  384. buffer_end(ses, "", "", "");
  385. }
  386. DEL_BIT(ses->flags, SES_FLAG_PRINTBUFFER);
  387. }
  388. if (HAS_BIT(ses->flags, SES_FLAG_BUFFERUPDATE))
  389. {
  390. check_all_events(ses, EVENT_FLAG_UPDATE, 0, 0, "BUFFER UPDATE");
  391. DEL_BIT(ses->flags, SES_FLAG_BUFFERUPDATE);
  392. }
  393. }
  394. }
  395. if (HAS_BIT(gtd->flags, TINTIN_FLAG_DISPLAYUPDATE))
  396. {
  397. check_all_events(gtd->ses, EVENT_FLAG_UPDATE, 0, 0, "DISPLAY UPDATE");
  398. DEL_BIT(gtd->flags, TINTIN_FLAG_DISPLAYUPDATE);
  399. fflush(stdout);
  400. }
  401. }
  402. void update_daemon(void)
  403. {
  404. fd_set read_fd, error_fd;
  405. static struct timeval timeout;
  406. static unsigned char sleep;
  407. socklen_t len;
  408. int rv;
  409. if (gtd->time_daemon + 10 < gtd->time)
  410. {
  411. if (sleep < 10)
  412. {
  413. sleep++;
  414. return;
  415. }
  416. sleep = 0;
  417. }
  418. if (gtd->detach_port)
  419. {
  420. if (TRUE)
  421. {
  422. FD_ZERO(&read_fd);
  423. FD_SET(gtd->detach_port, &read_fd);
  424. rv = select(FD_SETSIZE, &read_fd, NULL, NULL, &timeout);
  425. if (rv > 0)
  426. {
  427. if (FD_ISSET(gtd->detach_port, &read_fd))
  428. {
  429. gtd->time_daemon = gtd->time;
  430. if (gtd->detach_sock)
  431. {
  432. tintin_printf2(gtd->ses, "#DAEMON UPDATE: ANOTHER CONNECTION IS TAKING OVER {%s}.", gtd->detach_file);
  433. kill((pid_t) gtd->detach_sock, SIGTSTP);
  434. close(gtd->detach_sock);
  435. }
  436. gtd->detach_sock = accept(gtd->detach_port, 0, 0);
  437. if (gtd->detach_sock < 0)
  438. {
  439. syserr_printf(gtd->ses, "update_daemon: detach_port: accept");
  440. gtd->detach_sock = close(gtd->detach_sock);
  441. goto attach;
  442. }
  443. if (fcntl(gtd->detach_sock, F_SETFL, O_NDELAY|O_NONBLOCK) == -1)
  444. {
  445. syserr_printf(gtd->ses, "update_daemon: detach_port: fcntl O_NDELAY|O_NONBLOCK");
  446. gtd->detach_sock = close(gtd->detach_sock);
  447. goto attach;
  448. }
  449. len = sizeof(struct process_data);
  450. if (getsockopt(gtd->detach_sock, SOL_SOCKET, SO_PEERCRED, &gtd->detach_info, &len) == -1)
  451. {
  452. syserr_printf(gtd->ses, "update_daemon: getsockopt:");
  453. gtd->detach_sock = close(gtd->detach_sock);
  454. goto attach;
  455. }
  456. if (geteuid() != gtd->detach_info.uid)
  457. {
  458. tintin_printf2(gtd->ses, "#DAEMON UPDATE: YOUR UID IS %d WHILE {%s} HAS UID {%d}.", geteuid(), gtd->detach_file, gtd->detach_info.uid);
  459. gtd->detach_sock = close(gtd->detach_sock);
  460. goto attach;
  461. }
  462. // tintin_printf2(gtd->ses, "sock=%d pid=%d, euid=%d, egid=%d", gtd->detach_port, getpid(), geteuid(), getegid());
  463. // tintin_printf2(gtd->ses, "sock=%d pid=%d, euid=%d, egid=%d", gtd->detach_sock, gtd->detach_info.pid, gtd->detach_info.uid, gtd->detach_info.gid);
  464. winch_handler(0);
  465. dirty_screen(gtd->ses);
  466. tintin_printf2(gtd->ses, "#DAEMON UPDATE: ATTACHED {%s} TO PID {%d}.", gtd->detach_file, gtd->detach_info.pid);
  467. check_all_events(gtd->ses, EVENT_FLAG_SYSTEM, 0, 2, "DAEMON ATTACHED", gtd->detach_file, ntos(gtd->detach_info.pid));
  468. }
  469. }
  470. else if (rv < 0)
  471. {
  472. if (errno != EINTR)
  473. {
  474. syserr_printf(gtd->ses, "update_daemon: select:");
  475. }
  476. }
  477. }
  478. if (gtd->detach_sock > 0)
  479. {
  480. while (gtd->detach_sock)
  481. {
  482. FD_ZERO(&read_fd);
  483. // FD_ZERO(&write_fd);
  484. FD_ZERO(&error_fd);
  485. FD_SET(gtd->detach_sock, &read_fd);
  486. // FD_SET(gtd->detach_sock, &write_fd);
  487. FD_SET(gtd->detach_sock, &error_fd);
  488. rv = select(FD_SETSIZE, &read_fd, NULL, &error_fd, &timeout);
  489. // tintin_printf2(gtd->ses, "debug: rv: %d (%d,%d,%d)\n", rv, FD_ISSET(gtd->detach_sock, &read_fd), FD_ISSET(gtd->detach_sock, &write_fd), FD_ISSET(gtd->detach_sock, &error_fd));
  490. if (rv == 0)
  491. {
  492. break;
  493. }
  494. else if (rv < 0)
  495. {
  496. FD_CLR(gtd->detach_sock, &read_fd);
  497. gtd->detach_sock = close(gtd->detach_sock);
  498. syserr_printf(gtd->ses, "update_daemon: detach_sock: select:");
  499. break;
  500. }
  501. else if (rv > 0)
  502. {
  503. if (FD_ISSET(gtd->detach_sock, &error_fd))
  504. {
  505. FD_CLR(gtd->detach_sock, &read_fd);
  506. gtd->detach_sock = close(gtd->detach_sock);
  507. show_error(gtd->ses, LIST_COMMAND, "update_daemon: detach_sock: error_fd");
  508. goto attach;
  509. }
  510. /* if (!FD_ISSET(gtd->detach_sock, &write_fd))
  511. {
  512. FD_CLR(gtd->detach_sock, &read_fd);
  513. gtd->detach_sock = close(gtd->detach_sock);
  514. show_error(gtd->ses, LIST_COMMAND, "update_daemon: detach_sock: write_fd");
  515. goto attach;
  516. }
  517. */
  518. if (!FD_ISSET(gtd->detach_sock, &read_fd))
  519. {
  520. // gtd->detach_sock = close(gtd->detach_sock); // experimental
  521. break;
  522. }
  523. gtd->time_daemon = gtd->time;
  524. process_input();
  525. }
  526. }
  527. }
  528. }
  529. attach:
  530. if (gtd->attach_sock)
  531. {
  532. FD_ZERO(&read_fd);
  533. FD_ZERO(&error_fd);
  534. FD_SET(gtd->attach_sock, &read_fd);
  535. FD_SET(gtd->attach_sock, &error_fd);
  536. rv = select(FD_SETSIZE, &read_fd, NULL, &error_fd, &timeout);
  537. if (rv < 0)
  538. {
  539. gtd->attach_sock = close(gtd->attach_sock);
  540. show_message(gtd->ses, LIST_COMMAND, "#DAEMON UPDATE: UNATTACHING {%s} DUE TO SELECT ERROR.", gtd->attach_file);
  541. }
  542. else if (rv > 0)
  543. {
  544. if (FD_ISSET(gtd->attach_sock, &read_fd))
  545. {
  546. char buffer[BUFFER_SIZE];
  547. gtd->time_daemon = gtd->time;
  548. rv = read(gtd->attach_sock, buffer, BUFFER_SIZE -1);
  549. if (rv <= 0)
  550. {
  551. gtd->attach_sock = close(gtd->attach_sock);
  552. winch_handler(0);
  553. dirty_screen(gtd->ses);
  554. show_message(gtd->ses, LIST_COMMAND, "#DAEMON UPDATE: UNATTACHING {%s}.", gtd->attach_file);
  555. }
  556. else
  557. {
  558. buffer[rv] = 0;
  559. /*
  560. if (buffer[rv - 1] == (char) 255)
  561. {
  562. gtd->attach_sock = close(gtd->attach_sock);
  563. show_message(gtd->ses, LIST_COMMAND, "\n#DAEMON {%s} SIGTSTP: UNATTACHING.", gtd->attach_file);
  564. dirty_screen(gtd->ses);
  565. return;
  566. }
  567. */
  568. if (gtd->level->quiet == 0)
  569. {
  570. printf("%s", buffer);
  571. }
  572. if (FD_ISSET(gtd->attach_sock, &error_fd))
  573. {
  574. FD_CLR(gtd->attach_sock, &read_fd);
  575. gtd->attach_sock = close(gtd->attach_sock);
  576. show_message(gtd->ses, LIST_COMMAND, "#DAEMON UPDATE: UNATTACHING {%s} DUE TO EXCEPTION ERROR.", gtd->attach_file);
  577. }
  578. }
  579. fflush(stdout); // used to be down
  580. }
  581. else
  582. {
  583. return;
  584. }
  585. }
  586. // fflush(stdout);
  587. }
  588. }
  589. void update_chat(void)
  590. {
  591. fd_set read_fd, write_fd, error_fd;
  592. static struct timeval timeout;
  593. struct chat_data *buddy, *buddy_next;
  594. int rv;
  595. if (gtd->chat)
  596. {
  597. for (buddy = gtd->chat->next ; buddy ; buddy = buddy_next)
  598. {
  599. buddy_next = buddy->next;
  600. if (buddy->timeout && buddy->timeout < gtd->time)
  601. {
  602. chat_socket_printf(buddy, "<CHAT> Connection timed out.");
  603. close_chat(buddy, TRUE);
  604. }
  605. }
  606. if (gtd->chat->paste_time && gtd->chat->paste_time < gtd->utime)
  607. {
  608. chat_paste(NULL, NULL);
  609. }
  610. FD_ZERO(&read_fd);
  611. FD_ZERO(&write_fd);
  612. FD_ZERO(&error_fd);
  613. FD_SET(gtd->chat->fd, &read_fd);
  614. for (buddy = gtd->chat->next ; buddy ; buddy = buddy->next)
  615. {
  616. FD_SET(buddy->fd, &read_fd);
  617. FD_SET(buddy->fd, &write_fd);
  618. FD_SET(buddy->fd, &error_fd);
  619. }
  620. rv = select(FD_SETSIZE, &read_fd, &write_fd, &error_fd, &timeout);
  621. if (rv <= 0)
  622. {
  623. if (rv == 0 || errno == EINTR)
  624. {
  625. return;
  626. }
  627. syserr_fatal(-1, "update_chat: select");
  628. }
  629. process_chat_connections(&read_fd, &write_fd, &error_fd);
  630. }
  631. }
  632. void update_port(void)
  633. {
  634. struct session *ses;
  635. fd_set read_fd, write_fd, error_fd;
  636. static struct timeval timeout;
  637. struct port_data *buddy;
  638. int rv;
  639. for (ses = gts->next ; ses ; ses = gtd->update)
  640. {
  641. gtd->update = ses->next;
  642. if (ses->port && ses->port->port)
  643. {
  644. FD_ZERO(&read_fd);
  645. FD_ZERO(&write_fd);
  646. FD_ZERO(&error_fd);
  647. FD_SET(ses->port->fd, &read_fd);
  648. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  649. {
  650. FD_SET(buddy->fd, &read_fd);
  651. FD_SET(buddy->fd, &write_fd);
  652. FD_SET(buddy->fd, &error_fd);
  653. }
  654. rv = select(FD_SETSIZE, &read_fd, &write_fd, &error_fd, &timeout);
  655. if (rv <= 0)
  656. {
  657. if (rv == 0 || errno == EINTR)
  658. {
  659. continue;
  660. }
  661. syserr_fatal(-1, "update_port: select");
  662. }
  663. process_port_connections(ses, &read_fd, &write_fd, &error_fd);
  664. }
  665. }
  666. }
  667. void tick_update(void)
  668. {
  669. struct session *ses;
  670. struct listnode *node;
  671. struct listroot *root;
  672. if (gtd->utime < gtd->utime_next_tick)
  673. {
  674. return;
  675. }
  676. gtd->utime_next_tick = gtd->utime + 1000000000;
  677. for (ses = gts->next ; ses ; ses = gtd->update)
  678. {
  679. gtd->update = ses->next;
  680. root = ses->list[LIST_TICKER];
  681. for (root->update = 0 ; root->update < root->used ; root->update++)
  682. {
  683. node = root->list[root->update];
  684. if (node->val64 == 0)
  685. {
  686. tintin_printf2(gtd->ses, "error: tick_update: node->val64 == 0");
  687. }
  688. if (node->val64 <= gtd->utime)
  689. {
  690. node->val64 += (long long) (get_number(ses, node->arg3) * 1000000LL);
  691. show_info(ses, LIST_TICKER, "#INFO TICK {%s} INITIALIZED WITH TIMESTAMP {%lld}", node->arg1, node->val64);
  692. if (node->val64 < gtd->utime_next_tick)
  693. {
  694. gtd->utime_next_tick = node->val64;
  695. }
  696. if (!HAS_BIT(root->flags, LIST_FLAG_IGNORE))
  697. {
  698. show_debug(ses, LIST_TICKER, "#DEBUG TICKER {%s}", node->arg2);
  699. if (node->shots && --node->shots == 0)
  700. {
  701. delete_node_list(ses, LIST_TICKER, node);
  702. }
  703. script_driver(ses, LIST_TICKER, node->arg2);
  704. }
  705. }
  706. else
  707. {
  708. if (node->val64 < gtd->utime_next_tick)
  709. {
  710. gtd->utime_next_tick = node->val64;
  711. }
  712. }
  713. }
  714. }
  715. }
  716. void delay_update(void)
  717. {
  718. struct session *ses;
  719. struct listnode *node;
  720. struct listroot *root;
  721. if (gtd->utime < gtd->utime_next_delay)
  722. {
  723. return;
  724. }
  725. gtd->utime_next_delay = gtd->utime + 1000000000;
  726. for (ses = gts ; ses ; ses = gtd->update)
  727. {
  728. gtd->update = ses->next;
  729. root = ses->list[LIST_DELAY];
  730. for (root->update = 0 ; root->update < root->used ; root->update++)
  731. {
  732. node = root->list[root->update];
  733. if (node->val64 <= gtd->utime)
  734. {
  735. show_debug(ses, LIST_DELAY, "#DEBUG DELAY {%s}", node->arg2);
  736. delete_index_list(root, root->update);
  737. script_driver(ses, LIST_DELAY, node->arg2);
  738. }
  739. else
  740. {
  741. if (node->val64 < gtd->utime_next_delay)
  742. {
  743. gtd->utime_next_delay = node->val64;
  744. }
  745. break;
  746. }
  747. }
  748. }
  749. }
  750. void path_update(void)
  751. {
  752. struct session *ses;
  753. struct listnode *node;
  754. struct listroot *root;
  755. for (ses = gts ; ses ; ses = gtd->update)
  756. {
  757. gtd->update = ses->next;
  758. root = ses->list[LIST_PATH];
  759. while (root->update < root->used)
  760. {
  761. node = root->list[root->update];
  762. if (node->val64 > 0 && node->val64 <= gtd->utime)
  763. {
  764. root->update++;
  765. node->val64 = 0;
  766. show_debug(ses, LIST_COMMAND, "#DEBUG PATH {%s}", node->arg1);
  767. script_driver(ses, LIST_COMMAND, node->arg1);
  768. if (root->update == root->used)
  769. {
  770. check_all_events(ses, EVENT_FLAG_MAP, 0, 0, "END OF RUN");
  771. }
  772. }
  773. break;
  774. }
  775. }
  776. }
  777. void packet_update(void)
  778. {
  779. struct session *ses;
  780. for (ses = gts->next ; ses ; ses = gtd->update)
  781. {
  782. gtd->update = ses->next;
  783. if (ses->check_output && gtd->utime > ses->check_output)
  784. {
  785. char result[STRING_SIZE];
  786. if (HAS_BIT(ses->flags, SES_FLAG_SPLIT))
  787. {
  788. save_pos(ses);
  789. goto_pos(ses, ses->split->bot_row, 1);
  790. }
  791. SET_BIT(ses->flags, SES_FLAG_READMUD);
  792. strcpy(result, ses->more_output);
  793. if (HAS_BIT(ses->charset, CHARSET_FLAG_ALL_TOUTF8))
  794. {
  795. all_to_utf8(ses, ses->more_output, result);
  796. }
  797. else
  798. {
  799. strcpy(result, ses->more_output);
  800. }
  801. str_cpy(&ses->more_output, "");
  802. process_mud_output(ses, result, TRUE);
  803. DEL_BIT(ses->flags, SES_FLAG_READMUD);
  804. if (HAS_BIT(ses->flags, SES_FLAG_SPLIT))
  805. {
  806. restore_pos(ses);
  807. }
  808. }
  809. if (HAS_BIT(ses->telopts, TELOPT_FLAG_UPDATENAWS))
  810. {
  811. client_send_sb_naws(ses, 0, NULL);
  812. DEL_BIT(ses->telopts, TELOPT_FLAG_UPDATENAWS);
  813. }
  814. }
  815. if (HAS_BIT(gtd->screen->flags, SCREEN_FLAG_SCROLLUPDATE))
  816. {
  817. int line = gtd->ses->scroll->line >= 0 ? gtd->ses->scroll->line : gtd->ses->scroll->used + 1;
  818. line = URANGE(1, line - get_scroll_rows(gtd->ses), gtd->ses->scroll->used);
  819. check_all_events(ses, EVENT_FLAG_UPDATE, 0, 2, "SCROLLBAR UPDATE", ntos(line), ntos(gtd->ses->scroll->used));
  820. DEL_BIT(gtd->screen->flags, SCREEN_FLAG_SCROLLUPDATE);
  821. print_stdout(0, 0, "\e[%d;%d#t", line, gtd->ses->scroll->used);
  822. }
  823. }
  824. void terminal_update(void)
  825. {
  826. struct session *ses;
  827. for (ses = gts ; ses ; ses = ses->next)
  828. {
  829. if (HAS_BIT(ses->flags, SES_FLAG_UPDATEVTMAP))
  830. {
  831. DEL_BIT(ses->flags, SES_FLAG_UPDATEVTMAP);
  832. show_vtmap(ses);
  833. check_all_events(ses, EVENT_FLAG_MAP, 0, 0, "MAP UPDATED VTMAP");
  834. }
  835. }
  836. }
  837. void memory_update(void)
  838. {
  839. while (gtd->dispose_next)
  840. {
  841. dispose_session(gtd->dispose_next);
  842. }
  843. while (gtd->dispose_list->used)
  844. {
  845. dispose_node(gtd->dispose_list->list[--gtd->dispose_list->used]);
  846. }
  847. }
  848. void time_update(void)
  849. {
  850. static char str_sec[9], str_min[9], str_hour[9], str_wday[9], str_mday[9], str_mon[9], str_year[9];
  851. static struct tm calendar, old_calendar;
  852. static short event_date, event_minute, event_second, event_time;
  853. if (gtd->time == time(NULL))
  854. {
  855. return;
  856. }
  857. gtd->time = time(NULL);
  858. if (!HAS_BIT(gtd->event_flags, EVENT_FLAG_TIME))
  859. {
  860. return;
  861. }
  862. calendar = *localtime(&gtd->time);
  863. // Initialize on the first call.
  864. if (old_calendar.tm_year == 0)
  865. {
  866. old_calendar.tm_sec = calendar.tm_sec;
  867. old_calendar.tm_min = calendar.tm_min;
  868. old_calendar.tm_hour = calendar.tm_hour;
  869. old_calendar.tm_wday = calendar.tm_wday;
  870. old_calendar.tm_mday = calendar.tm_mday;
  871. old_calendar.tm_mon = calendar.tm_mon;
  872. old_calendar.tm_year = calendar.tm_year;
  873. strftime(str_sec, 9, "%S", &calendar);
  874. strftime(str_min, 9, "%M", &calendar);
  875. strftime(str_hour, 9, "%H", &calendar);
  876. strftime(str_wday, 9, "%w", &calendar);
  877. strftime(str_mday, 9, "%d", &calendar);
  878. strftime(str_mon, 9, "%m", &calendar);
  879. strftime(str_year, 9, "%Y", &calendar);
  880. while (strcmp(event_table[++event_date].name, "DATE"))
  881. {
  882. }
  883. event_minute = event_date;
  884. while (strcmp(event_table[++event_minute].name, "MINUTE"))
  885. {
  886. }
  887. event_second = event_minute;
  888. while (strcmp(event_table[++event_second].name, "SECOND"))
  889. {
  890. }
  891. event_time = event_second;
  892. while (strcmp(event_table[++event_time].name, "TIME "))
  893. {
  894. }
  895. }
  896. strftime(str_sec, 9, "%S", &calendar);
  897. old_calendar.tm_sec = calendar.tm_sec;
  898. if (calendar.tm_min == old_calendar.tm_min)
  899. {
  900. goto time_event_sec;
  901. }
  902. strftime(str_min, 9, "%M", &calendar);
  903. old_calendar.tm_min = calendar.tm_min;
  904. if (calendar.tm_hour == old_calendar.tm_hour)
  905. {
  906. goto time_event_min;
  907. }
  908. strftime(str_hour, 9, "%H", &calendar);
  909. old_calendar.tm_hour = calendar.tm_hour;
  910. if (calendar.tm_mday == old_calendar.tm_mday)
  911. {
  912. goto time_event_hour;
  913. }
  914. strftime(str_wday, 9, "%w", &calendar);
  915. old_calendar.tm_wday = calendar.tm_wday;
  916. strftime(str_mday, 9, "%d", &calendar);
  917. old_calendar.tm_mday = calendar.tm_mday;
  918. if (calendar.tm_mon == old_calendar.tm_mon)
  919. {
  920. goto time_event_mday;
  921. }
  922. strftime(str_mon, 9, "%m", &calendar);
  923. old_calendar.tm_mon = calendar.tm_mon;
  924. if (calendar.tm_year == old_calendar.tm_year)
  925. {
  926. goto time_event_mon;
  927. }
  928. strftime(str_year, 9, "%Y", &calendar);
  929. old_calendar.tm_year = calendar.tm_year;
  930. check_all_events(NULL, EVENT_FLAG_TIME, 0, 7, "YEAR", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  931. check_all_events(NULL, EVENT_FLAG_TIME, 1, 7, "YEAR %s", str_year, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  932. time_event_mon:
  933. check_all_events(NULL, EVENT_FLAG_TIME, 0, 7, "MONTH", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  934. check_all_events(NULL, EVENT_FLAG_TIME, 1, 7, "MONTH %s", str_mon, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  935. time_event_mday:
  936. check_all_events(NULL, EVENT_FLAG_TIME, 0, 7, "WEEK", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  937. check_all_events(NULL, EVENT_FLAG_TIME, 1, 7, "WEEK %s", str_wday, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  938. check_all_events(NULL, EVENT_FLAG_TIME, 2, 7, "DATE %s-%s", str_mon, str_mday, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  939. check_all_events(NULL, EVENT_FLAG_TIME, 0, 7, "DAY", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  940. check_all_events(NULL, EVENT_FLAG_TIME, 1, 7, "DAY %s", str_mday, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  941. time_event_hour:
  942. check_all_events(NULL, EVENT_FLAG_TIME, 0, 7, "HOUR", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  943. check_all_events(NULL, EVENT_FLAG_TIME, 1, 7, "HOUR %s", str_hour, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  944. time_event_min:
  945. if (event_table[event_date].level)
  946. {
  947. check_all_events(NULL, EVENT_FLAG_TIME, 4, 7, "DATE %s-%s %s:%s", str_mon, str_mday, str_hour, str_min, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  948. }
  949. if (event_table[event_time].level)
  950. {
  951. check_all_events(NULL, EVENT_FLAG_TIME, 2, 7, "TIME %s:%s", str_hour, str_min, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  952. }
  953. if (event_table[event_minute].level)
  954. {
  955. check_all_events(NULL, EVENT_FLAG_TIME, 0, 7, "MINUTE", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  956. check_all_events(NULL, EVENT_FLAG_TIME, 1, 7, "MINUTE %s", str_min, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  957. }
  958. time_event_sec:
  959. old_calendar.tm_sec = calendar.tm_sec;
  960. if (event_table[event_time].level)
  961. {
  962. check_all_events(NULL, EVENT_FLAG_TIME, 3, 7, "TIME %s:%s:%s", str_hour, str_min, str_sec, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  963. }
  964. if (event_table[event_second].level)
  965. {
  966. check_all_events(NULL, EVENT_FLAG_TIME, 0, 7, "SECOND", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  967. check_all_events(NULL, EVENT_FLAG_TIME, 1, 7, "SECOND %s", str_sec, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  968. }
  969. return;
  970. }
  971. void show_cpu(struct session *ses)
  972. {
  973. long long total_cpu = 0;
  974. int timer;
  975. tintin_printf2(ses, "Section Time (usec) Freq (msec) %%Prog %%CPU");
  976. tintin_printf2(ses, "");
  977. for (timer = 0 ; timer < TIMER_CPU ; timer++)
  978. {
  979. total_cpu += display_timer(ses, timer);
  980. }
  981. tintin_printf2(ses, "");
  982. tintin_printf2(ses, "Unknown CPU Usage: %7.3f percent", (gtd->total_io_exec - total_cpu) * 100.0 / (gtd->total_io_delay + gtd->total_io_exec));
  983. tintin_printf2(ses, "Average CPU Usage: %7.3f percent", (gtd->total_io_exec) * 100.0 / (gtd->total_io_delay + gtd->total_io_exec));
  984. // tintin_printf2(ses, "Total CPU Usecs: %10ld", gtd->total_io_exec);
  985. // tintin_printf2(ses, "Total CPU Delay: %10ld", gtd->total_io_delay);
  986. }
  987. long long display_timer(struct session *ses, int timer)
  988. {
  989. long long total_usage, indicated_usage;
  990. total_usage = gtd->total_io_exec + gtd->total_io_delay;
  991. if (total_usage == 0)
  992. {
  993. return 0;
  994. }
  995. if (cpu_timer[timer][1] == 0 || cpu_timer[timer][4] == 0)
  996. {
  997. return 0;
  998. }
  999. // indicated_usage = cpu_timer[timer][0] / cpu_timer[timer][1] * cpu_timer[timer][4];
  1000. indicated_usage = cpu_timer[timer][0];
  1001. tintin_printf2(ses, "%-29s %8.1f %8lld %8.2f %8.3f",
  1002. timer_table[timer].name,
  1003. (double) cpu_timer[timer][0] / (double) cpu_timer[timer][1],
  1004. cpu_timer[timer][3] / cpu_timer[timer][4] / 1000,
  1005. (double) (100000 * indicated_usage / gtd->total_io_exec) / 1000.0,
  1006. (double) (100000 * indicated_usage / total_usage) / 1000.0
  1007. );
  1008. return indicated_usage;
  1009. }
  1010. void open_timer(int timer)
  1011. {
  1012. struct timeval last_time;
  1013. long long current_time;
  1014. gettimeofday(&last_time, NULL);
  1015. current_time = (long long) last_time.tv_usec + 1000000LL * (long long) last_time.tv_sec;
  1016. if (cpu_timer[timer][2] == 0)
  1017. {
  1018. cpu_timer[timer][2] = current_time;
  1019. }
  1020. else
  1021. {
  1022. cpu_timer[timer][3] += current_time - cpu_timer[timer][2];
  1023. cpu_timer[timer][2] = current_time;
  1024. cpu_timer[timer][4] ++;
  1025. }
  1026. }
  1027. void close_timer(int timer)
  1028. {
  1029. struct timeval last_time;
  1030. long long current_time;
  1031. gettimeofday(&last_time, NULL);
  1032. current_time = (long long) last_time.tv_usec + 1000000LL * (long long) last_time.tv_sec;
  1033. cpu_timer[timer][0] += (current_time - cpu_timer[timer][2]);
  1034. cpu_timer[timer][1] ++;
  1035. }