Sync with Osd/OsdDebug.c rev1.9 in FreeBSD:
[dragonfly.git] / games / dm / dm.c
blobeb07dc9bd71dd1b9381238016b0fe70f91858174
1 /*
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
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 $
36 * $DragonFly: src/games/dm/dm.c,v 1.3 2003/11/12 14:53:52 eirikn Exp $
39 #include <sys/param.h>
40 #include <sys/file.h>
41 #include <sys/time.h>
42 #include <sys/resource.h>
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>
55 #include "pathnames.h"
57 static time_t now; /* current time value */
58 static int priority = 0; /* priority game runs at */
59 static char *game, /* requested game */
60 *gametty; /* from tty? */
62 void c_day (char *, char *, char *);
63 void c_tty (char *);
64 void c_game (char *, char *, char *, char *);
65 void hour (int);
66 double load (void);
67 void nogamefile (void);
68 void play (char **);
69 void read_config (void);
70 int users (void);
72 int
73 main(argc, argv)
74 int argc;
75 char *argv[];
77 char *cp;
79 nogamefile();
80 game = (cp = strrchr(*argv, '/')) ? ++cp : *argv;
82 if (!strcmp(game, "dm"))
83 exit(0);
85 gametty = ttyname(0);
86 unsetenv("TZ");
87 (void)time(&now);
88 read_config();
89 #ifdef LOG
90 logfile();
91 #endif
92 play(argv);
93 /*NOTREACHED*/
94 exit(EXIT_FAILURE);
98 * play --
99 * play the game
101 void
102 play(args)
103 char **args;
105 char pbuf[MAXPATHLEN];
107 if (sizeof(_PATH_HIDE) + strlen(game) > sizeof(pbuf)) {
108 (void)fprintf(stderr, "dm: %s/%s: %s\n", _PATH_HIDE, game,
109 strerror(ENAMETOOLONG));
110 exit(1);
112 (void)strcpy(pbuf, _PATH_HIDE);
113 (void)strcpy(pbuf + sizeof(_PATH_HIDE) - 1, game);
114 if (priority > 0) /* < 0 requires root */
115 (void)setpriority(PRIO_PROCESS, 0, priority);
116 execv(pbuf, args);
117 (void)fprintf(stderr, "dm: %s: %s\n", pbuf, strerror(errno));
118 exit(1);
122 * read_config --
123 * read through config file, looking for key words.
125 void
126 read_config()
128 FILE *cfp;
129 char lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40];
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);
153 (void)fclose(cfp);
157 * c_day --
158 * if day is today, see if okay to play
160 void
161 c_day(s_day, s_start, s_stop)
162 char *s_day, *s_start, *s_stop;
164 static const char *days[] = {
165 "sunday", "monday", "tuesday", "wednesday",
166 "thursday", "friday", "saturday",
168 static struct tm *ct;
169 int start, stop;
171 if (!ct)
172 ct = localtime(&now);
173 if (strcasecmp(s_day, days[ct->tm_wday]))
174 return;
175 if (!isdigit(*s_start) || !isdigit(*s_stop))
176 return;
177 start = atoi(s_start);
178 stop = atoi(s_stop);
179 if (ct->tm_hour >= start && ct->tm_hour < stop) {
180 (void)fputs("dm: Sorry, games are not available from ", stderr);
181 hour(start);
182 (void)fputs(" to ", stderr);
183 hour(stop);
184 (void)fputs(" today.\n", stderr);
185 exit(0);
190 * c_tty --
191 * decide if this tty can be used for games.
193 void
194 c_tty(tty)
195 char *tty;
197 static int first = 1;
198 static char *p_tty;
200 if (first) {
201 p_tty = strrchr(gametty, '/');
202 first = 0;
205 if (!strcmp(gametty, tty) || (p_tty && !strcmp(p_tty, tty))) {
206 (void)fprintf(stderr, "dm: Sorry, you may not play games on %s.\n", gametty);
207 exit(0);
212 * c_game --
213 * see if game can be played now.
215 void
216 c_game(s_game, s_load, s_users, s_priority)
217 char *s_game, *s_load, *s_users, *s_priority;
219 static int found;
221 if (found)
222 return;
223 if (strcmp(game, s_game) && strcasecmp("default", s_game))
224 return;
225 ++found;
226 if (isdigit(*s_load) && atoi(s_load) < load()) {
227 (void)fputs("dm: Sorry, the load average is too high right now.\n", stderr);
228 exit(0);
230 if (isdigit(*s_users) && atoi(s_users) <= users()) {
231 (void)fputs("dm: Sorry, there are too many users logged on right now.\n", stderr);
232 exit(0);
234 if (isdigit(*s_priority))
235 priority = atoi(s_priority);
239 * load --
240 * return 15 minute load average
242 double
243 load()
245 double avenrun[3];
247 if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0) {
248 (void)fputs("dm: getloadavg() failed.\n", stderr);
249 exit(1);
251 return(avenrun[2]);
255 * users --
256 * return current number of users
257 * todo: check idle time; if idle more than X minutes, don't
258 * count them.
261 users()
264 int nusers, utmp;
265 struct utmp buf;
267 if ((utmp = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
268 (void)fprintf(stderr, "dm: %s: %s\n",
269 _PATH_UTMP, strerror(errno));
270 exit(1);
272 for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;)
273 if (buf.ut_name[0] != '\0')
274 ++nusers;
275 return(nusers);
278 void
279 nogamefile()
281 int fd, n;
282 char buf[BUFSIZ];
284 if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
285 #define MESG "Sorry, no games right now.\n\n"
286 (void)write(2, MESG, sizeof(MESG) - 1);
287 while ((n = read(fd, buf, sizeof(buf))) > 0)
288 (void)write(2, buf, n);
289 exit(1);
294 * hour --
295 * print out the hour in human form
297 void
298 hour(h)
299 int h;
301 switch(h) {
302 case 0:
303 (void)fputs("midnight", stderr);
304 break;
305 case 12:
306 (void)fputs("noon", stderr);
307 break;
308 default:
309 if (h > 12)
310 (void)fprintf(stderr, "%dpm", h - 12);
311 else
312 (void)fprintf(stderr, "%dam", h);
316 #ifdef LOG
318 * logfile --
319 * log play of game
321 logfile()
323 struct passwd *pw;
324 FILE *lp;
325 uid_t uid;
326 int lock_cnt;
328 if (lp = fopen(_PATH_LOG, "a")) {
329 for (lock_cnt = 0;; ++lock_cnt) {
330 if (!flock(fileno(lp), LOCK_EX))
331 break;
332 if (lock_cnt == 4) {
333 perror("dm: log lock");
334 (void)fclose(lp);
335 return;
337 sleep((u_int)1);
339 if (pw = getpwuid(uid = getuid()))
340 fputs(pw->pw_name, lp);
341 else
342 fprintf(lp, "%u", uid);
343 fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
344 (void)fclose(lp);
345 (void)flock(fileno(lp), LOCK_UN);
348 #endif /* LOG */