/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- dorm
- same
- main
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 /*
31 * rmtest.c
32 *
33 * Tests file system synchronization by deleting an open file and
34 * then attempting to read it.
35 *
36 * This should run correctly when the file system assignment is complete.
37 */
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <err.h>
45
46 #define TEST "rmdata"
47 #define TESTDATA "I wish I was a headlight. -- Jerry Garcia"
48 #define TESTLEN (sizeof(TESTDATA)-1)
49
50 static
51 void
52 dorm(int fd)
53 {
54 /*
55 * This used to spawn a copy of /bin/rm, but that's silly.
56 * However, we will do the remove() from a subprocess, so
57 * that various kinds of improper hacks to make this test
58 * run won't work.
59 *
60 * Close the file in the subprocess, for similar reasons.
61 */
62
63 pid_t pid;
64 int status;
65
66 pid = fork();
67 if (pid<0) {
68 err(1, "fork");
69 }
70 if (pid==0) {
71 /* child process */
72 close(fd);
73 if (remove(TEST)) {
74 err(1, "%s: remove", TEST);
75 }
76 _exit(0);
77 }
78 /* parent process */
79 if (waitpid(pid, &status, 0)<0) {
80 err(1, "waitpid");
81 }
82 else if (WIFSIGNALED(status)) {
83 warn("child process exited with signal %d", WTERMSIG(status));
84 }
85 else if (WEXITSTATUS(status) != 0) {
86 warnx("child process exited with code %d",WEXITSTATUS(status));
87 }
88 }
89
90 static
91 int
92 same(const char *a, const char *b, int len)
93 {
94 while (len-- > 0) {
95 if (*a++ != *b++) return 0;
96 }
97 return 1;
98 }
99
100 int
101 main()
102 {
103 int file, len;
104 char buf[TESTLEN];
105
106 /* create test data file */
107 file = open(TEST, O_WRONLY | O_CREAT | O_TRUNC, 0664);
108 write(file, TESTDATA, TESTLEN);
109 close(file);
110
111 /* make sure the data is there */
112 file = open(TEST, O_RDONLY);
113 len = read(file, buf, TESTLEN);
114 if (len < 0) {
115 warn("read: before deletion");
116 }
117 else if (len < (int)TESTLEN) {
118 warnx("read: before deletion: short count %d", len);
119 }
120 if (!same(buf, TESTDATA, TESTLEN)) {
121 errx(1, "Failed: data read back was not the same");
122 }
123
124 /* rewind the file */
125 if (lseek(file, 0, SEEK_SET)) {
126 err(1, "lseek");
127 }
128
129 /* now spawn our killer and wait for it to do its work */
130 dorm(file);
131
132 /* we should be still able to read the data */
133 memset(buf, '\0', TESTLEN);
134 len = read(file, buf, TESTLEN);
135 if (len < 0) {
136 warn("read: after deletion");
137 }
138 else if (len < (int)TESTLEN) {
139 warnx("read: after deletion: short count %d", len);
140 }
141
142 if (!same(buf, TESTDATA, TESTLEN)) {
143 errx(1, "Failed: data read after deletion was not the same");
144 }
145
146 /* ok, close the file and it should go away */
147 close(file);
148
149 /* try to open it again */
150 file = open(TEST, O_RDONLY);
151 if (file >= 0) {
152 close(file);
153 errx(1, "Failed: the file could still be opened");
154 }
155
156 if (errno!=ENOENT) {
157 err(1, "Unexpected error reopening the file");
158 }
159
160 printf("Succeeded!\n");
161
162 return 0;
163 }