1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 
9 int main(int argc, char **argv)
10 {
11 	const char *path;
12 	char buf[5];
13 	int fd, rc;
14 
15 	if (argc < 2) {
16 		fprintf(stderr, "usage: %s <path>\n", argv[0]);
17 		return EXIT_FAILURE;
18 	}
19 
20 	path = argv[1];
21 
22 	/* attributes: EFI_VARIABLE_NON_VOLATILE |
23 	 *		EFI_VARIABLE_BOOTSERVICE_ACCESS |
24 	 *		EFI_VARIABLE_RUNTIME_ACCESS
25 	 */
26 	*(uint32_t *)buf = 0x7;
27 	buf[4] = 0;
28 
29 	/* create a test variable */
30 	fd = open(path, O_WRONLY | O_CREAT);
31 	if (fd < 0) {
32 		perror("open(O_WRONLY)");
33 		return EXIT_FAILURE;
34 	}
35 
36 	rc = write(fd, buf, sizeof(buf));
37 	if (rc != sizeof(buf)) {
38 		perror("write");
39 		return EXIT_FAILURE;
40 	}
41 
42 	close(fd);
43 
44 	fd = open(path, O_RDONLY);
45 	if (fd < 0) {
46 		perror("open");
47 		return EXIT_FAILURE;
48 	}
49 
50 	if (unlink(path) < 0) {
51 		perror("unlink");
52 		return EXIT_FAILURE;
53 	}
54 
55 	rc = read(fd, buf, sizeof(buf));
56 	if (rc > 0) {
57 		fprintf(stderr, "reading from an unlinked variable "
58 				"shouldn't be possible\n");
59 		return EXIT_FAILURE;
60 	}
61 
62 	return EXIT_SUCCESS;
63 }
64