alt.os.linux, comp.os.linux.hardware, comp.os.linux.misc

alt.os.linux, comp.os.linux.hardware, comp.os.linux.misc

Post by Seva Batki » Sat, 12 Jul 1997 04:00:00



HI

I am trying to setup an APC Backups 400 UPS on my linux server and I was
going to us the program that came with the UPS HOWTO. The listing for
the program is at the bottom.

When i try to compile it using gcc I get the following error message
back:

$ gcc backupsd.c
In file included from /usr/include/linux/types.h:4,
                 from /usr/include/sys/types.h:4,
                 from backupsd.c:15:
/usr/include/linux/posix_types.h:44: syntax error before `typedef'

Could it be that my version of posix_types.h is outdated? If it is maybe
somebody can tell me where I can get a newer version. I am using Linux
RedHat 4.2

seva batkin

______________________________________________________________________
  /*  backupsd.c -- Simple Daemon to catch power failure signals from a
   *                Back-UPS (from APC).
   *
   *  Parts of the code are from Miquel van Smoorenburg's powerd.c

   *  I believe that it is okay to say that this is Public Domain, just
   *  give credit, where credit is due.
   *
   *  Disclaimer:  We make NO claims to this software, and take no
   *               resposibility for it's use/misuse.
   */

  #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>

  /* This is the file needed by SysVInit */
  #define PWRSTAT         "/etc/powerstatus"

  void powerfail(int fail);

  /* Main program. */
  int main(int argc, char **argv)
  {
    int fd;
    int killpwr_bit = TIOCM_RTS;
    int flags;
    int status, oldstat = -1;
    int count = 0;

    if (argc < 2) {
          fprintf(stderr, "Usage: %s <device> [killpower]\n", argv[0]);
          exit(1);
    }

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

    if ( argc >= 3  && (strcmp(argv[2], "killpower")==0) )
        {
            /* Let's kill the power! */
            fprintf(stderr, "%s: Attempting to kill the
power!\n",argv[0] );
            ioctl(fd, TIOCMBIS, &killpwr_bit);
            /* Hmmm..... If you have a power outtage, you won't make it!
*/
            exit(0);
        }
    else
        /* Since we don't want to kill the power, clear the RTS.
(killpwr_bit) */
        ioctl(fd, TIOCMBIC, &killpwr_bit);

  /* Become a daemon. */
    switch(fork()) {
    case 0: /* I am the child. */
                  setsid();
                  break;
    case -1: /* Failed to become daemon. */
                  fprintf(stderr, "%s: can't fork.\n", argv[0]);
                  exit(1);
    default: /* I am the parent. */
                  exit(0);
    }

    /* Now sample the DCD line. */
    while(1) {
        ioctl(fd, TIOCMGET, &flags);
        status = (flags & TIOCM_CD);
        /* Did DCD jumps to high? Then the power has failed. */
        if (oldstat == 0 && status != 0) {
            count++;
            if (count > 3) powerfail(0);
            else { sleep(1); continue; }
        }
        /* Did DCD go down again? Then the power is back. */
        if (oldstat > 0 && status == 0) {
            count++;
            if (count > 3) powerfail(1);
            else { sleep(1); continue; }
        }
        /* Reset count, remember status and sleep 2 seconds. */
        count = 0;
        oldstat = status;
        sleep(2);
    }
    /* Error! (shouldn't happen) */
    return(1);
  }

  /* Tell init the power has either gone or is back. */
  void powerfail(ok)
  int ok;
  {
    int fd;

    /* Create an info file needed by init to shutdown/cancel shutdown */
    unlink(PWRSTAT);
    if ((fd = open(PWRSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
          if (ok)
                  write(fd, "OK\n", 3);
          else
                  write(fd, "FAIL\n", 5);
          close(fd);
    }
    kill(1, SIGPWR);
  }

  /* End of File */