1 #define _GNU_SOURCE /* pull in O_DIRECTORY in bits/fcntl.h */
13 #define T1 "write data before unlink\n"
14 #define T2 "write data after unlink\n"
18 "Usage: %s filename command-sequence\n"
19 " command-sequence items:\n"
21 " D open(O_DIRECTORY)\n"
23 " O open(O_CREAT|O_RDWR)\n"
29 " M rw mmap to EOF (must open and stat prior)\n"
32 " _ wait for signal\n"
33 " R reference entire mmap-ed region\n"
38 " T ftruncate to zero\n"
40 " W write entire mmap-ed region\n"
45 void null_handler(int unused) { }
48 pop_arg(int argc, char *argv[])
50 static int cur_arg = 3;
55 return argv[cur_arg++];
57 #define POP_ARG() (pop_arg(argc, argv))
59 int main(int argc, char **argv)
61 char *fname, *commands;
65 unsigned char *mmap_ptr = NULL, junk = 0;
69 fprintf(stderr, usage, argv[0]);
73 signal(SIGUSR1, null_handler);
77 for (commands = argv[2]; *commands; commands++) {
83 if (close(fd) == -1) {
90 if (mkdir(fname, 0755) == -1) {
91 perror("mkdir(0755)");
96 if (open(fname, O_DIRECTORY) == -1) {
97 perror("open(O_DIRECTORY)");
105 if (symlink(fname, newfile)) {
114 if (link(fname, newfile)) {
120 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
121 perror("mknod(S_IFREG|0644, 0)");
126 mmap_len = st.st_size;
127 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
129 if (mmap_ptr == MAP_FAILED) {
138 if (rename (fname, newfile)) {
144 fd = open(fname, O_CREAT|O_RDWR, 0644);
146 perror("open(O_RDWR|O_CREAT)");
151 fd = open(fname, O_RDONLY);
153 perror("open(O_RDONLY)");
159 if (read(fd, &buf, 1) == -1) {
165 if (fstat(fd, &st) == -1) {
171 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
175 if (stat(fname, &st) == -1) {
181 if (fchmod(fd, 0) == -1) {
187 if (ftruncate(fd, 0) == -1) {
193 if (unlink(fname) == -1) {
199 if (munmap(mmap_ptr, mmap_len)) {
205 if (write(fd, "w", 1) == -1) {
211 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
212 mmap_ptr[i] += junk++;
215 if (fsync(fd) == -1) {
221 if (fdatasync(fd) == -1) {
226 if (lseek(fd, 0, SEEK_SET) == -1) {
232 fprintf(stderr, "unknown command \"%c\"\n", *commands);
233 fprintf(stderr, usage, argv[0]);