xref: /freebsd/share/examples/ses/srcs/getencstat.c (revision f6ad3f237a54ef1465271c7e96bf0f2e4f9277e5)
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>
36*f6ad3f23SAlexander Motin #include <stddef.h>
37*f6ad3f23SAlexander Motin #include <stdint.h>
38daf1cffcSMatt Jacob #include <stdlib.h>
39daf1cffcSMatt Jacob #include <stdio.h>
40c2b1d5a3SXin LI #include <string.h>
41daf1cffcSMatt Jacob #include <sys/ioctl.h>
42daf1cffcSMatt Jacob #include <fcntl.h>
43*f6ad3f23SAlexander Motin #include <cam/scsi/scsi_all.h>
44*f6ad3f23SAlexander Motin #include <cam/scsi/scsi_enc.h>
45daf1cffcSMatt Jacob 
462804a96aSXin LI #include "eltsub.h"
47daf1cffcSMatt Jacob 
48daf1cffcSMatt Jacob int
492804a96aSXin LI main(int a, char **v)
50daf1cffcSMatt Jacob {
51*f6ad3f23SAlexander Motin 	encioc_element_t *objp;
52*f6ad3f23SAlexander Motin 	encioc_elm_status_t ob;
53*f6ad3f23SAlexander Motin 	encioc_elm_desc_t objd;
54*f6ad3f23SAlexander Motin 	encioc_elm_devnames_t objdn;
55daf1cffcSMatt Jacob 	int fd, nobj, f, i, verbose, quiet, errors;
56daf1cffcSMatt Jacob 	u_char estat;
57daf1cffcSMatt Jacob 
58daf1cffcSMatt Jacob 	if (a < 2) {
59daf1cffcSMatt Jacob 		fprintf(stderr, "usage: %s [ -v ] device [ device ... ]\n", *v);
60daf1cffcSMatt Jacob 		return (1);
61daf1cffcSMatt Jacob 	}
62daf1cffcSMatt Jacob 	errors = quiet = verbose = 0;
63daf1cffcSMatt Jacob 	if (strcmp(v[1], "-V") == 0) {
64daf1cffcSMatt Jacob 		verbose = 2;
65daf1cffcSMatt Jacob 		v++;
66daf1cffcSMatt Jacob 	} else if (strcmp(v[1], "-v") == 0) {
67daf1cffcSMatt Jacob 		verbose = 1;
68daf1cffcSMatt Jacob 		v++;
69daf1cffcSMatt Jacob 	} else if (strcmp(v[1], "-q") == 0) {
70daf1cffcSMatt Jacob 		quiet = 1;
71daf1cffcSMatt Jacob 		verbose = 0;
72daf1cffcSMatt Jacob 		v++;
73daf1cffcSMatt Jacob 	}
74daf1cffcSMatt Jacob 	while (*++v) {
75daf1cffcSMatt Jacob 
76daf1cffcSMatt Jacob 		fd = open(*v, O_RDONLY);
77daf1cffcSMatt Jacob 		if (fd < 0) {
78daf1cffcSMatt Jacob 			perror(*v);
79daf1cffcSMatt Jacob 			continue;
80daf1cffcSMatt Jacob 		}
81*f6ad3f23SAlexander Motin 		if (ioctl(fd, ENCIOC_GETNELM, (caddr_t) &nobj) < 0) {
82*f6ad3f23SAlexander Motin 			perror("ENCIOC_GETNELM");
83daf1cffcSMatt Jacob 			(void) close(fd);
84daf1cffcSMatt Jacob 			continue;
85daf1cffcSMatt Jacob 		}
86*f6ad3f23SAlexander Motin 		if (ioctl(fd, ENCIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
87*f6ad3f23SAlexander Motin 			perror("ENCIOC_GETENCSTAT");
88daf1cffcSMatt Jacob 			(void) close(fd);
89daf1cffcSMatt Jacob 			continue;
90daf1cffcSMatt Jacob 		}
91daf1cffcSMatt Jacob 		if ((verbose == 0 || quiet == 1) && estat == 0) {
92daf1cffcSMatt Jacob 			if (quiet == 0)
93daf1cffcSMatt Jacob 				fprintf(stdout, "%s: Enclosure OK\n", *v);
94daf1cffcSMatt Jacob 			(void) close(fd);
95daf1cffcSMatt Jacob 			continue;
96daf1cffcSMatt Jacob 		}
97daf1cffcSMatt Jacob 		fprintf(stdout, "%s: Enclosure Status ", *v);
98daf1cffcSMatt Jacob 		if (estat == 0) {
99daf1cffcSMatt Jacob 			fprintf(stdout, "<OK");
100daf1cffcSMatt Jacob 		} else {
101daf1cffcSMatt Jacob 			errors++;
102daf1cffcSMatt Jacob 			f = '<';
103daf1cffcSMatt Jacob 			if (estat & SES_ENCSTAT_INFO) {
104daf1cffcSMatt Jacob 				fprintf(stdout, "%cINFO", f);
105daf1cffcSMatt Jacob 				f = ',';
106daf1cffcSMatt Jacob 			}
107daf1cffcSMatt Jacob 			if (estat & SES_ENCSTAT_NONCRITICAL) {
108daf1cffcSMatt Jacob 				fprintf(stdout, "%cNONCRITICAL", f);
109daf1cffcSMatt Jacob 				f = ',';
110daf1cffcSMatt Jacob 			}
111daf1cffcSMatt Jacob 			if (estat & SES_ENCSTAT_CRITICAL) {
112daf1cffcSMatt Jacob 				fprintf(stdout, "%cCRITICAL", f);
113daf1cffcSMatt Jacob 				f = ',';
114daf1cffcSMatt Jacob 			}
115daf1cffcSMatt Jacob 			if (estat & SES_ENCSTAT_UNRECOV) {
116daf1cffcSMatt Jacob 				fprintf(stdout, "%cUNRECOV", f);
117daf1cffcSMatt Jacob 				f = ',';
118daf1cffcSMatt Jacob 			}
119daf1cffcSMatt Jacob 		}
120daf1cffcSMatt Jacob 		fprintf(stdout, ">\n");
121*f6ad3f23SAlexander Motin 		objp = calloc(nobj, sizeof (encioc_element_t));
122daf1cffcSMatt Jacob 		if (objp == NULL) {
123daf1cffcSMatt Jacob 			perror("calloc");
124daf1cffcSMatt Jacob 			(void) close(fd);
125daf1cffcSMatt Jacob 			continue;
126daf1cffcSMatt Jacob 		}
127*f6ad3f23SAlexander Motin                 if (ioctl(fd, ENCIOC_GETELMMAP, (caddr_t) objp) < 0) {
128*f6ad3f23SAlexander Motin                         perror("ENCIOC_GETELMMAP");
129daf1cffcSMatt Jacob                         (void) close(fd);
130daf1cffcSMatt Jacob                         continue;
131daf1cffcSMatt Jacob                 }
132daf1cffcSMatt Jacob 		for (i = 0; i < nobj; i++) {
133*f6ad3f23SAlexander Motin 			ob.elm_idx = objp[i].elm_idx;
134*f6ad3f23SAlexander Motin 			if (ioctl(fd, ENCIOC_GETELMSTAT, (caddr_t) &ob) < 0) {
135*f6ad3f23SAlexander Motin 				perror("ENCIOC_GETELMSTAT");
136daf1cffcSMatt Jacob 				(void) close(fd);
137daf1cffcSMatt Jacob 				break;
138daf1cffcSMatt Jacob 			}
139*f6ad3f23SAlexander Motin 			bzero(&objd, sizeof(objd));
140*f6ad3f23SAlexander Motin 			objd.elm_idx = objp[i].elm_idx;
141*f6ad3f23SAlexander Motin 			objd.elm_desc_len = UINT16_MAX;
142*f6ad3f23SAlexander Motin 			objd.elm_desc_str = calloc(UINT16_MAX, sizeof(char));
143*f6ad3f23SAlexander Motin 			if (objd.elm_desc_str == NULL) {
144*f6ad3f23SAlexander Motin 				perror("calloc");
145*f6ad3f23SAlexander Motin 				(void) close(fd);
146daf1cffcSMatt Jacob 				continue;
147daf1cffcSMatt Jacob 			}
148*f6ad3f23SAlexander Motin 			if (ioctl(fd, ENCIOC_GETELMDESC, (caddr_t)&objd) < 0) {
149*f6ad3f23SAlexander Motin 				perror("ENCIOC_GETELMDESC");
150*f6ad3f23SAlexander Motin 				(void) close(fd);
151*f6ad3f23SAlexander Motin 				break;
152*f6ad3f23SAlexander Motin 			}
153*f6ad3f23SAlexander Motin 			bzero(&objdn, sizeof(objdn));
154*f6ad3f23SAlexander Motin 			objdn.elm_idx = objp[i].elm_idx;
155*f6ad3f23SAlexander Motin 			objdn.elm_names_size = 128;
156*f6ad3f23SAlexander Motin 			objdn.elm_devnames = calloc(128, sizeof(char));
157*f6ad3f23SAlexander Motin 			if (objdn.elm_devnames == NULL) {
158*f6ad3f23SAlexander Motin 				perror("calloc");
159*f6ad3f23SAlexander Motin 				(void) close(fd);
160*f6ad3f23SAlexander Motin 				break;
161*f6ad3f23SAlexander Motin 			}
162*f6ad3f23SAlexander Motin 			/*
163*f6ad3f23SAlexander Motin 			 * This ioctl isn't critical and has a good chance
164*f6ad3f23SAlexander Motin 			 * of returning -1.
165*f6ad3f23SAlexander Motin 			 */
166*f6ad3f23SAlexander Motin 			(void)ioctl(fd, ENCIOC_GETELMDEVNAMES, (caddr_t)&objdn);
167*f6ad3f23SAlexander Motin 			fprintf(stdout, "Element 0x%x: %s", ob.elm_idx,
168*f6ad3f23SAlexander Motin 			    geteltnm(objp[i].elm_type));
169*f6ad3f23SAlexander Motin 			fprintf(stdout, ", %s",
170*f6ad3f23SAlexander Motin 			    stat2ascii(objp[i].elm_type, ob.cstat));
171*f6ad3f23SAlexander Motin 			if (objd.elm_desc_len > 0)
172*f6ad3f23SAlexander Motin 				fprintf(stdout, ", descriptor: '%s'",
173*f6ad3f23SAlexander Motin 				    objd.elm_desc_str);
174*f6ad3f23SAlexander Motin 			if (objdn.elm_names_len > 0)
175*f6ad3f23SAlexander Motin 				fprintf(stdout, ", dev: '%s'",
176*f6ad3f23SAlexander Motin 				    objdn.elm_devnames);
177*f6ad3f23SAlexander Motin 			fprintf(stdout, "\n");
178*f6ad3f23SAlexander Motin 			free(objdn.elm_devnames);
179daf1cffcSMatt Jacob 		}
180daf1cffcSMatt Jacob 		free(objp);
181daf1cffcSMatt Jacob 		(void) close(fd);
182daf1cffcSMatt Jacob 	}
183daf1cffcSMatt Jacob 	return (errors);
184daf1cffcSMatt Jacob }
185