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