summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_device_tbl.c
blob: 1741502b098d3ed375fb0c5670efd2cc5cc2feaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
 /*-
 * Copyright (c) 2005-2006 The FreeBSD Project
 * All rights reserved.
 *
 * Author: Victor Cruceru <soc-victor@freebsd.org>
 *
 * Redistribution of this software and documentation and use in source and
 * binary forms, with or without modification, are permitted provided that
 * the following conditions are met:
 *
 * 1. Redistributions of source code or documentation must retain the above
 *    copyright notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

/*
 * Host Resources MIB: hrDeviceTable implementation for SNMPd.
 */

#include <sys/un.h>
#include <sys/limits.h>

#include <assert.h>
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <sysexits.h>

#include "hostres_snmp.h"
#include "hostres_oid.h"
#include "hostres_tree.h"

#define FREE_DEV_STRUCT(entry_p) do {		\
	free(entry_p->name);			\
	free(entry_p->location);		\
	free(entry_p->descr);			\
	free(entry_p);				\
} while (0)

/*
 * Status of a device
 */
enum DeviceStatus {
	DS_UNKNOWN	= 1,
	DS_RUNNING	= 2,
	DS_WARNING	= 3,
	DS_TESTING	= 4,
	DS_DOWN		= 5
};

TAILQ_HEAD(device_tbl, device_entry);

/* the head of the list with hrDeviceTable's entries */
static struct device_tbl device_tbl = TAILQ_HEAD_INITIALIZER(device_tbl);

/* Table used for consistent device table indexing. */
struct device_map device_map = STAILQ_HEAD_INITIALIZER(device_map);

/* next int available for indexing the hrDeviceTable */
static uint32_t next_device_index = 1;

/* last (agent) tick when hrDeviceTable was updated */
static uint64_t device_tick = 0;

/* maximum number of ticks between updates of device table */
uint32_t device_tbl_refresh = 10 * 100;

/* socket for /var/run/devd.pipe */
static int devd_sock = -1;

/* used to wait notifications from /var/run/devd.pipe */
static void *devd_fd;

/* some constants */
static const struct asn_oid OIDX_hrDeviceProcessor_c = OIDX_hrDeviceProcessor;
static const struct asn_oid OIDX_hrDeviceOther_c = OIDX_hrDeviceOther;

/**
 * Create a new entry out of thin air.
 */
struct device_entry *
device_entry_create(const char *name, const char *location, const char *descr)
{
	struct device_entry *entry = NULL;
	struct device_map_entry *map = NULL;
	size_t name_len;
	size_t location_len;

	assert((name[0] != 0) || (location[0] != 0));

	if (name[0] == 0 && location[0] == 0)
		return (NULL);

	STAILQ_FOREACH(map, &device_map, link) {
		assert(map->name_key != NULL);
		assert(map->location_key != NULL);

		if (strcmp(map->name_key, name) == 0 &&
		    strcmp(map->location_key, location) == 0) {
			break;
		}
	}

	if (map == NULL) {
		/* new object - get a new index */
		if (next_device_index > INT_MAX) {
		        syslog(LOG_ERR,
			    "%s: hrDeviceTable index wrap", __func__);
			/* There isn't much we can do here.
			 * If the next_swins_index is consumed
			 * then we can't add entries to this table
			 * So it is better to exit - if the table is sparsed
			 * at the next agent run we can fill it fully.
			 */
			errx(EX_SOFTWARE, "hrDeviceTable index wrap");
			/* not reachable */
		}

		if ((map = malloc(sizeof(*map))) == NULL) {
			syslog(LOG_ERR, "hrDeviceTable: %s: %m", __func__ );
			return (NULL);
		}

		map->entry_p = NULL;

		name_len = strlen(name) + 1;
		if (name_len > DEV_NAME_MLEN)
			name_len = DEV_NAME_MLEN;

		if ((map->name_key = malloc(name_len)) == NULL) {
			syslog(LOG_ERR, "hrDeviceTable: %s: %m", __func__ );
			free(map);
			return (NULL);
		}

		location_len = strlen(location) + 1;
		if (location_len > DEV_LOC_MLEN)
			location_len = DEV_LOC_MLEN;

		if ((map->location_key = malloc(location_len )) == NULL) {
			syslog(LOG_ERR, "hrDeviceTable: %s: %m", __func__ );
			free(map->name_key);
			free(map);
			return (NULL);
		}

		map->hrIndex = next_device_index++;

		strlcpy(map->name_key, name, name_len);
		strlcpy(map->location_key, location, location_len);

		STAILQ_INSERT_TAIL(&device_map, map, link);
		HRDBG("%s at %s added into hrDeviceMap at index=%d",
		    name, location, map->hrIndex);
	} else {
		HRDBG("%s at %s exists in hrDeviceMap index=%d",
		    name, location, map->hrIndex);
	}

	if ((entry = malloc(sizeof(*entry))) == NULL) {
		syslog(LOG_WARNING, "hrDeviceTable: %s: %m", __func__);
		return (NULL);
	}
	memset(entry, 0, sizeof(*entry));

	entry->index = map->hrIndex;
	map->entry_p = entry;

	if ((entry->name = strdup(map->name_key)) == NULL) {
		syslog(LOG_ERR, "hrDeviceTable: %s: %m", __func__ );
		free(entry);
		return (NULL);
	}

	if ((entry->location = strdup(map->location_key)) == NULL) {
		syslog(LOG_ERR, "hrDeviceTable: %s: %m", __func__ );
		free(entry->name);
		free(entry);
		return (NULL);
	}

	/*
	 * From here till the end of this function we reuse name_len
	 * for a different purpose - for device_entry::descr
	 */
	if (name[0] != '\0')
		name_len = strlen(name) + strlen(descr) +
		    strlen(": ") + 1;
	else
		name_len = strlen(location) + strlen(descr) +
		    strlen("unknown at : ") + 1;

	if (name_len > DEV_DESCR_MLEN)
		name_len = DEV_DESCR_MLEN;

	if ((entry->descr = malloc(name_len )) == NULL) {
		syslog(LOG_ERR, "hrDeviceTable: %s: %m", __func__ );
		free(entry->name);
		free(entry->location);
		free(entry);
		return (NULL);
	}

	memset(&entry->descr[0], '\0', name_len);

	if (name[0] != '\0')
		snprintf(entry->descr, name_len,
		    "%s: %s", name, descr);
	else
		snprintf(entry->descr, name_len,
		    "unknown at %s: %s", location, descr);

	entry->id = &oid_zeroDotZero;	/* unknown id - FIXME */
	entry->status = (u_int)DS_UNKNOWN;
	entry->errors = 0;
	entry->type = &OIDX_hrDeviceOther_c;

	INSERT_OBJECT_INT(entry, &device_tbl);

	return (entry);
}

/**
 * Create a new entry into the device table.
 */
static struct device_entry *
device_entry_create_devinfo(const struct devinfo_dev *dev_p)
{

	assert(dev_p->dd_name != NULL);
	assert(dev_p->dd_location != NULL);

	return (device_entry_create(dev_p->dd_name, dev_p->dd_location,
	    dev_p->dd_desc));
}

/**
 * Delete an entry from the device table.
 */
void
device_entry_delete(struct device_entry *entry)
{
	struct device_map_entry *map;

	assert(entry != NULL);

	TAILQ_REMOVE(&device_tbl, entry, link);

	STAILQ_FOREACH(map, &device_map, link)
		if (map->entry_p == entry) {
			map->entry_p = NULL;
			break;
		}

	FREE_DEV_STRUCT(entry);
}

/**
 * Find an entry given its name and location
 */
static struct device_entry *
device_find_by_dev(const struct devinfo_dev *dev_p)
{
	struct device_map_entry  *map;

	assert(dev_p != NULL);

	STAILQ_FOREACH(map, &device_map, link)
		if (strcmp(map->name_key, dev_p->dd_name) == 0 &&
		    strcmp(map->location_key, dev_p->dd_location) == 0)
		    	return (map->entry_p);
	return (NULL);
}

/**
 * Find an entry given its index.
 */
struct device_entry *
device_find_by_index(int32_t idx)
{
	struct device_entry *entry;

	TAILQ_FOREACH(entry, &device_tbl, link)
		if (entry->index == idx)
			return (entry);
	return (NULL);
}

/**
 * Find an device entry given its name.
 */
struct device_entry *
device_find_by_name(const char *dev_name)
{
	struct device_map_entry *map;

	assert(dev_name != NULL);

	STAILQ_FOREACH(map, &device_map, link)
		if (strcmp(map->name_key, dev_name) == 0)
			return (map->entry_p);

	return (NULL);
}

/**
 * Find out the type of device. CPU only currently.
 */
static void
device_get_type(struct devinfo_dev *dev_p, const struct asn_oid **out_type_p)
{

	assert(dev_p != NULL);
	assert(out_type_p != NULL);

	if (dev_p == NULL)
		return;

	if (strncmp(dev_p->dd_name, "cpu", strlen("cpu")) == 0 &&
	    strstr(dev_p->dd_location, ".CPU") != NULL) {
		*out_type_p = &OIDX_hrDeviceProcessor_c;
		return;
	}
}

/**
 * Get the status of a device
 */
static enum DeviceStatus
device_get_status(struct devinfo_dev *dev)
{

	assert(dev != NULL);

	switch (dev->dd_state) {
	case DS_ALIVE:			/* probe succeeded */
	case DS_NOTPRESENT:		/* not probed or probe failed */
		return (DS_DOWN);
	case DS_ATTACHED:		/* attach method called */
	case DS_BUSY:			/* device is open */
		return (DS_RUNNING);
	default:
		return (DS_UNKNOWN);
	}
}

