[LEDE-DEV,procd,15/17] utrace: Forward SIGTERM to the traced process

Message ID 20170912111250.31576-18-sojkam1@fel.cvut.cz
State Accepted
Headers show
Series
  • [LEDE-DEV,procd,01/17] utrace: Fix environment initialization
Related show

Commit Message

Michal Sojka Sept. 12, 2017, 11:12 a.m.
When a service is started with "/etc/init.d/<service> trace" or when
it has seccomp enabled (i.e. runs under seccomp-trace), stopping the
service with "/etc/init.d/<service> stop" stops only the tracer. The
service itself continue executing. This patch ensures that the service
is terminated as well.

Signed-off-by: Michal Sojka <sojkam1@fel.cvut.cz>
---
 trace/trace.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

Patch

diff --git a/trace/trace.c b/trace/trace.c
index eead9c5..d86c215 100644
--- a/trace/trace.c
+++ b/trace/trace.c
@@ -254,6 +254,15 @@  static void tracer_cb(struct uloop_process *c, int ret)
 	uloop_process_add(c);
 }
 
+static void sigterm_handler(int signum)
+{
+	/* When we receive SIGTERM, we forward it to the tracee. After
+	 * the tracee exits, trace_cb() will be called and make us
+	 * exit too. */
+	kill(tracer.proc.pid, SIGTERM);
+}
+
+
 int main(int argc, char **argv, char **envp)
 {
 	int status, ch, policy = EPERM;
@@ -361,6 +370,7 @@  int main(int argc, char **argv, char **envp)
 	tracer.proc.pid = child;
 	tracer.proc.cb = tracer_cb;
 	uloop_process_add(&tracer.proc);
+	signal(SIGTERM, sigterm_handler); /* Override uloop's SIGTERM handler */
 	uloop_run();
 	uloop_done();