sesd.c revision 1.1
11.1Smjacob/* $NetBSD: sesd.c,v 1.1 2000/02/21 08:10:30 mjacob Exp $ */
21.1Smjacob/* $FreeBSD: $ */
31.1Smjacob/* $OpenBSD: $ */
41.1Smjacob/*
51.1Smjacob * Copyright (c) 2000 by Matthew Jacob
61.1Smjacob * All rights reserved.
71.1Smjacob *
81.1Smjacob * Redistribution and use in source and binary forms, with or without
91.1Smjacob * modification, are permitted provided that the following conditions
101.1Smjacob * are met:
111.1Smjacob * 1. Redistributions of source code must retain the above copyright
121.1Smjacob *    notice, this list of conditions, and the following disclaimer,
131.1Smjacob *    without modification, immediately at the beginning of the file.
141.1Smjacob * 2. The name of the author may not be used to endorse or promote products
151.1Smjacob *    derived from this software without specific prior written permission.
161.1Smjacob *
171.1Smjacob * Alternatively, this software may be distributed under the terms of the
181.1Smjacob * the GNU Public License ("GPL").
191.1Smjacob *
201.1Smjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
211.1Smjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221.1Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231.1Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
241.1Smjacob * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251.1Smjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261.1Smjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271.1Smjacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281.1Smjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291.1Smjacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301.1Smjacob * SUCH DAMAGE.
311.1Smjacob *
321.1Smjacob * Matthew Jacob
331.1Smjacob * Feral Software
341.1Smjacob * mjacob@feral.com
351.1Smjacob */
361.1Smjacob#include <unistd.h>
371.1Smjacob#include <stdlib.h>
381.1Smjacob#include <stdio.h>
391.1Smjacob#include <fcntl.h>
401.1Smjacob#include <errno.h>
411.1Smjacob#include <string.h>
421.1Smjacob#include <syslog.h>
431.1Smjacob#include <sys/ioctl.h>
441.1Smjacob#include SESINC
451.1Smjacob
461.1Smjacob#define	ALLSTAT (SES_ENCSTAT_UNRECOV | SES_ENCSTAT_CRITICAL | \
471.1Smjacob	SES_ENCSTAT_NONCRITICAL | SES_ENCSTAT_INFO)
481.1Smjacob
491.1Smjacob/*
501.1Smjacob * Monitor named SES devices and note (via syslog) any changes in status.
511.1Smjacob */
521.1Smjacob
531.1Smjacobint
541.1Smjacobmain(a, v)
551.1Smjacob	int a;
561.1Smjacob	char **v;
571.1Smjacob{
581.1Smjacob	static char *usage =
591.1Smjacob	    "usage: %s [ -d ] [ -t pollinterval ] device [ device ]\n";
601.1Smjacob	int fd, polltime, dev, devbase, nodaemon, bpri;
611.1Smjacob	ses_encstat stat, *carray;
621.1Smjacob
631.1Smjacob	if (a < 2) {
641.1Smjacob		fprintf(stderr, usage, *v);
651.1Smjacob		return (1);
661.1Smjacob	}
671.1Smjacob
681.1Smjacob	devbase = 1;
691.1Smjacob
701.1Smjacob	if (strcmp(v[1], "-d") == 0) {
711.1Smjacob		nodaemon = 1;
721.1Smjacob		devbase++;
731.1Smjacob	} else {
741.1Smjacob		nodaemon = 0;
751.1Smjacob	}
761.1Smjacob
771.1Smjacob	if (a > 2 && strcmp(v[2], "-t") == 0) {
781.1Smjacob		devbase += 2;
791.1Smjacob		polltime = atoi(v[3]);
801.1Smjacob	} else {
811.1Smjacob		polltime = 30;
821.1Smjacob	}
831.1Smjacob
841.1Smjacob	carray = malloc(a);
851.1Smjacob	if (carray == NULL) {
861.1Smjacob		perror("malloc");
871.1Smjacob		return (1);
881.1Smjacob	}
891.1Smjacob	for (dev = devbase; dev < a; dev++)
901.1Smjacob		carray[dev] = (ses_encstat) -1;
911.1Smjacob
921.1Smjacob	/*
931.1Smjacob	 * Check to make sure we can open all devices
941.1Smjacob	 */
951.1Smjacob	for (dev = devbase; dev < a; dev++) {
961.1Smjacob		fd = open(v[dev], O_RDWR);
971.1Smjacob		if (fd < 0) {
981.1Smjacob			perror(v[dev]);
991.1Smjacob			return (1);
1001.1Smjacob		}
1011.1Smjacob		if (ioctl(fd, SESIOC_INIT, NULL) < 0) {
1021.1Smjacob			fprintf(stderr, "%s: SESIOC_INIT fails- %s\n",
1031.1Smjacob			    v[dev], strerror(errno));
1041.1Smjacob			return (1);
1051.1Smjacob		}
1061.1Smjacob		(void) close(fd);
1071.1Smjacob	}
1081.1Smjacob	if (nodaemon == 0) {
1091.1Smjacob		if (daemon(0, 0) < 0) {
1101.1Smjacob			perror("daemon");
1111.1Smjacob			return (1);
1121.1Smjacob		}
1131.1Smjacob		openlog("sesd", LOG_CONS, LOG_USER);
1141.1Smjacob	} else {
1151.1Smjacob		openlog("sesd", LOG_CONS|LOG_PERROR, LOG_USER);
1161.1Smjacob	}
1171.1Smjacob
1181.1Smjacob	for (;;) {
1191.1Smjacob		for (dev = devbase; dev < a; dev++) {
1201.1Smjacob			char buf[128];
1211.1Smjacob			fd = open(v[dev], O_RDWR);
1221.1Smjacob			if (fd < 0) {
1231.1Smjacob				syslog(LOG_ERR, "%s: %m", v[dev]);
1241.1Smjacob				continue;
1251.1Smjacob			}
1261.1Smjacob
1271.1Smjacob			/*
1281.1Smjacob			 * Get the actual current enclosure status.
1291.1Smjacob			 */
1301.1Smjacob			if (ioctl(fd, SESIOC_GETENCSTAT, (caddr_t) &stat) < 0) {
1311.1Smjacob				syslog(LOG_ERR,
1321.1Smjacob				    "%s: SESIOC_GETENCSTAT- %m", v[dev]);
1331.1Smjacob				(void) close(fd);
1341.1Smjacob				continue;
1351.1Smjacob			}
1361.1Smjacob			(void) close(fd);
1371.1Smjacob
1381.1Smjacob			if (stat == carray[dev])
1391.1Smjacob				continue;
1401.1Smjacob
1411.1Smjacob			carray[dev] = stat;
1421.1Smjacob			if ((stat & ALLSTAT) == 0) {
1431.1Smjacob				syslog(LOG_NOTICE,
1441.1Smjacob				    "%s: Enclosure Status OK", v[dev]);
1451.1Smjacob			}
1461.1Smjacob			if (stat & SES_ENCSTAT_INFO) {
1471.1Smjacob				syslog(LOG_INFO,
1481.1Smjacob				    "%s: Enclosure Status Has Information",
1491.1Smjacob				    v[dev]);
1501.1Smjacob			}
1511.1Smjacob			if (stat & SES_ENCSTAT_NONCRITICAL) {
1521.1Smjacob				syslog(LOG_WARNING,
1531.1Smjacob				    "%s: Enclosure Non-Critical", v[dev]);
1541.1Smjacob			}
1551.1Smjacob			if (stat & SES_ENCSTAT_CRITICAL) {
1561.1Smjacob				syslog(LOG_CRIT,
1571.1Smjacob				    "%s: Enclosure Critical", v[dev]);
1581.1Smjacob			}
1591.1Smjacob			if (stat & SES_ENCSTAT_UNRECOV) {
1601.1Smjacob				syslog(LOG_ALERT,
1611.1Smjacob				    "%s: Enclosure Unrecoverable", v[dev]);
1621.1Smjacob			}
1631.1Smjacob		}
1641.1Smjacob		sleep(polltime);
1651.1Smjacob	}
1661.1Smjacob	/* NOTREACHED */
1671.1Smjacob}
168