HOWTO: UPS + Modem, no cable messing

HOWTO: UPS + Modem, no cable messing

Post by Chuc » Wed, 10 Mar 1999 04:00:00

I finally buy a UPS (500 VA) for my Linux system, but it
doesn't have a RS232 PORT (a very cheap model here in Taiwan).

I dig out some old articles and found 2 of them: one was from (HUANG.YU-HSIN) and the other one was from

Mr. lloyd contributed a modified program to suit his need for his
UPS (need to modify the RS232 cable), while Mr.thhsieh used an old
14400 modem to check power status, after he burned one of his UPS
when testing cable changing.

I adopt their source codes and idea and here is my method: Any UPS
with or without RS232 port, no cable messing.


  1.An old 14400 modem, a normal RS232 cable,and the modem power is not
    connected to UPS. You don't need to plug in telephone line (^^)
  2.UPS (of course ^^)

  I assume that you will connect your old modem to COM2 (i.e. /dev/ttyS1)


1.Compile the attached source codes with

       gcc -o upscheck upscheck.c

  Put the executable "upscheck" in /sbin

2.Modify /etc/inittab. If you got a normal Linux setup, you should have
  the following lines in  /etc/inittab:

     # What to do when power fails (shutdown to single user).
     # shutdown waiting time could be modified, e.g. +10 for 10 minutes
     pf::powerfail:/sbin/shutdown -f +5 "THE POWER IS FAILING"

     # If power is back before shutdown, cancel the running shutdown.
     pg:0123456:powerokwait:/sbin/shutdown -c "THE POWER IS BACK"

     # If power comes back in single user mode, return to multi user mode.
     ps:S:powerokwait:/sbin/init 5

  If you do modify this file, use

          kill -HUP 1
  to restart init

Test: You Linux should be connected to UPS now. Turn on your modem
      by the modem power switch. Then use

        /sbin/upscheck /dev/ttyS1 2 &

      to start the daemon program.

      Now, turn OFF your modem. You should see warning message
      of shutting down in 5 minutes.

      Turn modem ON again, You should see the "THE POWER IS BACK" message
      and the shutdown is canceled.

Success: If everything goes fine, put the following line in
         /etc/rc.d/rc.local :

            /sbin/upscheck /dev/ttyS1 2 &

        then it will be executed at the next reboot?C


------------------- upscheck.c -------------------- begin
   * upscheck     Check how UPS & computer communicate.
   * Usage:       upscheck <device> <bits to set>
   *              For example, upscheck /dev/cua4 4 to set bit 3 &
   *              monitor /dev/cua4.
   * Author:      Harvey J. Stein <>
   *              (but really just a minor modification of Miquel van
   *              Smoorenburg's <> powerd.c
   * Version:     1.0 19940802
  #include <sys/types.h>
  #include <sys/ioctl.h>
  #include <fcntl.h>
  #include <errno.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <stdio.h>
  #include <signal.h>

#include <syslog.h>
#include "path.h"

void powerfail(ok)
int ok;
  int fd;

  /* Create an info file for init. */
  if ((fd = open(PWRSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
        if (ok)
                write(fd, "OK\n", 3);

                write(fd, "FAIL\n", 5);
  kill(1, SIGPWR);


/* This segment was from powerd.c */
/* This was from upscheck in UPS-HOWTO.....................*/

  /* Main program. */
  int main(int argc, char **argv)
    int fd;

  /*  These TIOCM_* parameters are defined in <linux/termios.h>, which  */
  /*  is indirectly included here.                                      */
    int dtr_bit = TIOCM_DTR;
    int rts_bit = TIOCM_RTS;
    int set_bits;
    int flags;
    int status, oldstat = -1;
    int count = 0;
    int pc;
    int on = 1;
    int off = 1;

    if (argc < 2) {
          fprintf(stderr, "Usage: upscheck <device> <bits-to-set>\n");

    /* Open monitor device. */
    if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
      fprintf(stderr, "upscheck: %s: %s\n", argv[1], sys_errlist[errno]);

    /* Get the bits to set from the command line. */
    sscanf(argv[2], "%d", &set_bits);

    while (1) {
      /* Set the command line specified bits (& only the command line */
      /* specified bits).                                             */
      ioctl(fd, TIOCMSET, &set_bits);
      /* fprintf(stderr, "Setting %o.\n", set_bits);  --remark by lloyd */

      /* ^^^^^^  You chould change it to 10 sec.. or more...*/

      /* Get the current line bits */
      ioctl(fd, TIOCMGET, &flags);
      /* fprintf(stderr, "Flags are %o.\n", flags); --remark by lloyd */

  /*  Fiddle here by changing TIOCM_CTS to some other TIOCM until    */
  /*  this program detects that the power goes out when you yank     */
  /*  the plug on the UPS.  Then you'll know how to modify powerd.c. */
      if (flags & TIOCM_CTS)
          pc = 0 ;
         /*  fprintf(stderr, "power is up.\n"); --remark by lloyd */
          if ( on == 0)
            on = 1;
            off = 1;

         /*    fprintf(stderr, "--------------\n");  --lloyd */
          pc = pc + 1 ;
        /*  fprintf(stderr, "power is down.\n");    --lloyd */

          on = 0;
          if ( off == 1 )
            off = 0;

------------------ upscheck.c --------- end

------------ path.h ------------------- begin
 * path.h      Paths of files that init and related utilities need.
 * Version:     @(#) paths.h 1.60 15-Jan-1995
 * Author:      Miquel van Smoorenburg, <>
 *              This file is part of the sysvinit suite,
 *              Copyright 1991-1995 Miquel van Smoorenburg.
 *              This program is free software; you can redistribute it and/or
 *              modify it under the terms of the GNU General Public License
 *              as published by the Free Software Foundation; either version
 *              2 of the License, or (at your option) any later version.
#define INITLVL         "/etc/initrunlvl"       /* New runlevel for init */
#define CONSOLE         "/dev/console"          /* Logical system console */
#define SYSTTY          "/dev/systty"           /* Physical system console */
#define SYSDEV          0x0400                  /* to mknod() the sysdev */
#define SECURETTY       "/etc/securetty"        /* List of root terminals */
#define SDALLOW         "/etc/shutdown.allow"   /* Users allowed to shutdown */
#define INITTAB         "/etc/inittab"          /* Location of inittab */
#define PWRSTAT         "/etc/powerstatus"      /* SIGPWR reason (OK/BAD) */
#define INIT            "/sbin/init"            /* Location of init itself. */
#define NOLOGIN         "/etc/nologin"          /* Stop user logging in. */
#define FASTBOOT        "/etc/fastboot"         /* Enable fast boot. */
#define SDPID           "/etc/shutdownpid"      /* PID of shutdown program */
#define IOSAVE          "/etc/"       /* termios settings for SU */
#define SHELL           "/bin/sh"               /* Default shell */
#define INITSCRIPT      "/etc/initscript"       /* Initscript. */
#define HALTSCRIPT      "/etc/rc.d/rc.halt"     /* Called by "fast" shutdown */
#define REBOOTSCRIPT    "/etc/rc.d/rc.reboot"   /* Ditto. */

----------------------- path.h --------------- end


1. Cable modem..56 modem dial up, 500K cable modem download stream...

I have 2 network cards and 1 modem in a Linux Box with RH6.
My cable modem uses the modem to start a PPP session, and the network card
is then hooked up to the VPN of the ISP.

I would like to setup a IP Masquraded Firewall to hook some of my other
behind the firewall.

I  cannot find a HOW-TO to setup the Linux box using this type of Cable
Modem.  I
have only found 2 way cable modems.  Can someone point me it the right

My problem is that I don't know how to get Linux connected, then also, how
do I configure
ipchains or ipfwadm to use the correct rule set.  Do I do the modem or the
network card.  The
network card has a static IP of which is usally used for VPNs.  My
cable company
is of corse not supporting Linux, so I have to do it myself.  The PPP dialup
uses DHCP to
assign my address so I have dhcp setup.

Thanks in advance..


2. Sis620 and XFree86 4.0.2

3. APC Back-UPS 400, updated howto for UPS?

4. Passing arguments to ksh functions


6. LINUX; Appletalk and Fisrt Class Clienr?

7. @Home Cable Modem and RedHat 6.0 -- no packets get past the cable modem at all!

8. Scripts and AWK

9. Howto for a small peer network attached to a cable modem

10. HowTO: FreeBSD 2.2.1 Gatway to Cable Modem Internet Access ?

11. Which HOWTO for cable modem?

12. HowTo: Set up a simple cable modem router?

13. cable modem router howto..