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