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