b97725
From 354efb96f1e4574f458e994163bbe31c76769573 Mon Sep 17 00:00:00 2001
b97725
From: Chet Ramey <chet.ramey@case.edu>
b97725
Date: Fri, 1 Jun 2018 10:19:56 -0400
b97725
Subject: [PATCH] saved background process status hash table loop fixes
b97725
b97725
---
b97725
 jobs.c       | 62 +++++++++++++++++++++++++++++++++++++++++-----------
b97725
 patchlevel.h |  2 +-
b97725
 2 files changed, 50 insertions(+), 14 deletions(-)
b97725
b97725
diff --git a/jobs.c b/jobs.c
b97725
index fc966036..2684632d 100644
b97725
--- a/jobs.c
b97725
+++ b/jobs.c
b97725
@@ -812,8 +812,22 @@ bgp_add (pid, status)
b97725
   ps_index_t *bucket, psi;
b97725
   struct pidstat *ps;
b97725
 
b97725
-  bucket = pshash_getbucket (pid);
b97725
-  psi = bgp_getindex ();
b97725
+  /* bucket == existing chain of pids hashing to same value
b97725
+     psi = where were going to put this pid/status */
b97725
+
b97725
+  bucket = pshash_getbucket (pid);	/* index into pidstat_table */
b97725
+  psi = bgp_getindex ();		/* bgpids.head, index into storage */
b97725
+
b97725
+  /* XXX - what if psi == *bucket? */
b97725
+  if (psi == *bucket)
b97725
+    {
b97725
+#ifdef DEBUG
b97725
+      internal_warning ("hashed pid %d (pid %d) collides with bgpids.head, skipping", psi, pid);
b97725
+#endif
b97725
+      bgpids.storage[psi].pid = NO_PID;		/* make sure */
b97725
+      psi = bgp_getindex ();			/* skip to next one */
b97725
+    }
b97725
+
b97725
   ps = &bgpids.storage[psi];
b97725
 
b97725
   ps->pid = pid;
b97725
@@ -841,32 +855,47 @@ pshash_delindex (psi)
b97725
      ps_index_t psi;
b97725
 {
b97725
   struct pidstat *ps;
b97725
+  ps_index_t *bucket;
b97725
 
b97725
   ps = &bgpids.storage[psi];
b97725
   if (ps->pid == NO_PID)
b97725
     return;
b97725
 
b97725
-  if (ps->bucket_next != NO_PID)
b97725
+  if (ps->bucket_next != NO_PIDSTAT)
b97725
     bgpids.storage[ps->bucket_next].bucket_prev = ps->bucket_prev;
b97725
-  if (ps->bucket_prev != NO_PID)
b97725
+  if (ps->bucket_prev != NO_PIDSTAT)
b97725
     bgpids.storage[ps->bucket_prev].bucket_next = ps->bucket_next;
b97725
   else
b97725
-    *(pshash_getbucket (ps->pid)) = ps->bucket_next;
b97725
+    {
b97725
+      bucket = pshash_getbucket (ps->pid);
b97725
+      *bucket = ps->bucket_next;	/* deleting chain head in hash table */
b97725
+    }
b97725
+
b97725
+  /* clear out this cell, just in case */
b97725
+  ps->pid = NO_PID;
b97725
+  ps->bucket_next = ps->bucket_prev = NO_PIDSTAT;
b97725
 }
b97725
 
b97725
 static int
b97725
 bgp_delete (pid)
b97725
      pid_t pid;
b97725
 {
b97725
-  ps_index_t psi;
b97725
+  ps_index_t psi, orig_psi;
b97725
 
b97725
   if (bgpids.storage == 0 || bgpids.nalloc == 0 || bgpids.npid == 0)
b97725
     return 0;
b97725
 
b97725
   /* Search chain using hash to find bucket in pidstat_table */
b97725
-  for (psi = *(pshash_getbucket (pid)); psi != NO_PIDSTAT; psi = bgpids.storage[psi].bucket_next)
b97725
-    if (bgpids.storage[psi].pid == pid)
b97725
-      break;
b97725
+  for (orig_psi = psi = *(pshash_getbucket (pid)); psi != NO_PIDSTAT; psi = bgpids.storage[psi].bucket_next)
b97725
+    {
b97725
+      if (bgpids.storage[psi].pid == pid)
b97725
+	break;
b97725
+      if (orig_psi == bgpids.storage[psi].bucket_next)	/* catch reported bug */
b97725
+	{
b97725
+	  internal_warning ("bgp_delete: LOOP: psi (%d) == storage[psi].bucket_next", psi);
b97725
+	  return 0;
b97725
+	}
b97725
+    }
b97725
 
b97725
   if (psi == NO_PIDSTAT)
b97725
     return 0;		/* not found */
b97725
@@ -904,15 +933,22 @@ static int
b97725
 bgp_search (pid)
b97725
      pid_t pid;
b97725
 {
b97725
-  ps_index_t psi;
b97725
+  ps_index_t psi, orig_psi;
b97725
 
b97725
   if (bgpids.storage == 0 || bgpids.nalloc == 0 || bgpids.npid == 0)
b97725
     return -1;
b97725
 
b97725
   /* Search chain using hash to find bucket in pidstat_table */
b97725
-  for (psi = *(pshash_getbucket (pid)); psi != NO_PIDSTAT; psi = bgpids.storage[psi].bucket_next)
b97725
-    if (bgpids.storage[psi].pid == pid)
b97725
-      return (bgpids.storage[psi].status);
b97725
+  for (orig_psi = psi = *(pshash_getbucket (pid)); psi != NO_PIDSTAT; psi = bgpids.storage[psi].bucket_next)
b97725
+    {
b97725
+      if (bgpids.storage[psi].pid == pid)
b97725
+	return (bgpids.storage[psi].status);
b97725
+      if (orig_psi == bgpids.storage[psi].bucket_next)	/* catch reported bug */
b97725
+	{
b97725
+	  internal_warning ("bgp_search: LOOP: psi (%d) == storage[psi].bucket_next", psi);
b97725
+	  return -1;
b97725
+	}
b97725
+    }
b97725
 
b97725
   return -1;
b97725
 }
b97725
diff --git a/patchlevel.h b/patchlevel.h
b97725
index a711c495..4a65dc0f 100644
b97725
--- a/patchlevel.h
b97725
+++ b/patchlevel.h
b97725
@@ -25,6 +25,6 @@
b97725
    regexp `^#define[ 	]*PATCHLEVEL', since that's what support/mkversion.sh
b97725
    looks for to find the patch level (for the sccs version string). */
b97725
 
b97725
-#define PATCHLEVEL 19
b97725
+#define PATCHLEVEL 20
b97725
 
b97725
 #endif /* _PATCHLEVEL_H_ */
b97725
-- 
b97725
2.29.2
b97725