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
This commit is contained in:
Joshua Kwan 2004-01-28 16:31:37 +00:00
parent 6e6be8f40c
commit aaaa35c525
3 changed files with 90 additions and 46 deletions

View File

@ -235,11 +235,12 @@ gen_ttyrec_filename ()
/* ************************************************************* */ /* ************************************************************* */
void char*
gen_inprogress_lock (pid_t pid, char* ttyrec_filename) gen_inprogress_lock (pid_t pid, char* ttyrec_filename)
{ {
char lockfile[130], pidbuf[16]; char *lockfile = NULL, pidbuf[16];
int fd; int fd;
size_t len;
struct flock fl = { 0 }; struct flock fl = { 0 };
snprintf (pidbuf, 16, "%d", pid); snprintf (pidbuf, 16, "%d", pid);
@ -249,7 +250,10 @@ gen_inprogress_lock (pid_t pid, char* ttyrec_filename)
fl.l_start = 0; fl.l_start = 0;
fl.l_len = 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); me->username, ttyrec_filename);
fd = open (lockfile, O_WRONLY | O_CREAT, 0644); fd = open (lockfile, O_WRONLY | O_CREAT, 0644);
@ -257,6 +261,8 @@ gen_inprogress_lock (pid_t pid, char* ttyrec_filename)
graceful_exit (68); graceful_exit (68);
write (fd, pidbuf, strlen (pidbuf)); 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 /* 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. */ * as may be some code... don't think so though. Globalize it. */
void int
purge_stale_locks (void) purge_stale_locks (void)
{ {
DIR *pdir; DIR *pdir;
struct dirent *dent; struct dirent *dent;
char* dir; char* dir;
size_t len; size_t len;
short firsttime = 1;
len = strlen(myconfig->dglroot) + ARRAY_SIZE("inprogress/") + 1; len = strlen(myconfig->dglroot) + ARRAY_SIZE("inprogress/") + 1;
dir = malloc(len); dir = malloc(len);
snprintf(dir, len, "%sinprogress/", myconfig->dglroot); snprintf(dir, len, "%sinprogress/", myconfig->dglroot);
if (!(pdir = opendir (dir))) if (!(pdir = opendir (dir)))
graceful_exit (200); graceful_exit (200);
@ -1314,7 +1320,7 @@ purge_stale_locks (void)
pid_t pid; pid_t pid;
size_t len; size_t len;
int seconds = 0; int seconds = 0;
if (!strcmp (dent->d_name, ".") || !strcmp (dent->d_name, "..")) if (!strcmp (dent->d_name, ".") || !strcmp (dent->d_name, ".."))
continue; continue;
@ -1339,10 +1345,36 @@ purge_stale_locks (void)
fclose (ipfile); fclose (ipfile);
clear (); if (firsttime)
drawbanner (1, 1); {
mvaddstr (3, 1, clear ();
"There is a stale Nethack process, attempting to recover..."); 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 (); refresh ();
pid = atoi (buf); pid = atoi (buf);
@ -1383,6 +1415,44 @@ purge_stale_locks (void)
} }
closedir (pdir); 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 int
@ -1392,8 +1462,6 @@ main (int argc, char** argv)
char atrcfilename[81], *spool; char atrcfilename[81], *spool;
unsigned int len; unsigned int len;
int userchoice = 0;
if (argc == 2) if (argc == 2)
config = strdup(argv[1]); config = strdup(argv[1]);
@ -1451,41 +1519,12 @@ main (int argc, char** argv)
graceful_exit (110); graceful_exit (110);
initcurses (); initcurses ();
while ((userchoice != 'p') | (!loggedin)) menuloop();
{
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;
}
}
assert (loggedin); assert (loggedin);
purge_stale_locks (); while (!purge_stale_locks())
menuloop();
endwin (); endwin ();
signal(SIGWINCH, SIG_DFL); signal(SIGWINCH, SIG_DFL);

View File

@ -61,7 +61,7 @@ extern struct dg_config *myconfig;
extern void create_config (void); extern void create_config (void);
extern void ttyrec_getmaster (void); extern void ttyrec_getmaster (void);
extern char* gen_ttyrec_filename (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 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

@ -81,6 +81,7 @@ FILE *fscript;
int master; int master;
int slave; int slave;
pid_t child, subchild; pid_t child, subchild;
char* ipfile = NULL;
struct termios tt; struct termios tt;
struct winsize win; struct winsize win;
@ -130,7 +131,7 @@ ttyrec_main (char *username, char* ttyrec_filename)
if (child) if (child)
{ {
close (slave); close (slave);
gen_inprogress_lock (child, ttyrec_filename); ipfile = gen_inprogress_lock (child, ttyrec_filename);
dooutput (); dooutput ();
} }
else else
@ -138,6 +139,8 @@ ttyrec_main (char *username, char* ttyrec_filename)
} }
doinput (); doinput ();
unlink (ipfile);
return 0; return 0;
} }
@ -336,6 +339,8 @@ done ()
{ {
(void) tcsetattr (0, TCSAFLUSH, &tt); (void) tcsetattr (0, TCSAFLUSH, &tt);
} }
unlink(ipfile);
graceful_exit (0); graceful_exit (0);
} }