1/* $NetBSD: sesd.c,v 1.9 2018/01/23 21:06:26 sevan Exp $ */ 2/* $FreeBSD: $ */ 3/* $OpenBSD: $ */ 4/* 5 * Copyright (c) 2000 by Matthew Jacob 6 * All rights reserved. 7 * 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. 16 * 17 * Alternatively, this software may be distributed under the terms of the 18 * the GNU Public License ("GPL"). 19 * 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. 31 * 32 * Matthew Jacob 33 * Feral Software 34 * mjacob@feral.com 35 */ 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 45 46#define ALLSTAT (SES_ENCSTAT_UNRECOV | SES_ENCSTAT_CRITICAL | \ 47 SES_ENCSTAT_NONCRITICAL | SES_ENCSTAT_INFO) 48 49/* 50 * Monitor named SES devices and note (via syslog) any changes in status. 51 */ 52 53int 54main(int a, char *v[]) 55{ 56 static const char usage[] = 57 "usage: %s [-d] [-t pollinterval] device [device ...]\n"; 58 int c, fd, polltime, dev, nodaemon; 59 ses_encstat sestat, *carray; 60 61 if (a < 2) { 62 fprintf(stderr, usage, *v); 63 return (1); 64 } 65 66 nodaemon = 0; 67 polltime = 30; 68 while ((c = getopt(a, v, "dt:")) != -1) { 69 switch (c) { 70 case 'd': 71 nodaemon = 1; 72 break; 73 case 't': 74 polltime = atoi(optarg); 75 break; 76 default: 77 fprintf(stderr, usage, *v); 78 return (1); 79 } 80 } 81 82 carray = malloc(a); 83 if (carray == NULL) { 84 perror("malloc"); 85 return (1); 86 } 87 for (dev = optind; dev < a; dev++) 88 carray[dev] = (ses_encstat) -1; 89 90 /* 91 * Check to make sure we can open all devices 92 */ 93 for (dev = optind; dev < a; dev++) { 94 fd = open(v[dev], O_RDWR); 95 if (fd < 0) { 96 perror(v[dev]); 97 return (1); 98 } 99 if (ioctl(fd, SESIOC_INIT, NULL) < 0) { 100 fprintf(stderr, "%s: SESIOC_INIT fails- %s\n", 101 v[dev], strerror(errno)); 102 (void) close(fd); 103 return (1); 104 } 105 (void) close(fd); 106 } 107 if (nodaemon == 0) { 108 if (daemon(0, 0) < 0) { 109 perror("daemon"); 110 return (1); 111 } 112 openlog("sesd", 0, LOG_USER); 113 } else { 114 openlog("sesd", LOG_PERROR, LOG_USER); 115 } 116 117 for (;;) { 118 for (dev = optind; dev < a; dev++) { 119 fd = open(v[dev], O_RDWR); 120 if (fd < 0) { 121 syslog(LOG_ERR, "%s: %m", v[dev]); 122 continue; 123 } 124 125 /* 126 * Get the actual current enclosure status. 127 */ 128 if (ioctl(fd, SESIOC_GETENCSTAT, (caddr_t) &sestat) < 0) { 129 syslog(LOG_ERR, 130 "%s: SESIOC_GETENCSTAT- %m", v[dev]); 131 (void) close(fd); 132 continue; 133 } 134 (void) close(fd); 135 136 if (sestat == carray[dev]) 137 continue; 138 139 carray[dev] = sestat; 140 if ((sestat & ALLSTAT) == 0) { 141 syslog(LOG_NOTICE, 142 "%s: Enclosure Status OK", v[dev]); 143 } 144 if (sestat & SES_ENCSTAT_INFO) { 145 syslog(LOG_INFO, 146 "%s: Enclosure Status Has Information", 147 v[dev]); 148 } 149 if (sestat & SES_ENCSTAT_NONCRITICAL) { 150 syslog(LOG_WARNING, 151 "%s: Enclosure Non-Critical", v[dev]); 152 } 153 if (sestat & SES_ENCSTAT_CRITICAL) { 154 syslog(LOG_CRIT, 155 "%s: Enclosure Critical", v[dev]); 156 } 157 if (sestat & SES_ENCSTAT_UNRECOV) { 158 syslog(LOG_ALERT, 159 "%s: Enclosure Unrecoverable", v[dev]); 160 } 161 } 162 sleep(polltime); 163 } 164 /* NOTREACHED */ 165} 166