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