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.

285 lines
6.4 KiB

18 years ago
  1. /* $OpenBSD: sensors.c,v 1.40 2008/06/10 03:46:09 naddy 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;
  45. char d[MAXDEVNAMLEN];
  46. struct sensor s;
  47. n = 0;
  48. for (i = 0; i < MAXSENSORDEVICES; i++)
  49. if (sensor_probe(i, d, &s)) {
  50. sensor_add(i, d);
  51. n++;
  52. }
  53. return n;
  54. }
  55. int
  56. sensor_probe(int devid, char *dxname, struct sensor *sensor)
  57. {
  58. int mib[5];
  59. size_t slen, sdlen;
  60. struct sensordev sensordev;
  61. mib[0] = CTL_HW;
  62. mib[1] = HW_SENSORS;
  63. mib[2] = devid;
  64. mib[3] = SENSOR_TIMEDELTA;
  65. mib[4] = 0;
  66. sdlen = sizeof(sensordev);
  67. if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
  68. if (errno != ENOENT)
  69. log_warn("sensor_probe sysctl");
  70. return (0);
  71. }
  72. if (sensordev.maxnumt[SENSOR_TIMEDELTA] == 0)
  73. return (0);
  74. strlcpy(dxname, sensordev.xname, MAXDEVNAMLEN);
  75. slen = sizeof(*sensor);
  76. if (sysctl(mib, 5, sensor, &slen, NULL, 0) == -1) {
  77. if (errno != ENOENT)
  78. log_warn("sensor_probe sysctl");
  79. return (0);
  80. }
  81. return (1);
  82. }
  83. void
  84. sensor_add(int sensordev, char *dxname)
  85. {
  86. struct ntp_sensor *s;
  87. struct ntp_conf_sensor *cs;
  88. /* check wether it is already there */
  89. TAILQ_FOREACH(s, &conf->ntp_sensors, entry)
  90. if (!strcmp(s->device, dxname))
  91. return;
  92. /* check whether it is requested in the config file */
  93. for (cs = TAILQ_FIRST(&conf->ntp_conf_sensors); cs != NULL &&
  94. strcmp(cs->device, dxname) && strcmp(cs->device, "*");
  95. cs = TAILQ_NEXT(cs, entry))
  96. ; /* nothing */
  97. if (cs == NULL)
  98. return;
  99. if ((s = calloc(1, sizeof(*s))) == NULL)
  100. fatal("sensor_add calloc");
  101. s->next = getmonotime();
  102. s->weight = cs->weight;
  103. s->correction = cs->correction;
  104. if ((s->device = strdup(dxname)) == NULL)
  105. fatal("sensor_add strdup");
  106. s->sensordevid = sensordev;
  107. if (cs->refstr == NULL)
  108. memcpy(&s->refid, "HARD", sizeof(s->refid));
  109. else {
  110. s->refid = 0;
  111. strncpy((char *)&s->refid, cs->refstr, sizeof(s->refid));
  112. }
  113. TAILQ_INSERT_TAIL(&conf->ntp_sensors, s, entry);
  114. log_debug("sensor %s added (weight %d, correction %.6f, refstr %-4s)",
  115. s->device, s->weight, s->correction / 1e6, &s->refid);
  116. s->refid = htonl(s->refid);
  117. }
  118. void
  119. sensor_remove(struct ntp_sensor *s)
  120. {
  121. TAILQ_REMOVE(&conf->ntp_sensors, s, entry);
  122. free(s->device);
  123. free(s);
  124. }
  125. void
  126. sensor_query(struct ntp_sensor *s)
  127. {
  128. char dxname[MAXDEVNAMLEN];
  129. struct sensor sensor;
  130. s->next = getmonotime() + SENSOR_QUERY_INTERVAL;
  131. /* rcvd is walltime here, monotime in client.c. not used elsewhere */
  132. if (s->update.rcvd < time(NULL) - SENSOR_DATA_MAXAGE)
  133. s->update.good = 0;
  134. if (!sensor_probe(s->sensordevid, dxname, &sensor)) {
  135. sensor_remove(s);
  136. return;
  137. }
  138. if (sensor.flags & SENSOR_FINVALID ||
  139. sensor.status != SENSOR_S_OK)
  140. return;
  141. if (strcmp(dxname, s->device)) {
  142. sensor_remove(s);
  143. return;
  144. }
  145. if (sensor.tv.tv_sec == s->last) /* already seen */
  146. return;
  147. s->last = sensor.tv.tv_sec;
  148. /*
  149. * TD = device time
  150. * TS = system time
  151. * sensor.value = TS - TD in ns
  152. * if value is positive, system time is ahead
  153. */
  154. s->offsets[s->shift].offset = (sensor.value / -1e9) - getoffset() +
  155. (s->correction / 1e6);
  156. s->offsets[s->shift].rcvd = sensor.tv.tv_sec;
  157. s->offsets[s->shift].good = 1;
  158. s->offsets[s->shift].status.refid = s->refid;
  159. s->offsets[s->shift].status.stratum = 0; /* increased when sent out */
  160. s->offsets[s->shift].status.rootdelay = 0;
  161. s->offsets[s->shift].status.rootdispersion = 0;
  162. s->offsets[s->shift].status.reftime = sensor.tv.tv_sec;
  163. s->offsets[s->shift].status.synced = 1;
  164. log_debug("sensor %s: offset %f", s->device,
  165. s->offsets[s->shift].offset);
  166. if (++s->shift >= SENSOR_OFFSETS) {
  167. s->shift = 0;
  168. sensor_update(s);
  169. }
  170. }
  171. void
  172. sensor_update(struct ntp_sensor *s)
  173. {
  174. struct ntp_offset **offsets;
  175. int i;
  176. if ((offsets = calloc(SENSOR_OFFSETS, sizeof(struct ntp_offset *))) ==
  177. NULL)
  178. fatal("calloc sensor_update");
  179. for (i = 0; i < SENSOR_OFFSETS; i++)
  180. offsets[i] = &s->offsets[i];
  181. qsort(offsets, SENSOR_OFFSETS, sizeof(struct ntp_offset *),
  182. offset_compare);
  183. i = SENSOR_OFFSETS / 2;
  184. memcpy(&s->update, offsets[i], sizeof(s->update));
  185. if (SENSOR_OFFSETS % 2 == 0) {
  186. s->update.offset =
  187. (offsets[i - 1]->offset + offsets[i]->offset) / 2;
  188. }
  189. free(offsets);
  190. log_debug("sensor update %s: offset %f", s->device, s->update.offset);
  191. priv_adjtime();
  192. }
  193. int
  194. sensor_hotplugfd(void)
  195. {
  196. #ifdef notyet
  197. int fd, flags;
  198. if ((fd = open(_PATH_DEV_HOTPLUG, O_RDONLY, 0)) == -1) {
  199. log_warn("open %s", _PATH_DEV_HOTPLUG);
  200. return (-1);
  201. }
  202. if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
  203. fatal("fcntl F_GETFL");
  204. flags |= O_NONBLOCK;
  205. if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
  206. fatal("fcntl F_SETFL");
  207. return (fd);
  208. #else
  209. return (-1);
  210. #endif
  211. }
  212. void
  213. sensor_hotplugevent(int fd)
  214. {
  215. struct hotplug_event he;
  216. ssize_t n;
  217. do {
  218. if ((n = read(fd, &he, sizeof(he))) == -1 &&
  219. errno != EINTR && errno != EAGAIN)
  220. fatal("sensor_hotplugevent read");
  221. if (n == sizeof(he))
  222. switch (he.he_type) {
  223. case HOTPLUG_DEVAT:
  224. if (he.he_devclass == DV_DULL &&
  225. !strcmp(he.he_devname, "sensordev"))
  226. sensor_scan();
  227. break;
  228. default: /* ignore */
  229. break;
  230. }
  231. else if (n > 0)
  232. fatal("sensor_hotplugevent: short read");
  233. } while (n > 0 || (n == -1 && errno == EINTR));
  234. }