getencstat.c revision 198934
1/* $FreeBSD: head/share/examples/ses/srcs/getencstat.c 198934 2009-11-04 23:36:23Z delphij $ */
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 <string.h>
39#include <sys/ioctl.h>
40#include <fcntl.h>
41#include SESINC
42
43#include "eltsub.h"
44
45int
46main(int a, char **v)
47{
48	ses_object *objp;
49	ses_objstat ob;
50	int fd, nobj, f, i, verbose, quiet, errors;
51	u_char estat;
52
53	if (a < 2) {
54		fprintf(stderr, "usage: %s [ -v ] device [ device ... ]\n", *v);
55		return (1);
56	}
57	errors = quiet = verbose = 0;
58	if (strcmp(v[1], "-V") == 0) {
59		verbose = 2;
60		v++;
61	} else if (strcmp(v[1], "-v") == 0) {
62		verbose = 1;
63		v++;
64	} else if (strcmp(v[1], "-q") == 0) {
65		quiet = 1;
66		verbose = 0;
67		v++;
68	}
69	while (*++v) {
70
71		fd = open(*v, O_RDONLY);
72		if (fd < 0) {
73			perror(*v);
74			continue;
75		}
76		if (ioctl(fd, SESIOC_GETNOBJ, (caddr_t) &nobj) < 0) {
77			perror("SESIOC_GETNOBJ");
78			(void) close(fd);
79			continue;
80		}
81		if (ioctl(fd, SESIOC_GETENCSTAT, (caddr_t) &estat) < 0) {
82			perror("SESIOC_GETENCSTAT");
83			(void) close(fd);
84			continue;
85		}
86		if ((verbose == 0 || quiet == 1) && estat == 0) {
87			if (quiet == 0)
88				fprintf(stdout, "%s: Enclosure OK\n", *v);
89			(void) close(fd);
90			continue;
91		}
92		fprintf(stdout, "%s: Enclosure Status ", *v);
93		if (estat == 0) {
94			fprintf(stdout, "<OK");
95		} else {
96			errors++;
97			f = '<';
98			if (estat & SES_ENCSTAT_INFO) {
99				fprintf(stdout, "%cINFO", f);
100				f = ',';
101			}
102			if (estat & SES_ENCSTAT_NONCRITICAL) {
103				fprintf(stdout, "%cNONCRITICAL", f);
104				f = ',';
105			}
106			if (estat & SES_ENCSTAT_CRITICAL) {
107				fprintf(stdout, "%cCRITICAL", f);
108				f = ',';
109			}
110			if (estat & SES_ENCSTAT_UNRECOV) {
111				fprintf(stdout, "%cUNRECOV", f);
112				f = ',';
113			}
114		}
115		fprintf(stdout, ">\n");
116		objp = calloc(nobj, sizeof (ses_object));
117		if (objp == NULL) {
118			perror("calloc");
119			(void) close(fd);
120			continue;
121		}
122                if (ioctl(fd, SESIOC_GETOBJMAP, (caddr_t) objp) < 0) {
123                        perror("SESIOC_GETOBJMAP");
124                        (void) close(fd);
125                        continue;
126                }
127		for (i = 0; i < nobj; i++) {
128			ob.obj_id = objp[i].obj_id;
129			if (ioctl(fd, SESIOC_GETOBJSTAT, (caddr_t) &ob) < 0) {
130				perror("SESIOC_GETOBJSTAT");
131				(void) close(fd);
132				break;
133			}
134			if ((ob.cstat[0] & 0xf) == SES_OBJSTAT_OK) {
135				if (verbose) {
136					fprintf(stdout,
137					    "Element 0x%x: %s OK (%s)\n",
138					    ob.obj_id,
139					    geteltnm(objp[i].object_type),
140					    stat2ascii(objp[i].object_type,
141					    ob.cstat));
142				}
143				continue;
144			}
145			fprintf(stdout, "Element 0x%x: %s, %s\n",
146			    ob.obj_id, geteltnm(objp[i].object_type),
147			    stat2ascii(objp[i].object_type, ob.cstat));
148		}
149		free(objp);
150		(void) close(fd);
151	}
152	return (errors);
153}
154