a51424526d6d9d2c441dfd4166ff43e127f4266f
[samba.git] / source3 / torture / nbio.c
1 #define NBDEBUG 0
2
3 /* 
4    Unix SMB/CIFS implementation.
5    SMB torture tester
6    Copyright (C) Andrew Tridgell 1997-1998
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 as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "torture/proto.h"
24 #include "../libcli/security/security.h"
25
26 #define MAX_FILES 1000
27
28 static char buf[70000];
29 extern int line_count;
30 extern int nbio_id;
31 static int nprocs;
32 static struct timeval nb_start;
33
34 static struct {
35         int fd;
36         int handle;
37 } ftable[MAX_FILES];
38
39 static struct children {
40         double bytes_in, bytes_out;
41         int line;
42         int done;
43 } *children;
44
45 double nbio_total(void)
46 {
47         int i;
48         double total = 0;
49         for (i=0;i<nprocs;i++) {
50                 total += children[i].bytes_out + children[i].bytes_in;
51         }
52         return total;
53 }
54
55 void nb_alarm(int ignore)
56 {
57         int i;
58         int lines=0, num_clients=0;
59         if (nbio_id != -1) return;
60
61         for (i=0;i<nprocs;i++) {
62                 lines += children[i].line;
63                 if (!children[i].done) num_clients++;
64         }
65
66         printf("%4d  %8d  %.2f MB/sec\r",
67                num_clients, lines/nprocs,
68                1.0e-6 * nbio_total() / timeval_elapsed(&nb_start));
69
70         signal(SIGALRM, nb_alarm);
71         alarm(1);       
72 }
73
74 void nbio_shmem(int n)
75 {
76         nprocs = n;
77         children = (struct children *)shm_setup(sizeof(*children) * nprocs);
78         if (!children) {
79                 printf("Failed to setup shared memory!\n");
80                 exit(1);
81         }
82 }
83
84 #if 0
85 static int ne_find_handle(int handle)
86 {
87         int i;
88         children[nbio_id].line = line_count;
89         for (i=0;i<MAX_FILES;i++) {
90                 if (ftable[i].handle == handle) return i;
91         }
92         return -1;
93 }
94 #endif
95
96 static int find_handle(int handle)
97 {
98         int i;
99         children[nbio_id].line = line_count;
100         for (i=0;i<MAX_FILES;i++) {
101                 if (ftable[i].handle == handle) return i;
102         }
103         printf("(%d) ERROR: handle %d was not found\n", 
104                line_count, handle);
105         exit(1);
106
107         return -1;              /* Not reached */
108 }
109
110
111 static struct cli_state *c;
112
113 static void sigsegv(int sig)
114 {
115         char line[200];
116         printf("segv at line %d\n", line_count);
117         slprintf(line, sizeof(line), "/usr/X11R6/bin/xterm -e gdb /proc/%d/exe %d", 
118                 (int)getpid(), (int)getpid());
119         if (system(line) == -1) {
120                 printf("system() failed\n");
121         }
122         exit(1);
123 }
124
125 void nb_setup(struct cli_state *cli)
126 {
127         signal(SIGSEGV, sigsegv);
128         c = cli;
129         nb_start = timeval_current();
130         children[nbio_id].done = 0;
131 }
132
133
134 void nb_unlink(const char *fname)
135 {
136         if (!NT_STATUS_IS_OK(cli_unlink(c, fname, aSYSTEM | aHIDDEN))) {
137 #if NBDEBUG
138                 printf("(%d) unlink %s failed (%s)\n", 
139                        line_count, fname, cli_errstr(c));
140 #endif
141         }
142 }
143
144
145 void nb_createx(const char *fname, 
146                 unsigned create_options, unsigned create_disposition, int handle)
147 {
148         uint16_t fd = (uint16_t)-1;
149         int i;
150         NTSTATUS status;
151         uint32 desired_access;
152
153         if (create_options & FILE_DIRECTORY_FILE) {
154                 desired_access = FILE_READ_DATA;
155         } else {
156                 desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
157         }
158
159         status = cli_ntcreate(c, fname, 0, 
160                                 desired_access,
161                                 0x0,
162                                 FILE_SHARE_READ|FILE_SHARE_WRITE, 
163                                 create_disposition, 
164                                 create_options, 0, &fd);
165         if (!NT_STATUS_IS_OK(status) && handle != -1) {
166                 printf("ERROR: cli_ntcreate failed for %s - %s\n",
167                        fname, cli_errstr(c));
168                 exit(1);
169         }
170         if (NT_STATUS_IS_OK(status) && handle == -1) {
171                 printf("ERROR: cli_ntcreate succeeded for %s\n", fname);
172                 exit(1);
173         }
174         if (fd == (uint16_t)-1) return;
175
176         for (i=0;i<MAX_FILES;i++) {
177                 if (ftable[i].handle == 0) break;
178         }
179         if (i == MAX_FILES) {
180                 printf("(%d) file table full for %s\n", line_count, 
181                        fname);
182                 exit(1);
183         }
184         ftable[i].handle = handle;
185         ftable[i].fd = fd;
186 }
187
188 void nb_writex(int handle, int offset, int size, int ret_size)
189 {
190         int i;
191
192         if (buf[0] == 0) memset(buf, 1, sizeof(buf));
193
194         i = find_handle(handle);
195         if (cli_write(c, ftable[i].fd, 0, buf, offset, size) != ret_size) {
196                 printf("(%d) ERROR: write failed on handle %d, fd %d \
197 errno %d (%s)\n", line_count, handle, ftable[i].fd, errno, strerror(errno));
198                 exit(1);
199         }
200
201         children[nbio_id].bytes_out += ret_size;
202 }
203
204 void nb_readx(int handle, int offset, int size, int ret_size)
205 {
206         int i, ret;
207
208         i = find_handle(handle);
209         if ((ret=cli_read(c, ftable[i].fd, buf, offset, size)) != ret_size) {
210                 printf("(%d) ERROR: read failed on handle %d ofs=%d size=%d res=%d fd %d errno %d (%s)\n",
211                         line_count, handle, offset, size, ret, ftable[i].fd, errno, strerror(errno));
212                 exit(1);
213         }
214         children[nbio_id].bytes_in += ret_size;
215 }
216
217 void nb_close(int handle)
218 {
219         int i;
220         i = find_handle(handle);
221         if (!NT_STATUS_IS_OK(cli_close(c, ftable[i].fd))) {
222                 printf("(%d) close failed on handle %d\n", line_count, handle);
223                 exit(1);
224         }
225         ftable[i].handle = 0;
226 }
227
228 void nb_rmdir(const char *fname)
229 {
230         if (!NT_STATUS_IS_OK(cli_rmdir(c, fname))) {
231                 printf("ERROR: rmdir %s failed (%s)\n", 
232                        fname, cli_errstr(c));
233                 exit(1);
234         }
235 }
236
237 void nb_rename(const char *oldname, const char *newname)
238 {
239         if (!NT_STATUS_IS_OK(cli_rename(c, oldname, newname))) {
240                 printf("ERROR: rename %s %s failed (%s)\n", 
241                        oldname, newname, cli_errstr(c));
242                 exit(1);
243         }
244 }
245
246
247 void nb_qpathinfo(const char *fname)
248 {
249         cli_qpathinfo1(c, fname, NULL, NULL, NULL, NULL, NULL);
250 }
251
252 void nb_qfileinfo(int fnum)
253 {
254         int i;
255         i = find_handle(fnum);
256         cli_qfileinfo_basic(c, ftable[i].fd, NULL, NULL, NULL, NULL, NULL,
257                             NULL, NULL);
258 }
259
260 void nb_qfsinfo(int level)
261 {
262         int bsize, total, avail;
263         /* this is not the right call - we need cli_qfsinfo() */
264         cli_dskattr(c, &bsize, &total, &avail);
265 }
266
267 static NTSTATUS find_fn(const char *mnt, struct file_info *finfo, const char *name,
268                     void *state)
269 {
270         /* noop */
271         return NT_STATUS_OK;
272 }
273
274 void nb_findfirst(const char *mask)
275 {
276         cli_list(c, mask, 0, find_fn, NULL);
277 }
278
279 void nb_flush(int fnum)
280 {
281         int i;
282         i = find_handle(fnum);
283         /* hmmm, we don't have cli_flush() yet */
284 }
285
286 static int total_deleted;
287
288 static NTSTATUS delete_fn(const char *mnt, struct file_info *finfo,
289                       const char *name, void *state)
290 {
291         NTSTATUS status;
292         char *s, *n;
293         if (finfo->name[0] == '.') {
294                 return NT_STATUS_OK;
295         }
296
297         n = SMB_STRDUP(name);
298         n[strlen(n)-1] = 0;
299         if (asprintf(&s, "%s%s", n, finfo->name) == -1) {
300                 printf("asprintf failed\n");
301                 return NT_STATUS_NO_MEMORY;
302         }
303         if (finfo->mode & aDIR) {
304                 char *s2;
305                 if (asprintf(&s2, "%s\\*", s) == -1) {
306                         printf("asprintf failed\n");
307                         return NT_STATUS_NO_MEMORY;
308                 }
309                 status = cli_list(c, s2, aDIR, delete_fn, NULL);
310                 if (!NT_STATUS_IS_OK(status)) {
311                         free(n);
312                         free(s2);
313                         return status;
314                 }
315                 nb_rmdir(s);
316         } else {
317                 total_deleted++;
318                 nb_unlink(s);
319         }
320         free(s);
321         free(n);
322         return NT_STATUS_OK;
323 }
324
325 void nb_deltree(const char *dname)
326 {
327         char *mask;
328         if (asprintf(&mask, "%s\\*", dname) == -1) {
329                 printf("asprintf failed\n");
330                 return;
331         }
332
333         total_deleted = 0;
334         cli_list(c, mask, aDIR, delete_fn, NULL);
335         free(mask);
336         cli_rmdir(c, dname);
337
338         if (total_deleted) printf("WARNING: Cleaned up %d files\n", total_deleted);
339 }
340
341
342 void nb_cleanup(void)
343 {
344         cli_rmdir(c, "clients");
345         children[nbio_id].done = 1;
346 }