1 /* $NetBSD: dm.c,v 1.28 2009/08/12 05:51:59 dholland Exp $ */
4 * Copyright (c) 1987, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1987, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)dm.c 8.1 (Berkeley) 5/31/93";
42 __RCSID("$NetBSD: dm.c,v 1.28 2009/08/12 05:51:59 dholland Exp $");
46 #include <sys/param.h>
49 #include <sys/resource.h>
61 #include "utmpentry.h"
62 #include "pathnames.h"
64 static time_t now
; /* current time value */
65 static int priority
= 0; /* priority game runs at */
66 static char *game
, /* requested game */
67 *gametty
; /* from tty? */
69 static void c_day(const char *, const char *, const char *);
70 static void c_game(const char *, const char *, const char *, const char *);
71 static void c_tty(const char *);
72 static const char *hour(int);
73 static double load(void);
74 static void nogamefile(void);
75 static void play(char **) __dead
;
76 static void read_config(void);
77 static int users(void);
80 static void logfile(void);
84 main(int argc __unused
, char *argv
[])
89 game
= (cp
= strrchr(*argv
, '/')) ? ++cp
: *argv
;
91 if (!strcmp(game
, "dm"))
113 char pbuf
[MAXPATHLEN
];
115 snprintf(pbuf
, sizeof(pbuf
), "%s%s", _PATH_HIDE
, game
);
116 if (priority
> 0) /* < 0 requires root */
117 (void)setpriority(PRIO_PROCESS
, 0, priority
);
124 * read through config file, looking for key words.
130 char lbuf
[BUFSIZ
], f1
[40], f2
[40], f3
[40], f4
[40], f5
[40];
132 if (!(cfp
= fopen(_PATH_CONFIG
, "r")))
134 while (fgets(lbuf
, sizeof(lbuf
), cfp
))
136 case 'b': /* badtty */
137 if (sscanf(lbuf
, "%39s%39s", f1
, f2
) != 2 ||
138 strcasecmp(f1
, "badtty"))
143 if (sscanf(lbuf
, "%39s%39s%39s%39s%39s",
144 f1
, f2
, f3
, f4
, f5
) != 5 || strcasecmp(f1
, "game"))
146 c_game(f2
, f3
, f4
, f5
);
149 if (sscanf(lbuf
, "%39s%39s%39s%39s", f1
, f2
, f3
, f4
) != 4 ||
150 strcasecmp(f1
, "time"))
159 * if day is today, see if okay to play
162 c_day(const char *s_day
, const char *s_start
, const char *s_stop
)
164 static const char *const days
[] = {
165 "sunday", "monday", "tuesday", "wednesday",
166 "thursday", "friday", "saturday",
168 static struct tm
*ct
;
172 ct
= localtime(&now
);
173 if (strcasecmp(s_day
, days
[ct
->tm_wday
]))
175 if (!isdigit((unsigned char)*s_start
) ||
176 !isdigit((unsigned char)*s_stop
))
178 start
= atoi(s_start
);
180 if (ct
->tm_hour
>= start
&& ct
->tm_hour
< stop
) {
181 if (start
== 0 && stop
== 24)
182 errx(0, "Sorry, games are not available today.");
184 errx(0, "Sorry, games are not available from %s to %s today.",
185 hour(start
), hour(stop
));
191 * decide if this tty can be used for games.
194 c_tty(const char *tty
)
196 static int first
= 1;
200 p_tty
= strrchr(gametty
, '/');
204 if (!strcmp(gametty
, tty
) || (p_tty
&& !strcmp(p_tty
, tty
)))
205 errx(1, "Sorry, you may not play games on %s.", gametty
);
210 * see if game can be played now.
213 c_game(const char *s_game
, const char *s_load
, const char *s_users
,
214 const char *s_priority
)
220 if (strcmp(game
, s_game
) && strcasecmp("default", s_game
))
223 if (isdigit((unsigned char)*s_load
) && atoi(s_load
) < load())
224 errx(0, "Sorry, the load average is too high right now.");
225 if (isdigit((unsigned char)*s_users
) && atoi(s_users
) <= users())
226 errx(0, "Sorry, there are too many users logged on right now.");
227 if (isdigit((unsigned char)*s_priority
))
228 priority
= atoi(s_priority
);
233 * return 15 minute load average
240 if (getloadavg(avenrun
, sizeof(avenrun
)/sizeof(avenrun
[0])) < 0)
241 err(1, "getloadavg() failed");
247 * return current number of users
248 * todo: check idle time; if idle more than X minutes, don't
254 struct utmpentry
*ep
;
257 nusers
= getutentries(NULL
, &ep
);
267 if ((fd
= open(_PATH_NOGAMES
, O_RDONLY
, 0)) >= 0) {
268 #define MESG "Sorry, no games right now.\n\n"
269 (void)write(2, MESG
, sizeof(MESG
) - 1);
270 while ((n
= read(fd
, buf
, sizeof(buf
))) > 0)
271 (void)write(2, buf
, n
);
278 * print out the hour in human form
283 static const char *const hours
[] = {
284 "midnight", "1am", "2am", "3am", "4am", "5am",
285 "6am", "7am", "8am", "9am", "10am", "11am",
286 "noon", "1pm", "2pm", "3pm", "4pm", "5pm",
287 "6pm", "7pm", "8pm", "9pm", "10pm", "11pm", "midnight" };
308 if (lp
= fopen(_PATH_LOG
, "a")) {
309 for (lock_cnt
= 0;; ++lock_cnt
) {
310 if (!flock(fileno(lp
), LOCK_EX
))
319 if (pw
= getpwuid(uid
= getuid()))
320 fputs(pw
->pw_name
, lp
);
322 fprintf(lp
, "%u", uid
);
323 fprintf(lp
, "\t%s\t%s\t%s", game
, gametty
, ctime(&now
));
324 (void)flock(fileno(lp
), LOCK_UN
);