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