/**
 * Get the info for the given device and then recursively process all
 * child devices.
 */
static int
device_collector(struct devinfo_dev *dev, void *arg)
{
	struct device_entry *entry;

	HRDBG("%llu/%llu name='%s' desc='%s' drivername='%s' location='%s'",
	    (unsigned long long)dev->dd_handle,
	    (unsigned long long)dev->dd_parent, dev->dd_name, dev->dd_desc,
	    dev->dd_drivername, dev->dd_location);

	if (dev->dd_name[0] != '\0' || dev->dd_location[0] != '\0') {
		HRDBG("ANALYZING dev %s at %s",
		    dev->dd_name, dev->dd_location);

		if ((entry = device_find_by_dev(dev)) != NULL) {
			entry->flags |= HR_DEVICE_FOUND;
			entry->status = (u_int)device_get_status(dev);
		} else if ((entry = device_entry_create_devinfo(dev)) != NULL) {
			device_get_type(dev, &entry->type);

			entry->flags |= HR_DEVICE_FOUND;
			entry->status = (u_int)device_get_status(dev);
		}
	} else {
		HRDBG("SKIPPED unknown device at location '%s'",
		    dev->dd_location );
	}

	return (devinfo_foreach_device_child(dev, device_collector, arg));
}

/**
 * Create the socket to the device daemon.
 */
