[rtems-libbsd commit] rtemsbsd: Add call to program with a data restore

Sebastian Huber sebh at rtems.org
Wed Aug 3 12:44:14 UTC 2016


Module:    rtems-libbsd
Branch:    4.11
Commit:    c78c29633681328d62f3df43aaa9d13e9a1181d2
Changeset: http://git.rtems.org/rtems-libbsd/commit/?id=c78c29633681328d62f3df43aaa9d13e9a1181d2

Author:    Christian Mauderer <Christian.Mauderer at embedded-brains.de>
Date:      Fri Jul 15 11:39:48 2016 +0200

rtemsbsd: Add call to program with a data restore

This adds the rtems_bsd_program_call_main_width_data_restore function.

---

 rtemsbsd/include/machine/rtems-bsd-program.h |  5 ++++
 rtemsbsd/rtems/rtems-bsd-program.c           | 23 +++++++++++++++++++
 testsuite/program01/test_main.c              | 34 ++++++++++++++++++++++++++++
 3 files changed, 62 insertions(+)

diff --git a/rtemsbsd/include/machine/rtems-bsd-program.h b/rtemsbsd/include/machine/rtems-bsd-program.h
index 82830f5..4605017 100644
--- a/rtemsbsd/include/machine/rtems-bsd-program.h
+++ b/rtemsbsd/include/machine/rtems-bsd-program.h
@@ -53,6 +53,11 @@ int
 rtems_bsd_program_call_main(const char *name, int (*main)(int, char **),
     int argc, char **argv);
 
+int
+rtems_bsd_program_call_main_with_data_restore(const char *name,
+    int (*main)(int, char **), int argc, char **argv,
+    const void *data_buf, const size_t data_size);
+
 void
 rtems_bsd_program_exit(int exit_code) __dead2;
 
diff --git a/rtemsbsd/rtems/rtems-bsd-program.c b/rtemsbsd/rtems/rtems-bsd-program.c
index 18b9a62..8b5bf12 100644
--- a/rtemsbsd/rtems/rtems-bsd-program.c
+++ b/rtemsbsd/rtems/rtems-bsd-program.c
@@ -199,6 +199,29 @@ rtems_bsd_program_call_main(const char *name, int (*main)(int, char **),
 	return exit_code;
 }
 
+int
+rtems_bsd_program_call_main_with_data_restore(const char *name,
+    int (*main)(int, char **), int argc, char **argv,
+    const void *data_buf, const size_t data_size)
+{
+	int exit_code = EXIT_FAILURE;
+	void *savebuf;
+
+	savebuf = malloc(data_size, M_TEMP, 0);
+	if(savebuf == NULL) {
+		errno = ENOMEM;
+		exit_code = EXIT_FAILURE;
+	} else {
+		memcpy(savebuf, data_buf, data_size);
+		exit_code = rtems_bsd_program_call_main(name, main, argc,
+		    argv);
+		memcpy(data_buf, savebuf, data_size);
+		free(savebuf, M_TEMP);
+	}
+
+	return exit_code;
+}
+
 static struct mtx program_mtx;
 
 MTX_SYSINIT(rtems_bsd_program, &program_mtx, "BSD program", MTX_DEF);
diff --git a/testsuite/program01/test_main.c b/testsuite/program01/test_main.c
index 398dd62..ca14a36 100644
--- a/testsuite/program01/test_main.c
+++ b/testsuite/program01/test_main.c
@@ -170,6 +170,23 @@ no_mem_bsd_program(int fd)
 	assert(rtems_bsd_program_get_context() == NULL);
 }
 
+#define OVERWRITE_CONTENT	"Some test pattern"
+#define OVERWRITE_AFTER_RESTORE	"xxxxxtestxxxxxxxx"
+#define OVERWRITE_RESTORE_FIRST (5)
+#define OVERWRITE_RESTORE_SIZE (4)
+static const char overwrite_compare[] = OVERWRITE_AFTER_RESTORE;
+static char overwrite_me[] = OVERWRITE_CONTENT;
+
+static int
+overwrite_main(int argc, char **argv)
+{
+	size_t len = strlen(overwrite_me);
+	memset(overwrite_me, 'x', len);
+	assert(strcmp(overwrite_me, overwrite_compare) != 0);
+	errno = 0;
+	rtems_bsd_program_exit(1012);
+}
+
 static void
 test_bsd_program(void)
 {
@@ -196,11 +213,15 @@ test_bsd_program(void)
 	no_mem_bsd_program(-1);
 	rtems_workspace_greedy_free(greedy);
 
+	assert(rtems_resource_snapshot_check(&snapshot));
+
 	errno = 0;
 	exit_code = rtems_bsd_program_call_main(prog_name, NULL, 1, invalid_argv);
 	assert(errno == EFAULT);
 	assert(exit_code == EXIT_FAILURE);
 
+	assert(rtems_resource_snapshot_check(&snapshot));
+
 	errno = EINVAL;
 	exit_code = rtems_bsd_program_call(prog_name, some_prog, some_context);
 	assert(errno == 0);
@@ -208,6 +229,8 @@ test_bsd_program(void)
 	assert(strcmp(rtems_bsd_program_get_name(), "?") == 0);
 	assert(rtems_bsd_program_get_context() == NULL);
 
+	assert(rtems_resource_snapshot_check(&snapshot));
+
 	errno = EINVAL;
 	exit_code = rtems_bsd_program_call_main(prog_name, some_main,
 	    some_argc, some_argv);
@@ -217,6 +240,17 @@ test_bsd_program(void)
 	assert(rtems_bsd_program_get_context() == NULL);
 
 	assert(rtems_resource_snapshot_check(&snapshot));
+
+	exit_code = rtems_bsd_program_call_main_with_data_restore(prog_name,
+	    overwrite_main, some_argc, some_argv,
+	    overwrite_me + OVERWRITE_RESTORE_FIRST, OVERWRITE_RESTORE_SIZE);
+	assert(errno == 0);
+	assert(exit_code == 1012);
+	assert(strcmp(rtems_bsd_program_get_name(), "?") == 0);
+	assert(rtems_bsd_program_get_context() == NULL);
+	assert(strcmp(overwrite_me, overwrite_compare) == 0);
+
+	assert(rtems_resource_snapshot_check(&snapshot));
 }
 
 static void




More information about the vc mailing list