open-unlink.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. int main(int argc, char **argv)
  9. {
  10. const char *path;
  11. char buf[5];
  12. int fd, rc;
  13. if (argc < 2) {
  14. fprintf(stderr, "usage: %s <path>\n", argv[0]);
  15. return EXIT_FAILURE;
  16. }
  17. path = argv[1];
  18. /* attributes: EFI_VARIABLE_NON_VOLATILE |
  19. * EFI_VARIABLE_BOOTSERVICE_ACCESS |
  20. * EFI_VARIABLE_RUNTIME_ACCESS
  21. */
  22. *(uint32_t *)buf = 0x7;
  23. buf[4] = 0;
  24. /* create a test variable */
  25. fd = open(path, O_WRONLY | O_CREAT);
  26. if (fd < 0) {
  27. perror("open(O_WRONLY)");
  28. return EXIT_FAILURE;
  29. }
  30. rc = write(fd, buf, sizeof(buf));
  31. if (rc != sizeof(buf)) {
  32. perror("write");
  33. return EXIT_FAILURE;
  34. }
  35. close(fd);
  36. fd = open(path, O_RDONLY);
  37. if (fd < 0) {
  38. perror("open");
  39. return EXIT_FAILURE;
  40. }
  41. if (unlink(path) < 0) {
  42. perror("unlink");
  43. return EXIT_FAILURE;
  44. }
  45. rc = read(fd, buf, sizeof(buf));
  46. if (rc > 0) {
  47. fprintf(stderr, "reading from an unlinked variable "
  48. "shouldn't be possible\n");
  49. return EXIT_FAILURE;
  50. }
  51. return EXIT_SUCCESS;
  52. }