Whamcloud - gitweb
Some racy problems happened when sanity-quota.sh run on buffalo.
[fs/lustre-release.git] / lustre / tests / iopentest1.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <limits.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <libgen.h>
14 #include <errno.h>
15
16 const char *progname;
17 const char usage_fmt[] = "Usage: %s <file> <mountpoint>\n";
18 #define INAME_LEN (PATH_MAX + 1)
19
20 #define CHECK_IT(exp, pstr) \
21 if (!(exp)) { \
22     fprintf(stderr, "%s: at %s:%d: ", progname, __FILE__, __LINE__); \
23     perror((pstr)); \
24     exit(1); \
25 }
26
27 #define CHECK_SNPRINTF(rc, len) \
28     CHECK_IT((rc) > 0 && (rc) <= (len), "snprintf() failed")
29
30 static char *get_iname(char *fname, const char *mtpt)
31 {
32         char *iname;
33         int fd, rc;
34         struct stat buf;
35
36         iname = malloc(INAME_LEN);
37         CHECK_IT(iname, "malloc() failed");
38
39         fd = open(fname, O_CREAT, 0644);
40         if (fd < 0 && errno != EISDIR) {
41                 fprintf(stderr, "%s:%d: open(%s) failed: %s\n", __FILE__,
42                         __LINE__, fname, strerror(errno));
43                 exit(1);
44         }
45
46         if (fd >= 0)
47                 close(fd);
48
49         rc = stat(fname, &buf);
50         if (rc != 0) {
51                 fprintf(stderr, "%s:%d: stat(%s) failed: %s\n", __FILE__,
52                         __LINE__, fname, strerror(errno));
53                 exit(1);
54         }
55
56         rc = snprintf(iname, INAME_LEN,
57                       "%s/__iopen__/%lu", mtpt, (unsigned long)buf.st_ino);
58         CHECK_SNPRINTF(rc, INAME_LEN);
59
60         return iname;
61 }
62
63 int main(int argc, char *argv[])
64 {
65         char *fname, *mtpt, *iname, *pname;
66         struct stat buf;
67         int rc;
68         int i;
69
70         pname = strdup(argv[0]);
71         progname = basename(pname);
72         
73         if (argc != 3) {
74                 fprintf(stderr, usage_fmt, progname);
75                 return 1;
76         }
77
78         fname = argv[1];
79         mtpt  = argv[2];
80
81         iname = get_iname(fname, mtpt);
82         printf("%s:started...\n", argv[0]);
83         for (i = 0; i < 10000; i++) {
84                 rc = stat(fname, &buf);
85                 if (rc != 0) {
86                         fprintf(stderr, "%s:%d: stat(%s) failed: %s\n",
87                                 __FILE__, __LINE__, fname, strerror(errno));
88                         exit(1);
89                 }
90
91                 rc = stat(iname, &buf);
92                 if (rc != 0) {
93                         fprintf(stderr, "%s:%d: stat(%s) failed: %s\n",
94                                 __FILE__, __LINE__, iname, strerror(errno));
95                         exit(1);
96                 }
97         }
98         printf("%s:finished...\n", argv[0]);
99
100         return 0;
101 }