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