132216Sbostic /* 232216Sbostic * Copyright (c) 1987 Regents of the University of California. 332704Sbostic * All rights reserved. 432704Sbostic * 532704Sbostic * Redistribution and use in source and binary forms are permitted 634782Sbostic * provided that the above copyright notice and this paragraph are 734782Sbostic * duplicated in all such forms and that any documentation, 834782Sbostic * advertising materials, and other materials related to such 934782Sbostic * distribution and use acknowledge that the software was developed 1034782Sbostic * by the University of California, Berkeley. The name of the 1134782Sbostic * University may not be used to endorse or promote products derived 1234782Sbostic * from this software without specific prior written permission. 1334782Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434782Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534782Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1632216Sbostic */ 1732216Sbostic 1832216Sbostic #ifndef lint 1932216Sbostic char copyright[] = 2032216Sbostic "@(#) Copyright (c) 1987 Regents of the University of California.\n\ 2132216Sbostic All rights reserved.\n"; 2232704Sbostic #endif /* not lint */ 2332216Sbostic 2432216Sbostic #ifndef lint 25*35718Sbostic static char sccsid[] = "@(#)dm.c 5.9 (Berkeley) 09/26/88"; 2632704Sbostic #endif /* not lint */ 2732216Sbostic 2832216Sbostic #include <sys/param.h> 2932216Sbostic #include <sys/file.h> 3032216Sbostic #include <sys/time.h> 3132216Sbostic #include <sys/resource.h> 3232216Sbostic #include <pwd.h> 3332216Sbostic #include <utmp.h> 3432216Sbostic #include <nlist.h> 3532216Sbostic #include <stdio.h> 3632216Sbostic #include <ctype.h> 3732216Sbostic 3832216Sbostic static time_t now; /* current time value */ 3932227Sbostic static int priority = 0; /* priority game runs at */ 4032227Sbostic static char *game, /* requested game */ 4132227Sbostic *gametty; /* from tty? */ 4232216Sbostic 4333788Sbostic /*ARGSUSED*/ 4432216Sbostic main(argc, argv) 4533788Sbostic int argc; 4633788Sbostic char **argv; 4732216Sbostic { 4833788Sbostic char *cp, *rindex(), *ttyname(); 4933788Sbostic time_t time(); 5032216Sbostic 5132216Sbostic nogamefile(); 5232371Sbostic game = (cp = rindex(*argv, '/')) ? ++cp : *argv; 5332216Sbostic 5432371Sbostic if (!strcmp(game, "dm")) 5532227Sbostic exit(0); 5632216Sbostic 5732371Sbostic gametty = ttyname(0); 5832371Sbostic (void)time(&now); 5932371Sbostic read_config(); 6032216Sbostic #ifdef LOG 6132216Sbostic logfile(); 6232216Sbostic #endif 6332216Sbostic play(argv); 6432371Sbostic /*NOTREACHED*/ 6532216Sbostic } 6632216Sbostic 6732216Sbostic /* 6832216Sbostic * play -- 6932216Sbostic * play the game 7032216Sbostic */ 7133788Sbostic #define GAMEHIDE "/usr/games/hide/" 7232216Sbostic static 7332216Sbostic play(args) 7433788Sbostic char **args; 7532216Sbostic { 7633788Sbostic char pbuf[MAXPATHLEN], *strcpy(); 7733169Sbostic 7833169Sbostic (void)strcpy(pbuf, GAMEHIDE); 7933169Sbostic (void)strcpy(pbuf + sizeof(GAMEHIDE) - 1, game); 8032371Sbostic if (priority > 0) /* < 0 requires root */ 8132371Sbostic (void)setpriority(PRIO_PROCESS, 0, priority); 8232227Sbostic setgid(getgid()); /* we run setgid kmem; lose it */ 8333169Sbostic execv(pbuf, args); 8432227Sbostic perror("dm"); 8532216Sbostic exit(1); 8632216Sbostic } 8732216Sbostic 8832216Sbostic /* 8932371Sbostic * read_config -- 9032371Sbostic * read through config file, looking for key words. 9132216Sbostic */ 9233788Sbostic #define CONTROL "/usr/games/dm.config" 9332216Sbostic static 9432371Sbostic read_config() 9532216Sbostic { 9633788Sbostic FILE *cfp; 9733788Sbostic char *control, *host, *index(), *strcpy(); 9833788Sbostic char lbuf[BUFSIZ], path[MAXHOSTNAMELEN + sizeof(CONTROL)]; 9933788Sbostic char f1[40], f2[40], f3[40], f4[40], f5[40]; 10032371Sbostic 10133788Sbostic host = &path[sizeof(CONTROL)]; 10233788Sbostic if (gethostname(host, MAXHOSTNAMELEN)) { 10333788Sbostic perror("dm: gethostname"); 10432371Sbostic exit(1); 10532371Sbostic } 10633788Sbostic (void)strcpy(path, control = CONTROL); 10733788Sbostic host[-1] = '.'; 10833788Sbostic if (host = index(host, '.')) 10933788Sbostic *host = '\0'; 11033788Sbostic if (!(cfp = fopen(path, "r")) && !(cfp = fopen(control, "r"))) { 11133788Sbostic fprintf(stderr, "dm: unable to read %s or %s.\n", 11233788Sbostic path, control); 11333788Sbostic exit(1); 11433788Sbostic } 11532371Sbostic while (fgets(lbuf, sizeof(lbuf), cfp)) 11632371Sbostic switch(*lbuf) { 11732371Sbostic case 'b': /* badtty */ 11832371Sbostic if (sscanf(lbuf, "%s%s", f1, f2) != 2 || 11932371Sbostic strcasecmp(f1, "badtty")) 12032371Sbostic break; 12132371Sbostic c_tty(f2); 12232371Sbostic break; 12332371Sbostic case 'g': /* game */ 12432371Sbostic if (sscanf(lbuf, "%s%s%s%s%s", 12532371Sbostic f1, f2, f3, f4, f5) != 5 || strcasecmp(f1, "game")) 12632371Sbostic break; 12732371Sbostic c_game(f2, f3, f4, f5); 12832371Sbostic break; 12932371Sbostic case 't': /* time */ 13032371Sbostic if (sscanf(lbuf, "%s%s%s%s", f1, f2, f3, f4) != 4 || 13132371Sbostic strcasecmp(f1, "time")) 13232371Sbostic break; 13332371Sbostic c_day(f2, f3, f4); 13432371Sbostic } 13532371Sbostic (void)fclose(cfp); 13632371Sbostic } 13732371Sbostic 13832371Sbostic /* 13932371Sbostic * c_day -- 14032371Sbostic * if day is today, see if okay to play 14132371Sbostic */ 14232371Sbostic static 14332371Sbostic c_day(s_day, s_start, s_stop) 14433788Sbostic char *s_day, *s_start, *s_stop; 14532371Sbostic { 14633788Sbostic static char *days[] = { 14732227Sbostic "sunday", "monday", "tuesday", "wednesday", 14832227Sbostic "thursday", "friday", "saturday", 14932227Sbostic }; 15033788Sbostic static struct tm *ct; 15133788Sbostic int start, stop; 15232216Sbostic 15332371Sbostic if (!ct) 15432371Sbostic ct = localtime(&now); 15532371Sbostic if (strcasecmp(s_day, days[ct->tm_wday])) 15632371Sbostic return; 15732371Sbostic if (!isdigit(*s_start) || !isdigit(*s_stop)) 15832371Sbostic return; 15932371Sbostic start = atoi(s_start); 16032371Sbostic stop = atoi(s_stop); 161*35718Sbostic if (ct->tm_hour >= start && ct->tm_hour < stop) { 16232371Sbostic fputs("dm: Sorry, games are not available from ", stderr); 16332371Sbostic hour(start); 16432371Sbostic fputs(" to ", stderr); 16532371Sbostic hour(stop); 16632371Sbostic fputs(" today.\n", stderr); 16732371Sbostic exit(0); 16832216Sbostic } 16932216Sbostic } 17032216Sbostic 17132216Sbostic /* 17232371Sbostic * c_tty -- 17332371Sbostic * decide if this tty can be used for games. 17432227Sbostic */ 17532227Sbostic static 17632371Sbostic c_tty(tty) 17733788Sbostic char *tty; 17832227Sbostic { 17933788Sbostic static int first = 1; 18033788Sbostic static char *p_tty; 18133788Sbostic char *rindex(); 18232227Sbostic 18332371Sbostic if (first) { 18432371Sbostic p_tty = rindex(gametty, '/'); 18532371Sbostic first = 0; 18632227Sbostic } 18732371Sbostic 18832371Sbostic if (!strcmp(gametty, tty) || p_tty && !strcmp(p_tty, tty)) { 18932371Sbostic fprintf(stderr, "dm: Sorry, you may not play games on %s.\n", gametty); 19032371Sbostic exit(0); 19132371Sbostic } 19232227Sbostic } 19332227Sbostic 19432227Sbostic /* 19532371Sbostic * c_game -- 19632371Sbostic * see if game can be played now. 19732216Sbostic */ 19832216Sbostic static 19932371Sbostic c_game(s_game, s_load, s_users, s_priority) 20033788Sbostic char *s_game, *s_load, *s_users, *s_priority; 20132216Sbostic { 20233788Sbostic static int found; 20333788Sbostic double load(); 20432216Sbostic 20532371Sbostic if (found) 20632371Sbostic return; 20732371Sbostic if (strcmp(game, s_game) && strcasecmp("default", s_game)) 20832371Sbostic return; 20932371Sbostic ++found; 21032371Sbostic if (isdigit(*s_load) && atoi(s_load) < load()) { 21132371Sbostic fputs("dm: Sorry, the load average is too high right now.\n", stderr); 21232371Sbostic exit(0); 21332216Sbostic } 21432371Sbostic if (isdigit(*s_users) && atoi(s_users) <= users()) { 21532371Sbostic fputs("dm: Sorry, there are too many users logged on right now.\n", stderr); 21632371Sbostic exit(0); 21732371Sbostic } 21832371Sbostic if (isdigit(*s_priority)) 21932371Sbostic priority = atoi(s_priority); 22032216Sbostic } 22132216Sbostic 22233788Sbostic static struct nlist nl[] = { 22332216Sbostic { "_avenrun" }, 22432216Sbostic #define X_AVENRUN 0 22532216Sbostic { "" }, 22632216Sbostic }; 22732216Sbostic 22832216Sbostic /* 22932216Sbostic * load -- 23032216Sbostic * return 15 minute load average 23132216Sbostic */ 23232216Sbostic static double 23332216Sbostic load() 23432216Sbostic { 23533788Sbostic double avenrun[3]; 23633788Sbostic int kmem; 23733788Sbostic long lseek(); 23832216Sbostic 23932216Sbostic if (nlist("/vmunix", nl)) { 24032216Sbostic fputs("dm: nlist of /vmunix failed.\n", stderr); 24132216Sbostic exit(1); 24232216Sbostic } 24332216Sbostic if ((kmem = open("/dev/kmem", O_RDONLY, 0)) < 0) { 24432216Sbostic perror("dm: /dev/kmem"); 24532216Sbostic exit(1); 24632216Sbostic } 24732216Sbostic (void)lseek(kmem, (long)nl[X_AVENRUN].n_value, L_SET); 24832371Sbostic (void)read(kmem, (char *)avenrun, sizeof(avenrun)); 24932216Sbostic return(avenrun[2]); 25032216Sbostic } 25132216Sbostic 25232216Sbostic /* 25332216Sbostic * users -- 25432216Sbostic * return current number of users 25532371Sbostic * todo: check idle time; if idle more than X minutes, don't 25632371Sbostic * count them. 25732216Sbostic */ 25832216Sbostic static 25932216Sbostic users() 26032216Sbostic { 26133788Sbostic register int nusers, utmp; 26233788Sbostic struct utmp buf; 26332216Sbostic 26432216Sbostic if ((utmp = open("/etc/utmp", O_RDONLY, 0)) < 0) { 26532216Sbostic perror("dm: /etc/utmp"); 26632216Sbostic exit(1); 26732216Sbostic } 26832216Sbostic for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;) 26932216Sbostic if (buf.ut_name[0] != '\0') 27032216Sbostic ++nusers; 27132216Sbostic return(nusers); 27232216Sbostic } 27332216Sbostic 27432216Sbostic /* 27532216Sbostic * nogamefile -- 27632216Sbostic * if the file NOGAMING exists, no games allowed. 27732216Sbostic * file may also contain a message for the user. 27832216Sbostic */ 27933788Sbostic #define NOGAMING "/usr/games/nogames" 28032216Sbostic static 28132216Sbostic nogamefile() 28232216Sbostic { 28333788Sbostic register int fd, n; 28433788Sbostic char buf[BUFSIZ]; 28532216Sbostic 28632216Sbostic if ((fd = open(NOGAMING, O_RDONLY, 0)) >= 0) { 28732216Sbostic #define MESG "Sorry, no games right now.\n\n" 28832216Sbostic (void)write(2, MESG, sizeof(MESG) - 1); 28932216Sbostic while ((n = read(fd, buf, sizeof(buf))) > 0) 29032216Sbostic (void)write(2, buf, n); 29132216Sbostic exit(1); 29232216Sbostic } 29332216Sbostic } 29432216Sbostic 29532216Sbostic /* 29632216Sbostic * hour -- 29732216Sbostic * print out the hour in human form 29832216Sbostic */ 29932227Sbostic static 30032216Sbostic hour(h) 30133788Sbostic int h; 30232216Sbostic { 30332227Sbostic switch(h) { 30432227Sbostic case 0: 30532227Sbostic fputs("midnight", stderr); 30632227Sbostic break; 30732227Sbostic case 12: 30832227Sbostic fputs("noon", stderr); 30932227Sbostic break; 31032227Sbostic default: 31132227Sbostic if (h > 12) 31232227Sbostic fprintf(stderr, "%dpm", h - 12); 31332227Sbostic else 31432227Sbostic fprintf(stderr, "%dam", h); 31532227Sbostic } 31632216Sbostic } 31732216Sbostic 31832216Sbostic #ifdef LOG 31933788Sbostic /* 32033788Sbostic * logfile -- 32133788Sbostic * log play of game 32233788Sbostic */ 32333788Sbostic #define LOGFILE "/usr/adm/dm.log" 32432216Sbostic static 32532216Sbostic logfile() 32632216Sbostic { 32733788Sbostic struct passwd *pw, *getpwuid(); 32833788Sbostic FILE *lp; 32933788Sbostic uid_t uid; 33033788Sbostic int lock_cnt; 33133788Sbostic char *ctime(); 33232216Sbostic 33332216Sbostic if (lp = fopen(LOGFILE, "a")) { 33432216Sbostic for (lock_cnt = 0;; ++lock_cnt) { 33532216Sbostic if (!flock(fileno(lp), LOCK_EX)) 33632216Sbostic break; 33732216Sbostic if (lock_cnt == 4) { 33832216Sbostic perror("dm: log lock"); 33932216Sbostic (void)fclose(lp); 34032216Sbostic return; 34132216Sbostic } 34232216Sbostic sleep((u_int)1); 34332216Sbostic } 34432216Sbostic if (pw = getpwuid(uid = getuid())) 34532216Sbostic fputs(pw->pw_name, lp); 34632216Sbostic else 34732216Sbostic fprintf(lp, "%u", uid); 34832227Sbostic fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now)); 34932216Sbostic (void)fclose(lp); 35032216Sbostic (void)flock(fileno(lp), LOCK_UN); 35132216Sbostic } 35232216Sbostic } 35333788Sbostic #endif /* LOG */ 354