libgps_shm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /****************************************************************************
  2. NAME
  3. libgps_shm.c - reader access to shared-memory export
  4. DESCRIPTION
  5. This is a very lightweight alternative to JSON-over-sockets. Clients
  6. won't be able to filter by device, and won't get device activation/deactivation
  7. notifications. But both client and daemon will avoid all the marshalling and
  8. unmarshalling overhead.
  9. PERMISSIONS
  10. This file is Copyright 2010 by the GPSD project
  11. SPDX-License-Identifier: BSD-2-clause
  12. ***************************************************************************/
  13. #include "gpsd_config.h"
  14. #ifdef SHM_EXPORT_ENABLE
  15. #include <errno.h>
  16. #include <stddef.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/ipc.h>
  20. #include <sys/shm.h>
  21. #include <sys/time.h>
  22. #include "gpsd.h"
  23. #include "libgps.h"
  24. struct privdata_t
  25. {
  26. void *shmseg;
  27. int tick;
  28. };
  29. /* open a shared-memory connection to the daemon */
  30. int gps_shm_open(struct gps_data_t *gpsdata)
  31. {
  32. int shmid;
  33. long shmkey = getenv("GPSD_SHM_KEY") ?
  34. strtol(getenv("GPSD_SHM_KEY"), NULL, 0) : GPSD_SHM_KEY;
  35. libgps_debug_trace((DEBUG_CALLS, "gps_shm_open()\n"));
  36. gpsdata->privdata = NULL;
  37. shmid = shmget((key_t)shmkey, sizeof(struct gps_data_t), 0);
  38. if (shmid == -1) {
  39. /* daemon isn't running or failed to create shared segment */
  40. return -1;
  41. }
  42. gpsdata->privdata = (void *)malloc(sizeof(struct privdata_t));
  43. if (gpsdata->privdata == NULL)
  44. return -1;
  45. PRIVATE(gpsdata)->tick = 0;
  46. PRIVATE(gpsdata)->shmseg = shmat(shmid, 0, 0);
  47. if (PRIVATE(gpsdata)->shmseg == (void *) -1) {
  48. /* attach failed for sume unknown reason */
  49. free(gpsdata->privdata);
  50. gpsdata->privdata = NULL;
  51. return -2;
  52. }
  53. #ifndef USE_QT
  54. gpsdata->gps_fd = SHM_PSEUDO_FD;
  55. #else
  56. gpsdata->gps_fd = (void *)(intptr_t)SHM_PSEUDO_FD;
  57. #endif /* USE_QT */
  58. return 0;
  59. }
  60. /* check to see if new data has been written */
  61. /* timeout is in uSec */
  62. bool gps_shm_waiting(const struct gps_data_t *gpsdata, int timeout)
  63. {
  64. volatile struct shmexport_t *shared =
  65. (struct shmexport_t *)PRIVATE(gpsdata)->shmseg;
  66. volatile bool newdata = false;
  67. timespec_t endtime;
  68. (void)clock_gettime(CLOCK_REALTIME, &endtime);
  69. endtime.tv_sec += timeout / 1000000;
  70. endtime.tv_nsec += (timeout % 1000000) * 1000;
  71. TS_NORM(&endtime);
  72. /* busy-waiting sucks, but there's not really an alternative */
  73. for (;;) {
  74. volatile int bookend1, bookend2;
  75. timespec_t now;
  76. memory_barrier();
  77. bookend1 = shared->bookend1;
  78. memory_barrier();
  79. bookend2 = shared->bookend2;
  80. memory_barrier();
  81. if (bookend1 == bookend2 && bookend1 > PRIVATE(gpsdata)->tick) {
  82. newdata = true;
  83. break;
  84. }
  85. (void)clock_gettime(CLOCK_REALTIME, &now);
  86. if (TS_GT(&now, &endtime)) {
  87. break;
  88. }
  89. }
  90. return newdata;
  91. }
  92. int gps_shm_read(struct gps_data_t *gpsdata)
  93. /* read an update from the shared-memory segment */
  94. {
  95. if (gpsdata->privdata == NULL)
  96. return -1;
  97. else
  98. {
  99. int before, after;
  100. void *private_save = gpsdata->privdata;
  101. volatile struct shmexport_t *shared =
  102. (struct shmexport_t *)PRIVATE(gpsdata)->shmseg;
  103. struct gps_data_t noclobber;
  104. /*
  105. * Following block of instructions must not be reordered,
  106. * otherwise havoc will ensue. The memory_barrier() call
  107. * should prevent reordering of the data accesses.
  108. *
  109. * This is a simple optimistic-concurrency technique. We wrote
  110. * the second bookend first, then the data, then the first bookend.
  111. * Reader copies what it sees in normal order; that way, if we
  112. * start to write the segment during the read, the second bookend will
  113. * get clobbered first and the data can be detected as bad.
  114. */
  115. before = shared->bookend1;
  116. memory_barrier();
  117. (void)memcpy((void *)&noclobber,
  118. (void *)&shared->gpsdata,
  119. sizeof(struct gps_data_t));
  120. memory_barrier();
  121. after = shared->bookend2;
  122. if (before != after)
  123. return 0;
  124. else {
  125. (void)memcpy((void *)gpsdata,
  126. (void *)&noclobber,
  127. sizeof(struct gps_data_t));
  128. gpsdata->privdata = private_save;
  129. #ifndef USE_QT
  130. gpsdata->gps_fd = SHM_PSEUDO_FD;
  131. #else
  132. gpsdata->gps_fd = (void *)(intptr_t)SHM_PSEUDO_FD;
  133. #endif /* USE_QT */
  134. PRIVATE(gpsdata)->tick = after;
  135. if ((gpsdata->set & REPORT_IS)!=0) {
  136. gpsdata->set = STATUS_SET;
  137. }
  138. return (int)sizeof(struct gps_data_t);
  139. }
  140. }
  141. }
  142. void gps_shm_close(struct gps_data_t *gpsdata)
  143. {
  144. if (PRIVATE(gpsdata)) {
  145. if (PRIVATE(gpsdata)->shmseg != NULL)
  146. (void)shmdt((const void *)PRIVATE(gpsdata)->shmseg);
  147. free(PRIVATE(gpsdata));
  148. gpsdata->privdata = NULL;
  149. }
  150. }
  151. int gps_shm_mainloop(struct gps_data_t *gpsdata, int timeout,
  152. void (*hook)(struct gps_data_t *gpsdata))
  153. /* run a shm main loop with a specified handler */
  154. {
  155. for (;;) {
  156. if (!gps_shm_waiting(gpsdata, timeout)) {
  157. return -1;
  158. } else {
  159. int status = gps_shm_read(gpsdata);
  160. if (status == -1)
  161. return -1;
  162. if (status > 0)
  163. (*hook)(gpsdata);
  164. }
  165. }
  166. //return 0;
  167. }
  168. #endif /* SHM_EXPORT_ENABLE */
  169. /* end */
  170. // vim: set expandtab shiftwidth=4