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