xref: /netbsd/usr.sbin/sesd/srcs/sesd.c (revision 6550d01e)
1 /* $NetBSD: sesd.c,v 1.6 2011/01/04 10:10:39 wiz Exp $ */
2 /* $FreeBSD: $ */
3 /* $OpenBSD: $ */
4 /*
5  * Copyright (c) 2000 by Matthew Jacob
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * Alternatively, this software may be distributed under the terms of the
18  * the GNU Public License ("GPL").
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * Matthew Jacob
33  * Feral Software
34  * mjacob@feral.com
35  */
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <syslog.h>
43 #include <sys/ioctl.h>
44 #include SESINC
45 
46 #define	ALLSTAT (SES_ENCSTAT_UNRECOV | SES_ENCSTAT_CRITICAL | \
47 	SES_ENCSTAT_NONCRITICAL | SES_ENCSTAT_INFO)
48 int main __P((int, char **));
49 
50 /*
51  * Monitor named SES devices and note (via syslog) any changes in status.
52  */
53 
54 int
55 main(a, v)
56 	int a;
57 	char **v;
58 {
59 	static const char usage[] =
60 	    "usage: %s [ -d ] [ -t pollinterval ] device [ device ]\n";
61 	int fd, polltime, dev, devbase, nodaemon;
62 	ses_encstat sestat, *carray;
63 
64 	if (a < 2) {
65 		fprintf(stderr, usage, *v);
66 		return (1);
67 	}
68 
69 	devbase = 1;
70 
71 	if (strcmp(v[1], "-d") == 0) {
72 		nodaemon = 1;
73 		devbase++;
74 	} else {
75 		nodaemon = 0;
76 	}
77 
78 	if (a > 2 && strcmp(v[2], "-t") == 0) {
79 		devbase += 2;
80 		polltime = atoi(v[3]);
81 	} else {
82 		polltime = 30;
83 	}
84 
85 	carray = malloc(a);
86 	if (carray == NULL) {
87 		perror("malloc");
88 		return (1);
89 	}
90 	for (dev = devbase; dev < a; dev++)
91 		carray[dev] = (ses_encstat) -1;
92 
93 	/*
94 	 * Check to make sure we can open all devices
95 	 */
96 	for (dev = devbase; dev < a; dev++) {
97 		fd = open(v[dev], O_RDWR);
98 		if (fd < 0) {
99 			perror(v[dev]);
100 			return (1);
101 		}
102 		if (ioctl(fd, SESIOC_INIT, NULL) < 0) {
103 			fprintf(stderr, "%s: SESIOC_INIT fails- %s\n",
104 			    v[dev], strerror(errno));
105 			(void) close(fd);
106 			return (1);
107 		}
108 		(void) close(fd);
109 	}
110 	if (nodaemon == 0) {
111 		if (daemon(0, 0) < 0) {
112 			perror("daemon");
113 			return (1);
114 		}
115 		openlog("sesd", 0, LOG_USER);
116 	} else {
117 		openlog("sesd", LOG_PERROR, LOG_USER);
118 	}
119 
120 	for (;;) {
121 		for (dev = devbase; dev < a; dev++) {
122 			fd = open(v[dev], O_RDWR);
123 			if (fd < 0) {
124 				syslog(LOG_ERR, "%s: %m", v[dev]);
125 				continue;
126 			}
127 
128 			/*
129 			 * Get the actual current enclosure status.
130 			 */
131 			if (ioctl(fd, SESIOC_GETENCSTAT, (caddr_t) &sestat) < 0) {
132 				syslog(LOG_ERR,
133 				    "%s: SESIOC_GETENCSTAT- %m", v[dev]);
134 				(void) close(fd);
135 				continue;
136 			}
137 			(void) close(fd);
138 
139 			if (sestat == carray[dev])
140 				continue;
141 
142 			carray[dev] = sestat;
143 			if ((sestat & ALLSTAT) == 0) {
144 				syslog(LOG_NOTICE,
145 				    "%s: Enclosure Status OK", v[dev]);
146 			}
147 			if (sestat & SES_ENCSTAT_INFO) {
148 				syslog(LOG_INFO,
149 				    "%s: Enclosure Status Has Information",
150 				    v[dev]);
151 			}
152 			if (sestat & SES_ENCSTAT_NONCRITICAL) {
153 				syslog(LOG_WARNING,
154 				    "%s: Enclosure Non-Critical", v[dev]);
155 			}
156 			if (sestat & SES_ENCSTAT_CRITICAL) {
157 				syslog(LOG_CRIT,
158 				    "%s: Enclosure Critical", v[dev]);
159 			}
160 			if (sestat & SES_ENCSTAT_UNRECOV) {
161 				syslog(LOG_ALERT,
162 				    "%s: Enclosure Unrecoverable", v[dev]);
163 			}
164 		}
165 		sleep(polltime);
166 	}
167 	/* NOTREACHED */
168 }
169