home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / next / misc / 19140 < prev    next >
Encoding:
Text File  |  1992-08-30  |  9.0 KB  |  359 lines

  1. Path: sparky!uunet!kithrup!stanford.edu!ames!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!menudo.uh.edu!lobster!nuchat!xcluud!dadstoy!unkaphaed!biff!biff
  2. From: biff@biff.uucp (Dwight Everhart)
  3. Newsgroups: comp.sys.next.misc
  4. Subject: Re: ScreenBlanker during "login" console
  5. Message-ID: <1992Aug30.020904.8390@biff.uucp>
  6. Date: Sun, 30 Aug 1992 02:09:04 GMT
  7. References: <BtntLL.LB1@mentor.cc.purdue.edu>
  8. Sender: biff@biff.uucp
  9. Lines: 348
  10.  
  11. In article <BtntLL.LB1@mentor.cc.purdue.edu> jbradsha@mentor.cc.purdue.edu  
  12. (Jonathan Bradshaw) writes:
  13. > Is there a way to put a screen blanker on the console login window?
  14.  
  15. I believe there are screen blankers that will do that.  If you just want  
  16. something to blank the screen rather than flying toasters across it, then  
  17. perhaps the one I wrote will do.  It sets the brightness level of the screen  
  18. to 0 after a certain amount of inactivity (specified on the command line) has  
  19. occurred.  It will work whether someone is logged in or not.  It should be  
  20. run from /etc/rc.local.  Example:
  21.  
  22.     /usr/local/etc/blankit 5
  23.  
  24. This will cause the screen to be blanked after five minutes of inactivity,  
  25. whether or not someone is logged in.  Note the absence of an ampersand.   
  26. Blankit puts itself in the background.
  27.  
  28. I've used blankit for a year and only encountered a couple of bugs in the  
  29. beginning (which I corrected).  It's worked fine since then.  Enjoy.
  30.  
  31. --------------------------------Hack Here-----------------------------------
  32.  
  33. #! /bin/sh
  34. # This is a shell archive, meaning:
  35. # 1. Remove everything above the #! /bin/sh line.
  36. # 2. Save the resulting text in a file.
  37. # 3. Execute the file with /bin/sh (not csh) to create the files:
  38. #    blankit
  39. # This archive created: Sat Aug 29 20:51:34 1992
  40. export PATH; PATH=/bin:$PATH
  41. if test ! -d 'blankit'
  42. then
  43.     mkdir 'blankit'
  44. fi
  45. cd 'blankit'
  46. if test -f 'blankit.c'
  47. then
  48.     echo shar: will not over-write existing file "'blankit.c'"
  49. else
  50. cat << \SHAR_EOF > 'blankit.c'
  51. /*
  52.   blankit.c:  A daemon for NeXT computers that automatically blanks
  53.   the screen after argv[1] minutes
  54.  
  55.   Author:  Dwight Everhart (biff@biff.uucp), although originally based
  56.   on dim.c by John W. Garnett (garnett@cs.utexas.edu).
  57.  
  58.   Version:  1.0 (8/29/92)
  59.  
  60.   Usage:  blankit <dim time in minutes> (No ampersand (&) is needed.
  61.   Blankit will put itself in the background.)
  62.  
  63.   Bugs:  Blankit will blank the screen according the the parameter
  64.   given when it was started and not according to the preferences of
  65.   any user that may be logged in.  If this user has his blanking
  66.   interval set longer than blankit's, the screen will blank earlier
  67.   than he specified.  If his interval is shorter than blankit's, then
  68.   the screen will dim normally at the time he specified and later will
  69.   blank when blankit's interval is reached.
  70.   
  71.   Distribution:  Blankit is in the public domain and is provided AS
  72.   IS.  No warranties are expressed or implied.  Do with it what you
  73.   will.  Copy it freely.
  74.  
  75. */
  76.  
  77. #include <stdio.h>
  78. #include <stdlib.h>
  79. #include <sys/fcntl.h>
  80. #include <sys/ioctl.h>
  81. #include <nextdev/evsio.h>
  82. #include "daemon.h"
  83.  
  84. #define EVS_DEVICE "/dev/evs0"
  85.  
  86. /* When the screen is blanked, this is how often we will check for */
  87. /* activity, in seconds.                                           */
  88. #define ACTIVITY_CHECK  2
  89.  
  90.  
  91.  
  92. /* open the event status device (see man page for evs) */
  93.  
  94. int open_evs()
  95. {
  96.    int evs;
  97.  
  98.    evs = open (EVS_DEVICE, O_RDWR, 0660);
  99.    if (evs < 0) {
  100.       fprintf (stderr, "error: couldn't open %s\n", EVS_DEVICE);
  101.       exit (EXIT_FAILURE);
  102.    };
  103.    return (evs);
  104. };
  105.  
  106.  
  107.  
  108. main (int argc, char *argv[]) {
  109.    int blank_time, evs, cadt, ladt, idle_count = 0;
  110.    int old_brightness, zero_brightness = 0;
  111.  
  112.    if (argc != 2) {
  113.       fputs ("Usage:  blankit <dim time in minutes>\n", stderr);
  114.       exit (EXIT_FAILURE);
  115.    };
  116.  
  117.    blank_time = strtol (argv[1], 0, 10);
  118.    if ((blank_time < 1) || (blank_time > 60)) {
  119.       fputs ("The blank time should be between 1 and 60 minutes\n", stderr);
  120.       exit (EXIT_FAILURE);
  121.    };
  122.  
  123.    daemon_start (1);
  124.  
  125.    evs = open_evs();
  126.    ioctl (evs, EVSIOGADT, &cadt);
  127.  
  128.    while (1) {
  129.       ladt = cadt;
  130.       sleep (60);
  131.       ioctl (evs, EVSIOGADT, &cadt);
  132.       if (cadt == ladt) {
  133.      ++idle_count;
  134.      if (idle_count == blank_time) {
  135.         ioctl (evs, EVSIOCB, &old_brightness);
  136.         ioctl (evs, EVSIOSB, &zero_brightness);
  137.         while (cadt == ladt) {
  138.            sleep (ACTIVITY_CHECK);
  139.            ioctl (evs, EVSIOGADT, &cadt);
  140.         };
  141.         ioctl (evs, EVSIOSB, &old_brightness);
  142.         idle_count = 0;
  143.      };
  144.       }
  145.       else /* There was activity -- reset the idle count */
  146.      idle_count = 0;
  147.    };
  148.  
  149.    close (evs);
  150. };
  151.  
  152. SHAR_EOF
  153. fi # end of overwriting check
  154. if test -f 'daemon.c'
  155. then
  156.     echo shar: will not over-write existing file "'daemon.c'"
  157. else
  158. cat << \SHAR_EOF > 'daemon.c'
  159. /*
  160.  * daemon.c:  Typed in fairly faithfully from _UNIX Network Programming_,
  161.  * by W. Richard Stevens (Prentice Hall), p. 82-85.
  162.  */
  163.  
  164.  
  165. /* Initialize a daemon process. */
  166.  
  167. #include "daemon.h"
  168. #include <stdio.h>
  169. #include <signal.h>
  170. #include <sys/param.h>
  171. #include <errno.h>
  172.  
  173. extern int errno;
  174.  
  175. #ifdef SIGTSTP          /* True if BSD system */
  176. #include <sys/file.h>
  177. #include <sys/ioctl.h>
  178. #include <sys/wait.h>
  179. #endif
  180.  
  181.  
  182.  
  183. #ifdef SIGTSTP
  184.  
  185. /*
  186.  * This is a 4.3BSD SIGCLD signal handler that can be used by a
  187.  * server that's not interested in its child's exit status, but needs to
  188.  * wait for them, to avoid cloging up the system with zombies.
  189.  *
  190.  * Beware that the calling process may get an interrupted system call
  191.  * when we return, so they had better handle that.
  192.  */
  193.  
  194. void sig_child()
  195. {
  196.    int pid;
  197.    union wait status;
  198.  
  199.    /* Use the wait3() system call with the WNOHANG option. */
  200.    while ((pid = wait3 (&status, WNOHANG, (struct rusage *)0)) > 0);
  201. };
  202.  
  203. #endif
  204.  
  205.  
  206.  
  207. /* Detach a daemon process from login session context. */
  208.  
  209. void daemon_start (int ignsigcld)
  210.                 /* nonzero -> handle SIGCLDs so zombies don't clog */
  211. {
  212.    register int childpid, fd;
  213.  
  214.    /*
  215.     * If we were started by init (process 1) from the /etc/inittab file
  216.     * there's no need to detach.
  217.     * This test is unreliable due to an unavoidable ambiguity
  218.     * if the process is started by some other process and orphaned
  219.     * (i.e., if the parent process terminates before we are started).
  220.     */
  221.  
  222.    if (getppid() == 1) goto out;
  223.  
  224.    /* Ignore the terminal stop signals (BSD). */
  225.  
  226. #ifdef SIGTTOU
  227.    signal (SIGTTOU, SIG_IGN);
  228. #endif
  229. #ifdef SIGTTIN
  230.    signal (SIGTTIN, SIG_IGN);
  231. #endif
  232. #ifdef SIGTSTP
  233.    signal (SIGTSTP, SIG_IGN);
  234. #endif
  235.  
  236.    /*
  237.     * If we were not started in the background, fork and
  238.     * let the parent exit.  This also guarentees the first child
  239.     * is not a process group leader.
  240.     */
  241.  
  242.    if ((childpid = fork()) < 0)
  243.       perror ("can't fork first child");
  244.    else if (childpid > 0)
  245.       exit (0);     /* parent */
  246.  
  247.    /*
  248.     * First child process.
  249.     *
  250.     * Disassociate from controlling terminal and proces group.
  251.     * Ensure the process can't reacquire a new controlling terminal.
  252.     */
  253.  
  254. #ifdef SIGTSTP     /* BSD */
  255.  
  256.    if (setpgrp (0, getpid()) == -1)
  257.       perror ("can't change process group");
  258.  
  259.    if ((fd = open ("/dev/tty", O_RDWR)) >= 0) {
  260.       ioctl (fd, TIOCNOTTY, (char *)NULL);   /* lose controlling tty */
  261.       close (fd);
  262.    };
  263.  
  264. #else     /* System V */
  265.  
  266.    if (setpgrp() == -1)
  267.       perror ("can't change process group");
  268.  
  269.    signal (SIGHUP, SIG_IGN);   /* immune from pgrp leader death */
  270.  
  271.    if ((childpid = fork()) < 0)
  272.       perror ("can't fork second child");
  273.    else if (childpid > 0)
  274.       exit (0);   /* first child */
  275.  
  276.    /* second child */
  277.  
  278. #endif
  279.  
  280. out:
  281.  
  282.    /* Close any open file descriptors. */
  283.  
  284.    for (fd = 0; fd < NOFILE; fd++) close (fd);
  285.  
  286.    errno = 0;   /* probably got set to EBADF from a close */
  287.  
  288.    /*
  289.     * Move the current directory to root, to make sure we
  290.     * aren't on a mounted filesystem.
  291.     */
  292.  
  293.    chdir ("/");
  294.  
  295.    /* Clear any inherited file mode creation mask. */
  296.  
  297.    umask (0);
  298.  
  299.    /*
  300.     * See if the caller isn't interested in the exit status of its
  301.     * children, and doesn't want to have them become zombies and
  302.     * clog up the system.
  303.     *
  304.     * With System V all we need do is ignore the signal.
  305.     * With BSD, however, we have to catch each signal
  306.     * and execute the wait3() system call.
  307.     */
  308.  
  309.    if (ignsigcld) {
  310. #ifdef SIGTSTP
  311.       signal (SIGCLD, sig_child);   /* BSD */
  312. #else
  313.       signal (SIGCLD, SIG_IGN);     /* System V */
  314. #endif
  315.    };
  316. };
  317.  
  318. SHAR_EOF
  319. fi # end of overwriting check
  320. if test -f 'daemon.h'
  321. then
  322.     echo shar: will not over-write existing file "'daemon.h'"
  323. else
  324. cat << \SHAR_EOF > 'daemon.h'
  325. /* daemon.h */
  326.  
  327. void daemon_start (int ignsigcld);
  328. /*
  329.   Detach a daemon process from login session context.
  330.   ignsigcld:  nonzero -> handle SIGCLDs so zombies don't clog.
  331. */
  332.  
  333. SHAR_EOF
  334. fi # end of overwriting check
  335. if test -f 'Makefile'
  336. then
  337.     echo shar: will not over-write existing file "'Makefile'"
  338. else
  339. cat << \SHAR_EOF > 'Makefile'
  340. # Makefile for blankit
  341.  
  342. CFLAGS = -O
  343. LDFLAGS = -object -s
  344.  
  345. blankit: blankit.o daemon.o
  346.     cc $(LDFLAGS) -o blankit blankit.o daemon.o
  347.  
  348. blankit.c: daemon.h
  349.  
  350. SHAR_EOF
  351. fi # end of overwriting check
  352. cd ..
  353. #    End of shell archive
  354. exit 0
  355.  
  356. --
  357. Dwight Everhart
  358. biff@biff.uucp
  359.