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