[OpenWrt-Devel,2/7] procd: guard fork_worker calls

Message ID f5bb389345392ed9d6e2012c671d0f22b236bbb8.1546385477.git.mhei@heimpold.de
State New
Headers show
Series
  • procd: console hotplugging support
Related show

Commit Message

Michael Heimpold Jan. 1, 2019, 11:44 p.m.
Usually respawn(), askfirst(), askconsole() and rcrespawn() are run only
one time to start a worker child for the given inittab entry.

In case we want to allow calling these functions several times, we need
to ensure that we do not start multiple workers at the same time for the
same inittab item.

For this, we can re-use the remembered pid of the worker child,
however, we need to reset this pid to allow a new instance in case the
previous child exited.

Signed-off-by: Michael Heimpold <mhei@heimpold.de>
---
 inittab.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

Patch

diff --git a/inittab.c b/inittab.c
index c9e6c13..b8552e9 100644
--- a/inittab.c
+++ b/inittab.c
@@ -120,14 +120,17 @@  static void child_exit(struct uloop_process *proc, int ret)
 {
 	struct init_action *a = container_of(proc, struct init_action, proc);
 
-	DEBUG(4, "pid:%d\n", proc->pid);
-        uloop_timeout_set(&a->tout, a->respawn);
+	DEBUG(4, "pid:%d, exitcode:%d\n", proc->pid, ret);
+	proc->pid = 0;
+
+	uloop_timeout_set(&a->tout, a->respawn);
 }
 
 static void respawn(struct uloop_timeout *tout)
 {
 	struct init_action *a = container_of(tout, struct init_action, tout);
-	fork_worker(a);
+	if (!a->proc.pid)
+		fork_worker(a);
 }
 
 static void rcdone(struct runqueue *q)
@@ -163,7 +166,8 @@  static void askfirst(struct init_action *a)
 	a->respawn = 500;
 
 	a->proc.cb = child_exit;
-	fork_worker(a);
+	if (!a->proc.pid)
+		fork_worker(a);
 }
 
 static void askconsole(struct init_action *a)
@@ -197,7 +201,8 @@  static void askconsole(struct init_action *a)
 	a->respawn = 500;
 
 	a->proc.cb = child_exit;
-	fork_worker(a);
+	if (!a->proc.pid)
+		fork_worker(a);
 }
 
 static void rcrespawn(struct init_action *a)
@@ -206,7 +211,8 @@  static void rcrespawn(struct init_action *a)
 	a->respawn = 500;
 
 	a->proc.cb = child_exit;
-	fork_worker(a);
+	if (!a->proc.pid)
+		fork_worker(a);
 }
 
 static struct init_handler handlers[] = {