common_defs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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/termios.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <sys/ioctl.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <signal.h>
  29. #include <string.h>
  30. #include <poll.h>
  31. #include <syslog.h>
  32. #include <sysexits.h>
  33. #include <stdarg.h>
  34. #include <dirent.h>
  35. #include <fnmatch.h>
  36. #include "common_defs.h"
  37. #ifndef DEFAULT_BAUD
  38. #define DEFAULT_BAUD (B115200)
  39. #endif
  40. //------------------------------------------- This horrible blob of code goes and hunts for package install records and gathers
  41. //------------------------------------------- them into a sane data structure for display to the user in status/diagnostic screens.
  42. #define PKG_SEARCH_DIR CONFIG_FILE_PATH
  43. #define PATTERN_SUFFIX ".tgz.version"
  44. #define CHECKSUM_SUFFIX ".tgz.checksum"
  45. #define PATTTERN ("*" PATTERN_SUFFIX)
  46. static int package_filter(const struct dirent *file)
  47. {
  48. int match = fnmatch(PATTTERN, file->d_name, 0);
  49. return !match;
  50. }
  51. int find_packages(package_signature *pkgs, int n)
  52. {
  53. int i = 0;
  54. int scandir_ret;
  55. struct dirent **matchlist;
  56. int j;
  57. int match_len;
  58. int suffix_len = strlen(PATTERN_SUFFIX);
  59. FILE *f;
  60. struct stat st;
  61. char pathbuffer[1024];
  62. //Look for any file that matches the pattern PATTERN (see package_filter above) and return into matchlist a pointer
  63. //to and array of pointers to dirent structures for each directory entry that made the cut, sorted alphabetically
  64. scandir_ret = scandir(PKG_SEARCH_DIR, &matchlist, package_filter, alphasort);
  65. //If that seemed to work....
  66. if( (scandir_ret >= 0) && (matchlist != NULL) )
  67. {
  68. //Walk the resultant list
  69. for(i=0; i < scandir_ret; i++)
  70. {
  71. //if we've filled our caller's buffer, break out now
  72. if(i >= n)
  73. break;
  74. //Go figure out how long the module name part is (it'll be the length of the filename minus the length of the suffix)
  75. match_len = strlen(matchlist[i]->d_name) - suffix_len;
  76. //If that would cause an overflow, truncate it...
  77. if(match_len > (PKG_STRING_SIZE - 1))
  78. {
  79. match_len = PKG_STRING_SIZE - 1;
  80. }
  81. //Copy our package name
  82. strncpy(pkgs[i].pkgname, matchlist[i]->d_name, match_len);
  83. pkgs[i].pkgname[match_len] = '\0';
  84. //(if fscanf fails, we want an empty string rather than random crap from memory...)
  85. pkgs[i].pkgver[0] = '\0';
  86. //generate the file name of the package version string...
  87. snprintf(pathbuffer, sizeof(pathbuffer), "%s%s%s", PKG_SEARCH_DIR, pkgs[i].pkgname, PATTERN_SUFFIX);
  88. //Go and read the contents of that into the pkgver part of our structure...
  89. f = fopen(pathbuffer, "rb");
  90. if(f)
  91. {
  92. fscanf(f, PKG_STRING_FORMAT, pkgs[i].pkgver);
  93. fclose(f);
  94. }
  95. pkgs[i].checksum[0] = '\0';
  96. //generate the file name of the file containing the package checksum
  97. snprintf(pathbuffer, sizeof(pathbuffer), "%s%s%s", PKG_SEARCH_DIR, pkgs[i].pkgname, CHECKSUM_SUFFIX);
  98. //Go and read the contents of that into the checkdum part of our structure...
  99. f = fopen(pathbuffer, "rb");
  100. if(f)
  101. {
  102. fscanf(f, PKG_STRING_FORMAT, pkgs[i].checksum);
  103. fclose(f);
  104. }
  105. pkgs[i].installed = 0;
  106. //Go and grab the mtime of the package install file
  107. if(!stat(pathbuffer, &st))
  108. {
  109. pkgs[i].installed = st.st_mtime;
  110. }
  111. //Free this dirent
  112. free(matchlist[i]);
  113. }
  114. //Clean up any leftovers
  115. for(j = i; j < scandir_ret; j++)
  116. {
  117. free(matchlist[i]);
  118. }
  119. //free the array of pointers
  120. free(matchlist);
  121. }
  122. //return the number actually filled
  123. return i;
  124. }
  125. //--------------------------------------------------------------------------------------------------
  126. //This function checks the dropfile to see if the tunnel is up...
  127. int tunnel_is_up()
  128. {
  129. struct stat st;
  130. int retval;
  131. retval = stat(TUNNEL_DROPFILE, &st);
  132. if(retval)
  133. {
  134. return 0;
  135. }
  136. else
  137. {
  138. return 1;
  139. }
  140. }
  141. //This function checks the dropfile to see if the GPRS modem is up...
  142. int gprs_is_up()
  143. {
  144. struct stat st;
  145. int retval;
  146. retval = stat(GPRS_DROPFILE, &st);
  147. if(retval)
  148. {
  149. return 0;
  150. }
  151. else
  152. {
  153. return 1;
  154. }
  155. }
  156. //This function gets the equipment number from the appropriate config file
  157. //If it cannot be gotten, it returns -1
  158. int get_equip_num()
  159. {
  160. FILE *f;
  161. int num = -1;
  162. f = fopen(EQUIPNUM_FILE, "rb");
  163. if(f)
  164. {
  165. fscanf(f,"%d",&num);
  166. fclose(f);
  167. }
  168. else
  169. {
  170. num = -1;
  171. }
  172. return num;
  173. }
  174. //This function sets the euipment number in the config file.
  175. //if this operation fails, -1 is returned.
  176. int set_equip_num(int num)
  177. {
  178. FILE *f;
  179. if( (num < 1) || (num > MAX_EQUIPNUM) )
  180. {
  181. return -1;
  182. }
  183. f = fopen(EQUIPNUM_TEMPFILE, "wb");
  184. if(f)
  185. {
  186. fprintf(f,"%d\n", num);
  187. fclose(f);
  188. rename(EQUIPNUM_TEMPFILE, EQUIPNUM_FILE);
  189. sync();
  190. return 0;
  191. }
  192. return -1;
  193. }
  194. int get_server_desc(char *desc, int len)
  195. {
  196. char svrname[LINE_BUFFER_SIZE];
  197. FILE *f;
  198. if(!desc)
  199. return -1;
  200. if(len <= 0)
  201. return -1;
  202. f = fopen(SERVER_DESC_FILE, "rb");
  203. if(f)
  204. {
  205. fgets(svrname, LINE_BUFFER_SIZE, f);
  206. svrname[LINE_BUFFER_SIZE - 1] = '\0';
  207. strip_crlf(svrname);
  208. strncpy(desc, svrname, len);
  209. desc[len - 1] = '\0';
  210. return 0;
  211. }
  212. return -1;
  213. }
  214. int get_field(char *dest, char *src, int dest_len, int *eol_flag)
  215. {
  216. int i,j;
  217. int done = 0;
  218. int eol = 0;
  219. i = j = 0;
  220. while( ! done )
  221. {
  222. switch(src[i])
  223. {
  224. case '\0': //If we've hit the end of the input line
  225. case '\n':
  226. case '\r':
  227. if(j < dest_len) //cap off our destination string if there is room
  228. {
  229. dest[j++] = '\0';
  230. }
  231. eol = 1; //set our internal End Of Line flag
  232. done = 1; //set the done flag so we break out of the while
  233. break;
  234. case '\t': //If we have hit a record boundary
  235. if(j < dest_len) //cap off our input line
  236. {
  237. dest[j++] = '\0';
  238. }
  239. i++; //Advance past this input character
  240. done = 1; //flag us as done
  241. break;
  242. default: //otherwise (for normal characters) just copy them if there is space
  243. if(j < dest_len)
  244. {
  245. dest[j++] = src[i];
  246. }
  247. i++; //and advance past them
  248. break;
  249. }
  250. }
  251. if( j == dest_len ) //If we have filled our line to capacity
  252. { //Truncate it by 1 character to fit the terminating NUL
  253. dest[dest_len - 1] = '\0';
  254. }
  255. if( eol_flag != NULL ) //If the user has passed us a place to store our EOL flag,
  256. {
  257. *eol_flag = eol; //store it there
  258. }
  259. return i; //Return the index of the next character to be consumed.
  260. }
  261. unsigned long long stringhash(char *string) //magic hash function hashes english strings well enough for our needs
  262. {
  263. unsigned long long hash = 5381;
  264. int c;
  265. while ((c = *string++))
  266. hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  267. return hash;
  268. }
  269. int strip_crlf(char *buffer)
  270. {
  271. char *src,*dst;
  272. int count=0;
  273. //Set both our source and destination pointers to the buffer in question
  274. src=dst=buffer;
  275. //While we still have unprocessed buffer bytes
  276. while(*src)
  277. {
  278. //If we encounter a CR or LF character
  279. if((*src == '\r') || (*src == '\n'))
  280. {
  281. //If the NEXT character is not another EOL character or a terminating nul
  282. if( (src[1] != '\0') && (src[1] != '\r') && (src[1] != '\n') )
  283. {
  284. src++;
  285. *dst++ = ' '; //replace the CR or LF with a space.
  286. count++;
  287. continue;
  288. }
  289. else //On the other hand, if what comes next is EOL or end of string,
  290. {
  291. src++; //just trim this character
  292. continue;
  293. }
  294. }
  295. //If this character isn't one of the special ones, just copy it.
  296. *dst++ = *src++;
  297. count++;
  298. }
  299. //Copy the terminating NUL
  300. *dst++ = *src++;
  301. return count;
  302. }
  303. int open_rs232_device(char *devname, int custom_baud, int linemode)
  304. {
  305. struct termios tty={0};
  306. int retval;
  307. int fd;
  308. //Open the specified character device READ/WRITE and not as a Controlling TTY
  309. fd = open(devname, O_RDWR | O_NOCTTY);
  310. if(fd < 0)
  311. {
  312. #ifdef COMMON_PRINT_WARNING
  313. fprintf(stderr, "Warning: Cannot open TTY %s\n", devname);
  314. #endif
  315. return -1;
  316. }
  317. //Try and fetch the TTY properties of the device
  318. retval = ioctl(fd, TCGETS, &tty);
  319. if(retval)
  320. {
  321. #ifdef COMMON_PRINT_WARNING
  322. fprintf(stderr, "Warning: Cannot get TTY attributes on %s. Not a TTY?\n", devname);
  323. #endif
  324. close(fd);
  325. return -1;
  326. }
  327. if(custom_baud > 0)
  328. {
  329. tty.c_cflag = custom_baud | CS8 | CREAD | CLOCAL;
  330. }
  331. else
  332. {
  333. tty.c_cflag = DEFAULT_BAUD | CS8 | CREAD | CLOCAL;
  334. }
  335. if(linemode)
  336. {
  337. tty.c_iflag = IGNBRK;
  338. tty.c_oflag = 0;
  339. tty.c_lflag = ICANON;
  340. tty.c_line = 0;
  341. tty.c_cc[VMIN] = 1; //minimum one character read
  342. tty.c_cc[VTIME] = ACCUM_SECONDS * 10; //wait ACCUM_SECONDS without any further chars
  343. //before giving up on the arrival of a newline.
  344. //(VTIME is specified in deciseconds (who knows why...))
  345. tty.c_cc[VEOL] = '\n'; //allow a newline to release the buffer
  346. }
  347. else
  348. {
  349. tty.c_iflag = IGNBRK;
  350. tty.c_oflag = 0;
  351. tty.c_lflag = 0;
  352. tty.c_line = 0;
  353. tty.c_cc[VMIN] = 1;
  354. tty.c_cc[VTIME] = 5;
  355. }
  356. //try and plunk our desired settings down on the device
  357. retval = ioctl(fd, TCSETS, &tty);
  358. if(retval)
  359. {
  360. #ifdef COMMON_PRINT_WARNING
  361. fprintf(stderr, "Warning: Cannot set TTY attributes on %s. Unsupported mode?\n", devname);
  362. #endif
  363. close(fd);
  364. return -1;
  365. }
  366. //flush the serial port buffers to clean up any detritus that has accumulated before we got here
  367. tcflush(fd, TCIOFLUSH);
  368. return fd;
  369. }
  370. static int read_with_timeout(int fd, void *buffer, int n, int timeout)
  371. {
  372. int retval;
  373. struct pollfd fds[1];
  374. fds[0].fd = fd;
  375. fds[0].events = POLLIN;
  376. retval = poll(fds, 1, timeout);
  377. if(retval > 0)
  378. {
  379. return read(fd, buffer, n);
  380. }
  381. else
  382. {
  383. return retval;
  384. }
  385. }
  386. static int init_device(int fd, device_test_vector *vec, char *diag)
  387. {
  388. char buffer[1024] = {0};
  389. int i;
  390. int retval;
  391. int len;
  392. char *trav;
  393. int timeout = DEVICE_TEST_TIMEOUT;
  394. if(vec->init_timeout > 0)
  395. {
  396. timeout = vec->init_timeout;
  397. }
  398. if(vec->init_string != NULL) //If we were given an init string
  399. {
  400. len = strlen(vec->init_string); //calculate how long it is
  401. retval = write(fd, vec->init_string, len); //send it on its way
  402. if(retval != len) //if that didn't work
  403. {
  404. if(diag) //complain and fail
  405. {
  406. snprintf(diag, DIAG_BUFFER_SIZE, "Cannot write init string to device.");
  407. }
  408. return -1;
  409. }
  410. }
  411. i = 0;
  412. while(i < vec->n_reply_lines)
  413. {
  414. retval = read_with_timeout(fd, buffer, sizeof(buffer), timeout); //Ask the kernel for a line from the TTY
  415. if(retval > 0) //If we DID read something from the device, make sure it is correct
  416. {
  417. buffer[retval] = '\0';
  418. strip_crlf(buffer);
  419. trav = buffer;
  420. // printf("%d: %s\n", i, trav);
  421. //Skip any garbage before the start character
  422. while(*trav && (*trav != '/'))
  423. trav++;
  424. if(*trav == '\0')
  425. {
  426. continue; //ignore blank lines
  427. }
  428. if( (trav[0] == '/') && (trav[1] == '?') && (trav[2] == ':') )
  429. {
  430. continue; //ignore device ID lines
  431. }
  432. //The order of these tests in importand. Short circuit keeps us from
  433. //dereferencing expected_reply_lines if the first test fails
  434. if( (vec->reply_strings != NULL) && (vec->reply_strings[i] != NULL) ) //If we have a pattern to test against
  435. {
  436. if(strcmp(trav, vec->reply_strings[i]))
  437. {
  438. if(diag)
  439. {
  440. snprintf(diag, DIAG_BUFFER_SIZE, "Read init reply line %d from device, expected \"%s\" but got \"%s\"", i, vec->reply_strings[i], trav);
  441. }
  442. return -1;
  443. }
  444. }
  445. }
  446. else //If we DIDN'T read anything from the device...
  447. {
  448. if(diag) //Complain
  449. {
  450. snprintf(diag, DIAG_BUFFER_SIZE, "Reading init reply from device timed out waiting for line %d", i);
  451. }
  452. return -1; //And fail
  453. }
  454. i++;
  455. }
  456. return 0;
  457. }
  458. int test_and_init_device(int fd, device_test_vector *vec, char *diag)
  459. {
  460. char buffer[DEV_INIT_BUFFER_SIZE] = {0};
  461. char module_id[DEV_INIT_BUFFER_SIZE] = {0};
  462. int tries = DEVICE_TEST_TRIES;
  463. int timeout = DEVICE_TEST_TIMEOUT;
  464. int retval;
  465. char *trav;
  466. if(vec == NULL)
  467. {
  468. return -1;
  469. }
  470. if(vec->dev_id == NULL)
  471. {
  472. return -1;
  473. }
  474. if(vec->init_tries > 0)
  475. {
  476. tries = vec->init_tries;
  477. }
  478. if(vec->init_timeout > 0)
  479. {
  480. timeout = vec->init_timeout;
  481. }
  482. do //We want to iterate through DEVICE_TEST_TRIES tries at getting a valid line from the device
  483. {
  484. write(fd,"\r", 1); //Send a CR to stimulate the device to spit out its help message
  485. retval = read_with_timeout(fd, buffer, sizeof(buffer), timeout); //Ask the kernel for a line from the TTY
  486. //If we actually got a line of data
  487. if(retval > 0)
  488. {
  489. buffer[retval] = '\0';
  490. strip_crlf(buffer);
  491. //Start examining our buffer
  492. trav = buffer;
  493. //Skip any garbage before the start character
  494. while(*trav && (*trav != '/'))
  495. trav++;
  496. //See if it is our Device ID / help line...
  497. if( (trav[0] == '/') && (trav[1] == '?') && (trav[2] == ':') )
  498. {
  499. trav += 3; //Skip the header and go to the body
  500. retval = sscanf(trav, " ?=%s", module_id); //Look for our module ID string
  501. if(retval < 1)
  502. {
  503. retval = sscanf(trav, "?=%s", module_id); //Look for our module ID string without leading space
  504. }
  505. if(retval == 1) //If we have found id
  506. {
  507. if(!strcmp(module_id, vec->dev_id)) //See if it is the correct one
  508. {
  509. if(diag) //If so, pass on our diagnostic message if we have a place to
  510. {
  511. snprintf(diag, DIAG_BUFFER_SIZE, "Device connected OK");
  512. }
  513. return init_device(fd, vec, diag); //Perform initialization and return the status of that operation
  514. }
  515. else //Otherwise, if it is NOT the one we are expecting
  516. {
  517. if(diag) //Complain if we have a place to
  518. {
  519. snprintf(diag, DIAG_BUFFER_SIZE, "Device present: Expecting: \"%s\" Got: \"%s\"", vec->dev_id, module_id);
  520. }
  521. return -2; //Return a distinct failure code
  522. }
  523. }
  524. }
  525. else //If we DIDN'T get the line we were looking for, pretend it was blank so we'll try again...
  526. {
  527. retval = 0; //This pretends the received line was black.
  528. }
  529. } //if we got 0 bytes or EINTR from the alarm firing
  530. } while( (retval <= 0) && ( --tries > 0) ); //While we still have tries and a previous try didn't work
  531. if(diag)
  532. {
  533. snprintf(diag, DIAG_BUFFER_SIZE, "Could not get reply from device");
  534. }
  535. return -1;
  536. }
  537. //========================================================================================================
  538. //------------------------------- WATCHDOG TIMER and other SIGNAL HANDLERS -------------------------------
  539. //========================================================================================================
  540. //volatile int hup_request_status = 0;
  541. volatile sig_atomic_t hup_request_status = 0;
  542. void request_hup(char *fmt, ...) {
  543. va_list ap;
  544. hup_request_status = 1;
  545. va_start(ap, fmt);
  546. vprintf(fmt, ap);
  547. va_end(ap);
  548. }
  549. //Our signal handlers and standard message handlers will OR these bits into exit_request_status
  550. volatile int exit_request_status = 0;
  551. static int exit_signal_counter = 0;
  552. void request_polite_exit(int reason, char *fmt, ...)
  553. {
  554. va_list ap;
  555. exit_request_status |= reason;
  556. if(reason)
  557. {
  558. exit_signal_counter++;
  559. }
  560. if(exit_request_status & EXIT_REQUEST_CRASH)
  561. {
  562. va_start(ap, fmt);
  563. vsyslog(LOG_ERR, fmt, ap);
  564. va_end(ap);
  565. exit(EX_SOFTWARE);
  566. }
  567. if(exit_signal_counter >= MAX_POLITE_EXIT_REQUESTS)
  568. {
  569. va_start(ap, fmt);
  570. vsyslog(LOG_NOTICE, fmt, ap);
  571. va_end(ap);
  572. exit(SIGTERM);
  573. }
  574. }
  575. static void watchdog_handler(int signum, siginfo_t *info, void *data)
  576. {
  577. request_polite_exit(EXIT_REQUEST_CRASH, "Watchdog timer has expired!");
  578. }
  579. static void term_int_handler(int signum, siginfo_t *info, void *data)
  580. {
  581. request_polite_exit(EXIT_REQUEST_INT_TERM, "Received signal %d", signum);
  582. }
  583. static void hard_crash_handler(int signum, siginfo_t *info, void *data)
  584. {
  585. switch(signum)
  586. {
  587. case SIGSEGV:
  588. request_polite_exit(EXIT_REQUEST_CRASH, "Segmentation fault at virtual address %p", info->si_addr);
  589. break;
  590. case SIGILL:
  591. request_polite_exit(EXIT_REQUEST_CRASH, "Illegal instruction at virtual address %p", info->si_addr);
  592. break;
  593. case SIGFPE:
  594. request_polite_exit(EXIT_REQUEST_CRASH, "Floating point exception at virtual address %p", info->si_addr);
  595. break;
  596. case SIGBUS:
  597. request_polite_exit(EXIT_REQUEST_CRASH, "SIGBUS (hardware error!) at address %p", info->si_addr);
  598. break;
  599. default:
  600. request_polite_exit(EXIT_REQUEST_CRASH, "Caught Signal %d", signum);
  601. break;
  602. }
  603. }
  604. void configure_signal_handlers(char *procname)
  605. {
  606. struct sigaction sa = {{0}};
  607. openlog(procname, LOG_CONS | LOG_PERROR, LOG_USER);
  608. #ifdef USE_WATCHDOG_ALARM
  609. sa.sa_sigaction = watchdog_handler;
  610. sa.sa_flags = SA_SIGINFO;
  611. sigfillset(&sa.sa_mask);
  612. sigaction(SIGALRM, &sa, NULL);
  613. RESET_WATCHDOG();
  614. #endif
  615. //Install our "boy did we ever fuck up this time" signal handler
  616. //to trap segmentation faults, illegal instructions, divides by zero,
  617. //and things like RAM chips popping off a running board (SIGBUS).
  618. sa.sa_sigaction = hard_crash_handler;
  619. sa.sa_flags = SA_SIGINFO;
  620. sigaction(SIGSEGV, &sa, NULL);
  621. sigaction(SIGILL, &sa, NULL);
  622. sigaction(SIGFPE, &sa, NULL);
  623. sigaction(SIGBUS, &sa, NULL);
  624. //Install our polite exit handler...
  625. sa.sa_sigaction = term_int_handler;
  626. sa.sa_flags = SA_SIGINFO | SA_RESTART; //Allow interrupted I/O calls to finish to facilitate clean exit
  627. sigaction(SIGTERM, &sa, NULL);
  628. sigaction(SIGINT, &sa, NULL);
  629. }
  630. //------------------