pass_communication.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /*
  2. * Copyright (c) 2019 Clementine Computing LLC.
  3. *
  4. * This file is part of PopuFare.
  5. *
  6. * PopuFare is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero 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. * PopuFare 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 Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <sys/socket.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <sys/un.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <poll.h>
  31. #include <time.h>
  32. #include <zlib.h>
  33. #include "../common/common_defs.h"
  34. #include "../commhub/commhub.h"
  35. #include "../commhub/client_utils.h"
  36. #include "passdb.h"
  37. #include "rules.h"
  38. #include "rfid_decoder.h"
  39. //----------GLOBAL STATE VARIABLES
  40. int flush_in_progress = 0; //This flag is used to tell if there is a flush in progress
  41. time_t last_sync_attempt = 0; //Time of the last sync attempt in seconds since epoch
  42. time_t last_sync_ack = 0; //Time of the last ack from the server (update, delete, or nop)
  43. int commhub_fd = -1; //File descriptor of our connection to the comm hub
  44. int server_fd = -1; //File descriptor of our connection to the sync server
  45. int zflush_in_progress = 0;
  46. void *zflush_data = NULL;
  47. int zflush_data_size = 0;
  48. int zflush_idx = 0;
  49. time_t last_piu_update = 0; //Last PIU status update
  50. pass_status real_pass_status = {0};
  51. int update_piu_status_message(int busy)
  52. {
  53. struct message_record outgoing;
  54. time_t now = time(NULL);
  55. struct tm t;
  56. char *status = "READY";
  57. int am_pm;
  58. int hour;
  59. if(busy)
  60. {
  61. status = "BUSY";
  62. }
  63. else
  64. {
  65. if( real_pass_status.flush_status == FLUSH_STATUS_APPLY )
  66. {
  67. status = "APPLY";
  68. }
  69. else if(real_pass_status.flush_status == FLUSH_STATUS_WRITE)
  70. {
  71. status = "SAVING";
  72. }
  73. else if( !rules_loaded() ||
  74. (real_pass_status.flush_status == FLUSH_STATUS_LEGACY) ||
  75. (real_pass_status.flush_status == FLUSH_STATUS_DOWNLOAD) )
  76. {
  77. status = "LOADING";
  78. }
  79. else if( !driver_stat.logged_in_driver )
  80. {
  81. status = "SEE DRIVER";
  82. }
  83. }
  84. if(commhub_fd >= 0)
  85. {
  86. localtime_r(&now, &t);
  87. if( (t.tm_hour < 12) )
  88. {
  89. am_pm = 0;
  90. if(t.tm_hour == 0)
  91. {
  92. hour = 12;
  93. }
  94. else
  95. {
  96. hour = t.tm_hour;
  97. }
  98. }
  99. else
  100. {
  101. am_pm = 1;
  102. if(t.tm_hour == 12)
  103. {
  104. hour = 12;
  105. }
  106. else
  107. {
  108. hour = t.tm_hour - 12;
  109. }
  110. }
  111. format_piu_message(&outgoing, 0, 0, 0, "%d/%d %d:%02d %s %s", t.tm_mon + 1, t.tm_mday, hour, t.tm_min, am_pm?"PM":"AM", status);
  112. send_message(commhub_fd, &outgoing);
  113. if(real_pass_status.flush_status == FLUSH_STATUS_DOWNLOAD)
  114. {
  115. format_piu_message(&outgoing, 1, 0, 0, "Downloading %d%%", real_pass_status.progress_indicator);
  116. send_message(commhub_fd, &outgoing);
  117. }
  118. else if(real_pass_status.flush_status == FLUSH_STATUS_APPLY)
  119. {
  120. format_piu_message(&outgoing, 1, 0, 0, "Applying %d%%", real_pass_status.progress_indicator);
  121. send_message(commhub_fd, &outgoing);
  122. }
  123. else if(real_pass_status.flush_status == FLUSH_STATUS_WRITE)
  124. {
  125. format_piu_message(&outgoing, 1, 0, 0, "Saving Database");
  126. send_message(commhub_fd, &outgoing);
  127. }
  128. else
  129. {
  130. format_piu_message(&outgoing, 1, 0, 0, "");
  131. send_message(commhub_fd, &outgoing);
  132. }
  133. }
  134. return 0;
  135. }
  136. //This function attempts to connect to the pass server...
  137. int connect_to_pass_server()
  138. {
  139. int fd;
  140. int retval;
  141. struct sockaddr_in addr;
  142. fd = socket(PF_INET, SOCK_STREAM, 0);
  143. if(fd < 0)
  144. return -1;
  145. addr.sin_family = AF_INET;
  146. addr.sin_port = htons(PASS_SERVER_PORT);
  147. inet_aton(PASS_SERVER_IP, &addr.sin_addr);
  148. retval = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
  149. if(retval < 0)
  150. {
  151. close(fd);
  152. return -2;
  153. }
  154. return fd;
  155. }
  156. int do_database_flush(passdb_context *ctx)
  157. {
  158. int retval;
  159. //Detach from the current pass database
  160. detach_from_passdb(ctx);
  161. //Format a new database file...
  162. format_new_passdb();
  163. retval = attach_to_passdb(ctx);
  164. if( DB_FAIL(retval) ) //If we fail to attach to our new database
  165. {
  166. fprintf(stderr, "Error (%d) attaching to pass database during flush attempt\n", retval); //report failure
  167. return retval;
  168. }
  169. else //Otherwise, report success.
  170. {
  171. printf("Pass database flushed!\n");
  172. return 0;
  173. }
  174. }
  175. int send_query_message(int fd, passdb_context *ctx)
  176. {
  177. char query[LINE_BUFFER_SIZE] = {0};
  178. int i, n;
  179. int retval;
  180. if(fd < 0)
  181. return -1;
  182. n = sprintf(query, "QUERY\t%llu\n", ctx->seq);
  183. // printf("Sending: \"%s\"\n", query);
  184. i = 0;
  185. while(i < n)
  186. {
  187. retval = send(fd, query + i, n - i, 0);
  188. if(retval <= 0)
  189. return -1;
  190. i += retval;
  191. }
  192. return 0;
  193. }
  194. int send_flushreq_message(int fd)
  195. {
  196. char query[LINE_BUFFER_SIZE] = {0};
  197. int i, n;
  198. int retval;
  199. if(fd < 0)
  200. return -1;
  201. n = sprintf(query, "QUERY\t0\n");
  202. // printf("Sending: \"%s\"\n", query);
  203. i = 0;
  204. while(i < n)
  205. {
  206. retval = send(fd, query + i, n - i, 0);
  207. if(retval <= 0)
  208. return -1;
  209. i += retval;
  210. }
  211. return 0;
  212. }
  213. // This function sends our local buspass state structure (real_pass_status) to other modules if either the force flag is set,
  214. //or if it differs from the last one received from the IPC hub.
  215. //
  216. // This function returns:
  217. //
  218. // < 0 Failure in communication (IPC socket is down...)
  219. // == 0 No news to send (force was not set and our status has not changed)
  220. // == 1 News was successfully sent.
  221. //
  222. // This is useful because often the next thing we want to do after sending a status update is update the display on the
  223. // PIU, but we only want to do that when the update represents a meaningful change which the user will notice (so as not
  224. // to flood that serial port with redundant change notifications).
  225. //
  226. int send_pass_state(int force)
  227. {
  228. struct message_record outgoing_msg;
  229. int retval;
  230. // Only actually perform the send if EITHER: The force flag has been set, OR the status message we are sending
  231. //differs from the last status message we received from ourselves via the IPC hub (which should model what other
  232. //modules think out status currently is).
  233. if(force || memcmp(&real_pass_status, &pass_stat, sizeof(real_pass_status)))
  234. {
  235. prepare_message(&outgoing_msg, MAILBOX_PASS_STATUS, &real_pass_status, sizeof(real_pass_status));
  236. retval = send_message(commhub_fd,&outgoing_msg);
  237. return (retval < 0)?retval:1;
  238. }
  239. return 0;
  240. }
  241. // This function updates our local buspass state structure (real_pass_status) from the internal state of other data structures
  242. //and using the two parameters (flush_status (see ../commhub/commhub.h for values) and a progress indicater (in percent),
  243. //a copy of our local status to other modules if it differs from the last state (using the send_pass_state() function above).
  244. int update_pass_state(int flush_status, int progress)
  245. {
  246. real_pass_status.progress_indicator = 0;
  247. switch(flush_status)
  248. {
  249. case FLUSH_STATUS_DOWNLOAD: //This means we're in the process of downloading a ZFLUSH dataset
  250. real_pass_status.flush_status = FLUSH_STATUS_DOWNLOAD;
  251. if(progress == 0) //if no progress indicator number is supplied
  252. {
  253. if(zflush_data_size != 0) //and it is possible to calculate
  254. {
  255. //use our download count / expected size to calculate a percentage
  256. real_pass_status.progress_indicator = (100 * zflush_idx) / zflush_data_size;
  257. }
  258. }
  259. else //if a progress indicator is supplied, simnply use it...
  260. {
  261. real_pass_status.progress_indicator = progress;
  262. }
  263. break;
  264. case FLUSH_STATUS_APPLY: //In the case of APPLY, WRITE, or LEGACY, just take the progress number on faith
  265. case FLUSH_STATUS_WRITE:
  266. case FLUSH_STATUS_LEGACY:
  267. {
  268. real_pass_status.flush_status = flush_status;
  269. real_pass_status.progress_indicator = progress;
  270. }
  271. break;
  272. default: //Otherwise, just let the other modules know we're feeling normal today...
  273. real_pass_status.flush_status = FLUSH_STATUS_NORMAL;
  274. break;
  275. }
  276. //inform other modules of any changes...
  277. return send_pass_state(0);
  278. }
  279. int do_zflush(passdb_context *ctx, void *data, int len);
  280. //Cleanup after (or abort in progress) a ZFLUSH attempt
  281. int zflush_cleanup()
  282. {
  283. zflush_idx = 0;
  284. flush_in_progress = 0;
  285. zflush_in_progress = 0;
  286. zflush_data_size = 0;
  287. if(zflush_data)
  288. {
  289. free(zflush_data);
  290. }
  291. zflush_data = NULL;
  292. return 0;
  293. }
  294. int handle_pass_update_request(char *line, passdb_context *ctx, int sync)
  295. {
  296. rider_record foo = {0};
  297. char buffer[LINE_BUFFER_SIZE];
  298. int input_idx = 0;
  299. int eol = 0;
  300. memset(buffer, 0, sizeof(char)*LINE_BUFFER_SIZE);
  301. //Extract the first tab-delimited field from the input line...
  302. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  303. //If that field is blank, then we ignore this line
  304. if( buffer[0] == '\0' )
  305. {
  306. return 0;
  307. }
  308. if( !strcasecmp(buffer, "UPDATE") ) //========================================== UPDATE
  309. {
  310. //------------------------------------------------------------------ seq
  311. if( eol )
  312. {
  313. fprintf(stderr, "UPDATE: Premature end of line!\n");
  314. return -1;
  315. }
  316. //Get the next field (this should be sequence number...)
  317. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  318. foo.seq = strtoull(buffer, NULL, 10);
  319. if( foo.seq == 0 )
  320. {
  321. fprintf(stderr, "UPDATE: Blank or zero sequence number not allowed!\n");
  322. return -1;
  323. }
  324. //------------------------------------------------------------------ ID
  325. if( eol )
  326. {
  327. fprintf(stderr, "UPDATE: Premature end of line!\n");
  328. return -1;
  329. }
  330. //Get the next field (this should be Rider ID...)
  331. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  332. foo.id = strtoull(buffer, NULL, 10);
  333. if( foo.id == 0 )
  334. {
  335. fprintf(stderr, "UPDATE: Blank or zero rider ID not allowed!\n");
  336. return -1;
  337. }
  338. //------------------------------------------------------------------ Magstripe
  339. if( eol )
  340. {
  341. fprintf(stderr, "UPDATE: Premature end of line!\n");
  342. return -1;
  343. }
  344. //Get the next field (this should be the Magstripe Value...)
  345. input_idx += get_field(foo.magstripe_value, line + input_idx, sizeof(foo.magstripe_value), &eol);
  346. //------------------------------------------------------------------ RFID
  347. if( eol )
  348. {
  349. fprintf(stderr, "UPDATE: Premature end of line!\n");
  350. return -1;
  351. }
  352. //Get the next field (this should be the RFID Value...)
  353. input_idx += get_field(foo.rfid_value, line + input_idx, sizeof(foo.rfid_value), &eol);
  354. //------------------------------------------------------------------ Rule Name
  355. if( eol )
  356. {
  357. fprintf(stderr, "UPDATE: Premature end of line!\n");
  358. return -1;
  359. }
  360. //Get the next field (this should be the Rule Name...)
  361. input_idx += get_field(foo.rule_name, line + input_idx, sizeof(foo.rule_name), &eol);
  362. //------------------------------------------------------------------ Rule Parameter
  363. if( eol )
  364. {
  365. fprintf(stderr, "UPDATE: Premature end of line!\n");
  366. return -1;
  367. }
  368. //Get the next field (this should be the Rule Parameter...)
  369. input_idx += get_field(foo.rule_param, line + input_idx, sizeof(foo.rule_param), &eol);
  370. //If we have extra fields, that's BAD NEWS!
  371. if( !eol )
  372. {
  373. fprintf(stderr, "UPDATE: Too many fields!\n");
  374. return -1;
  375. }
  376. //If everything else is okay, go ahead and update our local database
  377. last_sync_ack = time(NULL);
  378. return update_rider(ctx, &foo, sync); //returning the status of that operation
  379. }
  380. else if( !strcasecmp(buffer, "DELETE") ) //========================================== DELETE
  381. {
  382. //------------------------------------------------------------------ seq
  383. if( eol )
  384. {
  385. fprintf(stderr, "DELETE: Premature end of line!\n");
  386. return -1;
  387. }
  388. //Get the next field (this should be sequence number...)
  389. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  390. foo.seq = strtoull(buffer, NULL, 10);
  391. if( foo.seq == 0 )
  392. {
  393. fprintf(stderr, "DELETE: Blank or zero sequence number not allowed!\n");
  394. return -1;
  395. }
  396. //------------------------------------------------------------------ ID
  397. if( eol )
  398. {
  399. fprintf(stderr, "DELETE: Premature end of line!\n");
  400. return -1;
  401. }
  402. //Get the next field (this should be Rider ID...)
  403. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  404. foo.id = strtoull(buffer, NULL, 10);
  405. if( foo.id == 0 )
  406. {
  407. fprintf(stderr, "DELETE: Blank or zero rider ID not allowed!\n");
  408. return -1;
  409. }
  410. //If we have extra fields, that's BAD NEWS!
  411. if( !eol )
  412. {
  413. fprintf(stderr, "DELETE: Too many fields!\n");
  414. return -1;
  415. }
  416. //If everything else is okay, go ahead and update our local database
  417. last_sync_ack = time(NULL);
  418. return delete_rider(ctx, &foo, sync); //returning the status of that operation
  419. }
  420. else if( !strcasecmp(buffer, "FLUSH") ) //========================================== FLUSH
  421. {
  422. flush_in_progress = 1;
  423. if(update_pass_state(FLUSH_STATUS_LEGACY, 0) > 0)
  424. {
  425. update_piu_status_message(0);
  426. }
  427. fprintf(stderr, "Legacy Flush Requested!\n");
  428. last_sync_ack = time(NULL);
  429. return do_database_flush(ctx);
  430. }
  431. else if( !strcasecmp(buffer, "FLUSHDONE") ) //========================================== FLUSHDONE
  432. {
  433. flush_in_progress = 0;
  434. if(update_pass_state(FLUSH_STATUS_NORMAL, 0) > 0)
  435. {
  436. update_piu_status_message(0);
  437. }
  438. fprintf(stderr, "Legacy Flush Done!\n");
  439. last_sync_ack = time(NULL);
  440. return 0;
  441. }
  442. else if( !strcasecmp(buffer, "ZFLUSH") ) //========================================== FLUSH
  443. {
  444. //------------------------------------------------------------------ seq
  445. if( eol )
  446. {
  447. fprintf(stderr, "ZFLUSH: Premature end of line!\n");
  448. return -1;
  449. }
  450. //Get the next field (this should be sequence number...)
  451. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  452. zflush_data_size = strtoull(buffer, NULL, 10);
  453. zflush_data = malloc(zflush_data_size);
  454. if(zflush_data == NULL)
  455. {
  456. fprintf(stderr, "Can't malloc %d bytes for ZFLUSH!\n", zflush_data_size);
  457. return FAIL_MEM;
  458. }
  459. printf("ZFLUSH: Downloading %d bytes.\n", zflush_data_size);
  460. zflush_idx = 0;
  461. zflush_in_progress = 1;
  462. flush_in_progress = 1;
  463. if(update_pass_state(FLUSH_STATUS_DOWNLOAD, 0) > 0) //Let update_pass_state() calculate our download percentage
  464. {
  465. update_piu_status_message(0);
  466. }
  467. last_sync_ack = time(NULL);
  468. return 0;
  469. }
  470. else if( !strcasecmp(buffer, "ZFLUSHDONE") ) //========================================== FLUSHDONE
  471. {
  472. fprintf(stderr, "ZFlush applying...\n");
  473. if(update_pass_state(FLUSH_STATUS_APPLY, 0) > 0) //Mark that we're starting an apply operation
  474. {
  475. update_piu_status_message(0);
  476. }
  477. do_zflush(ctx, zflush_data, zflush_data_size);
  478. zflush_cleanup();
  479. if(update_pass_state(FLUSH_STATUS_NORMAL, 0) > 0)
  480. {
  481. update_piu_status_message(0);
  482. }
  483. fprintf(stderr, "ZFlush Done!\n");
  484. last_sync_ack = time(NULL);
  485. return 0;
  486. }
  487. else if( !strcasecmp(buffer, "NOP") ) //========================================== NOP (this just updates ACK time)
  488. {
  489. last_sync_ack = time(NULL);
  490. return 0;
  491. }
  492. else
  493. {
  494. fprintf(stderr, "Unrecognized command: %s\n", buffer);
  495. return -1;
  496. }
  497. }
  498. int do_zflush(passdb_context *ctx, void *data, int len)
  499. {
  500. char line[LINE_BUFFER_SIZE] = {0};
  501. z_stream foo = {0};
  502. int zretval;
  503. int retval;
  504. int i,j,k;
  505. int nout;
  506. struct message_record incoming_msg;
  507. foo.zalloc = Z_NULL;
  508. foo.zfree = Z_NULL;
  509. foo.opaque = Z_NULL;
  510. foo.next_in = zflush_data;
  511. foo.avail_in = zflush_data_size;
  512. foo.next_out = (void *)line;
  513. foo.avail_out = sizeof(line);
  514. foo.total_out = 0;
  515. // printf("do_zflush(%p, %p, %d)\n", ctx, data, len);
  516. retval = inflateInit(&foo);
  517. if(retval != Z_OK)
  518. {
  519. fprintf(stderr, "inflateInit failed: Zlib error %d\n", retval);
  520. return FAIL_DATABASE;
  521. }
  522. retval = do_database_flush(ctx);
  523. if( DB_FAIL(retval) )
  524. {
  525. fprintf(stderr, "Database failure during ZFLUSH while performing flush\n");
  526. return retval;
  527. }
  528. do
  529. {
  530. RESET_WATCHDOG();
  531. //Respond to PING messages from the supervisor process lest we get killed off too early...
  532. while(message_socket_status(commhub_fd, MSG_RECV) & MSG_RECV)
  533. {
  534. if(get_message(commhub_fd, &incoming_msg) >= 0)
  535. {
  536. process_message(&incoming_msg);
  537. }
  538. }
  539. zretval = inflate(&foo, Z_NO_FLUSH);
  540. if( (zretval == Z_NEED_DICT) || (zretval == Z_DATA_ERROR) || (zretval == Z_MEM_ERROR) || (zretval == Z_STREAM_ERROR) )
  541. {
  542. fprintf(stderr, "Error inflating data: Zlib error %d\n", zretval);
  543. inflateEnd(&foo);
  544. return FAIL_DATABASE;
  545. }
  546. //The number of output bytes generated is the size of the buffer - number of free bytes
  547. nout = sizeof(line) - foo.avail_out;
  548. // printf("-- %d output bytes\n", nout);
  549. j = 0;
  550. //Scan through this buffer looking for EOL characters
  551. for(i=0; i < nout; i++)
  552. {
  553. if(line[i] == '\n') //if we've found one
  554. {
  555. line[i] = '\0'; //make it a terminating nul
  556. // printf("ZFLUSH->%s\n", line + j);
  557. //handle our pass update
  558. #ifdef ZFLUSH_SLOW_BUT_SAFE
  559. //In slow but safe mode, we msync() (or write()) the memory page containing each record after each update.
  560. //This is hell on the flash, and it takes a long time, but it means that we cannot POSSIBLY write a lower
  561. //sequence number record to the physical storage media before having physically written all previous
  562. //sequence numbers in the batch.
  563. retval = handle_pass_update_request(line + j, ctx, 1);
  564. #else
  565. //In fast mode, we apply all of our update to the memory window without calling msync() or write(), and then
  566. //(see below) when we're done rewriting the mmap()'d window for the local database, then we make one call
  567. //to msync() or write() and splat it all down to flash at once. This gets us a better compression ratio
  568. //on the large chunks of file which change infrequently and it produces less
  569. //filesystem fragmentation, but it means that it is theoretically possible for a power event to cause a
  570. //partial write to corrupt the local database (requiring a new flush). That being said, when running on
  571. //top of JFFS2, this should cause file truncation, which will be easily detectable and trigger another
  572. //flush request next time the application comes up.
  573. retval = handle_pass_update_request(line + j, ctx, 0);
  574. #endif
  575. if( DB_FAIL(retval) )
  576. {
  577. inflateEnd(&foo);
  578. fprintf(stderr, "Database failure during ZFLUSH while processing line \"%s\"\n", line + j);
  579. return retval;
  580. }
  581. //mark the first character of the next line
  582. j = i + 1;
  583. }
  584. }
  585. //k will be equal to the number of leftover bytes
  586. k = i - j;
  587. // printf("-- i = %d j = %d k = %d\n", i, j, k);
  588. //copy those leftovers back to the beginning
  589. for(i = 0; i < k; i++)
  590. {
  591. line[i] = line[j + i];
  592. }
  593. //and update our output buffer structure
  594. foo.next_out = (void *)(line + k);
  595. foo.avail_out = sizeof(line) - k;
  596. if(foo.avail_out == 0)
  597. {
  598. fprintf(stderr, "Line too long in decompressed ZFLUSH data!\n");
  599. inflateEnd(&foo);
  600. return FAIL_DATABASE;
  601. }
  602. // Update the other modules with our status (APPLY) and a progress indicator based on the amount of compressed data
  603. //read as a percentage of the total ZFLUSH data blob.
  604. if(update_pass_state(FLUSH_STATUS_APPLY, (foo.total_in * 100) / zflush_data_size) > 0)
  605. {
  606. update_piu_status_message(0);
  607. }
  608. } while(zretval != Z_STREAM_END);
  609. inflateEnd(&foo);
  610. //In fast mode, we have to ensure that we write our big block of changes back to secondary storage all at once
  611. #ifndef ZFLUSH_SLOW_BUT_SAFE
  612. if(update_pass_state(FLUSH_STATUS_WRITE, 0) > 0) //Let the other modules know we're performing the final write...
  613. {
  614. update_piu_status_message(0);
  615. }
  616. fprintf(stderr, "Doing one-shot file write...\n");
  617. // This resets the watchdog timer for a double-long timeframe (120 seconds these days...) allowing the write to be
  618. //slow as dirt (this is rarely needed, but if the flash was badly fragmented it can take some time to coalesce free
  619. //blocks and possibly perform some erase cycles.
  620. RESET_WATCHDOG_LONG(2);
  621. // This calls msync() if we're working with a non-broken implementation of mmap() (i.e. non-jffs2) or write()
  622. //to take our updated state from RAM and save it to the underlying file.
  623. sync_all_riders(ctx);
  624. fprintf(stderr, "Done.\n");
  625. #endif
  626. // After completing a ZFLUSH operation, we want to hang up on the server (and reconnect asap). The server should
  627. //also hang up on us after completing transmission of a ZFLUSH dataset. This effectively clears the buffers going both
  628. //ways which prevents tha case where a high communication (or server-side database) lag causes two "QUERY 0" messages
  629. //to be issued in a row, the first one triggering (correctly) a ZFLUSH which will be completed, then the server will read
  630. //the second (now obsolete) "QUERY 0" message and respond to it with another (now just useless and annoying) ZFLUSH.
  631. close(server_fd); //We just do a rude close... The server does a proper shutdown(fd, 2), and the FIN will have reached
  632. //us LONG before we execute this...
  633. server_fd = -1; //Mark the TCP connection to the server as dead so we flush buffers and restart the connection next
  634. //time through the loop.
  635. return 0;
  636. }
  637. //callback handles MAILBOX_UPDATE_PASSES
  638. message_callback_return handle_update_passes_message(struct message_record *msg, void *param)
  639. {
  640. //flag our last sync attempt as NEVER
  641. last_sync_attempt = 0;
  642. return MESSAGE_HANDLED_CONT;
  643. }
  644. //callback handles MAILBOX_FLUSH_PASSES
  645. message_callback_return handle_flush_passes_message(struct message_record *msg, void *param)
  646. {
  647. //We only want to allow a FLUSH_PASSES request to go through if the following conditions are met:
  648. if( (server_fd >= 0) && //1: We have a connection to the server
  649. (tunnel_is_up()) && //2: That connection is real (i.e. the SSH tunnel is in a known good state)
  650. (real_pass_status.flush_status == FLUSH_STATUS_NORMAL) //3: we are not currently in the middle of doing a flush...
  651. )
  652. {
  653. send_flushreq_message(server_fd);
  654. }
  655. return MESSAGE_HANDLED_CONT;
  656. }
  657. //callback handles MAILBOX_STATUS_REQUEST
  658. message_callback_return handle_status_request_message(struct message_record *msg, void *param)
  659. {
  660. // If we're responding to a status request message, we have to force a send even if it seems redundant because odds are
  661. //the requesting module just came on line and has no current status data for us.
  662. send_pass_state(1);
  663. return MESSAGE_HANDLED_CONT;
  664. }
  665. int reject_unknown_card(char *cred)
  666. {
  667. struct message_record bill_msg;
  668. struct message_record user_msg;
  669. struct message_record rider_msg;
  670. format_piu_message(&rider_msg, 1, PIU_PRIORITY_FARE, PASSENGER_MESSAGE_DURATION, "%s %s", REJECT_STR, "UNKNOWN CARD");
  671. format_driver_message(&user_msg, LOGLEVEL_REJECT, "Unknown card");
  672. format_billing_message(&bill_msg, REJECT_STR, "UNKNOWN-CARD", "", "Unknown card", cred, 0, 0);
  673. if(commhub_fd >= 0)
  674. {
  675. send_message(commhub_fd, &bill_msg);
  676. send_message(commhub_fd, &user_msg);
  677. send_message(commhub_fd, &rider_msg);
  678. return 0;
  679. }
  680. else
  681. {
  682. return -1;
  683. }
  684. }
  685. #ifdef REJECT_IF_NO_DRIVER
  686. int reject_no_driver()
  687. {
  688. struct message_record user_msg;
  689. struct message_record rider_msg;
  690. format_piu_message(&rider_msg, 1, PIU_PRIORITY_FARE, PASSENGER_MESSAGE_DURATION, "%s", "SEE DRIVER");
  691. format_driver_message(&user_msg, LOGLEVEL_REJECT, "Not Logged In!");
  692. if(commhub_fd >= 0)
  693. {
  694. send_message(commhub_fd, &user_msg);
  695. send_message(commhub_fd, &rider_msg);
  696. return 0;
  697. }
  698. else
  699. {
  700. return -1;
  701. }
  702. }
  703. #endif
  704. message_callback_return handle_token_mag_message(struct message_record *msg, void *param)
  705. {
  706. int idx;
  707. char cred[LINE_BUFFER_SIZE] = {0};
  708. passdb_context *ctx = (passdb_context *)param;
  709. if(!ctx)
  710. return MESSAGE_HANDLED_CONT;
  711. update_piu_status_message(1);
  712. #ifdef REJECT_IF_NO_DRIVER
  713. if( (driver_stat.logged_in_driver <= 0) || (stop_stat.paddle <= 0) )
  714. {
  715. reject_no_driver();
  716. return MESSAGE_HANDLED_CONT;
  717. }
  718. #endif
  719. idx = smart_find_mag(ctx, (char *)msg->payload, cred);
  720. if(idx < 0)
  721. {
  722. reject_unknown_card(cred);
  723. }
  724. else
  725. {
  726. process_rider(ctx, idx, cred);
  727. }
  728. return MESSAGE_HANDLED_CONT;
  729. }
  730. message_callback_return handle_token_rfid_message(struct message_record *msg, void *param)
  731. {
  732. int idx;
  733. char cred[LINE_BUFFER_SIZE] = {0};
  734. passdb_context *ctx = (passdb_context *)param;
  735. if(!ctx)
  736. return MESSAGE_HANDLED_CONT;
  737. update_piu_status_message(1);
  738. #ifdef REJECT_IF_NO_DRIVER
  739. if( (driver_stat.logged_in_driver <= 0) || (stop_stat.paddle <= 0) )
  740. {
  741. reject_no_driver();
  742. return MESSAGE_HANDLED_CONT;
  743. }
  744. #endif
  745. idx = smart_find_rf(ctx, (char *)msg->payload, cred);
  746. if(idx < 0)
  747. {
  748. reject_unknown_card(cred);
  749. }
  750. else
  751. {
  752. process_rider(ctx, idx, cred);
  753. }
  754. return MESSAGE_HANDLED_CONT;
  755. }
  756. message_callback_return update_anti_passback_cache(struct message_record *msg, void *param)
  757. {
  758. apb_flush_if_needed();
  759. return MESSAGE_HANDLED_CONT;
  760. }
  761. message_callback_return handle_rule_call(struct message_record *msg, void *param)
  762. {
  763. process_driver_rulecall( (driver_rulecall *)msg->payload );
  764. return MESSAGE_HANDLED_CONT;
  765. }
  766. void maintain_ipc_hub_connect(char *progname)
  767. {
  768. struct message_record outgoing_msg;
  769. if(commhub_fd < 0) //if we have no connection to the communication hub
  770. {
  771. commhub_fd = connect_to_message_server(progname); //try and get one
  772. // printf("commhub_fd = %d\n", commhub_fd);
  773. if(commhub_fd >= 0) //if it worked
  774. {
  775. //Subscribe to the command mailboxes we act on
  776. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_TOKEN_MAG, strlen(MAILBOX_TOKEN_MAG));
  777. send_message(commhub_fd,&outgoing_msg);
  778. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_TOKEN_RFID, strlen(MAILBOX_TOKEN_RFID));
  779. send_message(commhub_fd,&outgoing_msg);
  780. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_FLUSH_PASSES, strlen(MAILBOX_FLUSH_PASSES));
  781. send_message(commhub_fd,&outgoing_msg);
  782. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_UPDATE_PASSES, strlen(MAILBOX_UPDATE_PASSES));
  783. send_message(commhub_fd,&outgoing_msg);
  784. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_RULE_CALL, strlen(MAILBOX_RULE_CALL));
  785. send_message(commhub_fd,&outgoing_msg);
  786. //Subscribe to the relevant status management mailboxes
  787. subscribe_to_default_messages(commhub_fd);
  788. //Request updated status information...
  789. prepare_message(&outgoing_msg, MAILBOX_STATUS_REQUEST, "", 0);
  790. send_message(commhub_fd,&outgoing_msg);
  791. }
  792. }
  793. }
  794. int main(int argc, char **argv)
  795. {
  796. struct pollfd fds[2];
  797. int nfds = 0;
  798. int poll_return = 0;
  799. int read_return = 0;
  800. int i;
  801. struct message_record incoming_msg;
  802. char input_line[LINE_BUFFER_SIZE] = {0};
  803. int input_idx = 0;
  804. int checked_idx = 0;
  805. passdb_context ctx = {0};
  806. //------------------
  807. configure_signal_handlers(argv[0]);
  808. maintain_ipc_hub_connect(argv[0]);
  809. read_return = attach_to_passdb(&ctx);
  810. if( read_return < 0 )
  811. {
  812. fprintf(stderr, "Database is missing or corrupt. Attempting to format new database.\n");
  813. read_return = format_new_passdb();
  814. if( read_return < 0 )
  815. {
  816. fprintf(stderr, "Database cannot be created. Aborting!\n");
  817. return -1;
  818. }
  819. else
  820. {
  821. read_return = attach_to_passdb(&ctx);
  822. if( read_return < 0 )
  823. {
  824. fprintf(stderr, "New database is ALSO missing or corrupt. Aborting.\n");
  825. return -1;
  826. }
  827. }
  828. }
  829. //Register our default keep-up-with-system status callbacks
  830. register_system_status_callbacks();
  831. //Add our module-specific callbacks
  832. register_dispatch_callback(MAILBOX_UPDATE_PASSES, CALLBACK_USER(1), handle_update_passes_message, NULL);
  833. register_dispatch_callback(MAILBOX_FLUSH_PASSES, CALLBACK_USER(2), handle_flush_passes_message, NULL);
  834. register_dispatch_callback(MAILBOX_STATUS_REQUEST, CALLBACK_USER(3), handle_status_request_message, NULL);
  835. register_dispatch_callback(MAILBOX_TOKEN_RFID, CALLBACK_USER(4), handle_token_rfid_message, &ctx);
  836. register_dispatch_callback(MAILBOX_TOKEN_MAG, CALLBACK_USER(5), handle_token_mag_message, &ctx);
  837. register_dispatch_callback(MAILBOX_RULE_CALL, CALLBACK_USER(6), handle_rule_call, &ctx);
  838. //Handle status updates which require us to check expiration of the anti-passback cache
  839. register_dispatch_callback(MAILBOX_GPS_STATUS, CALLBACK_USER(6), update_anti_passback_cache, NULL);
  840. register_dispatch_callback(MAILBOX_STOP_STATUS, CALLBACK_USER(7), update_anti_passback_cache, NULL);
  841. while( exit_request_status == EXIT_REQUEST_NONE )
  842. {
  843. time_t now = time(NULL);
  844. RESET_WATCHDOG();
  845. //DEBUG
  846. printf("[%lli] passdb: heartbeat\n", get_usec_time());
  847. //DEBUG
  848. maintain_ipc_hub_connect(argv[0]);
  849. if( (now - last_piu_update) > 0)
  850. {
  851. update_piu_status_message(0);
  852. last_piu_update = now;
  853. }
  854. if(server_fd < 0) //If we don't have a connection to the sync server...
  855. {
  856. if( (now - last_sync_attempt) > DEFAULT_CONNECT_RETRY ) //See if it is time to try again
  857. {
  858. if( tunnel_is_up() ) //and if the tunnel thinks it is up
  859. {
  860. server_fd = connect_to_pass_server(); //if so, try again...
  861. if(server_fd >= 0) //if it worked
  862. {
  863. input_idx = 0; //reset our buffer index
  864. last_sync_attempt = 0;
  865. zflush_cleanup();;
  866. }
  867. else
  868. {
  869. last_sync_attempt = now;
  870. }
  871. }
  872. }
  873. }
  874. if(!rules_loaded())
  875. {
  876. load_rules(RULES_FILE);
  877. }
  878. if(rfid_pattern_loaded() <= 0)
  879. {
  880. load_rfid_decode_patterns(RFID_PATTERN_FILE);
  881. }
  882. if(hup_request_status)
  883. {
  884. hup_request_status = 0;
  885. unload_rules();
  886. load_rules(RULES_FILE);
  887. load_rfid_decode_patterns(RFID_PATTERN_FILE);
  888. }
  889. nfds=0;
  890. if(server_fd >= 0)
  891. {
  892. fds[nfds].fd = server_fd;
  893. fds[nfds].events = POLLIN;
  894. if( (now - last_sync_attempt) > DEFAULT_PASS_SYNC_RETRY )
  895. {
  896. // printf("We want to sync...\n");
  897. fds[nfds].events |= POLLOUT;
  898. }
  899. nfds++;
  900. }
  901. if(commhub_fd >= 0)
  902. {
  903. fds[nfds].fd = commhub_fd;
  904. fds[nfds].events = POLLIN;
  905. nfds++;
  906. }
  907. if(nfds > 0)
  908. {
  909. poll_return = poll(fds, nfds, POLL_TIMEOUT);
  910. }
  911. else
  912. {
  913. usleep(POLL_TIMEOUT * 1000);
  914. poll_return = 0;
  915. }
  916. if(poll_return <= 0)
  917. {
  918. continue;
  919. }
  920. for(i=0; i < nfds; i++)
  921. {
  922. if( fds[i].fd == server_fd )
  923. {
  924. if(fds[i].revents & POLLIN) //If we just got some input...
  925. {
  926. if(zflush_in_progress) //And we're in the middle of downloading ZFLUSH data
  927. {
  928. //Read it into our ZFLUSH buffer
  929. read_return = recv(fds[i].fd, zflush_data + zflush_idx, zflush_data_size - zflush_idx, 0);
  930. //If the socket has closed politely (0), or had an error other than EINTR...
  931. if( (read_return == 0) || ((read_return < 0) && (errno != EINTR)) )
  932. {
  933. printf("Lost touch with server. This may be OK if we just finished downloading ZFLUSH data\n");
  934. close(server_fd);
  935. server_fd = -1;
  936. break;
  937. }
  938. else
  939. {
  940. if(read_return > 0) //Otherwise, if we got some real data
  941. {
  942. zflush_idx += read_return; //advance our download pointer
  943. //We want to let the update_pass_state() function compute our progress bar...
  944. update_pass_state(FLUSH_STATUS_DOWNLOAD, 0);
  945. update_piu_status_message(0);
  946. last_sync_attempt = now; //remember our last sync time
  947. if(zflush_idx == zflush_data_size) //if we've got the whole shebang...
  948. {
  949. //Flag that we're done downloading zflush blob and returning to command mode...
  950. zflush_in_progress = 0;
  951. // We don't have to do anything else about this, because the next thing we will hear
  952. //from the server is ZFLUSHDONE which will cause us to apply our downloaded zflush data.
  953. }
  954. }
  955. }
  956. }
  957. else //If we are not in the middle of a ZFLUSH
  958. {
  959. read_return = recv(fds[i].fd, input_line + input_idx, sizeof(input_line) - input_idx, 0);
  960. //If the socket has closed politely (0), or had an error other than EINTR...
  961. if( (read_return == 0) || ((read_return < 0) && (errno != EINTR)) )
  962. {
  963. printf("Lost touch with server.\n");
  964. close(server_fd);
  965. server_fd = -1;
  966. break;
  967. }
  968. else
  969. {
  970. if(read_return > 0) //and we got some real data
  971. {
  972. input_idx += read_return; //advance our input index
  973. do //loop through the data we've received...
  974. {
  975. if(zflush_in_progress) //if we have begun a ZFLUSH at some point,
  976. {
  977. if(input_idx >= zflush_data_size)
  978. {
  979. int j,k;
  980. memcpy(zflush_data, input_line, zflush_data_size);
  981. zflush_idx = zflush_data_size;
  982. zflush_in_progress = 0;
  983. k = input_idx - zflush_data_size;
  984. for(j = 0; j < k; j++)
  985. {
  986. input_line[j] = input_line[j + zflush_data_size];
  987. }
  988. input_idx = j;
  989. checked_idx = 0;
  990. }
  991. else
  992. {
  993. // Steal the rest of our input buffer and tack it on to the beginning of the zflush
  994. //buffer. We do this because otherwise we'd have the beginning of the zflush data blob
  995. //sitting around collecting dust in the line buffer and we'd download the rest into the
  996. //zflush buffer and be unable to process it because the beginning would still be
  997. //sitting around in this other buffer.
  998. memcpy(zflush_data, input_line, input_idx);
  999. zflush_idx = input_idx; //advance the zflush buffer index appropriately
  1000. checked_idx = input_idx = 0; //clear our line buffer index as it is now empty
  1001. last_sync_attempt = now; //mark our last sync time as now
  1002. //printf("Added %d leftover bytes to zflush_data\n", input_idx);
  1003. break; //break out of the do loop since we're done with our input buffer
  1004. }
  1005. }
  1006. //Advance until we either hit the end of the buffer, or we hit a line-terminator
  1007. while(checked_idx < input_idx)
  1008. {
  1009. if( (input_line[checked_idx] == '\r') || (input_line[checked_idx] == '\n') )
  1010. {
  1011. break;
  1012. }
  1013. else
  1014. {
  1015. checked_idx++;
  1016. }
  1017. }
  1018. //If we didn't hit the end of the input... (meaning we got a whole line...)
  1019. if(checked_idx != input_idx)
  1020. {
  1021. int j,k;
  1022. //Null terminate the line we got as a string...
  1023. input_line[checked_idx] = '\0';
  1024. //printf("Got Command \"%s\"\n", input_line);
  1025. if( handle_pass_update_request(input_line, &ctx, 1) < 0 )
  1026. {
  1027. // printf("Command Failed: \"%s\"\n", input_line);
  1028. }
  1029. else
  1030. {
  1031. real_pass_status.last_ack_time = now;
  1032. }
  1033. last_sync_attempt = now; //remember this server contact time
  1034. real_pass_status.last_sync_time = now; //add it to our passdb status
  1035. send_pass_state(0); //update other modules if this represents a change
  1036. // Now that we've done that, we can bump the rest of characters to the beginning
  1037. //of the next line...
  1038. k = input_idx - (checked_idx + 1);
  1039. //copy those characters the the beginning of the buffer...
  1040. for(j=0; j < k; j++)
  1041. {
  1042. input_line[j] = input_line[j + checked_idx + 1];
  1043. }
  1044. input_idx = j; //Move the index to point to the next free byte in the buffer
  1045. checked_idx = 0; // Move the 'checked' index to the beginning so that next time
  1046. //we start counting index 0 as the beginning of the next line when
  1047. //it is time to start scanning for EOL.
  1048. }
  1049. // If we have hit an overflow condition such that our buffer is full and no newline
  1050. //has been received, we want to hang up on the server and try again because something is
  1051. //seriously borked...
  1052. if(input_idx == sizeof(input_line))
  1053. {
  1054. fprintf(stderr, "Input overrun from server (line too long).\n");
  1055. close(server_fd);
  1056. server_fd = -1;
  1057. break;
  1058. }
  1059. } while(checked_idx < input_idx); // While we have unchecked data (that we have yet to check
  1060. //for containing an end-of-line.
  1061. }
  1062. }
  1063. }
  1064. }
  1065. else //Be sure we process all input before closing a remotely closed socket...
  1066. {
  1067. //If we've lost connection, break this loop and poll all over again
  1068. if(fds[i].revents & (POLLERR | POLLHUP | POLLNVAL))
  1069. {
  1070. printf("Lost touch with server.\n");
  1071. close(server_fd);
  1072. server_fd = -1;
  1073. break;
  1074. }
  1075. }
  1076. if(fds[i].revents & POLLOUT) //If we have flagged that we intend to send a query and poll says it's OK...
  1077. {
  1078. // printf("Trying to write to server...\n");
  1079. if(real_pass_status.flush_status == FLUSH_STATUS_NORMAL)
  1080. {
  1081. send_query_message(server_fd, &ctx);
  1082. real_pass_status.last_sync_time = now;
  1083. send_pass_state(0);
  1084. }
  1085. else
  1086. {
  1087. printf("Skipping query opportunity because we are mid-flush.\n");
  1088. }
  1089. //and then update our last sync attempt time
  1090. last_sync_attempt = now;
  1091. }
  1092. }
  1093. else if( fds[i].fd == commhub_fd )
  1094. {
  1095. //If we've lost connection, break this loop and poll all over again
  1096. if(fds[i].revents & (POLLERR | POLLHUP | POLLNVAL))
  1097. {
  1098. close(commhub_fd);
  1099. commhub_fd = -1;
  1100. break;
  1101. }
  1102. if(fds[i].revents & POLLIN)
  1103. {
  1104. // printf("Trying to read from hub...\n");
  1105. read_return = get_message(commhub_fd, &incoming_msg);
  1106. if( read_return < 0 )
  1107. {
  1108. close(commhub_fd);
  1109. commhub_fd = -1;
  1110. break;
  1111. }
  1112. process_message(&incoming_msg); //This passes the received message through the callback list
  1113. }
  1114. }
  1115. }
  1116. }
  1117. printf("Detatching from Pass Database\n");
  1118. detach_from_passdb(&ctx);
  1119. printf("Closing connections\n");
  1120. if(server_fd >= 0)
  1121. {
  1122. close(server_fd);
  1123. server_fd = -1;
  1124. }
  1125. if(commhub_fd >= 0)
  1126. {
  1127. close(commhub_fd);
  1128. server_fd = -1;
  1129. }
  1130. printf("Unloading rules...\n");
  1131. unload_rules();
  1132. printf("Goodbye.\n");
  1133. return 0;
  1134. }