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