- only show 1 cleanup msg per client
[amitay/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 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 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         printf("ERROR: handle %d was not found\n", 
90                line_count, handle);
91         exit(1);
92 }
93
94
95 static struct cli_state *c;
96
97 static void sigsegv(int sig)
98 {
99         char line[200];
100         printf("segv at line %d\n", line_count);
101         slprintf(line, sizeof(line), "/usr/X11R6/bin/xterm -e gdb /proc/%d/exe %d", 
102                 (int)getpid(), (int)getpid());
103         system(line);
104         exit(1);
105 }
106
107 void nb_setup(struct cli_state *cli)
108 {
109         signal(SIGSEGV, sigsegv);
110         c = cli;
111         start_timer();
112         children[nbio_id].done = 0;
113 }
114
115
116 void nb_unlink(char *fname)
117 {
118         if (!cli_unlink(c, fname)) {
119 #if NBDEBUG
120                 printf("(%d) unlink %s failed (%s)\n", 
121                        line_count, fname, cli_errstr(c));
122 #endif
123         }
124 }
125
126
127 void nb_createx(char *fname, 
128                 unsigned create_options, unsigned create_disposition, int handle)
129 {
130         int fd, i;
131         static int count;
132
133         fd = cli_nt_create_full(c, fname, 
134                                 FILE_READ_DATA | FILE_WRITE_DATA,
135                                 0,
136                                 FILE_SHARE_READ|FILE_SHARE_WRITE, 
137                                 create_disposition, 
138                                 create_options);
139         if (fd == -1 && handle != -1) {
140                 printf("ERROR: cli_nt_create_full failed for %s - %s\n",
141                        fname, cli_errstr(c));
142                 exit(1);
143         }
144         if (fd != -1 && handle == -1) {
145                 printf("ERROR: cli_nt_create_full succeeded for %s\n", fname);
146                 exit(1);
147         }
148         if (fd == -1) return;
149
150         for (i=0;i<MAX_FILES;i++) {
151                 if (ftable[i].handle == 0) break;
152         }
153         if (i == MAX_FILES) {
154                 printf("(%d) file table full for %s\n", line_count, 
155                        fname);
156                 exit(1);
157         }
158         ftable[i].handle = handle;
159         ftable[i].fd = fd;
160 }
161
162 void nb_writex(int handle, int offset, int size, int ret_size)
163 {
164         int i;
165
166         if (buf[0] == 0) memset(buf, 1, sizeof(buf));
167
168         i = find_handle(handle);
169         if (cli_write(c, ftable[i].fd, 0, buf, offset, size) != ret_size) {
170                 printf("(%d) ERROR: write failed on handle %d, fd %d \
171 errno %d (%s)\n", line_count, handle, ftable[i].fd, errno, strerror(errno));
172                 exit(1);
173         }
174
175         children[nbio_id].bytes_out += ret_size;
176 }
177
178 void nb_readx(int handle, int offset, int size, int ret_size)
179 {
180         int i, ret;
181
182         i = find_handle(handle);
183         if ((ret=cli_read(c, ftable[i].fd, buf, offset, size)) != ret_size) {
184                 printf("(%d) ERROR: read failed on handle %d ofs=%d size=%d res=%d fd %d errno %d (%s)\n",
185                         line_count, handle, offset, size, ret, ftable[i].fd, errno, strerror(errno));
186                 exit(1);
187         }
188         children[nbio_id].bytes_in += ret_size;
189 }
190
191 void nb_close(int handle)
192 {
193         int i;
194         i = find_handle(handle);
195         if (!cli_close(c, ftable[i].fd)) {
196                 printf("(%d) close failed on handle %d\n", line_count, handle);
197                 exit(1);
198         }
199         ftable[i].handle = 0;
200 }
201
202 void nb_rmdir(char *fname)
203 {
204         if (!cli_rmdir(c, fname)) {
205                 printf("ERROR: rmdir %s failed (%s)\n", 
206                        fname, cli_errstr(c));
207                 exit(1);
208         }
209 }
210
211 void nb_rename(char *old, char *new)
212 {
213         if (!cli_rename(c, old, new)) {
214                 printf("ERROR: rename %s %s failed (%s)\n", 
215                        old, new, cli_errstr(c));
216                 exit(1);
217         }
218 }
219
220
221 void nb_qpathinfo(char *fname)
222 {
223         cli_qpathinfo(c, fname, NULL, NULL, NULL, NULL, NULL);
224 }
225
226 void nb_qfileinfo(int fnum)
227 {
228         int i;
229         i = find_handle(fnum);
230         cli_qfileinfo(c, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
231 }
232
233 void nb_qfsinfo(int level)
234 {
235         int bsize, total, avail;
236         /* this is not the right call - we need cli_qfsinfo() */
237         cli_dskattr(c, &bsize, &total, &avail);
238 }
239
240 static void find_fn(file_info *finfo, const char *name, void *state)
241 {
242         /* noop */
243 }
244
245 void nb_findfirst(char *mask)
246 {
247         cli_list(c, mask, 0, find_fn, NULL);
248 }
249
250 void nb_flush(int fnum)
251 {
252         int i;
253         i = find_handle(fnum);
254         /* hmmm, we don't have cli_flush() yet */
255 }
256
257 static int total_deleted;
258
259 static void delete_fn(file_info *finfo, const char *name, void *state)
260 {
261         char *s, *n;
262         if (finfo->name[0] == '.') return;
263
264         n = strdup(name);
265         n[strlen(n)-1] = 0;
266         asprintf(&s, "%s%s", n, finfo->name);
267         if (finfo->mode & aDIR) {
268                 char *s2;
269                 asprintf(&s2, "%s\\*", s);
270                 cli_list(c, s2, aDIR, delete_fn, NULL);
271                 nb_rmdir(s);
272         } else {
273                 total_deleted++;
274                 nb_unlink(s);
275         }
276         free(s);
277         free(n);
278 }
279
280 void nb_deltree(char *dname)
281 {
282         char *mask;
283         asprintf(&mask, "%s\\*", dname);
284
285         total_deleted = 0;
286         cli_list(c, mask, aDIR, delete_fn, NULL);
287         free(mask);
288         cli_rmdir(c, dname);
289
290         if (total_deleted) printf("WARNING: Cleaned up %d files\n", total_deleted);
291 }
292
293
294 void nb_cleanup(void)
295 {
296         cli_rmdir(c, "clients");
297         children[nbio_id].done = 1;
298 }