11.9Ssevan/* $NetBSD: sesd.c,v 1.9 2018/01/23 21:06:26 sevan 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.9Ssevanmain(int a, char *v[]) 551.1Smjacob{ 561.3Sis static const char usage[] = 571.8Swiz "usage: %s [-d] [-t pollinterval] device [device ...]\n"; 581.7Swiz int c, fd, polltime, dev, nodaemon; 591.5Slukem ses_encstat sestat, *carray; 601.1Smjacob 611.1Smjacob if (a < 2) { 621.1Smjacob fprintf(stderr, usage, *v); 631.1Smjacob return (1); 641.1Smjacob } 651.1Smjacob 661.7Swiz nodaemon = 0; 671.7Swiz polltime = 30; 681.7Swiz while ((c = getopt(a, v, "dt:")) != -1) { 691.7Swiz switch (c) { 701.7Swiz case 'd': 711.7Swiz nodaemon = 1; 721.7Swiz break; 731.7Swiz case 't': 741.7Swiz polltime = atoi(optarg); 751.7Swiz break; 761.7Swiz default: 771.7Swiz fprintf(stderr, usage, *v); 781.7Swiz return (1); 791.7Swiz } 801.7Swiz } 811.1Smjacob 821.1Smjacob carray = malloc(a); 831.1Smjacob if (carray == NULL) { 841.1Smjacob perror("malloc"); 851.1Smjacob return (1); 861.1Smjacob } 871.7Swiz for (dev = optind; dev < a; dev++) 881.1Smjacob carray[dev] = (ses_encstat) -1; 891.1Smjacob 901.1Smjacob /* 911.1Smjacob * Check to make sure we can open all devices 921.1Smjacob */ 931.7Swiz for (dev = optind; dev < a; dev++) { 941.1Smjacob fd = open(v[dev], O_RDWR); 951.1Smjacob if (fd < 0) { 961.1Smjacob perror(v[dev]); 971.1Smjacob return (1); 981.1Smjacob } 991.1Smjacob if (ioctl(fd, SESIOC_INIT, NULL) < 0) { 1001.1Smjacob fprintf(stderr, "%s: SESIOC_INIT fails- %s\n", 1011.1Smjacob v[dev], strerror(errno)); 1021.6Swiz (void) close(fd); 1031.1Smjacob return (1); 1041.1Smjacob } 1051.1Smjacob (void) close(fd); 1061.1Smjacob } 1071.1Smjacob if (nodaemon == 0) { 1081.1Smjacob if (daemon(0, 0) < 0) { 1091.1Smjacob perror("daemon"); 1101.1Smjacob return (1); 1111.1Smjacob } 1121.4Slukem openlog("sesd", 0, LOG_USER); 1131.1Smjacob } else { 1141.4Slukem openlog("sesd", LOG_PERROR, LOG_USER); 1151.1Smjacob } 1161.1Smjacob 1171.1Smjacob for (;;) { 1181.7Swiz for (dev = optind; dev < a; dev++) { 1191.1Smjacob fd = open(v[dev], O_RDWR); 1201.1Smjacob if (fd < 0) { 1211.1Smjacob syslog(LOG_ERR, "%s: %m", v[dev]); 1221.1Smjacob continue; 1231.1Smjacob } 1241.1Smjacob 1251.1Smjacob /* 1261.1Smjacob * Get the actual current enclosure status. 1271.1Smjacob */ 1281.5Slukem if (ioctl(fd, SESIOC_GETENCSTAT, (caddr_t) &sestat) < 0) { 1291.1Smjacob syslog(LOG_ERR, 1301.1Smjacob "%s: SESIOC_GETENCSTAT- %m", v[dev]); 1311.1Smjacob (void) close(fd); 1321.1Smjacob continue; 1331.1Smjacob } 1341.1Smjacob (void) close(fd); 1351.1Smjacob 1361.5Slukem if (sestat == carray[dev]) 1371.1Smjacob continue; 1381.1Smjacob 1391.5Slukem carray[dev] = sestat; 1401.5Slukem if ((sestat & ALLSTAT) == 0) { 1411.1Smjacob syslog(LOG_NOTICE, 1421.1Smjacob "%s: Enclosure Status OK", v[dev]); 1431.1Smjacob } 1441.5Slukem if (sestat & SES_ENCSTAT_INFO) { 1451.1Smjacob syslog(LOG_INFO, 1461.1Smjacob "%s: Enclosure Status Has Information", 1471.1Smjacob v[dev]); 1481.1Smjacob } 1491.5Slukem if (sestat & SES_ENCSTAT_NONCRITICAL) { 1501.1Smjacob syslog(LOG_WARNING, 1511.1Smjacob "%s: Enclosure Non-Critical", v[dev]); 1521.1Smjacob } 1531.5Slukem if (sestat & SES_ENCSTAT_CRITICAL) { 1541.1Smjacob syslog(LOG_CRIT, 1551.1Smjacob "%s: Enclosure Critical", v[dev]); 1561.1Smjacob } 1571.5Slukem if (sestat & SES_ENCSTAT_UNRECOV) { 1581.1Smjacob syslog(LOG_ALERT, 1591.1Smjacob "%s: Enclosure Unrecoverable", v[dev]); 1601.1Smjacob } 1611.1Smjacob } 1621.1Smjacob sleep(polltime); 1631.1Smjacob } 1641.1Smjacob /* NOTREACHED */ 1651.1Smjacob} 166