change to keyword search
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Fri, 9 Oct 1987 10:25:13 +0000 (02:25 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Fri, 9 Oct 1987 10:25:13 +0000 (02:25 -0800)
SCCS-vsn: games/dm/dm.conf.5 5.2
SCCS-vsn: games/dm/dm.c 5.4
SCCS-vsn: etc/dm.conf 5.2

usr/src/etc/dm.conf
usr/src/games/dm/dm.c
usr/src/games/dm/dm.conf.5

index 0a50739..66927b3 100644 (file)
@@ -3,45 +3,21 @@
 # All rights reserved.  The Berkeley software License Agreement
 # specifies the terms and conditions for redistribution.
 #
 # All rights reserved.  The Berkeley software License Agreement
 # specifies the terms and conditions for redistribution.
 #
-#      @(#)dm.conf     5.1 (Berkeley) %G%
+#      @(#)dm.conf     5.2 (Berkeley) %G%
 #
 # Game Control File
 #
 # Game Control File
-#
-# Standard games hours -- no games may be played between these hours.
-# Hours are in a 24 hour clock, 0-23; a non-digit means no limitations.
-# For example, to limit gamesplaying from 8am to 5pm on Monday, the
-# entry would be:
-# Monday       8       17
-#
-# Day          Start   Stop
-# --------------------------
-Monday         *       *
-Tuesday                *       *
-Wednesday      *       *
-Thursday       *       *
-Friday         *       *
-Saturday       *       *
-Sunday         *       *
 
 
-%% -- leave this here, it separates the three parts of this file.
+#              TTYNAME
+# badtty       /dev/tty19                              # news
+# badtty       /dev/tty20                              # news/mail
 
 
-# Tty limitations -- any tty listed after the keyword "badtty" may
-# not have games played on it.  The tty should be as returned by
-# ttyname(3), for example, to keep tty19, your news dialin tty from
-# being used, the entry would be:
-# badtty       /dev/tty19
-#
-# badtty       tty
-#--------------------
+#              DAY OF WEEK     START           STOP
+# time         Monday          7               16      # 7am to 4pm
+# time         Tuesday         7               16
+# time         Wednesday       7               16
+# time         Thursday        7               16
+# time         Friday          7               16
 
 
-%% -- leave this here, it separates the three parts of this file.
-
-# Specific game limitations -- games can be set to run only if the system
-# is under a certain load average and/or a maximum number of users.  A
-# priority for the game may also be set.  A non-digit means no limitation
-# or value for that parameter.  The game "default" controls the settings for
-# any game not otherwise listed, and should be the last entry.
-#
-# Name         max load ave.   max users       priority
-#-------------------------------------------------------
-default                5               50              *
+#              GAME            MAX LOAD        MAX USERS       PRIORITY
+# default must be the last entry for the ``game'' keyword
+game           default         5               *               *
index c3c7ee2..7b4b943 100644 (file)
@@ -11,7 +11,7 @@ char copyright[] =
 #endif not lint
 
 #ifndef lint
 #endif not lint
 
 #ifndef lint
-static char sccsid[] = "@(#)dm.c       5.3 (Berkeley) %G%";
+static char sccsid[] = "@(#)dm.c       5.4 (Berkeley) %G%";
 #endif not lint
 
 #include <sys/param.h>
 #endif not lint
 
 #include <sys/param.h>
@@ -31,7 +31,6 @@ static char sccsid[] = "@(#)dm.c      5.3 (Berkeley) %G%";
 #define        LOGFILE         "/usr/adm/dm.log"
 #endif
 
 #define        LOGFILE         "/usr/adm/dm.log"
 #endif
 
-static FILE    *cfp;
 static time_t  now;                    /* current time value */
 static int     priority = 0;           /* priority game runs at */
 static char    *game,                  /* requested game */
 static time_t  now;                    /* current time value */
 static int     priority = 0;           /* priority game runs at */
 static char    *game,                  /* requested game */
@@ -41,30 +40,24 @@ main(argc, argv)
        int     argc;
        char    **argv;
 {
        int     argc;
        char    **argv;
 {
-       char    *C, *rindex();
-       double  load();
+       char    *cp, *rindex(), *ttyname();
+       time_t  time();
 
        nogamefile();
 
        nogamefile();
+       game = (cp = rindex(*argv, '/')) ? ++cp : *argv;
 
 
-       if (!strcmp(*argv, "dm"))
+       if (!strcmp(game, "dm"))
                exit(0);
 
                exit(0);
 
-       if (!(cfp = fopen(CONTROL, "r"))) {
-               fprintf(stderr, "dm: unable to read %s.\n", CONTROL);
-               exit(1);
-       }
-
-       read_days();
-       read_ttys();
-
-       game = (C = rindex(*argv, '/')) ? ++C : *argv;
-       read_games();
+       gametty = ttyname(0);
+       (void)time(&now);
+       read_config();
 
 #ifdef LOG
        logfile();
 #endif
        play(argv);
 
 #ifdef LOG
        logfile();
 #endif
        play(argv);
-/*NOTREACHED*/
+       /*NOTREACHED*/
 }
 
 /*
 }
 
 /*
@@ -79,125 +72,133 @@ play(args)
                perror("dm: chdir");
                exit(1);
        }
                perror("dm: chdir");
                exit(1);
        }
-       if (priority > 0 && setpriority(PRIO_PROCESS, 0, priority) < 0)
-               fputs("dm: unable to set priority!\n", stderr);
+       if (priority > 0)       /* < 0 requires root */
+               (void)setpriority(PRIO_PROCESS, 0, priority);
        setgid(getgid());       /* we run setgid kmem; lose it */
        execv(game, args);
        perror("dm");
        exit(1);
 }
 
        setgid(getgid());       /* we run setgid kmem; lose it */
        execv(game, args);
        perror("dm");
        exit(1);
 }
 
