Whamcloud - gitweb
LU-116 fix mmp test 9 and test 10
[fs/lustre-release.git] / lustre / tests / createtest.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <string.h>
45
46 #ifndef S_SHIFT
47 #define S_SHIFT 12
48 #endif
49
50 int usage(char *prog)
51 {
52         fprintf(stderr, "usage: %s <basename>\n", prog);
53         exit(1);
54 }
55
56 int main(int argc, char *argv[])
57 {
58         char name[4096];
59         int i;
60
61         if (argc != 2)
62                 usage(argv[0]);
63
64         umask(0);
65         for (i = 0; i <= S_IFMT; i += (1 << S_SHIFT)) {
66                 struct stat st;
67                 int mode = i | 0644;
68                 int rc;
69
70                 sprintf(name, "%s-mknod%07o", argv[1], mode);
71                 rc = mknod(name, mode, 0x1234);
72                 switch (i) {
73                 case 0:
74                         mode |= S_IFREG;
75                 case S_IFREG:
76                 case S_IFCHR: case S_IFBLK:
77                         if (rc < 0 && getuid() != 0)
78                                 continue;
79                 case S_IFSOCK: case S_IFIFO:
80                         if (rc < 0) {
81                                 fprintf(stderr, "%s: ERROR mknod %s: %s\n",
82                                         argv[0], name, strerror(errno));
83                                 exit(10);
84                         }
85                         rc = stat(name, &st);
86                         if (rc < 0) {
87                                 fprintf(stderr, "%s: ERROR stat %s: %s",
88                                         argv[0], name, strerror(errno));
89                                 exit(11);
90                         }
91                         if (st.st_mode != mode) {
92                                 fprintf(stderr, "%s: ERROR mode %s: %o != %o",
93                                         argv[0], name, st.st_mode, mode);
94                                 exit(12);
95                         }
96                         if (i == S_IFCHR || i == S_IFBLK) {
97                                 if (st.st_rdev != 0x1234) {
98                                         fprintf(stderr, "%s: ERROR rdev %s: "
99                                                 "%llu != 0x1234",
100                                                 argv[0], name,
101                                                 (unsigned long long)st.st_rdev);
102                                         exit(13);
103                                 }
104                         }
105                         rc = unlink(name);
106                         if (rc < 0) {
107                                 fprintf(stderr, "%s: ERROR unlink %s: %s",
108                                         argv[0], name, strerror(errno));
109                                 exit(14);
110                         }
111                         break;
112                 default:
113                         if (rc == 0) {
114                                 fprintf(stderr, "%s: ERROR: %s created\n",
115                                         argv[0], name);
116                                 exit(15);
117                         }
118                 }
119         }
120
121         for (i = 0; i <= S_IFMT; i += (1 << S_SHIFT)) {
122                 struct stat st;
123                 int mode;
124                 int fd;
125                 int rc;
126
127                 mode = i | 0644;
128                 sprintf(name, "%s-creat%07o", argv[1], mode);
129                 fd = open(name, O_CREAT|O_RDONLY, mode);
130                 if (fd < 0) {
131                         fprintf(stderr, "%s: ERROR creat %s: %s\n",
132                                 argv[0], name, strerror(errno));
133                         exit(21);
134                 }
135                 close(fd);
136                 rc = stat(name, &st);
137                 if (rc < 0) {
138                         fprintf(stderr, "%s: ERROR stat %s: %s",
139                                 argv[0], name, strerror(errno));
140                         exit(11);
141                 }
142                 if (!S_ISREG(st.st_mode & S_IFMT)) {
143                         fprintf(stderr, "%s: ERROR mode %s: %o != %o",
144                                 argv[0], name, st.st_mode & S_IFMT, S_IFREG);
145                         exit(12);
146                 }
147                 rc = unlink(name);
148                 if (rc < 0) {
149                         fprintf(stderr, "%s: ERROR unlink %s: %s\n",
150                                 argv[0], name, strerror(errno));
151                         exit(20);
152                 }
153         }
154
155         for (i = 0; i <= S_IFMT; i += (1 << S_SHIFT)) {
156                 struct stat st;
157                 int rc;
158
159                 sprintf(name, "%s-mkdir%06o", argv[1], i | 0644);
160                 rc = mkdir(name, i | 0664);
161                 if (rc < 0) {
162                         fprintf(stderr, "%s: ERROR mkdir %s: %s\n",
163                                 argv[0], name, strerror(errno));
164                         exit(30);
165                 }
166                 rc = stat(name, &st);
167                 if (rc < 0) {
168                         fprintf(stderr, "%s: ERROR stat %s: %s",
169                                 argv[0], name, strerror(errno));
170                         exit(11);
171                 }
172                 if (!S_ISDIR(st.st_mode)) {
173                         fprintf(stderr, "%s: ERROR mode %s: %o != %o",
174                                 argv[0], name, st.st_mode & S_IFMT, S_IFDIR);
175                         exit(12);
176                 }
177                 rc = rmdir(name);
178                 if (rc < 0) {
179                         fprintf(stderr, "%s: ERROR rmdir %s: %s\n",
180                                 argv[0], name, strerror(errno));
181                         exit(31);
182                 }
183         }
184
185         printf("%s: SUCCESS\n", argv[0]);
186         return 0;
187 }