diff --git a/src/rpmsg_char.c b/src/rpmsg_char.c
index b03819634adfb8368b6e281e3e23b7c858f5e982..88cc5dbc92bece93e827a4d07021bdd3e73830bb 100644 (file)
--- a/src/rpmsg_char.c
+++ b/src/rpmsg_char.c
#include <unistd.h>
#include <stdint.h>
#include <pthread.h>
+#include <signal.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <linux/rpmsg.h>
static void _rpmsg_char_cleanup(void)
{
- struct rpmsg_char_endpt *iter;
+ struct rpmsg_char_endpt *iter, *next;
int ret;
if (ghead)
fprintf(stderr, "Application did not close some rpmsg_char devices\n");
- for (iter = ghead; iter != NULL; iter = iter->next) {
+ for (iter = ghead; iter != NULL; iter = next) {
+ next = iter->next;
ret = rpmsg_char_close(&iter->rcdev);
if (ret) {
fprintf(stderr, "rpmsg_char_close failed during cleanup, rcdev = 0x%x, ret = %d\n",
}
}
+static void signal_handler(int sig)
+{
+ fprintf(stderr, "\nClean up and exit while handling signal %d\n", sig);
+ rpmsg_char_exit();
+ exit(EXIT_FAILURE);
+}
+
+/*
+ * setup the signal handlers optionally, only if applications have not
+ * registered one themselves. Any new signal handler registration in
+ * the application after rpmsg_char_init() is invoked will install the
+ * application's handlers. Applications are responsible for calling
+ * rpmsg_char_exit() in their signal handlers (if present) to ensure
+ * proper cleanup
+ */
+static int _rpmsg_char_register_signal_handlers(void)
+{
+ struct sigaction sigint = { 0 };
+ struct sigaction sigterm = { 0 };
+ int ret;
+
+ ret = sigaction(SIGINT, NULL, &sigint);
+ if (ret < 0)
+ return -errno;
+
+ ret = sigaction(SIGTERM, NULL, &sigterm);
+ if (ret < 0)
+ return -errno;
+
+ if (!sigint.sa_sigaction)
+ signal(SIGINT, signal_handler);
+ if (!sigterm.sa_sigaction)
+ signal(SIGTERM, signal_handler);
+
+ return 0;
+}
+
rpmsg_char_dev_t *rpmsg_char_open(enum rproc_id id, char *dev_name,
int remote_endpt, char *eptdev_name,
int flags)
if (ret < 0)
goto out;
+ ret = _rpmsg_char_register_signal_handlers();
+ if (ret < 0)
+ goto out;
+
inited = true;
out: