1 /*-
2  * Copyright (c) 2014 Luiz Otavio O Souza <loos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/sysctl.h>
33 
34 #include <bsnmp/snmpmod.h>
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <syslog.h>
40 
41 #include "lm75_oid.h"
42 #include "lm75_tree.h"
43 
44 #ifndef	LM75BUF
45 #define	LM75BUF		64
46 #endif
47 #define	TZ_ZEROC	2732
48 #define	UPDATE_INTERVAL	500	/* update interval in ticks */
49 
50 static struct lmodule *module;
51 
52 static const struct asn_oid oid_lm75 = OIDX_begemotLm75;
53 
54 /* the Object Resource registration index */
55 static u_int lm75_index = 0;
56 
57 /* Number of available sensors in the system. */
58 static int lm75_sensors;
59 
60 /*
61  * Structure that describes single sensor.
62  */
63 struct lm75_snmp_sensor {
64 	TAILQ_ENTRY(lm75_snmp_sensor) link;
65 	int32_t		index;
66 	int32_t		sysctlidx;
67 	int32_t		temp;
68 	char		desc[LM75BUF];
69 	char		location[LM75BUF];
70 	char		parent[LM75BUF];
71 	char		pnpinfo[LM75BUF];
72 };
73 
74 static TAILQ_HEAD(, lm75_snmp_sensor) sensors =
75     TAILQ_HEAD_INITIALIZER(sensors);
76 
77 /* Ticks of the last sensors reading. */
78 static uint64_t last_sensors_update;
79 
80 static void free_sensors(void);
81 static int lm75_fini(void);
82 static int lm75_init(struct lmodule *mod, int argc, char *argv[]);
83 static void lm75_start(void);
84 static int update_sensors(void);
85 
86 const struct snmp_module config = {
87     .comment   =
88 	"This module implements the BEGEMOT MIB for reading LM75 sensors data.",
89     .init      = lm75_init,
90     .start     = lm75_start,
91     .fini      = lm75_fini,
92     .tree      = lm75_ctree,
93     .tree_size = lm75_CTREE_SIZE,
94 };
95 
96 static int
97 lm75_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)
98 {
99 
100 	module = mod;
101 
102 	lm75_sensors = 0;
103 	openlog("snmp_lm75", LOG_NDELAY | LOG_PID, LOG_DAEMON);
104 
105 	return(0);
106 }
107 
108 static void
109 lm75_start(void)
110 {
111 
112 	lm75_index = or_register(&oid_lm75,
113 	    "The MIB module for reading lm75 sensors data.", module);
114 }
115 
116 static int
117 lm75_fini(void)
118 {
119 
120 	or_unregister(lm75_index);
121 	free_sensors();
122 	closelog();
123 
124 	return (0);
125 }
126 
127 static void
128 free_sensors(void)
129 {
130 	struct lm75_snmp_sensor *sensor;
131 
132 	while ((sensor = TAILQ_FIRST(&sensors)) != NULL) {
133 		TAILQ_REMOVE(&sensors, sensor, link);
134 		free(sensor);
135 	}
136 }
137 
138 static int
139 sysctlname(int *oid, int nlen, char *name, size_t len)
140 {
141 	int mib[12];
142 
143 	if (nlen > (int)(sizeof(mib) / sizeof(int) - 2))
144 		return (-1);
145 
146 	mib[0] = 0;
147 	mib[1] = 1;
148 	memcpy(mib + 2, oid, nlen * sizeof(int));
149 
150 	if (sysctl(mib, nlen + 2, name, &len, 0, 0) == -1)
151 		return (-1);
152 
153 	return (0);
154 }
155 
156 static int
157 sysctlgetnext(int *oid, int nlen, int *next, size_t *nextlen)
158 {
159 	int mib[12];
160 
161 	if (nlen > (int)(sizeof(mib) / sizeof(int) - 2))
162 		return (-1);
163 
164 	mib[0] = 0;
165 	mib[1] = 2;
166 	memcpy(mib + 2, oid, nlen * sizeof(int));
167 
168 	if (sysctl(mib, nlen + 2, next, nextlen, 0, 0) == -1)
169 		return (-1);
170 
171 	return (0);
172 }
173 
174 static int
175 update_sensor_sysctl(char *obuf, size_t *obuflen, int idx, const char *name)
176 {
177 	char buf[LM75BUF];
178 	int mib[5];
179 	size_t len;
180 
181 	/* Fill out the mib information. */
182 	snprintf(buf, sizeof(buf) - 1, "dev.lm75.%d.%s", idx, name);
183 	len = sizeof(mib) / sizeof(int);
184 	if (sysctlnametomib(buf, mib, &len) == -1)
185 		return (-1);
186 
187 	if (len != 4)
188 		return (-1);
189 
190 	/* Read the sysctl data. */
191 	if (sysctl(mib, len, obuf, obuflen, NULL, 0) == -1)
192 		return (-1);
193 
194 	return (0);
195 }
196 
197 static void
198 update_sensor(struct lm75_snmp_sensor *sensor, int idx)
199 {
200 	size_t len;
201 
202 	len = sizeof(sensor->desc);
203 	update_sensor_sysctl(sensor->desc, &len, idx, "%desc");
204 
205 	len = sizeof(sensor->location);
206 	update_sensor_sysctl(sensor->location, &len, idx, "%location");
207 
208 	len = sizeof(sensor->pnpinfo);
209 	update_sensor_sysctl(sensor->pnpinfo, &len, idx, "%pnpinfo");
210 
211 	len = sizeof(sensor->parent);
212 	update_sensor_sysctl(sensor->parent, &len, idx, "%parent");
213 }
214 
215 static int
216 add_sensor(char *buf, size_t nlen)
217 {
218 	int idx, mib[5], temp;
219 	size_t len;
220 	struct lm75_snmp_sensor *sensor;
221 
222 	if (sscanf(buf, "dev.lm75.%d.temperature", &idx) != 1)
223 		return (-1);
224 
225 	/* Fill out the mib information. */
226 	if (sysctlnametomib(buf, mib, &nlen) == -1)
227 		return (-1);
228 
229 	/* Read the sensor temperature. */
230 	len = sizeof(temp);
231 	if (sysctl(mib, nlen, &temp, &len, NULL, 0) == -1)
232 		return (-1);
233 
234 	/* Add the sensor data to the table. */
235 	sensor = calloc(1, sizeof(*sensor));
236 	if (sensor == NULL) {
237 		syslog(LOG_ERR, "Unable to allocate %zu bytes for resource",
238 		    sizeof(*sensor));
239 		return (-1);
240 	}
241 	sensor->index = ++lm75_sensors;
242 	sensor->sysctlidx = idx;
243 	sensor->temp = (temp - TZ_ZEROC) / 10;
244 	TAILQ_INSERT_TAIL(&sensors, sensor, link);
245 
246 	update_sensor(sensor, idx);
247 
248 	return (0);
249 }
250 
251 static int
252 update_sensors(void)
253 {
254 	char buf[LM75BUF];
255 	int i, root[5], *next, *oid;
256 	size_t len, nextlen, rootlen;
257 	static uint64_t now;
258 
259 	now = get_ticks();
260 	if (now - last_sensors_update < UPDATE_INTERVAL)
261 		return (0);
262 
263 	last_sensors_update = now;
264 
265 	/* Reset the sensor data. */
266 	free_sensors();
267 	lm75_sensors = 0;
268 
269 	/* Start from the lm75 default root node. */
270 	rootlen = 2;
271 	if (sysctlnametomib("dev.lm75", root, &rootlen) == -1)
272 		return (0);
273 
274 	oid = (int *)malloc(sizeof(int) * rootlen);
275 	if (oid == NULL) {
276 		perror("malloc");
277 		return (-1);
278 	}
279 	memcpy(oid, root, rootlen * sizeof(int));
280 	len = rootlen;
281 
282 	/* Traverse the sysctl(3) interface and find the active sensors. */
283 	for (;;) {
284 
285 		/* Find the size of the next mib. */
286 		nextlen = 0;
287 		if (sysctlgetnext(oid, len, NULL, &nextlen) == -1) {
288 			free(oid);
289 			return (0);
290 		}
291 		/* Alocate and read the next mib. */
292 		next = (int *)malloc(nextlen);
293 		if (next == NULL) {
294 			syslog(LOG_ERR,
295 			    "Unable to allocate %zu bytes for resource",
296 			    nextlen);
297 			free(oid);
298 			return (-1);
299 		}
300 		if (sysctlgetnext(oid, len, next, &nextlen) == -1) {
301 			free(oid);
302 			free(next);
303 			return (0);
304 		}
305 		free(oid);
306 		/* Check if we care about the next mib. */
307 		for (i = 0; i < (int)rootlen; i++)
308 			if (next[i] != root[i]) {
309 				free(next);
310 				return (0);
311 			}
312 		oid = (int *)malloc(nextlen);
313 		if (oid == NULL) {
314 			syslog(LOG_ERR,
315 			    "Unable to allocate %zu bytes for resource",
316 			    nextlen);
317 			free(next);
318 			return (-1);
319 		}
320 		memcpy(oid, next, nextlen);
321 		free(next);
322 		len = nextlen / sizeof(int);
323 
324 		/* Find the mib name. */
325 		if (sysctlname(oid, len, buf, sizeof(buf)) != 0)
326 			continue;
327 
328 		if (strstr(buf, "temperature"))
329 			if (add_sensor(buf, len) != 0) {
330 				free(oid);
331 				return (-1);
332 			}
333 	}
334 
335 	return (0);
336 }
337 
338 int
339 op_lm75Sensors(struct snmp_context *context __unused, struct snmp_value *value,
340     u_int sub, u_int iidx __unused, enum snmp_op op)
341 {
342 	asn_subid_t which;
343 
344 	if (update_sensors() == -1)
345 		return (SNMP_ERR_RES_UNAVAIL);
346 
347 	which = value->var.subs[sub - 1];
348 
349 	switch (op) {
350 	case SNMP_OP_GET:
351 		switch (which) {
352 		case LEAF_lm75Sensors:
353 			value->v.integer = lm75_sensors;
354 			break;
355 		default:
356 			return (SNMP_ERR_RES_UNAVAIL);
357 		}
358 		break;
359 	case SNMP_OP_SET:
360 		return (SNMP_ERR_NOT_WRITEABLE);
361 	case SNMP_OP_GETNEXT:
362 	case SNMP_OP_ROLLBACK:
363 	case SNMP_OP_COMMIT:
364 		return (SNMP_ERR_NOERROR);
365 	default:
366 		return (SNMP_ERR_RES_UNAVAIL);
367 	}
368 
369 	return (SNMP_ERR_NOERROR);
370 }
371 
372 int
373 op_lm75SensorTable(struct snmp_context *context __unused,
374     struct snmp_value *value, u_int sub, u_int iidx __unused, enum snmp_op op)
375 {
376 	struct lm75_snmp_sensor *sensor;
377 	asn_subid_t which;
378 	int ret;
379 
380 	if (update_sensors() == -1)
381 		return (SNMP_ERR_RES_UNAVAIL);
382 
383 	which = value->var.subs[sub - 1];
384 
385 	switch (op) {
386 	case SNMP_OP_GETNEXT:
387 		sensor = NEXT_OBJECT_INT(&sensors, &value->var, sub);
388 		if (sensor == NULL)
389 			return (SNMP_ERR_NOSUCHNAME);
390 		value->var.len = sub + 1;
391 		value->var.subs[sub] = sensor->index;
392 		break;
393 	case SNMP_OP_GET:
394 		if (value->var.len - sub != 1)
395 			return (SNMP_ERR_NOSUCHNAME);
396 		sensor = FIND_OBJECT_INT(&sensors, &value->var, sub);
397 		if (sensor == NULL)
398 			return (SNMP_ERR_NOSUCHNAME);
399 		break;
400 	case SNMP_OP_SET:
401 		return (SNMP_ERR_NOT_WRITEABLE);
402 	case SNMP_OP_ROLLBACK:
403 	case SNMP_OP_COMMIT:
404 		return (SNMP_ERR_NOERROR);
405 	default:
406 		return (SNMP_ERR_RES_UNAVAIL);
407 	}
408 
409 	ret = SNMP_ERR_NOERROR;
410 
411 	switch (which) {
412 	case LEAF_lm75SensorIndex:
413 		value->v.integer = sensor->index;
414 		break;
415 	case LEAF_lm75SensorSysctlIndex:
416 		value->v.integer = sensor->sysctlidx;
417 		break;
418 	case LEAF_lm75SensorDesc:
419 		ret = string_get(value, sensor->desc, -1);
420 		break;
421 	case LEAF_lm75SensorLocation:
422 		ret = string_get(value, sensor->location, -1);
423 		break;
424 	case LEAF_lm75SensorPnpInfo:
425 		ret = string_get(value, sensor->pnpinfo, -1);
426 		break;
427 	case LEAF_lm75SensorParent:
428 		ret = string_get(value, sensor->parent, -1);
429 		break;
430 	case LEAF_lm75SensorTemperature:
431 		value->v.integer = sensor->temp;
432 		break;
433 	default:
434 		ret = SNMP_ERR_RES_UNAVAIL;
435 		break;
436 	}
437 
438 	return (ret);
439 }
440