xref: /netbsd-src/games/dm/dm.c (revision 1182a44c59cae4d586117d55eca24b4b8b173211)
1*1182a44cSrillig /*	$NetBSD: dm.c,v 1.30 2021/05/02 12:50:44 rillig Exp $	*/
2101657d1Scgd 
361f28255Scgd /*
4101657d1Scgd  * Copyright (c) 1987, 1993
5101657d1Scgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
3282090a8bSlukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
342fe2731dSlukem __COPYRIGHT("@(#) Copyright (c) 1987, 1993\
352fe2731dSlukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
39101657d1Scgd #if 0
40101657d1Scgd static char sccsid[] = "@(#)dm.c	8.1 (Berkeley) 5/31/93";
41101657d1Scgd #else
42*1182a44cSrillig __RCSID("$NetBSD: dm.c,v 1.30 2021/05/02 12:50:44 rillig Exp $");
43101657d1Scgd #endif
4461f28255Scgd #endif /* not lint */
4561f28255Scgd 
4661f28255Scgd #include <sys/param.h>
4761f28255Scgd #include <sys/file.h>
4861f28255Scgd #include <sys/time.h>
4961f28255Scgd #include <sys/resource.h>
50101657d1Scgd 
5182090a8bSlukem #include <err.h>
5261f28255Scgd #include <ctype.h>
5382090a8bSlukem #include <errno.h>
54101657d1Scgd #include <pwd.h>
55101657d1Scgd #include <stdio.h>
5682090a8bSlukem #include <stdlib.h>
57f7f4b85cSmycroft #include <string.h>
58101657d1Scgd #include <time.h>
59101657d1Scgd #include <unistd.h>
60101657d1Scgd 
61346b4189Schristos #include "utmpentry.h"
6261f28255Scgd #include "pathnames.h"
6361f28255Scgd 
6461f28255Scgd static time_t	now;			/* current time value */
6561f28255Scgd static int	priority = 0;		/* priority game runs at */
6661f28255Scgd static char	*game,			/* requested game */
6761f28255Scgd 		*gametty;		/* from tty? */
6861f28255Scgd 
69e0ba63feSdholland static void c_day(const char *, const char *, const char *);
70e0ba63feSdholland static void c_game(const char *, const char  *, const char *, const char *);
71e0ba63feSdholland static void c_tty(const char *);
72e0ba63feSdholland static const char *hour(int);
73e0ba63feSdholland static double load(void);
74e0ba63feSdholland static void nogamefile(void);
75e0ba63feSdholland static void play(char **) __dead;
76e0ba63feSdholland static void read_config(void);
77e0ba63feSdholland static int users(void);
78e0ba63feSdholland 
79e0ba63feSdholland #ifdef LOG
80e0ba63feSdholland static void logfile(void);
81e0ba63feSdholland #endif
8282090a8bSlukem 
83101657d1Scgd int
main(int argc __unused,char * argv[])848b0f9554Sperry main(int argc __unused, char *argv[])
8561f28255Scgd {
86101657d1Scgd 	char *cp;
8761f28255Scgd 
8861f28255Scgd 	nogamefile();
8982090a8bSlukem 	game = (cp = strrchr(*argv, '/')) ? ++cp : *argv;
9061f28255Scgd 
9161f28255Scgd 	if (!strcmp(game, "dm"))
9261f28255Scgd 		exit(0);
9361f28255Scgd 
9461f28255Scgd 	gametty = ttyname(0);
955795fc4cShubertf 	unsetenv("TZ");
9661f28255Scgd 	(void)time(&now);
9761f28255Scgd 	read_config();
9861f28255Scgd #ifdef LOG
9961f28255Scgd 	logfile();
10061f28255Scgd #endif
10161f28255Scgd 	play(argv);
10261f28255Scgd 	/*NOTREACHED*/
10382090a8bSlukem 	return (0);
10461f28255Scgd }
10561f28255Scgd 
10661f28255Scgd /*
10761f28255Scgd  * play --
10861f28255Scgd  *	play the game
10961f28255Scgd  */
110e0ba63feSdholland static void
play(char ** args)1114f50e1e4Sjmc play(char **args)
11261f28255Scgd {
113f7f4b85cSmycroft 	char pbuf[MAXPATHLEN];
11461f28255Scgd 
115a30fb66eSjdolecek 	snprintf(pbuf, sizeof(pbuf), "%s%s", _PATH_HIDE, game);
11661f28255Scgd 	if (priority > 0)	/* < 0 requires root */
11761f28255Scgd 		(void)setpriority(PRIO_PROCESS, 0, priority);
11861f28255Scgd 	execv(pbuf, args);
11982090a8bSlukem 	err(1, "%s", pbuf);
12061f28255Scgd }
12161f28255Scgd 
12261f28255Scgd /*
12361f28255Scgd  * read_config --
12461f28255Scgd  *	read through config file, looking for key words.
12561f28255Scgd  */
126e0ba63feSdholland static void
read_config(void)1274f50e1e4Sjmc read_config(void)
12861f28255Scgd {
12961f28255Scgd 	FILE *cfp;
13061f28255Scgd 	char lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40];
13161f28255Scgd 
13261f28255Scgd 	if (!(cfp = fopen(_PATH_CONFIG, "r")))
13361f28255Scgd 		return;
13461f28255Scgd 	while (fgets(lbuf, sizeof(lbuf), cfp))
13561f28255Scgd 		switch (*lbuf) {
13661f28255Scgd 		case 'b':		/* badtty */
137f96b4306Sdan 			if (sscanf(lbuf, "%39s%39s", f1, f2) != 2 ||
13861f28255Scgd 			    strcasecmp(f1, "badtty"))
13961f28255Scgd 				break;
14061f28255Scgd 			c_tty(f2);
14161f28255Scgd 			break;
14261f28255Scgd 		case 'g':		/* game */
143f96b4306Sdan 			if (sscanf(lbuf, "%39s%39s%39s%39s%39s",
14461f28255Scgd 			    f1, f2, f3, f4, f5) != 5 || strcasecmp(f1, "game"))
14561f28255Scgd 				break;
14661f28255Scgd 			c_game(f2, f3, f4, f5);
14761f28255Scgd 			break;
14861f28255Scgd 		case 't':		/* time */
149f96b4306Sdan 			if (sscanf(lbuf, "%39s%39s%39s%39s", f1, f2, f3, f4) != 4 ||
15061f28255Scgd 			    strcasecmp(f1, "time"))
15161f28255Scgd 				break;
15261f28255Scgd 			c_day(f2, f3, f4);
15361f28255Scgd 		}
15461f28255Scgd 	(void)fclose(cfp);
15561f28255Scgd }
15661f28255Scgd 
15761f28255Scgd /*
15861f28255Scgd  * c_day --
15961f28255Scgd  *	if day is today, see if okay to play
16061f28255Scgd  */
161e0ba63feSdholland static void
c_day(const char * s_day,const char * s_start,const char * s_stop)1624f50e1e4Sjmc c_day(const char *s_day, const char *s_start, const char *s_stop)
16361f28255Scgd {
16424d3aaa3Shubertf 	static const char *const days[] = {
16561f28255Scgd 		"sunday", "monday", "tuesday", "wednesday",
16661f28255Scgd 		"thursday", "friday", "saturday",
16761f28255Scgd 	};
16861f28255Scgd 	static struct tm *ct;
16961f28255Scgd 	int start, stop;
17061f28255Scgd 
17161f28255Scgd 	if (!ct)
17261f28255Scgd 		ct = localtime(&now);
17361f28255Scgd 	if (strcasecmp(s_day, days[ct->tm_wday]))
17461f28255Scgd 		return;
1754f50e1e4Sjmc 	if (!isdigit((unsigned char)*s_start) ||
1764f50e1e4Sjmc 	    !isdigit((unsigned char)*s_stop))
17761f28255Scgd 		return;
17861f28255Scgd 	start = atoi(s_start);
17961f28255Scgd 	stop = atoi(s_stop);
1802e57752aSjsm 	if (ct->tm_hour >= start && ct->tm_hour < stop) {
1812e57752aSjsm 		if (start == 0 && stop == 24)
1822e57752aSjsm 			errx(0, "Sorry, games are not available today.");
1832e57752aSjsm 		else
18482090a8bSlukem 			errx(0, "Sorry, games are not available from %s to %s today.",
18582090a8bSlukem 			     hour(start), hour(stop));
18661f28255Scgd 	}
1872e57752aSjsm }
18861f28255Scgd 
18961f28255Scgd /*
19061f28255Scgd  * c_tty --
19161f28255Scgd  *	decide if this tty can be used for games.
19261f28255Scgd  */
193e0ba63feSdholland static void
c_tty(const char * tty)1944f50e1e4Sjmc c_tty(const char *tty)
19561f28255Scgd {
19661f28255Scgd 	static int first = 1;
19761f28255Scgd 	static char *p_tty;
19861f28255Scgd 
19961f28255Scgd 	if (first) {
20082090a8bSlukem 		p_tty = strrchr(gametty, '/');
20161f28255Scgd 		first = 0;
20261f28255Scgd 	}
20361f28255Scgd 
20482090a8bSlukem 	if (!strcmp(gametty, tty) || (p_tty && !strcmp(p_tty, tty)))
20582090a8bSlukem 		errx(1, "Sorry, you may not play games on %s.", gametty);
20661f28255Scgd }
20761f28255Scgd 
20861f28255Scgd /*
20961f28255Scgd  * c_game --
21061f28255Scgd  *	see if game can be played now.
21161f28255Scgd  */
212e0ba63feSdholland static void
c_game(const char * s_game,const char * s_load,const char * s_users,const char * s_priority)2134f50e1e4Sjmc c_game(const char *s_game, const char *s_load, const char *s_users,
2144f50e1e4Sjmc        const char *s_priority)
21561f28255Scgd {
21661f28255Scgd 	static int found;
21761f28255Scgd 
21861f28255Scgd 	if (found)
21961f28255Scgd 		return;
22061f28255Scgd 	if (strcmp(game, s_game) && strcasecmp("default", s_game))
22161f28255Scgd 		return;
22261f28255Scgd 	++found;
22349f7d8a9Sdsl 	if (isdigit((unsigned char)*s_load) && atoi(s_load) < load())
22482090a8bSlukem 		errx(0, "Sorry, the load average is too high right now.");
22549f7d8a9Sdsl 	if (isdigit((unsigned char)*s_users) && atoi(s_users) <= users())
22682090a8bSlukem 		errx(0, "Sorry, there are too many users logged on right now.");
22749f7d8a9Sdsl 	if (isdigit((unsigned char)*s_priority))
22861f28255Scgd 		priority = atoi(s_priority);
22961f28255Scgd }
23061f28255Scgd 
23161f28255Scgd /*
23261f28255Scgd  * load --
23361f28255Scgd  *	return 15 minute load average
23461f28255Scgd  */
235e0ba63feSdholland static double
load(void)2364f50e1e4Sjmc load(void)
23761f28255Scgd {
23861f28255Scgd 	double avenrun[3];
23961f28255Scgd 
24082090a8bSlukem 	if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0)
24113f44a1fSjsm 		err(1, "getloadavg() failed");
24261f28255Scgd 	return (avenrun[2]);
24361f28255Scgd }
24461f28255Scgd 
24561f28255Scgd /*
24661f28255Scgd  * users --
24761f28255Scgd  *	return current number of users
24861f28255Scgd  *	todo: check idle time; if idle more than X minutes, don't
24961f28255Scgd  *	count them.
25061f28255Scgd  */
251e0ba63feSdholland static int
users(void)2524f50e1e4Sjmc users(void)
25361f28255Scgd {
254346b4189Schristos 	struct utmpentry *ep;
255346b4189Schristos 	int nusers;
25661f28255Scgd 
257346b4189Schristos 	nusers = getutentries(NULL, &ep);
258346b4189Schristos 	return nusers;
25961f28255Scgd }
26061f28255Scgd 
261e0ba63feSdholland static void
nogamefile(void)2624f50e1e4Sjmc nogamefile(void)
26361f28255Scgd {
26482090a8bSlukem 	int fd, n;
26561f28255Scgd 	char buf[BUFSIZ];
26661f28255Scgd 
26761f28255Scgd 	if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
26861f28255Scgd #define	MESG	"Sorry, no games right now.\n\n"
26961f28255Scgd 		(void)write(2, MESG, sizeof(MESG) - 1);
27061f28255Scgd 		while ((n = read(fd, buf, sizeof(buf))) > 0)
27161f28255Scgd 			(void)write(2, buf, n);
27261f28255Scgd 		exit(1);
27361f28255Scgd 	}
27461f28255Scgd }
27561f28255Scgd 
27661f28255Scgd /*
27761f28255Scgd  * hour --
27861f28255Scgd  *	print out the hour in human form
27961f28255Scgd  */
280e0ba63feSdholland static const char *
hour(int h)2814f50e1e4Sjmc hour(int h)
28261f28255Scgd {
28324d3aaa3Shubertf 	static const char *const hours[] = {
28482090a8bSlukem 	    "midnight", "1am", "2am", "3am", "4am", "5am",
28582090a8bSlukem 	    "6am", "7am", "8am", "9am", "10am", "11am",
28682090a8bSlukem 	    "noon", "1pm", "2pm", "3pm", "4pm", "5pm",
2872e57752aSjsm 	    "6pm", "7pm", "8pm", "9pm", "10pm", "11pm", "midnight" };
28882090a8bSlukem 
2892e57752aSjsm 	if (h < 0 || h > 24)
290a3c28055Smrg 		return ("BAD TIME");
29161f28255Scgd 	else
292a3c28055Smrg 		return (hours[h]);
29361f28255Scgd }
29461f28255Scgd 
29561f28255Scgd #ifdef LOG
29661f28255Scgd /*
29761f28255Scgd  * logfile --
29861f28255Scgd  *	log play of game
29961f28255Scgd  */
300e0ba63feSdholland static void
logfile(void)3014f50e1e4Sjmc logfile(void)
30261f28255Scgd {
303101657d1Scgd 	struct passwd *pw;
30461f28255Scgd 	FILE *lp;
30561f28255Scgd 	uid_t uid;
30661f28255Scgd 	int lock_cnt;
30761f28255Scgd 
30861f28255Scgd 	if (lp = fopen(_PATH_LOG, "a")) {
30961f28255Scgd 		for (lock_cnt = 0;; ++lock_cnt) {
31061f28255Scgd 			if (!flock(fileno(lp), LOCK_EX))
31161f28255Scgd 				break;
31261f28255Scgd 			if (lock_cnt == 4) {
31382090a8bSlukem 				warnx("log lock");
31461f28255Scgd 				(void)fclose(lp);
31561f28255Scgd 				return;
31661f28255Scgd 			}
317431304b3Sdholland 			sleep(1);
31861f28255Scgd 		}
31961f28255Scgd 		if (pw = getpwuid(uid = getuid()))
32061f28255Scgd 			fputs(pw->pw_name, lp);
32161f28255Scgd 		else
32261f28255Scgd 			fprintf(lp, "%u", uid);
32361f28255Scgd 		fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
32461f28255Scgd 		(void)flock(fileno(lp), LOCK_UN);
325b76cf716Sdholland 		(void)fclose(lp);
32661f28255Scgd 	}
32761f28255Scgd }
32861f28255Scgd #endif /* LOG */
329