home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char *RCSid = "$Header: gr.c,v 1.16 86/04/26 17:30:53 mcooper Exp $";
- #endif
-
- /*
- *------------------------------------------------------------------
- *
- * $Source: /usr/src/local/gr/RCS/gr.c,v $
- * $Revision: 1.16 $
- * $Date: 86/04/26 17:30:53 $
- * $State: Exp $
- * $Author: mcooper $
- * $Locker: mcooper $
- *
- *------------------------------------------------------------------
- *
- * Michael Cooper (mcooper@usc-oberon.arpa)
- * University Computing Services,
- * University of Southern California,
- * Los Angeles, California, 90089-0251
- * (213) 743-3469
- *
- *------------------------------------------------------------------
- * $Log: gr.c,v $
- * Revision 1.16 86/04/26 17:30:53 mcooper
- * This version adds a new field to the gr.control
- * file that specifies the priority that the game will
- * run at.
- *
- * Revision 1.15 86/03/25 18:50:08 mcooper
- * Added #ifdef LOGFILE.
- *
- * Revision 1.14 86/03/25 15:48:49 mcooper
- * New headers.
- *
- *------------------------------------------------------------------
- */
-
- #include <stdio.h>
- #include <signal.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/time.h>
- #include <sys/resource.h>
- #include <utmp.h>
- #include <nlist.h>
- #include "gr.h"
-
-
- static int ufd = 0;
- static int max_users;
- static int priority = 0;
- static lfd = 0;
- static double avenrun[3];
- static double max_load;
- static struct utmp buf;
- static struct nlist nl[] = {
- { "_avenrun" },
- { "" },
- };
-
- int was_tod;
- int was_load;
- int was_users;
- char *rindex();
- long time();
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- char *game = rindex(argv[0], '/');
-
- if (game == 0)
- game = argv[0];
- else
- game++; /* Skip the '/' */
- setup(game);
- if(checkfor(NOGAMING))
- exit(2);
- if (tod() || users() || load()) {
- fprintf(stderr, "Sorry, no games now.\n");
- if (was_load)
- fprintf(stderr,
- "The system load is greater than %.2lf.\n",
- max_load);
- if (was_users)
- fprintf(stderr,
- "There are more than %d users logged in.\n",
- max_users);
- if (was_tod)
- fprintf(stderr,
- "Game playing is not permitted between %d00 and %d00 hours on weekdays.\n",
- MORNING, EVENING);
- exit(1);
- }
- #ifdef LOGFILE
- logfile(game);
- #endif
- play(game, argv);
- }
-
- play(game, args)
- char *game;
- char **args;
- {
- int pid;
- char tmp[128];
-
- switch (pid = fork()) {
- case -1:
- fprintf(stderr, "Cannot fork.\n");
- exit(1);
- case 0:
- strcpy(tmp, HIDEDIR);
- strcat(tmp, "/");
- strcat(tmp, game);
- signal(SIGINT, SIG_DFL);
- signal(SIGQUIT, SIG_DFL);
- signal(SIGTSTP, SIG_DFL);
- setuid(getuid());
- if(setpriority(PRIO_PROCESS, 0, priority) < 0) {
- perror("setpriority");
- exit(1);
- }
- execv(tmp, args);
- perror(tmp);
- exit(1);
- }
- for (;;) {
- sleep(60);
- if (load() || users() || tod()) {
- warn(0);
- sleep(60);
- if (reprieve())
- continue;
- sleep(60);
- if (reprieve())
- continue;
- warn(1);
- sleep(60);
- if (reprieve())
- continue;
- warn(2);
- sleep(60);
- if (reprieve())
- continue;
- blast(pid);
- }
- }
- }
-
- reprieve()
- {
- static char mess[] = "\rYou have a reprieve. Continue playing.\r\n";
-
- if (load() || users() || tod())
- return (0);
- write(2, mess, sizeof(mess));
- return (1);
- }
-
- warn(which)
- int which;
- {
- static char buff[512];
- static char *mesg = "to save your game. If you do not leave\r\n\
- the game within this period, your game will be terminated.\r\n";
- static char *t[3] = {
- "4 minutes ",
- "2 minutes ",
- "1 minute "
- };
-
- if (was_load)
- sprintf(buff, "\rThe system load is greater than %.2lf.\r\n",
- max_load);
- if (was_users)
- sprintf(buff, "\rThere are more than %d users logged in.\r\n",
- max_users);
- if (was_tod)
- sprintf(buff, "\rGame time is over.\r\n");
- strcat(buff, "You have ");
- strcat(buff, t[which]);
- strcat(buff, mesg);
- write(2, buff, strlen(buff));
- }
-
- blast(pid)
- int pid;
- {
- static char mess[] = "\rYour game is forfeit.\r\n";
-
- write(2, mess, sizeof(mess));
- kill(pid, SIGHUP);
- sleep(60);
- kill(pid, SIGKILL);
- }
-
- death(n)
- int n;
- {
- union wait status;
-
- if (wait3(&status, WNOHANG | WUNTRACED, (char *)0) == 0) {
- return;
- }
- if (status.w_stopval == WSTOPPED) {
- signal(SIGTSTP, SIG_DFL);
- kill(getpid(), SIGTSTP);
- signal(SIGTSTP, SIG_IGN);
- return;
- } else
- exit(0);
- }
-
-
- setup(game)
- char *game;
- {
- char tmp[16];
- char lbuf[BUFSIZ];
- int n;
- FILE *list;
-
- if ((ufd = open("/etc/utmp", 0)) < 0) {
- perror("/etc/utmp");
- exit(1);
- }
- if ((lfd = open("/dev/kmem", 0)) < 0) {
- perror("/dev/kmem");
- exit(1);
- }
- nlist("/vmunix", nl);
- if (nl[0].n_type == 0) {
- perror("/vmunix");
- exit(1);
- }
- if ((list = fopen(CONTROL, "r")) == NULL) {
- perror(CONTROL);
- exit(1);
- }
- while (fgets(lbuf, sizeof(lbuf), list)) {
- if(lbuf[0] == COMMENT)
- continue;
- sscanf(lbuf, "%s%lf%d%d",
- tmp, &max_load, &max_users, &priority);
- if (strcmp(tmp, game) == 0)
- break;
- }
- fclose(list);
- signal(SIGCHLD, death);
- signal(SIGINT, SIG_IGN);
- signal(SIGQUIT, SIG_IGN);
- signal(SIGTSTP, SIG_IGN);
- }
-
- load()
- {
- lseek(lfd, (long)nl[0].n_value, 0);
- read(lfd, avenrun, sizeof(avenrun));
- return (was_load = (avenrun[1] >= max_load));
- }
-
- users()
- {
- char tmp[32];
- int count = 0;
- long l = time((long *)0);
- struct stat sbuf;
-
- lseek(ufd, (long)0, 0);
- while (read(ufd, &buf, sizeof(buf)) > 0) {
- if (buf.ut_name[0] != '\0') {
- count++;
- }
- }
- return (was_users = (count > max_users));
- }
-
- tod()
- {
- #ifdef TOD
- long now;
- struct tm *localtime();
- struct tm *ntime;
-
- time(&now);
- ntime = localtime(&now);
- if(ntime->tm_wday == 0 || ntime->tm_wday == 6)
- return(was_tod = FALSE);/* OK on Sat & Sun */
- if(ntime->tm_hour < MORNING || ntime->tm_hour >= EVENING)
- return(was_tod = FALSE);/* OK during non working hours */
-
- return(was_tod = TRUE);
- #endif
- }
-
- checkfor(file)
- char *file;
- {
- char buf[200];
- FILE *fd, *fopen();
-
- if((fd = fopen(file, "r")) != NULL) {
- fprintf(stderr, "Sorry, no games now...\n");
- while(fgets(buf, sizeof(buf), fd))
- fprintf(stderr, buf);
- fclose(fd);
- return(TRUE);
- }
- return(FALSE);
- }
-