sesd.c revision 1.4
11.4Slukem/* $NetBSD: sesd.c,v 1.4 2001/01/11 02:46:21 lukem 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.2Smjacobint main __P((int, char **)); 491.1Smjacob 501.1Smjacob/* 511.1Smjacob * Monitor named SES devices and note (via syslog) any changes in status. 521.1Smjacob */ 531.1Smjacob 541.1Smjacobint 551.1Smjacobmain(a, v) 561.1Smjacob int a; 571.1Smjacob char **v; 581.1Smjacob{ 591.3Sis static const char usage[] = 601.1Smjacob "usage: %s [ -d ] [ -t pollinterval ] device [ device ]\n"; 611.2Smjacob int fd, polltime, dev, devbase, nodaemon; 621.1Smjacob ses_encstat stat, *carray; 631.1Smjacob 641.1Smjacob if (a < 2) { 651.1Smjacob fprintf(stderr, usage, *v); 661.1Smjacob return (1); 671.1Smjacob } 681.1Smjacob 691.1Smjacob devbase = 1; 701.1Smjacob 711.1Smjacob if (strcmp(v[1], "-d") == 0) { 721.1Smjacob nodaemon = 1; 731.1Smjacob devbase++; 741.1Smjacob } else { 751.1Smjacob nodaemon = 0; 761.1Smjacob } 771.1Smjacob 781.1Smjacob if (a > 2 && strcmp(v[2], "-t") == 0) { 791.1Smjacob devbase += 2; 801.1Smjacob polltime = atoi(v[3]); 811.1Smjacob } else { 821.1Smjacob polltime = 30; 831.1Smjacob } 841.1Smjacob 851.1Smjacob carray = malloc(a); 861.1Smjacob if (carray == NULL) { 871.1Smjacob perror("malloc"); 881.1Smjacob return (1); 891.1Smjacob } 901.1Smjacob for (dev = devbase; dev < a; dev++) 911.1Smjacob carray[dev] = (ses_encstat) -1; 921.1Smjacob 931.1Smjacob /* 941.1Smjacob * Check to make sure we can open all devices 951.1Smjacob */ 961.1Smjacob for (dev = devbase; dev < a; dev++) { 971.1Smjacob fd = open(v[dev], O_RDWR); 981.1Smjacob if (fd < 0) { 991.1Smjacob perror(v[dev]); 1001.1Smjacob return (1); 1011.1Smjacob } 1021.1Smjacob if (ioctl(fd, SESIOC_INIT, NULL) < 0) { 1031.1Smjacob fprintf(stderr, "%s: SESIOC_INIT fails- %s\n", 1041.1Smjacob v[dev], strerror(errno)); 1051.1Smjacob return (1); 1061.1Smjacob } 1071.1Smjacob (void) close(fd); 1081.1Smjacob } 1091.1Smjacob if (nodaemon == 0) { 1101.1Smjacob if (daemon(0, 0) < 0) { 1111.1Smjacob perror("daemon"); 1121.1Smjacob return (1); 1131.1Smjacob } 1141.4Slukem openlog("sesd", 0, LOG_USER); 1151.1Smjacob } else { 1161.4Slukem openlog("sesd", LOG_PERROR, LOG_USER); 1171.1Smjacob } 1181.1Smjacob 1191.1Smjacob for (;;) { 1201.1Smjacob for (dev = devbase; dev < a; dev++) { 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