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