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