Print this page
10076 make usr/src/test smatch clean
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/test/smbclient-tests/cmd/mkfile_mmap/mkfile_mmap.c
+++ new/usr/src/test/smbclient-tests/cmd/mkfile_mmap/mkfile_mmap.c
1 1 /*
2 2 * This file and its contents are supplied under the terms of the
3 3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 4 * You may only use this file in accordance with the terms of version
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
5 5 * 1.0 of the CDDL.
6 6 *
7 7 * A full copy of the text of the CDDL should have accompanied this
8 8 * source. A copy of the CDDL is also available via the Internet at
9 9 * http://www.illumos.org/license/CDDL.
10 10 */
11 11
12 12 /*
13 13 * Copyright 2012 Jilin Xpd <jilinxpd@gmail.com>
14 14 * Copyright 2018 Nexenta Systems, Inc.
15 + * Copyright (c) 2018, Joyent, Inc.
15 16 */
16 17
17 18 /*
18 19 * Using mmap, make a file and padding it with random chars.
19 20 */
20 21
21 22 #include <sys/mman.h>
22 23 #include <sys/types.h>
23 24 #include <sys/stat.h>
24 25 #include <fcntl.h>
25 26 #include <stdio.h>
26 27 #include <stdlib.h>
27 28 #include <unistd.h>
28 29 #include <string.h>
29 30 #include <errno.h>
30 31 #include <time.h>
31 32
32 33 void
33 34 usage(void)
34 35 {
35 36 fprintf(stderr,
36 37 "usage: mkfile_mmap -n <size>[b|k|m|g] -f <filename>\n");
37 38 exit(1);
38 39 }
39 40
40 41 int
41 42 main(int argc, char **argv)
42 43 {
43 44 char *suffix;
44 45 char *filename = NULL;
45 46 char *file_addr;
46 47 char *p, *q;
47 48 off_t offset;
48 49 size_t filesize;
49 50 size_t blksize;
50 51 size_t numblks;
51 52 size_t cnt = 1;
52 53 size_t mul = 1;
53 54 size_t i;
54 55 int mret = 0;
55 56 int c, fid;
56 57
57 58 /*
58 59 * parse arguments
59 60 */
60 61 while ((c = getopt(argc, argv, "n:f:")) != -1) {
61 62 switch (c) {
62 63 case 'n':
63 64 cnt = (size_t)strtoul(optarg, &suffix, 0);
64 65 if (cnt == 0)
65 66 goto bad_n_arg;
66 67 switch (*suffix) {
67 68 case '\0':
68 69 case 'b':
69 70 mul = 1;
70 71 break;
71 72 case 'k':
72 73 mul = 1024;
73 74 break;
74 75 case 'm':
75 76 mul = (1024 * 1024);
76 77 break;
77 78 case 'g':
78 79 mul = (1024 * 1024 * 1024);
79 80 break;
80 81 default:
81 82 bad_n_arg:
82 83 fprintf(stderr, "-n %s: invalid size\n",
83 84 optarg);
84 85 return (1);
85 86 }
86 87 cnt = cnt * mul;
87 88 break;
88 89
89 90 case 'f': /* target file */
90 91 filename = optarg;
91 92 break;
92 93
93 94 case ':': /* missing optarg */
94 95 fprintf(stderr,
95 96 "Option -%c requires an arg\n", optopt);
96 97 usage();
97 98 break;
98 99 case '?':
99 100 fprintf(stderr,
100 101 "Unrecognized option: -%c\n", optopt);
101 102 usage();
102 103 break;
103 104 }
104 105 }
105 106
106 107 /* open test file */
107 108 fid = open(filename, O_RDWR | O_CREAT | O_TRUNC,
108 109 S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH);
109 110 if (fid == -1) {
110 111 fprintf(stderr, "open %s error=%d\n", filename, errno);
111 112 mret = 1;
112 113 goto exit3;
113 114 }
114 115
115 116 /* extend file */
116 117 filesize = cnt;
117 118 if (ftruncate(fid, filesize) == -1) {
118 119 fprintf(stderr, "ftrunc %s error=%d\n", filename, errno);
119 120 mret = 1;
120 121 goto exit2;
121 122 }
122 123
123 124 #define K 1024
124 125
125 126 blksize = 64 * K * K;
126 127 numblks = (filesize + blksize - 1) / blksize;
127 128 for (i = 0; i < numblks && mret == 0; i++) {
128 129
129 130 offset = i*blksize;
130 131 if (offset + blksize > filesize)
131 132 blksize = filesize - offset;
↓ open down ↓ |
107 lines elided |
↑ open up ↑ |
132 133
133 134 /* map file */
134 135 file_addr = mmap(NULL, blksize,
135 136 PROT_READ | PROT_WRITE, MAP_SHARED, fid, offset);
136 137 if (file_addr == MAP_FAILED) {
137 138 fprintf(stderr, "mmap %s error=%d\n", filename, errno);
138 139 mret = 1;
139 140 break;
140 141 }
141 142
142 - /* tag each block (to aid debug) */
143 - p = file_addr;
144 - q = file_addr + blksize - K;
145 - memset(p, ' ', K);
146 - snprintf(p, K, "\nblk=%d\n\n", i);
147 - p += K;
148 -
149 - /* fill something into mapped addr */
150 - while (p < q) {
143 + /* tag each block (to aid debug) */
144 + p = file_addr;
145 + q = file_addr + blksize - K;
151 146 memset(p, ' ', K);
152 - snprintf(p, K, "\noff=0x%x\n\n",
153 - (i * blksize) + (p - file_addr));
147 + snprintf(p, K, "\nblk=%d\n\n", i);
154 148 p += K;
155 - }
156 149
150 + /* fill something into mapped addr */
151 + while (p < q) {
152 + memset(p, ' ', K);
153 + snprintf(p, K, "\noff=0x%x\n\n",
154 + (i * blksize) + (p - file_addr));
155 + p += K;
156 + }
157 +
157 158 /* sync mapped pages to file */
158 159 if (msync(file_addr, blksize, MS_SYNC) == -1) {
159 160 fprintf(stderr, "msync %s error=%d\n", filename, errno);
160 161 mret = 1;
161 162 }
162 163
163 164 /* unmap file */
164 165 if (munmap(file_addr, blksize) == -1) {
165 166 fprintf(stderr, "unmap %s error=%d\n", filename, errno);
166 167 mret = 1;
167 168 }
168 169 }
169 170
170 171 /* close file */
171 172 exit2:
172 173 close(fid);
173 174 exit3:
174 175 return (mret);
175 176 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX