NetBSD/games/dm/dm.c

329 lines
7.6 KiB
C
Raw Normal View History

2009-08-27 04:22:28 +04:00
/* $NetBSD: dm.c,v 1.29 2009/08/27 00:22:28 dholland Exp $ */
1995-03-21 18:03:38 +03:00
1993-03-21 12:45:37 +03:00
/*
1995-03-21 18:03:38 +03:00
* Copyright (c) 1987, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* 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. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* 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.
*/
1997-10-10 16:49:45 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1987, 1993\
The Regents of the University of California. All rights reserved.");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
1995-03-21 18:03:38 +03:00
#if 0
static char sccsid[] = "@(#)dm.c 8.1 (Berkeley) 5/31/93";
#else
2009-08-27 04:22:28 +04:00
__RCSID("$NetBSD: dm.c,v 1.29 2009/08/27 00:22:28 dholland Exp $");
1995-03-21 18:03:38 +03:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/resource.h>
1995-03-21 18:03:38 +03:00
1997-10-10 16:49:45 +04:00
#include <err.h>
1995-03-21 18:03:38 +03:00
#include <ctype.h>
1997-10-10 16:49:45 +04:00
#include <errno.h>
1995-03-21 18:03:38 +03:00
#include <pwd.h>
1993-03-21 12:45:37 +03:00
#include <stdio.h>
1997-10-10 16:49:45 +04:00
#include <stdlib.h>
1993-04-19 01:52:18 +04:00
#include <string.h>
1995-03-21 18:03:38 +03:00
#include <time.h>
#include <unistd.h>
2002-08-02 07:06:24 +04:00
#include "utmpentry.h"
1993-03-21 12:45:37 +03:00
#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? */
2009-08-12 09:48:04 +04:00
static void c_day(const char *, const char *, const char *);
static void c_game(const char *, const char *, const char *, const char *);
static void c_tty(const char *);
static const char *hour(int);
static double load(void);
static void nogamefile(void);
static void play(char **) __dead;
static void read_config(void);
static int users(void);
#ifdef LOG
static void logfile(void);
#endif
1997-10-10 16:49:45 +04:00
1995-03-21 18:03:38 +03:00
int
main(int argc __unused, char *argv[])
1993-03-21 12:45:37 +03:00
{
1995-03-21 18:03:38 +03:00
char *cp;
1993-03-21 12:45:37 +03:00
nogamefile();
1997-10-10 16:49:45 +04:00
game = (cp = strrchr(*argv, '/')) ? ++cp : *argv;
1993-03-21 12:45:37 +03:00
if (!strcmp(game, "dm"))
exit(0);
gametty = ttyname(0);
unsetenv("TZ");
1993-03-21 12:45:37 +03:00
(void)time(&now);
read_config();
#ifdef LOG
logfile();
#endif
play(argv);
/*NOTREACHED*/
1998-07-05 00:06:55 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/*
* play --
* play the game
*/
2009-08-12 09:48:04 +04:00
static void
2005-07-02 12:35:29 +04:00
play(char **args)
1993-03-21 12:45:37 +03:00
{
1993-04-19 01:52:18 +04:00
char pbuf[MAXPATHLEN];
1993-03-21 12:45:37 +03:00
snprintf(pbuf, sizeof(pbuf), "%s%s", _PATH_HIDE, game);
1993-03-21 12:45:37 +03:00
if (priority > 0) /* < 0 requires root */
(void)setpriority(PRIO_PROCESS, 0, priority);
execv(pbuf, args);
1997-10-10 16:49:45 +04:00
err(1, "%s", pbuf);
1993-03-21 12:45:37 +03:00
}
/*
* read_config --
* read through config file, looking for key words.
*/
2009-08-12 09:48:04 +04:00
static void
2005-07-02 12:35:29 +04:00
read_config(void)
1993-03-21 12:45:37 +03:00
{
FILE *cfp;
char lbuf[BUFSIZ], f1[40], f2[40], f3[40], f4[40], f5[40];
if (!(cfp = fopen(_PATH_CONFIG, "r")))
return;
while (fgets(lbuf, sizeof(lbuf), cfp))
1998-07-05 00:06:55 +04:00
switch (*lbuf) {
1993-03-21 12:45:37 +03:00
case 'b': /* badtty */
if (sscanf(lbuf, "%39s%39s", f1, f2) != 2 ||
1993-03-21 12:45:37 +03:00
strcasecmp(f1, "badtty"))
break;
c_tty(f2);
break;
case 'g': /* game */
if (sscanf(lbuf, "%39s%39s%39s%39s%39s",
1993-03-21 12:45:37 +03:00
f1, f2, f3, f4, f5) != 5 || strcasecmp(f1, "game"))
break;
c_game(f2, f3, f4, f5);
break;
case 't': /* time */
if (sscanf(lbuf, "%39s%39s%39s%39s", f1, f2, f3, f4) != 4 ||
1993-03-21 12:45:37 +03:00
strcasecmp(f1, "time"))
break;
c_day(f2, f3, f4);
}
(void)fclose(cfp);
}
/*
* c_day --
* if day is today, see if okay to play
*/
2009-08-12 09:48:04 +04:00
static void
2005-07-02 12:35:29 +04:00
c_day(const char *s_day, const char *s_start, const char *s_stop)
1993-03-21 12:45:37 +03:00
{
static const char *const days[] = {
1993-03-21 12:45:37 +03:00
"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;
2005-07-02 12:35:29 +04:00
if (!isdigit((unsigned char)*s_start) ||
!isdigit((unsigned char)*s_stop))
1993-03-21 12:45:37 +03:00
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));
}
1993-03-21 12:45:37 +03:00
}
/*
* c_tty --
* decide if this tty can be used for games.
*/
2009-08-12 09:48:04 +04:00
static void
2005-07-02 12:35:29 +04:00
c_tty(const char *tty)
1993-03-21 12:45:37 +03:00
{
static int first = 1;
static char *p_tty;
if (first) {
1997-10-10 16:49:45 +04:00
p_tty = strrchr(gametty, '/');
1993-03-21 12:45:37 +03:00
first = 0;
}
1997-10-10 16:49:45 +04:00
if (!strcmp(gametty, tty) || (p_tty && !strcmp(p_tty, tty)))
errx(1, "Sorry, you may not play games on %s.", gametty);
1993-03-21 12:45:37 +03:00
}
/*
* c_game --
* see if game can be played now.
*/
2009-08-12 09:48:04 +04:00
static void
2005-07-02 12:35:29 +04:00
c_game(const char *s_game, const char *s_load, const char *s_users,
const char *s_priority)
1993-03-21 12:45:37 +03:00
{
static int found;
if (found)
return;
if (strcmp(game, s_game) && strcasecmp("default", s_game))
return;
++found;
if (isdigit((unsigned char)*s_load) && atoi(s_load) < load())
1997-10-10 16:49:45 +04:00
errx(0, "Sorry, the load average is too high right now.");
if (isdigit((unsigned char)*s_users) && atoi(s_users) <= users())
1997-10-10 16:49:45 +04:00
errx(0, "Sorry, there are too many users logged on right now.");
if (isdigit((unsigned char)*s_priority))
1993-03-21 12:45:37 +03:00
priority = atoi(s_priority);
}
/*
* load --
* return 15 minute load average
*/
2009-08-12 09:48:04 +04:00
static double
2005-07-02 12:35:29 +04:00
load(void)
1993-03-21 12:45:37 +03:00
{
double avenrun[3];
1997-10-10 16:49:45 +04:00
if (getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])) < 0)
err(1, "getloadavg() failed");
1998-07-05 00:06:55 +04:00
return (avenrun[2]);
1993-03-21 12:45:37 +03:00
}
/*
* users --
* return current number of users
* todo: check idle time; if idle more than X minutes, don't
* count them.
*/
2009-08-12 09:48:04 +04:00
static int
2005-07-02 12:35:29 +04:00
users(void)
1993-03-21 12:45:37 +03:00
{
2002-08-02 07:06:24 +04:00
struct utmpentry *ep;
int nusers;
1993-03-21 12:45:37 +03:00
2002-08-02 07:06:24 +04:00
nusers = getutentries(NULL, &ep);
return nusers;
1993-03-21 12:45:37 +03:00
}
2009-08-12 09:48:04 +04:00
static void
2005-07-02 12:35:29 +04:00
nogamefile(void)
1993-03-21 12:45:37 +03:00
{
1997-10-10 16:49:45 +04:00
int fd, n;
1993-03-21 12:45:37 +03:00
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
*/
2009-08-12 09:48:04 +04:00
static const char *
2005-07-02 12:35:29 +04:00
hour(int h)
1993-03-21 12:45:37 +03:00
{
static const char *const hours[] = {
1997-10-10 16:49:45 +04:00
"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" };
1997-10-10 16:49:45 +04:00
if (h < 0 || h > 24)
1998-07-05 00:06:55 +04:00
return ("BAD TIME");
1997-10-10 16:49:45 +04:00
else
1998-07-05 00:06:55 +04:00
return (hours[h]);
1993-03-21 12:45:37 +03:00
}
#ifdef LOG
/*
* logfile --
* log play of game
*/
2009-08-12 09:48:04 +04:00
static void
2005-07-02 12:35:29 +04:00
logfile(void)
1993-03-21 12:45:37 +03:00
{
1995-03-21 18:03:38 +03:00
struct passwd *pw;
1993-03-21 12:45:37 +03:00
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) {
1997-10-10 16:49:45 +04:00
warnx("log lock");
1993-03-21 12:45:37 +03:00
(void)fclose(lp);
return;
}
2009-08-27 04:22:28 +04:00
sleep(1);
1993-03-21 12:45:37 +03:00
}
if (pw = getpwuid(uid = getuid()))
fputs(pw->pw_name, lp);
else
fprintf(lp, "%u", uid);
fprintf(lp, "\t%s\t%s\t%s", game, gametty, ctime(&now));
(void)flock(fileno(lp), LOCK_UN);
(void)fclose(lp);
1993-03-21 12:45:37 +03:00
}
}
#endif /* LOG */