sesd.c revision 1.7
11.7Swiz/* $NetBSD: sesd.c,v 1.7 2013/04/07 18:48:24 wiz 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.7Swiz "usage: %s [-d] [-t pollinterval] device [...]\n"; 611.7Swiz int c, fd, polltime, dev, nodaemon; 621.5Slukem ses_encstat sestat, *carray; 631.1Smjacob 641.1Smjacob if (a < 2) { 651.1Smjacob fprintf(stderr, usage, *v); 661.1Smjacob return (1); 671.1Smjacob } 681.1Smjacob 691.7Swiz nodaemon = 0; 701.7Swiz polltime = 30; 711.7Swiz while ((c = getopt(a, v, "dt:")) != -1) { 721.7Swiz switch (c) { 731.7Swiz case 'd': 741.7Swiz nodaemon = 1; 751.7Swiz break; 761.7Swiz case 't': 771.7Swiz polltime = atoi(optarg); 781.7Swiz break; 791.7Swiz default: 801.7Swiz fprintf(stderr, usage, *v); 811.7Swiz return (1); 821.7Swiz } 831.7Swiz } 841.1Smjacob 851.1Smjacob carray = malloc(a); 861.1Smjacob if (carray == NULL) { 871.1Smjacob perror("malloc"); 881.1Smjacob return (1); 891.1Smjacob } 901.7Swiz for (dev = optind; 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.7Swiz for (dev = optind; 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.6Swiz (void) close(fd); 1061.1Smjacob return (1); 1071.1Smjacob } 1081.1Smjacob (void) close(fd); 1091.1Smjacob } 1101.1Smjacob if (nodaemon == 0) { 1111.1Smjacob if (daemon(0, 0) < 0) { 1121.1Smjacob perror("daemon"); 1131.1Smjacob return (1); 1141.1Smjacob } 1151.4Slukem openlog("sesd", 0, LOG_USER); 1161.1Smjacob } else { 1171.4Slukem openlog("sesd", LOG_PERROR, LOG_USER); 1181.1Smjacob } 1191.1Smjacob 1201.1Smjacob for (;;) { 1211.7Swiz for (dev = optind; dev < a; dev++) { 1221.1Smjacob fd = open(v[dev], O_RDWR); 1231.1Smjacob if (fd < 0) { 1241.1Smjacob syslog(LOG_ERR, "%s: %m", v[dev]); 1251.1Smjacob continue; 1261.1Smjacob } 1271.1Smjacob 1281.1Smjacob /* 1291.1Smjacob * Get the actual current enclosure status. 1301.1Smjacob */ 1311.5Slukem if (ioctl(fd, SESIOC_GETENCSTAT, (caddr_t) &sestat) < 0) { 1321.1Smjacob syslog(LOG_ERR, 1331.1Smjacob "%s: SESIOC_GETENCSTAT- %m", v[dev]); 1341.1Smjacob (void) close(fd); 1351.1Smjacob continue; 1361.1Smjacob } 1371.1Smjacob (void) close(fd); 1381.1Smjacob 1391.5Slukem if (sestat == carray[dev]) 1401.1Smjacob continue; 1411.1Smjacob 1421.5Slukem carray[dev] = sestat; 1431.5Slukem if ((sestat & ALLSTAT) == 0) { 1441.1Smjacob syslog(LOG_NOTICE, 1451.1Smjacob "%s: Enclosure Status OK", v[dev]); 1461.1Smjacob } 1471.5Slukem if (sestat & SES_ENCSTAT_INFO) { 1481.1Smjacob syslog(LOG_INFO, 1491.1Smjacob "%s: Enclosure Status Has Information", 1501.1Smjacob v[dev]); 1511.1Smjacob } 1521.5Slukem if (sestat & SES_ENCSTAT_NONCRITICAL) { 1531.1Smjacob syslog(LOG_WARNING, 1541.1Smjacob "%s: Enclosure Non-Critical", v[dev]); 1551.1Smjacob } 1561.5Slukem if (sestat & SES_ENCSTAT_CRITICAL) { 1571.1Smjacob syslog(LOG_CRIT, 1581.1Smjacob "%s: Enclosure Critical", v[dev]); 1591.1Smjacob } 1601.5Slukem if (sestat & SES_ENCSTAT_UNRECOV) { 1611.1Smjacob syslog(LOG_ALERT, 1621.1Smjacob "%s: Enclosure Unrecoverable", v[dev]); 1631.1Smjacob } 1641.1Smjacob } 1651.1Smjacob sleep(polltime); 1661.1Smjacob } 1671.1Smjacob /* NOTREACHED */ 1681.1Smjacob} 169