Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
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.

301 lines
6.8 KiB

18 years ago
  1. /* $OpenBSD: sensors.c,v 1.47 2013/10/04 14:28:16 phessler Exp $ */
  2. /*
  3. * Copyright (c) 2006 Henning Brauer <henning@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/param.h>
  18. #include <sys/queue.h>
  19. #include <sys/sensors.h>
  20. #include <sys/sysctl.h>
  21. #include <sys/device.h>
  22. #include <sys/hotplug.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include "ntpd.h"
  30. #define MAXDEVNAMLEN 16
  31. #define _PATH_DEV_HOTPLUG "/dev/hotplug"
  32. int sensor_probe(int, char *, struct sensor *);
  33. void sensor_add(int, char *);
  34. void sensor_remove(struct ntp_sensor *);
  35. void sensor_update(struct ntp_sensor *);
  36. void
  37. sensor_init(void)
  38. {
  39. TAILQ_INIT(&conf->ntp_sensors);
  40. }
  41. int
  42. sensor_scan(void)
  43. {
  44. int i, n, err;
  45. char d[MAXDEVNAMLEN];
  46. struct sensor s;
  47. n = 0;
  48. for (i = 0; ; i++)
  49. if ((err = sensor_probe(i, d, &s))) {
  50. if (err == 0)
  51. continue;
  52. if (err == -1) /* no further sensors */
  53. break;
  54. sensor_add(i, d);
  55. n++;
  56. }
  57. return n;
  58. }
  59. /*
  60. * 1 = time sensor!
  61. * 0 = sensor exists... but is not a time sensor
  62. * -1: no sensor here, and no further sensors after this
  63. */
  64. int
  65. sensor_probe(int devid, char *dxname, struct sensor *sensor)
  66. {
  67. int mib[5];
  68. size_t slen, sdlen;
  69. struct sensordev sensordev;
  70. mib[0] = CTL_HW;
  71. mib[1] = HW_SENSORS;
  72. mib[2] = devid;
  73. mib[3] = SENSOR_TIMEDELTA;
  74. mib[4] = 0;
  75. sdlen = sizeof(sensordev);
  76. if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
  77. if (errno == ENXIO)
  78. return (0);
  79. if (errno == ENOENT)
  80. return (-1);
  81. log_warn("sensor_probe sysctl");
  82. }
  83. if (sensordev.maxnumt[SENSOR_TIMEDELTA] == 0)
  84. return (0);
  85. strlcpy(dxname, sensordev.xname, MAXDEVNAMLEN);
  86. slen = sizeof(*sensor);
  87. if (sysctl(mib, 5, sensor, &slen, NULL, 0) == -1) {
  88. if (errno != ENOENT)
  89. log_warn("sensor_probe sysctl");
  90. return (0);
  91. }
  92. return (1);
  93. }
  94. void
  95. sensor_add(int sensordev, char *dxname)
  96. {
  97. struct ntp_sensor *s;
  98. struct ntp_conf_sensor *cs;
  99. /* check whether it is already there */
  100. TAILQ_FOREACH(s, &conf->ntp_sensors, entry)
  101. if (!strcmp(s->device, dxname))
  102. return;
  103. /* check whether it is requested in the config file */
  104. for (cs = TAILQ_FIRST(&conf->ntp_conf_sensors); cs != NULL &&
  105. strcmp(cs->device, dxname) && strcmp(cs->device, "*");
  106. cs = TAILQ_NEXT(cs, entry))
  107. ; /* nothing */
  108. if (cs == NULL)
  109. return;
  110. if ((s = calloc(1, sizeof(*s))) == NULL)
  111. fatal("sensor_add calloc");
  112. s->next = getmonotime();
  113. s->weight = cs->weight;
  114. s->correction = cs->correction;
  115. s->stratum = cs->stratum - 1;
  116. if ((s->device = strdup(dxname)) == NULL)
  117. fatal("sensor_add strdup");
  118. s->sensordevid = sensordev;
  119. if (cs->refstr == NULL)
  120. memcpy(&s->refid, SENSOR_DEFAULT_REFID, sizeof(s->refid));
  121. else {
  122. s->refid = 0;
  123. strncpy((char *)&s->refid, cs->refstr, sizeof(s->refid));
  124. }
  125. TAILQ_INSERT_TAIL(&conf->ntp_sensors, s, entry);
  126. log_debug("sensor %s added (weight %d, correction %.6f, refstr %.4s, "
  127. "stratum %d)", s->device, s->weight, s->correction / 1e6,
  128. &s->refid, s->stratum);
  129. }
  130. void
  131. sensor_remove(struct ntp_sensor *s)
  132. {
  133. TAILQ_REMOVE(&conf->ntp_sensors, s, entry);
  134. free(s->device);
  135. free(s);
  136. }
  137. void
  138. sensor_query(struct ntp_sensor *s)
  139. {
  140. char dxname[MAXDEVNAMLEN];
  141. struct sensor sensor;
  142. if (conf->settime)
  143. s->next = getmonotime() + SENSOR_QUERY_INTERVAL_SETTIME;
  144. else
  145. s->next = getmonotime() + SENSOR_QUERY_INTERVAL;
  146. /* rcvd is walltime here, monotime in client.c. not used elsewhere */
  147. if (s->update.rcvd < time(NULL) - SENSOR_DATA_MAXAGE)
  148. s->update.good = 0;
  149. if (!sensor_probe(s->sensordevid, dxname, &sensor)) {
  150. sensor_remove(s);
  151. return;
  152. }
  153. if (sensor.flags & SENSOR_FINVALID ||
  154. sensor.status != SENSOR_S_OK)
  155. return;
  156. if (strcmp(dxname, s->device)) {
  157. sensor_remove(s);
  158. return;
  159. }
  160. if (sensor.tv.tv_sec == s->last) /* already seen */
  161. return;
  162. s->last = sensor.tv.tv_sec;
  163. /*
  164. * TD = device time
  165. * TS = system time
  166. * sensor.value = TS - TD in ns
  167. * if value is positive, system time is ahead
  168. */
  169. s->offsets[s->shift].offset = (sensor.value / -1e9) - getoffset() +
  170. (s->correction / 1e6);
  171. s->offsets[s->shift].rcvd = sensor.tv.tv_sec;
  172. s->offsets[s->shift].good = 1;
  173. s->offsets[s->shift].status.send_refid = s->refid;
  174. /* stratum increased when sent out */
  175. s->offsets[s->shift].status.stratum = s->stratum;
  176. s->offsets[s->shift].status.rootdelay = 0;
  177. s->offsets[s->shift].status.rootdispersion = 0;
  178. s->offsets[s->shift].status.reftime = sensor.tv.tv_sec;
  179. s->offsets[s->shift].status.synced = 1;
  180. log_debug("sensor %s: offset %f", s->device,
  181. s->offsets[s->shift].offset);
  182. if (++s->shift >= SENSOR_OFFSETS) {
  183. s->shift = 0;
  184. sensor_update(s);
  185. }
  186. }
  187. void
  188. sensor_update(struct ntp_sensor *s)
  189. {
  190. struct ntp_offset **offsets;
  191. int i;
  192. if ((offsets = calloc(SENSOR_OFFSETS, sizeof(struct ntp_offset *))) ==
  193. NULL)
  194. fatal("calloc sensor_update");
  195. for (i = 0; i < SENSOR_OFFSETS; i++)
  196. offsets[i] = &s->offsets[i];
  197. qsort(offsets, SENSOR_OFFSETS, sizeof(struct ntp_offset *),
  198. offset_compare);
  199. i = SENSOR_OFFSETS / 2;
  200. memcpy(&s->update, offsets[i], sizeof(s->update));
  201. if (SENSOR_OFFSETS % 2 == 0) {
  202. s->update.offset =
  203. (offsets[i - 1]->offset + offsets[i]->offset) / 2;
  204. }
  205. free(offsets);
  206. log_debug("sensor update %s: offset %f", s->device, s->update.offset);
  207. priv_adjtime();
  208. }
  209. int
  210. sensor_hotplugfd(void)
  211. {
  212. #ifdef notyet
  213. int fd, flags;
  214. if ((fd = open(_PATH_DEV_HOTPLUG, O_RDONLY, 0)) == -1) {
  215. log_warn("open %s", _PATH_DEV_HOTPLUG);
  216. return (-1);
  217. }
  218. if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
  219. fatal("fcntl F_GETFL");
  220. flags |= O_NONBLOCK;
  221. if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
  222. fatal("fcntl F_SETFL");
  223. return (fd);
  224. #else
  225. return (-1);
  226. #endif
  227. }
  228. void
  229. sensor_hotplugevent(int fd)
  230. {
  231. struct hotplug_event he;
  232. ssize_t n;
  233. do {
  234. if ((n = read(fd, &he, sizeof(he))) == -1 &&
  235. errno != EINTR && errno != EAGAIN)
  236. fatal("sensor_hotplugevent read");
  237. if (n == sizeof(he))
  238. switch (he.he_type) {
  239. case HOTPLUG_DEVAT:
  240. if (he.he_devclass == DV_DULL &&
  241. !strcmp(he.he_devname, "sensordev"))
  242. sensor_scan();
  243. break;
  244. default: /* ignore */
  245. break;
  246. }
  247. else if (n > 0)
  248. fatal("sensor_hotplugevent: short read");
  249. } while (n > 0 || (n == -1 && errno == EINTR));
  250. }