From aaaa35c5256f8689374a775d2ebab47ac82bf669 Mon Sep 17 00:00:00 2001 From: Joshua Kwan Date: Wed, 28 Jan 2004 16:31:37 +0000 Subject: [PATCH] Fix some stuff related to HUP'ping processes (not completely fixed yet) git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@181 db0b04b0-f4d1-0310-9a6d-de3e77497b0e --- dgamelaunch.c | 127 +++++++++++++++++++++++++++++++++----------------- dgamelaunch.h | 2 +- ttyrec.c | 7 ++- 3 files changed, 90 insertions(+), 46 deletions(-) diff --git a/dgamelaunch.c b/dgamelaunch.c index bda3034..3a898bb 100644 --- a/dgamelaunch.c +++ b/dgamelaunch.c @@ -235,11 +235,12 @@ gen_ttyrec_filename () /* ************************************************************* */ -void +char* gen_inprogress_lock (pid_t pid, char* ttyrec_filename) { - char lockfile[130], pidbuf[16]; + char *lockfile = NULL, pidbuf[16]; int fd; + size_t len; struct flock fl = { 0 }; snprintf (pidbuf, 16, "%d", pid); @@ -249,7 +250,10 @@ gen_inprogress_lock (pid_t pid, char* ttyrec_filename) fl.l_start = 0; fl.l_len = 0; - snprintf (lockfile, 130, "%sinprogress/%s:%s", myconfig->dglroot, + len = strlen(myconfig->dglroot) + strlen(me->username) + strlen(ttyrec_filename) + 13; + lockfile = calloc(len, sizeof(char)); + + snprintf (lockfile, len, "%sinprogress/%s:%s", myconfig->dglroot, me->username, ttyrec_filename); fd = open (lockfile, O_WRONLY | O_CREAT, 0644); @@ -257,6 +261,8 @@ gen_inprogress_lock (pid_t pid, char* ttyrec_filename) graceful_exit (68); write (fd, pidbuf, strlen (pidbuf)); + + return lockfile; } /* ************************************************************* */ @@ -1288,19 +1294,19 @@ graceful_exit (int status) /* TODO: Some of the messages here (sorry no nethack for you!) are nethack specific * as may be some code... don't think so though. Globalize it. */ -void +int purge_stale_locks (void) { DIR *pdir; struct dirent *dent; char* dir; size_t len; + short firsttime = 1; len = strlen(myconfig->dglroot) + ARRAY_SIZE("inprogress/") + 1; dir = malloc(len); snprintf(dir, len, "%sinprogress/", myconfig->dglroot); - if (!(pdir = opendir (dir))) graceful_exit (200); @@ -1314,7 +1320,7 @@ purge_stale_locks (void) pid_t pid; size_t len; int seconds = 0; - + if (!strcmp (dent->d_name, ".") || !strcmp (dent->d_name, "..")) continue; @@ -1339,10 +1345,36 @@ purge_stale_locks (void) fclose (ipfile); - clear (); - drawbanner (1, 1); - mvaddstr (3, 1, - "There is a stale Nethack process, attempting to recover..."); + if (firsttime) + { + clear (); + drawbanner (1, 1); + + mvaddstr (3, 1, + "There are some stale Nethack processes, will recover in 5 seconds."); + mvaddstr (4, 1, + "Press a key NOW if you don't want this to happen!"); + + move (3, 58); /* pedantry */ + halfdelay(10); + + for (seconds = 5; seconds > 0; seconds--) + { + if (getch() != ERR) + { + nocbreak(); /* leave half-delay */ + cbreak(); + return 0; + } + mvaddch (3, 57, (char)(seconds + '0') - 1); + } + + nocbreak(); + cbreak(); + + firsttime = 0; + } + refresh (); pid = atoi (buf); @@ -1383,6 +1415,44 @@ purge_stale_locks (void) } closedir (pdir); + return 1; +} + +void +menuloop (void) +{ + int userchoice = 0; + while ((userchoice != 'p') | (!loggedin)) + { + drawmenu (); + userchoice = getch (); + switch (tolower (userchoice)) + { + case 'c': + if (loggedin) + changepw (); + break; + case 'w': + inprogressmenu (); + break; + case 'o': + if (loggedin) + editoptions (); + break; + case 'q': + endwin (); + graceful_exit(0); + /* break; */ + case 'r': + if (!loggedin) /*not visible to loggedin */ + newuser (); + break; + case 'l': + if (!loggedin) /* not visible to loggedin */ + loginprompt (); + break; + } + } } int @@ -1392,8 +1462,6 @@ main (int argc, char** argv) char atrcfilename[81], *spool; unsigned int len; - int userchoice = 0; - if (argc == 2) config = strdup(argv[1]); @@ -1451,41 +1519,12 @@ main (int argc, char** argv) graceful_exit (110); initcurses (); - while ((userchoice != 'p') | (!loggedin)) - { - drawmenu (); - userchoice = getch (); - switch (tolower (userchoice)) - { - case 'c': - if (loggedin) - changepw (); - break; - case 'w': - inprogressmenu (); - break; - case 'o': - if (loggedin) - editoptions (); - break; - case 'q': - endwin (); - return 0; - /* break; */ - case 'r': - if (!loggedin) /*not visible to loggedin */ - newuser (); - break; - case 'l': - if (!loggedin) /* not visible to loggedin */ - loginprompt (); - break; - } - } + menuloop(); assert (loggedin); - purge_stale_locks (); + while (!purge_stale_locks()) + menuloop(); endwin (); signal(SIGWINCH, SIG_DFL); diff --git a/dgamelaunch.h b/dgamelaunch.h index 48bf816..0c696d1 100644 --- a/dgamelaunch.h +++ b/dgamelaunch.h @@ -61,7 +61,7 @@ extern struct dg_config *myconfig; extern void create_config (void); extern void ttyrec_getmaster (void); extern char* gen_ttyrec_filename (void); -extern void gen_inprogress_lock (pid_t pid, char* ttyrec_filename); +extern char* gen_inprogress_lock (pid_t pid, char* ttyrec_filename); extern void catch_sighup (int signum); extern void loadbanner (struct dg_banner *ban); extern void drawbanner (unsigned int start_line, unsigned int howmany); diff --git a/ttyrec.c b/ttyrec.c index 007e512..f2d9f3a 100644 --- a/ttyrec.c +++ b/ttyrec.c @@ -81,6 +81,7 @@ FILE *fscript; int master; int slave; pid_t child, subchild; +char* ipfile = NULL; struct termios tt; struct winsize win; @@ -130,7 +131,7 @@ ttyrec_main (char *username, char* ttyrec_filename) if (child) { close (slave); - gen_inprogress_lock (child, ttyrec_filename); + ipfile = gen_inprogress_lock (child, ttyrec_filename); dooutput (); } else @@ -138,6 +139,8 @@ ttyrec_main (char *username, char* ttyrec_filename) } doinput (); + unlink (ipfile); + return 0; } @@ -336,6 +339,8 @@ done () { (void) tcsetattr (0, TCSAFLUSH, &tt); } + + unlink(ipfile); graceful_exit (0); }