dm.c revision 1.2 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
35 char 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
41 static 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 <string.h>
54 #include "pathnames.h"
55
56 extern int errno;
57 static time_t now; /* current time value */
58 static int priority = 0; /* priority game runs at */
59 static char *game, /* requested game */
60 *gametty; /* from tty? */
61
62 /*ARGSUSED*/
63 main(argc, argv)
64 int argc;
65 char **argv;
66 {
67 char *cp, *rindex(), *ttyname();
68 time_t time();
69
70 nogamefile();
71 game = (cp = rindex(*argv, '/')) ? ++cp : *argv;
72
73 if (!strcmp(game, "dm"))
74 exit(0);
75
76 gametty = ttyname(0);
77 (void)time(&now);
78 read_config();
79 #ifdef LOG
80 logfile();
81 #endif
82 play(argv);
83 /*NOTREACHED*/
84 }
85
86 /*
87 * play --
88 * play the game
89 */
90 play(args)
91 char **args;
92 {
93 char pbuf[MAXPATHLEN];
94
95 (void)strcpy(pbuf, _PATH_HIDE);
96 (void)strcpy(pbuf + sizeof(_PATH_HIDE) - 1, game);
97 if (priority > 0) /* < 0 requires root */
98 (void)setpriority(PRIO_PROCESS, 0, priority);
99 setgid(getgid()); /* we run setgid kmem; lose it */
100 execv(pbuf, args);
101 (void)fprintf(stderr, "dm: %s: %s\n", pbuf, strerror(errno));
102 exit(1);
103 }
104
105 /*
106 * read_config --
107 * read through config file, looking for key words.
108 */
109 read_config()
110 {
111 FILE *cfp;
112 char lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40];
113
114 if (!(cfp = fopen(_PATH_CONFIG, "r")))
115 return;
116 while (fgets(lbuf, sizeof(lbuf), cfp))
117 switch(*lbuf) {
118 case 'b': /* badtty */
119 if (sscanf(lbuf, "%s%s", f1, f2) != 2 ||
120 strcasecmp(f1, "badtty"))
121 break;
122 c_tty(f2);
123 break;
124 case 'g': /* game */
125 if (sscanf(lbuf, "%s%s%s%s%s",
126 f1, f2, f3, f4, f5) != 5 || strcasecmp(f1, "game"))
127 break;
128 c_game(f2, f3, f4, f5);
129 break;
130 case 't': /* time */
131 if (sscanf(lbuf, "%s%s%s%s", f1, f2, f3, f4) != 4 ||
132 strcasecmp(f1, "time"))
133 break;
134 c_day(f2, f3, f4);
135 }
136 (void)fclose(cfp);
137 }
138
139 /*
140 * c_day --
141 * if day is today, see if okay to play
142 */
143 c_day(s_day, s_start, s_stop)
144 char *s_day, *s_start, *s_stop;
145 {
146 static char *days[] = {
147 "sunday", "monday", "tuesday", "wednesday",
148 "thursday", "friday", "saturday",
149 };
150 static struct tm *ct;
151 int start, stop;
152
153 if (!ct)
154 ct = localtime(&now);
155 if (strcasecmp(s_day, days[ct->tm_wday]))
156 return;
157 if (!isdigit(*s_start) || !isdigit(*s_stop))
158 return;
159 start = atoi(s_start);
160 stop = atoi(s_stop);
161 if (ct->tm_hour >= start && ct->tm_hour < stop) {
162 fputs("dm: Sorry, games are not available from ", stderr);
163 hour(start);
164 fputs(" to ", stderr);
165 hour(stop);
166 fputs(" today.\n", stderr);
167 exit(0);
168 }
169 }
170
171 /*
172 * c_tty --
173 * decide if this tty can be used for games.
174 */
175 c_tty(tty)
176 char *tty;
177 {
178 static int first = 1;
179 static char *p_tty;
180 char *rindex();
181
182 if (first) {
183 p_tty = rindex(gametty, '/');
184 first = 0;
185 }
186
187 if (!strcmp(gametty, tty) || p_tty && !strcmp(p_tty, tty)) {
188 fprintf(stderr, "dm: Sorry, you may not play games on %s.\n", gametty);
189 exit(0);
190 }
191 }
192
193 /*
194 * c_game --
195 * see if game can be played now.
196 */
197 c_game(s_game, s_load, s_users, s_priority)
198 char *s_game, *s_load, *s_users, *s_priority;
199 {
200 static int found;
201 double load();
202
203 if (found)
204 return;
205 if (strcmp(game, s_game) && strcasecmp("default", s_game))
206 return;
207 ++found;
208 if (isdigit(*s_load) && atoi(s_load) < load()) {
209 fputs("dm: Sorry, the load average is too high right now.\n", stderr);
210 exit(0);
211 }
212 if (isdigit(*s_users) && atoi(s_users) <= users()) {
213 fputs("dm: Sorry, there are too many users logged on right now.\n", stderr);
214 exit(0);
215 }
216 if (isdigit(*s_priority))
217 priority = atoi(s_priority);
218 }
219
220 /*
221 * load --
222 * return 15 minute load average
223 */
224 double
225 load()
226 {
227 double avenrun[3];
228
229 if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0) {
230 fputs("dm: getloadavg() failed.\n", stderr);
231 exit(1);
232 }
233 return(avenrun[2]);
234 }
235
236 /*
237 * users --
238 * return current number of users
239 * todo: check idle time; if idle more than X minutes, don't
240 * count them.
241 */
242 users()
243 {
244
245 register int nusers, utmp;
246 struct utmp buf;
247
248 if ((utmp = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
249 (void)fprintf(stderr, "dm: %s: %s\n",
250 _PATH_UTMP, strerror(errno));
251 exit(1);
252 }
253 for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;)
254 if (buf.ut_name[0] != '\0')
255 ++nusers;
256 return(nusers);
257 }
258
259 nogamefile()
260 {
261 register int fd, n;
262 char buf[BUFSIZ];
263
264 if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
265 #define MESG "Sorry, no games right now.\n\n"
266 (void)write(2, MESG, sizeof(MESG) - 1);
267 while ((n = read(fd, buf, sizeof(buf))) > 0)
268 (void)write(2, buf, n);
269 exit(1);
270 }
271 }
272
273 /*
274 * hour --
275 * print out the hour in human form
276 */
277 hour(h)
278 int h;
279 {
280 switch(h) {
281 case 0:
282 fputs("midnight", stderr);
283 break;
284 case 12:
285 fputs("noon", stderr);
286 break;
287 default:
288 if (h > 12)
289 fprintf(stderr, "%dpm", h - 12);
290 else
291 fprintf(stderr, "%dam", h);
292 }
293 }
294
295 #ifdef LOG
296 /*
297 * logfile --
298 * log play of game
299 */
300 logfile()
301 {
302 struct passwd *pw, *getpwuid();
303 FILE *lp;
304 uid_t uid;
305 int lock_cnt;
306 char *ctime();
307
308 if (lp = fopen(_PATH_LOG, "a")) {
309 for (lock_cnt = 0;; ++lock_cnt) {
310 if (!flock(fileno(lp), LOCK_EX))
311 break;
312 if (lock_cnt == 4) {
313 perror("dm: log lock");
314 (void)fclose(lp);
315 return;
316 }
317 sleep((u_int)1);
318 }
319 if (pw = getpwuid(uid = getuid()))
320 fputs(pw->pw_name, lp);
321 else
322 fprintf(lp, "%u", uid);
323 fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
324 (void)fclose(lp);
325 (void)flock(fileno(lp), LOCK_UN);
326 }
327 }
328 #endif /* LOG */
329