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