pass_communication.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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. //Extract the first tab-delimited field from the input line...
  301. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  302. //If that field is blank, then we ignore this line
  303. if( buffer[0] == '\0' )
  304. {
  305. return 0;
  306. }
  307. if( !strcasecmp(buffer, "UPDATE") ) //========================================== UPDATE
  308. {
  309. //------------------------------------------------------------------ seq
  310. if( eol )
  311. {
  312. fprintf(stderr, "UPDATE: Premature end of line!\n");
  313. return -1;
  314. }
  315. //Get the next field (this should be sequence number...)
  316. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  317. foo.seq = strtoull(buffer, NULL, 10);
  318. if( foo.seq == 0 )
  319. {
  320. fprintf(stderr, "UPDATE: Blank or zero sequence number not allowed!\n");
  321. return -1;
  322. }
  323. //------------------------------------------------------------------ ID
  324. if( eol )
  325. {
  326. fprintf(stderr, "UPDATE: Premature end of line!\n");
  327. return -1;
  328. }
  329. //Get the next field (this should be Rider ID...)
  330. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  331. foo.id = strtoull(buffer, NULL, 10);
  332. if( foo.id == 0 )
  333. {
  334. fprintf(stderr, "UPDATE: Blank or zero rider ID not allowed!\n");
  335. return -1;
  336. }
  337. //------------------------------------------------------------------ Magstripe
  338. if( eol )
  339. {
  340. fprintf(stderr, "UPDATE: Premature end of line!\n");
  341. return -1;
  342. }
  343. //Get the next field (this should be the Magstripe Value...)
  344. input_idx += get_field(foo.magstripe_value, line + input_idx, sizeof(foo.magstripe_value), &eol);
  345. //------------------------------------------------------------------ RFID
  346. if( eol )
  347. {
  348. fprintf(stderr, "UPDATE: Premature end of line!\n");
  349. return -1;
  350. }
  351. //Get the next field (this should be the RFID Value...)
  352. input_idx += get_field(foo.rfid_value, line + input_idx, sizeof(foo.rfid_value), &eol);
  353. //------------------------------------------------------------------ Rule Name
  354. if( eol )
  355. {
  356. fprintf(stderr, "UPDATE: Premature end of line!\n");
  357. return -1;
  358. }
  359. //Get the next field (this should be the Rule Name...)
  360. input_idx += get_field(foo.rule_name, line + input_idx, sizeof(foo.rule_name), &eol);
  361. //------------------------------------------------------------------ Rule Parameter
  362. if( eol )
  363. {
  364. fprintf(stderr, "UPDATE: Premature end of line!\n");
  365. return -1;
  366. }
  367. //Get the next field (this should be the Rule Parameter...)
  368. input_idx += get_field(foo.rule_param, line + input_idx, sizeof(foo.rule_param), &eol);
  369. //If we have extra fields, that's BAD NEWS!
  370. if( !eol )
  371. {
  372. fprintf(stderr, "UPDATE: Too many fields!\n");
  373. return -1;
  374. }
  375. //If everything else is okay, go ahead and update our local database
  376. last_sync_ack = time(NULL);
  377. return update_rider(ctx, &foo, sync); //returning the status of that operation
  378. }
  379. else if( !strcasecmp(buffer, "DELETE") ) //========================================== DELETE
  380. {
  381. //------------------------------------------------------------------ seq
  382. if( eol )
  383. {
  384. fprintf(stderr, "DELETE: Premature end of line!\n");
  385. return -1;
  386. }
  387. //Get the next field (this should be sequence number...)
  388. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  389. foo.seq = strtoull(buffer, NULL, 10);
  390. if( foo.seq == 0 )
  391. {
  392. fprintf(stderr, "DELETE: Blank or zero sequence number not allowed!\n");
  393. return -1;
  394. }
  395. //------------------------------------------------------------------ ID
  396. if( eol )
  397. {
  398. fprintf(stderr, "DELETE: Premature end of line!\n");
  399. return -1;
  400. }
  401. //Get the next field (this should be Rider ID...)
  402. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  403. foo.id = strtoull(buffer, NULL, 10);
  404. if( foo.id == 0 )
  405. {
  406. fprintf(stderr, "DELETE: Blank or zero rider ID not allowed!\n");
  407. return -1;
  408. }
  409. //If we have extra fields, that's BAD NEWS!
  410. if( !eol )
  411. {
  412. fprintf(stderr, "DELETE: Too many fields!\n");
  413. return -1;
  414. }
  415. //If everything else is okay, go ahead and update our local database
  416. last_sync_ack = time(NULL);
  417. return delete_rider(ctx, &foo, sync); //returning the status of that operation
  418. }
  419. else if( !strcasecmp(buffer, "FLUSH") ) //========================================== FLUSH
  420. {
  421. flush_in_progress = 1;
  422. if(update_pass_state(FLUSH_STATUS_LEGACY, 0) > 0)
  423. {
  424. update_piu_status_message(0);
  425. }
  426. fprintf(stderr, "Legacy Flush Requested!\n");
  427. last_sync_ack = time(NULL);
  428. return do_database_flush(ctx);
  429. }
  430. else if( !strcasecmp(buffer, "FLUSHDONE") ) //========================================== FLUSHDONE
  431. {
  432. flush_in_progress = 0;
  433. if(update_pass_state(FLUSH_STATUS_NORMAL, 0) > 0)
  434. {
  435. update_piu_status_message(0);
  436. }
  437. fprintf(stderr, "Legacy Flush Done!\n");
  438. last_sync_ack = time(NULL);
  439. return 0;
  440. }
  441. else if( !strcasecmp(buffer, "ZFLUSH") ) //========================================== FLUSH
  442. {
  443. //------------------------------------------------------------------ seq
  444. if( eol )
  445. {
  446. fprintf(stderr, "ZFLUSH: Premature end of line!\n");
  447. return -1;
  448. }
  449. //Get the next field (this should be sequence number...)
  450. input_idx += get_field(buffer, line + input_idx, sizeof(buffer), &eol);
  451. zflush_data_size = strtoull(buffer, NULL, 10);
  452. zflush_data = malloc(zflush_data_size);
  453. if(zflush_data == NULL)
  454. {
  455. fprintf(stderr, "Can't malloc %d bytes for ZFLUSH!\n", zflush_data_size);
  456. return FAIL_MEM;
  457. }
  458. printf("ZFLUSH: Downloading %d bytes.\n", zflush_data_size);
  459. zflush_idx = 0;
  460. zflush_in_progress = 1;
  461. flush_in_progress = 1;
  462. if(update_pass_state(FLUSH_STATUS_DOWNLOAD, 0) > 0) //Let update_pass_state() calculate our download percentage
  463. {
  464. update_piu_status_message(0);
  465. }
  466. last_sync_ack = time(NULL);
  467. return 0;
  468. }
  469. else if( !strcasecmp(buffer, "ZFLUSHDONE") ) //========================================== FLUSHDONE
  470. {
  471. fprintf(stderr, "ZFlush applying...\n");
  472. if(update_pass_state(FLUSH_STATUS_APPLY, 0) > 0) //Mark that we're starting an apply operation
  473. {
  474. update_piu_status_message(0);
  475. }
  476. do_zflush(ctx, zflush_data, zflush_data_size);
  477. zflush_cleanup();
  478. if(update_pass_state(FLUSH_STATUS_NORMAL, 0) > 0)
  479. {
  480. update_piu_status_message(0);
  481. }
  482. fprintf(stderr, "ZFlush Done!\n");
  483. last_sync_ack = time(NULL);
  484. return 0;
  485. }
  486. else if( !strcasecmp(buffer, "NOP") ) //========================================== NOP (this just updates ACK time)
  487. {
  488. last_sync_ack = time(NULL);
  489. return 0;
  490. }
  491. else
  492. {
  493. fprintf(stderr, "Unrecognized command: %s\n", buffer);
  494. return -1;
  495. }
  496. }
  497. int do_zflush(passdb_context *ctx, void *data, int len)
  498. {
  499. char line[LINE_BUFFER_SIZE] = {0};
  500. z_stream foo = {0};
  501. int zretval;
  502. int retval;
  503. int i,j,k;
  504. int nout;
  505. struct message_record incoming_msg;
  506. foo.zalloc = Z_NULL;
  507. foo.zfree = Z_NULL;
  508. foo.opaque = Z_NULL;
  509. foo.next_in = zflush_data;
  510. foo.avail_in = zflush_data_size;
  511. foo.next_out = (void *)line;
  512. foo.avail_out = sizeof(line);
  513. foo.total_out = 0;
  514. // printf("do_zflush(%p, %p, %d)\n", ctx, data, len);
  515. retval = inflateInit(&foo);
  516. if(retval != Z_OK)
  517. {
  518. fprintf(stderr, "inflateInit failed: Zlib error %d\n", retval);
  519. return FAIL_DATABASE;
  520. }
  521. retval = do_database_flush(ctx);
  522. if( DB_FAIL(retval) )
  523. {
  524. fprintf(stderr, "Database failure during ZFLUSH while performing flush\n");
  525. return retval;
  526. }
  527. do
  528. {
  529. RESET_WATCHDOG();
  530. //Respond to PING messages from the supervisor process lest we get killed off too early...
  531. while(message_socket_status(commhub_fd, MSG_RECV) & MSG_RECV)
  532. {
  533. if(get_message(commhub_fd, &incoming_msg) >= 0)
  534. {
  535. process_message(&incoming_msg);
  536. }
  537. }
  538. zretval = inflate(&foo, Z_NO_FLUSH);
  539. if( (zretval == Z_NEED_DICT) || (zretval == Z_DATA_ERROR) || (zretval == Z_MEM_ERROR) || (zretval == Z_STREAM_ERROR) )
  540. {
  541. fprintf(stderr, "Error inflating data: Zlib error %d\n", zretval);
  542. inflateEnd(&foo);
  543. return FAIL_DATABASE;
  544. }
  545. //The number of output bytes generated is the size of the buffer - number of free bytes
  546. nout = sizeof(line) - foo.avail_out;
  547. // printf("-- %d output bytes\n", nout);
  548. j = 0;
  549. //Scan through this buffer looking for EOL characters
  550. for(i=0; i < nout; i++)
  551. {
  552. if(line[i] == '\n') //if we've found one
  553. {
  554. line[i] = '\0'; //make it a terminating nul
  555. // printf("ZFLUSH->%s\n", line + j);
  556. //handle our pass update
  557. #ifdef ZFLUSH_SLOW_BUT_SAFE
  558. //In slow but safe mode, we msync() (or write()) the memory page containing each record after each update.
  559. //This is hell on the flash, and it takes a long time, but it means that we cannot POSSIBLY write a lower
  560. //sequence number record to the physical storage media before having physically written all previous
  561. //sequence numbers in the batch.
  562. retval = handle_pass_update_request(line + j, ctx, 1);
  563. #else
  564. //In fast mode, we apply all of our update to the memory window without calling msync() or write(), and then
  565. //(see below) when we're done rewriting the mmap()'d window for the local database, then we make one call
  566. //to msync() or write() and splat it all down to flash at once. This gets us a better compression ratio
  567. //on the large chunks of file which change infrequently and it produces less
  568. //filesystem fragmentation, but it means that it is theoretically possible for a power event to cause a
  569. //partial write to corrupt the local database (requiring a new flush). That being said, when running on
  570. //top of JFFS2, this should cause file truncation, which will be easily detectable and trigger another
  571. //flush request next time the application comes up.
  572. retval = handle_pass_update_request(line + j, ctx, 0);
  573. #endif
  574. if( DB_FAIL(retval) )
  575. {
  576. inflateEnd(&foo);
  577. fprintf(stderr, "Database failure during ZFLUSH while processing line \"%s\"\n", line + j);
  578. return retval;
  579. }
  580. //mark the first character of the next line
  581. j = i + 1;
  582. }
  583. }
  584. //k will be equal to the number of leftover bytes
  585. k = i - j;
  586. // printf("-- i = %d j = %d k = %d\n", i, j, k);
  587. //copy those leftovers back to the beginning
  588. for(i = 0; i < k; i++)
  589. {
  590. line[i] = line[j + i];
  591. }
  592. //and update our output buffer structure
  593. foo.next_out = (void *)(line + k);
  594. foo.avail_out = sizeof(line) - k;
  595. if(foo.avail_out == 0)
  596. {
  597. fprintf(stderr, "Line too long in decompressed ZFLUSH data!\n");
  598. inflateEnd(&foo);
  599. return FAIL_DATABASE;
  600. }
  601. // Update the other modules with our status (APPLY) and a progress indicator based on the amount of compressed data
  602. //read as a percentage of the total ZFLUSH data blob.
  603. if(update_pass_state(FLUSH_STATUS_APPLY, (foo.total_in * 100) / zflush_data_size) > 0)
  604. {
  605. update_piu_status_message(0);
  606. }
  607. } while(zretval != Z_STREAM_END);
  608. inflateEnd(&foo);
  609. //In fast mode, we have to ensure that we write our big block of changes back to secondary storage all at once
  610. #ifndef ZFLUSH_SLOW_BUT_SAFE
  611. if(update_pass_state(FLUSH_STATUS_WRITE, 0) > 0) //Let the other modules know we're performing the final write...
  612. {
  613. update_piu_status_message(0);
  614. }
  615. fprintf(stderr, "Doing one-shot file write...\n");
  616. // This resets the watchdog timer for a double-long timeframe (120 seconds these days...) allowing the write to be
  617. //slow as dirt (this is rarely needed, but if the flash was badly fragmented it can take some time to coalesce free
  618. //blocks and possibly perform some erase cycles.
  619. RESET_WATCHDOG_LONG(2);
  620. // This calls msync() if we're working with a non-broken implementation of mmap() (i.e. non-jffs2) or write()
  621. //to take our updated state from RAM and save it to the underlying file.
  622. sync_all_riders(ctx);
  623. fprintf(stderr, "Done.\n");
  624. #endif
  625. // After completing a ZFLUSH operation, we want to hang up on the server (and reconnect asap). The server should
  626. //also hang up on us after completing transmission of a ZFLUSH dataset. This effectively clears the buffers going both
  627. //ways which prevents tha case where a high communication (or server-side database) lag causes two "QUERY 0" messages
  628. //to be issued in a row, the first one triggering (correctly) a ZFLUSH which will be completed, then the server will read
  629. //the second (now obsolete) "QUERY 0" message and respond to it with another (now just useless and annoying) ZFLUSH.
  630. close(server_fd); //We just do a rude close... The server does a proper shutdown(fd, 2), and the FIN will have reached
  631. //us LONG before we execute this...
  632. server_fd = -1; //Mark the TCP connection to the server as dead so we flush buffers and restart the connection next
  633. //time through the loop.
  634. return 0;
  635. }
  636. //callback handles MAILBOX_UPDATE_PASSES
  637. message_callback_return handle_update_passes_message(struct message_record *msg, void *param)
  638. {
  639. //flag our last sync attempt as NEVER
  640. last_sync_attempt = 0;
  641. return MESSAGE_HANDLED_CONT;
  642. }
  643. //callback handles MAILBOX_FLUSH_PASSES
  644. message_callback_return handle_flush_passes_message(struct message_record *msg, void *param)
  645. {
  646. //We only want to allow a FLUSH_PASSES request to go through if the following conditions are met:
  647. if( (server_fd >= 0) && //1: We have a connection to the server
  648. (tunnel_is_up()) && //2: That connection is real (i.e. the SSH tunnel is in a known good state)
  649. (real_pass_status.flush_status == FLUSH_STATUS_NORMAL) //3: we are not currently in the middle of doing a flush...
  650. )
  651. {
  652. send_flushreq_message(server_fd);
  653. }
  654. return MESSAGE_HANDLED_CONT;
  655. }
  656. //callback handles MAILBOX_STATUS_REQUEST
  657. message_callback_return handle_status_request_message(struct message_record *msg, void *param)
  658. {
  659. // If we're responding to a status request message, we have to force a send even if it seems redundant because odds are
  660. //the requesting module just came on line and has no current status data for us.
  661. send_pass_state(1);
  662. return MESSAGE_HANDLED_CONT;
  663. }
  664. int reject_unknown_card(char *cred)
  665. {
  666. struct message_record bill_msg;
  667. struct message_record user_msg;
  668. struct message_record rider_msg;
  669. format_piu_message(&rider_msg, 1, PIU_PRIORITY_FARE, PASSENGER_MESSAGE_DURATION, "%s %s", REJECT_STR, "UNKNOWN CARD");
  670. format_driver_message(&user_msg, LOGLEVEL_REJECT, "Unknown card");
  671. format_billing_message(&bill_msg, REJECT_STR, "UNKNOWN-CARD", "", "Unknown card", cred, 0, 0);
  672. if(commhub_fd >= 0)
  673. {
  674. send_message(commhub_fd, &bill_msg);
  675. send_message(commhub_fd, &user_msg);
  676. send_message(commhub_fd, &rider_msg);
  677. return 0;
  678. }
  679. else
  680. {
  681. return -1;
  682. }
  683. }
  684. #ifdef REJECT_IF_NO_DRIVER
  685. int reject_no_driver()
  686. {
  687. struct message_record user_msg;
  688. struct message_record rider_msg;
  689. format_piu_message(&rider_msg, 1, PIU_PRIORITY_FARE, PASSENGER_MESSAGE_DURATION, "%s", "SEE DRIVER");
  690. format_driver_message(&user_msg, LOGLEVEL_REJECT, "Not Logged In!");
  691. if(commhub_fd >= 0)
  692. {
  693. send_message(commhub_fd, &user_msg);
  694. send_message(commhub_fd, &rider_msg);
  695. return 0;
  696. }
  697. else
  698. {
  699. return -1;
  700. }
  701. }
  702. #endif
  703. message_callback_return handle_token_mag_message(struct message_record *msg, void *param)
  704. {
  705. int idx;
  706. char cred[LINE_BUFFER_SIZE] = {0};
  707. passdb_context *ctx = (passdb_context *)param;
  708. if(!ctx)
  709. return MESSAGE_HANDLED_CONT;
  710. update_piu_status_message(1);
  711. #ifdef REJECT_IF_NO_DRIVER
  712. if( (driver_stat.logged_in_driver <= 0) || (stop_stat.paddle <= 0) )
  713. {
  714. reject_no_driver();
  715. return MESSAGE_HANDLED_CONT;
  716. }
  717. #endif
  718. idx = smart_find_mag(ctx, (char *)msg->payload, cred);
  719. if(idx < 0)
  720. {
  721. reject_unknown_card(cred);
  722. }
  723. else
  724. {
  725. process_rider(ctx, idx, cred);
  726. }
  727. return MESSAGE_HANDLED_CONT;
  728. }
  729. message_callback_return handle_token_rfid_message(struct message_record *msg, void *param)
  730. {
  731. int idx;
  732. char cred[LINE_BUFFER_SIZE] = {0};
  733. passdb_context *ctx = (passdb_context *)param;
  734. if(!ctx)
  735. return MESSAGE_HANDLED_CONT;
  736. update_piu_status_message(1);
  737. #ifdef REJECT_IF_NO_DRIVER
  738. if( (driver_stat.logged_in_driver <= 0) || (stop_stat.paddle <= 0) )
  739. {
  740. reject_no_driver();
  741. return MESSAGE_HANDLED_CONT;
  742. }
  743. #endif
  744. idx = smart_find_rf(ctx, (char *)msg->payload, cred);
  745. if(idx < 0)
  746. {
  747. reject_unknown_card(cred);
  748. }
  749. else
  750. {
  751. process_rider(ctx, idx, cred);
  752. }
  753. return MESSAGE_HANDLED_CONT;
  754. }
  755. message_callback_return update_anti_passback_cache(struct message_record *msg, void *param)
  756. {
  757. apb_flush_if_needed();
  758. return MESSAGE_HANDLED_CONT;
  759. }
  760. message_callback_return handle_rule_call(struct message_record *msg, void *param)
  761. {
  762. process_driver_rulecall( (driver_rulecall *)msg->payload );
  763. return MESSAGE_HANDLED_CONT;
  764. }
  765. void maintain_ipc_hub_connect(char *progname)
  766. {
  767. struct message_record outgoing_msg;
  768. if(commhub_fd < 0) //if we have no connection to the communication hub
  769. {
  770. commhub_fd = connect_to_message_server(progname); //try and get one
  771. // printf("commhub_fd = %d\n", commhub_fd);
  772. if(commhub_fd >= 0) //if it worked
  773. {
  774. //Subscribe to the command mailboxes we act on
  775. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_TOKEN_MAG, strlen(MAILBOX_TOKEN_MAG));
  776. send_message(commhub_fd,&outgoing_msg);
  777. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_TOKEN_RFID, strlen(MAILBOX_TOKEN_RFID));
  778. send_message(commhub_fd,&outgoing_msg);
  779. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_FLUSH_PASSES, strlen(MAILBOX_FLUSH_PASSES));
  780. send_message(commhub_fd,&outgoing_msg);
  781. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_UPDATE_PASSES, strlen(MAILBOX_UPDATE_PASSES));
  782. send_message(commhub_fd,&outgoing_msg);
  783. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_RULE_CALL, strlen(MAILBOX_RULE_CALL));
  784. send_message(commhub_fd,&outgoing_msg);
  785. //Subscribe to the relevant status management mailboxes
  786. subscribe_to_default_messages(commhub_fd);
  787. //Request updated status information...
  788. prepare_message(&outgoing_msg, MAILBOX_STATUS_REQUEST, "", 0);
  789. send_message(commhub_fd,&outgoing_msg);
  790. }
  791. }
  792. }
  793. int main(int argc, char **argv)
  794. {
  795. struct pollfd fds[2];
  796. int nfds = 0;
  797. int poll_return = 0;
  798. int read_return = 0;
  799. int i;
  800. struct message_record incoming_msg;
  801. char input_line[LINE_BUFFER_SIZE] = {0};
  802. int input_idx = 0;
  803. int checked_idx = 0;
  804. passdb_context ctx = {0};
  805. //------------------
  806. configure_signal_handlers(argv[0]);
  807. maintain_ipc_hub_connect(argv[0]);
  808. read_return = attach_to_passdb(&ctx);
  809. if( read_return < 0 )
  810. {
  811. fprintf(stderr, "Database is missing or corrupt. Attempting to format new database.\n");
  812. read_return = format_new_passdb();
  813. if( read_return < 0 )
  814. {
  815. fprintf(stderr, "Database cannot be created. Aborting!\n");
  816. return -1;
  817. }
  818. else
  819. {
  820. read_return = attach_to_passdb(&ctx);
  821. if( read_return < 0 )
  822. {
  823. fprintf(stderr, "New database is ALSO missing or corrupt. Aborting.\n");
  824. return -1;
  825. }
  826. }
  827. }
  828. //Register our default keep-up-with-system status callbacks
  829. register_system_status_callbacks();
  830. //Add our module-specific callbacks
  831. register_dispatch_callback(MAILBOX_UPDATE_PASSES, CALLBACK_USER(1), handle_update_passes_message, NULL);
  832. register_dispatch_callback(MAILBOX_FLUSH_PASSES, CALLBACK_USER(2), handle_flush_passes_message, NULL);
  833. register_dispatch_callback(MAILBOX_STATUS_REQUEST, CALLBACK_USER(3), handle_status_request_message, NULL);
  834. register_dispatch_callback(MAILBOX_TOKEN_RFID, CALLBACK_USER(4), handle_token_rfid_message, &ctx);
  835. register_dispatch_callback(MAILBOX_TOKEN_MAG, CALLBACK_USER(5), handle_token_mag_message, &ctx);
  836. register_dispatch_callback(MAILBOX_RULE_CALL, CALLBACK_USER(6), handle_rule_call, &ctx);
  837. //Handle status updates which require us to check expiration of the anti-passback cache
  838. register_dispatch_callback(MAILBOX_GPS_STATUS, CALLBACK_USER(6), update_anti_passback_cache, NULL);
  839. register_dispatch_callback(MAILBOX_STOP_STATUS, CALLBACK_USER(7), update_anti_passback_cache, NULL);
  840. while( exit_request_status == EXIT_REQUEST_NONE )
  841. {
  842. time_t now = time(NULL);
  843. RESET_WATCHDOG();
  844. //DEBUG
  845. printf("[%lli] passdb: heartbeat\n", get_usec_time());
  846. //DEBUG
  847. maintain_ipc_hub_connect(argv[0]);
  848. if( (now - last_piu_update) > 0)
  849. {
  850. update_piu_status_message(0);
  851. last_piu_update = now;
  852. }
  853. if(server_fd < 0) //If we don't have a connection to the sync server...
  854. {
  855. if( (now - last_sync_attempt) > DEFAULT_CONNECT_RETRY ) //See if it is time to try again
  856. {
  857. if( tunnel_is_up() ) //and if the tunnel thinks it is up
  858. {
  859. server_fd = connect_to_pass_server(); //if so, try again...
  860. if(server_fd >= 0) //if it worked
  861. {
  862. input_idx = 0; //reset our buffer index
  863. last_sync_attempt = 0;
  864. zflush_cleanup();;
  865. }
  866. else
  867. {
  868. last_sync_attempt = now;
  869. }
  870. }
  871. }
  872. }
  873. if(!rules_loaded())
  874. {
  875. load_rules(RULES_FILE);
  876. }
  877. if(rfid_pattern_loaded() <= 0)
  878. {
  879. load_rfid_decode_patterns(RFID_PATTERN_FILE);
  880. }
  881. if(hup_request_status)
  882. {
  883. hup_request_status = 0;
  884. unload_rules();
  885. load_rules(RULES_FILE);
  886. load_rfid_decode_patterns(RFID_PATTERN_FILE);
  887. }
  888. nfds=0;
  889. if(server_fd >= 0)
  890. {
  891. fds[nfds].fd = server_fd;
  892. fds[nfds].events = POLLIN;
  893. if( (now - last_sync_attempt) > DEFAULT_PASS_SYNC_RETRY )
  894. {
  895. // printf("We want to sync...\n");
  896. fds[nfds].events |= POLLOUT;
  897. }
  898. nfds++;
  899. }
  900. if(commhub_fd >= 0)
  901. {
  902. fds[nfds].fd = commhub_fd;
  903. fds[nfds].events = POLLIN;
  904. nfds++;
  905. }
  906. if(nfds > 0)
  907. {
  908. poll_return = poll(fds, nfds, POLL_TIMEOUT);
  909. }
  910. else
  911. {
  912. usleep(POLL_TIMEOUT * 1000);
  913. poll_return = 0;
  914. }
  915. if(poll_return <= 0)
  916. {
  917. continue;
  918. }
  919. for(i=0; i < nfds; i++)
  920. {
  921. if( fds[i].fd == server_fd )
  922. {
  923. if(fds[i].revents & POLLIN) //If we just got some input...
  924. {
  925. if(zflush_in_progress) //And we're in the middle of downloading ZFLUSH data
  926. {
  927. //Read it into our ZFLUSH buffer
  928. read_return = recv(fds[i].fd, zflush_data + zflush_idx, zflush_data_size - zflush_idx, 0);
  929. //If the socket has closed politely (0), or had an error other than EINTR...
  930. if( (read_return == 0) || ((read_return < 0) && (errno != EINTR)) )
  931. {
  932. printf("Lost touch with server. This may be OK if we just finished downloading ZFLUSH data\n");
  933. close(server_fd);
  934. server_fd = -1;
  935. break;
  936. }
  937. else
  938. {
  939. if(read_return > 0) //Otherwise, if we got some real data
  940. {
  941. zflush_idx += read_return; //advance our download pointer
  942. //We want to let the update_pass_state() function compute our progress bar...
  943. update_pass_state(FLUSH_STATUS_DOWNLOAD, 0);
  944. update_piu_status_message(0);
  945. last_sync_attempt = now; //remember our last sync time
  946. if(zflush_idx == zflush_data_size) //if we've got the whole shebang...
  947. {
  948. //Flag that we're done downloading zflush blob and returning to command mode...
  949. zflush_in_progress = 0;
  950. // We don't have to do anything else about this, because the next thing we will hear
  951. //from the server is ZFLUSHDONE which will cause us to apply our downloaded zflush data.
  952. }
  953. }
  954. }
  955. }
  956. else //If we are not in the middle of a ZFLUSH
  957. {
  958. read_return = recv(fds[i].fd, input_line + input_idx, sizeof(input_line) - input_idx, 0);
  959. //If the socket has closed politely (0), or had an error other than EINTR...
  960. if( (read_return == 0) || ((read_return < 0) && (errno != EINTR)) )
  961. {
  962. printf("Lost touch with server.\n");
  963. close(server_fd);
  964. server_fd = -1;
  965. break;
  966. }
  967. else
  968. {
  969. if(read_return > 0) //and we got some real data
  970. {
  971. input_idx += read_return; //advance our input index
  972. do //loop through the data we've received...
  973. {
  974. if(zflush_in_progress) //if we have begun a ZFLUSH at some point,
  975. {
  976. if(input_idx >= zflush_data_size)
  977. {
  978. int j,k;
  979. memcpy(zflush_data, input_line, zflush_data_size);
  980. zflush_idx = zflush_data_size;
  981. zflush_in_progress = 0;
  982. k = input_idx - zflush_data_size;
  983. for(j = 0; j < k; j++)
  984. {
  985. input_line[j] = input_line[j + zflush_data_size];
  986. }
  987. input_idx = j;
  988. checked_idx = 0;
  989. }
  990. else
  991. {
  992. // Steal the rest of our input buffer and tack it on to the beginning of the zflush
  993. //buffer. We do this because otherwise we'd have the beginning of the zflush data blob
  994. //sitting around collecting dust in the line buffer and we'd download the rest into the
  995. //zflush buffer and be unable to process it because the beginning would still be
  996. //sitting around in this other buffer.
  997. memcpy(zflush_data, input_line, input_idx);
  998. zflush_idx = input_idx; //advance the zflush buffer index appropriately
  999. checked_idx = input_idx = 0; //clear our line buffer index as it is now empty
  1000. last_sync_attempt = now; //mark our last sync time as now
  1001. //printf("Added %d leftover bytes to zflush_data\n", input_idx);
  1002. break; //break out of the do loop since we're done with our input buffer
  1003. }
  1004. }
  1005. //Advance until we either hit the end of the buffer, or we hit a line-terminator
  1006. while(checked_idx < input_idx)
  1007. {
  1008. if( (input_line[checked_idx] == '\r') || (input_line[checked_idx] == '\n') )
  1009. {
  1010. break;
  1011. }
  1012. else
  1013. {
  1014. checked_idx++;
  1015. }
  1016. }
  1017. //If we didn't hit the end of the input... (meaning we got a whole line...)
  1018. if(checked_idx != input_idx)
  1019. {
  1020. int j,k;
  1021. //Null terminate the line we got as a string...
  1022. input_line[checked_idx] = '\0';
  1023. //printf("Got Command \"%s\"\n", input_line);
  1024. if( handle_pass_update_request(input_line, &ctx, 1) < 0 )
  1025. {
  1026. // printf("Command Failed: \"%s\"\n", input_line);
  1027. }
  1028. else
  1029. {
  1030. real_pass_status.last_ack_time = now;
  1031. }
  1032. last_sync_attempt = now; //remember this server contact time
  1033. real_pass_status.last_sync_time = now; //add it to our passdb status
  1034. send_pass_state(0); //update other modules if this represents a change
  1035. // Now that we've done that, we can bump the rest of characters to the beginning
  1036. //of the next line...
  1037. k = input_idx - (checked_idx + 1);
  1038. //copy those characters the the beginning of the buffer...
  1039. for(j=0; j < k; j++)
  1040. {
  1041. input_line[j] = input_line[j + checked_idx + 1];
  1042. }
  1043. input_idx = j; //Move the index to point to the next free byte in the buffer
  1044. checked_idx = 0; // Move the 'checked' index to the beginning so that next time
  1045. //we start counting index 0 as the beginning of the next line when
  1046. //it is time to start scanning for EOL.
  1047. }
  1048. // If we have hit an overflow condition such that our buffer is full and no newline
  1049. //has been received, we want to hang up on the server and try again because something is
  1050. //seriously borked...
  1051. if(input_idx == sizeof(input_line))
  1052. {
  1053. fprintf(stderr, "Input overrun from server (line too long).\n");
  1054. close(server_fd);
  1055. server_fd = -1;
  1056. break;
  1057. }
  1058. } while(checked_idx < input_idx); // While we have unchecked data (that we have yet to check
  1059. //for containing an end-of-line.
  1060. }
  1061. }
  1062. }
  1063. }
  1064. else //Be sure we process all input before closing a remotely closed socket...
  1065. {
  1066. //If we've lost connection, break this loop and poll all over again
  1067. if(fds[i].revents & (POLLERR | POLLHUP | POLLNVAL))
  1068. {
  1069. printf("Lost touch with server.\n");
  1070. close(server_fd);
  1071. server_fd = -1;
  1072. break;
  1073. }
  1074. }
  1075. if(fds[i].revents & POLLOUT) //If we have flagged that we intend to send a query and poll says it's OK...
  1076. {
  1077. // printf("Trying to write to server...\n");
  1078. if(real_pass_status.flush_status == FLUSH_STATUS_NORMAL)
  1079. {
  1080. send_query_message(server_fd, &ctx);
  1081. real_pass_status.last_sync_time = now;
  1082. send_pass_state(0);
  1083. }
  1084. else
  1085. {
  1086. printf("Skipping query opportunity because we are mid-flush.\n");
  1087. }
  1088. //and then update our last sync attempt time
  1089. last_sync_attempt = now;
  1090. }
  1091. }
  1092. else if( fds[i].fd == commhub_fd )
  1093. {
  1094. //If we've lost connection, break this loop and poll all over again
  1095. if(fds[i].revents & (POLLERR | POLLHUP | POLLNVAL))
  1096. {
  1097. close(commhub_fd);
  1098. commhub_fd = -1;
  1099. break;
  1100. }
  1101. if(fds[i].revents & POLLIN)
  1102. {
  1103. // printf("Trying to read from hub...\n");
  1104. read_return = get_message(commhub_fd, &incoming_msg);
  1105. if( read_return < 0 )
  1106. {
  1107. close(commhub_fd);
  1108. commhub_fd = -1;
  1109. break;
  1110. }
  1111. process_message(&incoming_msg); //This passes the received message through the callback list
  1112. }
  1113. }
  1114. }
  1115. }
  1116. printf("Detatching from Pass Database\n");
  1117. detach_from_passdb(&ctx);
  1118. printf("Closing connections\n");
  1119. if(server_fd >= 0)
  1120. {
  1121. close(server_fd);
  1122. server_fd = -1;
  1123. }
  1124. if(commhub_fd >= 0)
  1125. {
  1126. close(commhub_fd);
  1127. server_fd = -1;
  1128. }
  1129. printf("Goodbye.\n");
  1130. return 0;
  1131. }