Fix bug #6009 - Setting "min receivefile size = 1" breaks writes.
[Samba.git] / source / torture / nbio.c
blob81d0eb816b9fbdd415e21d930d7cd6facf6b1034
1 #define NBDEBUG 0
3 /*
4 Unix SMB/CIFS implementation.
5 SMB torture tester
6 Copyright (C) Andrew Tridgell 1997-1998
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.
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.
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/>.
22 #include "includes.h"
24 #define MAX_FILES 1000
26 static char buf[70000];
27 extern int line_count;
28 extern int nbio_id;
29 static int nprocs;
31 static struct {
32 int fd;
33 int handle;
34 } ftable[MAX_FILES];
36 static struct children {
37 double bytes_in, bytes_out;
38 int line;
39 int done;
40 } *children;
42 double nbio_total(void)
44 int i;
45 double total = 0;
46 for (i=0;i<nprocs;i++) {
47 total += children[i].bytes_out + children[i].bytes_in;
49 return total;
52 void nb_alarm(int ignore)
54 int i;
55 int lines=0, num_clients=0;
56 if (nbio_id != -1) return;
58 for (i=0;i<nprocs;i++) {
59 lines += children[i].line;
60 if (!children[i].done) num_clients++;
63 printf("%4d %8d %.2f MB/sec\r", num_clients, lines/nprocs, 1.0e-6 * nbio_total() / end_timer());
65 signal(SIGALRM, nb_alarm);
66 alarm(1);
69 void nbio_shmem(int n)
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);
79 #if 0
80 static int ne_find_handle(int handle)
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;
87 return -1;
89 #endif
91 static int find_handle(int handle)
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;
98 printf("(%d) ERROR: handle %d was not found\n",
99 line_count, handle);
100 exit(1);
102 return -1; /* Not reached */
106 static struct cli_state *c;
108 static void sigsegv(int sig)
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 system(line);
115 exit(1);
118 void nb_setup(struct cli_state *cli)
120 signal(SIGSEGV, sigsegv);
121 c = cli;
122 start_timer();
123 children[nbio_id].done = 0;
127 void nb_unlink(const char *fname)
129 if (!cli_unlink(c, fname)) {
130 #if NBDEBUG
131 printf("(%d) unlink %s failed (%s)\n",
132 line_count, fname, cli_errstr(c));
133 #endif
138 void nb_createx(const char *fname,
139 unsigned create_options, unsigned create_disposition, int handle)
141 int fd, i;
142 uint32 desired_access;
144 if (create_options & FILE_DIRECTORY_FILE) {
145 desired_access = FILE_READ_DATA;
146 } else {
147 desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
150 fd = cli_nt_create_full(c, fname, 0,
151 desired_access,
152 0x0,
153 FILE_SHARE_READ|FILE_SHARE_WRITE,
154 create_disposition,
155 create_options, 0);
156 if (fd == -1 && handle != -1) {
157 printf("ERROR: cli_nt_create_full failed for %s - %s\n",
158 fname, cli_errstr(c));
159 exit(1);
161 if (fd != -1 && handle == -1) {
162 printf("ERROR: cli_nt_create_full succeeded for %s\n", fname);
163 exit(1);
165 if (fd == -1) return;
167 for (i=0;i<MAX_FILES;i++) {
168 if (ftable[i].handle == 0) break;
170 if (i == MAX_FILES) {
171 printf("(%d) file table full for %s\n", line_count,
172 fname);
173 exit(1);
175 ftable[i].handle = handle;
176 ftable[i].fd = fd;
179 void nb_writex(int handle, int offset, int size, int ret_size)
181 int i;
183 if (buf[0] == 0) memset(buf, 1, sizeof(buf));
185 i = find_handle(handle);
186 if (cli_write(c, ftable[i].fd, 0, buf, offset, size) != ret_size) {
187 printf("(%d) ERROR: write failed on handle %d, fd %d \
188 errno %d (%s)\n", line_count, handle, ftable[i].fd, errno, strerror(errno));
189 exit(1);
192 children[nbio_id].bytes_out += ret_size;
195 void nb_readx(int handle, int offset, int size, int ret_size)
197 int i, ret;
199 i = find_handle(handle);
200 if ((ret=cli_read(c, ftable[i].fd, buf, offset, size)) != ret_size) {
201 printf("(%d) ERROR: read failed on handle %d ofs=%d size=%d res=%d fd %d errno %d (%s)\n",
202 line_count, handle, offset, size, ret, ftable[i].fd, errno, strerror(errno));
203 exit(1);
205 children[nbio_id].bytes_in += ret_size;
208 void nb_close(int handle)
210 int i;
211 i = find_handle(handle);
212 if (!cli_close(c, ftable[i].fd)) {
213 printf("(%d) close failed on handle %d\n", line_count, handle);
214 exit(1);
216 ftable[i].handle = 0;
219 void nb_rmdir(const char *fname)
221 if (!cli_rmdir(c, fname)) {
222 printf("ERROR: rmdir %s failed (%s)\n",
223 fname, cli_errstr(c));
224 exit(1);
228 void nb_rename(const char *oldname, const char *newname)
230 if (!cli_rename(c, oldname, newname)) {
231 printf("ERROR: rename %s %s failed (%s)\n",
232 oldname, newname, cli_errstr(c));
233 exit(1);
238 void nb_qpathinfo(const char *fname)
240 cli_qpathinfo(c, fname, NULL, NULL, NULL, NULL, NULL);
243 void nb_qfileinfo(int fnum)
245 int i;
246 i = find_handle(fnum);
247 cli_qfileinfo(c, ftable[i].fd, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
250 void nb_qfsinfo(int level)
252 int bsize, total, avail;
253 /* this is not the right call - we need cli_qfsinfo() */
254 cli_dskattr(c, &bsize, &total, &avail);
257 static void find_fn(const char *mnt, file_info *finfo, const char *name, void *state)
259 /* noop */
262 void nb_findfirst(const char *mask)
264 cli_list(c, mask, 0, find_fn, NULL);
267 void nb_flush(int fnum)
269 int i;
270 i = find_handle(fnum);
271 /* hmmm, we don't have cli_flush() yet */
274 static int total_deleted;
276 static void delete_fn(const char *mnt, file_info *finfo, const char *name, void *state)
278 char *s, *n;
279 if (finfo->name[0] == '.') return;
281 n = SMB_STRDUP(name);
282 n[strlen(n)-1] = 0;
283 asprintf(&s, "%s%s", n, finfo->name);
284 if (finfo->mode & aDIR) {
285 char *s2;
286 asprintf(&s2, "%s\\*", s);
287 cli_list(c, s2, aDIR, delete_fn, NULL);
288 nb_rmdir(s);
289 } else {
290 total_deleted++;
291 nb_unlink(s);
293 free(s);
294 free(n);
297 void nb_deltree(const char *dname)
299 char *mask;
300 asprintf(&mask, "%s\\*", dname);
302 total_deleted = 0;
303 cli_list(c, mask, aDIR, delete_fn, NULL);
304 free(mask);
305 cli_rmdir(c, dname);
307 if (total_deleted) printf("WARNING: Cleaned up %d files\n", total_deleted);
311 void nb_cleanup(void)
313 cli_rmdir(c, "clients");
314 children[nbio_id].done = 1;