genode-ehmry/repos/libports/src/test/execve/test.cc
Norman Feske 6894ced63b libc: execve
This patch implements 'execve' in Genode's libc.

The mechanism relies on the dynamic linker's ability to replace the
loaded binary while keeping crucial libraries - in particular the libc -
intact. The state outside the libc is wiped. For this reason, all libc
internal state needed beyond the 'execve' call must be allocated on a
heap separate from the application-owned malloc heap. E.g.,
libc-internal file-descriptor objects must not be allocated or refer to
any memory object allocated from the malloc heap.

Issue #3481
2019-08-28 14:19:45 +02:00

38 lines
589 B
C++

/*
* \brief Simple execve test
* \author Norman Feske
* \date 2019-08-20
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
if (argc <= 1)
return -1;
int const count = atoi(argv[1]);
printf("count %d\n", count);
if (count <= 0)
return 0;
{
char argv0[20];
char argv1[20];
snprintf(argv0, sizeof(argv0), "test-execve");
snprintf(argv1, sizeof(argv1), "%d", count - 1);
char *argv[] { argv0, argv1, NULL };
execve("test-execve", argv, NULL);
}
printf("This code should never be reached.\n");
return -1;
}