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