]> skyeroc.xyz Git - dgamelaunch/commitdiff
Reindent, and add some error handling for kill
authorJoshua Kwan <joshk@triplehelix.org>
Sun, 4 Jan 2004 02:19:12 +0000 (02:19 +0000)
committerJoshua Kwan <joshk@triplehelix.org>
Sun, 4 Jan 2004 02:19:12 +0000 (02:19 +0000)
git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@66 db0b04b0-f4d1-0310-9a6d-de3e77497b0e

dgamelaunch.c
dgamelaunch.h
strlcpy.c
ttyrec.c

index 1b1aed270fa3255c4357323e8cde24a6a8077ead..3199717e3d55c4482c3fe8bf3a1bf481d236238e 100644 (file)
@@ -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)
 {
-  DIRpdir;
+  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;
+      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))
+        continue;
 
-  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 (!(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);
 
-    pid = atoi(buf);
+      errno = 0;
 
-    kill(pid, SIGHUP);
-  }
-  
-  closedir(pdir);
+      /* 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);
 
index 5473a17a540eab3bf62c4355b3ba99c09a50a583..bb5a1813e968e228b682f025cdc6afd178e24b9e 100644 (file)
@@ -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
index 51e86702118e15acb81081feb6ceb6f956e01b98..020e1aadbd9ade07f6c576416ffcf6df1ed852f9 100644 (file)
--- 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 <sys/types.h>
@@ -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 */
 }
index 4ed2e6a122be5e4c0cc406b88f30dfb75038522c..54f1df3aed7fc763143d17c5cfeae186b2f0a055 100644 (file)
--- 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