xref: /csrg-svn/games/dm/dm.c (revision 41188)
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*41188Sbostic static char sccsid[] = "@(#)dm.c	5.14 (Berkeley) 05/01/90";
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 
39*41188Sbostic extern int errno;
4032216Sbostic static time_t	now;			/* current time value */
4132227Sbostic static int	priority = 0;		/* priority game runs at */
4232227Sbostic static char	*game,			/* requested game */
4332227Sbostic 		*gametty;		/* from tty? */
4432216Sbostic 
4533788Sbostic /*ARGSUSED*/
4632216Sbostic main(argc, argv)
4733788Sbostic 	int argc;
4833788Sbostic 	char **argv;
4932216Sbostic {
5033788Sbostic 	char *cp, *rindex(), *ttyname();
5133788Sbostic 	time_t time();
5232216Sbostic 
5332216Sbostic 	nogamefile();
5432371Sbostic 	game = (cp = rindex(*argv, '/')) ? ++cp : *argv;
5532216Sbostic 
5632371Sbostic 	if (!strcmp(game, "dm"))
5732227Sbostic 		exit(0);
5832216Sbostic 
5932371Sbostic 	gametty = ttyname(0);
6032371Sbostic 	(void)time(&now);
6132371Sbostic 	read_config();
6232216Sbostic #ifdef LOG
6332216Sbostic 	logfile();
6432216Sbostic #endif
6532216Sbostic 	play(argv);
6632371Sbostic 	/*NOTREACHED*/
6732216Sbostic }
6832216Sbostic 
6932216Sbostic /*
7032216Sbostic  * play --
7132216Sbostic  *	play the game
7232216Sbostic  */
7332216Sbostic static
7432216Sbostic play(args)
7533788Sbostic 	char **args;
7632216Sbostic {
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;
9739891Sbostic 	char lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40];
9832371Sbostic 
9939891Sbostic 	if (!(cfp = fopen(_PATH_CONFIG, "r")))
10037412Sbostic 		return;
10132371Sbostic 	while (fgets(lbuf, sizeof(lbuf), cfp))
10232371Sbostic 		switch(*lbuf) {
10332371Sbostic 		case 'b':		/* badtty */
10432371Sbostic 			if (sscanf(lbuf, "%s%s", f1, f2) != 2 ||
10532371Sbostic 			    strcasecmp(f1, "badtty"))
10632371Sbostic 				break;
10732371Sbostic 			c_tty(f2);
10832371Sbostic 			break;
10932371Sbostic 		case 'g':		/* game */
11032371Sbostic 			if (sscanf(lbuf, "%s%s%s%s%s",
11132371Sbostic 			    f1, f2, f3, f4, f5) != 5 || strcasecmp(f1, "game"))
11232371Sbostic 				break;
11332371Sbostic 			c_game(f2, f3, f4, f5);
11432371Sbostic 			break;
11532371Sbostic 		case 't':		/* time */
11632371Sbostic 			if (sscanf(lbuf, "%s%s%s%s", f1, f2, f3, f4) != 4 ||
11732371Sbostic 			    strcasecmp(f1, "time"))
11832371Sbostic 				break;
11932371Sbostic 			c_day(f2, f3, f4);
12032371Sbostic 		}
12132371Sbostic 	(void)fclose(cfp);
12232371Sbostic }
12332371Sbostic 
12432371Sbostic /*
12532371Sbostic  * c_day --
12632371Sbostic  *	if day is today, see if okay to play
12732371Sbostic  */
12832371Sbostic static
12932371Sbostic c_day(s_day, s_start, s_stop)
13033788Sbostic 	char *s_day, *s_start, *s_stop;
13132371Sbostic {
13233788Sbostic 	static char *days[] = {
13332227Sbostic 		"sunday", "monday", "tuesday", "wednesday",
13432227Sbostic 		"thursday", "friday", "saturday",
13532227Sbostic 	};
13633788Sbostic 	static struct tm *ct;
13733788Sbostic 	int start, stop;
13832216Sbostic 
13932371Sbostic 	if (!ct)
14032371Sbostic 		ct = localtime(&now);
14132371Sbostic 	if (strcasecmp(s_day, days[ct->tm_wday]))
14232371Sbostic 		return;
14332371Sbostic 	if (!isdigit(*s_start) || !isdigit(*s_stop))
14432371Sbostic 		return;
14532371Sbostic 	start = atoi(s_start);
14632371Sbostic 	stop = atoi(s_stop);
14735718Sbostic 	if (ct->tm_hour >= start && ct->tm_hour < stop) {
14832371Sbostic 		fputs("dm: Sorry, games are not available from ", stderr);
14932371Sbostic 		hour(start);
15032371Sbostic 		fputs(" to ", stderr);
15132371Sbostic 		hour(stop);
15232371Sbostic 		fputs(" today.\n", stderr);
15332371Sbostic 		exit(0);
15432216Sbostic 	}
15532216Sbostic }
15632216Sbostic 
15732216Sbostic /*
15832371Sbostic  * c_tty --
15932371Sbostic  *	decide if this tty can be used for games.
16032227Sbostic  */
16132227Sbostic static
16232371Sbostic c_tty(tty)
16333788Sbostic 	char *tty;
16432227Sbostic {
16533788Sbostic 	static int first = 1;
16633788Sbostic 	static char *p_tty;
16733788Sbostic 	char *rindex();
16832227Sbostic 
16932371Sbostic 	if (first) {
17032371Sbostic 		p_tty = rindex(gametty, '/');
17132371Sbostic 		first = 0;
17232227Sbostic 	}
17332371Sbostic 
17432371Sbostic 	if (!strcmp(gametty, tty) || p_tty && !strcmp(p_tty, tty)) {
17532371Sbostic 		fprintf(stderr, "dm: Sorry, you may not play games on %s.\n", gametty);
17632371Sbostic 		exit(0);
17732371Sbostic 	}
17832227Sbostic }
17932227Sbostic 
18032227Sbostic /*
18132371Sbostic  * c_game --
18232371Sbostic  *	see if game can be played now.
18332216Sbostic  */
18432216Sbostic static
18532371Sbostic c_game(s_game, s_load, s_users, s_priority)
18633788Sbostic 	char *s_game, *s_load, *s_users, *s_priority;
18732216Sbostic {
18833788Sbostic 	static int found;
18933788Sbostic 	double load();
19032216Sbostic 
19132371Sbostic 	if (found)
19232371Sbostic 		return;
19332371Sbostic 	if (strcmp(game, s_game) && strcasecmp("default", s_game))
19432371Sbostic 		return;
19532371Sbostic 	++found;
19632371Sbostic 	if (isdigit(*s_load) && atoi(s_load) < load()) {
19732371Sbostic 		fputs("dm: Sorry, the load average is too high right now.\n", stderr);
19832371Sbostic 		exit(0);
19932216Sbostic 	}
20032371Sbostic 	if (isdigit(*s_users) && atoi(s_users) <= users()) {
20132371Sbostic 		fputs("dm: Sorry, there are too many users logged on right now.\n", stderr);
20232371Sbostic 		exit(0);
20332371Sbostic 	}
20432371Sbostic 	if (isdigit(*s_priority))
20532371Sbostic 		priority = atoi(s_priority);
20632216Sbostic }
20732216Sbostic 
20832216Sbostic /*
20932216Sbostic  * load --
21032216Sbostic  *	return 15 minute load average
21132216Sbostic  */
21232216Sbostic static double
21332216Sbostic load()
21432216Sbostic {
21533788Sbostic 	double avenrun[3];
21632216Sbostic 
21738197Smckusick 	if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0) {
21838197Smckusick 		fputs("dm: getloadavg() failed.\n", stderr);
21932216Sbostic 		exit(1);
22032216Sbostic 	}
22132216Sbostic 	return(avenrun[2]);
22232216Sbostic }
22332216Sbostic 
22432216Sbostic /*
22532216Sbostic  * users --
22632216Sbostic  *	return current number of users
22732371Sbostic  *	todo: check idle time; if idle more than X minutes, don't
22832371Sbostic  *	count them.
22932216Sbostic  */
23032216Sbostic static
23132216Sbostic users()
23232216Sbostic {
233*41188Sbostic 
23433788Sbostic 	register int nusers, utmp;
23533788Sbostic 	struct utmp buf;
23632216Sbostic 
237*41188Sbostic 	if ((utmp = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
238*41188Sbostic 		(void)fprintf(stderr, "dm: %s: %s\n",
239*41188Sbostic 		    _PATH_UTMP, strerror(errno));
24032216Sbostic 		exit(1);
24132216Sbostic 	}
24232216Sbostic 	for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;)
24332216Sbostic 		if (buf.ut_name[0] != '\0')
24432216Sbostic 			++nusers;
24532216Sbostic 	return(nusers);
24632216Sbostic }
24732216Sbostic 
24832216Sbostic static
24932216Sbostic nogamefile()
25032216Sbostic {
25133788Sbostic 	register int fd, n;
25233788Sbostic 	char buf[BUFSIZ];
25332216Sbostic 
25437412Sbostic 	if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
25532216Sbostic #define	MESG	"Sorry, no games right now.\n\n"
25632216Sbostic 		(void)write(2, MESG, sizeof(MESG) - 1);
25732216Sbostic 		while ((n = read(fd, buf, sizeof(buf))) > 0)
25832216Sbostic 			(void)write(2, buf, n);
25932216Sbostic 		exit(1);
26032216Sbostic 	}
26132216Sbostic }
26232216Sbostic 
26332216Sbostic /*
26432216Sbostic  * hour --
26532216Sbostic  *	print out the hour in human form
26632216Sbostic  */
26732227Sbostic static
26832216Sbostic hour(h)
26933788Sbostic 	int h;
27032216Sbostic {
27132227Sbostic 	switch(h) {
27232227Sbostic 	case 0:
27332227Sbostic 		fputs("midnight", stderr);
27432227Sbostic 		break;
27532227Sbostic 	case 12:
27632227Sbostic 		fputs("noon", stderr);
27732227Sbostic 		break;
27832227Sbostic 	default:
27932227Sbostic 		if (h > 12)
28032227Sbostic 			fprintf(stderr, "%dpm", h - 12);
28132227Sbostic 		else
28232227Sbostic 			fprintf(stderr, "%dam", h);
28332227Sbostic 	}
28432216Sbostic }
28532216Sbostic 
28632216Sbostic #ifdef LOG
28733788Sbostic /*
28833788Sbostic  * logfile --
28933788Sbostic  *	log play of game
29033788Sbostic  */
29132216Sbostic static
29232216Sbostic logfile()
29332216Sbostic {
29433788Sbostic 	struct passwd *pw, *getpwuid();
29533788Sbostic 	FILE *lp;
29633788Sbostic 	uid_t uid;
29733788Sbostic 	int lock_cnt;
29833788Sbostic 	char *ctime();
29932216Sbostic 
30037412Sbostic 	if (lp = fopen(_PATH_LOG, "a")) {
30132216Sbostic 		for (lock_cnt = 0;; ++lock_cnt) {
30232216Sbostic 			if (!flock(fileno(lp), LOCK_EX))
30332216Sbostic 				break;
30432216Sbostic 			if (lock_cnt == 4) {
30532216Sbostic 				perror("dm: log lock");
30632216Sbostic 				(void)fclose(lp);
30732216Sbostic 				return;
30832216Sbostic 			}
30932216Sbostic 			sleep((u_int)1);
31032216Sbostic 		}
31132216Sbostic 		if (pw = getpwuid(uid = getuid()))
31232216Sbostic 			fputs(pw->pw_name, lp);
31332216Sbostic 		else
31432216Sbostic 			fprintf(lp, "%u", uid);
31532227Sbostic 		fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
31632216Sbostic 		(void)fclose(lp);
31732216Sbostic 		(void)flock(fileno(lp), LOCK_UN);
31832216Sbostic 	}
31932216Sbostic }
32033788Sbostic #endif /* LOG */
321