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.

278 lines
6.3 KiB

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