-#define        lcontrol(buf)   (buf[0] == '%' && buf[1] == '%')
-#define        lignore(buf)    (buf[0] == '\n' || buf[0] == '#')
 /*
 /*
- * read_days --
- *     read through days listed in the control file, decide
- *     if current time is an okay play time.
+ * read_config --
+ *     read through config file, looking for key words.
+ */
+static
+read_config()
+{
+       FILE    *cfp;
+       char    lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40];
+
+       if (!(cfp = fopen(CONTROL, "r"))) {
+               fprintf(stderr, "dm: unable to read %s.\n", CONTROL);
+               exit(1);
+       }
+       while (fgets(lbuf, sizeof(lbuf), cfp))
+               switch(*lbuf) {
+               case 'b':               /* badtty */
+                       if (sscanf(lbuf, "%s%s", f1, f2) != 2 ||
+                           strcasecmp(f1, "badtty"))
+                               break;
+                       c_tty(f2);
+                       break;
+               case 'g':               /* game */
+                       if (sscanf(lbuf, "%s%s%s%s%s",
+                           f1, f2, f3, f4, f5) != 5 || strcasecmp(f1, "game"))
+                               break;
+                       c_game(f2, f3, f4, f5);
+                       break;
+               case 't':               /* time */
+                       if (sscanf(lbuf, "%s%s%s%s", f1, f2, f3, f4) != 4 ||
+                           strcasecmp(f1, "time"))
+                               break;
+                       c_day(f2, f3, f4);
+               }
+       (void)fclose(cfp);
+}
+
+/*
+ * c_day --
+ *     if day is today, see if okay to play
  */
 static
  */
 static
