mirror of
https://git.rtems.org/rtems-libbsd/
synced 2025-05-13 18:21:06 +08:00
rtemsbsd: Add call to program with a data restore
This adds the rtems_bsd_program_call_main_width_data_restore function.
This commit is contained in:
parent
110dbd0267
commit
c78c296336
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user