Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.sbin / sesd / srcs / sesd.c
blob86356d299ed6676807c76b12d00b6fcdfd2dd279
1 /* $NetBSD: sesd.c,v 1.4 2001/01/11 02:46:21 lukem Exp $ */
2 /* $FreeBSD: $ */
3 /* $OpenBSD: $ */
4 /*
5 * Copyright (c) 2000 by Matthew Jacob
6 * All rights reserved.
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.
17 * Alternatively, this software may be distributed under the terms of the
18 * the GNU Public License ("GPL").
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.
32 * Matthew Jacob
33 * Feral Software
34 * mjacob@feral.com
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
46 #define ALLSTAT (SES_ENCSTAT_UNRECOV | SES_ENCSTAT_CRITICAL | \
47 SES_ENCSTAT_NONCRITICAL | SES_ENCSTAT_INFO)
48 int main __P((int, char **));
51 * Monitor named SES devices and note (via syslog) any changes in status.
54 int
55 main(a, v)
56 int a;
57 char **v;
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;
64 if (a < 2) {
65 fprintf(stderr, usage, *v);
66 return (1);
69 devbase = 1;
71 if (strcmp(v[1], "-d") == 0) {
72 nodaemon = 1;
73 devbase++;
74 } else {
75 nodaemon = 0;
78 if (a > 2 && strcmp(v[2], "-t") == 0) {
79 devbase += 2;
80 polltime = atoi(v[3]);
81 } else {
82 polltime = 30;
85 carray = malloc(a);
86 if (carray == NULL) {
87 perror("malloc");
88 return (1);
90 for (dev = devbase; dev < a; dev++)
91 carray[dev] = (ses_encstat) -1;
94 * Check to make sure we can open all devices
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);
102 if (ioctl(fd, SESIOC_INIT, NULL) < 0) {
103 fprintf(stderr, "%s: SESIOC_INIT fails- %s\n",
104 v[dev], strerror(errno));
105 return (1);
107 (void) close(fd);
109 if (nodaemon == 0) {
110 if (daemon(0, 0) < 0) {
111 perror("daemon");
112 return (1);
114 openlog("sesd", 0, LOG_USER);
115 } else {
116 openlog("sesd", LOG_PERROR, LOG_USER);
119 for (;;) {
120 for (dev = devbase; dev < a; dev++) {
121 fd = open(v[dev], O_RDWR);
122 if (fd < 0) {
123 syslog(LOG_ERR, "%s: %m", v[dev]);
124 continue;
128 * Get the actual current enclosure status.
130 if (ioctl(fd, SESIOC_GETENCSTAT, (caddr_t) &sestat) < 0) {
131 syslog(LOG_ERR,
132 "%s: SESIOC_GETENCSTAT- %m", v[dev]);
133 (void) close(fd);
134 continue;
136 (void) close(fd);
138 if (sestat == carray[dev])
139 continue;
141 carray[dev] = sestat;
142 if ((sestat & ALLSTAT) == 0) {
143 syslog(LOG_NOTICE,
144 "%s: Enclosure Status OK", v[dev]);
146 if (sestat & SES_ENCSTAT_INFO) {
147 syslog(LOG_INFO,
148 "%s: Enclosure Status Has Information",
149 v[dev]);
151 if (sestat & SES_ENCSTAT_NONCRITICAL) {
152 syslog(LOG_WARNING,
153 "%s: Enclosure Non-Critical", v[dev]);
155 if (sestat & SES_ENCSTAT_CRITICAL) {
156 syslog(LOG_CRIT,
157 "%s: Enclosure Critical", v[dev]);
159 if (sestat & SES_ENCSTAT_UNRECOV) {
160 syslog(LOG_ALERT,
161 "%s: Enclosure Unrecoverable", v[dev]);
164 sleep(polltime);
166 /* NOTREACHED */