Home | History | Annotate | Line # | Download | only in dm
dm.c revision 1.1.1.2
      1 /*
      2  * Copyright (c) 1987, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      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.
     20  *
     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.
     32  */
     33 
     34 #ifndef lint
     35 static char copyright[] =
     36 "@(#) Copyright (c) 1987, 1993\n\
     37 	The Regents of the University of California.  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 static char sccsid[] = "@(#)dm.c	8.1 (Berkeley) 5/31/93";
     42 #endif /* not lint */
     43 
     44 #include <sys/param.h>
     45 #include <sys/file.h>
     46 #include <sys/time.h>
     47 #include <sys/resource.h>
     48 
     49 #include <ctype.h>
     50 #include <nlist.h>
     51 #include <pwd.h>
     52 #include <stdio.h>
     53 #include <string.h>
     54 #include <time.h>
     55 #include <unistd.h>
     56 #include <utmp.h>
     57 
     58 #include "pathnames.h"
     59 
     60 extern int errno;
     61 static time_t	now;			/* current time value */
     62 static int	priority = 0;		/* priority game runs at */
     63 static char	*game,			/* requested game */
     64 		*gametty;		/* from tty? */
     65 
     66 int
     67 main(argc, argv)
     68 	int argc;
     69 	char *argv[];
     70 {
     71 	char *cp;
     72 
     73 	nogamefile();
     74 	game = (cp = rindex(*argv, '/')) ? ++cp : *argv;
     75 
     76 	if (!strcmp(game, "dm"))
     77 		exit(0);
     78 
     79 	gametty = ttyname(0);
     80 	(void)time(&now);
     81 	read_config();
     82 #ifdef LOG
     83 	logfile();
     84 #endif
     85 	play(argv);
     86 	/*NOTREACHED*/
     87 }
     88 
     89 /*
     90  * play --
     91  *	play the game
     92  */
     93 play(args)
     94 	char **args;
     95 {
     96 	char pbuf[MAXPATHLEN];
     97 
     98 	(void)strcpy(pbuf, _PATH_HIDE);
     99 	(void)strcpy(pbuf + sizeof(_PATH_HIDE) - 1, game);
    100 	if (priority > 0)	/* < 0 requires root */
    101 		(void)setpriority(PRIO_PROCESS, 0, priority);
    102 	setgid(getgid());	/* we run setgid kmem; lose it */
    103 	execv(pbuf, args);
    104 	(void)fprintf(stderr, "dm: %s: %s\n", pbuf, strerror(errno));
    105 	exit(1);
    106 }
    107 
    108 /*
    109  * read_config --
    110  *	read through config file, looking for key words.
    111  */
    112 read_config()
    113 {
    114 	FILE *cfp;
    115 	char lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40];
    116 
    117 	if (!(cfp = fopen(_PATH_CONFIG, "r")))
    118 		return;
    119 	while (fgets(lbuf, sizeof(lbuf), cfp))
    120 		switch(*lbuf) {
    121 		case 'b':		/* badtty */
    122 			if (sscanf(lbuf, "%s%s", f1, f2) != 2 ||
    123 			    strcasecmp(f1, "badtty"))
    124 				break;
    125 			c_tty(f2);
    126 			break;
    127 		case 'g':		/* game */
    128 			if (sscanf(lbuf, "%s%s%s%s%s",
    129 			    f1, f2, f3, f4, f5) != 5 || strcasecmp(f1, "game"))
    130 				break;
    131 			c_game(f2, f3, f4, f5);
    132 			break;
    133 		case 't':		/* time */
    134 			if (sscanf(lbuf, "%s%s%s%s", f1, f2, f3, f4) != 4 ||
    135 			    strcasecmp(f1, "time"))
    136 				break;
    137 			c_day(f2, f3, f4);
    138 		}
    139 	(void)fclose(cfp);
    140 }
    141 
    142 /*
    143  * c_day --
    144  *	if day is today, see if okay to play
    145  */
    146 c_day(s_day, s_start, s_stop)
    147 	char *s_day, *s_start, *s_stop;
    148 {
    149 	static char *days[] = {
    150 		"sunday", "monday", "tuesday", "wednesday",
    151 		"thursday", "friday", "saturday",
    152 	};
    153 	static struct tm *ct;
    154 	int start, stop;
    155 
    156 	if (!ct)
    157 		ct = localtime(&now);
    158 	if (strcasecmp(s_day, days[ct->tm_wday]))
    159 		return;
    160 	if (!isdigit(*s_start) || !isdigit(*s_stop))
    161 		return;
    162 	start = atoi(s_start);
    163 	stop = atoi(s_stop);
    164 	if (ct->tm_hour >= start && ct->tm_hour < stop) {
    165 		fputs("dm: Sorry, games are not available from ", stderr);
    166 		hour(start);
    167 		fputs(" to ", stderr);
    168 		hour(stop);
    169 		fputs(" today.\n", stderr);
    170 		exit(0);
    171 	}
    172 }
    173 
    174 /*
    175  * c_tty --
    176  *	decide if this tty can be used for games.
    177  */
    178 c_tty(tty)
    179 	char *tty;
    180 {
    181 	static int first = 1;
    182 	static char *p_tty;
    183 
    184 	if (first) {
    185 		p_tty = rindex(gametty, '/');
    186 		first = 0;
    187 	}
    188 
    189 	if (!strcmp(gametty, tty) || p_tty && !strcmp(p_tty, tty)) {
    190 		fprintf(stderr, "dm: Sorry, you may not play games on %s.\n", gametty);
    191 		exit(0);
    192 	}
    193 }
    194 
    195 /*
    196  * c_game --
    197  *	see if game can be played now.
    198  */
    199 c_game(s_game, s_load, s_users, s_priority)
    200 	char *s_game, *s_load, *s_users, *s_priority;
    201 {
    202 	static int found;
    203 	double load();
    204 
    205 	if (found)
    206 		return;
    207 	if (strcmp(game, s_game) && strcasecmp("default", s_game))
    208 		return;
    209 	++found;
    210 	if (isdigit(*s_load) && atoi(s_load) < load()) {
    211 		fputs("dm: Sorry, the load average is too high right now.\n", stderr);
    212 		exit(0);
    213 	}
    214 	if (isdigit(*s_users) && atoi(s_users) <= users()) {
    215 		fputs("dm: Sorry, there are too many users logged on right now.\n", stderr);
    216 		exit(0);
    217 	}
    218 	if (isdigit(*s_priority))
    219 		priority = atoi(s_priority);
    220 }
    221 
    222 /*
    223  * load --
    224  *	return 15 minute load average
    225  */
    226 double
    227 load()
    228 {
    229 	double avenrun[3];
    230 
    231 	if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0) {
    232 		fputs("dm: getloadavg() failed.\n", stderr);
    233 		exit(1);
    234 	}
    235 	return(avenrun[2]);
    236 }
    237 
    238 /*
    239  * users --
    240  *	return current number of users
    241  *	todo: check idle time; if idle more than X minutes, don't
    242  *	count them.
    243  */
    244 users()
    245 {
    246 
    247 	register int nusers, utmp;
    248 	struct utmp buf;
    249 
    250 	if ((utmp = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
    251 		(void)fprintf(stderr, "dm: %s: %s\n",
    252 		    _PATH_UTMP, strerror(errno));
    253 		exit(1);
    254 	}
    255 	for (nusers = 0; read(utmp, (char *)&buf, sizeof(struct utmp)) > 0;)
    256 		if (buf.ut_name[0] != '\0')
    257 			++nusers;
    258 	return(nusers);
    259 }
    260 
    261 nogamefile()
    262 {
    263 	register int fd, n;
    264 	char buf[BUFSIZ];
    265 
    266 	if ((fd = open(_PATH_NOGAMES, O_RDONLY, 0)) >= 0) {
    267 #define	MESG	"Sorry, no games right now.\n\n"
    268 		(void)write(2, MESG, sizeof(MESG) - 1);
    269 		while ((n = read(fd, buf, sizeof(buf))) > 0)
    270 			(void)write(2, buf, n);
    271 		exit(1);
    272 	}
    273 }
    274 
    275 /*
    276  * hour --
    277  *	print out the hour in human form
    278  */
    279 hour(h)
    280 	int h;
    281 {
    282 	switch(h) {
    283 	case 0:
    284 		fputs("midnight", stderr);
    285 		break;
    286 	case 12:
    287 		fputs("noon", stderr);
    288 		break;
    289 	default:
    290 		if (h > 12)
    291 			fprintf(stderr, "%dpm", h - 12);
    292 		else
    293 			fprintf(stderr, "%dam", h);
    294 	}
    295 }
    296 
    297 #ifdef LOG
    298 /*
    299  * logfile --
    300  *	log play of game
    301  */
    302 logfile()
    303 {
    304 	struct passwd *pw;
    305 	FILE *lp;
    306 	uid_t uid;
    307 	int lock_cnt;
    308 
    309 	if (lp = fopen(_PATH_LOG, "a")) {
    310 		for (lock_cnt = 0;; ++lock_cnt) {
    311 			if (!flock(fileno(lp), LOCK_EX))
    312 				break;
    313 			if (lock_cnt == 4) {
    314 				perror("dm: log lock");
    315 				(void)fclose(lp);
    316 				return;
    317 			}
    318 			sleep((u_int)1);
    319 		}
    320 		if (pw = getpwuid(uid = getuid()))
    321 			fputs(pw->pw_name, lp);
    322 		else
    323 			fprintf(lp, "%u", uid);
    324 		fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
    325 		(void)fclose(lp);
    326 		(void)flock(fileno(lp), LOCK_UN);
    327 	}
    328 }
    329 #endif /* LOG */
    330