s3: Delete streams on directories
[Samba.git] / source3 / torture / nbio.c
blob7634f52b140c85c4c25ee2f02a26c539e335c9a5
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"
23 #include "torture/proto.h"
24 #include "../libcli/security/security.h"
25 #include "libsmb/libsmb.h"
26 #include "libsmb/clirap.h"
28 #define MAX_FILES 1000
30 static char buf[70000];
31 extern int line_count;
32 extern int nbio_id;
33 static int nprocs;
34 static struct timeval nb_start;
36 static struct {
37 int fd;
38 int handle;
39 } ftable[MAX_FILES];
41 static struct children {
42 double bytes_in, bytes_out;
43 int line;
44 int done;
45 } *children;
47 double nbio_total(void)
49 int i;
50 double total = 0;
51 for (i=0;i<nprocs;i++) {
52 total += children[i].bytes_out + children[i].bytes_in;
54 return total;
57 void nb_alarm(int ignore)
59 int i;
60 int lines=0, num_clients=0;
61 if (nbio_id != -1) return;
63 for (i=0;i<nprocs;i++) {
64 lines += children[i].line;
65 if (!children[i].done) num_clients++;
68 printf("%4d %8d %.2f MB/sec\r",
69 num_clients, lines/nprocs,
70 1.0e-6 * nbio_total() / timeval_elapsed(&nb_start));
72 signal(SIGALRM, nb_alarm);
73 alarm(1);
76 void nbio_shmem(int n)
78 nprocs = n;
79 children = (struct children *)shm_setup(sizeof(*children) * nprocs);
80 if (!children) {
81 printf("Failed to setup shared memory!\n");
82 exit(1);
86 #if 0
87 static int ne_find_handle(int handle)
89 int i;
90 children[nbio_id].line = line_count;
91 for (i=0;i<MAX_FILES;i++) {
92 if (ftable[i].handle == handle) return i;
94 return -1;
96 #endif
98 static int find_handle(int handle)
100 int i;
101 children[nbio_id].line = line_count;
102 for (i=0;i<MAX_FILES;i++) {
103 if (ftable[i].handle == handle) return i;
105 printf("(%d) ERROR: handle %d was not found\n",
106 line_count, handle);
107 exit(1);
109 return -1; /* Not reached */
113 static struct cli_state *c;
115 static void sigsegv(int sig)
117 char line[200];
118 printf("segv at line %d\n", line_count);
119 slprintf(line, sizeof(line), "/usr/X11R6/bin/xterm -e gdb /proc/%d/exe %d",
120 (int)getpid(), (int)getpid());
121 if (system(line) == -1) {
122 printf("system() failed\n");
124 exit(1);
127 void nb_setup(struct cli_state *cli)
129 signal(SIGSEGV, sigsegv);
130 c = cli;
131 nb_start = timeval_current();
132 children[nbio_id].done = 0;
136 void nb_unlink(const char *fname)
138 if (!NT_STATUS_IS_OK(cli_unlink(c, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
139 #if NBDEBUG
140 printf("(%d) unlink %s failed (%s)\n",
141 line_count, fname, cli_errstr(c));
142 #endif
147 void nb_createx(const char *fname,
148 unsigned create_options, unsigned create_disposition, int handle)
150 uint16_t fd = (uint16_t)-1;
151 int i;
152 NTSTATUS status;
153 uint32 desired_access;
155 if (create_options & FILE_DIRECTORY_FILE) {
156 desired_access = FILE_READ_DATA;
157 } else {
158 desired_access = FILE_READ_DATA | FILE_WRITE_DATA;
161 status = cli_ntcreate(c, fname, 0,
162 desired_access,
163 0x0,
164 FILE_SHARE_READ|FILE_SHARE_WRITE,
165 create_disposition,
166 create_options, 0, &fd);
167 if (!NT_STATUS_IS_OK(status) && handle != -1) {
168 printf("ERROR: cli_ntcreate failed for %s - %s\n",
169 fname, cli_errstr(c));
170 exit(1);
172 if (NT_STATUS_IS_OK(status) && handle == -1) {
173 printf("ERROR: cli_ntcreate succeeded for %s\n", fname);
174 exit(1);
176 if (fd == (uint16_t)-1) return;
178 for (i=0;i<MAX_FILES;i++) {
179 if (ftable[i].handle == 0) break;
181 if (i == MAX_FILES) {
182 printf("(%d) file table full for %s\n", line_count,
183 fname);
184 exit(1);
186 ftable[i].handle = handle;
187 ftable[i].fd = fd;
190 void nb_writex(int handle, int offset, int size, int ret_size)
192 int i;
193 NTSTATUS status;
195 if (buf[0] == 0) memset(buf, 1, sizeof(buf));
197 i = find_handle(handle);
198 status = cli_writeall(c, ftable[i].fd, 0, (uint8_t *)buf, offset, size,
199 NULL);
200 if (!NT_STATUS_IS_OK(status)) {
201 printf("(%d) ERROR: write failed on handle %d, fd %d "
202 "error %s\n", line_count, handle, ftable[i].fd,
203 nt_errstr(status));
204 exit(1);
207 children[nbio_id].bytes_out += ret_size;
210 void nb_readx(int handle, int offset, int size, int ret_size)
212 int i, ret;
214 i = find_handle(handle);
215 if ((ret=cli_read(c, ftable[i].fd, buf, offset, size)) != ret_size) {
216 printf("(%d) ERROR: read failed on handle %d ofs=%d size=%d res=%d fd %d errno %d (%s)\n",
217 line_count, handle, offset, size, ret, ftable[i].fd, errno, strerror(errno));
218 exit(1);
220 children[nbio_id].bytes_in += ret_size;
223 void nb_close(int handle)
225 int i;
226 i = find_handle(handle);
227 if (!NT_STATUS_IS_OK(cli_close(c, ftable[i].fd))) {
228 printf("(%d) close failed on handle %d\n", line_count, handle);
229 exit(1);
231 ftable[i].handle = 0;
234 void nb_rmdir(const char *fname)
236 if (!NT_STATUS_IS_OK(cli_rmdir(c, fname))) {
237 printf("ERROR: rmdir %s failed (%s)\n",
238 fname, cli_errstr(c));
239 exit(1);
243 void nb_rename(const char *oldname, const char *newname)
245 if (!NT_STATUS_IS_OK(cli_rename(c, oldname, newname))) {
246 printf("ERROR: rename %s %s failed (%s)\n",
247 oldname, newname, cli_errstr(c));
248 exit(1);
253 void nb_qpathinfo(const char *fname)
255 cli_qpathinfo1(c, fname, NULL, NULL, NULL, NULL, NULL);
258 void nb_qfileinfo(int fnum)
260 int i;
261 i = find_handle(fnum);
262 cli_qfileinfo_basic(c, ftable[i].fd, NULL, NULL, NULL, NULL, NULL,
263 NULL, NULL);
266 void nb_qfsinfo(int level)
268 int bsize, total, avail;
269 /* this is not the right call - we need cli_qfsinfo() */
270 cli_dskattr(c, &bsize, &total, &avail);
273 static NTSTATUS find_fn(const char *mnt, struct file_info *finfo, const char *name,
274 void *state)
276 /* noop */
277 return NT_STATUS_OK;
280 void nb_findfirst(const char *mask)
282 cli_list(c, mask, 0, find_fn, NULL);
285 void nb_flush(int fnum)
287 int i;
288 i = find_handle(fnum);
289 /* hmmm, we don't have cli_flush() yet */
292 static int total_deleted;
294 static NTSTATUS delete_fn(const char *mnt, struct file_info *finfo,
295 const char *name, void *state)
297 NTSTATUS status;
298 char *s, *n;
299 if (finfo->name[0] == '.') {
300 return NT_STATUS_OK;
303 n = SMB_STRDUP(name);
304 n[strlen(n)-1] = 0;
305 if (asprintf(&s, "%s%s", n, finfo->name) == -1) {
306 printf("asprintf failed\n");
307 return NT_STATUS_NO_MEMORY;
309 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
310 char *s2;
311 if (asprintf(&s2, "%s\\*", s) == -1) {
312 printf("asprintf failed\n");
313 return NT_STATUS_NO_MEMORY;
315 status = cli_list(c, s2, FILE_ATTRIBUTE_DIRECTORY, delete_fn, NULL);
316 if (!NT_STATUS_IS_OK(status)) {
317 free(n);
318 free(s2);
319 return status;
321 nb_rmdir(s);
322 } else {
323 total_deleted++;
324 nb_unlink(s);
326 free(s);
327 free(n);
328 return NT_STATUS_OK;
331 void nb_deltree(const char *dname)
333 char *mask;
334 if (asprintf(&mask, "%s\\*", dname) == -1) {
335 printf("asprintf failed\n");
336 return;
339 total_deleted = 0;
340 cli_list(c, mask, FILE_ATTRIBUTE_DIRECTORY, delete_fn, NULL);
341 free(mask);
342 cli_rmdir(c, dname);
344 if (total_deleted) printf("WARNING: Cleaned up %d files\n", total_deleted);
348 void nb_cleanup(void)
350 cli_rmdir(c, "clients");
351 children[nbio_id].done = 1;