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:
parent
0daf7cdb3b
commit
71f367b1b4
115
dgamelaunch.c
115
dgamelaunch.c
|
@ -140,8 +140,8 @@ 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;
|
||||||
fl.l_start = 0;
|
fl.l_start = 0;
|
||||||
|
@ -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,8 +285,8 @@ 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 ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -509,7 +509,7 @@ domailuser (char *username)
|
||||||
time_t now;
|
time_t now;
|
||||||
int mail_empty = 1;
|
int mail_empty = 1;
|
||||||
struct flock fl = { 0 };
|
struct flock fl = { 0 };
|
||||||
|
|
||||||
fl.l_type = F_WRLCK;
|
fl.l_type = F_WRLCK;
|
||||||
fl.l_whence = SEEK_SET;
|
fl.l_whence = SEEK_SET;
|
||||||
fl.l_start = 0;
|
fl.l_start = 0;
|
||||||
|
@ -560,15 +560,16 @@ domailuser (char *username)
|
||||||
refresh ();
|
refresh ();
|
||||||
|
|
||||||
while (fcntl (fileno (user_spool), F_SETLK, &fl) == -1)
|
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.");
|
if (errno != EAGAIN)
|
||||||
getch();
|
{
|
||||||
return;
|
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);
|
fprintf (user_spool, "%s:%s\n", me->username, message);
|
||||||
|
|
||||||
|
@ -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, ':');
|
|
||||||
/* should never happen */
|
|
||||||
if (!colon)
|
|
||||||
graceful_exit(201);
|
|
||||||
|
|
||||||
if (strncmp(dent->d_name, me->username, colon - dent->d_name))
|
colon = strchr (dent->d_name, ':');
|
||||||
continue;
|
/* should never happen */
|
||||||
|
if (!colon)
|
||||||
|
graceful_exit (201);
|
||||||
|
|
||||||
if (!(ipfile = fopen(dent->d_name, "r")))
|
if (strncmp (dent->d_name, me->username, colon - dent->d_name))
|
||||||
graceful_exit(202);
|
continue;
|
||||||
|
|
||||||
if (fgets(buf, 16, ipfile) == NULL)
|
if (!(ipfile = fopen (dent->d_name, "r")))
|
||||||
graceful_exit(203);
|
graceful_exit (202);
|
||||||
|
|
||||||
fclose(ipfile);
|
if (fgets (buf, 16, ipfile) == NULL)
|
||||||
unlink(dent->d_name);
|
graceful_exit (203);
|
||||||
|
|
||||||
pid = atoi(buf);
|
fclose (ipfile);
|
||||||
|
unlink (dent->d_name);
|
||||||
|
|
||||||
kill(pid, SIGHUP);
|
pid = atoi (buf);
|
||||||
}
|
|
||||||
|
kill (pid, SIGHUP);
|
||||||
closedir(pdir);
|
|
||||||
|
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
|
int
|
||||||
|
@ -1258,8 +1283,8 @@ main (void)
|
||||||
|
|
||||||
endwin ();
|
endwin ();
|
||||||
|
|
||||||
purge_stale_locks();
|
purge_stale_locks ();
|
||||||
|
|
||||||
/* environment */
|
/* environment */
|
||||||
snprintf (atrcfilename, 81, "@%s", rcfilename);
|
snprintf (atrcfilename, 81, "@%s", rcfilename);
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
52
strlcpy.c
52
strlcpy.c
|
@ -29,7 +29,7 @@
|
||||||
|
|
||||||
#if defined(LIBC_SCCS) && !defined(lint)
|
#if defined(LIBC_SCCS) && !defined(lint)
|
||||||
static char *rcsid =
|
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 */
|
#endif /* LIBC_SCCS and not lint */
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
@ -42,33 +42,33 @@ static char *rcsid =
|
||||||
*/
|
*/
|
||||||
size_t
|
size_t
|
||||||
strlcpy (dst, src, siz)
|
strlcpy (dst, src, siz)
|
||||||
char *dst;
|
char *dst;
|
||||||
const char *src;
|
const char *src;
|
||||||
size_t siz;
|
size_t siz;
|
||||||
{
|
{
|
||||||
register char *d = dst;
|
register char *d = dst;
|
||||||
register const char *s = src;
|
register const char *s = src;
|
||||||
register size_t n = siz;
|
register size_t n = siz;
|
||||||
|
|
||||||
/* Copy as many bytes as will fit */
|
/* Copy as many bytes as will fit */
|
||||||
if (n != 0 && --n != 0)
|
if (n != 0 && --n != 0)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if ((*d++ = *s++) == 0)
|
if ((*d++ = *s++) == 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
while (--n != 0);
|
while (--n != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Not enough room in dst, add NUL and traverse rest of src */
|
/* Not enough room in dst, add NUL and traverse rest of src */
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
{
|
{
|
||||||
if (siz != 0)
|
if (siz != 0)
|
||||||
*d = '\0'; /* NUL-terminate dst */
|
*d = '\0'; /* NUL-terminate dst */
|
||||||
while (*s++)
|
while (*s++)
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (s - src - 1); /* count does not include NUL */
|
return (s - src - 1); /* count does not include NUL */
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue