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