Skip to contentSkip to conversation

main: Add handler for SIGSEGV signal

PATCH Applied Inga Stotland 4 participants 5 messages Jan 25, 2021 – Jan 27, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 main: Add handler for SIGSEGV signal

When an ell main loop based process is spun off as a child and the parent process dies due to a segfault, the child process is still left running and needs to detect the segfault condition. To address this need, add watch and signal handler for SIGSEGV.

---
 ell/main.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
ell/main.c +11 -0
index a479b74..712cfb7 100644
@@ -619,6 +619,14 @@ static void sigterm_handler(void *user_data)
 		data->callback(SIGTERM, data->user_data);
 }
 
+static void sigsegv_handler(void *user_data)
+{
+	struct signal_data *data = user_data;
+
+	if (data->callback)
+		data->callback(SIGSEGV, data->user_data);
+}
+
 /**
  * l_main_run_with_signal:
  *
@@ -633,6 +641,7 @@ LIB_EXPORT int l_main_run_with_signal(l_main_signal_cb_t callback,
 	struct signal_data *data;
 	struct l_signal *sigint;
 	struct l_signal *sigterm;
+	struct l_signal *sigsegv;
 	int result;
 
 	data = l_new(struct signal_data, 1);
@@ -642,11 +651,13 @@ LIB_EXPORT int l_main_run_with_signal(l_main_signal_cb_t callback,
 
 	sigint = l_signal_create(SIGINT, sigint_handler, data, NULL);
 	sigterm = l_signal_create(SIGTERM, sigterm_handler, data, NULL);
+	sigsegv = l_signal_create(SIGSEGV, sigsegv_handler, data, NULL);
 
 	result = l_main_run();
 
 	l_signal_remove(sigint);
 	l_signal_remove(sigterm);
+	l_signal_remove(sigsegv);
 
 	l_free(data);
 
Signature · 1 line
2.26.2
#1 lore raw

Hi Inga,

Quoted 8 lines · Inga Stotland

When an ell main loop based process is spun off as a child and the parent process dies due to a segfault, the child process is still left running and needs to detect the segfault condition. To address this need, add watch and signal handler for SIGSEGV.

---
  ell/main.c | 11 +++++++++++
  1 file changed, 11 insertions(+)

Applied, thanks.

Regards,
-Denis

#2 lore raw
In reply to Inga Stotland

Hi Inga,

Quoted 14 lines

When an ell main loop based process is spun off as a child and the parent process dies due to a segfault, the child process is still left running and needs to detect the segfault condition. To address this need, add watch and signal handler for SIGSEGV.
---
ell/main.c | 11 +++++++++++

1 file changed, 11 insertions(+)
ell/main.c +0 -0
index a479b74..712cfb7 100644
@@ -619,6 +619,14 @@ static void sigterm_handler(void *user_data)
 		data->callback(SIGTERM, data->user_data);

ell/main.c static void sigterm_handler(void *user_data) · line 629

Quoted 10 lines
ell/main.c +8 -0
 }
 
+static void sigsegv_handler(void *user_data)
+{
+	struct signal_data *data = user_data;
+
+	if (data->callback)
+		data->callback(SIGSEGV, data->user_data);
+}
+

actually this is not enough. I never included SIGSEGV here for the simple reason since you have to do a lot of work when that signal happens.

What we need to do is to add proper child spawning handling that does all the nasty signal handling for you. Otherwise you are just about to create zombies. For now if you need child handling, then add your own signal handler instead trying to overload l_main_run_with_signal with it.

Regards

Marcel

#3 lore raw

Hi Marcel,

On Tue, 2021-01-26 at 10:02 +0100, Marcel Holtmann wrote:

Hi Inga,

When an ell main loop based process is spun off as a child

and the parent process dies due to a segfault, the child process

is still left running and needs to detect the segfault condition.

To address this need, add watch and signal handler for SIGSEGV.

---

ell/main.c | 11 +++++++++++

1 file changed, 11 insertions(+)
ell/main.c +0 -0

index a479b74..712cfb7 100644

--- a/ell/main.c

+++ b/ell/main.c

@@ -619,6 +619,14 @@ static void sigterm_handler(void *user_data)

                data->callback(SIGTERM, data->user_data);

}

+static void sigsegv_handler(void *user_data)

+{

+ struct signal_data *data = user_data;

+

+ if (data->callback)

+ data->callback(SIGSEGV, data->user_data);

+}

+

actually this is not enough. I never included SIGSEGV here for the simple reason since you have to do a lot of work when that signal happens.

What we need to do is to add proper child spawning handling that does all the nasty signal handling for you. Otherwise you are just about to create zombies. For now if you need child handling, then add your own signal handler instead trying to overload l_main_run_with_signal with it.

I don't disagree that this is not enough.

However, this is the easiest way I could come up that allows a child to detect a parent process crash.

Not very elegant as I had to add prctl() call to the child (but only in testing mode, so should not be awful).

Do you have better suggestions on to how to catch a segfault originating in another process without modifying ell?

Best regards,

Inga

#4 lore raw
In reply to Marcel Holtmann

Hi Denis,

On Tue, 2021-01-26 at 10:02 +0100, Marcel Holtmann wrote:

Hi Inga,

When an ell main loop based process is spun off as a child

and the parent process dies due to a segfault, the child process

is still left running and needs to detect the segfault condition.

To address this need, add watch and signal handler for SIGSEGV.

---

ell/main.c | 11 +++++++++++

1 file changed, 11 insertions(+)
ell/main.c +0 -0

index a479b74..712cfb7 100644

--- a/ell/main.c

+++ b/ell/main.c

@@ -619,6 +619,14 @@ static void sigterm_handler(void *user_data)

                data->callback(SIGTERM, data->user_data);

}

+static void sigsegv_handler(void *user_data)

+{

+ struct signal_data *data = user_data;

+

+ if (data->callback)

+ data->callback(SIGSEGV, data->user_data);

+}

+

actually this is not enough. I never included SIGSEGV here for the simple reason since you have to do a lot of work when that signal happens.

What we need to do is to add proper child spawning handling that does all the nasty signal handling for you. Otherwise you are just about to create zombies. For now if you need child handling, then add your own signal handler instead trying to overload l_main_run_with_signal with it.

We may want to revert this commit. It's harmless, but adds extra code to ell.

There's a way to deal with the segfault situation across the processes without involving ell (thank you Phil for the suggestion).

Sorry for the churn.

Best Regards,

Inga