static int
create_devd_socket(void)
{
	int d_sock;
 	struct sockaddr_un devd_addr;

 	bzero(&devd_addr, sizeof(struct sockaddr_un));

 	if ((d_sock = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
 		syslog(LOG_ERR, "Failed to create the socket for %s: %m",
		    PATH_DEVD_PIPE);
 		return (-1);
 	}

 	devd_addr.sun_family = PF_LOCAL;
	devd_addr.sun_len = sizeof(devd_addr);
 	strlcpy(devd_addr.sun_path, PATH_DEVD_PIPE,
	    sizeof(devd_addr.sun_path) - 1);

 	if (connect(d_sock, (struct sockaddr *)&devd_addr,
	    sizeof(devd_addr)) == -1) {
 		syslog(LOG_ERR,"Failed to connect socket for %s: %m",
		    PATH_DEVD_PIPE);
 		if (close(d_sock) < 0 )
 			syslog(LOG_ERR,"Failed to close socket for %s: %m",
			    PATH_DEVD_PIPE);
		return (-1);
 	}

 	return (d_sock);
}

/*
 * Event on the devd socket.
 *
 * We should probably directly process entries here. For simplicity just
 * call the refresh routine with the force flag for now.
 */
static void
devd_socket_callback(int fd, void *arg __unused)
{
	char buf[512];
	int read_len = -1;

	assert(fd == devd_sock);

	HRDBG("called");

again:
	read_len = read(fd, buf, sizeof(buf));
	if (read_len < 0) {
		if (errno == EBADF) {
			devd_sock = -1;
			if (devd_fd != NULL) {
				fd_deselect(devd_fd);
				devd_fd = NULL;
			}
			syslog(LOG_ERR, "Closing devd_fd, revert to "
			    "devinfo polling");
		}

	} else if (read_len == 0) {
		syslog(LOG_ERR, "zero bytes read from devd pipe... "
		    "closing socket!");

		if (close(devd_sock) < 0 )
 			syslog(LOG_ERR, "Failed to close devd socket: %m");

		devd_sock = -1;
		if (devd_fd != NULL) {
			fd_deselect(devd_fd);
			devd_fd = NULL;
		}
		syslog(LOG_ERR, "Closing devd_fd, revert to devinfo polling");

	} else {
		if (read_len == sizeof(buf))
			goto again;
		refresh_device_tbl(1);
	}
}

/**
 * Initialize and populate the device table.
 */
void
init_device_tbl(void)
{

	/* initially populate table for the other tables */
	refresh_device_tbl(1);

	/* no problem if that fails - just use polling mode */
	devd_sock = create_devd_socket();
}

/**
 * Start devd(8) monitoring.
 */
void
start_device_tbl(struct lmodule *mod)
{

	if (devd_sock > 0) {
		devd_fd = fd_select(devd_sock, devd_socket_callback, NULL, mod);
		if (devd_fd == NULL)
			syslog(LOG_ERR, "fd_select failed on devd socket: %m");
	}
}

/**
 * Finalization routine for hrDeviceTable
 * It destroys the lists and frees any allocated heap memory
 */
void
fini_device_tbl(void)
{
	struct device_map_entry *n1;

	if (devd_fd != NULL)
		fd_deselect(devd_fd);

	if (devd_sock != -1)
		(void)close(devd_sock);

	devinfo_free();

     	while ((n1 = STAILQ_FIRST(&device_map)) != NULL) {
		STAILQ_REMOVE_HEAD(&device_map, link);
		if (n1->entry_p != NULL) {
			TAILQ_REMOVE(&device_tbl, n1->entry_p, link);
			FREE_DEV_STRUCT(n1->entry_p);
		}
		free(n1->name_key);
		free(n1->location_key);
		free(n1);
     	}
	assert(TAILQ_EMPTY(&device_tbl));
}

/**
 * Refresh routine for hrDeviceTable. We don't refresh here if the devd socket
 * is open, because in this case we have the actual information always. We
 * also don't refresh when the table is new enough (if we don't have a devd
 * socket). In either case a refresh can be forced by passing a non-zero value.
 */
void
refresh_device_tbl(int force)
{
	struct device_entry *entry, *entry_tmp;
	struct devinfo_dev *dev_root;
	static int act = 0;

	if (!force && (devd_sock >= 0 ||
	   (device_tick != 0 && this_tick - device_tick < device_tbl_refresh))){
		HRDBG("no refresh needed");
		return;
	}

	if (act) {
		syslog(LOG_ERR, "%s: recursive call", __func__);
		return;
	}

	if (devinfo_init() != 0) {
		syslog(LOG_ERR,"%s: devinfo_init failed: %m", __func__);
		return;
	}

	act = 1;
	if ((dev_root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL){
		syslog(LOG_ERR, "%s: can't get the root device: %m", __func__);
		goto out;
	}

	/* mark each entry as missing */
	TAILQ_FOREACH(entry, &device_tbl, link)
		entry->flags &= ~HR_DEVICE_FOUND;

	if (devinfo_foreach_device_child(dev_root, device_collector, NULL))
		syslog(LOG_ERR, "%s: devinfo_foreach_device_child failed",
		    __func__);

	/*
	 * Purge items that disappeared
	 */
	TAILQ_FOREACH_SAFE(entry, &device_tbl, link, entry_tmp) {
		/*
		 * If HR_DEVICE_IMMUTABLE bit is set then this means that
		 * this entry was not detected by the above
		 * devinfo_foreach_device() call. So we are not deleting
		 * it there.
		 */
		if (!(entry->flags & HR_DEVICE_FOUND) &&
		    !(entry->flags & HR_DEVICE_IMMUTABLE))
			device_entry_delete(entry);
	}

	device_tick = this_tick;

	/*
	 * Force a refresh for the hrDiskStorageTable
	 * XXX Why not the other dependen tables?
	 */
	refresh_disk_storage_tbl(1);

  out:
	devinfo_free();
	act = 0;
}

/**
 * This is the implementation for a generated (by a SNMP tool)
 * function prototype, see hostres_tree.h
 * It handles the SNMP operations for hrDeviceTable
 */
int
op_hrDeviceTable(struct snmp_context *ctx __unused, struct snmp_value *value,
    u_int sub, u_int iidx __unused, enum snmp_op curr_op)
{
	struct device_entry *entry;

	refresh_device_tbl(0);

	switch (curr_op) {

	case SNMP_OP_GETNEXT:
		if ((entry = NEXT_OBJECT_INT(&device_tbl,
		    &value->var, sub)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		value->var.len = sub + 1;
		value->var.subs[sub] = entry->index;
		goto get;

	case SNMP_OP_GET:
		if ((entry = FIND_OBJECT_INT(&device_tbl,
		    &value->var, sub)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		goto get;

	case SNMP_OP_SET:
		if ((entry = FIND_OBJECT_INT(&device_tbl,
		    &value->var, sub)) == NULL)
			return (SNMP_ERR_NO_CREATION);
		return (SNMP_ERR_NOT_WRITEABLE);

	case SNMP_OP_ROLLBACK:
	case SNMP_OP_COMMIT:
		abort();
	}
	abort();

  get:
	switch (value->var.subs[sub - 1]) {

	case LEAF_hrDeviceIndex:
		value->v.integer = entry->index;
		return (SNMP_ERR_NOERROR);

	case LEAF_hrDeviceType:
		assert(entry->type != NULL);
	  	value->v.oid = *(entry->type);
	  	return (SNMP_ERR_NOERROR);

	case LEAF_hrDeviceDescr:
	  	return (string_get(value, entry->descr, -1));

	case LEAF_hrDeviceID:
		value->v.oid = *(entry->id);
	  	return (SNMP_ERR_NOERROR);

	case LEAF_hrDeviceStatus:
	  	value->v.integer = entry->status;
	  	return (SNMP_ERR_NOERROR);

	case LEAF_hrDeviceErrors:
	  	value->v.uint32 = entry->errors;
	  	return (SNMP_ERR_NOERROR);
	}
	abort();
}
OpenPOWER on IntegriCloud