Back out the wrong changes I made yesterday ...
[ira/wip.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #define NO_SYSLOG
24
25 #include "includes.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
34 static struct {
35         int fd;
36         int handle;
37 } ftable[MAX_FILES];
38
39 static struct {
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(void)
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", num_clients, lines/nprocs, 1.0e-6 * nbio_total() / end_timer());
67
68         signal(SIGALRM, nb_alarm);
69         alarm(1);       
70 }
71
72 void nbio_shmem(int n)
73 {
74         nprocs = n;
75         children = shm_setup(sizeof(*children) * nprocs);
76         if (!children) {
77                 printf("Failed to setup shared memory!\n");
78                 exit(1);
79         }
80 }
81
82 static int ne_find_handle(int handle)
83 {
84         int i;
85         children[nbio_id].line = line_count;
86         for (i=0;i<MAX_FILES;i++) {
87                 if (ftable[i].handle == handle) return i;
88         }
89         return -1;
90 }
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         system(line);
116         exit(1);
117 }
118
119 void nb_setup(struct cli_state *cli)
120 {
121         signal(SIGSEGV, sigsegv);
122         c = cli;
123         start_timer();
124         children[nbio_id].done = 0;
125 }
126
127
128 void nb_unlink(char *fname)
129 {
130         if (!cli_unlink(c, fname)) {
131 #if NBDEBUG
132                 printf("(%d) unlink %s failed (%s)\n", 
133                        line_count, fname, cli_errstr(c));
134 #endif
135         }
136 }
137
138
139 void nb_createx(char *fname, 
140                 unsigned create_options, unsigned create_disposition, int handle)
141 {
142         int fd, i;
143         uint32 desired_access;
144
145         if (create_options & FILE_DIRECTORY_FILE) {
146                 desired_access = FILE_READ_DATA;
147         } else {
148                 desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
149         }
150
151         fd = cli_nt_create_full(c, fname, 
152                                 desired_access,
153                                 0x0,
154                                 FILE_SHARE_READ|FILE_SHARE_WRITE, 
155                                 create_disposition, 
156                                 create_options);
157         if (fd == -1 && handle != -1) {
158                 printf("ERROR: cli_nt_create_full failed for %s - %s\n",
159                        fname, cli_errstr(c));
160                 exit(1);
161         }
162         if (fd != -1 && handle == -1) {
163                 printf("ERROR: cli_nt_create_full succeeded for %s\n", fname);
164                 exit(1);
165         }
166         if (fd == -1) return;
167
168         for (i=0;i<MAX_FILES;i++) {
169                 if (ftable[i].handle == 0) break;
170         }
171         if (i == MAX_FILES) {
172                 printf("(%d) file table full for %s\n", line_count, 
173                        fname);
174                 exit(1);
175         }
176         ftable[i].handle = handle;
177         ftable[i].fd = fd;
178 }
179
180 void nb_writex(int handle, int offset, int size, int ret_size)
181 {
182         int i;
183
184         if (buf[0] == 0) memset(buf, 1, sizeof(buf));
185
186         i = find_handle(handle);
187         if (cli_write(c, ftable[i].fd, 0, buf, offset, size) != ret_size) {
188                 printf("(%d) ERROR: write failed on handle %d, fd %d \
189 errno %d (%s)\n", line_count, handle, ftable[i].fd, errno, strerror(errno));
190                 exit(1);
191         }
192
193         children[nbio_id].bytes_out += ret_size;
194 }
195
196 void nb_readx(int handle, int offset, int size, int ret_size)
197 {
198         int i, ret;
199
200         i = find_handle(handle);
201         if ((ret=cli_read(c, ftable[i].fd, buf, offset, size)) != ret_size) {
202                 printf("(%d) ERROR: read failed on handle %d ofs=%d size=%d res=%d fd %d errno %d (%s)\n",
203                         line_count, handle, offset, size, ret, ftable[i].fd, errno, strerror(errno));
204                 exit(1);
205         }
206         children[nbio_id].bytes_in += ret_size;
207 }
208
209 void nb_close(int handle)
210 {
211         int i;
212         i = find_handle(handle);
213         if (!cli_close(c, ftable[i].fd)) {
214                 printf("(%d) close failed on handle %d\n", line_count, handle);
215                 exit(1);
216         }
217         ftable[i].handle = 0;
218 }
219
220 void nb_rmdir(char *fname)
221 {
222         if (!cli_rmdir(c, fname)) {
223                 printf("ERROR: rmdir %s failed (%s)\n", 
224                        fname, cli_errstr(c));
225                 exit(1);
226         }
227 }
228
229 void nb_rename(char *old, char *new)
230 {
231         if (!cli_rename(c, old, new)) {
232                 printf("ERROR: rename %s %s failed (%s)\n", 
233                        old, new, cli_errstr(c));
234                 exit(1);
235         }
236 }
237
238
239 void nb_qpathinfo(char *fname)
240 {
241         cli_qpathinfo(c, fname, NULL, NULL, NULL, NULL, NULL);
242 }
243
244 void nb_qfileinfo(int fnum)
245 {
246         int i;
247         i = find_handle(fnum);
248         cli_qfileinfo(c, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
249 }
250
251 void nb_qfsinfo(int level)
252 {
253         int bsize, total, avail;
254         /* this is not the right call - we need cli_qfsinfo() */
255         cli_dskattr(c, &bsize, &total, &avail);
256 }
257
258 static void find_fn(file_info *finfo, const char *name, void *state)
259 {
260         /* noop */
261 }
262
263 void nb_findfirst(char *mask)
264 {
265         cli_list(c, mask, 0, find_fn, NULL);
266 }
267
268 void nb_flush(int fnum)
269 {
270         int i;
271         i = find_handle(fnum);
272         /* hmmm, we don't have cli_flush() yet */
273 }
274
275 static int total_deleted;
276
277 static void delete_fn(file_info *finfo, const char *name, void *state)
278 {
279         char *s, *n;
280         if (finfo->name[0] == '.') return;
281
282         n = strdup(name);
283         n[strlen(n)-1] = 0;
284         asprintf(&s, "%s%s", n, finfo->name);
285         if (finfo->mode & aDIR) {
286                 char *s2;
287                 asprintf(&s2, "%s\\*", s);
288                 cli_list(c, s2, aDIR, delete_fn, NULL);
289                 nb_rmdir(s);
290         } else {
291                 total_deleted++;
292                 nb_unlink(s);
293         }
294         free(s);
295         free(n);
296 }
297
298 void nb_deltree(char *dname)
299 {
300         char *mask;
301         asprintf(&mask, "%s\\*", dname);
302
303         total_deleted = 0;
304         cli_list(c, mask, aDIR, delete_fn, NULL);
305         free(mask);
306         cli_rmdir(c, dname);
307
308         if (total_deleted) printf("WARNING: Cleaned up %d files\n", total_deleted);
309 }
310
311
312 void nb_cleanup(void)
313 {
314         cli_rmdir(c, "clients");
315         children[nbio_id].done = 1;
316 }