]>
git.wh0rd.org - sysvinit.git/blob - obsolete/powerd.c
2 * powerd Monitor the DCD line of a serial port connected to
3 * an UPS. If the power goes down, notify init.
4 * If the power comes up again, notify init again.
5 * As long as the power is OK, the DCD line should be
6 * "HIGH". When the power fails, DCD should go "LOW".
7 * Powerd keeps DTR high so that you can connect
8 * DCD and DTR with a resistor of 10 Kilo Ohm and let the
9 * UPS or some relais pull the DCD line to ground.
10 * You also need to connect DTR and DSR together. This
11 * way, powerd can check now and then if DSR is high
12 * so it knows the UPS is connected!!
14 * Usage: powerd /dev/cua4 (or any other serial device).
16 * Author: Miquel van Smoorenburg, <miquels@drinkel.cistron.nl>.
18 * Version: 1.31, 29-Feb-1996.
20 * This program was originally written for my employer,
21 * ** Cistron Electronics **
22 * who has given kind permission to release this program
23 * for general puppose.
25 * Copyright (C) 1991-1996 Cistron Electronics.
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
42 /* Use the new way of communicating with init. */
45 #include <sys/types.h>
47 #include <sys/ioctl.h>
62 # define SIGPWR SIGUSR1
71 /* Tell init the power has either gone or is back. */
77 struct init_request req
;
79 /* Fill out the request struct. */
80 memset(&req
, 0, sizeof(req
));
81 req
.magic
= INIT_MAGIC
;
82 req
.cmd
= ok
? INIT_CMD_POWEROK
: INIT_CMD_POWERFAIL
;
84 /* Open the fifo (with timeout) */
85 signal(SIGALRM
, alrm_handler
);
87 if ((fd
= open(INIT_FIFO
, O_WRONLY
)) >= 0
88 && write(fd
, &req
, sizeof(req
)) == sizeof(req
)) {
92 /* Fall through to the old method.. */
95 /* Create an info file for init. */
97 if ((fd
= open(PWRSTAT
, O_CREAT
|O_WRONLY
, 0644)) >= 0) {
101 write(fd
, "FAIL\n", 5);
108 int main(int argc
, char **argv
)
111 int dtr_bit
= TIOCM_DTR
;
113 int status
, oldstat
= -1;
118 fprintf(stderr
, "Usage: powerd <device>\n");
123 openlog("powerd", LOG_CONS
|LOG_PERROR
, LOG_DAEMON
);
125 /* Open monitor device. */
126 if ((fd
= open(argv
[1], O_RDWR
| O_NDELAY
)) < 0) {
127 syslog(LOG_ERR
, "%s: %s", argv
[1], sys_errlist
[errno
]);
132 /* Line is opened, so DTR is high. Force it anyway to be sure. */
133 ioctl(fd
, TIOCMBIS
, &dtr_bit
);
142 syslog(LOG_ERR
, "can't fork.");
145 default: /* Parent */
150 /* Restart syslog. */
151 openlog("powerd", LOG_CONS
, LOG_DAEMON
);
153 /* Now sample the DCD line. */
155 /* Get the status. */
156 ioctl(fd
, TIOCMGET
, &flags
);
158 /* Check the connection: DSR should be high. */
160 while((flags
& TIOCM_DSR
) == 0) {
161 /* Keep on trying, and warn every two minutes. */
162 if ((tries
% 60) == 0)
163 syslog(LOG_ALERT
, "UPS connection error");
166 ioctl(fd
, TIOCMGET
, &flags
);
169 syslog(LOG_ALERT
, "UPS connection OK");
171 /* Calculate present status. */
172 status
= (flags
& TIOCM_CAR
);
174 /* Did DCD drop to zero? Then the power has failed. */
175 if (oldstat
!= 0 && status
== 0) {
184 /* Did DCD come up again? Then the power is back. */
185 if (oldstat
== 0 && status
> 0) {
194 /* Reset count, remember status and sleep 2 seconds. */