diff --git a/dgamelaunch.c b/dgamelaunch.c index 1b1aed2..3199717 100644 --- a/dgamelaunch.c +++ b/dgamelaunch.c @@ -140,8 +140,8 @@ gen_inprogress_lock (pid_t pid) int fd; struct flock fl = { 0 }; - snprintf(pidbuf, 16, "%.15d", pid); - + snprintf (pidbuf, 16, "%.15d", pid); + fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; fl.l_start = 0; @@ -154,7 +154,7 @@ gen_inprogress_lock (pid_t pid) if (fcntl (fd, F_SETLKW, &fl) == -1) graceful_exit (68); - write(fd, pidbuf, strlen(pidbuf)); + write (fd, pidbuf, strlen (pidbuf)); } /* ************************************************************* */ @@ -285,8 +285,8 @@ populate_games (int *l) while ((pdirent = readdir (pdir))) { - if (!strcmp (pdirent->d_name, ".") || !strcmp(pdirent->d_name, "..")) - continue; + if (!strcmp (pdirent->d_name, ".") || !strcmp (pdirent->d_name, "..")) + continue; 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 * an exclusive lock */ 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 */ @@ -426,7 +426,7 @@ inprogressmenu () refresh (); endwin (); ttyplay_main (ttyrecname, 1, 0); - initncurses(); + initncurses (); } } @@ -509,7 +509,7 @@ domailuser (char *username) time_t now; int mail_empty = 1; struct flock fl = { 0 }; - + fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; fl.l_start = 0; @@ -560,15 +560,16 @@ domailuser (char *username) refresh (); while (fcntl (fileno (user_spool), F_SETLK, &fl) == -1) - { - if (errno != EAGAIN) { - mvaddstr (10, 1, "Received a weird error from fcntl, so I'm giving up."); - getch(); - return; + if (errno != EAGAIN) + { + mvaddstr (10, 1, + "Received a weird error from fcntl, so I'm giving up."); + getch (); + return; + } + sleep (1); } - sleep (1); - } fprintf (user_spool, "%s:%s\n", me->username, message); @@ -869,7 +870,7 @@ readfile (int nolock) fpl = fopen ("/dgl-lock", "r"); if (!fpl) graceful_exit (106); - if (fcntl (fileno(fpl), F_SETLKW, &fl) == -1) + if (fcntl (fileno (fpl), F_SETLKW, &fl) == -1) graceful_exit (114); } @@ -1117,42 +1118,66 @@ graceful_exit (int status) void purge_stale_locks (void) { - DIR* pdir; + DIR *pdir; struct dirent *dent; - if (!(pdir = opendir(LOC_INPROGRESSDIR))) - graceful_exit(200); + if (!(pdir = opendir (LOC_INPROGRESSDIR))) + graceful_exit (200); - while ((dent = readdir(pdir)) != NULL) - { - FILE* ipfile; - char* colon; - char buf[16]; - pid_t pid; - - colon = strchr(dent->d_name, ':'); - /* should never happen */ - if (!colon) - graceful_exit(201); + while ((dent = readdir (pdir)) != NULL) + { + FILE *ipfile; + char *colon; + char buf[16]; + pid_t pid; + int seconds = 0; - if (strncmp(dent->d_name, me->username, colon - dent->d_name)) - continue; + colon = strchr (dent->d_name, ':'); + /* should never happen */ + if (!colon) + graceful_exit (201); - if (!(ipfile = fopen(dent->d_name, "r"))) - graceful_exit(202); + if (strncmp (dent->d_name, me->username, colon - dent->d_name)) + continue; - if (fgets(buf, 16, ipfile) == NULL) - graceful_exit(203); + if (!(ipfile = fopen (dent->d_name, "r"))) + graceful_exit (202); - fclose(ipfile); - unlink(dent->d_name); + if (fgets (buf, 16, ipfile) == NULL) + graceful_exit (203); - pid = atoi(buf); + fclose (ipfile); + unlink (dent->d_name); - kill(pid, SIGHUP); - } - - closedir(pdir); + pid = atoi (buf); + + 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); } int @@ -1258,8 +1283,8 @@ main (void) endwin (); - purge_stale_locks(); - + purge_stale_locks (); + /* environment */ snprintf (atrcfilename, 81, "@%s", rcfilename); diff --git a/dgamelaunch.h b/dgamelaunch.h index 5473a17..bb5a181 100644 --- a/dgamelaunch.h +++ b/dgamelaunch.h @@ -74,6 +74,6 @@ extern void writefile (int requirenew); extern void graceful_exit (int status); /* 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 diff --git a/strlcpy.c b/strlcpy.c index 51e8670..020e1aa 100644 --- a/strlcpy.c +++ b/strlcpy.c @@ -29,7 +29,7 @@ #if defined(LIBC_SCCS) && !defined(lint) static char *rcsid = - "$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $"; + "$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $"; #endif /* LIBC_SCCS and not lint */ #include @@ -42,33 +42,33 @@ static char *rcsid = */ size_t strlcpy (dst, src, siz) - char *dst; - const char *src; - size_t siz; + char *dst; + const char *src; + size_t siz; { - register char *d = dst; - register const char *s = src; - register size_t n = siz; + register char *d = dst; + register const char *s = src; + register size_t n = siz; - /* Copy as many bytes as will fit */ - if (n != 0 && --n != 0) - { - do - { - if ((*d++ = *s++) == 0) - break; - } - while (--n != 0); - } + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) + { + do + { + if ((*d++ = *s++) == 0) + break; + } + while (--n != 0); + } - /* Not enough room in dst, add NUL and traverse rest of src */ - if (n == 0) - { - if (siz != 0) - *d = '\0'; /* NUL-terminate dst */ - while (*s++) - ; - } + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) + { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } - return (s - src - 1); /* count does not include NUL */ + return (s - src - 1); /* count does not include NUL */ } diff --git a/ttyrec.c b/ttyrec.c index 4ed2e6a..54f1df3 100644 --- a/ttyrec.c +++ b/ttyrec.c @@ -145,7 +145,7 @@ ttyrec_main (char *username) { close (slave); pid_game = child; - gen_inprogress_lock(pid_game); + gen_inprogress_lock (pid_game); dooutput (); } else