xref: /netbsd-src/games/dm/dm.c (revision eb7c1594f145c931049e1fd9eb056a5987e87e59)
1 /*	$NetBSD: dm.c,v 1.18 2003/08/07 09:37:11 agc Exp $	*/
2 
3 /*
4  * Copyright (c) 1987, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)dm.c	8.1 (Berkeley) 5/31/93";
41 #else
42 __RCSID("$NetBSD: dm.c,v 1.18 2003/08/07 09:37:11 agc Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 #include <sys/file.h>
48 #include <sys/time.h>
49 #include <sys/resource.h>
50 
51 #include <err.h>
52 #include <ctype.h>
53 #include <errno.h>
54 #include <pwd.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <time.h>
59 #include <unistd.h>
60 
61 #include "utmpentry.h"
62 #include "pathnames.h"
63 
64 static time_t	now;			/* current time value */
65 static int	priority = 0;		/* priority game runs at */
66 static char	*game,			/* requested game */
67 		*gametty;		/* from tty? */
68 
69 void	c_day __P((const char *, const char *, const char *));
70 void	c_game __P((const char *, const char  *, const char *, const char *));
71 void	c_tty __P((const char *));
72 const char *hour __P((int));
73 double	load __P((void));
74 int	main __P((int, char *[]));
75 void	nogamefile __P((void));
76 void	play __P((char **)) __attribute__((__noreturn__));
77 void	read_config __P((void));
78 int	users __P((void));
79 
80 int
81 main(argc, argv)
82 	int argc;
83 	char *argv[];
84 {
85 	char *cp;
86 
87 	nogamefile();
88 	game = (cp = strrchr(*argv, '/')) ? ++cp : *argv;
89 
90 	if (!strcmp(game, "dm"))
91 		exit(0);
92 
93 	gametty = ttyname(0);
94 	unsetenv("TZ");
95 	(void)time(&now);
96 	read_config();
97 #ifdef LOG
98 	logfile();
99 #endif
100 	play(argv);
101 	/*NOTREACHED*/
102 	return (0);
103 }
104 
105 /*
106  * play --
107  *	play the game
108  */
109 void
110 play(args)
111 	char **args;
112 {
113 	char pbuf[MAXPATHLEN];
114 
115 	snprintf(pbuf, sizeof(pbuf), "%s%s", _PATH_HIDE, game);
116 	if (priority > 0)	/* < 0 requires root */
117 		(void)setpriority(PRIO_PROCESS, 0, priority);
118 	execv(pbuf, args);
119 	err(1, "%s", pbuf);
120 }
121 
122 /*
123  * read_config --
124  *	read through config file, looking for key words.
125  */
126 void
127 read_config()
128 {
129 	FILE *cfp;
130 	char lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40];
131 
132 	if (!(cfp = fopen(_PATH_CONFIG, "r")))
133 		return;
134 	while (fgets(lbuf, sizeof(lbuf), cfp))
135 		switch (*lbuf) {
136 		case 'b':		/* badtty */
137 			if (sscanf(lbuf, "%s%s", f1, f2) != 2 ||
138 			    strcasecmp(f1, "badtty"))
139 				break;
140 			c_tty(f2);
141 			break;
142 		case 'g':		/* game */
143 			if (sscanf(lbuf, "%s%s%s%s%s",
144 			    f1, f2, f3, f4, f5) != 5 || strcasecmp(f1, "game"))
145 				break;
146 			c_game(f2, f3, f4, f5);
147 			break;
148 		case 't':		/* time */
149 			if (sscanf(lbuf, "%s%s%s%s", f1, f2, f3, f4) != 4 ||
150 			    strcasecmp(f1, "time"))
151 				break;
152 			c_day(f2, f3, f4);
153 		}
154 	(void)fclose(cfp);
155 }
156 
157 /*
158  * c_day --
159  *	if day is today, see if okay to play
160  */
161 void
162 c_day(s_day, s_start, s_stop)
163 	const char *s_day, *s_start, *s_stop;
164 {
165 	static const char *const days[] = {
166 		"sunday", "monday", "tuesday", "wednesday",
167 		"thursday", "friday", "saturday",
168 	};
169 	static struct tm *ct;
170 	int start, stop;
171 
172 	if (!ct)
173 		ct = localtime(&now);
174 	if (strcasecmp(s_day, days[ct->tm_wday]))
175 		return;
176 	if (!isdigit(*s_start) || !isdigit(*s_stop))
177 		return;
178 	start = atoi(s_start);
179 	stop = atoi(s_stop);
180 	if (ct->tm_hour >= start && ct->tm_hour < stop) {
181 		if (start == 0 && stop == 24)
182 			errx(0, "Sorry, games are not available today.");
183 		else
184 			errx(0, "Sorry, games are not available from %s to %s today.",
185 			     hour(start), hour(stop));
186 	}
187 }
188 
189 /*
190  * c_tty --
191  *	decide if this tty can be used for games.
192  */
193 void
194 c_tty(tty)
195 	const char *tty;
196 {
197 	static int first = 1;
198 	static char *p_tty;
199 
200 	if (first) {
201 		p_tty = strrchr(gametty, '/');
202 		first = 0;
203 	}
204 
205 	if (!strcmp(gametty, tty) || (p_tty && !strcmp(p_tty, tty)))
206 		errx(1, "Sorry, you may not play games on %s.", gametty);
207 }
208 
209 /*
210  * c_game --
211  *	see if game can be played now.
212  */
213 void
214 c_game(s_game, s_load, s_users, s_priority)
215 	const char *s_game, *s_load, *s_users, *s_priority;
216 {
217 	static int found;
218 
219 	if (found)
220 		return;
221 	if (strcmp(game, s_game) && strcasecmp("default", s_game))
222 		return;
223 	++found;
224 	if (isdigit(*s_load) && atoi(s_load) < load())
225 		errx(0, "Sorry, the load average is too high right now.");
226 	if (isdigit(*s_users) && atoi(s_users) <= users())
227 		errx(0, "Sorry, there are too many users logged on right now.");
228 	if (isdigit(*s_priority))
229 		priority = atoi(s_priority);
230 }
231 
232 /*
233  * load --
234  *	return 15 minute load average
235  */
236 double
237 load()
238 {
239 	double avenrun[3];
240 
241 	if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0)
242 		err(1, "getloadavg() failed");
243 	return (avenrun[2]);
244 }
245 
246 /*
247  * users --
248  *	return current number of users
249  *	todo: check idle time; if idle more than X minutes, don't
250  *	count them.
251  */
252 int
253 users()
254 {
255 	static struct utmpentry *ohead = NULL;
256 	struct utmpentry *ep;
257 	int nusers;
258 
259 	nusers = getutentries(NULL, &ep);
260 	if (ep != ohead) {
261 		freeutentries(ep);
262 		ohead = ep;
263 	}
264 	return nusers;
265 }
266 
267 void
268 nogamefile()
269 {
270 	int fd, n;
271 	char buf[BUFSIZ];
272 
273 	if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
274 #define	MESG	"Sorry, no games right now.\n\n"
275 		(void)write(2, MESG, sizeof(MESG) - 1);
276 		while ((n = read(fd, buf, sizeof(buf))) > 0)
277 			(void)write(2, buf, n);
278 		exit(1);
279 	}
280 }
281 
282 /*
283  * hour --
284  *	print out the hour in human form
285  */
286 const char *
287 hour(h)
288 	int h;
289 {
290 	static const char *const hours[] = {
291 	    "midnight", "1am", "2am", "3am", "4am", "5am",
292 	    "6am", "7am", "8am", "9am", "10am", "11am",
293 	    "noon", "1pm", "2pm", "3pm", "4pm", "5pm",
294 	    "6pm", "7pm", "8pm", "9pm", "10pm", "11pm", "midnight" };
295 
296 	if (h < 0 || h > 24)
297 		return ("BAD TIME");
298 	else
299 		return (hours[h]);
300 }
301 
302 #ifdef LOG
303 /*
304  * logfile --
305  *	log play of game
306  */
307 logfile()
308 {
309 	struct passwd *pw;
310 	FILE *lp;
311 	uid_t uid;
312 	int lock_cnt;
313 
314 	if (lp = fopen(_PATH_LOG, "a")) {
315 		for (lock_cnt = 0;; ++lock_cnt) {
316 			if (!flock(fileno(lp), LOCK_EX))
317 				break;
318 			if (lock_cnt == 4) {
319 				warnx("log lock");
320 				(void)fclose(lp);
321 				return;
322 			}
323 			sleep((u_int)1);
324 		}
325 		if (pw = getpwuid(uid = getuid()))
326 			fputs(pw->pw_name, lp);
327 		else
328 			fprintf(lp, "%u", uid);
329 		fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
330 		(void)fclose(lp);
331 		(void)flock(fileno(lp), LOCK_UN);
332 	}
333 }
334 #endif /* LOG */
335