Cleanups - globals, extraneous #defines, #include order, etc.

git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@161 db0b04b0-f4d1-0310-9a6d-de3e77497b0e
This commit is contained in:
Joshua Kwan 2004-01-25 22:34:35 +00:00
parent 5a152569a8
commit c4932ed44b
4 changed files with 43 additions and 67 deletions

View File

@ -1,7 +1,7 @@
/* dgamelaunch.c /* dgamelaunch.c
* *
* (c)2001-3 M. Drew Streib <dtype@dtype.org> * (c)2001-4 M. Drew Streib <dtype@dtype.org>
* also parts (c) 2003 Joshua Kwan <joshk@triplehelix.org>, * also parts (c) 2003-4 Joshua Kwan <joshk@triplehelix.org>,
* Brett Carrington <brettcar@segvio.org>, * Brett Carrington <brettcar@segvio.org>,
* Jilles Tjoelker <jilles@stack.nl> * Jilles Tjoelker <jilles@stack.nl>
* *
@ -26,12 +26,6 @@
* This is a little wrapper for nethack (and soon other programs) that * This is a little wrapper for nethack (and soon other programs) that
* will allow them to be run from a telnetd session, chroot, shed privs, * will allow them to be run from a telnetd session, chroot, shed privs,
* make a simple login, then play the game. * make a simple login, then play the game.
*
* By default, this thing is also statically compiled, and can thus be
* run inside of a chroot jail itself if necessary.
*
* Yes, I know it is all global variables. Deal with it. The program
* is very small.
*/ */
#define _GNU_SOURCE #define _GNU_SOURCE
@ -48,12 +42,14 @@
/* program stuff */ /* program stuff */
#include <stdlib.h>
#include <curses.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/file.h> /* for flock() */
#include <sys/time.h> #include <sys/time.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/ioctl.h> /* ttyrec */
#include <sys/stat.h>
#include <stdlib.h>
#include <curses.h>
#ifndef __FreeBSD__ #ifndef __FreeBSD__
# include <crypt.h> # include <crypt.h>
@ -73,12 +69,9 @@
#include <pwd.h> #include <pwd.h>
#include <grp.h> #include <grp.h>
#include <time.h> #include <time.h>
#include <sys/resource.h>
#include <sys/ioctl.h> /* ttyrec */
#include <errno.h> #include <errno.h>
#include <dirent.h> #include <dirent.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include <signal.h> #include <signal.h>
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
@ -89,11 +82,12 @@
extern FILE* yyin; extern FILE* yyin;
extern int yyparse (); extern int yyparse ();
extern pid_t child; /* nethack process */
extern int ee_main (int argc, char **argv); extern int ee_main (int argc, char **argv);
extern int ttyplay_main (char *ttyfile, int mode, int rstripgfx); extern int ttyplay_main (char *ttyfile, int mode, int rstripgfx);
extern int ttyrec_main (char *); extern int ttyrec_main (char *username, char* ttyrec_filename);
extern int master; extern int master, slave;
extern int slave;
extern struct termios tt; extern struct termios tt;
extern struct winsize win; extern struct winsize win;
@ -103,23 +97,21 @@ struct dg_config *myconfig = NULL;
char* config = NULL; char* config = NULL;
struct dg_config defconfig = { struct dg_config defconfig = {
"/var/lib/dgamelaunch/", .chroot = "/var/lib/dgamelaunch/",
"/bin/nethack", .nethack = "/bin/nethack",
"/dgldir/", .dglroot = "/dgldir/",
"/dgl-lock", .lockfile = "/dgl-lock",
"/dgl-login", .passwd = "/dgl-login",
"/dgl-banner", .banner = "/dgl-banner",
"/dgl-default-rcfile", .rcfile = "/dgl-default-rcfile",
"/var/mail/", .spool = "/var/mail/",
"games", "games", .shed_user = "games", .shed_group = "games",
5, 60, /* games:games in Debian */ .shed_uid = 5, .shed_gid = 60, /* games:games in Debian */
64000 .max = 64000
}; };
int pid_game = 0;
int loggedin = 0; int loggedin = 0;
char rcfilename[80]; char rcfilename[80];
char ttyrec_filename[100];
char *chosen_name; char *chosen_name;
int f_num = 0; int f_num = 0;
@ -225,11 +217,12 @@ ttyrec_getmaster ()
/* ************************************************************* */ /* ************************************************************* */
void char*
gen_ttyrec_filename () gen_ttyrec_filename ()
{ {
time_t rawtime; time_t rawtime;
struct tm *ptm; struct tm *ptm;
char *ttyrec_filename = calloc(100, sizeof(char));
/* append time to filename */ /* append time to filename */
time (&rawtime); time (&rawtime);
@ -237,12 +230,13 @@ gen_ttyrec_filename ()
snprintf (ttyrec_filename, 100, "%04i-%02i-%02i.%02i:%02i:%02i.ttyrec", snprintf (ttyrec_filename, 100, "%04i-%02i-%02i.%02i:%02i:%02i.ttyrec",
ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday,
ptm->tm_hour, ptm->tm_min, ptm->tm_sec); ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
return ttyrec_filename;
} }
/* ************************************************************* */ /* ************************************************************* */
void void
gen_inprogress_lock (pid_t pid) gen_inprogress_lock (pid_t pid, char* ttyrec_filename)
{ {
char lockfile[130], pidbuf[16]; char lockfile[130], pidbuf[16];
int fd; int fd;
@ -270,10 +264,10 @@ gen_inprogress_lock (pid_t pid)
void void
catch_sighup (int signum) catch_sighup (int signum)
{ {
if (pid_game) if (child)
{ {
sleep (10); sleep (10);
kill (pid_game, SIGHUP); kill (child, SIGHUP);
sleep (5); sleep (5);
} }
graceful_exit (2); graceful_exit (2);
@ -1520,11 +1514,8 @@ main (int argc, char** argv)
free (spool); free (spool);
/* lock */
gen_ttyrec_filename ();
/* launch program */ /* launch program */
ttyrec_main (me->username); ttyrec_main (me->username, gen_ttyrec_filename());
/* NOW we can safely kill this */ /* NOW we can safely kill this */
freefile (); freefile ();

View File

@ -56,13 +56,12 @@ struct dg_config
extern char* config; /* file path */ extern char* config; /* file path */
extern struct dg_config *myconfig; extern struct dg_config *myconfig;
extern struct dg_config defconfig;
/* dgamelaunch.c function prototypes */ /* dgamelaunch.c function prototypes */
extern void create_config (void); extern void create_config (void);
extern void ttyrec_getmaster (void); extern void ttyrec_getmaster (void);
extern void gen_ttyrec_filename (void); extern char* gen_ttyrec_filename (void);
extern void gen_inprogress_lock (pid_t pid); extern void gen_inprogress_lock (pid_t pid, char* ttyrec_filename);
extern void catch_sighup (int signum); extern void catch_sighup (int signum);
extern void loadbanner (struct dg_banner *ban); extern void loadbanner (struct dg_banner *ban);
extern void drawbanner (unsigned int start_line, unsigned int howmany); extern void drawbanner (unsigned int start_line, unsigned int howmany);

View File

@ -31,14 +31,15 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h> #include <string.h>
#include <curses.h> #include <curses.h>
#include "ttyrec.h" #include "ttyrec.h"

View File

@ -45,10 +45,13 @@
#include "dgamelaunch.h" #include "dgamelaunch.h"
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <termios.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <time.h>
#include <sys/file.h> #include <sys/file.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <termios.h>
#include <time.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -59,7 +62,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/time.h>
#include "ttyrec.h" #include "ttyrec.h"
#include "io.h" #include "io.h"
@ -67,19 +69,6 @@
# define XCASE 0 # define XCASE 0
#endif #endif
#define HAVE_inet_aton
#define HAVE_scsi_h
#define HAVE_kd_h
#define _(FOO) FOO
#ifdef HAVE_openpty
#include <libutil.h>
#endif
extern char ttyrec_filename[100];
extern int pid_game;
void done (void); void done (void);
void fail (void); void fail (void);
void fixtty (void); void fixtty (void);
@ -91,8 +80,7 @@ void doshell (char *);
FILE *fscript; FILE *fscript;
int master; int master;
int slave; int slave;
int child; pid_t child, subchild;
int subchild;
struct termios tt; struct termios tt;
struct winsize win; struct winsize win;
@ -102,7 +90,7 @@ int aflg;
int uflg; int uflg;
int int
ttyrec_main (char *username) ttyrec_main (char *username, char* ttyrec_filename)
{ {
void finish (); void finish ();
char dirname[100]; char dirname[100];
@ -142,8 +130,7 @@ ttyrec_main (char *username)
if (child) if (child)
{ {
close (slave); close (slave);
pid_game = child; gen_inprogress_lock (child, ttyrec_filename);
gen_inprogress_lock (pid_game);
dooutput (); dooutput ();
} }
else else
@ -166,8 +153,6 @@ doinput ()
done (); done ();
} }
#include <sys/wait.h>
void void
finish () finish ()
{ {