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