xref: /csrg-svn/games/dm/dm.c (revision 38197)
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*38197Smckusick static char sccsid[] = "@(#)dm.c	5.12 (Berkeley) 05/29/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>
3737412Sbostic #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 
7937412Sbostic 	(void)strcpy(pbuf, _PATH_HIDE);
8037412Sbostic 	(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();
9837412Sbostic 	char lbuf[BUFSIZ], path[MAXHOSTNAMELEN + sizeof(_PATH_CONFIG)];
9933788Sbostic 	char f1[40], f2[40], f3[40], f4[40], f5[40];
10032371Sbostic 
10137412Sbostic 	host = &path[sizeof(_PATH_CONFIG)];
10233788Sbostic 	if (gethostname(host, MAXHOSTNAMELEN)) {
10333788Sbostic 		perror("dm: gethostname");
10432371Sbostic 		exit(1);
10532371Sbostic 	}
10637412Sbostic 	(void)strcpy(path, control = _PATH_CONFIG);
10733788Sbostic 	host[-1] = '.';
10833788Sbostic 	if (host = index(host, '.'))
10933788Sbostic 		*host = '\0';
11037412Sbostic 	if (!(cfp = fopen(path, "r")) && !(cfp = fopen(control, "r")))
11137412Sbostic 		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 
21932216Sbostic /*
22032216Sbostic  * load --
22132216Sbostic  *	return 15 minute load average
22232216Sbostic  */
22332216Sbostic static double
22432216Sbostic load()
22532216Sbostic {
22633788Sbostic 	double avenrun[3];
22732216Sbostic 
228*38197Smckusick 	if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0) {
229*38197Smckusick 		fputs("dm: getloadavg() failed.\n", stderr);
23032216Sbostic 		exit(1);
23132216Sbostic 	}
23232216Sbostic 	return(avenrun[2]);
23332216Sbostic }
23432216Sbostic 
23532216Sbostic /*
23632216Sbostic  * users --
23732216Sbostic  *	return current number of users
23832371Sbostic  *	todo: check idle time; if idle more than X minutes, don't
23932371Sbostic  *	count them.
24032216Sbostic  */
24132216Sbostic static
24232216Sbostic users()
24332216Sbostic {
24433788Sbostic 	register int nusers, utmp;
24533788Sbostic 	struct utmp buf;
24632216Sbostic 
24732216Sbostic 	if ((utmp = open("/etc/utmp", O_RDONLY, 0)) < 0) {
24832216Sbostic 		perror("dm: /etc/utmp");
24932216Sbostic 		exit(1);
25032216Sbostic 	}
25132216Sbostic 	for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;)
25232216Sbostic 		if (buf.ut_name[0] != '\0')
25332216Sbostic 			++nusers;
25432216Sbostic 	return(nusers);
25532216Sbostic }
25632216Sbostic 
25732216Sbostic static
25832216Sbostic nogamefile()
25932216Sbostic {
26033788Sbostic 	register int fd, n;
26133788Sbostic 	char buf[BUFSIZ];
26232216Sbostic 
26337412Sbostic 	if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
26432216Sbostic #define	MESG	"Sorry, no games right now.\n\n"
26532216Sbostic 		(void)write(2, MESG, sizeof(MESG) - 1);
26632216Sbostic 		while ((n = read(fd, buf, sizeof(buf))) > 0)
26732216Sbostic 			(void)write(2, buf, n);
26832216Sbostic 		exit(1);
26932216Sbostic 	}
27032216Sbostic }
27132216Sbostic 
27232216Sbostic /*
27332216Sbostic  * hour --
27432216Sbostic  *	print out the hour in human form
27532216Sbostic  */
27632227Sbostic static
27732216Sbostic hour(h)
27833788Sbostic 	int h;
27932216Sbostic {
28032227Sbostic 	switch(h) {
28132227Sbostic 	case 0:
28232227Sbostic 		fputs("midnight", stderr);
28332227Sbostic 		break;
28432227Sbostic 	case 12:
28532227Sbostic 		fputs("noon", stderr);
28632227Sbostic 		break;
28732227Sbostic 	default:
28832227Sbostic 		if (h > 12)
28932227Sbostic 			fprintf(stderr, "%dpm", h - 12);
29032227Sbostic 		else
29132227Sbostic 			fprintf(stderr, "%dam", h);
29232227Sbostic 	}
29332216Sbostic }
29432216Sbostic 
29532216Sbostic #ifdef LOG
29633788Sbostic /*
29733788Sbostic  * logfile --
29833788Sbostic  *	log play of game
29933788Sbostic  */
30032216Sbostic static
30132216Sbostic logfile()
30232216Sbostic {
30333788Sbostic 	struct passwd *pw, *getpwuid();
30433788Sbostic 	FILE *lp;
30533788Sbostic 	uid_t uid;
30633788Sbostic 	int lock_cnt;
30733788Sbostic 	char *ctime();
30832216Sbostic 
30937412Sbostic 	if (lp = fopen(_PATH_LOG, "a")) {
31032216Sbostic 		for (lock_cnt = 0;; ++lock_cnt) {
31132216Sbostic 			if (!flock(fileno(lp), LOCK_EX))
31232216Sbostic 				break;
31332216Sbostic 			if (lock_cnt == 4) {
31432216Sbostic 				perror("dm: log lock");
31532216Sbostic 				(void)fclose(lp);
31632216Sbostic 				return;
31732216Sbostic 			}
31832216Sbostic 			sleep((u_int)1);
31932216Sbostic 		}
32032216Sbostic 		if (pw = getpwuid(uid = getuid()))
32132216Sbostic 			fputs(pw->pw_name, lp);
32232216Sbostic 		else
32332216Sbostic 			fprintf(lp, "%u", uid);
32432227Sbostic 		fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
32532216Sbostic 		(void)fclose(lp);
32632216Sbostic 		(void)flock(fileno(lp), LOCK_UN);
32732216Sbostic 	}
32832216Sbostic }
32933788Sbostic #endif /* LOG */
330