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