2 * Copyright (c) 2005-2006 The FreeBSD Project
5 * Author: Victor Cruceru <soc-victor@freebsd.org>
7 * Redistribution of this software and documentation and use in source and
8 * binary forms, with or without modification, are permitted provided that
9 * the following conditions are met:
11 * 1. Redistributions of source code or documentation must retain the above
12 * copyright notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Host Resources MIB implementation for SNMPd: instrumentation for
37 #include <sys/param.h>
49 #include "hostres_snmp.h"
50 #include "hostres_oid.h"
51 #include "hostres_tree.h"
53 #include <sys/dirent.h>
57 static const struct asn_oid OIDX_hrDevicePrinter_c = OIDX_hrDevicePrinter;
68 * This structure is used to hold a SNMP table entry
69 * for HOST-RESOURCES-MIB's hrPrinterTable.
71 struct printer_entry {
73 int32_t status; /* values from PrinterStatus enum above */
74 u_char detectedErrorState[2];
75 TAILQ_ENTRY(printer_entry) link;
76 #define HR_PRINTER_FOUND 0x001
80 TAILQ_HEAD(printer_tbl, printer_entry);
82 /* the hrPrinterTable */
83 static struct printer_tbl printer_tbl = TAILQ_HEAD_INITIALIZER(printer_tbl);
85 /* last (agent) tick when hrPrinterTable was updated */
86 static uint64_t printer_tick;
89 * Create entry into the printer table.
91 static struct printer_entry *
92 printer_entry_create(const struct device_entry *devEntry)
94 struct printer_entry *entry = NULL;
96 assert(devEntry != NULL);
100 if ((entry = malloc(sizeof(*entry))) == NULL) {
101 syslog(LOG_WARNING, "hrPrinterTable: %s: %m", __func__);
104 memset(entry, 0, sizeof(*entry));
105 entry->index = devEntry->index;
106 INSERT_OBJECT_INT(entry, &printer_tbl);
111 * Delete entry from the printer table.
114 printer_entry_delete(struct printer_entry *entry)
117 assert(entry != NULL);
121 TAILQ_REMOVE(&printer_tbl, entry, link);
126 * Find a printer by its index
128 static struct printer_entry *
129 printer_find_by_index(int32_t idx)
131 struct printer_entry *entry;
133 TAILQ_FOREACH(entry, &printer_tbl, link)
134 if (entry->index == idx)
141 * Get the status of a printer
143 static enum PrinterStatus
144 get_printer_status(const struct printer *pp)
146 char statfile[MAXPATHLEN];
147 char lockfile[MAXPATHLEN];
151 enum PrinterStatus ps = PS_UNKNOWN;
153 if (pp->lock_file[0] == '/')
154 strlcpy(lockfile, pp->lock_file, sizeof(lockfile));
156 snprintf(lockfile, sizeof(lockfile), "%s/%s",
157 pp->spool_dir, pp->lock_file);
159 fd = open(lockfile, O_RDONLY);
160 if (fd < 0 || flock(fd, LOCK_SH | LOCK_NB) == 0) {
165 if (pp->status_file[0] == '/')
166 strlcpy(statfile, pp->status_file, sizeof(statfile));
168 snprintf(statfile, sizeof(statfile), "%s/%s",
169 pp->spool_dir, pp->status_file);
171 f = fopen(statfile, "r");
173 syslog(LOG_ERR, "cannot open status file: %s", strerror(errno));
178 memset(&fline[0], '\0', sizeof(line));
179 if (fgets(fline, sizeof(fline) -1, f) == NULL) {
184 if (strstr(fline, "is ready and printing") != NULL) {
189 if (strstr(fline, "to become ready (offline?)") != NULL) {
196 (void)close(fd); /* unlocks as well */
205 * Called for each printer found in /etc/printcap.
208 handle_printer(struct printer *pp)
210 struct device_entry *dev_entry;
211 struct printer_entry *printer_entry;
215 if (pp->remote_host != NULL) {
216 HRDBG("skipped %s -- remote", pp->printer);
220 if (strncmp(pp->lp, _PATH_DEV, strlen(_PATH_DEV)) != 0) {
221 HRDBG("skipped %s [device %s] -- remote", pp->printer, pp->lp);
225 memset(dev_only, '\0', sizeof(dev_only));
226 snprintf(dev_only, sizeof(dev_only), "%s", pp->lp + strlen(_PATH_DEV));
228 HRDBG("printer %s has device %s", pp->printer, dev_only);
230 if (stat(pp->lp, &sb) < 0) {
231 if (errno == ENOENT) {
232 HRDBG("skipped %s -- device %s missing",
233 pp->printer, pp->lp);
238 if ((dev_entry = device_find_by_name(dev_only)) == NULL) {
239 HRDBG("%s not in hrDeviceTable", pp->lp);
242 HRDBG("%s found in hrDeviceTable", pp->lp);
243 dev_entry->type = &OIDX_hrDevicePrinter_c;
245 dev_entry->flags |= HR_DEVICE_IMMUTABLE;
247 /* Then check hrPrinterTable for this device */
248 if ((printer_entry = printer_find_by_index(dev_entry->index)) == NULL &&
249 (printer_entry = printer_entry_create(dev_entry)) == NULL)
252 printer_entry->flags |= HR_PRINTER_FOUND;
253 printer_entry->status = get_printer_status(pp);
254 memset(printer_entry->detectedErrorState, 0,
255 sizeof(printer_entry->detectedErrorState));
259 hrPrinter_get_OS_entries(void)
262 struct printer myprinter, *pp = &myprinter;
265 HRDBG("---->Getting printers .....");
266 more = firstprinter(pp, &status);
272 HRDBG("---->Got printer %s", pp->printer);
275 more = nextprinter(pp, &status);
279 "hrPrinterTable: printcap entry for %s "
280 "has errors, skipping",
281 pp->printer ? pp->printer : "<noname?>");
282 } while (more && status);
286 printer_tick = this_tick;
290 * Init the things for hrPrinterTable
293 init_printer_tbl(void)
296 hrPrinter_get_OS_entries();
300 * Finalization routine for hrPrinterTable
301 * It destroys the lists and frees any allocated heap memory
304 fini_printer_tbl(void)
306 struct printer_entry *n1;
308 while ((n1 = TAILQ_FIRST(&printer_tbl)) != NULL) {
309 TAILQ_REMOVE(&printer_tbl, n1, link);
315 * Refresh the printer table if needed.
318 refresh_printer_tbl(void)
320 struct printer_entry *entry;
321 struct printer_entry *entry_tmp;
323 if (this_tick <= printer_tick) {
324 HRDBG("no refresh needed");
328 /* mark each entry as missing */
329 TAILQ_FOREACH(entry, &printer_tbl, link)
330 entry->flags &= ~HR_PRINTER_FOUND;
332 hrPrinter_get_OS_entries();
335 * Purge items that disappeared
337 entry = TAILQ_FIRST(&printer_tbl);
338 while (entry != NULL) {
339 entry_tmp = TAILQ_NEXT(entry, link);
340 if (!(entry->flags & HR_PRINTER_FOUND))
341 printer_entry_delete(entry);
345 printer_tick = this_tick;
347 HRDBG("refresh DONE ");
351 op_hrPrinterTable(struct snmp_context *ctx __unused, struct snmp_value *value,
352 u_int sub, u_int iidx __unused, enum snmp_op curr_op)
354 struct printer_entry *entry;
356 refresh_printer_tbl();
360 case SNMP_OP_GETNEXT:
361 if ((entry = NEXT_OBJECT_INT(&printer_tbl, &value->var,
363 return (SNMP_ERR_NOSUCHNAME);
364 value->var.len = sub + 1;
365 value->var.subs[sub] = entry->index;
369 if ((entry = FIND_OBJECT_INT(&printer_tbl, &value->var,
371 return (SNMP_ERR_NOSUCHNAME);
375 if ((entry = FIND_OBJECT_INT(&printer_tbl, &value->var,
377 return (SNMP_ERR_NO_CREATION);
378 return (SNMP_ERR_NOT_WRITEABLE);
380 case SNMP_OP_ROLLBACK:
387 switch (value->var.subs[sub - 1]) {
389 case LEAF_hrPrinterStatus:
390 value->v.integer = entry->status;
391 return (SNMP_ERR_NOERROR);
393 case LEAF_hrPrinterDetectedErrorState:
394 return (string_get(value, entry->detectedErrorState,
395 sizeof(entry->detectedErrorState)));