]> skyeroc.xyz Git - dgamelaunch/commitdiff
Move gen_inprogress_lock() call to ttyrec.c, with the pid of nethack, and
authorJoshua Kwan <joshk@triplehelix.org>
Sun, 4 Jan 2004 02:03:26 +0000 (02:03 +0000)
committerJoshua Kwan <joshk@triplehelix.org>
Sun, 4 Jan 2004 02:03:26 +0000 (02:03 +0000)
purge_stale_locks() to remove all stale lockfiles.

git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@65 db0b04b0-f4d1-0310-9a6d-de3e77497b0e

dgamelaunch.c
dgamelaunch.h
ttyrec.c

index 3995884dcb644fcf073b763473264cb4cc031099..1b1aed270fa3255c4357323e8cde24a6a8077ead 100644 (file)
@@ -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);
index 1ad4fda0f9cae405be23715a6f0e6f6616605ce4..5473a17a540eab3bf62c4355b3ba99c09a50a583 100644 (file)
@@ -3,6 +3,7 @@
 #ifndef __DGAMELAUNCH_H
 #define __DGAMELAUNCH_H
 
+#include <sys/types.h>
 #include <time.h>
 
 /* 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);
index 1f4689238e56264b3e9698976b27768c77c646d9..4ed2e6a122be5e4c0cc406b88f30dfb75038522c 100644 (file)
--- 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