From 4e6139d9fbe2dd86033017c90104268ee947bc12 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Beat=20K=C3=BCng?= <beat-kueng@gmx.net>
Date: Fri, 31 Aug 2018 13:28:28 +0200
Subject: [PATCH] Posix: add '#pragma GCC poison exit'

Generally exit() should not be used on Posix, because it exits the whole
program instead of only the task.
---
 platforms/posix/src/px4_daemon/client.cpp           |  4 ++--
 platforms/posix/src/px4_layer/px4_posix_tasks.cpp   |  2 +-
 src/include/visibility.h                            | 12 ++++++++++++
 src/lib/DriverFramework                             |  2 +-
 src/modules/events/temperature_calibration/task.cpp |  2 +-
 src/modules/systemlib/uthash/utarray.h              |  3 ++-
 src/platforms/apps.cpp.in                           |  2 +-
 7 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/platforms/posix/src/px4_daemon/client.cpp b/platforms/posix/src/px4_daemon/client.cpp
index 587fada858..560475c8e3 100644
--- a/platforms/posix/src/px4_daemon/client.cpp
+++ b/platforms/posix/src/px4_daemon/client.cpp
@@ -330,7 +330,7 @@ Client::_sig_handler(int sig_num)
 
 	if (_client_send_pipe_fd < 0) {
 		PX4_ERR("pipe open fail");
-		exit(-1);
+		system_exit(-1);
 	}
 
 	int bytes_to_send = get_client_send_packet_length(&packet);
@@ -338,7 +338,7 @@ Client::_sig_handler(int sig_num)
 
 	if (bytes_sent != bytes_to_send) {
 		PX4_ERR("write fail");
-		exit(-1);
+		system_exit(-1);
 	}
 }
 
diff --git a/platforms/posix/src/px4_layer/px4_posix_tasks.cpp b/platforms/posix/src/px4_layer/px4_posix_tasks.cpp
index e74c588419..607d5071b9 100644
--- a/platforms/posix/src/px4_layer/px4_posix_tasks.cpp
+++ b/platforms/posix/src/px4_layer/px4_posix_tasks.cpp
@@ -115,7 +115,7 @@ void
 px4_systemreset(bool to_bootloader)
 {
 	PX4_WARN("Called px4_system_reset");
-	exit(0);
+	system_exit(0);
 }
 
 px4_task_t px4_task_spawn_cmd(const char *name, int scheduler, int priority, int stack_size, px4_main_t entry,
diff --git a/src/include/visibility.h b/src/include/visibility.h
index 00fa1a78e7..9f1cf06c6f 100644
--- a/src/include/visibility.h
+++ b/src/include/visibility.h
@@ -59,6 +59,18 @@
 #  define __END_DECLS
 #endif
 
+/* exit() is used on NuttX to exit a task. However on Posix, it will exit the
+ * whole application, so we prevent its use there. There are cases where it
+ * still needs to be used, thus we remap system_exit to exit.
+ */
+#define system_exit exit
+#ifdef __PX4_POSIX
+#include <stdlib.h>
+#ifdef __cplusplus
+#include <cstdlib>
+#endif
+#pragma GCC poison exit
+#endif
 
 #ifdef __PX4_NUTTX
 /* On NuttX we call clearenv() so we cannot use getenv() and others (see px4_task_spawn_cmd() in px4_nuttx_tasks.c).
diff --git a/src/lib/DriverFramework b/src/lib/DriverFramework
index 3bdfdf3db2..b439556d54 160000
--- a/src/lib/DriverFramework
+++ b/src/lib/DriverFramework
@@ -1 +1 @@
-Subproject commit 3bdfdf3db201d19dc243a1a1f1f3f85b55d08212
+Subproject commit b439556d54aa53532586e69de87cc720af4543c2
diff --git a/src/modules/events/temperature_calibration/task.cpp b/src/modules/events/temperature_calibration/task.cpp
index 4fc1a82ef5..3f2afeb7ce 100644
--- a/src/modules/events/temperature_calibration/task.cpp
+++ b/src/modules/events/temperature_calibration/task.cpp
@@ -87,7 +87,7 @@ public:
 
 	void		task_main();
 
-	void exit() { _force_task_exit = true; }
+	void exit_task() { _force_task_exit = true; }
 
 private:
 	void publish_led_control(led_control_s &led_control);
diff --git a/src/modules/systemlib/uthash/utarray.h b/src/modules/systemlib/uthash/utarray.h
index 4ffb630bf3..b29e2d0268 100644
--- a/src/modules/systemlib/uthash/utarray.h
+++ b/src/modules/systemlib/uthash/utarray.h
@@ -39,7 +39,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <string.h>  /* memset, etc */
 #include <stdlib.h>  /* exit */
 
-#define oom() exit(-1)
+// FIXME: this needs to be checked: we need to handle OOM properly instead of just exiting
+#define oom() system_exit(-1)
 
 typedef void (ctor_f)(void *dst, const void *src);
 typedef void (dtor_f)(void *elt);
diff --git a/src/platforms/apps.cpp.in b/src/platforms/apps.cpp.in
index de416a2589..bc417688f4 100644
--- a/src/platforms/apps.cpp.in
+++ b/src/platforms/apps.cpp.in
@@ -48,7 +48,7 @@ void list_builtins(apps_map_type &apps)
 int shutdown_main(int argc, char *argv[])
 {
 	printf("Shutting down\n");
-	exit(0);
+	system_exit(0);
 }
 
 int list_tasks_main(int argc, char *argv[])
-- 
GitLab