os161-1.99
 All Data Structures
rmtest.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * rmtest.c
00032  *
00033  *      Tests file system synchronization by deleting an open file and
00034  *      then attempting to read it.
00035  *
00036  * This should run correctly when the file system assignment is complete.
00037  */
00038 
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <string.h>
00042 #include <unistd.h>
00043 #include <errno.h>
00044 #include <err.h>
00045 
00046 #define TEST        "rmdata"
00047 #define TESTDATA    "I wish I was a headlight. -- Jerry Garcia"
00048 #define TESTLEN     (sizeof(TESTDATA)-1)
00049 
00050 static
00051 void
00052 dorm(int fd)
00053 {
00054         /*
00055          * This used to spawn a copy of /bin/rm, but that's silly.
00056          * However, we will do the remove() from a subprocess, so
00057          * that various kinds of improper hacks to make this test
00058          * run won't work.
00059          *
00060          * Close the file in the subprocess, for similar reasons.
00061          */
00062 
00063         pid_t pid;
00064         int status;
00065 
00066         pid = fork();
00067         if (pid<0) {
00068                 err(1, "fork");
00069         }
00070         if (pid==0) {
00071                 /* child process */
00072                 close(fd);
00073                 if (remove(TEST)) {
00074                         err(1, "%s: remove", TEST);
00075                 }
00076                 _exit(0);
00077         }
00078         /* parent process */
00079         if (waitpid(pid, &status, 0)<0) {
00080                 err(1, "waitpid");
00081         }
00082         else if (WIFSIGNALED(status)) {
00083                 warn("child process exited with signal %d", WTERMSIG(status));
00084         }
00085         else if (WEXITSTATUS(status) != 0) {
00086                 warnx("child process exited with code %d",WEXITSTATUS(status));
00087         }
00088 }
00089 
00090 static
00091 int 
00092 same(const char *a, const char *b, int len)
00093 {
00094         while (len-- > 0) {
00095                 if (*a++ != *b++) return 0;
00096         }
00097         return 1;
00098 }
00099 
00100 int
00101 main()
00102 {
00103         int file, len;
00104         char buf[TESTLEN];
00105 
00106         /* create test data file */
00107         file = open(TEST, O_WRONLY | O_CREAT | O_TRUNC, 0664);
00108         write(file, TESTDATA, TESTLEN);
00109         close(file);
00110 
00111         /* make sure the data is there */
00112         file = open(TEST, O_RDONLY);
00113         len = read(file, buf, TESTLEN);
00114         if (len < 0) {
00115                 warn("read: before deletion");
00116         }
00117         else if (len < (int)TESTLEN) {
00118                 warnx("read: before deletion: short count %d", len);
00119         }
00120         if (!same(buf, TESTDATA, TESTLEN)) {
00121                 errx(1, "Failed: data read back was not the same");
00122         }
00123 
00124         /* rewind the file */
00125         if (lseek(file, 0, SEEK_SET)) {
00126                 err(1, "lseek");
00127         }
00128 
00129         /* now spawn our killer and wait for it to do its work */
00130         dorm(file);
00131 
00132         /* we should be still able to read the data */
00133         memset(buf, '\0', TESTLEN);
00134         len = read(file, buf, TESTLEN);
00135         if (len < 0) {
00136                 warn("read: after deletion");
00137         }
00138         else if (len < (int)TESTLEN) {
00139                 warnx("read: after deletion: short count %d", len);
00140         }
00141 
00142         if (!same(buf, TESTDATA, TESTLEN)) {
00143                 errx(1, "Failed: data read after deletion was not the same");
00144         }
00145 
00146         /* ok, close the file and it should go away */
00147         close(file);
00148 
00149         /* try to open it again */
00150         file = open(TEST, O_RDONLY);
00151         if (file >= 0) {
00152                 close(file);
00153                 errx(1, "Failed: the file could still be opened");
00154         }
00155 
00156         if (errno!=ENOENT) {
00157                 err(1, "Unexpected error reopening the file");
00158         }
00159 
00160         printf("Succeeded!\n");
00161 
00162         return 0;
00163 }
 All Data Structures