You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timehint.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * timehint.c - put time information in SHM segment for ntpd, or to chrony
  3. *
  4. * Note that for easy debugging all logging from this file is prefixed
  5. * with PPS or NTP.
  6. *
  7. * This file is Copyright (c)2010-2018 by the GPSD project
  8. * SPDX-License-Identifier: BSD-2-clause
  9. */
  10. #include "gpsd_config.h" /* must be before all includes */
  11. #include <errno.h>
  12. #include <libgen.h>
  13. #include <math.h>
  14. #include <stdbool.h>
  15. #include <string.h>
  16. #include <sys/socket.h>
  17. #include <sys/stat.h>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <time.h> /* for timespec */
  21. #include <unistd.h>
  22. #include "timespec.h"
  23. #include "gpsd.h"
  24. #include "ntpshm.h"
  25. /* Note: you can start gpsd as non-root, and have it work with ntpd.
  26. * However, it will then only use the ntpshm segments 2 3, and higher.
  27. *
  28. * Ntpd always runs as root (to be able to control the system clock).
  29. * After that it often (depending on its host configuration) drops to run as
  30. * user ntpd and group ntpd.
  31. *
  32. * As of February 2015 its rules for the creation of ntpshm segments are:
  33. *
  34. * Segments 0 and 1: permissions 0600, i.e. other programs can only
  35. * read and write as root.
  36. *
  37. * Segments 2, 3, and higher:
  38. * permissions 0666, i.e. other programs can read
  39. * and write as any user. I.e.: if ntpd has been
  40. * configured to use these segments, any
  41. * unprivileged user is allowed to provide data
  42. * for synchronisation.
  43. *
  44. * By default ntpd creates 0 segments (though the documentation is
  45. * written in such a way as to suggest it creates 4). It can be
  46. * configured to create up to 217. gpsd creates two segments for each
  47. * device it can drive; by default this is 8 segments for 4
  48. * devices,but can be higher if it was compiled with a larger value of
  49. * MAX_DEVICES.
  50. *
  51. * Started as root, gpsd does as ntpd when attaching (creating) the
  52. * segments. In contrast to ntpd, which only attaches (creates)
  53. * configured segments, gpsd creates all segments. Thus a gpsd will
  54. * by default create eight segments 0-7 that an ntpd with default
  55. * configuration does not watch.
  56. *
  57. * Started as non-root, gpsd will only attach (create) segments 2 and
  58. * above, with permissions 0666. As the permissions are for any user,
  59. * the creator does not matter.
  60. *
  61. * For each GPS module gpsd controls, it will use the attached ntpshm
  62. * segments in pairs (for coarse clock and pps source, respectively)
  63. * starting from the first found segments. I.e. started as root, one
  64. * GPS will deliver data on all segments including 0 and 1; started as
  65. * non-root, gpsd will be deliver data only on segments 2 and higher.
  66. *
  67. * Segments are allocated to activated devices on a first-come-first-served
  68. * basis. A device's segment is marked unused when the device is closed and
  69. * may be re-used by devices connected later.
  70. *
  71. * To debug, try looking at the live segments this way:
  72. *
  73. * ipcs -m
  74. *
  75. * results should look like this:
  76. * ------ Shared Memory Segments --------
  77. * key shmid owner perms bytes nattch status
  78. * 0x4e545030 0 root 700 96 2
  79. * 0x4e545031 32769 root 700 96 2
  80. * 0x4e545032 163842 root 666 96 1
  81. * 0x4e545033 196611 root 666 96 1
  82. * 0x4e545034 253555 root 666 96 1
  83. * 0x4e545035 367311 root 666 96 1
  84. *
  85. * For a bit more data try this:
  86. * cat /proc/sysvipc/shm
  87. *
  88. * If gpsd can not open the segments be sure you are not running SELinux
  89. * or apparmor.
  90. *
  91. * if you see the shared segments (keys 1314148400 -- 1314148405), and
  92. * no gpsd or ntpd is running, you can remove them like this:
  93. *
  94. * ipcrm -M 0x4e545030
  95. * ipcrm -M 0x4e545031
  96. * ipcrm -M 0x4e545032
  97. * ipcrm -M 0x4e545033
  98. * ipcrm -M 0x4e545034
  99. * ipcrm -M 0x4e545035
  100. *
  101. * Removing these segments is usually not necessary, as the operating system
  102. * garbage-collects them when they have no attached processes.
  103. */
  104. static volatile struct shmTime *getShmTime(struct gps_context_t *context, int unit)
  105. {
  106. int shmid;
  107. unsigned int perms;
  108. volatile struct shmTime *p;
  109. // set the SHM perms the way ntpd does
  110. if (unit < 2) {
  111. // we are root, be careful
  112. perms = 0600;
  113. } else {
  114. // we are not root, try to work anyway
  115. perms = 0666;
  116. }
  117. /*
  118. * Note: this call requires root under BSD, and possibly on
  119. * well-secured Linux systems. This is why ntpshm_context_init() has to be
  120. * called before privilege-dropping.
  121. */
  122. shmid = shmget((key_t) (NTPD_BASE + unit),
  123. sizeof(struct shmTime), (int)(IPC_CREAT | perms));
  124. if (shmid == -1) {
  125. GPSD_LOG(LOG_ERROR, &context->errout,
  126. "NTP: shmget(%ld, %zd, %o) fail: %s\n",
  127. (long int)(NTPD_BASE + unit), sizeof(struct shmTime),
  128. (int)perms, strerror(errno));
  129. return NULL;
  130. }
  131. p = (struct shmTime *)shmat(shmid, 0, 0);
  132. if ((int)(long)p == -1) {
  133. GPSD_LOG(LOG_ERROR, &context->errout,
  134. "NTP: shmat failed: %s\n",
  135. strerror(errno));
  136. return NULL;
  137. }
  138. GPSD_LOG(LOG_PROG, &context->errout,
  139. "NTP: shmat(%d,0,0) succeeded, segment %d\n",
  140. shmid, unit);
  141. return p;
  142. }
  143. void ntpshm_context_init(struct gps_context_t *context)
  144. /* Attach all NTP SHM segments. Called once at startup, while still root. */
  145. {
  146. int i;
  147. for (i = 0; i < NTPSHMSEGS; i++) {
  148. // Only grab the first two when running as root.
  149. if (2 <= i || 0 == getuid()) {
  150. context->shmTime[i] = getShmTime(context, i);
  151. }
  152. }
  153. memset(context->shmTimeInuse, 0, sizeof(context->shmTimeInuse));
  154. }
  155. static volatile struct shmTime *ntpshm_alloc(struct gps_context_t *context)
  156. /* allocate NTP SHM segment. return its segment number, or -1 */
  157. {
  158. int i;
  159. for (i = 0; i < NTPSHMSEGS; i++)
  160. if (context->shmTime[i] != NULL && !context->shmTimeInuse[i]) {
  161. context->shmTimeInuse[i] = true;
  162. /*
  163. * In case this segment gets sent to ntpd before an
  164. * ephemeris is available, the LEAP_NOTINSYNC value will
  165. * tell ntpd that this source is in a "clock alarm" state
  166. * and should be ignored. The goal is to prevent ntpd
  167. * from declaring the GPS a falseticker before it gets
  168. * all its marbles together.
  169. */
  170. memset((void *)context->shmTime[i], 0, sizeof(struct shmTime));
  171. context->shmTime[i]->mode = 1;
  172. context->shmTime[i]->leap = LEAP_NOTINSYNC;
  173. context->shmTime[i]->precision = -20;/* initially 1 micro sec */
  174. context->shmTime[i]->nsamples = 3; /* stages of median filter */
  175. return context->shmTime[i];
  176. }
  177. return NULL;
  178. }
  179. static bool ntpshm_free(struct gps_context_t * context, volatile struct shmTime *s)
  180. /* free NTP SHM segment */
  181. {
  182. int i;
  183. for (i = 0; i < NTPSHMSEGS; i++)
  184. if (s == context->shmTime[i]) {
  185. context->shmTimeInuse[i] = false;
  186. return true;
  187. }
  188. return false;
  189. }
  190. void ntpshm_session_init(struct gps_device_t *session)
  191. {
  192. /* mark NTPD shared memory segments as unused */
  193. session->shm_clock = NULL;
  194. session->shm_pps = NULL;
  195. }
  196. /* put a received fix time into shared memory for NTP */
  197. int ntpshm_put(struct gps_device_t *session, volatile struct shmTime *shmseg,
  198. struct timedelta_t *td)
  199. {
  200. char real_str[TIMESPEC_LEN];
  201. char clock_str[TIMESPEC_LEN];
  202. /* Any NMEA will be about -1 or -2. Garmin GPS-18/USB is around -6 or -7. */
  203. int precision = -20; /* default precision, 1 micro sec */
  204. if (shmseg == NULL) {
  205. GPSD_LOG(LOG_RAW, &session->context->errout, "NTP:PPS: missing shm\n");
  206. return 0;
  207. }
  208. // FIXME: make NMEA precision -1
  209. if (shmseg == session->shm_pps) {
  210. /* precision is a floor so do not make it tight */
  211. if ( source_usb == session->sourcetype ) {
  212. /* if PPS over USB, then precision = -10, 1 milli sec */
  213. precision = -10;
  214. } else {
  215. /* likely PPS over serial, precision = -20, 1 micro sec */
  216. precision = -20;
  217. }
  218. }
  219. ntp_write(shmseg, td, precision, session->context->leap_notify);
  220. GPSD_LOG(LOG_PROG, &session->context->errout,
  221. "NTP: ntpshm_put(%s,%d) %s @ %s\n",
  222. session->gpsdata.dev.path,
  223. precision,
  224. timespec_str(&td->real, real_str, sizeof(real_str)),
  225. timespec_str(&td->clock, clock_str, sizeof(clock_str)));
  226. return 1;
  227. }
  228. #define SOCK_MAGIC 0x534f434b
  229. struct sock_sample {
  230. struct timeval tv;
  231. double offset;
  232. int pulse;
  233. int leap; /* notify that a leap second is upcoming */
  234. // cppcheck-suppress unusedStructMember
  235. int _pad;
  236. int magic; /* must be SOCK_MAGIC */
  237. };
  238. static void init_hook(struct gps_device_t *session)
  239. /* for chrony SOCK interface, which allows nSec timekeeping */
  240. {
  241. /* open the chrony socket */
  242. char chrony_path[GPS_PATH_MAX];
  243. session->chronyfd = -1;
  244. if ( 0 == getuid() ) {
  245. /* this case will fire on command-line devices;
  246. * they're opened before priv-dropping. Matters because
  247. * only root can use /var/run.
  248. */
  249. (void)snprintf(chrony_path, sizeof (chrony_path),
  250. "/var/run/chrony.%s.sock", basename(session->gpsdata.dev.path));
  251. } else {
  252. (void)snprintf(chrony_path, sizeof (chrony_path),
  253. "/tmp/chrony.%s.sock", basename(session->gpsdata.dev.path));
  254. }
  255. if (access(chrony_path, F_OK) != 0) {
  256. GPSD_LOG(LOG_PROG, &session->context->errout,
  257. "PPS:%s chrony socket %s doesn't exist\n",
  258. session->gpsdata.dev.path, chrony_path);
  259. } else {
  260. session->chronyfd = netlib_localsocket(chrony_path, SOCK_DGRAM);
  261. if (session->chronyfd < 0)
  262. GPSD_LOG(LOG_PROG, &session->context->errout,
  263. "PPS:%s connect chrony socket failed: %s, error: %d, errno: %d/%s\n",
  264. session->gpsdata.dev.path,
  265. chrony_path, session->chronyfd, errno, strerror(errno));
  266. else
  267. GPSD_LOG(LOG_RAW, &session->context->errout,
  268. "PPS:%s using chrony socket: %s\n",
  269. session->gpsdata.dev.path, chrony_path);
  270. }
  271. }
  272. /* td is the real time and clock time of the edge */
  273. /* offset is actual_ts - clock_ts */
  274. static void chrony_send(struct gps_device_t *session, struct timedelta_t *td)
  275. {
  276. char real_str[TIMESPEC_LEN];
  277. char clock_str[TIMESPEC_LEN];
  278. struct sock_sample sample;
  279. struct tm tm;
  280. int leap_notify = session->context->leap_notify;
  281. /*
  282. * insist that leap seconds only happen in june and december
  283. * GPS emits leap pending for 3 months prior to insertion
  284. * NTP expects leap pending for only 1 month prior to insertion
  285. * Per http://bugs.ntp.org/1090
  286. *
  287. * ITU-R TF.460-6, Section 2.1, says leap seconds can be primarily
  288. * in Jun/Dec but may be in March or September
  289. */
  290. (void)gmtime_r( &(td->real.tv_sec), &tm);
  291. if ( 5 != tm.tm_mon && 11 != tm.tm_mon ) {
  292. /* Not june, not December, no way */
  293. leap_notify = LEAP_NOWARNING;
  294. }
  295. /* chrony expects tv-sec since Jan 1970 */
  296. sample.pulse = 0;
  297. sample.leap = leap_notify;
  298. sample.magic = SOCK_MAGIC;
  299. /* chronyd wants a timeval, not a timspec, not to worry, it is
  300. * just the top of the second */
  301. TSTOTV(&sample.tv, &td->clock);
  302. /* calculate the offset as a timespec to not lose precision */
  303. /* if tv_sec greater than 2 then tv_nsec loses precision, but
  304. * not a big deal as slewing will be required */
  305. sample.offset = TS_SUB_D(&td->real, &td->clock);
  306. sample._pad = 0;
  307. GPSD_LOG(LOG_RAW, &session->context->errout,
  308. "PPS chrony_send %s @ %s Offset: %0.9f\n",
  309. timespec_str(&td->real, real_str, sizeof(real_str)),
  310. timespec_str(&td->clock, clock_str, sizeof(clock_str)),
  311. sample.offset);
  312. (void)send(session->chronyfd, &sample, sizeof (sample), 0);
  313. }
  314. static char *report_hook(volatile struct pps_thread_t *pps_thread,
  315. struct timedelta_t *td)
  316. /* ship the time of a PPS event to ntpd and/or chrony */
  317. {
  318. char *log1;
  319. struct gps_device_t *session = (struct gps_device_t *)pps_thread->context;
  320. /* PPS only source never get any serial info
  321. * so no NTPTIME_IS or fixcnt */
  322. if ( source_pps != session->sourcetype) {
  323. /* FIXME! these two validations need to move back into ppsthread.c */
  324. if ( !session->ship_to_ntpd)
  325. return "skipped ship_to_ntp=0";
  326. /*
  327. * Only listen to PPS after several consecutive fixes,
  328. * otherwise time may be inaccurate. (We know this is
  329. * required on all Garmin and u-blox; safest to do it
  330. * for all cases as we have no other general way to know
  331. * if PPS is good.
  332. */
  333. if (session->fixcnt <= NTP_MIN_FIXES &&
  334. (session->gpsdata.set & GOODTIME_IS) == 0)
  335. return "no fix";
  336. }
  337. /* FIXME? how to log socket AND shm reported? */
  338. log1 = "accepted";
  339. if ( 0 <= session->chronyfd ) {
  340. log1 = "accepted chrony sock";
  341. chrony_send(session, td);
  342. }
  343. if (session->shm_pps != NULL)
  344. (void)ntpshm_put(session, session->shm_pps, td);
  345. /* session context might have a hook set, too */
  346. if (session->context->pps_hook != NULL)
  347. session->context->pps_hook(session, td);
  348. return log1;
  349. }
  350. void ntpshm_link_deactivate(struct gps_device_t *session)
  351. /* release ntpshm storage for a session */
  352. {
  353. if (session->shm_clock != NULL) {
  354. (void)ntpshm_free(session->context, session->shm_clock);
  355. session->shm_clock = NULL;
  356. }
  357. if (session->shm_pps != NULL) {
  358. pps_thread_deactivate(&session->pps_thread);
  359. if (session->chronyfd != -1)
  360. (void)close(session->chronyfd);
  361. (void)ntpshm_free(session->context, session->shm_pps);
  362. session->shm_pps = NULL;
  363. }
  364. }
  365. void ntpshm_link_activate(struct gps_device_t *session)
  366. /* set up ntpshm storage for a session */
  367. {
  368. /* don't talk to NTP when we're running inside the test harness */
  369. if (session->sourcetype == source_pty)
  370. return;
  371. if (session->sourcetype != source_pps ) {
  372. /* allocate a shared-memory segment for "NMEA" time data */
  373. session->shm_clock = ntpshm_alloc(session->context);
  374. if (session->shm_clock == NULL) {
  375. GPSD_LOG(LOG_WARN, &session->context->errout,
  376. "NTP: ntpshm_alloc() failed\n");
  377. return;
  378. }
  379. }
  380. if (session->sourcetype == source_usb
  381. || session->sourcetype == source_rs232
  382. || session->sourcetype == source_pps) {
  383. /* We also have the 1pps capability, allocate a shared-memory segment
  384. * for the 1pps time data and launch a thread to capture the 1pps
  385. * transitions
  386. */
  387. if ((session->shm_pps = ntpshm_alloc(session->context)) == NULL) {
  388. GPSD_LOG(LOG_WARN, &session->context->errout,
  389. "PPS: ntpshm_alloc(1) failed\n");
  390. } else {
  391. init_hook(session);
  392. session->pps_thread.report_hook = report_hook;
  393. #ifdef MAGIC_HAT_ENABLE
  394. /*
  395. * The HAT kludge. If we're using the HAT GPS on a
  396. * Raspberry Pi or a workalike like the ODROIDC2, and
  397. * there is a static "first PPS", and we have access because
  398. * we're root, assume we want to use KPPS.
  399. */
  400. if (strcmp(session->pps_thread.devicename, MAGIC_HAT_GPS) == 0
  401. || strcmp(session->pps_thread.devicename,
  402. MAGIC_LINK_GPS) == 0) {
  403. char *first_pps = pps_get_first();
  404. if (access(first_pps, R_OK | W_OK) == 0)
  405. session->pps_thread.devicename = first_pps;
  406. }
  407. #endif /* MAGIC_HAT_ENABLE */
  408. pps_thread_activate(&session->pps_thread);
  409. }
  410. }
  411. }
  412. /* end */