getencstat.c revision 1.1 1 1.1 mjacob /* $NetBSD: getencstat.c,v 1.1 2000/02/21 08:10:28 mjacob Exp $ */
2 1.1 mjacob /* $FreeBSD: $ */
3 1.1 mjacob /* $OpenBSD: $ */
4 1.1 mjacob /*
5 1.1 mjacob * Copyright (c) 2000 by Matthew Jacob
6 1.1 mjacob * All rights reserved.
7 1.1 mjacob *
8 1.1 mjacob * Redistribution and use in source and binary forms, with or without
9 1.1 mjacob * modification, are permitted provided that the following conditions
10 1.1 mjacob * are met:
11 1.1 mjacob * 1. Redistributions of source code must retain the above copyright
12 1.1 mjacob * notice, this list of conditions, and the following disclaimer,
13 1.1 mjacob * without modification, immediately at the beginning of the file.
14 1.1 mjacob * 2. The name of the author may not be used to endorse or promote products
15 1.1 mjacob * derived from this software without specific prior written permission.
16 1.1 mjacob *
17 1.1 mjacob * Alternatively, this software may be distributed under the terms of the
18 1.1 mjacob * the GNU Public License ("GPL").
19 1.1 mjacob *
20 1.1 mjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 1.1 mjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 mjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 mjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 1.1 mjacob * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 mjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 mjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 mjacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 mjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 mjacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 mjacob * SUCH DAMAGE.
31 1.1 mjacob *
32 1.1 mjacob * Matthew Jacob
33 1.1 mjacob * Feral Software
34 1.1 mjacob * mjacob (at) feral.com
35 1.1 mjacob */
36 1.1 mjacob
37 1.1 mjacob #include <unistd.h>
38 1.1 mjacob #include <stdlib.h>
39 1.1 mjacob #include <stdio.h>
40 1.1 mjacob #include <sys/ioctl.h>
41 1.1 mjacob #include <fcntl.h>
42 1.1 mjacob #include SESINC
43 1.1 mjacob
44 1.1 mjacob extern char *geteltnm __P((int));
45 1.1 mjacob extern char *stat2ascii __P((int, u_char *));
46 1.1 mjacob
47 1.1 mjacob int
48 1.1 mjacob main(a, v)
49 1.1 mjacob int a;
50 1.1 mjacob char **v;
51 1.1 mjacob {
52 1.1 mjacob ses_object *objp;
53 1.1 mjacob ses_objstat ob;
54 1.1 mjacob int fd, nobj, f, i, verbose, quiet, errors;
55 1.1 mjacob u_char estat;
56 1.1 mjacob
57 1.1 mjacob if (a < 2) {
58 1.1 mjacob fprintf(stderr, "usage: %s [ -v ] device [ device ... ]\n", *v);
59 1.1 mjacob return (1);
60 1.1 mjacob }
61 1.1 mjacob errors = quiet = verbose = 0;
62 1.1 mjacob if (strcmp(v[1], "-V") == 0) {
63 1.1 mjacob verbose = 2;
64 1.1 mjacob v++;
65 1.1 mjacob } else if (strcmp(v[1], "-v") == 0) {
66 1.1 mjacob verbose = 1;
67 1.1 mjacob v++;
68 1.1 mjacob } else if (strcmp(v[1], "-q") == 0) {
69 1.1 mjacob quiet = 1;
70 1.1 mjacob verbose = 0;
71 1.1 mjacob v++;
72 1.1 mjacob }
73 1.1 mjacob while (*++v) {
74 1.1 mjacob
75 1.1 mjacob fd = open(*v, O_RDONLY);
76 1.1 mjacob if (fd < 0) {
77 1.1 mjacob perror(*v);
78 1.1 mjacob continue;
79 1.1 mjacob }
80 1.1 mjacob if (ioctl(fd, SESIOC_GETNOBJ, (caddr_t) &nobj) < 0) {
81 1.1 mjacob perror("SESIOC_GETNOBJ");
82 1.1 mjacob (void) close(fd);
83 1.1 mjacob continue;
84 1.1 mjacob }
85 1.1 mjacob if (ioctl(fd, SESIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
86 1.1 mjacob perror("SESIOC_GETENCSTAT");
87 1.1 mjacob (void) close(fd);
88 1.1 mjacob continue;
89 1.1 mjacob }
90 1.1 mjacob if ((verbose == 0 || quiet == 1) && estat == 0) {
91 1.1 mjacob if (quiet == 0)
92 1.1 mjacob fprintf(stdout, "%s: Enclosure OK\n", *v);
93 1.1 mjacob (void) close(fd);
94 1.1 mjacob continue;
95 1.1 mjacob }
96 1.1 mjacob fprintf(stdout, "%s: Enclosure Status ", *v);
97 1.1 mjacob if (estat == 0) {
98 1.1 mjacob fprintf(stdout, "<OK");
99 1.1 mjacob } else {
100 1.1 mjacob errors++;
101 1.1 mjacob f = '<';
102 1.1 mjacob if (estat & SES_ENCSTAT_INFO) {
103 1.1 mjacob fprintf(stdout, "%cINFO", f);
104 1.1 mjacob f = ',';
105 1.1 mjacob }
106 1.1 mjacob if (estat & SES_ENCSTAT_NONCRITICAL) {
107 1.1 mjacob fprintf(stdout, "%cNONCRITICAL", f);
108 1.1 mjacob f = ',';
109 1.1 mjacob }
110 1.1 mjacob if (estat & SES_ENCSTAT_CRITICAL) {
111 1.1 mjacob fprintf(stdout, "%cCRITICAL", f);
112 1.1 mjacob f = ',';
113 1.1 mjacob }
114 1.1 mjacob if (estat & SES_ENCSTAT_UNRECOV) {
115 1.1 mjacob fprintf(stdout, "%cUNRECOV", f);
116 1.1 mjacob f = ',';
117 1.1 mjacob }
118 1.1 mjacob }
119 1.1 mjacob fprintf(stdout, ">\n");
120 1.1 mjacob objp = calloc(nobj, sizeof (ses_object));
121 1.1 mjacob if (objp == NULL) {
122 1.1 mjacob perror("calloc");
123 1.1 mjacob (void) close(fd);
124 1.1 mjacob continue;
125 1.1 mjacob }
126 1.1 mjacob if (ioctl(fd, SESIOC_GETOBJMAP, (caddr_t) objp) < 0) {
127 1.1 mjacob perror("SESIOC_GETOBJMAP");
128 1.1 mjacob (void) close(fd);
129 1.1 mjacob continue;
130 1.1 mjacob }
131 1.1 mjacob for (i = 0; i < nobj; i++) {
132 1.1 mjacob ob.obj_id = objp[i].obj_id;
133 1.1 mjacob if (ioctl(fd, SESIOC_GETOBJSTAT, (caddr_t) &ob) < 0) {
134 1.1 mjacob perror("SESIOC_GETOBJSTAT");
135 1.1 mjacob (void) close(fd);
136 1.1 mjacob break;
137 1.1 mjacob }
138 1.1 mjacob if ((ob.cstat[0] & 0xf) == SES_OBJSTAT_OK) {
139 1.1 mjacob if (verbose) {
140 1.1 mjacob fprintf(stdout,
141 1.1 mjacob "Element 0x%x: %s OK (%s)\n",
142 1.1 mjacob ob.obj_id,
143 1.1 mjacob geteltnm(objp[i].object_type),
144 1.1 mjacob stat2ascii(objp[i].object_type,
145 1.1 mjacob ob.cstat));
146 1.1 mjacob }
147 1.1 mjacob continue;
148 1.1 mjacob }
149 1.1 mjacob fprintf(stdout, "Element 0x%x: %s, %s\n",
150 1.1 mjacob ob.obj_id, geteltnm(objp[i].object_type),
151 1.1 mjacob stat2ascii(objp[i].object_type, ob.cstat));
152 1.1 mjacob }
153 1.1 mjacob free(objp);
154 1.1 mjacob (void) close(fd);
155 1.1 mjacob }
156 1.1 mjacob return (errors);
157 1.1 mjacob }
158