BSD 4_4 release
[unix-history] / usr / src / games / dm / dm.c
index 324d886..b268a5c 100644 (file)
 /*
 /*
- * Copyright (c) 1987 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1987, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1987 Regents of the University of California.\n\
- All rights reserved.\n";
-#endif not lint
+static char copyright[] =
+"@(#) Copyright (c) 1987, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)dm.c       5.1 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)dm.c       8.1 (Berkeley) 5/31/93";
+#endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/file.h>
 #include <sys/time.h>
 #include <sys/resource.h>
 
 #include <sys/param.h>
 #include <sys/file.h>
 #include <sys/time.h>
 #include <sys/resource.h>
-#include <pwd.h>
-#include <utmp.h>
+
+#include <ctype.h>
 #include <nlist.h>
 #include <nlist.h>
+#include <pwd.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <ctype.h>
-
-#define        LOG                     /* if want game usage logged */
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <utmp.h>
 
 
-#define        GAMEHIDE        "/usr/games/hide"
-#define        NOGAMING        "/etc/nogames"
-#define        CONTROL         "/usr/games/game.control"
-#ifdef LOG
-#define        LOGFILE         "/usr/adm/gamelog"
-#endif
+#include "pathnames.h"
 
 
-static struct tm       *ct;            /* current time structure */
+extern int errno;
 static time_t  now;                    /* current time value */
 static time_t  now;                    /* current time value */
-static double  maxload = -1;           /* max load for game */
-static int     maxusers = -1;          /* max users for game */
-               priority = 0;           /* priority game runs at */
-static char    *game;                  /* requested game */
-
-typedef struct dow {
-       char    *day;
-       int     start, stop;
-} DOW;
-
-static DOW     days[] = {
-       "sunday",       25, -1,
-       "monday",       25, -1,
-       "tuesday",      25, -1,
-       "wednesday",    25, -1,
-       "thursday",     25, -1,
-       "friday",       25, -1,
-       "saturday",     25, -1,
-       0,              0,  0,
-};
+static int     priority = 0;           /* priority game runs at */
+static char    *game,                  /* requested game */
+               *gametty;               /* from tty? */
 
 
+int
 main(argc, argv)
 main(argc, argv)
-       int     argc;
-       char    **argv;
+       int argc;
+       char *argv[];
 {
 {
-       FILE    *fp;
-       char    *C, *hour(), *rindex();
-       struct tm       *localtime();
-       time_t  time();
-       double  load();
+       char *cp;
 
        nogamefile();
 
        nogamefile();
+       game = (cp = rindex(*argv, '/')) ? ++cp : *argv;
 
 
-       if (argc == 1) {
-               fputs("dm: game\n", stderr);
-               exit(1);
-       }
-
-       if (!(fp = fopen(CONTROL, "r"))) {
-               fprintf(stderr, "dm: unable to read %s.\n", CONTROL);
-               exit(1);
-       }
-
-       read_days(fp);
-       (void)time(&now);
-       ct = localtime(&now);
-       if (ct->tm_hour >= days[ct->tm_wday].start && ct->tm_hour <= days[ct->tm_wday].stop) {
-               fprintf(stderr, "dm: Sorry, games are only available from %s to %s today.\n", hour(days[ct->tm_wday].start), hour(days[ct->tm_wday].stop));
-               exit(0);
-       }
-
-       ++argv;
-       game = (C = rindex(*argv, '/')) ? ++C : *argv;
-
-       read_games(fp);
-       if (maxusers >= 0 && maxusers <= users()) {
-               fputs("dm: Sorry, there are too many users logged on right now.\n", stderr);
-               exit(0);
-       }
-       if (maxload >= 0 && maxload < load()) {
-               fputs("dm: Sorry, the load average is too high right now.\n", stderr);
+       if (!strcmp(game, "dm"))
                exit(0);
                exit(0);
-       }
-       (void)fclose(fp);
 
 
+       gametty = ttyname(0);
+       (void)time(&now);
+       read_config();
 #ifdef LOG
        logfile();
 #endif
        play(argv);
 #ifdef LOG
        logfile();
 #endif
        play(argv);
+       /*NOTREACHED*/
 }
 
 /*
  * play --
  *     play the game
  */
 }
 
 /*
  * play --
  *     play the game
  */
-static
 play(args)
 play(args)
