ntpshmread.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* ntpshmread.c -- monitor the client side of an ntpshmwrite.connection
  2. *
  3. * This file is Copyright 2010 by the GPSD project
  4. * SPDX-License-Identifier: BSD-2-clause
  5. *
  6. * Some of this was swiped from the NTPD distribution.
  7. *
  8. */
  9. #include "../include/gpsd_config.h" // must be before all includes
  10. #include <assert.h>
  11. #include <errno.h>
  12. #include <math.h>
  13. #include <stdbool.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <sys/stat.h>
  18. #include <sys/types.h>
  19. #include <unistd.h>
  20. #include "../include/ntpshm.h"
  21. #include "../include/compiler.h"
  22. // initialize a SHM segment
  23. struct shmTime *shm_get(const int unit, const bool create, const bool forall)
  24. {
  25. struct shmTime *p = NULL;
  26. int shmid;
  27. /*
  28. * Big units will give non-ASCII but that's OK
  29. * as long as everybody does it the same way.
  30. */
  31. shmid = shmget((key_t)(NTPD_BASE + unit), sizeof(struct shmTime),
  32. (create ? IPC_CREAT : 0) | (forall ? 0666 : 0600));
  33. if (shmid == -1) { // error
  34. if (ENOENT != errno) {
  35. // if ENOENT (2:No such file or directory)
  36. // could be forgot to be root "Permission denied(13)"
  37. // we scan for many uncommon, usually non-exitent, SHMs as well.
  38. (void)fprintf(stderr, "WARNING: could not open SHM(%d): %s(%d)\n",
  39. unit, strerror(errno), errno);
  40. }
  41. return NULL;
  42. }
  43. p = (struct shmTime *)shmat(shmid, 0, 0);
  44. if ((struct shmTime *)-1 == p) { // error
  45. (void)fprintf(stderr, "WARNING: unit %d, shmat(x%x): %s(%d)\n",
  46. unit, shmid, strerror(errno), errno);
  47. return NULL;
  48. }
  49. return p;
  50. }
  51. // return the name of a specified segment
  52. char *ntp_name(const int unit)
  53. {
  54. static char name[5] = "NTP\0";
  55. name[3] = (char)('0' + unit);
  56. return name;
  57. }
  58. // try to grab a sample from the specified SHM segment
  59. enum segstat_t ntp_read(struct shmTime *shm_in, struct shm_stat_t *shm_stat,
  60. const bool consume)
  61. {
  62. volatile struct shmTime shmcopy, *shm = shm_in;
  63. volatile int cnt;
  64. unsigned int cns_new, rns_new;
  65. if (NULL == shm) {
  66. shm_stat->status = NO_SEGMENT;
  67. return NO_SEGMENT;
  68. }
  69. shm_stat->tvc.tv_sec = shm_stat->tvc.tv_nsec = 0;
  70. // relying on word access to be atomic here
  71. if (0 == shm->valid) {
  72. shm_stat->status = NOT_READY;
  73. return NOT_READY;
  74. }
  75. cnt = shm->count;
  76. /*
  77. * This is proof against concurrency issues if either (a) the
  78. * memory_barrier() call works on this host, or (b) memset
  79. * compiles to an uninterruptible single-instruction bitblt (this
  80. * will probably cease to be true if the structure exceeds your VM
  81. * page size).
  82. */
  83. memory_barrier();
  84. memcpy((void *)&shmcopy, (void *)shm, sizeof(struct shmTime));
  85. /*
  86. * An updated consumer such as ntpd should zero the valid flag at this point.
  87. * A program snooping the updates to collect statistics should not, lest
  88. * it make the data unavailable for consumers.
  89. */
  90. if (consume) {
  91. shm->valid = 0;
  92. }
  93. memory_barrier();
  94. /*
  95. * Clash detection in case neither (a) nor (b) was true.
  96. * Not supported in mode 0, and word access to the count field
  97. * must be atomic for this to work.
  98. */
  99. if (0 < shmcopy.mode &&
  100. cnt != shm->count) {
  101. shm_stat->status = CLASH;
  102. return shm_stat->status;
  103. }
  104. shm_stat->status = OK;
  105. switch (shmcopy.mode) {
  106. case 0:
  107. shm_stat->tvr.tv_sec = shmcopy.receiveTimeStampSec;
  108. shm_stat->tvr.tv_nsec = shmcopy.receiveTimeStampUSec * 1000;
  109. rns_new = shmcopy.receiveTimeStampNSec;
  110. shm_stat->tvt.tv_sec = shmcopy.clockTimeStampSec;
  111. shm_stat->tvt.tv_nsec = shmcopy.clockTimeStampUSec * 1000;
  112. cns_new = shmcopy.clockTimeStampNSec;
  113. /* Since the following comparisons are between unsigned
  114. ** variables they are always well defined, and any
  115. ** (signed) underflow will turn into very large unsigned
  116. ** values, well above the 1000 cutoff.
  117. **
  118. ** Note: The usecs *must* be a *truncated*
  119. ** representation of the nsecs. This code will fail for
  120. ** *rounded* usecs, and the logic to deal with
  121. ** wrap-arounds in the presence of rounded values is
  122. ** much more convoluted.
  123. */
  124. if ((1000 > (cns_new - (unsigned)shm_stat->tvt.tv_nsec)) &&
  125. (1000 > (rns_new - (unsigned)shm_stat->tvr.tv_nsec))) {
  126. shm_stat->tvt.tv_nsec = cns_new;
  127. shm_stat->tvr.tv_nsec = rns_new;
  128. }
  129. /* At this point shm_stat->tvr and shm_stat->tvt contain valid ns-level
  130. ** timestamps, possibly generated by extending the old
  131. ** us-level timestamps
  132. */
  133. break;
  134. case 1:
  135. shm_stat->tvr.tv_sec = shmcopy.receiveTimeStampSec;
  136. shm_stat->tvr.tv_nsec = shmcopy.receiveTimeStampUSec * 1000;
  137. rns_new = shmcopy.receiveTimeStampNSec;
  138. shm_stat->tvt.tv_sec = shmcopy.clockTimeStampSec;
  139. shm_stat->tvt.tv_nsec = shmcopy.clockTimeStampUSec * 1000;
  140. cns_new = shmcopy.clockTimeStampNSec;
  141. /* See the case above for an explanation of the
  142. ** following test.
  143. */
  144. if ((1000 > (cns_new - (unsigned)shm_stat->tvt.tv_nsec)) &&
  145. (1000 > (rns_new - (unsigned)shm_stat->tvr.tv_nsec))) {
  146. shm_stat->tvt.tv_nsec = cns_new;
  147. shm_stat->tvr.tv_nsec = rns_new;
  148. }
  149. /* At this point shm_stat->tvr and shm_stat->tvt contains valid ns-level
  150. ** timestamps, possibly generated by extending the old
  151. ** us-level timestamps
  152. */
  153. break;
  154. default:
  155. shm_stat->status = BAD_MODE;
  156. break;
  157. }
  158. /*
  159. * leap field is not a leap offset but a leap notification code.
  160. * The values are magic numbers used by NTP and set by GPSD, if at all, in
  161. * the subframe code.
  162. */
  163. shm_stat->leap = shmcopy.leap;
  164. shm_stat->precision = shmcopy.precision;
  165. return shm_stat->status;
  166. }
  167. // vim: set expandtab shiftwidth=4