Print this page
15291 zfs-tests errno flaws exposed by 15220
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/test/zfs-tests/cmd/mktree/mktree.c
+++ new/usr/src/test/zfs-tests/cmd/mktree/mktree.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
|
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 +/*
28 + * Copyright 2022 MNX Cloud, Inc.
29 + */
30 +
27 31 #include <stdio.h>
28 32 #include <stdlib.h>
29 33 #include <unistd.h>
30 34 #include <string.h>
31 35 #include <fcntl.h>
32 36 #include <sys/stat.h>
33 37 #include <sys/types.h>
34 38 #include <sys/errno.h>
35 39 #include <sys/param.h>
36 40
37 41 #define TYPE_D 'D'
38 42 #define TYPE_F 'F'
39 43
40 44 extern int errno;
41 45
42 46 static char fdname[MAXPATHLEN] = {0};
43 47 static char *pbasedir = NULL;
44 48 static int nlevel = 2;
45 49 static int ndir = 2;
46 50 static int nfile = 2;
47 51
48 52 static void usage(char *this);
49 53 static void crtfile(char *pname);
50 54 static char *getfdname(char *pdir, char type, int level, int dir, int file);
51 55 static int mktree(char *pbasedir, int level);
52 56
53 57 int
54 58 main(int argc, char *argv[])
55 59 {
56 60 int c, ret;
57 61
58 62 while ((c = getopt(argc, argv, "b:l:d:f:")) != -1) {
59 63 switch (c) {
60 64 case 'b':
61 65 pbasedir = optarg;
62 66 break;
63 67 case 'l':
64 68 nlevel = atoi(optarg);
65 69 break;
66 70 case 'd':
67 71 ndir = atoi(optarg);
68 72 break;
69 73 case 'f':
70 74 nfile = atoi(optarg);
71 75 break;
72 76 case '?':
73 77 usage(argv[0]);
74 78 }
75 79 }
76 80 if (nlevel < 0 || ndir < 0 || nfile < 0 || pbasedir == NULL) {
77 81 usage(argv[0]);
78 82 }
79 83
80 84 ret = mktree(pbasedir, 1);
81 85
82 86 return (ret);
83 87 }
84 88
85 89 static void
86 90 usage(char *this)
87 91 {
88 92 (void) fprintf(stderr,
89 93 "\tUsage: %s -b <base_dir> -l [nlevel] -d [ndir] -f [nfile]\n",
90 94 this);
91 95 exit(1);
92 96 }
93 97
94 98 static int
95 99 mktree(char *pdir, int level)
96 100 {
97 101 int d, f;
98 102 char dname[MAXPATHLEN] = {0};
99 103 char fname[MAXPATHLEN] = {0};
|
↓ open down ↓ |
63 lines elided |
↑ open up ↑ |
100 104
101 105 if (level > nlevel) {
102 106 return (1);
103 107 }
104 108
105 109 for (d = 0; d < ndir; d++) {
106 110 (void) memset(dname, '\0', sizeof (dname));
107 111 (void) strcpy(dname, getfdname(pdir, TYPE_D, level, d, 0));
108 112
109 113 if (mkdir(dname, 0777) != 0) {
114 + int exitcode = errno;
110 115 (void) fprintf(stderr, "mkdir(%s) failed."
111 116 "\n[%d]: %s.\n",
112 117 dname, errno, strerror(errno));
113 - exit(errno);
118 + exit(exitcode);
114 119 }
115 120
116 121 /*
117 122 * No sub-directory need be created, only create files in it.
118 123 */
119 124 if (mktree(dname, level+1) != 0) {
120 125 for (f = 0; f < nfile; f++) {
121 126 (void) memset(fname, '\0', sizeof (fname));
122 127 (void) strcpy(fname,
123 128 getfdname(dname, TYPE_F, level+1, d, f));
124 129 crtfile(fname);
125 130 }
126 131 }
127 132 }
128 133
129 134 for (f = 0; f < nfile; f++) {
130 135 (void) memset(fname, '\0', sizeof (fname));
131 136 (void) strcpy(fname, getfdname(pdir, TYPE_F, level, d, f));
132 137 crtfile(fname);
133 138 }
134 139
135 140 return (0);
136 141 }
137 142
138 143 static char *
139 144 getfdname(char *pdir, char type, int level, int dir, int file)
140 145 {
141 146 (void) snprintf(fdname, sizeof (fdname),
|
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
142 147 "%s/%c-l%dd%df%d", pdir, type, level, dir, file);
143 148 return (fdname);
144 149 }
145 150
146 151 static void
147 152 crtfile(char *pname)
148 153 {
149 154 int fd = -1;
150 155 int afd = -1;
151 156 int i, size;
157 + int exitcode;
152 158 char *context = "0123456789ABCDF";
153 159 char *pbuf;
154 160
155 161 if (pname == NULL) {
156 162 exit(1);
157 163 }
158 164
159 165 size = sizeof (char) * 1024;
160 166 pbuf = (char *)valloc(size);
161 167 for (i = 0; i < size / strlen(context); i++) {
162 168 int offset = i * strlen(context);
163 169 (void) snprintf(pbuf+offset, size-offset, "%s", context);
164 170 }
165 171
166 172 if ((fd = open(pname, O_CREAT|O_RDWR, 0777)) < 0) {
173 + exitcode = errno;
167 174 (void) fprintf(stderr, "open(%s, O_CREAT|O_RDWR, 0777) failed."
168 175 "\n[%d]: %s.\n", pname, errno, strerror(errno));
169 - exit(errno);
176 + exit(exitcode);
170 177 }
171 178 if (write(fd, pbuf, 1024) < 1024) {
179 + exitcode = errno;
172 180 (void) fprintf(stderr, "write(fd, pbuf, 1024) failed."
173 181 "\n[%d]: %s.\n", errno, strerror(errno));
174 - exit(errno);
182 + exit(exitcode);
175 183 }
176 184
177 185 if ((afd = openat(fd, "xattr", O_CREAT | O_RDWR | O_XATTR, 0777)) < 0) {
186 + exitcode = errno;
178 187 (void) fprintf(stderr, "openat failed.\n[%d]: %s.\n",
179 188 errno, strerror(errno));
180 - exit(errno);
189 + exit(exitcode);
181 190 }
182 191 if (write(afd, pbuf, 1024) < 1024) {
192 + exitcode = errno;
183 193 (void) fprintf(stderr, "write(afd, pbuf, 1024) failed."
184 194 "\n[%d]: %s.\n", errno, strerror(errno));
185 - exit(errno);
195 + exit(exitcode);
186 196 }
187 197
188 198 (void) close(afd);
189 199 (void) close(fd);
190 200 free(pbuf);
191 201 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX