[PATCH] irmd: Retry bind for live processes

  • From: Dimitri Staessens <dimitri.staessens@xxxxxxxx>
  • To: ouroboros@xxxxxxxxxxxxx
  • Date: Wed, 6 Jun 2018 15:50:03 +0200

If bind was called before the process registered with the IRMd, the
bind operation would fail. The IRMd will now wait for a short period
until the process is registered or exits.

Signed-off-by: Dimitri Staessens <dimitri.staessens@xxxxxxxx>
---
 src/irmd/main.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/src/irmd/main.c b/src/irmd/main.c
index 19bcc8c..75f4a6c 100644
--- a/src/irmd/main.c
+++ b/src/irmd/main.c
@@ -69,6 +69,7 @@
 #define SHM_SAN_HOLDOFF 1000 /* ms */
 #define IPCP_HASH_LEN(e) hash_len(e->dir_hash_algo)
 #define IB_LEN SOCK_BUF_SIZE
+#define BIND_TIMEOUT  10 /* ms */
 
 enum init_state {
         IPCP_NULL = 0,
@@ -758,18 +759,31 @@ static int bind_program(char *   prog,
 static int bind_process(pid_t  pid,
                         char * name)
 {
-        char * name_dup = NULL;
-        struct proc_entry * e = NULL;
-        struct reg_entry * re = NULL;
+        char * name_dup        = NULL;
+        struct proc_entry * e  = NULL;
+        struct reg_entry *  re = NULL;
+        struct timespec     now;
+        struct timespec     dl = {0, 10 * MILLION};
 
         if (name == NULL)
                 return -EINVAL;
 
+        clock_gettime(PTHREAD_COND_CLOCK, &now);
+
+        ts_add(&dl, &now, &dl);
+
         pthread_rwlock_wrlock(&irmd.reg_lock);
 
-        e = proc_table_get(&irmd.proc_table, pid);
+        while(!kill(pid, 0)) {
+                e = proc_table_get(&irmd.proc_table, pid);
+                if (e != NULL || ts_diff_ms(&now, &dl) > 0)
+                        break;
+                clock_gettime(PTHREAD_COND_CLOCK, &now);
+                sched_yield();
+        }
         if (e == NULL) {
-                log_err("Process %d does not exist.", pid);
+                log_err("Process %d does not %s.", pid,
+                        kill(pid, 0) ? "exist" : "respond");
                 pthread_rwlock_unlock(&irmd.reg_lock);
                 return -1;
         }
-- 
2.17.1


Other related posts: