forked from checkpoint-restore/criu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a zdtm test to test checkpoint/restore a memfd_secret fd containing process. Signed-off-by: Dhanuka Warusadura <[email protected]>
- Loading branch information
1 parent
a51e18d
commit bbc20f9
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <sys/mman.h> | ||
#include <sys/syscall.h> | ||
#include <unistd.h> | ||
|
||
#include "zdtmtst.h" | ||
|
||
#define SECRET "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
#define SIZE 26 | ||
|
||
const char *test_doc = "memfd_secret file descriptor"; | ||
const char *test_author = "Dhanuka Warusadura <[email protected]>"; | ||
|
||
#ifdef __NR_memfd_secret | ||
|
||
static int _memfd_secret(unsigned int flags) | ||
{ | ||
return syscall(__NR_memfd_secret, flags); | ||
} | ||
|
||
static void *secret_init(size_t size) | ||
{ | ||
int fd; | ||
void *secretmem = NULL; | ||
|
||
fd = _memfd_secret(0); | ||
if (fd < 0) | ||
return secretmem; | ||
|
||
if (ftruncate(fd, size) < 0) { | ||
close(fd); | ||
return secretmem; | ||
} | ||
|
||
secretmem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | ||
if (secretmem == MAP_FAILED) { | ||
close(fd); | ||
return secretmem; | ||
} | ||
|
||
return secretmem; | ||
} | ||
|
||
static void secret_fini(void *mem, size_t size) | ||
{ | ||
munmap(mem, size); | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
char *secretmem; | ||
|
||
test_init(argc, argv); | ||
|
||
secretmem = secret_init(SIZE); | ||
if (!secretmem) { | ||
fail("memfd_secret: not supported operation"); | ||
return 1; | ||
} | ||
|
||
memcpy(secretmem, SECRET, SIZE); | ||
|
||
test_daemon(); | ||
test_waitsig(); | ||
|
||
if (strncmp(secretmem, SECRET, SIZE)) { | ||
fail("secretmem content mismatch"); | ||
return 1; | ||
} | ||
|
||
secret_fini(secretmem, SIZE); | ||
|
||
pass(); | ||
|
||
return 0; | ||
} | ||
|
||
#else | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
test_init(argc, argv); | ||
skip("This test is supposed to run if secretmem feature is enabled"); | ||
return 0; | ||
} | ||
|
||
#endif |