Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / dm / dm.c
1 /*
2  * Copyright (c) 1987, 1993
3  *      The Regents of the University of California.  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 static const char copyright[] =
36 "@(#) Copyright (c) 1987, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)dm.c        8.1 (Berkeley) 5/31/93";
43 #endif
44 static const char rcsid[] =
45  "$FreeBSD: src/games/dm/dm.c,v 1.8 1999/12/10 02:54:18 billf Exp $";
46 #endif /* not lint */
47
48 #include <sys/param.h>
49 #include <sys/file.h>
50 #include <sys/time.h>
51 #include <sys/resource.h>
52
53 #include <ctype.h>
54 #include <errno.h>
55 #include <nlist.h>
56 #include <pwd.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <time.h>
61 #include <unistd.h>
62 #include <utmp.h>
63
64 #include "pathnames.h"
65
66 static time_t   now;                    /* current time value */
67 static int      priority = 0;           /* priority game runs at */
68 static char     *game,                  /* requested game */
69                 *gametty;               /* from tty? */
70
71 void c_day __P((char *, char *, char *));
72 void c_tty __P((char *));
73 void c_game __P((char *, char *, char *, char *));
74 void hour __P((int));
75 double load __P((void));
76 void nogamefile __P((void));
77 void play __P((char **));
78 void read_config __P((void));
79 int users __P((void));
80
81 int
82 main(argc, argv)
83         int argc;
84         char *argv[];
85 {
86         char *cp;
87
88         nogamefile();
89         game = (cp = strrchr(*argv, '/')) ? ++cp : *argv;
90
91         if (!strcmp(game, "dm"))
92                 exit(0);
93
94         gametty = ttyname(0);
95         unsetenv("TZ");
96         (void)time(&now);
97         read_config();
98 #ifdef LOG
99         logfile();
100 #endif
101         play(argv);
102         /*NOTREACHED*/
103         exit(EXIT_FAILURE);
104 }
105
106 /*
107  * play --
108  *      play the game
109  */
110 void
111 play(args)
112         char **args;
113 {
114         char pbuf[MAXPATHLEN];
115
116         if (sizeof(_PATH_HIDE) + strlen(game) > sizeof(pbuf)) {
117                 (void)fprintf(stderr, "dm: %s/%s: %s\n", _PATH_HIDE, game,
118                         strerror(ENAMETOOLONG));
119                 exit(1);
120         }
121         (void)strcpy(pbuf, _PATH_HIDE);
122         (void)strcpy(pbuf + sizeof(_PATH_HIDE) - 1, game);
123         if (priority > 0)       /* < 0 requires root */
124                 (void)setpriority(PRIO_PROCESS, 0, priority);
125         execv(pbuf, args);
126         (void)fprintf(stderr, "dm: %s: %s\n", pbuf, strerror(errno));
127         exit(1);
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         char *s_day, *s_start, *s_stop;
172 {
173         static const char *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                 (void)fputs("dm: Sorry, games are not available from ", stderr);
190                 hour(start);
191                 (void)fputs(" to ", stderr);
192                 hour(stop);
193                 (void)fputs(" today.\n", stderr);
194                 exit(0);
195         }
196 }
197
198 /*
199  * c_tty --
200  *      decide if this tty can be used for games.
201  */
202 void
203 c_tty(tty)
204         char *tty;
205 {
206         static int first = 1;
207         static char *p_tty;
208
209         if (first) {
210                 p_tty = strrchr(gametty, '/');
211                 first = 0;
212         }
213
214         if (!strcmp(gametty, tty) || (p_tty && !strcmp(p_tty, tty))) {
215                 (void)fprintf(stderr, "dm: Sorry, you may not play games on %s.\n", gametty);
216                 exit(0);
217         }
218 }
219
220 /*
221  * c_game --
222  *      see if game can be played now.
223  */
224 void
225 c_game(s_game, s_load, s_users, s_priority)
226         char *s_game, *s_load, *s_users, *s_priority;
227 {
228         static int found;
229
230         if (found)
231                 return;
232         if (strcmp(game, s_game) && strcasecmp("default", s_game))
233                 return;
234         ++found;
235         if (isdigit(*s_load) && atoi(s_load) < load()) {
236                 (void)fputs("dm: Sorry, the load average is too high right now.\n", stderr);
237                 exit(0);
238         }
239         if (isdigit(*s_users) && atoi(s_users) <= users()) {
240                 (void)fputs("dm: Sorry, there are too many users logged on right now.\n", stderr);
241                 exit(0);
242         }
243         if (isdigit(*s_priority))
244                 priority = atoi(s_priority);
245 }
246
247 /*
248  * load --
249  *      return 15 minute load average
250  */
251 double
252 load()
253 {
254         double avenrun[3];
255
256         if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0) {
257                 (void)fputs("dm: getloadavg() failed.\n", stderr);
258                 exit(1);
259         }
260         return(avenrun[2]);
261 }
262
263 /*
264  * users --
265  *      return current number of users
266  *      todo: check idle time; if idle more than X minutes, don't
267  *      count them.
268  */
269 int
270 users()
271 {
272
273         int nusers, utmp;
274         struct utmp buf;
275
276         if ((utmp = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
277                 (void)fprintf(stderr, "dm: %s: %s\n",
278                     _PATH_UTMP, strerror(errno));
279                 exit(1);
280         }
281         for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;)
282                 if (buf.ut_name[0] != '\0')
283                         ++nusers;
284         return(nusers);
285 }
286
287 void
288 nogamefile()
289 {
290         int fd, n;
291         char buf[BUFSIZ];
292
293         if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
294 #define MESG    "Sorry, no games right now.\n\n"
295                 (void)write(2, MESG, sizeof(MESG) - 1);
296                 while ((n = read(fd, buf, sizeof(buf))) > 0)
297                         (void)write(2, buf, n);
298                 exit(1);
299         }
300 }
301
302 /*
303  * hour --
304  *      print out the hour in human form
305  */
306 void
307 hour(h)
308         int h;
309 {
310         switch(h) {
311         case 0:
312                 (void)fputs("midnight", stderr);
313                 break;
314         case 12:
315                 (void)fputs("noon", stderr);
316                 break;
317         default:
318                 if (h > 12)
319                         (void)fprintf(stderr, "%dpm", h - 12);
320                 else
321                         (void)fprintf(stderr, "%dam", h);
322         }
323 }
324
325 #ifdef LOG
326 /*
327  * logfile --
328  *      log play of game
329  */
330 logfile()
331 {
332         struct passwd *pw;
333         FILE *lp;
334         uid_t uid;
335         int lock_cnt;
336
337         if (lp = fopen(_PATH_LOG, "a")) {
338                 for (lock_cnt = 0;; ++lock_cnt) {
339                         if (!flock(fileno(lp), LOCK_EX))
340                                 break;
341                         if (lock_cnt == 4) {
342                                 perror("dm: log lock");
343                                 (void)fclose(lp);
344                                 return;
345                         }
346                         sleep((u_int)1);
347                 }
348                 if (pw = getpwuid(uid = getuid()))
349                         fputs(pw->pw_name, lp);
350                 else
351                         fprintf(lp, "%u", uid);
352                 fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
353                 (void)fclose(lp);
354                 (void)flock(fileno(lp), LOCK_UN);
355         }
356 }
357 #endif /* LOG */