update.c 26 KB

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