From: Joshua Kwan Date: Sun, 4 Jan 2004 02:03:26 +0000 (+0000) Subject: Move gen_inprogress_lock() call to ttyrec.c, with the pid of nethack, and X-Git-Tag: v1.6.1-roc-dev~633 X-Git-Url: https://skyeroc.xyz/gitweb/?a=commitdiff_plain;h=0daf7cdb3b658ec8ec8de3fef190bced96e31cbe;p=dgamelaunch Move gen_inprogress_lock() call to ttyrec.c, with the pid of nethack, and purge_stale_locks() to remove all stale lockfiles. git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@65 db0b04b0-f4d1-0310-9a6d-de3e77497b0e --- diff --git a/dgamelaunch.c b/dgamelaunch.c index 3995884..1b1aed2 100644 --- a/dgamelaunch.c +++ b/dgamelaunch.c @@ -134,12 +134,14 @@ gen_ttyrec_filename () /* ************************************************************* */ void -gen_inprogress_lock () +gen_inprogress_lock (pid_t pid) { - char lockfile[130]; + char lockfile[130], pidbuf[16]; int fd; struct flock fl = { 0 }; + snprintf(pidbuf, 16, "%.15d", pid); + fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; fl.l_start = 0; @@ -151,6 +153,8 @@ gen_inprogress_lock () fd = open (lockfile, O_WRONLY | O_CREAT, 0644); if (fcntl (fd, F_SETLKW, &fl) == -1) graceful_exit (68); + + write(fd, pidbuf, strlen(pidbuf)); } /* ************************************************************* */ @@ -1110,6 +1114,47 @@ graceful_exit (int status) /* ************************************************************* */ /* ************************************************************* */ +void +purge_stale_locks (void) +{ + DIR* pdir; + struct dirent *dent; + + 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); + + if (strncmp(dent->d_name, me->username, colon - dent->d_name)) + continue; + + if (!(ipfile = fopen(dent->d_name, "r"))) + graceful_exit(202); + + if (fgets(buf, 16, ipfile) == NULL) + graceful_exit(203); + + fclose(ipfile); + unlink(dent->d_name); + + pid = atoi(buf); + + kill(pid, SIGHUP); + } + + closedir(pdir); +} + int main (void) { @@ -1213,6 +1258,8 @@ main (void) endwin (); + purge_stale_locks(); + /* environment */ snprintf (atrcfilename, 81, "@%s", rcfilename); @@ -1232,7 +1279,6 @@ main (void) /* lock */ gen_ttyrec_filename (); - gen_inprogress_lock (); /* launch program */ ttyrec_main (me->username); diff --git a/dgamelaunch.h b/dgamelaunch.h index 1ad4fda..5473a17 100644 --- a/dgamelaunch.h +++ b/dgamelaunch.h @@ -3,6 +3,7 @@ #ifndef __DGAMELAUNCH_H #define __DGAMELAUNCH_H +#include #include /* Default - should work everywhere */ @@ -50,7 +51,7 @@ struct dg_game /* dgamelaunch.c function prototypes */ extern void ttyrec_getmaster (void); extern void gen_ttyrec_filename (void); -extern void gen_inprogress_lock (void); +extern void gen_inprogress_lock (pid_t pid); 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 1f46892..4ed2e6a 100644 --- a/ttyrec.c +++ b/ttyrec.c @@ -145,6 +145,7 @@ ttyrec_main (char *username) { close (slave); pid_game = child; + gen_inprogress_lock(pid_game); dooutput (); } else