home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / HOWTO / MINI / POWER-SU < prev    next >
Encoding:
Text File  |  1995-04-20  |  24.2 KB  |  680 lines

  1. The Power Micro-Mini-HOWTO
  2. By Harvey J. Stein (hjstein@math.huji.ac.il)
  3. Version 1.0 (last edited Tue Aug  2  1994)
  4.  
  5.                       Contents
  6.  
  7.          1. Introduction
  8.          2. What you need to do (summary)
  9.          3. How it's supposed to work
  10.          4. Where to get the appropriate software
  11.          5. How to set things up
  12.      6. User Enhancements
  13.      7. How to make a cable
  14.      8. Serial port pin assignments
  15.      9. Ioctl bit numbers corresponding to RS232 control lines
  16.      10. Info on selected UPSs
  17.      10.1. Advice 1200 A
  18.      10.2. GPS
  19.      11. Reverse-engineering cables & hacking powerd.c
  20.      12. How to shutdown other machines on the same UPS
  21.          12.1. UPS status port method
  22.          12.2. Broadcast method
  23.          12.3. Dummy login method
  24.  
  25.  
  26. 1. Introduction
  27.  
  28. This HOWTO covers connecting a UPS to a PC running Linux in such a way
  29. that Linux can shutdown cleanly when the power goes out.  To a large
  30. extent it is reduntant, because all the basic info is contained in the
  31. powerd man page that comes with the SysVinit package.  None the less, 
  32. there seems to periodically be alot of discussion on the net regarding
  33. connecting Linux PCs to UPSs, (and the versions of Linux that I
  34. installed didn't come with a powerd man page).  I figured having a
  35. HOWTO would be a good idea because:
  36.  
  37.    -A second source of information might help to understand how
  38.     to connect Linux to a UPS, even if it's just the same information
  39.     written differently.
  40.    -The HOWTO can serve as a repository for UPS specific data.
  41.    -The HOWTO contains additional details that aren't in the powerd
  42.     man page.
  43.  
  44. None the less, this does not replace the powerd man page.  Hopefully,
  45. after reading both, people will be able to deal with UPSs.
  46.  
  47.  
  48. 2. What you need to do (summary)
  49.  
  50.   -Plug the PC into the UPS.
  51.   -Connect the PC's serial port to the UPS with a special cable.
  52.   -Run powerd on the PC.
  53.   -Setup your initd to do something reasonable on powerfail & powerok
  54.    events (like start a shutdown & kill any currently running
  55.    shutdowns, respectively, for example).
  56.  
  57.  
  58. 3. How it's supposed to work
  59.  
  60.    UPS's job:
  61.       When the power goes out, the UPS continues to power the PC &
  62.       signals that the power went out by throwing a relay or turning
  63.       on an opticoupler on it's control port.
  64.  
  65.    Cable's job:
  66.       The cable is designed so that when the UPS throws said relay,
  67.       this causes a particular serial port control line (typically
  68.       DCD) to go high.
  69.  
  70.    Powerd's job:
  71.       Powerd monitors the serial port.  Keeps raised/lowered
  72.       whatever serial port control lines the UPS needs to have
  73.       raised/lowered (typically, DTR must be kept high & whatever line
  74.       shuts off the UPS must be kept low).  When powerd sees the UPS control
  75.       line go high, it writes "FAIL" to /etc/powerfail & sends the
  76.       initd process a SIGPWR signal.  When the control line goes low
  77.       again, it writes "OK" to /etc/powerfail & sends initd a SIGPWR
  78.       signal.
  79.  
  80.    Initd's job (aside from everything else it does):
  81.       When it receives a SIGPWR, it looks at /etc/powerfail.  If it
  82.       contains "FAIL" it runs the powerfail entry from /etc/inittab.
  83.       If it contains "OK" it runs the powerokwait entry from inittab.
  84.  
  85.  
  86. 4. Where to get the appropriate software
  87.  
  88. Pick up /pub/Linux/system/Daemons/SysVinit-2.50.tgz from
  89. sunsite.unc.edu or a mirror.  It includes a copy of powerd.c,
  90. shutdown.c, an initd that understands what to do with SIGPWR, & can
  91. handle powerfail & powerokwait entries in the inittab file.
  92.  
  93.  
  94. 5. How to set things up
  95.  
  96.    -Edit /etc/inittab.  Put in something like this:
  97.  
  98. # What to do when power fails (Halt system & drain battery :):
  99. pf::powerfail:/etc/powerfailscript +5
  100.  
  101. # If power is back before shutdown, cancel the running shutdown.
  102. pg:0123456:powerokwait:/etc/powerokscript
  103.  
  104.    -Write scripts /etc/powerfailscript & /etc/powerokscript to
  105.     shutdown in 5 minutes (or whatever's appropriate) & kill any
  106.     existing shutdown, respectively.  Depending on the version of
  107.     shutdown that you're using, this will be either so trivial that
  108.     you'll dispense with the scripts, or be a 1 line bash script,
  109.     something along the lines of:
  110.  
  111.        kill `ps -aux | grep "shutdown" | grep -v grep | awk '{print $2}'`
  112.  
  113.     and you'll keep the scripts.
  114.  
  115.    -Tell initd to re-process the inittab file with the command:
  116.  
  117.        telinit q
  118.  
  119.    -Edit rc.local so that powerd gets run upon startup.  The syntax
  120.     is:
  121.          powerd <line>
  122.  
  123.     Replace <line> with the serial port that the modem is connected,
  124.     such as /dev/cua1.
  125.  
  126.    -Connect PC's serial port to UPS's serial port.  DO NOT PLUG PC
  127.     INTO UPS YET.
  128.  
  129.    -Plug a light into the UPS.
  130.  
  131.    -Turn on the UPS & the light.
  132.  
  133.    -Run powerd.
  134.  
  135.    -Test the setup:
  136.       -Yank the UPS's plug.
  137.          -Check that the light stays on.
  138.          -Check that /etc/powerfailscript is running.
  139.          -Check that shutdown is running.
  140.       -Plug the UPS back in.
  141.          -Check that the light stays on.
  142.          -Check that /etc/powerfailscript is no longer running.
  143.          -Check that shutdown is no longer running.
  144.       -Yank the UPS's plug again.  Leave it out & make sure that the
  145.        PC shuts down properly in the proper amount of time.
  146.  
  147.       -After everything seems to be proper, powerdown the PC & plug it
  148.        into the UPS.  Run a script that sync's the hard disk every
  149.        second or so.  Simultaneously run a second script that keeps
  150.        doing a find over your entire hard disk.  The first is to make
  151.        this a little safer & the second is to help draw lots of power.
  152.        Now, pull the plug on the UPS, check again that shutdown is
  153.        running & wait.  Make sure that the PC shuts down cleanly
  154.        before the battery on the UPS gives out.
  155.  
  156. Congratulations!  You now have a Linux PC that's protected by a UPS
  157. and will shutdown cleanly when the power goes out!
  158.  
  159.  
  160. 6. User Enhancements
  161.  
  162.    -Hack powerd.c to monitor the line indicating that the batteries
  163.     are low.  When the batteries get low, do an *immediate* shutdown.
  164.    -Modify shutdown procedure so that if it's shutting down in a
  165.     powerfail situation, then it turns off the UPS after doing
  166.     everything necessary.
  167.  
  168.  
  169. 7. How to make a cable
  170.  
  171. This section is just from messages I've seen on the net.  I haven't
  172. done it so I can't write from experience.  If anyone has, please write
  173. this section for me :).  See also the message about the GPS1000
  174. contained in section 10.2.
  175.  
  176.  
  177.    >From miquels@caution.cistron.nl.mugnet.org Wed Jul 21 14:26:33 1993
  178.    Newsgroups: comp.os.linux
  179.    Subject: Re: UPS interface for Linux?
  180.    From: miquels@caution.cistron.nl.mugnet.org (Miquel van Smoorenburg)
  181.    Date: Sat, 17 Jul 93 18:03:37
  182.    Distribution: world
  183.    Organization: Cistron Electronics.
  184.  
  185.    In article <1993Jul15.184450.5193@excaliber.uucp> joel@rac1.wam.umd.edu (Joel M. Hoffman) writes:
  186.    >I'm in the process of buying a UPS (Uninteruptable Power Supply), and
  187.    >notice that some of them have interfaces for LAN's to signal the LAN
  188.    >when the power fails.
  189.    >
  190.    >Is there such an interface for Linux?
  191.    >
  192.    >Thanks.
  193.    >
  194.    >-Joel
  195.    >(joel@wam.umd.edu)
  196.    >
  197.  
  198.    When I worked on the last versioon of SysVinit (Now version 2.4),
  199.    I temporarily had a UPS on my computer, so I added support for it.
  200.    You might have seen that in the latest <signal.h> header files there
  201.    is a #define SIGPWR 30 now :-). Anyway, I did not have such a special
  202.    interface but the output of most UPS's is just a relais that makes or breaks
  203.    on power interrupt. I thought up a simple way to connect this to the
  204.    DCD line of the serial port. In the SysVinit package there is a daemon
  205.    called 'powerd' that keeps an eye on that serial line and sends SIGPWR
  206.    to init when the status changes, so that init can do something (such as
  207.    bringing the system down within 5 minutes). How to connect the UPS to
  208.    the serial line is described in the source "powerd.c", but I will
  209.    draw it here for explanation:
  210.  
  211.             +------------------------o  DTR
  212.             |
  213.               +---+
  214.               |   | resistor
  215.               |   | 10 kilo-Ohm
  216.               |   |
  217.               +---+                                To serial port.
  218.             |
  219.       +-----o-------+------------------------o  DCD
  220.       |             |
  221.       o  UPS        |
  222.     \    relais     |
  223.      \              |
  224.       |             |
  225.       +-----o-------+------------------------o  GND
  226.  
  227.    Nice drawing eh?
  228.  
  229.    Hope this helps.
  230.    SysVinit can be found on sunsite (and tsx-11 probably) as
  231.    SysVinit2.4.tar.z
  232.  
  233.    Mike.
  234.  
  235.    --
  236.  
  237.    Miquel van Smoorenburg, <miquels@cistron.nl.mugnet.org>
  238.    Ibmio.com: cannot open CONFIG.SYS: file handle broke off.
  239.  
  240.  
  241.    >From danny@caution.cistron.nl.mugnet.org Wed Jul 21 14:27:04 1993
  242.    Newsgroups: comp.os.linux
  243.    Subject: Re: UPS interface for Linux?
  244.    From: danny@caution.cistron.nl.mugnet.org (Danny ter Haar)
  245.    Date: Mon, 19 Jul 93 11:02:14
  246.    Distribution: world
  247.    Organization: Cistron Electronics.
  248.  
  249.    In article <9307174330@caution.cistron.nl.mugnet.org> miquels@caution.cistron.nl.mugnet.org (Miquel van Smoorenburg) writes:
  250.    >How to connect the UPS to the serial line is described in the source
  251.    >"powerd.c", but I will draw it here for explanation:
  252.  
  253.    The drawing wasn't really clear, please use this one in stead !
  254.    >
  255.    >                     +------------------------o  DTR
  256.    >                     |
  257.    >                   +---+
  258.    >                   |   | resistor
  259.    >                   |   | 10 kilo-Ohm
  260.    >                   |   |
  261.    >                   +---+                                To serial port.
  262.    >                     |
  263.    >       +-----o-------+------------------------o  DCD
  264.    >       |
  265.    >       o  UPS
  266.    >     \    relais
  267.    >      \
  268.    >       |
  269.    >       +-----o--------------------------------o  GND
  270.    >
  271.  
  272.    The DTR is kept high, when the UPS's power input is gone it
  273.    will close the relais . The computer is monitoring
  274.    the DCD input port to go LOW . When this happens it will start a
  275.    shutdown sequence...
  276.  
  277.    _____
  278.    Danny
  279.  
  280.    --
  281.    <=====================================================================>
  282.    Danny ter Haar  <dannyth@hacktic.nl> or <danny@cistron.nl.mugnet.org>
  283.    Robins law #103: 'a couple of lightyears can't part good friends'
  284.  
  285.  
  286. 8. Serial port pin assignments
  287.  
  288. (The following is from David Tal's <GSRGAAO@TECHNION.BITNET>
  289. 'Frequently Used Cables and Connectors' document).
  290.  
  291.    Pin Assignment for the Serial Port (RS-232C), 25-pin and 9-pin
  292.    --------------------------------------------------------------
  293.    DB-25   DB-9
  294.    Pin #   Pin #   Name    EIA     CCITT   DTE-DCE Description
  295.    -----   -----   -----   -----   -----   ------- -------------------
  296.     1              FG      AA       101    ----    Frame Ground/Chassis GND
  297.     2      3       TD      BA       103    --->    Transmitted Data, TxD
  298.     3      2       RD      BB       104    <---    Received Data, RxD
  299.     4      7       RTS     CA       105    --->    Request To Send
  300.     5      8       CTS     CB       106    <---    Clear To Send
  301.     6      6       DSR     CC       107    <---    Data Set Ready
  302.     7      5       SG      AB       102    ----    Signal Ground, GND
  303.     8      1       DCD     CF       109    <---    Data Carrier Detect
  304.     9              --      --        -       -     Positive DC test voltage
  305.    10              --      --        -       -     Negative DC test voltage
  306.    11              QM      --        -     <---    Equalizer mode
  307.    12              SDCD    SCF      122    <---    Secondary Data Carrier Detect
  308.    13              SCTS    SCB      121    <---    Secondary Clear To Send
  309.    14              STD     SBA      118    --->    Secondary Transmitted Data
  310.    15              TC      DB       114    <---    Transmitter (signal) Clock
  311.    16              SRD     SBB      119    <---    Secondary Receiver Clock
  312.    17              RC      DD       115    --->    Receiver (signal) Clock
  313.    18              DCR     --        -     <---    Divided Clock Receiver
  314.    19              SRTS    SCA      120    --->    Secondary Request To Send
  315.    20      4       DTR     CD       108.2  --->    Data Terminal Ready
  316.    21              SQ      CG       110    <---    Signal Quality Detect
  317.    22      9       RI      CE       125    <---    Ring Indicator
  318.    23              --      CH       111    --->    Data rate selector
  319.    24              --      CI       112    <---    Data rate selector
  320.    25              TC      DA       113    <---    Transmitted Clock
  321.  
  322.     1                         13         1         5
  323.       _______________________________      _______________
  324.       \  . . . . . . . . . . . . .  /      \  . . . . .  /    RS232-connectors
  325.        \  . . . . . . . . . . . .  /        \  . . . .  /     seen from outside
  326.     ---------------------------          -----------      of computer.
  327.     14                      25            6       9
  328.  
  329.    DTE : Data Terminal Equipment (i.e. computer)
  330.    DCE : Data Communications Equipment (i.e. modem)
  331.    RxD : Data received; 1 is transmitted "low", 0 as "high"
  332.    TxD : Data sent; 1 is transmitted "low", 0 as "high"
  333.    DTR : DTE announces that it is powered up and ready to communicate
  334.    DSR : DCE announces that it is ready to communicate; low=modem hangup
  335.    RTS : DTE asks DCE for permission to send data
  336.    CTS : DCE agrees on RTS
  337.    RI  : DCE signals the DTE that an establishment of a connection is attempted
  338.    DCD : DCE announces that a connection is established
  339.  
  340.  
  341.  
  342. 9. Ioctl bit numbers corresponding to RS232 control lines
  343. (taken from /usr/include/linux/termios.h)
  344.  
  345. /* modem lines */
  346. #define TIOCM_LE        0x001
  347. #define TIOCM_DTR       0x002
  348. #define TIOCM_RTS       0x004
  349. #define TIOCM_ST        0x008
  350. #define TIOCM_SR        0x010
  351. #define TIOCM_CTS       0x020
  352. #define TIOCM_CAR       0x040
  353. #define TIOCM_RNG       0x080
  354. #define TIOCM_DSR       0x100
  355. #define TIOCM_CD        TIOCM_CAR
  356. #define TIOCM_RI        TIOCM_RNG
  357.  
  358. Note that the 3rd column is in Hex.
  359.  
  360.  
  361. 10. Info on selected UPSs
  362.  
  363.    **** Please send them to me for inclusion here. ****
  364.  
  365.  
  366. 10.1. Advice 1200 A
  367.  
  368.    UPS from Advice Electronics, Tel Aviv Israel (they stick their own
  369.    name on the things).
  370.  
  371.    UPS Control Port
  372.    ----------------
  373.  
  374.          2 - Power Fail.
  375.          5 - Battery Low.
  376.          6 - Shut Down UPS.
  377.          4 - Common ground for pin 2, 5, 6.
  378.  
  379.    They also gave me the following picture which didn't help me, but
  380.    may help you if you want to build a cable yourself:
  381.  
  382.  
  383.          2 ----------+
  384.                      |
  385.                      \
  386.                       \|
  387.                        |--------------
  388.                       /|
  389.                     \/     (<--- The "\/" here indicates the type of
  390.                     |             this transister.  I forget what
  391.                     |             denotes what, but this one points
  392.                  +-----+          away from the center line.)
  393.                 /  /  /
  394.  
  395.  
  396.          5 ----------+
  397.                      |
  398.                      \
  399.                       \|
  400.                        |--------------
  401.                       /|
  402.                     \/
  403.                     |
  404.                     |
  405.                  +-----+
  406.                 /  /  /
  407.  
  408.  
  409.                        +-------------
  410.                        |
  411.                        /
  412.               10K    |/
  413.          6 --\/\/\/--|
  414.                      |\
  415.                        \/
  416.                        |
  417.                        |
  418.                     +-----+
  419.                    /  /  /
  420.  
  421.  
  422.          4 ----------+
  423.                      |
  424.                      |
  425.                   +-----+
  426.                  /  /  /
  427.  
  428.  
  429.    Cable supplied
  430.    --------------
  431.    They first game me a cable that was part of a DOS UPS control
  432.    package called RUPS.  I used this for testing.  When I was
  433.    satisfied, they gave me a cable they use for Netware servers
  434.    connected to UPSs.  It functioned identically.  Here are the
  435.    details:
  436.  
  437.       DTR - Powers cable (keep high).
  438.       CTS - Power out (stays high & goes low when power goes out).
  439.       DSR - Battery low (stays high & goes low when battery does).
  440.       RTS - Turns off UPS (keep low & set high to turn off UPS).
  441.  
  442.    (The powerd.c that comes with SysVinit set or left RTS high,
  443.    causing the UPS to shut off immediately when powerd was started
  444.    up!)
  445.  
  446.  
  447. 10.2. GPS1000 from ACCODATA
  448.  
  449.    >From hennus@sky.nl.mugnet.org Thu Mar 10 15:10:22 1994
  450.    Newsgroups: comp.os.linux.help
  451.    Subject: Re: auto-shutdown with UPS
  452.    From: hennus@sky.nl.mugnet.org (Hennus Bergman)
  453.    Date: Tue, 1 Mar 1994 22:17:45 GMT
  454.    Distribution: world
  455.    Organization: The Organization For Removal Of On-Screen Logos
  456.  
  457.    In article <CRAFFERT.94Feb28125452@nostril.lehman.com>,
  458.    Colin Owen Rafferty <craffert@nostril.lehman.com> wrote:
  459.    >I am about to buy an Uninterruptable Power Supply for my machine, and
  460.    >I would like to get one that has the "auto-shutdown" feature.
  461.    >
  462.    I just got one of those real cheap :-)
  463.    It's a GPS1000 by ACCODATA. Anybody know how good the output
  464.    signal of these things is? [Don't have a scope myself :-(]
  465.  
  466.    >I assume that these each have some kind of serial connection that
  467.    >tells the system information about it.
  468.    >
  469.    I took it apart to find out how it worked. There were three optocouplers
  470.    (two output, one input) connected to a 9 pin connector at the back.
  471.    One turns on when the power fails, and goes off again when the power
  472.    returns. While the power is off, you can use the `input' to shut the
  473.    battery off. [It releases the power-relay.] The third one is some kind
  474.    of feedback to tell that it did accepted the `shut-down command'.
  475.    I think the interface for my UPS was designed to be connected to TTL-level
  476.    signals, but with some resistors it could be connected to serial port.
  477.    It's wired in such a way that using a RS-232 port you cannot use both
  478.    output optocouplers; but the shutdown feedback is not necessary anyway,
  479.    just use the important one. ;-)
  480.    [Note that it is possible to blow the transistor part in optocouplers
  481.    with RS-232 levels if you wire it the wrong way round ;-)]
  482.  
  483.    I was hoping I would be able to connect it to my unused game port,
  484.    but that doesn't have an output, does it?
  485.    I'll probably end up getting an extra printer port for this.
  486.  
  487.    Not all UPS' use optocouplers, some use simple relays, which are
  488.    less critical to connect, but of course not as `nice'.
  489.  
  490.    >Has anyone written a package that watches the UPS and does a shutdown
  491.    >(or something) when the power is off?
  492.    SysVinit-2.4 (and probably 2.5 as well) has a `powerd' daemon that
  493.    continually watches a serial port for presence of the CD (Carrier
  494.    Detect) line and signals init when it drops. Init then activates
  495.    shutdown with a time delay. If the power returns within a few minutes
  496.    the shutdown is cancelled. Very Nice.
  497.    The only problem I had with it is that it doesn't actually tell the
  498.    UPS to turn off when the shutdown is complete. It just sits there with
  499.    a root prompt. I'll probably write a small program to shut it down
  500.    >from /etc/brc. RSN.
  501.  
  502.    >    Colin Rafferty, Lehman Brothers <craffert@lehman.com>
  503.  
  504.    Hennus Bergman
  505.  
  506.  
  507. 11. Reverse-engineering cables & hacking powerd.c
  508.  
  509. Try to get documentation for the cables that your UPS seller supplies.
  510. In particular find out:
  511.  
  512.    -What lines need to be kept high.
  513.    -What line(s) turn off the UPS.
  514.    -What lines the UPS toggles to indicate that:
  515.       -Power is out.
  516.       -Battery is low.
  517.  
  518. You then need to hack powerd.c appropriately.
  519.  
  520. If you have trouble getting the above information (or just want to
  521. check it) the following program (upscheck.c) might help.  It's a
  522. hacked version of powerd.c.  It allows you to set the necessary port
  523. flags from the command line & then monitors the port, displaying the
  524. control lines every second.  I used it as "upscheck /dev/cua1 2" (for
  525. example) to set the 2nd bit (DTR) & to clear the other bits.  The
  526. number base 2 indicates which bits to set, so for example to set bits
  527. 1, 2 & 3, (& clear the others) use 7.  See the code for details.
  528.  
  529. Here's the (untested) upscheck.c program.  It's untested because I
  530. edited the version I originally used to make it clearer, and can't
  531. test the new version at the moment.
  532.  
  533.  --------- Begin upscheck.c ------------------
  534. /*
  535.  * upscheck    Check how UPS & computer communicate.
  536.  *
  537.  * Usage:    upscheck <device> <bits to set>
  538.  *              For example, upscheck /dev/cua4 4 to set bit 3 &
  539.  *              monitor /dev/cua4.
  540.  *
  541.  * Author:    Harvey J. Stein <hjstein@math.huji.ac.il>
  542.  *              (but really just a minor modification of Miquel van
  543.  *              Smoorenburg's <miquels@drinkel.nl.mugnet.org> powerd.c
  544.  *
  545.  * Version:    1.0 19940802
  546.  *
  547.  */
  548. #include <sys/types.h>
  549. #include <sys/ioctl.h>
  550. #include <fcntl.h>
  551. #include <errno.h>
  552. #include <stdlib.h>
  553. #include <unistd.h>
  554. #include <stdio.h>
  555. #include <signal.h>
  556.  
  557. /* Main program. */
  558. int main(int argc, char **argv)
  559. {
  560.   int fd;
  561.  
  562. /*  These TIOCM_* parameters are defined in <linux/termios.h>, which  */
  563. /*  is indirectly included here.                                      */
  564.   int dtr_bit = TIOCM_DTR;
  565.   int rts_bit = TIOCM_RTS;
  566.   int set_bits;
  567.   int flags;
  568.   int status, oldstat = -1;
  569.   int count = 0;
  570.   int pc;
  571.  
  572.   if (argc < 2) {
  573.     fprintf(stderr, "Usage: upscheck <device> <bits-to-set>\n");
  574.     exit(1);
  575.   }
  576.  
  577.   /* Open monitor device. */
  578.   if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
  579.     fprintf(stderr, "upscheck: %s: %s\n", argv[1], sys_errlist[errno]);
  580.     exit(1);}
  581.  
  582.   /* Line is opened, so DTR is high. Force it anyway to be sure. */
  583.   /*    ioctl(fd, TIOCMBIS, &dtr_bit); */
  584.   /*    The above line was from the original powerd.c, but it turned off */
  585.   /*    my UPS!  So, I changed it to the line below which clears the DTR */
  586.   /*    instead of setting the DTR bit & that worked for me.  However,   */
  587.   /*    it might not work for you, so I commented it out too.            */
  588.   /*    ioctl(fd, TIOCMBIC, &dtr_bit); */
  589.  
  590.   /* Get the bits to set from the command line. */
  591.   sscanf(argv[2], "%d", &set_bits);
  592.  
  593.   while (1) {
  594.     /* Set the command line specified bits (& only the command line */
  595.     /* specified bits).                                             */
  596.     ioctl(fd, TIOCMSET, &set_bits);
  597.     fprintf(stderr, "Setting %o.\n", set_bits);
  598.  
  599.     sleep(1);
  600.  
  601.     /* Get the current line bits */
  602.     ioctl(fd, TIOCMGET, &flags);
  603.     fprintf(stderr, "Flags are %o.\n", flags);
  604.  
  605. /*  Fiddle here by changing TIOCM_CTS to some other TIOCM until    */
  606. /*  this program detects that the power goes out when you yank     */
  607. /*  the plug on the UPS.  Then you'll know how to modify powerd.c. */
  608.     if (flags & TIOCM_CTS) 
  609.       {
  610.     pc = 0 ;
  611.     fprintf(stderr, "power is up.\n");
  612.       }
  613.     else 
  614.       { 
  615.     pc = pc + 1 ; 
  616.     fprintf(stderr, "power is down.\n");
  617.       }
  618.     }
  619.  
  620.   close(fd);
  621. }
  622.  ----------- End upscheck.c ---------------------------
  623.  
  624.  
  625.  
  626. 12. How to shutdown other machines on the same UPS
  627.  
  628. Some people (myself included) have several Linux PCs connected to one
  629. UPS.  One PC monitors the UPS & needs to get the other PCs to shut
  630. down when the power goes out.  There are a number of ways to do this,
  631. all are do-it-yourself currently, and most are just hypothetical.
  632.  
  633. We assume the PCs can communicate over a network.  Call the PC that
  634. monitors the UPS the master & the other PCs the slaves.
  635.  
  636.  
  637. 12.1. UPS status port method
  638.  
  639. Set up a port on the master which, when connected to, either sends
  640. "OK", "FAIL", or "BATLOW", the first when the power is ok, the second
  641. when the power has failed, and the third when the battery is low.
  642. Model this on port 13 (the time port) which one can telnet to &
  643. receive the local time.
  644.  
  645. Have the slaves run versions of powerd that look at this port instead
  646. of checking a serial line.
  647.  
  648. The only down side I can see to this method is the network load due to
  649. checking this port.  One would want to check this port often to
  650. quickly catch the BATLOW message & shut down before the battery dies.
  651.  
  652.  
  653. 12.2. Broadcast method
  654.  
  655. Same as 12.1 except send an ethernet broadcast message that the power
  656. has just gone down.
  657.  
  658. This might have security implications.
  659.  
  660.  
  661. 12.3. Dummy login method
  662.  
  663. Set up dummy logins on the slaves with login names "powerok" &
  664. "powerfail", both with the same UID.  Make /etc/powerokscript the
  665. shell of the powerok user, & make /etc/powerfailscript the shell of
  666. the powerfail user.  On the master, have the /etc/powerokscript rlogin
  667. to each slave as user powerok, & have the /etc/powerfailscript rlogin
  668. to each slave as user powerfail.  Put a .rhosts file on each slave in
  669. the home directory of powerok & powerfail to allow root from the
  670. master to login as user powerok & powerfail to each slave.
  671.  
  672. This is the system I'm currently using.
  673.  
  674. This might also have security implications.
  675.  
  676.  
  677.  ---------- END UPS micro-mini HOWTO -------------------------
  678.  
  679.  
  680.