getencstat.c revision 198929
157580Smjacob/* $FreeBSD: head/share/examples/ses/srcs/getencstat.c 198929 2009-11-04 21:12:33Z delphij $ */ 257580Smjacob/* 357580Smjacob * Copyright (c) 2000 by Matthew Jacob 457580Smjacob * All rights reserved. 557580Smjacob * 657580Smjacob * Redistribution and use in source and binary forms, with or without 757580Smjacob * modification, are permitted provided that the following conditions 857580Smjacob * are met: 957580Smjacob * 1. Redistributions of source code must retain the above copyright 1057580Smjacob * notice, this list of conditions, and the following disclaimer, 1157580Smjacob * without modification, immediately at the beginning of the file. 1257580Smjacob * 2. The name of the author may not be used to endorse or promote products 1357580Smjacob * derived from this software without specific prior written permission. 1457580Smjacob * 1557580Smjacob * Alternatively, this software may be distributed under the terms of the 1657580Smjacob * the GNU Public License ("GPL"). 1757580Smjacob * 1857580Smjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1957580Smjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2057580Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2157580Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 2257580Smjacob * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2357580Smjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2457580Smjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2557580Smjacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2657580Smjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2757580Smjacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2857580Smjacob * SUCH DAMAGE. 2957580Smjacob * 3057580Smjacob * Matthew Jacob 3157580Smjacob * Feral Software 3257580Smjacob * mjacob@feral.com 3357580Smjacob */ 3457580Smjacob 3557580Smjacob#include <unistd.h> 3657580Smjacob#include <stdlib.h> 3757580Smjacob#include <stdio.h> 38198929Sdelphij#include <string.h> 3957580Smjacob#include <sys/ioctl.h> 4057580Smjacob#include <fcntl.h> 4157580Smjacob#include SESINC 4257580Smjacob 4357580Smjacobextern char *geteltnm __P((int)); 4457580Smjacobextern char *stat2ascii __P((int, u_char *)); 4557580Smjacob 4657580Smjacobint 4757580Smjacobmain(a, v) 4857580Smjacob int a; 4957580Smjacob char **v; 5057580Smjacob{ 5157580Smjacob ses_object *objp; 5257580Smjacob ses_objstat ob; 5357580Smjacob int fd, nobj, f, i, verbose, quiet, errors; 5457580Smjacob u_char estat; 5557580Smjacob 5657580Smjacob if (a < 2) { 5757580Smjacob fprintf(stderr, "usage: %s [ -v ] device [ device ... ]\n", *v); 5857580Smjacob return (1); 5957580Smjacob } 6057580Smjacob errors = quiet = verbose = 0; 6157580Smjacob if (strcmp(v[1], "-V") == 0) { 6257580Smjacob verbose = 2; 6357580Smjacob v++; 6457580Smjacob } else if (strcmp(v[1], "-v") == 0) { 6557580Smjacob verbose = 1; 6657580Smjacob v++; 6757580Smjacob } else if (strcmp(v[1], "-q") == 0) { 6857580Smjacob quiet = 1; 6957580Smjacob verbose = 0; 7057580Smjacob v++; 7157580Smjacob } 7257580Smjacob while (*++v) { 7357580Smjacob 7457580Smjacob fd = open(*v, O_RDONLY); 7557580Smjacob if (fd < 0) { 7657580Smjacob perror(*v); 7757580Smjacob continue; 7857580Smjacob } 7957580Smjacob if (ioctl(fd, SESIOC_GETNOBJ, (caddr_t) &nobj) < 0) { 8057580Smjacob perror("SESIOC_GETNOBJ"); 8157580Smjacob (void) close(fd); 8257580Smjacob continue; 8357580Smjacob } 8457580Smjacob if (ioctl(fd, SESIOC_GETENCSTAT, (caddr_t) &estat) < 0) { 8557580Smjacob perror("SESIOC_GETENCSTAT"); 8657580Smjacob (void) close(fd); 8757580Smjacob continue; 8857580Smjacob } 8957580Smjacob if ((verbose == 0 || quiet == 1) && estat == 0) { 9057580Smjacob if (quiet == 0) 9157580Smjacob fprintf(stdout, "%s: Enclosure OK\n", *v); 9257580Smjacob (void) close(fd); 9357580Smjacob continue; 9457580Smjacob } 9557580Smjacob fprintf(stdout, "%s: Enclosure Status ", *v); 9657580Smjacob if (estat == 0) { 9757580Smjacob fprintf(stdout, "<OK"); 9857580Smjacob } else { 9957580Smjacob errors++; 10057580Smjacob f = '<'; 10157580Smjacob if (estat & SES_ENCSTAT_INFO) { 10257580Smjacob fprintf(stdout, "%cINFO", f); 10357580Smjacob f = ','; 10457580Smjacob } 10557580Smjacob if (estat & SES_ENCSTAT_NONCRITICAL) { 10657580Smjacob fprintf(stdout, "%cNONCRITICAL", f); 10757580Smjacob f = ','; 10857580Smjacob } 10957580Smjacob if (estat & SES_ENCSTAT_CRITICAL) { 11057580Smjacob fprintf(stdout, "%cCRITICAL", f); 11157580Smjacob f = ','; 11257580Smjacob } 11357580Smjacob if (estat & SES_ENCSTAT_UNRECOV) { 11457580Smjacob fprintf(stdout, "%cUNRECOV", f); 11557580Smjacob f = ','; 11657580Smjacob } 11757580Smjacob } 11857580Smjacob fprintf(stdout, ">\n"); 11957580Smjacob objp = calloc(nobj, sizeof (ses_object)); 12057580Smjacob if (objp == NULL) { 12157580Smjacob perror("calloc"); 12257580Smjacob (void) close(fd); 12357580Smjacob continue; 12457580Smjacob } 12557580Smjacob if (ioctl(fd, SESIOC_GETOBJMAP, (caddr_t) objp) < 0) { 12657580Smjacob perror("SESIOC_GETOBJMAP"); 12757580Smjacob (void) close(fd); 12857580Smjacob continue; 12957580Smjacob } 13057580Smjacob for (i = 0; i < nobj; i++) { 13157580Smjacob ob.obj_id = objp[i].obj_id; 13257580Smjacob if (ioctl(fd, SESIOC_GETOBJSTAT, (caddr_t) &ob) < 0) { 13357580Smjacob perror("SESIOC_GETOBJSTAT"); 13457580Smjacob (void) close(fd); 13557580Smjacob break; 13657580Smjacob } 13757580Smjacob if ((ob.cstat[0] & 0xf) == SES_OBJSTAT_OK) { 13857580Smjacob if (verbose) { 13957580Smjacob fprintf(stdout, 14057580Smjacob "Element 0x%x: %s OK (%s)\n", 14157580Smjacob ob.obj_id, 14257580Smjacob geteltnm(objp[i].object_type), 14357580Smjacob stat2ascii(objp[i].object_type, 14457580Smjacob ob.cstat)); 14557580Smjacob } 14657580Smjacob continue; 14757580Smjacob } 14857580Smjacob fprintf(stdout, "Element 0x%x: %s, %s\n", 14957580Smjacob ob.obj_id, geteltnm(objp[i].object_type), 15057580Smjacob stat2ascii(objp[i].object_type, ob.cstat)); 15157580Smjacob } 15257580Smjacob free(objp); 15357580Smjacob (void) close(fd); 15457580Smjacob } 15557580Smjacob return (errors); 15657580Smjacob} 157