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"
18 char buf[] = "yabba dabba doo, I'm coming for you, I live in a shoe, I don't know what to do.\n'Bigger, bigger,and bigger yet!' cried the Creator. 'You are not yet substantial enough for my boundless intents!' And ever greater and greater the object became, until all was lost 'neath its momentus bulk.\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"
37 " r[num] read [optional length]\n"
41 " T[num] ftruncate [optional position, default 0]\n"
42 " w[num] write optional length\n"
43 " W write entire mmap-ed region\n"
46 " z[num] seek [optional position, default 0]\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))
65 #define min(a,b) ((a)>(b)?(b):(a))
67 int main(int argc, char **argv)
69 char *fname, *commands;
72 size_t mmap_len = 0, i;
73 unsigned char *mmap_ptr = NULL, junk = 0;
77 fprintf(stderr, usage, argv[0]);
81 signal(SIGUSR1, usr1_handler);
85 for (commands = argv[2]; *commands; commands++) {
88 if (usr1_received == 0)
91 signal(SIGUSR1, usr1_handler);
94 if (close(fd) == -1) {
101 if (mkdir(fname, 0755) == -1) {
102 perror("mkdir(0755)");
107 fd = open(fname, O_DIRECTORY);
109 perror("open(O_DIRECTORY)");
117 if (symlink(fname, newfile)) {
126 if (link(fname, newfile)) {
132 if (mknod(fname, S_IFREG | 0644, 0) == -1) {
133 perror("mknod(S_IFREG|0644, 0)");
138 mmap_len = st.st_size;
139 mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
141 if (mmap_ptr == MAP_FAILED) {
150 if (rename (fname, newfile)) {
156 fd = open(fname, O_CREAT|O_RDWR, 0644);
158 perror("open(O_RDWR|O_CREAT)");
163 fd = open(fname, O_RDONLY);
165 perror("open(O_RDONLY)");
170 len = atoi(commands+1);
175 min(len,sizeof(buf))) == -1) {
183 if (fstat(fd, &st) == -1) {
189 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
193 if (stat(fname, &st) == -1) {
199 if (fchmod(fd, 0) == -1) {
205 len = atoi(commands+1);
206 if (ftruncate(fd, len) == -1) {
207 printf("ftruncate (%d,%d)\n", fd, len);
213 if (unlink(fname) == -1) {
219 if (munmap(mmap_ptr, mmap_len)) {
225 len = atoi(commands+1);
229 if ((rc = write(fd, buf,
230 min(len, sizeof(buf))))
239 for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
240 mmap_ptr[i] += junk++;
243 if (fsync(fd) == -1) {
249 if (fdatasync(fd) == -1) {
254 len = atoi(commands+1);
255 if (lseek(fd, len, SEEK_SET) == -1) {
272 fprintf(stderr, "unknown command \"%c\"\n", *commands);
273 fprintf(stderr, usage, argv[0]);