sesd.c revision 1.1
1/* $NetBSD: sesd.c,v 1.1 2000/02/21 08:10:30 mjacob 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(a, v) 55 int a; 56 char **v; 57{ 58 static char *usage = 59 "usage: %s [ -d ] [ -t pollinterval ] device [ device ]\n"; 60 int fd, polltime, dev, devbase, nodaemon, bpri; 61 ses_encstat stat, *carray; 62 63 if (a < 2) { 64 fprintf(stderr, usage, *v); 65 return (1); 66 } 67 68 devbase = 1; 69 70 if (strcmp(v[1], "-d") == 0) { 71 nodaemon = 1; 72 devbase++; 73 } else { 74 nodaemon = 0; 75 } 76 77 if (a > 2 && strcmp(v[2], "-t") == 0) { 78 devbase += 2; 79 polltime = atoi(v[3]); 80 } else { 81 polltime = 30; 82 } 83 84 carray = malloc(a); 85 if (carray == NULL) { 86 perror("malloc"); 87 return (1); 88 } 89 for (dev = devbase; dev < a; dev++) 90 carray[dev] = (ses_encstat) -1; 91 92 /* 93 * Check to make sure we can open all devices 94 */ 95 for (dev = devbase; dev < a; dev++) { 96 fd = open(v[dev], O_RDWR); 97 if (fd < 0) { 98 perror(v[dev]); 99 return (1); 100 } 101 if (ioctl(fd, SESIOC_INIT, NULL) < 0) { 102 fprintf(stderr, "%s: SESIOC_INIT fails- %s\n", 103 v[dev], strerror(errno)); 104 return (1); 105 } 106 (void) close(fd); 107 } 108 if (nodaemon == 0) { 109 if (daemon(0, 0) < 0) { 110 perror("daemon"); 111 return (1); 112 } 113 openlog("sesd", LOG_CONS, LOG_USER); 114 } else { 115 openlog("sesd", LOG_CONS|LOG_PERROR, LOG_USER); 116 } 117 118 for (;;) { 119 for (dev = devbase; dev < a; dev++) { 120 char buf[128]; 121 fd = open(v[dev], O_RDWR); 122 if (fd < 0) { 123 syslog(LOG_ERR, "%s: %m", v[dev]); 124 continue; 125 } 126 127 /* 128 * Get the actual current enclosure status. 129 */ 130 if (ioctl(fd, SESIOC_GETENCSTAT, (caddr_t) &stat) < 0) { 131 syslog(LOG_ERR, 132 "%s: SESIOC_GETENCSTAT- %m", v[dev]); 133 (void) close(fd); 134 continue; 135 } 136 (void) close(fd); 137 138 if (stat == carray[dev]) 139 continue; 140 141 carray[dev] = stat; 142 if ((stat & ALLSTAT) == 0) { 143 syslog(LOG_NOTICE, 144 "%s: Enclosure Status OK", v[dev]); 145 } 146 if (stat & SES_ENCSTAT_INFO) { 147 syslog(LOG_INFO, 148 "%s: Enclosure Status Has Information", 149 v[dev]); 150 } 151 if (stat & SES_ENCSTAT_NONCRITICAL) { 152 syslog(LOG_WARNING, 153 "%s: Enclosure Non-Critical", v[dev]); 154 } 155 if (stat & SES_ENCSTAT_CRITICAL) { 156 syslog(LOG_CRIT, 157 "%s: Enclosure Critical", v[dev]); 158 } 159 if (stat & SES_ENCSTAT_UNRECOV) { 160 syslog(LOG_ALERT, 161 "%s: Enclosure Unrecoverable", v[dev]); 162 } 163 } 164 sleep(polltime); 165 } 166 /* NOTREACHED */ 167} 168