Index: head/usr.sbin/sesutil/Makefile =================================================================== --- head/usr.sbin/sesutil/Makefile (revision 320477) +++ head/usr.sbin/sesutil/Makefile (revision 320478) @@ -1,7 +1,9 @@ # $FreeBSD$ PROG= sesutil SRCS= sesutil.c eltsub.c MAN= sesutil.8 +LIBADD= xo + .include Index: head/usr.sbin/sesutil/eltsub.c =================================================================== --- head/usr.sbin/sesutil/eltsub.c (revision 320477) +++ head/usr.sbin/sesutil/eltsub.c (revision 320478) @@ -1,143 +1,144 @@ /* $FreeBSD$ */ /* * Copyright (c) 2000 by Matthew Jacob * All rights reserved. * * Redistribution 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 must retain the above copyright * notice, this list of conditions, and the following disclaimer, * without modification, immediately at the beginning of the file. * 2. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * Alternatively, this software may be distributed under the terms of the * the GNU Public License ("GPL"). * * 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. * * Matthew Jacob * Feral Software * mjacob@feral.com */ #include #include #include #include #include #include +#include #include "eltsub.h" /* * offset by +20 degrees. * The range of the value expresses a temperature between -19 and +235 degrees * Celsius. A value of 00h is reserved. */ #define TEMPERATURE_OFFSET 20 const char * geteltnm(int type) { static char rbuf[132]; switch (type) { case ELMTYP_UNSPECIFIED: return ("Unspecified"); case ELMTYP_DEVICE: return ("Device Slot"); case ELMTYP_POWER: return ("Power Supply"); case ELMTYP_FAN: return ("Cooling"); case ELMTYP_THERM: return ("Temperature Sensors"); case ELMTYP_DOORLOCK: return ("Door Lock"); case ELMTYP_ALARM: return ("Audible alarm"); case ELMTYP_ESCC: return ("Enclosure Services Controller Electronics"); case ELMTYP_SCC: return ("SCC Controller Electronics"); case ELMTYP_NVRAM: return ("Nonvolatile Cache"); case ELMTYP_INV_OP_REASON: return ("Invalid Operation Reason"); case ELMTYP_UPS: return ("Uninterruptible Power Supply"); case ELMTYP_DISPLAY: return ("Display"); case ELMTYP_KEYPAD: return ("Key Pad Entry"); case ELMTYP_ENCLOSURE: return ("Enclosure"); case ELMTYP_SCSIXVR: return ("SCSI Port/Transceiver"); case ELMTYP_LANGUAGE: return ("Language"); case ELMTYP_COMPORT: return ("Communication Port"); case ELMTYP_VOM: return ("Voltage Sensor"); case ELMTYP_AMMETER: return ("Current Sensor"); case ELMTYP_SCSI_TGT: return ("SCSI Target Port"); case ELMTYP_SCSI_INI: return ("SCSI Initiator Port"); case ELMTYP_SUBENC: return ("Simple Subenclosure"); case ELMTYP_ARRAY_DEV: return ("Array Device Slot"); case ELMTYP_SAS_EXP: return ("SAS Expander"); case ELMTYP_SAS_CONN: return ("SAS Connector"); default: snprintf(rbuf, sizeof(rbuf), "", type); return (rbuf); } } const char * scode2ascii(u_char code) { static char rbuf[32]; switch (code & 0xf) { case SES_OBJSTAT_UNSUPPORTED: return ("Unsupported"); case SES_OBJSTAT_OK: return ("OK"); case SES_OBJSTAT_CRIT: return ("Critical"); case SES_OBJSTAT_NONCRIT: return ("Noncritical"); case SES_OBJSTAT_UNRECOV: return ("Unrecoverable"); case SES_OBJSTAT_NOTINSTALLED: return ("Not Installed"); case SES_OBJSTAT_UNKNOWN: return ("Unknown"); case SES_OBJSTAT_NOTAVAIL: return ("Not Available"); case SES_OBJSTAT_NOACCESS: return ("No Access Allowed"); default: snprintf(rbuf, sizeof(rbuf), "", code & 0xf); return (rbuf); } } Index: head/usr.sbin/sesutil/sesutil.8 =================================================================== --- head/usr.sbin/sesutil/sesutil.8 (revision 320477) +++ head/usr.sbin/sesutil/sesutil.8 (revision 320478) @@ -1,130 +1,140 @@ .\" Copyright (c) 2015 Baptiste Daroussin .\" All rights reserved. .\" .\" Redistribution 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 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$ .\" -.Dd September 6, 2015 +.Dd June 29, 2017 .Dt SESUTIL 8 .Os .Sh NAME .Nm sesutil .Nd Utility for managing SCSI Enclosure Services (SES) device .Sh SYNOPSIS .Nm .Cm fault .Op Fl u Ar /dev/sesN .Aq Ar disk | Ar sesid | Li all .Op on | off .Nm .Cm locate .Op Fl u Ar /dev/sesN .Aq Ar disk | Ar sesid | Li all .Op on | off .Nm .Cm map +.Op Fl -libxo Ar options .Op Fl u Ar /dev/sesN .Nm .Cm status +.Op Fl -libxo Ar options .Op Fl u Ar /dev/sesN .Sh DESCRIPTION The .Nm utility can be used to query and modify various parameter of SCSI Enclosure Services (SES) devices. .Pp List of supported commands: .Bl -tag -width indent +.It Fl -libxo +Generate output via +.Xr libxo 3 +in a selection of different human and machine readable formats. +See +.Xr xo_parse_args 3 .It Cm fault Oo Fl u Ar /dev/sesN Oc Ao Ar disk | Li all Ac Op on | off Change the state of the external fault LED associated with .Ar disk . .Ar disk can be the device name of the disk, like .Cm da12 , or .Ql all . to indicate all disks attached to SES controllers. .It Cm fault Fl u Ar /dev/sesN Ar sesid Op on | off Change the state of the external fault LED associated with an element connected to the SES controller. .Ar sesid must be the element ID of a valid item attached to the controller. Use the .Cm map command to list the elements attached to a controller. .It Cm locate Oo Fl u Ar /dev/sesN Oc Ao Ar disk | Li all Ac Op on | off Change the state of the external locate LED associated with .Ar disk . .Ar disk can be the device name of the disk, like .Cm da12 , or .Ql all . to indicate all disks attached to SES controllers. .It Cm locate Fl u Ar /dev/sesN Ar sesid Op on | off Change the state of the external locate LED associated with an element connected to the SES controller. .Ar sesid must be the element ID of a valid item attached to the controller. Use the .Cm map command to list the elements attached to a controller. .It Cm map Op Fl u Ar /dev/sesN Display a map of all elements connected to the specified .Xr ses 4 controller. If no controller is specified, all controllers are mapped. .It Cm status Op Fl u Ar /dev/sesN Display the status of the specified .Xr ses 4 controller. If no controller is specified, the status of each controller is returned. .El .Sh EXAMPLES Turn off all locate LEDs: .Pp .Dl Nm Cm locate all off .Pp Turn on the locate LED for the drive bay corresponding to .Pa da15 : .Pp .Dl Nm Cm locate da15 on .Pp Turn on the fault LED for a drive bay not associated with a device: .Pp .Dl Nm Cm fault -u /dev/ses2 7 on .Sh SEE ALSO +.Xr libxo 3 , +.Xr xo_parse_args 3 , .Xr ses 4 .Sh HISTORY The .Nm utility first appeared in .Fx 11.0 . .Sh AUTHORS .An -nosplit The .Nm utility was written by .An Baptiste Daroussin Aq Mt bapt@FreeBSD.org and .An Allan Jude Aq Mt allanjude@FreeBSD.org . Index: head/usr.sbin/sesutil/sesutil.c =================================================================== --- head/usr.sbin/sesutil/sesutil.c (revision 320477) +++ head/usr.sbin/sesutil/sesutil.c (revision 320478) @@ -1,626 +1,650 @@ /*- * Copyright (c) 2015 Baptiste Daroussin * Copyright (c) 2015 Allan Jude * Copyright (c) 2000 by Matthew Jacob * All rights reserved. * * Redistribution 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 must retain the above copyright * notice, this list of conditions and the following disclaimer * in this position and unchanged. * 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(S) ``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(S) 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. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#include #include #include "eltsub.h" +#define SESUTIL_XO_VERSION "1" + static int encstatus(int argc, char **argv); static int fault(int argc, char **argv); static int locate(int argc, char **argv); static int objmap(int argc, char **argv); static int sesled(int argc, char **argv, bool fault); static void sesutil_print(bool *title, const char *fmt, ...) __printflike(2,3); static struct command { const char *name; const char *param; const char *desc; int (*exec)(int argc, char **argv); } cmds[] = { { "fault", "(||all) (on|off)", "Change the state of the fault LED associated with a disk", fault }, { "locate", "(||all) (on|off)", "Change the state of the locate LED associated with a disk", locate }, { "map", "", "Print a map of the devices managed by the enclosure", objmap } , { "status", "", "Print the status of the enclosure", encstatus }, }; static const int nbcmds = nitems(cmds); static const char *uflag; static void usage(FILE *out, const char *subcmd) { int i; if (subcmd == NULL) { fprintf(out, "Usage: %s [-u /dev/ses] [options]\n", getprogname()); fprintf(out, "Commands supported:\n"); } for (i = 0; i < nbcmds; i++) { if (subcmd != NULL) { if (strcmp(subcmd, cmds[i].name) == 0) { fprintf(out, "Usage: %s %s [-u /dev/ses] " "%s\n\t%s\n", getprogname(), subcmd, cmds[i].param, cmds[i].desc); break; } continue; } fprintf(out, " %-12s%s\n\t\t%s\n\n", cmds[i].name, cmds[i].param, cmds[i].desc); } exit(EXIT_FAILURE); } static void do_led(int fd, unsigned int idx, bool onoff, bool setfault) { encioc_elm_status_t o; o.elm_idx = idx; if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &o) < 0) { close(fd); - err(EXIT_FAILURE, "ENCIOC_GETELMSTAT"); + xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT"); } o.cstat[0] |= 0x80; if (setfault) { if (onoff) o.cstat[3] |= 0x20; else o.cstat[3] &= 0xdf; } else { if (onoff) o.cstat[2] |= 0x02; else o.cstat[2] &= 0xfd; } if (ioctl(fd, ENCIOC_SETELMSTAT, (caddr_t) &o) < 0) { close(fd); - err(EXIT_FAILURE, "ENCIOC_SETELMSTAT"); + xo_err(EXIT_FAILURE, "ENCIOC_SETELMSTAT"); } } static bool disk_match(const char *devnames, const char *disk, size_t len) { const char *dname; dname = devnames; while ((dname = strstr(dname, disk)) != NULL) { if (dname[len] == '\0' || dname[len] == ',') { return (true); } dname++; } return (false); } static int sesled(int argc, char **argv, bool setfault) { encioc_elm_devnames_t objdn; encioc_element_t *objp; glob_t g; char *disk, *endptr; size_t len, i, ndisks; int fd; unsigned int nobj, j, sesid; bool all, isses, onoff; isses = false; all = false; onoff = false; if (argc != 3) { usage(stderr, (setfault ? "fault" : "locate")); } disk = argv[1]; sesid = strtoul(disk, &endptr, 10); if (*endptr == '\0') { endptr = strrchr(uflag, '*'); if (endptr != NULL && *endptr == '*') { - warnx("Must specifying a SES device (-u) to use a SES " + xo_warnx("Must specifying a SES device (-u) to use a SES " "id# to identify a disk"); usage(stderr, (setfault ? "fault" : "locate")); } isses = true; } if (strcmp(argv[2], "on") == 0) { onoff = true; } else if (strcmp(argv[2], "off") == 0) { onoff = false; } else { usage(stderr, (setfault ? "fault" : "locate")); } if (strcmp(disk, "all") == 0) { all = true; } len = strlen(disk); /* Get the list of ses devices */ if (glob((uflag != NULL ? uflag : "/dev/ses[0-9]*"), 0, NULL, &g) == GLOB_NOMATCH) { globfree(&g); - errx(EXIT_FAILURE, "No SES devices found"); + xo_errx(EXIT_FAILURE, "No SES devices found"); } ndisks = 0; for (i = 0; i < g.gl_pathc; i++) { /* ensure we only got numbers after ses */ if (strspn(g.gl_pathv[i] + 8, "0123456789") != strlen(g.gl_pathv[i] + 8)) { continue; } if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { /* * Don't treat non-access errors as critical if we are * accessing all devices */ if (errno == EACCES && g.gl_pathc > 1) { - err(EXIT_FAILURE, "unable to access SES device"); + xo_err(EXIT_FAILURE, "unable to access SES device"); } - warn("unable to access SES device: %s", g.gl_pathv[i]); + xo_warn("unable to access SES device: %s", g.gl_pathv[i]); continue; } if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) { close(fd); - err(EXIT_FAILURE, "ENCIOC_GETNELM"); + xo_err(EXIT_FAILURE, "ENCIOC_GETNELM"); } objp = calloc(nobj, sizeof(encioc_element_t)); if (objp == NULL) { close(fd); - err(EXIT_FAILURE, "calloc()"); + xo_err(EXIT_FAILURE, "calloc()"); } if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) { close(fd); - err(EXIT_FAILURE, "ENCIOC_GETELMMAP"); + xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP"); } if (isses) { if (sesid > nobj) { close(fd); - errx(EXIT_FAILURE, + xo_errx(EXIT_FAILURE, "Requested SES ID does not exist"); } do_led(fd, sesid, onoff, setfault); ndisks++; close(fd); break; } for (j = 0; j < nobj; j++) { if (all) { do_led(fd, objp[j].elm_idx, onoff, setfault); continue; } memset(&objdn, 0, sizeof(objdn)); objdn.elm_idx = objp[j].elm_idx; objdn.elm_names_size = 128; objdn.elm_devnames = calloc(128, sizeof(char)); if (objdn.elm_devnames == NULL) { close(fd); - err(EXIT_FAILURE, "calloc()"); + xo_err(EXIT_FAILURE, "calloc()"); } if (ioctl(fd, ENCIOC_GETELMDEVNAMES, (caddr_t) &objdn) <0) { continue; } if (objdn.elm_names_len > 0) { if (disk_match(objdn.elm_devnames, disk, len)) { do_led(fd, objdn.elm_idx, onoff, setfault); ndisks++; break; } } } free(objp); close(fd); } globfree(&g); if (ndisks == 0 && all == false) { - errx(EXIT_FAILURE, "Count not find the SES id of device '%s'", + xo_errx(EXIT_FAILURE, "Count not find the SES id of device '%s'", disk); } return (EXIT_SUCCESS); } static int locate(int argc, char **argv) { return (sesled(argc, argv, false)); } static int fault(int argc, char **argv) { return (sesled(argc, argv, true)); } #define TEMPERATURE_OFFSET 20 static void sesutil_print(bool *title, const char *fmt, ...) { va_list args; if (!*title) { - printf("\t\tExtra status:\n"); + xo_open_container("extra_status"); + xo_emit("\t\tExtra status:\n"); *title = true; } va_start(args, fmt); - vprintf(fmt, args); + xo_emit_hv(NULL, fmt, args); va_end(args); } static void print_extra_status(int eletype, u_char *cstat) { bool title = false; if (cstat[0] & 0x40) { - sesutil_print(&title, "\t\t- Predicted Failure\n"); + sesutil_print(&title, "\t\t-{e:predicted_failure/true} Predicted Failure\n"); } if (cstat[0] & 0x20) { - sesutil_print(&title, "\t\t- Disabled\n"); + sesutil_print(&title, "\t\t-{e:disabled/true} Disabled\n"); } if (cstat[0] & 0x10) { - sesutil_print(&title, "\t\t- Swapped\n"); + sesutil_print(&title, "\t\t-{e:swapped/true} Swapped\n"); } switch (eletype) { case ELMTYP_DEVICE: - if (cstat[2] & 0x02) { - sesutil_print(&title, "\t\t- LED=locate\n"); - } - if (cstat[2] & 0x20) { - sesutil_print(&title, "\t\t- LED=fault\n"); - } - break; case ELMTYP_ARRAY_DEV: if (cstat[2] & 0x02) { - sesutil_print(&title, "\t\t- LED=locate\n"); + sesutil_print(&title, "\t\t- LED={q:led/locate}\n"); } if (cstat[2] & 0x20) { - sesutil_print(&title, "\t\t- LED=fault\n"); + sesutil_print(&title, "\t\t- LED={q:led/fault}\n"); } break; case ELMTYP_FAN: - sesutil_print(&title, "\t\t- Speed: %d rpm\n", + sesutil_print(&title, "\t\t- Speed: {:speed/%d}{Uw:rpm}\n", (((0x7 & cstat[1]) << 8) + cstat[2]) * 10); break; case ELMTYP_THERM: if (cstat[2]) { - sesutil_print(&title, "\t\t- Temperature: %d C\n", + sesutil_print(&title, "\t\t- Temperature: {:temperature/%d}{Uw:C}\n", cstat[2] - TEMPERATURE_OFFSET); } else { - sesutil_print(&title, "\t\t- Temperature: -reserved-\n"); + sesutil_print(&title, "\t\t- Temperature: -{q:temperature/reserved}-\n"); } break; case ELMTYP_VOM: - sesutil_print(&title, "\t\t- Voltage: %.2f V\n", + sesutil_print(&title, "\t\t- Voltage: {:voltage/%.2f}{Uw:V}\n", be16dec(cstat + 2) / 100.0); break; } + if (title) { + xo_close_container("extra_status"); + } } static int objmap(int argc, char **argv __unused) { encioc_string_t stri; encioc_elm_devnames_t e_devname; encioc_elm_status_t e_status; encioc_elm_desc_t e_desc; encioc_element_t *e_ptr; glob_t g; int fd; unsigned int j, nobj; size_t i; char str[32]; if (argc != 1) { usage(stderr, "map"); } /* Get the list of ses devices */ if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) { globfree(&g); - errx(EXIT_FAILURE, "No SES devices found"); + xo_errx(EXIT_FAILURE, "No SES devices found"); } + xo_set_version(SESUTIL_XO_VERSION); + xo_open_container("sesutil"); + xo_open_list("enclosures"); for (i = 0; i < g.gl_pathc; i++) { /* ensure we only got numbers after ses */ if (strspn(g.gl_pathv[i] + 8, "0123456789") != strlen(g.gl_pathv[i] + 8)) { continue; } if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { /* * Don't treat non-access errors as critical if we are * accessing all devices */ if (errno == EACCES && g.gl_pathc > 1) { - err(EXIT_FAILURE, "unable to access SES device"); + xo_err(EXIT_FAILURE, "unable to access SES device"); } - warn("unable to access SES device: %s", g.gl_pathv[i]); + xo_warn("unable to access SES device: %s", g.gl_pathv[i]); continue; } if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) { close(fd); - err(EXIT_FAILURE, "ENCIOC_GETNELM"); + xo_err(EXIT_FAILURE, "ENCIOC_GETNELM"); } e_ptr = calloc(nobj, sizeof(encioc_element_t)); if (e_ptr == NULL) { close(fd); - err(EXIT_FAILURE, "calloc()"); + xo_err(EXIT_FAILURE, "calloc()"); } if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) e_ptr) < 0) { close(fd); - err(EXIT_FAILURE, "ENCIOC_GETELMMAP"); + xo_err(EXIT_FAILURE, "ENCIOC_GETELMMAP"); } - printf("%s:\n", g.gl_pathv[i] + 5); + xo_open_instance("enclosures"); + xo_emit("{t:enc/%s}:\n", g.gl_pathv[i] + 5); stri.bufsiz = sizeof(str); stri.buf = &str[0]; if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0) - printf("\tEnclosure Name: %s\n", stri.buf); + xo_emit("\tEnclosure Name: {t:name/%s}\n", stri.buf); stri.bufsiz = sizeof(str); stri.buf = &str[0]; if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0) - printf("\tEnclosure ID: %s\n", stri.buf); + xo_emit("\tEnclosure ID: {t:id/%s}\n", stri.buf); + xo_open_list("elements"); for (j = 0; j < nobj; j++) { /* Get the status of the element */ memset(&e_status, 0, sizeof(e_status)); e_status.elm_idx = e_ptr[j].elm_idx; if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &e_status) < 0) { close(fd); - err(EXIT_FAILURE, "ENCIOC_GETELMSTAT"); + xo_err(EXIT_FAILURE, "ENCIOC_GETELMSTAT"); } /* Get the description of the element */ memset(&e_desc, 0, sizeof(e_desc)); e_desc.elm_idx = e_ptr[j].elm_idx; e_desc.elm_desc_len = UINT16_MAX; e_desc.elm_desc_str = calloc(UINT16_MAX, sizeof(char)); if (e_desc.elm_desc_str == NULL) { close(fd); - err(EXIT_FAILURE, "calloc()"); + xo_err(EXIT_FAILURE, "calloc()"); } if (ioctl(fd, ENCIOC_GETELMDESC, (caddr_t) &e_desc) < 0) { close(fd); - err(EXIT_FAILURE, "ENCIOC_GETELMDESC"); + xo_err(EXIT_FAILURE, "ENCIOC_GETELMDESC"); } /* Get the device name(s) of the element */ memset(&e_devname, 0, sizeof(e_devname)); e_devname.elm_idx = e_ptr[j].elm_idx; e_devname.elm_names_size = 128; e_devname.elm_devnames = calloc(128, sizeof(char)); if (e_devname.elm_devnames == NULL) { close(fd); - err(EXIT_FAILURE, "calloc()"); + xo_err(EXIT_FAILURE, "calloc()"); } if (ioctl(fd, ENCIOC_GETELMDEVNAMES, (caddr_t) &e_devname) <0) { /* We don't care if this fails */ e_devname.elm_devnames[0] = '\0'; } - printf("\tElement %u, Type: %s\n", e_ptr[j].elm_idx, + xo_open_instance("elements"); + xo_emit("\tElement {:id/%u}, Type: {:type/%s}\n", e_ptr[j].elm_idx, geteltnm(e_ptr[j].elm_type)); - printf("\t\tStatus: %s (0x%02x 0x%02x 0x%02x 0x%02x)\n", + xo_emit("\t\tStatus: {:status/%s} ({q:status_code/0x%02x 0x%02x 0x%02x 0x%02x})\n", scode2ascii(e_status.cstat[0]), e_status.cstat[0], e_status.cstat[1], e_status.cstat[2], e_status.cstat[3]); if (e_desc.elm_desc_len > 0) { - printf("\t\tDescription: %s\n", + xo_emit("\t\tDescription: {:description/%s}\n", e_desc.elm_desc_str); } if (e_devname.elm_names_len > 0) { - printf("\t\tDevice Names: %s\n", + xo_emit("\t\tDevice Names: {:device_names/%s}\n", e_devname.elm_devnames); } print_extra_status(e_ptr[j].elm_type, e_status.cstat); + xo_close_instance("elements"); free(e_devname.elm_devnames); } + xo_close_list("elements"); free(e_ptr); close(fd); } globfree(&g); + xo_close_list("enclosures"); + xo_close_container("sesutil"); + xo_finish(); return (EXIT_SUCCESS); } static int encstatus(int argc, char **argv __unused) { glob_t g; int fd, status; size_t i, e; u_char estat; status = 0; if (argc != 1) { usage(stderr, "status"); } /* Get the list of ses devices */ if (glob(uflag, 0, NULL, &g) == GLOB_NOMATCH) { globfree(&g); - errx(EXIT_FAILURE, "No SES devices found"); + xo_errx(EXIT_FAILURE, "No SES devices found"); } + + xo_set_version(SESUTIL_XO_VERSION); + xo_open_container("sesutil"); + xo_open_list("enclosures"); for (i = 0; i < g.gl_pathc; i++) { /* ensure we only got numbers after ses */ if (strspn(g.gl_pathv[i] + 8, "0123456789") != strlen(g.gl_pathv[i] + 8)) { continue; } if ((fd = open(g.gl_pathv[i], O_RDWR)) < 0) { /* * Don't treat non-access errors as critical if we are * accessing all devices */ if (errno == EACCES && g.gl_pathc > 1) { - err(EXIT_FAILURE, "unable to access SES device"); + xo_err(EXIT_FAILURE, "unable to access SES device"); } - warn("unable to access SES device: %s", g.gl_pathv[i]); + xo_warn("unable to access SES device: %s", g.gl_pathv[i]); continue; } if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) { + xo_err(EXIT_FAILURE, "ENCIOC_GETENCSTAT"); close(fd); - err(EXIT_FAILURE, "ENCIOC_GETENCSTAT"); } - printf("%s: ", g.gl_pathv[i] + 5); + xo_open_instance("enclosures"); + xo_emit("{:enc/%s}: ", g.gl_pathv[i] + 5); e = 0; if (estat == 0) { if (status == 0) { status = 1; } - printf("OK"); + xo_emit("{q:status/OK}"); } else { if (estat & SES_ENCSTAT_INFO) { - printf("INFO"); + xo_emit("{lq:status/INFO}"); e++; } if (estat & SES_ENCSTAT_NONCRITICAL) { if (e) - printf(","); - printf("NONCRITICAL"); + xo_emit(","); + xo_emit("{lq:status/NONCRITICAL}"); e++; } if (estat & SES_ENCSTAT_CRITICAL) { if (e) - printf(","); - printf("CRITICAL"); + xo_emit(","); + xo_emit("{lq:status/CRITICAL}"); e++; status = -1; } if (estat & SES_ENCSTAT_UNRECOV) { if (e) - printf(","); - printf("UNRECOV"); + xo_emit(","); + xo_emit("{lq:status/UNRECOV}"); e++; status = -1; } } - printf("\n"); - + xo_close_instance("enclosures"); + xo_emit("\n"); close(fd); } globfree(&g); + xo_close_list("enclosures"); + xo_close_container("sesutil"); + xo_finish(); + if (status == 1) { return (EXIT_SUCCESS); } else { return (EXIT_FAILURE); } } int main(int argc, char **argv) { int i, ch; struct command *cmd = NULL; + + argc = xo_parse_args(argc, argv); + if (argc < 0) + exit(1); uflag = "/dev/ses[0-9]*"; while ((ch = getopt_long(argc, argv, "u:", NULL, NULL)) != -1) { switch (ch) { case 'u': uflag = optarg; break; case '?': default: usage(stderr, NULL); } } argc -= optind; argv += optind; if (argc < 1) { warnx("Missing command"); usage(stderr, NULL); } for (i = 0; i < nbcmds; i++) { if (strcmp(argv[0], cmds[i].name) == 0) { cmd = &cmds[i]; break; } } if (cmd == NULL) { warnx("unknown command %s", argv[0]); usage(stderr, NULL); } return (cmd->exec(argc, argv)); }