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