1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 #define _GNU_SOURCE /* pull in O_DIRECTORY in bits/fcntl.h */
16 #define T1 "write data before unlink\n"
17 #define T2 "write data after unlink\n"
21 "Usage: %s filename command-sequence\n"
22 " command-sequence items:\n"
24 " D open(O_DIRECTORY)\n"
26 " O open(O_CREAT|O_RDWR)\n"
32 " M rw mmap to EOF (must open and stat prior)\n"
35 " _ wait for signal\n"
36 " R reference entire mmap-ed region\n"
41 " T ftruncate to zero\n"
43 " W write entire mmap-ed region\n"
48 void null_handler(int unused) { }
51 pop_arg(int argc, char *argv[])
53 static int cur_arg = 3;
58 return argv[cur_arg++];
60 #define POP_ARG() (pop_arg(argc, argv))
62 int main(int argc, char **argv)
64 char *fname, *commands;
67 size_t mmap_len = 0, i;
68 unsigned char *mmap_ptr = NULL, junk = 0;
72 fprintf(stderr, usage, argv[0]);
76 signal(SIGUSR1, null_handler);
80 for (commands = argv[2]; *commands; commands++) {
86 if (close(fd) == -1) {
93 if (mkdir(fname, 0755) == -1) {
94 perror("mkdir(0755)");
99 if (open(fname, O_DIRECTORY) == -1) {
100 perror("open(O_DIRECTORY)");
108 if (symlink(fname, newfile)) {
117 if (link(fname, newfile)) {
123 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
124 perror("mknod(S_IFREG|0644, 0)");
129 mmap_len = st.st_size;
130 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
132 if (mmap_ptr == MAP_FAILED) {
141 if (rename (fname, newfile)) {
147 fd = open(fname, O_CREAT|O_RDWR, 0644);
149 perror("open(O_RDWR|O_CREAT)");
154 fd = open(fname, O_RDONLY);
156 perror("open(O_RDONLY)");
162 if (read(fd, &buf, 1) == -1) {
168 if (fstat(fd, &st) == -1) {
174 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
178 if (stat(fname, &st) == -1) {
184 if (fchmod(fd, 0) == -1) {
190 if (ftruncate(fd, 0) == -1) {
196 if (unlink(fname) == -1) {
202 if (munmap(mmap_ptr, mmap_len)) {
209 if ((rc = write(fd, "w", 1)) == -1) {
216 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
217 mmap_ptr[i] += junk++;
220 if (fsync(fd) == -1) {
226 if (fdatasync(fd) == -1) {
231 if (lseek(fd, 0, SEEK_SET) == -1) {
237 fprintf(stderr, "unknown command \"%c\"\n", *commands);
238 fprintf(stderr, usage, argv[0]);