Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[fs/lustre-release.git] / lustre / tests / multiop.c
1 #define _GNU_SOURCE /* pull in O_DIRECTORY in bits/fcntl.h */
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/mman.h>
9 #include <signal.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12
13 #define T1 "write data before unlink\n"
14 #define T2 "write data after unlink\n"
15 char buf[128];
16
17 char usage[] = 
18 "Usage: %s filename command-sequence\n"
19 "    command-sequence items:\n"
20 "        d  mkdir\n"
21 "        D  open(O_DIRECTORY)\n"
22 "        o  open(O_RDONLY)\n"
23 "        O  open(O_CREAT|O_RDWR)\n"
24 "        L  link\n"
25 "        l  symlink\n"
26 "        u  unlink\n"
27 "        U  munmap\n"
28 "        m  mknod\n"
29 "        M  rw mmap to EOF (must open and stat prior)\n"
30 "        N  rename\n"
31 "        c  close\n"
32 "        _  wait for signal\n"
33 "        R  reference entire mmap-ed region\n"
34 "        r  read\n"
35 "        s  stat\n"
36 "        S  fstat\n"
37 "        t  fchmod\n"
38 "        T  ftruncate to zero\n"
39 "        w  write\n"
40 "        W  write entire mmap-ed region\n"
41 "        y  fsync\n"
42 "        Y  fdatasync\n"
43 "        z  seek to zero\n";
44
45 void null_handler(int unused) { }
46
47 static const char *
48 pop_arg(int argc, char *argv[])
49 {
50         static int cur_arg = 3;
51
52         if (cur_arg >= argc)
53                 return NULL;
54
55         return argv[cur_arg++];
56 }
57 #define POP_ARG() (pop_arg(argc, argv))
58
59 int main(int argc, char **argv)
60 {
61         char *fname, *commands;
62         const char *newfile;
63         struct stat st;
64         size_t mmap_len, i;
65         unsigned char *mmap_ptr = NULL, junk = 0;
66         int fd = -1;
67
68         if (argc < 3) {
69                 fprintf(stderr, usage, argv[0]);
70                 exit(1);
71         }
72
73         signal(SIGUSR1, null_handler);
74
75         fname = argv[1];
76
77         for (commands = argv[2]; *commands; commands++) {
78                 switch (*commands) {
79                 case '_':
80                         pause();
81                         break;
82                 case 'c':
83                         if (close(fd) == -1) {
84                                 perror("close");
85                                 exit(1);
86                         }
87                         fd = -1;
88                         break;
89                 case 'd':
90                         if (mkdir(fname, 0755) == -1) {
91                                 perror("mkdir(0755)");
92                                 exit(1);
93                         }
94                         break;
95                 case 'D':
96                         if (open(fname, O_DIRECTORY) == -1) {
97                                 perror("open(O_DIRECTORY)");
98                                 exit(1);
99                         }
100                         break;
101                 case 'l':
102                         newfile = POP_ARG();
103                         if (!newfile)
104                                 newfile = fname;
105                         if (symlink(fname, newfile)) {
106                                 perror("symlink()");
107                                 exit(1);
108                         }
109                         break;
110                 case 'L':
111                         newfile = POP_ARG();
112                         if (!newfile)
113                                 newfile = fname;
114                         if (link(fname, newfile)) {
115                                 perror("symlink()");
116                                 exit(1);
117                         }
118                         break;
119                 case 'm':
120                         if (mknod(fname, S_IFREG | 0644, 0) == -1) {
121                                 perror("mknod(S_IFREG|0644, 0)");
122                                 exit(1);
123                         }
124                         break;
125                 case 'M':
126                         mmap_len = st.st_size;
127                         mmap_ptr = mmap(NULL, mmap_len, PROT_WRITE | PROT_READ,
128                                         MAP_SHARED, fd, 0);
129                         if (mmap_ptr == MAP_FAILED) {
130                                 perror("mmap");
131                                 exit(1);
132                         }
133                         break;
134                 case 'N':
135                         newfile = POP_ARG();
136                         if (!newfile)
137                                 newfile = fname;
138                         if (rename (fname, newfile)) {
139                                 perror("rename()");
140                                 exit(1);
141                         }
142                         break;
143                 case 'O':
144                         fd = open(fname, O_CREAT|O_RDWR, 0644);
145                         if (fd == -1) {
146                                 perror("open(O_RDWR|O_CREAT)");
147                                 exit(1);
148                         }
149                         break;
150                 case 'o':
151                         fd = open(fname, O_RDONLY);
152                         if (fd == -1) {
153                                 perror("open(O_RDONLY)");
154                                 exit(1);
155                         }
156                         break;
157                 case 'r': {
158                         char buf;
159                         if (read(fd, &buf, 1) == -1) {
160                                 perror("read");
161                                 exit(1);
162                         }
163                 }
164                 case 'S':
165                         if (fstat(fd, &st) == -1) {
166                                 perror("fstat");
167                                 exit(1);
168                         }
169                         break;
170                 case 'R':
171                         for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
172                                 junk += mmap_ptr[i];
173                         break;
174                 case 's':
175                         if (stat(fname, &st) == -1) {
176                                 perror("stat");
177                                 exit(1);
178                         }
179                         break;
180                 case 't':
181                         if (fchmod(fd, 0) == -1) {
182                                 perror("fchmod");
183                                 exit(1);
184                         }
185                         break;
186                 case 'T':
187                         if (ftruncate(fd, 0) == -1) {
188                                 perror("ftruncate");
189                                 exit(1);
190                         }
191                         break;
192                 case 'u':
193                         if (unlink(fname) == -1) {
194                                 perror("unlink");
195                                 exit(1);
196                         }
197                         break;
198                 case 'U':
199                         if (munmap(mmap_ptr, mmap_len)) {
200                                 perror("munmap");
201                                 exit(1);
202                         }
203                         break;
204                 case 'w':
205                         if (write(fd, "w", 1) == -1) {
206                                 perror("write");
207                                 exit(1);
208                         }
209                         break;
210                 case 'W':
211                         for (i = 0; i < mmap_len && mmap_ptr; i += 4096)
212                                 mmap_ptr[i] += junk++;
213                         break;
214                 case 'y':
215                         if (fsync(fd) == -1) {
216                                 perror("fsync");
217                                 exit(1);
218                         }
219                         break;
220                 case 'Y':
221                         if (fdatasync(fd) == -1) {
222                                 perror("fdatasync");
223                                 exit(1);
224                         }
225                 case 'z':
226                         if (lseek(fd, 0, SEEK_SET) == -1) {
227                                 perror("lseek");
228                                 exit(1);
229                         }
230                         break;
231                 default:
232                         fprintf(stderr, "unknown command \"%c\"\n", *commands);
233                         fprintf(stderr, usage, argv[0]);
234                         exit(1);
235                 }
236         }
237
238         return 0;
239 }