-       char    **args;
+       char **args;
 {
 {
-       uid_t   uid;
-       gid_t   gid;
-
-       if (chdir(GAMEHIDE)) {
-               perror("dm: chdir");
-               exit(1);
-       }
-       if (priority && setpriority(PRIO_PROCESS, 0, priority) < 0)
-               fputs("dm: unable to set priority!\n", stderr);
-       uid = getuid();
-       setreuid(uid, uid);
-       gid = getgid();
-       setregid(gid, gid);
-       execv(game, args);
-       fprintf(stderr, "dm: can't find %s.\n", game);
+       char pbuf[MAXPATHLEN];
+
+       (void)strcpy(pbuf, _PATH_HIDE);
+       (void)strcpy(pbuf + sizeof(_PATH_HIDE) - 1, game);
+       if (priority > 0)       /* < 0 requires root */
+               (void)setpriority(PRIO_PROCESS, 0, priority);
+       setgid(getgid());       /* we run setgid kmem; lose it */
+       execv(pbuf, args);
+       (void)fprintf(stderr, "dm: %s: %s\n", pbuf, strerror(errno));
        exit(1);
 }
 
 /*
        exit(1);
 }
 
 /*
- * read_days --
- *     read through days listed in the control file
+ * read_config --
+ *     read through config file, looking for key words.
  */
  */
-static
-read_days(fp)
-       register FILE   *fp;
+read_config()
 {
 {
-       register DOW    *dp;
-       char    lbuf[BUFSIZ], f1[20], f2[20], f3[20];
-
-       while (fgets(lbuf, sizeof(lbuf), fp)) {
-               if (lbuf[0] == '\n' || lbuf[0] == '#')
-                       continue;
-               /* special line separates days from game names */
-               if (lbuf[0] == '%' && lbuf[1] == '%')
-                       return;
-               if (sscanf(lbuf, "%s%s%s\n", f1, f2, f3) != 3)
-                       continue;
-               for (dp = days; dp->day; ++dp)
-                       if (dp->start <= 0 && !strcasecmp(dp->day, f1)) {
-                               if (isdigit(*f2))
-                                       dp->start = atoi(f2);
-                               if (isdigit(*f3))
-                                       dp->stop = atoi(f3);
+       FILE *cfp;
+       char lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40];
+
+       if (!(cfp = fopen(_PATH_CONFIG, "r")))
+               return;
+       while (fgets(lbuf, sizeof(lbuf), cfp))
+               switch(*lbuf) {
+               case 'b':               /* badtty */
+                       if (sscanf(lbuf, "%s%s", f1, f2) != 2 ||
+                           strcasecmp(f1, "badtty"))
                                break;
                                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
+ */
+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 struct tm *ct;
+       int start, stop;
+
+       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_games --
- *     read through games listed in the control file
+ * c_tty --
+ *     decide if this tty can be used for games.
  */
  */
-static
-read_games(fp)
-       register FILE   *fp;
+c_tty(tty)
+       char *tty;
 {
 {
-       register char   *C;
-       char    lbuf[BUFSIZ], f1[20], f2[20], f3[20];
-       double  atof();
+       static int first = 1;
+       static char *p_tty;
 
 
-       while (fgets(lbuf, sizeof(lbuf), fp)) {
-               if (lbuf[0] == '\n' || lbuf[0] == '#')
-                       continue;
-               for (C = lbuf; *C && !isspace(*C); ++C);
-               *C = '\0';
-               if (!strcmp(game, lbuf) || !strcasecmp("default", lbuf)) {
-                       if (sscanf(++C, "%s%s%s\n", f1, f2, f3) != 3)
-                               break;
-                       if (isdigit(*f1))
-                               maxload = atof(f1);
-                       if (isdigit(*f2))
-                               maxusers = atoi(f2);
-                       if (isdigit(*f3) || *f3 == '-' || *f3 == '+')
-                               priority = atoi(f3);
-                       break;
-               }
+       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);
        }
 }
 
        }
 }
 
-static struct  nlist nl[] = {
-       { "_avenrun" },
-#define        X_AVENRUN       0
-       { "" },
-};
+/*
+ * c_game --
+ *     see if game can be played now.
+ */
+c_game(s_game, s_load, s_users, s_priority)
+       char *s_game, *s_load, *s_users, *s_priority;
+{
+       static int found;
+       double load();
+
+       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);
+}
 
 /*
  * load --
  *     return 15 minute load average
  */
 
 /*
  * load --
  *     return 15 minute load average
  */
-static double
+double
 load()
 {
 load()
 {
-       double  avenrun[3];
-       int     kmem;
-       long    lseek();
+       double avenrun[3];
 
 
-       if (nlist("/vmunix", nl)) {
-               fputs("dm: nlist of /vmunix failed.\n", stderr);
-               exit(1);
-       }
-       if ((kmem = open("/dev/kmem", O_RDONLY, 0)) < 0) {
-               perror("dm: /dev/kmem");
+       if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0) {
+               fputs("dm: getloadavg() failed.\n", stderr);
                exit(1);
        }
                exit(1);
        }
-       (void)lseek(kmem, (long)nl[X_AVENRUN].n_value, L_SET);
-       (void)read(kmem, 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()
 {
 users()
 {
-       register int    nusers,
-                       utmp;
-       struct utmp     buf;
+       
+       register int nusers, utmp;
+       struct utmp buf;
 
 
-       if ((utmp = open("/etc/utmp", O_RDONLY, 0)) < 0) {
-               perror("dm: /etc/utmp");
+       if ((utmp = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
+               (void)fprintf(stderr, "dm: %s: %s\n",
+                   _PATH_UTMP, strerror(errno));
                exit(1);
        }
        for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;)
                exit(1);
        }
        for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;)
@@ -244,18 +258,12 @@ users()
        return(nusers);
 }
 
        return(nusers);
 }
 
-/*
- * nogamefile --
- *     if the file NOGAMING exists, no games allowed.
- *     file may also contain a message for the user.
- */
-static
 nogamefile()
 {
 nogamefile()
 {
-       register int    fd, n;
-       char    buf[BUFSIZ];
+       register int fd, n;
+       char buf[BUFSIZ];
 
 
-       if ((fd = open(NOGAMING, O_RDONLY, 0)) >= 0) {
+       if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
 #define        MESG    "Sorry, no games right now.\n\n"
                (void)write(2, MESG, sizeof(MESG) - 1);
                while ((n = read(fd, buf, sizeof(buf))) > 0)
 #define        MESG    "Sorry, no games right now.\n\n"
                (void)write(2, MESG, sizeof(MESG) - 1);
                while ((n = read(fd, buf, sizeof(buf))) > 0)
@@ -268,34 +276,37 @@ nogamefile()
  * hour --
  *     print out the hour in human form
  */
  * hour --
  *     print out the hour in human form
  */
-static char *
 hour(h)
 hour(h)
-       int     h;
+       int h;
 {
 {
-       static char     buf[20];
-
-       if (!h)
-               return("midnight");
-       if (h == 12)
-               return("noon");
-       if (h < 12)
-               (void)sprintf(buf, "%d pm", h - 12);
-       else
-               (void)sprintf(buf, "%d am", h);
-       return(buf);
+       switch(h) {
+       case 0:
+               fputs("midnight", stderr);
+               break;
+       case 12:
+               fputs("noon", stderr);
+               break;
+       default:
+               if (h > 12)
+                       fprintf(stderr, "%dpm", h - 12);
+               else
+                       fprintf(stderr, "%dam", h);
+       }
 }
 
 #ifdef LOG
 }
 
 #ifdef LOG
-static
+/*
+ * logfile --
+ *     log play of game
+ */
 logfile()
 {
 logfile()
 {
-       struct passwd   *pw, *getpwuid();
-       FILE    *lp;
-       uid_t   uid;
-       int     lock_cnt;
-       char    *ctime(), *ttyname();
+       struct passwd *pw;
+       FILE *lp;
+       uid_t uid;
+       int lock_cnt;
 
 
-       if (lp = fopen(LOGFILE, "a")) {
+       if (lp = fopen(_PATH_LOG, "a")) {
                for (lock_cnt = 0;; ++lock_cnt) {
                        if (!flock(fileno(lp), LOCK_EX))
                                break;
                for (lock_cnt = 0;; ++lock_cnt) {
                        if (!flock(fileno(lp), LOCK_EX))
                                break;
@@ -310,9 +321,9 @@ logfile()
                        fputs(pw->pw_name, lp);
                else
                        fprintf(lp, "%u", uid);
                        fputs(pw->pw_name, lp);
                else
                        fprintf(lp, "%u", uid);
-               fprintf(lp, "\t%s\t%s\t%s", game, ttyname(0), ctime(&now));
+               fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
                (void)fclose(lp);
                (void)flock(fileno(lp), LOCK_UN);
        }
 }
                (void)fclose(lp);
                (void)flock(fileno(lp), LOCK_UN);
        }
 }
-#endif
+#endif /* LOG */