PERFORCE change 82389 for review
Victor Cruceru
soc-victor at FreeBSD.org
Sun Aug 21 21:10:28 GMT 2005
http://perforce.freebsd.org/chv.cgi?CH=82389
Change 82389 by soc-victor at soc-victor_82.76.158.176 on 2005/08/21 21:09:43
Finished the SNMP instrumentation for hrPrinterTable.
Tested using only one parallel printer.
Affected files ...
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/Makefile#23 edit
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_diskstorage_tbl.c#9 edit
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_partition_tbl.c#4 edit
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_printer_tbl.c#3 edit
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.c#21 edit
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h#26 edit
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_storage_tbl.c#9 edit
Differences ...
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/Makefile#23 (text+ko) ====
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_diskstorage_tbl.c#9 (text+ko) ====
@@ -47,6 +47,7 @@
#include <errno.h>
#include <sys/disk.h>
#include <sys/mdioctl.h>
+#include <paths.h>
extern
struct hrDeviceTblEntry *
@@ -169,7 +170,7 @@
strncpy(map->name_key, dev_name, sizeof(map->name_key) - 1);
memset(&map->location_key[0], 0, sizeof(map->location_key));
- snprintf(map->location_key, sizeof(map->location_key) - 1, "/dev/%s", dev_name);
+ snprintf(map->location_key, sizeof(map->location_key) - 1, "%s%s", _PATH_DEV, dev_name);
map->entry_p = entry;
@@ -189,7 +190,7 @@
strncpy(entry->name, dev_name, sizeof(entry->name) - 1);
memset(&entry->location[0], 0, sizeof(entry->location));
- snprintf(entry->location, sizeof(entry->location) - 1, "/dev/%s", dev_name);
+ snprintf(entry->location, sizeof(entry->location) - 1, "%s%s", _PATH_DEV, dev_name);
INSERT_OBJECT_INT(entry, &hrState_g.hr_device_tbl);
@@ -223,7 +224,7 @@
if( entry->dev_name[0] == '\0' ) {
return;
}
- snprintf(dev_path, sizeof(dev_path) - 1, "/dev/%s", entry->dev_name);
+ snprintf(dev_path, sizeof(dev_path) - 1, "%s%s", _PATH_DEV, entry->dev_name);
entry->capacity = 0;
HR_DPRINTF((stderr,"OPENING device %s\n",dev_path));
fd = open(dev_path, O_RDONLY|O_NONBLOCK);
@@ -624,7 +625,7 @@
disk = strsep(&hrState_g.disk_list, " ");
if (disk == NULL)
break;
- snprintf(disk_device, sizeof(disk_device) - 1, "/dev/%s", disk);
+ snprintf(disk_device, sizeof(disk_device) - 1, "%s%s", _PATH_DEV, disk);
/*
First check if the disk is in the hrDeviceTable.
If not,add it.
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_partition_tbl.c#4 (text+ko) ====
@@ -39,6 +39,7 @@
#include <stdarg.h>
#include <string.h>
#include <err.h>
+#include <paths.h>
/*just some prototypes below*/
void
@@ -183,7 +184,7 @@
}
assert(entry != NULL);
entry->flags |= HR_PARTITION_FOUND;
- snprintf(entry->label, sizeof(entry->label) - 1, "/dev/%s", chunk_p->name );
+ snprintf(entry->label, sizeof(entry->label) - 1, "%s%s", _PATH_DEV, chunk_p->name );
daddr_t k_size= chunk_p->size/2;
/*may overflow the SNMP type*/
entry->size = ( k_size > (daddr_t)INT_MAX ? INT_MAX : k_size );
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_printer_tbl.c#3 (text+ko) ====
@@ -80,6 +80,7 @@
#include <syslog.h>
#include <err.h>
+
#define _PATH_PRINTCAP "/etc/printcap"
#define _PATH_DEFDEVLP "/dev/lp"
#define _PATH_DEFSPOOL "/var/spool/output/lpd"
@@ -143,20 +144,56 @@
const char *lng, const char *_dflt, char **_result);
static int getprintcap_int(char *_bp, struct printer *_pp);
-/*static*/int getprintcap(char *printer, struct printer *pp);
-
static void init_printer(struct printer *pp);
-/*static*/int firstprinter(struct printer *pp, int *error);
+static int firstprinter(struct printer *pp, int *error);
-/*static*/int nextprinter(struct printer *pp, int *error);
+static int nextprinter(struct printer *pp, int *error);
static void free_printer(struct printer *pp);
-/*static*/ void lastprinter(void);
+static void lastprinter(void);
const char *pcaperr(int error);
+static
+char *
+status_file_name(const struct printer *pp, char *buf, size_t len)
+{
+ static char staticbuf[MAXPATHLEN];
+
+ if (buf == 0)
+ buf = staticbuf;
+ if (len == 0)
+ len = MAXPATHLEN;
+
+ if (pp->status_file[0] == '/')
+ strlcpy(buf, pp->status_file, len);
+ else
+ snprintf(buf, len, "%s/%s", pp->spool_dir, pp->status_file);
+
+ return buf;
+}
+
+static
+char *
+lock_file_name(const struct printer *pp, char *buf, size_t len)
+{
+ static char staticbuf[MAXPATHLEN];
+
+ if (buf == 0)
+ buf = staticbuf;
+ if (len == 0)
+ len = MAXPATHLEN;
+
+ if (pp->lock_file[0] == '/')
+ strlcpy(buf, pp->lock_file, len);
+ else
+ snprintf(buf, len, "%s/%s", pp->spool_dir, pp->lock_file);
+
+ return buf;
+}
+
extern
struct hrDeviceTblEntry*
hrDeviceTblEntry_find_by_name(const char *dev_name);
@@ -173,43 +210,7 @@
hrDeviceTblEntry_delete_v( struct hrDeviceTblEntry* entry );
-/*
- * Read the printcap database for printer `printer' into the
- * struct printer pointed by `pp'. Return values are as for
- * cgetent(3): -1 means we could not find what we wanted, -2
- * means a system error occurred (and errno is set), -3 if a
- * reference (`tc=') loop was detected, and 0 means success.
- *
- * Copied from lpr; should add additional capabilities as they
- * are required by the other programs in the suite so that
- * printcap-reading is consistent across the entire family.
- */
-int
-getprintcap(char *printer, struct printer *pp)
-{
- int status;
- char *bp = NULL;
- if (hrState_g._printcapdb == NULL) {
- assert(0);
- return (PCAPERR_OSERR);
- }
-
- /*
- * A bug in the declaration of cgetent(3) means that we have
- * to hide the constness of its third argument.
- int
- cgetent(char **buf, char **db_array, char *name);
-
- */
-
- if ((status = cgetent(&bp, hrState_g._printcapdb, (char*)printer)) < 0)
- return status;
- status = getprintcap_int(bp, pp);
- free(bp);
- return status;
-}
-
/*
* Map the status values returned by cgetfirst/cgetnext into those
* used by cgetent, returning truth if there are more records to
@@ -554,7 +555,7 @@
hrDeviceTblEntry *entry;
struct
deviceNameMapEntry *map = NULL;
-
+
assert(dev_name != NULL);
@@ -573,12 +574,14 @@
memset(entry, 0, sizeof(*entry));
- STAILQ_FOREACH(map, &hrState_g.device_name_map, link)
+
+ STAILQ_FOREACH(map, &hrState_g.device_name_map, link) {
if (strcmp(map->name_key, dev_name) == 0) {
entry->index = map->hrIndex;
map->entry_p = entry;
break;
}
+ }
if (map == NULL) {
/* new object - get a new index */
@@ -622,15 +625,73 @@
INSERT_OBJECT_INT(entry, &hrState_g.hr_device_tbl);
- return entry;
+ return (entry);
}
static
+enum PrinterStatus
+getPrinterSatus(const struct printer *pp) {
+ char statfile[MAXPATHLEN];
+ char lockfile[MAXPATHLEN];
+
+ char line[128];
+ int fd = -1;
+ FILE* f = NULL;
+ enum PrinterStatus ps = PS_UNKNOWN;
+
+ lock_file_name(pp, lockfile, sizeof lockfile);
+ fd = open(lockfile, O_RDONLY);
+ if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) {
+ (void) close(fd); /* unlocks as well */
+ fd = -1;
+ ps = PS_IDLE;
+ goto LABEL_DONE;
+ }
+
+
+ status_file_name(pp, statfile, sizeof(statfile));
+ f = fopen(statfile, "r");
+ if (f == NULL) {
+ syslog(LOG_ERR, "cannot open status file: %s\n", strerror(errno));
+ ps = PS_UNKNOWN;
+ goto LABEL_DONE;
+ }
+
+
+
+ memset(&line[0], '\0', sizeof(line));
+ if (fgets(line, sizeof(line) -1, f) == NULL) {
+ ps = PS_UNKNOWN;
+ goto LABEL_DONE;
+ }
+ if (strstr(line, "is ready and printing") != NULL) {
+ ps = PS_PRINTING;
+ goto LABEL_DONE;
+ }
+ if (strstr(line, "to become ready (offline?)") != NULL) {
+ ps = PS_OTHER;
+ goto LABEL_DONE;
+ }
+
+
+LABEL_DONE:
+ if (fd > 0) {
+ (void) close(fd); /* unlocks as well */
+ }
+ if (f != NULL) {
+ fclose(f);
+ }
+
+ return (ps);
+
+}
+static
void
handle_printer(struct printer *pp) {
struct hrDeviceTblEntry* dev_entry = NULL;
struct hrPrinterTblEntry* printer_entry = NULL;
+ char dev_only[128];
if (pp->rp_matches_local != 1) {
HR_DPRINTF((stderr,
@@ -639,13 +700,36 @@
return;
}
- dev_entry = hrDeviceTblEntry_find_by_name(pp->lp);
+
+ if (strncmp(pp->lp, _PATH_DEV, strlen(_PATH_DEV)) != 0) {
+ HR_DPRINTF((stderr,
+ "hrPrinterTable: skipped %s [device %s] because it is not a local one\n",
+ pp->printer,
+ pp->lp ));
+ return;
+
+ }
+ memset(&dev_only[0], '\0', sizeof(dev_only));
+ snprintf(dev_only, sizeof(dev_only) - 1, "%s", pp->lp + strlen(_PATH_DEV));
+
+ HR_DPRINTF((stderr,
+ "hrPrinterTable: printer %s has device %s\n",
+ pp->printer,
+ dev_only ));
+
+
+ dev_entry = hrDeviceTblEntry_find_by_name(dev_only);
if (dev_entry == NULL) {
+
+
HR_DPRINTF((stderr,
"hrPrinterTable: %s not in hrDeviceTable\n",
pp->lp ));
- dev_entry = hrDeviceTblEntry_create(pp->lp);
+
+
+
+ dev_entry = hrDeviceTblEntry_create(dev_only);
if (dev_entry == NULL) {
return;
}
@@ -673,7 +757,7 @@
}
assert(printer_entry != NULL);
printer_entry->flags |= HR_PRINTER_FOUND;
- printer_entry->status = PS_UNKNOWN;
+ printer_entry->status = getPrinterSatus(pp);
memset(printer_entry->detectedErrorState, 0, sizeof(printer_entry->detectedErrorState));
@@ -686,13 +770,14 @@
struct printer myprinter, *pp = &myprinter;
init_printer(pp);
+ HR_DPRINTF((stderr, "---->Getting printers .....\n" ));
more = firstprinter(pp, &status);
if (status)
goto errloop;
while (more) {
do {
- HR_DPRINTF((stderr, "*****>Got printer %s", pp->printer));
+ HR_DPRINTF((stderr, "---->Got printer %s\n", pp->printer));
handle_printer(pp);
@@ -705,6 +790,7 @@
} while (more && status);
}
+ lastprinter();
hrState_g.hr_printer_tick = this_tick;
hrState_g.hrPrinter_tbl_age = time(NULL);
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.c#21 (text+ko) ====
@@ -154,7 +154,7 @@
mdmaybeload();
hrState_g.md_fd = -1;
- snprintf(mddev, sizeof(mddev) - 1, "/dev/%s", MDCTL_NAME);
+ snprintf(mddev, sizeof(mddev) - 1, "%s%s", _PATH_DEV, MDCTL_NAME);
if ((hrState_g.md_fd = open(mddev, O_RDWR)) < 0) {
syslog(LOG_ERR, "open %s failed: %m", mddev);
}
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h#26 (text+ko) ====
@@ -316,7 +316,7 @@
PS_UNKNOWN = 2,
PS_IDLE = 3,
PS_PRINTING = 4,
- DS_WARMUP = 5
+ PS_WARMUP = 5
};
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_storage_tbl.c#9 (text+ko) ====
@@ -41,7 +41,7 @@
#include <err.h>
#include <unistd.h> /*for getpagesize()*/
#include <vm/vm_param.h>
-
+#include <paths.h>
@@ -238,7 +238,7 @@
for (len = 0; len < nswapdev; len++ ) {
memset(&swap_w_prefix[0], '\0', sizeof(swap_w_prefix) );
snprintf(swap_w_prefix, sizeof(swap_w_prefix) - 1,
- "Swap:/dev/%s", hrState_g.swap_devs[len].ksw_devname);
+ "Swap:%s%s", _PATH_DEV, hrState_g.swap_devs[len].ksw_devname);
entry = hrStorageTblEntry_find_by_name(swap_w_prefix);
if (entry != NULL) {
entry->flags |= HR_STORAGE_FOUND;
More information about the p4-projects
mailing list