1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
/* $OpenBSD: dm.c,v 1.13 1999/09/25 15:52:19 pjanzen Exp $ */
/* $NetBSD: dm.c,v 1.5 1996/02/06 22:47:20 jtc Exp $ */
/*
* Copyright (c) 1987, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1987, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)dm.c 8.1 (Berkeley) 5/31/93";
#else
static char rcsid[] = "$OpenBSD: dm.c,v 1.13 1999/09/25 15:52:19 pjanzen Exp $";
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <utmp.h>
#include "pathnames.h"
static time_t now; /* current time value */
static int priority = 0; /* priority game runs at */
static char *game, /* requested game */
*gametty; /* from tty? */
void c_day __P((const char *, const char *, const char *));
void c_game __P((const char *, const char *, const char *, const char *));
void c_tty __P((const char *));
const char *hour __P((int));
double load __P((void));
int main __P((int, char *[]));
void nogamefile __P((void));
void play __P((char **));
void read_config __P((void));
int users __P((void));
#ifdef LOG
void logfile __P((void));
#endif
int
main(argc, argv)
int argc;
char *argv[];
{
char *cp;
nogamefile();
game = (cp = strrchr(*argv, '/')) ? ++cp : *argv;
if (!strcmp(game, "dm"))
exit(0);
gametty = ttyname(0);
unsetenv("TZ");
(void)time(&now);
read_config();
#ifdef LOG
logfile();
#endif
play(argv);
/*NOTREACHED*/
}
/*
* play --
* play the game
*/
void
play(args)
char **args;
{
char pbuf[MAXPATHLEN];
if (sizeof(_PATH_HIDE) + strlen(game) > sizeof(pbuf)) {
(void)fprintf(stderr, "dm: %s/%s: %s\n", _PATH_HIDE, game,
strerror(ENAMETOOLONG));
exit(1);
}
(void)strcpy(pbuf, _PATH_HIDE);
(void)strcpy(pbuf + sizeof(_PATH_HIDE) - 1, game);
if (priority > 0) /* < 0 requires root */
(void)setpriority(PRIO_PROCESS, 0, priority);
execv(pbuf, args);
(void)fprintf(stderr, "dm: %s: %s\n", pbuf, strerror(errno));
/* use fprintf(stderr, "dm: ...) for error conditions in dm.
* use err() and family for denied games, since then you get
* the actual name of the game in the output message.
*/
exit(1);
}
/*
* read_config --
* read through config file, looking for key words.
*/
void
read_config()
{
FILE *cfp;
char lbuf[BUFSIZ], f1[41], f2[41], f3[41], f4[41], f5[41];
if (!(cfp = fopen(_PATH_CONFIG, "r")))
return;
while (fgets(lbuf, sizeof(lbuf), cfp))
switch (*lbuf) {
case 'b': /* badtty */
if (sscanf(lbuf, "%40s%40s", f1, f2) != 2 ||
strcasecmp(f1, "badtty"))
break;
c_tty(f2);
break;
case 'g': /* game */
if (sscanf(lbuf, "%40s%40s%40s%40s%40s",
f1, f2, f3, f4, f5) != 5 || strcasecmp(f1, "game"))
break;
c_game(f2, f3, f4, f5);
break;
case 't': /* time */
if (sscanf(lbuf, "%40s%40s%40s%40s", f1, f2, f3, f4) != 4 ||
strcasecmp(f1, "time"))
break;
c_day(f2, f3, f4);
}
(void)fclose(cfp);
}
/*
* c_day --
* if day is today, see if okay to play
*/
void
c_day(s_day, s_start, s_stop)
const char *s_day, *s_start, *s_stop;
{
static const char *const days[] = {
"sunday", "monday", "tuesday", "wednesday",
"thursday", "friday", "saturday",
};
static struct tm *ct;
int start, stop;
if (!ct)
ct = localtime(&now);
if (strcasecmp(s_day, days[ct->tm_wday]))
return;
if (!isdigit(*s_start) || !isdigit(*s_stop))
return;
start = atoi(s_start);
stop = atoi(s_stop);
if (ct->tm_hour >= start && ct->tm_hour < stop) {
if (start == 0 && stop == 24)
errx(0, "Sorry, games are not available today.");
else
errx(0, "Sorry, games are not available from %s to %s today.",
hour(start), hour(stop));
}
}
/*
* c_tty --
* decide if this tty can be used for games.
*/
void
c_tty(tty)
const char *tty;
{
static int first = 1;
static char *p_tty;
if (first) {
p_tty = strrchr(gametty, '/');
first = 0;
}
if (!strcmp(gametty, tty) || (p_tty && !strcmp(p_tty, tty)))
errx(0, "Sorry, you may not play games on %s.", gametty);
}
/*
* c_game --
* see if game can be played now.
*/
void
c_game(s_game, s_load, s_users, s_priority)
const char *s_game, *s_load, *s_users, *s_priority;
{
static int found;
if (found)
return;
if (strcmp(game, s_game) && strcasecmp("default", s_game))
return;
++found;
if (isdigit(*s_load) && atoi(s_load) < load())
errx(0, "Sorry, the load average is too high right now.");
if (isdigit(*s_users) && atoi(s_users) <= users())
errx(0, "Sorry, there are too many users logged on right now.");
if (isdigit(*s_priority))
priority = atoi(s_priority);
}
/*
* load --
* return 15 minute load average
*/
double
load()
{
double avenrun[3];
if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0) {
(void)fputs("dm: getloadavg() failed.\n", stderr);
exit(1);
}
return (avenrun[2]);
}
/*
* users --
* return current number of users
* todo: check idle time; if idle more than X minutes, don't
* count them.
*/
int
users()
{
int nusers, utmp;
struct utmp buf;
if ((utmp = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
(void)fprintf(stderr, "dm: %s: %s\n",
_PATH_UTMP, strerror(errno));
exit(1);
}
for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;)
if (buf.ut_name[0] != '\0')
++nusers;
return (nusers);
}
void
nogamefile()
{
int fd, n;
char buf[BUFSIZ];
if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
#define MESG "Sorry, no games right now.\n\n"
(void)write(2, MESG, sizeof(MESG) - 1);
while ((n = read(fd, buf, sizeof(buf))) > 0)
(void)write(2, buf, n);
exit(1);
}
}
/*
* hour --
* print out the hour in human form
*/
const char *
hour(h)
int h;
{
static const char *const hours[] = {
"midnight", "1am", "2am", "3am", "4am", "5am",
"6am", "7am", "8am", "9am", "10am", "11am",
"noon", "1pm", "2pm", "3pm", "4pm", "5pm",
"6pm", "7pm", "8pm", "9pm", "10pm", "11pm", "midnight" };
if (h < 0 || h > 24)
return ("BAD TIME");
else
return (hours[h]);
}
#ifdef LOG
/*
* logfile --
* log play of game
*/
void
logfile()
{
struct passwd *pw;
FILE *lp;
uid_t uid;
int lock_cnt;
if (lp = fopen(_PATH_LOG, "a")) {
for (lock_cnt = 0;; ++lock_cnt) {
if (!flock(fileno(lp), LOCK_EX))
break;
if (lock_cnt == 4) {
perror("dm: log lock");
(void)fclose(lp);
return;
}
sleep((u_int)1);
}
if (pw = getpwuid(uid = getuid()))
(void)fputs(pw->pw_name, lp);
else
(void)fprintf(lp, "%u", uid);
(void)fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
(void)flock(fileno(lp), LOCK_UN);
(void)fclose(lp);
}
}
#endif /* LOG */
|