* Copyright (c) 1987 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
"@(#) Copyright (c) 1987 Regents of the University of California.\n\
static char sccsid
[] = "@(#)dm.c 5.9 (Berkeley) %G%";
#include <sys/resource.h>
static time_t now
; /* current time value */
static int priority
= 0; /* priority game runs at */
static char *game
, /* requested game */
*gametty
; /* from tty? */
char *cp
, *rindex(), *ttyname();
game
= (cp
= rindex(*argv
, '/')) ? ++cp
: *argv
;
#define GAMEHIDE "/usr/games/hide/"
char pbuf
[MAXPATHLEN
], *strcpy();
(void)strcpy(pbuf
, GAMEHIDE
);
(void)strcpy(pbuf
+ sizeof(GAMEHIDE
) - 1, game
);
if (priority
> 0) /* < 0 requires root */
(void)setpriority(PRIO_PROCESS
, 0, priority
);
setgid(getgid()); /* we run setgid kmem; lose it */
* read through config file, looking for key words.
#define CONTROL "/usr/games/dm.config"
char *control
, *host
, *index(), *strcpy();
char lbuf
[BUFSIZ
], path
[MAXHOSTNAMELEN
+ sizeof(CONTROL
)];
char f1
[40], f2
[40], f3
[40], f4
[40], f5
[40];
host
= &path
[sizeof(CONTROL
)];
if (gethostname(host
, MAXHOSTNAMELEN
)) {
perror("dm: gethostname");
(void)strcpy(path
, control
= CONTROL
);
if (host
= index(host
, '.'))
if (!(cfp
= fopen(path
, "r")) && !(cfp
= fopen(control
, "r"))) {
fprintf(stderr
, "dm: unable to read %s or %s.\n",
while (fgets(lbuf
, sizeof(lbuf
), cfp
))
if (sscanf(lbuf
, "%s%s", f1
, f2
) != 2 ||
strcasecmp(f1
, "badtty"))
if (sscanf(lbuf
, "%s%s%s%s%s",
f1
, f2
, f3
, f4
, f5
) != 5 || strcasecmp(f1
, "game"))
if (sscanf(lbuf
, "%s%s%s%s", f1
, f2
, f3
, f4
) != 4 ||
* if day is today, see if okay to play
c_day(s_day
, s_start
, s_stop
)
char *s_day
, *s_start
, *s_stop
;
"sunday", "monday", "tuesday", "wednesday",
"thursday", "friday", "saturday",
if (strcasecmp(s_day
, days
[ct
->tm_wday
]))
if (!isdigit(*s_start
) || !isdigit(*s_stop
))
if (ct
->tm_hour
>= start
&& ct
->tm_hour
< stop
) {
fputs("dm: Sorry, games are not available from ", stderr
);
fputs(" today.\n", stderr
);
* decide if this tty can be used for games.
p_tty
= rindex(gametty
, '/');
if (!strcmp(gametty
, tty
) || p_tty
&& !strcmp(p_tty
, tty
)) {
fprintf(stderr
, "dm: Sorry, you may not play games on %s.\n", gametty
);
* see if game can be played now.
c_game(s_game
, s_load
, s_users
, s_priority
)
char *s_game
, *s_load
, *s_users
, *s_priority
;
if (strcmp(game
, s_game
) && strcasecmp("default", s_game
))
if (isdigit(*s_load
) && atoi(s_load
) < load()) {
fputs("dm: Sorry, the load average is too high right now.\n", stderr
);
if (isdigit(*s_users
) && atoi(s_users
) <= users()) {
fputs("dm: Sorry, there are too many users logged on right now.\n", stderr
);
if (isdigit(*s_priority
))
priority
= atoi(s_priority
);
static struct nlist nl
[] = {
* return 15 minute load average
if (nlist("/vmunix", nl
)) {
fputs("dm: nlist of /vmunix failed.\n", stderr
);
if ((kmem
= open("/dev/kmem", O_RDONLY
, 0)) < 0) {
(void)lseek(kmem
, (long)nl
[X_AVENRUN
].n_value
, L_SET
);
(void)read(kmem
, (char *)avenrun
, sizeof(avenrun
));
* return current number of users
* todo: check idle time; if idle more than X minutes, don't
register int nusers
, utmp
;
if ((utmp
= open("/etc/utmp", O_RDONLY
, 0)) < 0) {
for (nusers
= 0; read(utmp
, (char *)&buf
, sizeof(struct utmp
)) > 0;)
if (buf
.ut_name
[0] != '\0')
* if the file NOGAMING exists, no games allowed.
* file may also contain a message for the user.
#define NOGAMING "/usr/games/nogames"
if ((fd
= open(NOGAMING
, 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)
* print out the hour in human form
fputs("midnight", stderr
);
fprintf(stderr
, "%dpm", h
- 12);
fprintf(stderr
, "%dam", h
);
#define LOGFILE "/usr/adm/dm.log"
struct passwd
*pw
, *getpwuid();
if (lp
= fopen(LOGFILE
, "a")) {
for (lock_cnt
= 0;; ++lock_cnt
) {
if (!flock(fileno(lp
), LOCK_EX
))
if (pw
= getpwuid(uid
= getuid()))
fprintf(lp
, "\t%s\t%s\t%s", game
, gametty
, ctime(&now
));
(void)flock(fileno(lp
), LOCK_UN
);