rmtest.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * rmtest.c
  31. *
  32. * Tests file system synchronization by deleting an open file and
  33. * then attempting to read it.
  34. *
  35. * This should run correctly when the file system assignment is complete.
  36. */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include <errno.h>
  42. #include <err.h>
  43. #define TEST "rmdata"
  44. #define TESTDATA "I wish I was a headlight. -- Jerry Garcia"
  45. #define TESTLEN (sizeof(TESTDATA)-1)
  46. static
  47. void
  48. dorm(int fd)
  49. {
  50. /*
  51. * This used to spawn a copy of /bin/rm, but that's silly.
  52. * However, we will do the remove() from a subprocess, so
  53. * that various kinds of improper hacks to make this test
  54. * run won't work.
  55. *
  56. * Close the file in the subprocess, for similar reasons.
  57. */
  58. pid_t pid;
  59. int status;
  60. pid = fork();
  61. if (pid<0) {
  62. err(1, "fork");
  63. }
  64. if (pid==0) {
  65. /* child process */
  66. close(fd);
  67. if (remove(TEST)) {
  68. err(1, "%s: remove", TEST);
  69. }
  70. _exit(0);
  71. }
  72. /* parent process */
  73. if (waitpid(pid, &status, 0)<0) {
  74. err(1, "waitpid");
  75. }
  76. else if (WIFSIGNALED(status)) {
  77. warn("child process exited with signal %d", WTERMSIG(status));
  78. }
  79. else if (WEXITSTATUS(status) != 0) {
  80. warnx("child process exited with code %d",WEXITSTATUS(status));
  81. }
  82. }
  83. static
  84. int
  85. same(const char *a, const char *b, int len)
  86. {
  87. while (len-- > 0) {
  88. if (*a++ != *b++) return 0;
  89. }
  90. return 1;
  91. }
  92. int
  93. main()
  94. {
  95. int file, len;
  96. char buf[TESTLEN];
  97. /* create test data file */
  98. file = open(TEST, O_WRONLY | O_CREAT | O_TRUNC, 0664);
  99. write(file, TESTDATA, TESTLEN);
  100. close(file);
  101. /* make sure the data is there */
  102. file = open(TEST, O_RDONLY);
  103. len = read(file, buf, TESTLEN);
  104. if (len < 0) {
  105. warn("read: before deletion");
  106. }
  107. else if (len < (int)TESTLEN) {
  108. warnx("read: before deletion: short count %d", len);
  109. }
  110. if (!same(buf, TESTDATA, TESTLEN)) {
  111. errx(1, "Failed: data read back was not the same");
  112. }
  113. /* rewind the file */
  114. if (lseek(file, 0, SEEK_SET)) {
  115. err(1, "lseek");
  116. }
  117. /* now spawn our killer and wait for it to do its work */
  118. dorm(file);
  119. /* we should be still able to read the data */
  120. memset(buf, '\0', TESTLEN);
  121. len = read(file, buf, TESTLEN);
  122. if (len < 0) {
  123. warn("read: after deletion");
  124. }
  125. else if (len < (int)TESTLEN) {
  126. warnx("read: after deletion: short count %d", len);
  127. }
  128. if (!same(buf, TESTDATA, TESTLEN)) {
  129. errx(1, "Failed: data read after deletion was not the same");
  130. }
  131. /* ok, close the file and it should go away */
  132. close(file);
  133. /* try to open it again */
  134. file = open(TEST, O_RDONLY);
  135. if (file >= 0) {
  136. close(file);
  137. errx(1, "Failed: the file could still be opened");
  138. }
  139. if (errno!=ENOENT) {
  140. err(1, "Unexpected error reopening the file");
  141. }
  142. printf("Succeeded!\n");
  143. return 0;
  144. }