Reindent, and add some error handling for kill

git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@66 db0b04b0-f4d1-0310-9a6d-de3e77497b0e
This commit is contained in:
Joshua Kwan 2004-01-04 02:19:12 +00:00
parent 0daf7cdb3b
commit 71f367b1b4
4 changed files with 98 additions and 73 deletions

View File

@ -140,7 +140,7 @@ gen_inprogress_lock (pid_t pid)
int fd; int fd;
struct flock fl = { 0 }; struct flock fl = { 0 };
snprintf(pidbuf, 16, "%.15d", pid); snprintf (pidbuf, 16, "%.15d", pid);
fl.l_type = F_WRLCK; fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET; fl.l_whence = SEEK_SET;
@ -154,7 +154,7 @@ gen_inprogress_lock (pid_t pid)
if (fcntl (fd, F_SETLKW, &fl) == -1) if (fcntl (fd, F_SETLKW, &fl) == -1)
graceful_exit (68); graceful_exit (68);
write(fd, pidbuf, strlen(pidbuf)); write (fd, pidbuf, strlen (pidbuf));
} }
/* ************************************************************* */ /* ************************************************************* */
@ -285,7 +285,7 @@ populate_games (int *l)
while ((pdirent = readdir (pdir))) while ((pdirent = readdir (pdir)))
{ {
if (!strcmp (pdirent->d_name, ".") || !strcmp(pdirent->d_name, "..")) if (!strcmp (pdirent->d_name, ".") || !strcmp (pdirent->d_name, ".."))
continue; continue;
snprintf (fullname, 130, "%s%s", LOC_INPROGRESSDIR, pdirent->d_name); snprintf (fullname, 130, "%s%s", LOC_INPROGRESSDIR, pdirent->d_name);
@ -294,7 +294,7 @@ populate_games (int *l)
/* O_RDWR here should be O_RDONLY, but we need to test for /* O_RDWR here should be O_RDONLY, but we need to test for
* an exclusive lock */ * an exclusive lock */
fd = open (fullname, O_RDWR); fd = open (fullname, O_RDWR);
if ((fd > 0) && fcntl(fd, F_SETLK, &fl) == -1) if ((fd > 0) && fcntl (fd, F_SETLK, &fl) == -1)
{ {
/* stat to check idle status */ /* stat to check idle status */
@ -426,7 +426,7 @@ inprogressmenu ()
refresh (); refresh ();
endwin (); endwin ();
ttyplay_main (ttyrecname, 1, 0); ttyplay_main (ttyrecname, 1, 0);
initncurses(); initncurses ();
} }
} }
@ -563,8 +563,9 @@ domailuser (char *username)
{ {
if (errno != EAGAIN) if (errno != EAGAIN)
{ {
mvaddstr (10, 1, "Received a weird error from fcntl, so I'm giving up."); mvaddstr (10, 1,
getch(); "Received a weird error from fcntl, so I'm giving up.");
getch ();
return; return;
} }
sleep (1); sleep (1);
@ -869,7 +870,7 @@ readfile (int nolock)
fpl = fopen ("/dgl-lock", "r"); fpl = fopen ("/dgl-lock", "r");
if (!fpl) if (!fpl)
graceful_exit (106); graceful_exit (106);
if (fcntl (fileno(fpl), F_SETLKW, &fl) == -1) if (fcntl (fileno (fpl), F_SETLKW, &fl) == -1)
graceful_exit (114); graceful_exit (114);
} }
@ -1117,42 +1118,66 @@ graceful_exit (int status)
void void
purge_stale_locks (void) purge_stale_locks (void)
{ {
DIR* pdir; DIR *pdir;
struct dirent *dent; struct dirent *dent;
if (!(pdir = opendir(LOC_INPROGRESSDIR))) if (!(pdir = opendir (LOC_INPROGRESSDIR)))
graceful_exit(200); graceful_exit (200);
while ((dent = readdir(pdir)) != NULL) while ((dent = readdir (pdir)) != NULL)
{ {
FILE* ipfile; FILE *ipfile;
char* colon; char *colon;
char buf[16]; char buf[16];
pid_t pid; pid_t pid;
int seconds = 0;
colon = strchr(dent->d_name, ':'); colon = strchr (dent->d_name, ':');
/* should never happen */ /* should never happen */
if (!colon) if (!colon)
graceful_exit(201); graceful_exit (201);
if (strncmp(dent->d_name, me->username, colon - dent->d_name)) if (strncmp (dent->d_name, me->username, colon - dent->d_name))
continue; continue;
if (!(ipfile = fopen(dent->d_name, "r"))) if (!(ipfile = fopen (dent->d_name, "r")))
graceful_exit(202); graceful_exit (202);
if (fgets(buf, 16, ipfile) == NULL) if (fgets (buf, 16, ipfile) == NULL)
graceful_exit(203); graceful_exit (203);
fclose(ipfile); fclose (ipfile);
unlink(dent->d_name); unlink (dent->d_name);
pid = atoi(buf); pid = atoi (buf);
kill(pid, SIGHUP); kill (pid, SIGHUP);
errno = 0;
/* Wait for it to stop running */
while (kill (pid, 0) == 0)
{
seconds++;
sleep (1);
if (seconds == 10)
{
clear ();
drawbanner (1, 1);
mvaddstr (3, 1,
"Couldn't terminate one of your stale Nethack processes gracefully.");
mvaddstr (4, 1, "Force its termination? [yn] ");
if (tolower (getch ()) == 'y')
{
kill (pid, SIGTERM);
break;
}
}
}
seconds = 0;
} }
closedir(pdir); closedir (pdir);
} }
int int
@ -1258,7 +1283,7 @@ main (void)
endwin (); endwin ();
purge_stale_locks(); purge_stale_locks ();
/* environment */ /* environment */
snprintf (atrcfilename, 81, "@%s", rcfilename); snprintf (atrcfilename, 81, "@%s", rcfilename);

View File

@ -74,6 +74,6 @@ extern void writefile (int requirenew);
extern void graceful_exit (int status); extern void graceful_exit (int status);
/* strlcpy.c */ /* strlcpy.c */
extern size_t strlcpy(char *dst, const char *src, size_t siz); extern size_t strlcpy (char *dst, const char *src, size_t siz);
#endif #endif

View File

@ -145,7 +145,7 @@ ttyrec_main (char *username)
{ {
close (slave); close (slave);
pid_game = child; pid_game = child;
gen_inprogress_lock(pid_game); gen_inprogress_lock (pid_game);
dooutput (); dooutput ();
} }
else else