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