summaryrefslogtreecommitdiffstats
path: root/tests/memory/memfd_create/t_memfd_create.c
blob: 73d641a6e14cec94f7a45c650e3fe5fadbbb317c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright (c) 2016 Red Hat, Inc.
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 *
 */

#include <linux/memfd.h>
#include <linux/fcntl.h>
#include <sys/syscall.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
} while (0)

int main(int argc, char *argv[])
{
	int fd;
	unsigned int seals;
	char *addr;
	char *name, *seals_arg;
	ssize_t len;
	char* message = "this is a test message for memfd\n";

	if (argc < 3) {
		fprintf(stderr, "%s name size [seals]\n", argv[0]);
		fprintf(stderr, "\t'seals' can contain any of the "
				"following characters:\n");
		fprintf(stderr, "\t\tg - F_SEAL_GROW\n");
		fprintf(stderr, "\t\ts - F_SEAL_SHRINK\n");
		fprintf(stderr, "\t\tw - F_SEAL_WRITE\n");
		fprintf(stderr, "\t\tS - F_SEAL_SEAL\n");
		exit(EXIT_FAILURE);
	}

	name = argv[1];
	len = atoi(argv[2]);
	seals_arg = argv[3];

	/* Create an anonymous file in tmpfs; allow seals to be
	   placed on the file */

	fd = syscall(SYS_memfd_create, name, MFD_ALLOW_SEALING);
	if (fd == -1)
		errExit("memfd_create");

	/* Size the file as specified on the command line */

	if (ftruncate(fd, len) == -1)
		errExit("truncate");

	if (write(fd, message, strlen(message)) <= 0)
		errExit("write");

	//printf("PID: %ld; fd: %d; /proc/%ld/fd/%d\n",
	//        (long) getpid(), fd, (long) getpid(), fd);
	printf("/proc/%ld/fd/%d\n", (long) getpid(), fd);

	/* Code to map the file and populate the mapping with data
	   omitted */

	/* If a 'seals' command-line argument was supplied, set some
	   seals on the file */

	if (seals_arg != NULL) {
		seals = 0;

		if (strchr(seals_arg, 'g') != NULL)
			seals |= F_SEAL_GROW;
		if (strchr(seals_arg, 's') != NULL)
			seals |= F_SEAL_SHRINK;
		if (strchr(seals_arg, 'w') != NULL)
			seals |= F_SEAL_WRITE;
		if (strchr(seals_arg, 'S') != NULL)
			seals |= F_SEAL_SEAL;

		if (fcntl(fd, F_ADD_SEALS, seals) == -1)
			errExit("fcntl");
	}

	/* Keep running, so that the file created by memfd_create()
	   continues to exist */

	pause();

	exit(EXIT_SUCCESS);
}