Path: blob/main/share/examples/ses/srcs/getencstat.c
39481 views
/*1* Copyright (c) 2000 by Matthew Jacob2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions, and the following disclaimer,9* without modification, immediately at the beginning of the file.10* 2. The name of the author may not be used to endorse or promote products11* derived from this software without specific prior written permission.12*13* Alternatively, this software may be distributed under the terms of the14* the GNU Public License ("GPL").15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR20* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*28* Matthew Jacob29* Feral Software30* [email protected]31*/3233#include <unistd.h>34#include <stddef.h>35#include <stdint.h>36#include <stdlib.h>37#include <stdio.h>38#include <string.h>39#include <sys/ioctl.h>40#include <fcntl.h>41#include <cam/scsi/scsi_all.h>42#include <cam/scsi/scsi_enc.h>4344#include "eltsub.h"4546int47main(int a, char **v)48{49encioc_string_t stri;50encioc_element_t *objp;51encioc_elm_status_t ob;52encioc_elm_desc_t objd;53encioc_elm_devnames_t objdn;54int fd, nobj, f, i, verbose, quiet, errors;55u_char estat;56char str[32];5758if (a < 2) {59fprintf(stderr, "usage: %s [ -v ] device [ device ... ]\n", *v);60return (1);61}62errors = quiet = verbose = 0;63if (strcmp(v[1], "-V") == 0) {64verbose = 2;65v++;66} else if (strcmp(v[1], "-v") == 0) {67verbose = 1;68v++;69} else if (strcmp(v[1], "-q") == 0) {70quiet = 1;71verbose = 0;72v++;73}74while (*++v) {7576fd = open(*v, O_RDONLY);77if (fd < 0) {78perror(*v);79continue;80}81if (verbose > 1) {82stri.bufsiz = sizeof(str);83stri.buf = &str[0];84if (ioctl(fd, ENCIOC_GETENCNAME, (caddr_t) &stri) == 0)85printf("%s: Enclosure Name: %s\n", *v, stri.buf);86stri.bufsiz = sizeof(str);87stri.buf = &str[0];88if (ioctl(fd, ENCIOC_GETENCID, (caddr_t) &stri) == 0)89printf("%s: Enclosure ID: %s\n", *v, stri.buf);90}91if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {92perror("ENCIOC_GETNELM");93(void) close(fd);94continue;95}96if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {97perror("ENCIOC_GETENCSTAT");98(void) close(fd);99continue;100}101if ((verbose == 0 || quiet == 1) && estat == 0) {102if (quiet == 0)103fprintf(stdout, "%s: Enclosure OK\n", *v);104(void) close(fd);105continue;106}107fprintf(stdout, "%s: Enclosure Status ", *v);108if (estat == 0) {109fprintf(stdout, "<OK");110} else {111errors++;112f = '<';113if (estat & SES_ENCSTAT_INFO) {114fprintf(stdout, "%cINFO", f);115f = ',';116}117if (estat & SES_ENCSTAT_NONCRITICAL) {118fprintf(stdout, "%cNONCRITICAL", f);119f = ',';120}121if (estat & SES_ENCSTAT_CRITICAL) {122fprintf(stdout, "%cCRITICAL", f);123f = ',';124}125if (estat & SES_ENCSTAT_UNRECOV) {126fprintf(stdout, "%cUNRECOV", f);127f = ',';128}129}130fprintf(stdout, ">\n");131objp = calloc(nobj, sizeof (encioc_element_t));132if (objp == NULL) {133perror("calloc");134(void) close(fd);135continue;136}137if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {138perror("ENCIOC_GETELMMAP");139(void) close(fd);140continue;141}142for (i = 0; i < nobj; i++) {143ob.elm_idx = objp[i].elm_idx;144if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &ob) < 0) {145perror("ENCIOC_GETELMSTAT");146(void) close(fd);147break;148}149bzero(&objd, sizeof(objd));150objd.elm_idx = objp[i].elm_idx;151objd.elm_desc_len = UINT16_MAX;152objd.elm_desc_str = calloc(UINT16_MAX, sizeof(char));153if (objd.elm_desc_str == NULL) {154perror("calloc");155(void) close(fd);156continue;157}158if (ioctl(fd, ENCIOC_GETELMDESC, (caddr_t)&objd) < 0) {159perror("ENCIOC_GETELMDESC");160(void) close(fd);161break;162}163bzero(&objdn, sizeof(objdn));164objdn.elm_idx = objp[i].elm_idx;165objdn.elm_names_size = 128;166objdn.elm_devnames = calloc(128, sizeof(char));167if (objdn.elm_devnames == NULL) {168perror("calloc");169(void) close(fd);170break;171}172/*173* This ioctl isn't critical and has a good chance174* of returning -1.175*/176(void)ioctl(fd, ENCIOC_GETELMDEVNAMES, (caddr_t)&objdn);177fprintf(stdout, "Element 0x%x: %s", ob.elm_idx,178geteltnm(objp[i].elm_type));179fprintf(stdout, ", %s",180stat2ascii(objp[i].elm_type, ob.cstat));181if (objd.elm_desc_len > 0)182fprintf(stdout, ", descriptor: '%s'",183objd.elm_desc_str);184if (objdn.elm_names_len > 0)185fprintf(stdout, ", dev: '%s'",186objdn.elm_devnames);187fprintf(stdout, "\n");188free(objdn.elm_devnames);189}190free(objp);191(void) close(fd);192}193return (errors);194}195196197