/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* rmtest.c31*32* Tests file system synchronization by deleting an open file and33* then attempting to read it.34*35* This should run correctly when the file system assignment is complete.36*/3738#include <stdio.h>39#include <stdlib.h>40#include <string.h>41#include <unistd.h>42#include <errno.h>43#include <err.h>4445#define TEST "rmdata"46#define TESTDATA "I wish I was a headlight. -- Jerry Garcia"47#define TESTLEN (sizeof(TESTDATA)-1)4849static50void51dorm(int fd)52{53/*54* This used to spawn a copy of /bin/rm, but that's silly.55* However, we will do the remove() from a subprocess, so56* that various kinds of improper hacks to make this test57* run won't work.58*59* Close the file in the subprocess, for similar reasons.60*/6162pid_t pid;63int status;6465pid = fork();66if (pid<0) {67err(1, "fork");68}69if (pid==0) {70/* child process */71close(fd);72if (remove(TEST)) {73err(1, "%s: remove", TEST);74}75_exit(0);76}77/* parent process */78if (waitpid(pid, &status, 0)<0) {79err(1, "waitpid");80}81else if (WIFSIGNALED(status)) {82warn("child process exited with signal %d", WTERMSIG(status));83}84else if (WEXITSTATUS(status) != 0) {85warnx("child process exited with code %d",WEXITSTATUS(status));86}87}8889static90int91same(const char *a, const char *b, int len)92{93while (len-- > 0) {94if (*a++ != *b++) return 0;95}96return 1;97}9899int100main()101{102int file, len;103char buf[TESTLEN];104105/* create test data file */106file = open(TEST, O_WRONLY | O_CREAT | O_TRUNC, 0664);107write(file, TESTDATA, TESTLEN);108close(file);109110/* make sure the data is there */111file = open(TEST, O_RDONLY);112len = read(file, buf, TESTLEN);113if (len < 0) {114warn("read: before deletion");115}116else if (len < (int)TESTLEN) {117warnx("read: before deletion: short count %d", len);118}119if (!same(buf, TESTDATA, TESTLEN)) {120errx(1, "Failed: data read back was not the same");121}122123/* rewind the file */124if (lseek(file, 0, SEEK_SET)) {125err(1, "lseek");126}127128/* now spawn our killer and wait for it to do its work */129dorm(file);130131/* we should be still able to read the data */132memset(buf, '\0', TESTLEN);133len = read(file, buf, TESTLEN);134if (len < 0) {135warn("read: after deletion");136}137else if (len < (int)TESTLEN) {138warnx("read: after deletion: short count %d", len);139}140141if (!same(buf, TESTDATA, TESTLEN)) {142errx(1, "Failed: data read after deletion was not the same");143}144145/* ok, close the file and it should go away */146close(file);147148/* try to open it again */149file = open(TEST, O_RDONLY);150if (file >= 0) {151close(file);152errx(1, "Failed: the file could still be opened");153}154155if (errno!=ENOENT) {156err(1, "Unexpected error reopening the file");157}158159printf("Succeeded!\n");160161return 0;162}163164165