Blame SOURCES/bz1473695-dont-crash-on-shm-truncate.patch

63b087
diff --git a/include/qb/qbipcs.h b/include/qb/qbipcs.h
63b087
index 55c0f81..7b4daa7 100644
63b087
--- a/include/qb/qbipcs.h
63b087
+++ b/include/qb/qbipcs.h
63b087
@@ -142,6 +142,10 @@ typedef void (*qb_ipcs_connection_created_fn) (qb_ipcs_connection_t *c);
63b087
  * successfully created.
63b087
  * @note if you return anything but 0 this function will be
63b087
  * repeativily called (until 0 is returned).
63b087
+ *
63b087
+ * With SHM connections libqb will briefly trap SIGBUS during the
63b087
+ * disconnect process to guard against server crashes if the mapped
63b087
+ * file is truncated. The signal will be restored afterwards.
63b087
  */
63b087
 typedef int32_t (*qb_ipcs_connection_closed_fn) (qb_ipcs_connection_t *c);
63b087
 
63b087
diff --git a/lib/ipc_int.h b/lib/ipc_int.h
63b087
index 67fc444..9cd06cf 100644
63b087
--- a/lib/ipc_int.h
63b087
+++ b/lib/ipc_int.h
63b087
@@ -92,6 +92,7 @@ struct qb_ipcc_connection {
63b087
 	char name[NAME_MAX];
63b087
 	int32_t needs_sock_for_poll;
63b087
 	gid_t egid;
63b087
+	pid_t server_pid;
63b087
 	struct qb_ipc_one_way setup;
63b087
 	struct qb_ipc_one_way request;
63b087
 	struct qb_ipc_one_way response;
63b087
diff --git a/lib/ipc_setup.c b/lib/ipc_setup.c
63b087
index 57d755b..0e16964 100644
63b087
--- a/lib/ipc_setup.c
63b087
+++ b/lib/ipc_setup.c
63b087
@@ -494,6 +494,7 @@ qb_ipcc_us_setup_connect(struct qb_ipcc_connection *c,
63b087
 
63b087
 	qb_ipc_auth_creds(data);
63b087
 	c->egid = data->ugp.gid;
63b087
+	c->server_pid = data->ugp.pid;
63b087
 
63b087
 	destroy_ipc_auth_data(data);
63b087
 	return r->hdr.error;
63b087
diff --git a/lib/ipc_shm.c b/lib/ipc_shm.c
63b087
index 699f4e4..9f237b6 100644
63b087
--- a/lib/ipc_shm.c
63b087
+++ b/lib/ipc_shm.c
63b087
@@ -20,6 +20,8 @@
63b087
  */
63b087
 #include "os_base.h"
63b087
 #include <poll.h>
63b087
+#include <signal.h>
63b087
+#include <setjmp.h>
63b087
 
63b087
 #include "ipc_int.h"
63b087
 #include "util_int.h"
63b087
@@ -36,9 +38,12 @@
63b087
 static void
63b087
 qb_ipcc_shm_disconnect(struct qb_ipcc_connection *c)
63b087
 {
63b087
-	void (*rb_destructor)(struct qb_ringbuffer_s *) = c->is_connected
63b087
-		? qb_rb_close
63b087
-		: qb_rb_force_close;
63b087
+	void (*rb_destructor)(struct qb_ringbuffer_s *);
63b087
+
63b087
+	rb_destructor = qb_rb_close;
63b087
+	if (!c->is_connected && (!c->server_pid || (kill(c->server_pid, 0) == -1 && errno == ESRCH))) {
63b087
+		rb_destructor = qb_rb_force_close;
63b087
+	}
63b087
 
63b087
 	qb_ipcc_us_sock_close(c->setup.u.us.sock);
63b087
 
63b087
@@ -215,18 +220,30 @@ return_error:
63b087
  * service functions
63b087
  * --------------------------------------------------------
63b087
  */
63b087
+static jmp_buf sigbus_jmpbuf;
63b087
+static void catch_sigbus(int signal)
63b087
+{
63b087
+	longjmp(sigbus_jmpbuf, 1);
63b087
+}
63b087
 
63b087
 static void
63b087
 qb_ipcs_shm_disconnect(struct qb_ipcs_connection *c)
63b087
 {
63b087
-	if (c->state == QB_IPCS_CONNECTION_ESTABLISHED ||
63b087
-	    c->state == QB_IPCS_CONNECTION_ACTIVE) {
63b087
-		if (c->setup.u.us.sock > 0) {
63b087
-			(void)c->service->poll_fns.dispatch_del(c->setup.u.us.sock);
63b087
-			qb_ipcc_us_sock_close(c->setup.u.us.sock);
63b087
-			c->setup.u.us.sock = -1;
63b087
-		}
63b087
+	struct sigaction sa;
63b087
+	struct sigaction old_sa;
63b087
+
63b087
+	/* Don't die if the client has truncated the SHM under us */
63b087
+	memset(&old_sa, 0, sizeof(old_sa));
63b087
+	memset(&sa, 0, sizeof(sa));
63b087
+	sa.sa_handler = catch_sigbus;
63b087
+	sigemptyset(&sa.sa_mask);
63b087
+	sa.sa_flags = 0;
63b087
+	sigaction(SIGBUS, &sa, &old_sa);
63b087
+
63b087
+	if (setjmp(sigbus_jmpbuf) == 1) {
63b087
+		goto end_disconnect;
63b087
 	}
63b087
+
63b087
 	if (c->state == QB_IPCS_CONNECTION_SHUTTING_DOWN ||
63b087
 	    c->state == QB_IPCS_CONNECTION_ACTIVE) {
63b087
 		if (c->response.u.shm.rb) {
63b087
@@ -239,6 +256,17 @@ qb_ipcs_shm_disconnect(struct qb_ipcs_connection *c)
63b087
 			qb_rb_close(qb_rb_lastref_and_ret(&c->request.u.shm.rb));
63b087
 		}
63b087
 	}
63b087
+
63b087
+	if (c->state == QB_IPCS_CONNECTION_ESTABLISHED ||
63b087
+	    c->state == QB_IPCS_CONNECTION_ACTIVE) {
63b087
+		if (c->setup.u.us.sock > 0) {
63b087
+			(void)c->service->poll_fns.dispatch_del(c->setup.u.us.sock);
63b087
+			qb_ipcc_us_sock_close(c->setup.u.us.sock);
63b087
+			c->setup.u.us.sock = -1;
63b087
+		}
63b087
+	}
63b087
+end_disconnect:
63b087
+	sigaction(SIGBUS, &old_sa, NULL);
63b087
 }
63b087
 
63b087
 static int32_t
63b087
diff --git a/tests/check_ipc.c b/tests/check_ipc.c
63b087
index f8af2c5..46c3b40 100644
63b087
--- a/tests/check_ipc.c
63b087
+++ b/tests/check_ipc.c
63b087
@@ -444,18 +444,30 @@ run_ipc_server(void)
63b087
 static int32_t
63b087
 run_function_in_new_process(void (*run_ipc_server_fn)(void))
63b087
 {
63b087
-	pid_t pid = fork ();
63b087
+	pid_t pid1 = fork ();
63b087
+	pid_t pid2;
63b087
 
63b087
-	if (pid == -1) {
63b087
+	if (pid1 == -1) {
63b087
 		fprintf (stderr, "Can't fork\n");
63b087
 		return -1;
63b087
 	}
63b087
 
63b087
-	if (pid == 0) {
63b087
-		run_ipc_server_fn();
63b087
-		exit(0);
63b087
+	/* Double-fork so the servers can be reaped in a timely manner */
63b087
+	if (pid1 == 0) {
63b087
+		pid2 = fork();
63b087
+		if (pid2 == -1) {
63b087
+			fprintf (stderr, "Can't fork twice\n");
63b087
+			exit(0);
63b087
+		}
63b087
+		if (pid2 == 0) {
63b087
+			run_ipc_server_fn();
63b087
+			exit(0);
63b087
+		} else {
63b087
+			waitpid(pid2, NULL, 0);
63b087
+			exit(0);
63b087
+		}
63b087
 	}
63b087
-	return pid;
63b087
+	return pid1;
63b087
 }
63b087
 
63b087
 static void
63b087
--- a/lib/loop_poll.cg	2018-04-20 11:23:05.255007538 +0100
63b087
+++ b/lib/loop_poll.c	2018-04-20 11:23:36.000113203 +0100
63b087
@@ -723,7 +723,7 @@ qb_loop_signal_mod(qb_loop_t * lp,
63b087
 	sig->p = p;
63b087
 
63b087
 	if (sig->signal != the_sig) {
63b087
-		signal(sig->signal, SIG_DFL);
63b087
+		(void)signal(sig->signal, SIG_DFL);
63b087
 		sig->signal = the_sig;
63b087
 		_adjust_sigactions_(s);
63b087
 	}
63b087
@@ -774,7 +774,7 @@ qb_loop_signal_del(qb_loop_t * lp, qb_lo
63b087
 	}
63b087
 
63b087
 	qb_list_del(&sig->item.list);
63b087
-	signal(sig->signal, SIG_DFL);
63b087
+	(void)signal(sig->signal, SIG_DFL);
63b087
 	free(sig);
63b087
 	_adjust_sigactions_(s);
63b087
 	return 0;