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 static int usr1_received;
49 void usr1_handler(int unused)
55 pop_arg(int argc, char *argv[])
57 static int cur_arg = 3;
62 return argv[cur_arg++];
64 #define POP_ARG() (pop_arg(argc, argv))
66 int main(int argc, char **argv)
68 char *fname, *commands;
71 size_t mmap_len = 0, i;
72 unsigned char *mmap_ptr = NULL, junk = 0;
76 fprintf(stderr, usage, argv[0]);
80 signal(SIGUSR1, usr1_handler);
84 for (commands = argv[2]; *commands; commands++) {
87 if (usr1_received == 0)
90 signal(SIGUSR1, usr1_handler);
93 if (close(fd) == -1) {
100 if (mkdir(fname, 0755) == -1) {
101 perror("mkdir(0755)");
106 fd = open(fname, O_DIRECTORY);
108 perror("open(O_DIRECTORY)");
116 if (symlink(fname, newfile)) {
125 if (link(fname, newfile)) {
131 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
132 perror("mknod(S_IFREG|0644, 0)");
137 mmap_len = st.st_size;
138 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
140 if (mmap_ptr == MAP_FAILED) {
149 if (rename (fname, newfile)) {
155 fd = open(fname, O_CREAT|O_RDWR, 0644);
157 perror("open(O_RDWR|O_CREAT)");
162 fd = open(fname, O_RDONLY);
164 perror("open(O_RDONLY)");
170 if (read(fd, &buf, 1) == -1) {
176 if (fstat(fd, &st) == -1) {
182 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
186 if (stat(fname, &st) == -1) {
192 if (fchmod(fd, 0) == -1) {
198 if (ftruncate(fd, 0) == -1) {
204 if (unlink(fname) == -1) {
210 if (munmap(mmap_ptr, mmap_len)) {
217 if ((rc = write(fd, "w", 1)) == -1) {
224 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
225 mmap_ptr[i] += junk++;
228 if (fsync(fd) == -1) {
234 if (fdatasync(fd) == -1) {
239 if (lseek(fd, 0, SEEK_SET) == -1) {
245 fprintf(stderr, "unknown command \"%c\"\n", *commands);
246 fprintf(stderr, usage, argv[0]);