xref: /csrg-svn/games/dm/dm.c (revision 37412)
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*37412Sbostic static char sccsid[] = "@(#)dm.c	5.11 (Berkeley) 04/12/89";
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>
37*37412Sbostic #include "pathnames.h"
3832216Sbostic 
3932216Sbostic static time_t	now;			/* current time value */
4032227Sbostic static int	priority = 0;		/* priority game runs at */
4132227Sbostic static char	*game,			/* requested game */
4232227Sbostic 		*gametty;		/* from tty? */
4332216Sbostic 
4433788Sbostic /*ARGSUSED*/
4532216Sbostic main(argc, argv)
4633788Sbostic 	int argc;
4733788Sbostic 	char **argv;
4832216Sbostic {
4933788Sbostic 	char *cp, *rindex(), *ttyname();
5033788Sbostic 	time_t time();
5132216Sbostic 
5232216Sbostic 	nogamefile();
5332371Sbostic 	game = (cp = rindex(*argv, '/')) ? ++cp : *argv;
5432216Sbostic 
5532371Sbostic 	if (!strcmp(game, "dm"))
5632227Sbostic 		exit(0);
5732216Sbostic 
5832371Sbostic 	gametty = ttyname(0);
5932371Sbostic 	(void)time(&now);
6032371Sbostic 	read_config();
6132216Sbostic #ifdef LOG
6232216Sbostic 	logfile();
6332216Sbostic #endif
6432216Sbostic 	play(argv);
6532371Sbostic 	/*NOTREACHED*/
6632216Sbostic }
6732216Sbostic 
6832216Sbostic /*
6932216Sbostic  * play --
7032216Sbostic  *	play the game
7132216Sbostic  */
7232216Sbostic static
7332216Sbostic play(args)
7433788Sbostic 	char **args;
7532216Sbostic {
7636812Sbostic 	extern int errno;
7736812Sbostic 	char pbuf[MAXPATHLEN], *strcpy(), *strerror();
7833169Sbostic 
79*37412Sbostic 	(void)strcpy(pbuf, _PATH_HIDE);
80*37412Sbostic 	(void)strcpy(pbuf + sizeof(_PATH_HIDE) - 1, game);
8132371Sbostic 	if (priority > 0)	/* < 0 requires root */
8232371Sbostic 		(void)setpriority(PRIO_PROCESS, 0, priority);
8332227Sbostic 	setgid(getgid());	/* we run setgid kmem; lose it */
8433169Sbostic 	execv(pbuf, args);
8536812Sbostic 	(void)fprintf(stderr, "dm: %s: %s\n", pbuf, strerror(errno));
8632216Sbostic 	exit(1);
8732216Sbostic }
8832216Sbostic 
8932216Sbostic /*
9032371Sbostic  * read_config --
9132371Sbostic  *	read through config file, looking for key words.
9232216Sbostic  */
9332216Sbostic static
9432371Sbostic read_config()
9532216Sbostic {
9633788Sbostic 	FILE *cfp;
9733788Sbostic 	char *control, *host, *index(), *strcpy();
98*37412Sbostic 	char lbuf[BUFSIZ], path[MAXHOSTNAMELEN + sizeof(_PATH_CONFIG)];
9933788Sbostic 	char f1[40], f2[40], f3[40], f4[40], f5[40];
10032371Sbostic 
101*37412Sbostic 	host = &path[sizeof(_PATH_CONFIG)];
10233788Sbostic 	if (gethostname(host, MAXHOSTNAMELEN)) {
10333788Sbostic 		perror("dm: gethostname");
10432371Sbostic 		exit(1);
10532371Sbostic 	}
106*37412Sbostic 	(void)strcpy(path, control = _PATH_CONFIG);
10733788Sbostic 	host[-1] = '.';
10833788Sbostic 	if (host = index(host, '.'))
10933788Sbostic 		*host = '\0';
110*37412Sbostic 	if (!(cfp = fopen(path, "r")) && !(cfp = fopen(control, "r")))
111*37412Sbostic 		return;
11232371Sbostic 	while (fgets(lbuf, sizeof(lbuf), cfp))
11332371Sbostic 		switch(*lbuf) {
11432371Sbostic 		case 'b':		/* badtty */
11532371Sbostic 			if (sscanf(lbuf, "%s%s", f1, f2) != 2 ||
11632371Sbostic 			    strcasecmp(f1, "badtty"))
11732371Sbostic 				break;
11832371Sbostic 			c_tty(f2);
11932371Sbostic 			break;
12032371Sbostic 		case 'g':		/* game */
12132371Sbostic 			if (sscanf(lbuf, "%s%s%s%s%s",
12232371Sbostic 			    f1, f2, f3, f4, f5) != 5 || strcasecmp(f1, "game"))
12332371Sbostic 				break;
12432371Sbostic 			c_game(f2, f3, f4, f5);
12532371Sbostic 			break;
12632371Sbostic 		case 't':		/* time */
12732371Sbostic 			if (sscanf(lbuf, "%s%s%s%s", f1, f2, f3, f4) != 4 ||
12832371Sbostic 			    strcasecmp(f1, "time"))
12932371Sbostic 				break;
13032371Sbostic 			c_day(f2, f3, f4);
13132371Sbostic 		}
13232371Sbostic 	(void)fclose(cfp);
13332371Sbostic }
13432371Sbostic 
13532371Sbostic /*
13632371Sbostic  * c_day --
13732371Sbostic  *	if day is today, see if okay to play
13832371Sbostic  */
13932371Sbostic static
14032371Sbostic c_day(s_day, s_start, s_stop)
14133788Sbostic 	char *s_day, *s_start, *s_stop;
14232371Sbostic {
14333788Sbostic 	static char *days[] = {
14432227Sbostic 		"sunday", "monday", "tuesday", "wednesday",
14532227Sbostic 		"thursday", "friday", "saturday",
14632227Sbostic 	};
14733788Sbostic 	static struct tm *ct;
14833788Sbostic 	int start, stop;
14932216Sbostic 
15032371Sbostic 	if (!ct)
15132371Sbostic 		ct = localtime(&now);
15232371Sbostic 	if (strcasecmp(s_day, days[ct->tm_wday]))
15332371Sbostic 		return;
15432371Sbostic 	if (!isdigit(*s_start) || !isdigit(*s_stop))
15532371Sbostic 		return;
15632371Sbostic 	start = atoi(s_start);
15732371Sbostic 	stop = atoi(s_stop);
15835718Sbostic 	if (ct->tm_hour >= start && ct->tm_hour < stop) {
15932371Sbostic 		fputs("dm: Sorry, games are not available from ", stderr);
16032371Sbostic 		hour(start);
16132371Sbostic 		fputs(" to ", stderr);
16232371Sbostic 		hour(stop);
16332371Sbostic 		fputs(" today.\n", stderr);
16432371Sbostic 		exit(0);
16532216Sbostic 	}
16632216Sbostic }
16732216Sbostic 
16832216Sbostic /*
16932371Sbostic  * c_tty --
17032371Sbostic  *	decide if this tty can be used for games.
17132227Sbostic  */
17232227Sbostic static
17332371Sbostic c_tty(tty)
17433788Sbostic 	char *tty;
17532227Sbostic {
17633788Sbostic 	static int first = 1;
17733788Sbostic 	static char *p_tty;
17833788Sbostic 	char *rindex();
17932227Sbostic 
18032371Sbostic 	if (first) {
18132371Sbostic 		p_tty = rindex(gametty, '/');
18232371Sbostic 		first = 0;
18332227Sbostic 	}
18432371Sbostic 
18532371Sbostic 	if (!strcmp(gametty, tty) || p_tty && !strcmp(p_tty, tty)) {
18632371Sbostic 		fprintf(stderr, "dm: Sorry, you may not play games on %s.\n", gametty);
18732371Sbostic 		exit(0);
18832371Sbostic 	}
18932227Sbostic }
19032227Sbostic 
19132227Sbostic /*
19232371Sbostic  * c_game --
19332371Sbostic  *	see if game can be played now.
19432216Sbostic  */
19532216Sbostic static
19632371Sbostic c_game(s_game, s_load, s_users, s_priority)
19733788Sbostic 	char *s_game, *s_load, *s_users, *s_priority;
19832216Sbostic {
19933788Sbostic 	static int found;
20033788Sbostic 	double load();
20132216Sbostic 
20232371Sbostic 	if (found)
20332371Sbostic 		return;
20432371Sbostic 	if (strcmp(game, s_game) && strcasecmp("default", s_game))
20532371Sbostic 		return;
20632371Sbostic 	++found;
20732371Sbostic 	if (isdigit(*s_load) && atoi(s_load) < load()) {
20832371Sbostic 		fputs("dm: Sorry, the load average is too high right now.\n", stderr);
20932371Sbostic 		exit(0);
21032216Sbostic 	}
21132371Sbostic 	if (isdigit(*s_users) && atoi(s_users) <= users()) {
21232371Sbostic 		fputs("dm: Sorry, there are too many users logged on right now.\n", stderr);
21332371Sbostic 		exit(0);
21432371Sbostic 	}
21532371Sbostic 	if (isdigit(*s_priority))
21632371Sbostic 		priority = atoi(s_priority);
21732216Sbostic }
21832216Sbostic 
21933788Sbostic static struct nlist nl[] = {
22032216Sbostic 	{ "_avenrun" },
22132216Sbostic #define	X_AVENRUN	0
22232216Sbostic 	{ "" },
22332216Sbostic };
22432216Sbostic 
22532216Sbostic /*
22632216Sbostic  * load --
22732216Sbostic  *	return 15 minute load average
22832216Sbostic  */
22932216Sbostic static double
23032216Sbostic load()
23132216Sbostic {
23233788Sbostic 	double avenrun[3];
23333788Sbostic 	int kmem;
23433788Sbostic 	long lseek();
23532216Sbostic 
23632216Sbostic 	if (nlist("/vmunix", nl)) {
23732216Sbostic 		fputs("dm: nlist of /vmunix failed.\n", stderr);
23832216Sbostic 		exit(1);
23932216Sbostic 	}
24032216Sbostic 	if ((kmem = open("/dev/kmem", O_RDONLY, 0)) < 0) {
24132216Sbostic 		perror("dm: /dev/kmem");
24232216Sbostic 		exit(1);
24332216Sbostic 	}
24432216Sbostic 	(void)lseek(kmem, (long)nl[X_AVENRUN].n_value, L_SET);
24532371Sbostic 	(void)read(kmem, (char *)avenrun, sizeof(avenrun));
24632216Sbostic 	return(avenrun[2]);
24732216Sbostic }
24832216Sbostic 
24932216Sbostic /*
25032216Sbostic  * users --
25132216Sbostic  *	return current number of users
25232371Sbostic  *	todo: check idle time; if idle more than X minutes, don't
25332371Sbostic  *	count them.
25432216Sbostic  */
25532216Sbostic static
25632216Sbostic users()
25732216Sbostic {
25833788Sbostic 	register int nusers, utmp;
25933788Sbostic 	struct utmp buf;
26032216Sbostic 
26132216Sbostic 	if ((utmp = open("/etc/utmp", O_RDONLY, 0)) < 0) {
26232216Sbostic 		perror("dm: /etc/utmp");
26332216Sbostic 		exit(1);
26432216Sbostic 	}
26532216Sbostic 	for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;)
26632216Sbostic 		if (buf.ut_name[0] != '\0')
26732216Sbostic 			++nusers;
26832216Sbostic 	return(nusers);
26932216Sbostic }
27032216Sbostic 
27132216Sbostic static
27232216Sbostic nogamefile()
27332216Sbostic {
27433788Sbostic 	register int fd, n;
27533788Sbostic 	char buf[BUFSIZ];
27632216Sbostic 
277*37412Sbostic 	if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
27832216Sbostic #define	MESG	"Sorry, no games right now.\n\n"
27932216Sbostic 		(void)write(2, MESG, sizeof(MESG) - 1);
28032216Sbostic 		while ((n = read(fd, buf, sizeof(buf))) > 0)
28132216Sbostic 			(void)write(2, buf, n);
28232216Sbostic 		exit(1);
28332216Sbostic 	}
28432216Sbostic }
28532216Sbostic 
28632216Sbostic /*
28732216Sbostic  * hour --
28832216Sbostic  *	print out the hour in human form
28932216Sbostic  */
29032227Sbostic static
29132216Sbostic hour(h)
29233788Sbostic 	int h;
29332216Sbostic {
29432227Sbostic 	switch(h) {
29532227Sbostic 	case 0:
29632227Sbostic 		fputs("midnight", stderr);
29732227Sbostic 		break;
29832227Sbostic 	case 12:
29932227Sbostic 		fputs("noon", stderr);
30032227Sbostic 		break;
30132227Sbostic 	default:
30232227Sbostic 		if (h > 12)
30332227Sbostic 			fprintf(stderr, "%dpm", h - 12);
30432227Sbostic 		else
30532227Sbostic 			fprintf(stderr, "%dam", h);
30632227Sbostic 	}
30732216Sbostic }
30832216Sbostic 
30932216Sbostic #ifdef LOG
31033788Sbostic /*
31133788Sbostic  * logfile --
31233788Sbostic  *	log play of game
31333788Sbostic  */
31432216Sbostic static
31532216Sbostic logfile()
31632216Sbostic {
31733788Sbostic 	struct passwd *pw, *getpwuid();
31833788Sbostic 	FILE *lp;
31933788Sbostic 	uid_t uid;
32033788Sbostic 	int lock_cnt;
32133788Sbostic 	char *ctime();
32232216Sbostic 
323*37412Sbostic 	if (lp = fopen(_PATH_LOG, "a")) {
32432216Sbostic 		for (lock_cnt = 0;; ++lock_cnt) {
32532216Sbostic 			if (!flock(fileno(lp), LOCK_EX))
32632216Sbostic 				break;
32732216Sbostic 			if (lock_cnt == 4) {
32832216Sbostic 				perror("dm: log lock");
32932216Sbostic 				(void)fclose(lp);
33032216Sbostic 				return;
33132216Sbostic 			}
33232216Sbostic 			sleep((u_int)1);
33332216Sbostic 		}
33432216Sbostic 		if (pw = getpwuid(uid = getuid()))
33532216Sbostic 			fputs(pw->pw_name, lp);
33632216Sbostic 		else
33732216Sbostic 			fprintf(lp, "%u", uid);
33832227Sbostic 		fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
33932216Sbostic 		(void)fclose(lp);
34032216Sbostic 		(void)flock(fileno(lp), LOCK_UN);
34132216Sbostic 	}
34232216Sbostic }
34333788Sbostic #endif /* LOG */
344