-read_days()
+c_day(s_day, s_start, s_stop)
+       char    *s_day, *s_start, *s_stop;
 {
        static char     *days[] = {
                "sunday", "monday", "tuesday", "wednesday",
                "thursday", "friday", "saturday",
        };
 {
        static char     *days[] = {
                "sunday", "monday", "tuesday", "wednesday",
                "thursday", "friday", "saturday",
        };
-       struct tm       *ct;
-       register char   *dp;
+       static struct tm        *ct;
        int     start, stop;
        int     start, stop;
-       char    lbuf[BUFSIZ], f1[40], f2[40], f3[40];
-       time_t  time();
-
-       (void)time(&now);
-       ct = localtime(&now);
-       dp = days[ct->tm_wday];
 
 
-       while (fgets(lbuf, sizeof(lbuf), cfp)) {
-               if (lignore(lbuf))
-                       continue;
-               if (lcontrol(lbuf))
-                       return;
-               if (sscanf(lbuf, "%s%s%s", f1, f2, f3) != 3)
-                       continue;
-               if (!strcasecmp(dp, f1)) {
-                       if (!isdigit(*f2) || !isdigit(*f3))
-                               continue;
-                       start = atoi(f2);
-                       stop = atoi(f3);
-                       if (ct->tm_hour >= start && ct->tm_hour <= stop) {
-                               fputs("dm: Sorry, games are not available from ", stderr);
-                               hour(start);
-                               fputs(" to ", stderr);
-                               hour(stop);
-                               fputs(" today.\n", stderr);
-                               exit(0);
-                       }
-                       continue;
-               }
+       if (!ct)
+               ct = localtime(&now);
+       if (strcasecmp(s_day, days[ct->tm_wday]))
+               return;
+       if (!isdigit(*s_start) || !isdigit(*s_stop))
+               return;
+       start = atoi(s_start);
+       stop = atoi(s_stop);
+       if (ct->tm_hour >= start && ct->tm_hour <= stop) {
+               fputs("dm: Sorry, games are not available from ", stderr);
+               hour(start);
+               fputs(" to ", stderr);
+               hour(stop);
+               fputs(" today.\n", stderr);
+               exit(0);
        }
 }
 
 /*
        }
 }
 
 /*
- * read_ttys --
- *     read through ttys listed in the control file, decide if this
- *     tty can be used for games.
+ * c_tty --
+ *     decide if this tty can be used for games.
  */
 static
  */
 static
-read_ttys()
+c_tty(tty)
+       char    *tty;
 {
 {
-       register char   *p_tty;
-       char    lbuf[BUFSIZ], f1[40], f2[40],
-               *ttyname(), *rindex();
+       static int      first = 1;
+       static char     *p_tty;
+       char    *rindex();
 
 
-       gametty = ttyname(0);
-       if (p_tty = rindex(gametty, '/'))
-               ++p_tty;
-       while (fgets(lbuf, sizeof(lbuf), cfp)) {
-               if (lignore(lbuf))
-                       continue;
-               if (lcontrol(lbuf))
-                       return;
-               if (sscanf(lbuf, "%s%s", f1, f2) != 2)
-                       continue;
-               if (strcasecmp("badtty", f1))
-                       continue;
-               if (!strcmp(gametty, f2) || p_tty && !strcmp(p_tty, f2)) {
-                       fprintf(stderr, "dm: Sorry, you may not play games on %s.\n", gametty);
-                       exit(0);
-               }
+       if (first) {
+               p_tty = rindex(gametty, '/');
+               first = 0;
+       }
+
+       if (!strcmp(gametty, tty) || p_tty && !strcmp(p_tty, tty)) {
+               fprintf(stderr, "dm: Sorry, you may not play games on %s.\n", gametty);
+               exit(0);
        }
 }
 
 /*
        }
 }
 
 /*
- * read_games --
- *     read through games listed in the control file, decide if this
- *     game can be played now.
+ * c_game --
+ *     see if game can be played now.
  */
 static
  */
 static
-read_games()
+c_game(s_game, s_load, s_users, s_priority)
+       char    *s_game, *s_load, *s_users, *s_priority;
 {
 {
-       char    lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40];
+       static int      found;
+       double  load();
 
 
-       while (fgets(lbuf, sizeof(lbuf), cfp)) {
-               if (lignore(lbuf))
-                       continue;
-               if (lcontrol(lbuf))
-                       return;
-               if (sscanf(lbuf, "%s%s%s%s", f1, f2, f3, f4) != 4)
-                       break;
-               if (!strcmp(game, f1) || !strcasecmp("default", f1)) {
-                       if (isdigit(*f2) && atoi(f2) < load()) {
-                               fputs("dm: Sorry, the load average is too high right now.\n", stderr);
-                               exit(0);
-                       }
-                       if (isdigit(*f3) && atoi(f3) <= users()) {
-                               fputs("dm: Sorry, there are too many users logged on right now.\n", stderr);
-                               exit(0);
-                       }
-                       if (isdigit(*f4))
-                               priority = atoi(f3);
-                       return;
-               }
+       if (found)
+               return;
+       if (strcmp(game, s_game) && strcasecmp("default", s_game))
+               return;
+       ++found;
+       if (isdigit(*s_load) && atoi(s_load) < load()) {
+               fputs("dm: Sorry, the load average is too high right now.\n", stderr);
+               exit(0);
+       }
+       if (isdigit(*s_users) && atoi(s_users) <= users()) {
+               fputs("dm: Sorry, there are too many users logged on right now.\n", stderr);
+               exit(0);
        }
        }
+       if (isdigit(*s_priority))
+               priority = atoi(s_priority);
 }
 
 static struct  nlist nl[] = {
 }
 
 static struct  nlist nl[] = {
@@ -226,13 +227,15 @@ load()
                exit(1);
        }
        (void)lseek(kmem, (long)nl[X_AVENRUN].n_value, L_SET);
                exit(1);
        }
        (void)lseek(kmem, (long)nl[X_AVENRUN].n_value, L_SET);
-       (void)read(kmem, avenrun, sizeof(avenrun));
+       (void)read(kmem, (char *)avenrun, sizeof(avenrun));
        return(avenrun[2]);
 }
 
 /*
  * users --
  *     return current number of users
        return(avenrun[2]);
 }
 
 /*
  * users --
  *     return current number of users
+ *     todo: check idle time; if idle more than X minutes, don't
+ *     count them.
  */
 static
 users()
  */
 static
 users()
index 23ba128..62c9819 100644 (file)
@@ -2,36 +2,55 @@
 .\" All rights reserved.  The Berkeley software License Agreement
 .\" specifies the terms and conditions for redistribution.
 .\"
 .\" All rights reserved.  The Berkeley software License Agreement
 .\" specifies the terms and conditions for redistribution.
 .\"
-.\"    @(#)dm.conf.5   5.1 (Berkeley) %G%
+.\"    @(#)dm.conf.5   5.2 (Berkeley) %G%
 .\"
 .TH DM.CONFIG 5 ""
 .UC 5
 .SH NAME
 dm.config \- dm configuration file
 .SH DESCRIPTION
 .\"
 .TH DM.CONFIG 5 ""
 .UC 5
 .SH NAME
 dm.config \- dm configuration file
 .SH DESCRIPTION
-\fIDm.config\fP is the configuration file for the \fIdm\fP(8) program.  It
-consists of three parts, each separated by percent signs (``%'')
-occurring as the first two characters in a line.  Blank lines and lines
-preceded by a hash mark (``#'') are ignored.
+\fIDm.config\fP is the configuration file for the \fIdm\fP(8) program.
+It consists of lines beginning with one of three keywords, ``badtty'',
+``game'', and ``time''.  All other lines are ignored.
 .PP
 .PP
-The first part of the file sets the standard games hours on a day-by-day
-basis.  Entries consist of three white-space separated fields: the
-unabbreviated day of the week and the beginning and ending time of the
-period of the day when games may not be played.  The time fields are in a
-0 based, 24-hour clock.  Any of the time fields may be a non-numeric
-character, resulting in no time limitations for that day.
+Any tty listed after the keyword ``badtty'' may not have games played on
+it.  Entries consist of two white-space separated fields: the string
+``badtty'' and the ttyname as returned by \fIttyname\fP(3).  For example,
+to keep the uucp dialout, ``tty19'', from being used for games, the
+entry would be:
 .PP
 .PP
-The second part sets the tty limitations.  Entries consist of two white-space
-separated fields: the string "badtty", and the ttyname as returned by
-\fIttyname\fP(3).  Games may not be played on tty's listed in this section.
+.RS
+       badtty  /dev/tty19
+.RE
 .PP
 .PP
-The third, and final, part sets specific game limitations.  Entries consist
-of four white-space separated fields: the name of a game, the highest system
-load average at which the game may be played, the maximum users allowed
-if the game is to be played, and the priority at which the game is to be run.
-Any of these fields may be a non-numeric character, resulting in no game
-limitation or priority based on the field.  The game "default" controls the
-settings for any game not otherwise listed.  Priorities may not be negative.
+Any day/hour combination listed after the keyword ``time'' will disallow
+games during those hours.  Entries consist of four white-space separated
+fields: the string ``time'', the unabbreviated day of the week and the
+beginning and ending time of a period of the day when games may not be
+played.  The time fields are in a 0 based, 24-hour clock.  For example,
+the following entry allows games playing before 8AM and after 5PM on
+Mondays.
+.PP
+.RS
+       time            Monday  8       17
+.RE
+.PP
+Any game listed after the keyword ``game'' will set parameters for a specific
+game.  Entries consist of five white-space separated fields: the keyword
+``game'', the name of a game, the highest system load average at which the
+game may be played, the maximum users allowed if the game is to be played,
+and the priority at which the game is to be run.  Any of these fields may
+start with a non-numeric character, resulting in no game limitation or
+priority based on that field.  The game "default" controls the settings for
+any game not otherwise listed, and must be the last ``game'' entry in the
+file.  Priorities may not be negative.  For example, the following entries
+limits the game ``hack'' to running only when the system has 10 or less
+users and a load average of 5 or less; all other games may be run any time
+the system has 15 or less users.
+.PP
+.RS
+       game            hack            5       10      *
+       game            default *       15      *
+.RE
 .SH "SEE ALSO"
 setpriority(2), ttyname(3), dm(8)
 .SH "SEE ALSO"
 setpriority(2), ttyname(3), dm(8)
-