4 Unix SMB/CIFS implementation.
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/>.
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
;
34 static struct timeval nb_start
;
41 static struct children
{
42 double bytes_in
, bytes_out
;
47 double nbio_total(void)
51 for (i
=0;i
<nprocs
;i
++) {
52 total
+= children
[i
].bytes_out
+ children
[i
].bytes_in
;
57 void nb_alarm(int ignore
)
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
);
76 void nbio_shmem(int n
)
79 children
= (struct children
*)anonymous_shared_allocate(sizeof(*children
) * nprocs
);
81 printf("Failed to setup shared memory!\n");
87 static int ne_find_handle(int handle
)
90 children
[nbio_id
].line
= line_count
;
91 for (i
=0;i
<MAX_FILES
;i
++) {
92 if (ftable
[i
].handle
== handle
) return i
;
98 static int find_handle(int handle
)
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",
109 return -1; /* Not reached */
113 static struct cli_state
*c
;
115 static void sigsegv(int sig
)
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");
127 void nb_setup(struct cli_state
*cli
)
129 signal(SIGSEGV
, sigsegv
);
131 nb_start
= timeval_current();
132 children
[nbio_id
].done
= 0;
136 void nb_unlink(const char *fname
)
140 status
= cli_unlink(c
, fname
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
141 if (!NT_STATUS_IS_OK(status
)) {
143 printf("(%d) unlink %s failed (%s)\n",
144 line_count
, fname
, nt_errstr(status
));
150 void nb_createx(const char *fname
,
151 unsigned create_options
, unsigned create_disposition
, int handle
)
153 uint16_t fd
= (uint16_t)-1;
156 uint32_t desired_access
;
158 if (create_options
& FILE_DIRECTORY_FILE
) {
159 desired_access
= FILE_READ_DATA
;
161 desired_access
= FILE_READ_DATA
| FILE_WRITE_DATA
;
164 status
= cli_ntcreate(c
, fname
, 0,
167 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
169 create_options
, 0, &fd
, NULL
);
170 if (!NT_STATUS_IS_OK(status
) && handle
!= -1) {
171 printf("ERROR: cli_ntcreate failed for %s - %s\n",
172 fname
, nt_errstr(status
));
175 if (NT_STATUS_IS_OK(status
) && handle
== -1) {
176 printf("ERROR: cli_ntcreate succeeded for %s\n", fname
);
179 if (fd
== (uint16_t)-1) return;
181 for (i
=0;i
<MAX_FILES
;i
++) {
182 if (ftable
[i
].handle
== 0) break;
184 if (i
== MAX_FILES
) {
185 printf("(%d) file table full for %s\n", line_count
,
189 ftable
[i
].handle
= handle
;
193 void nb_writex(int handle
, int offset
, int size
, int ret_size
)
198 if (buf
[0] == 0) memset(buf
, 1, sizeof(buf
));
200 i
= find_handle(handle
);
201 status
= cli_writeall(c
, ftable
[i
].fd
, 0, (uint8_t *)buf
, offset
, size
,
203 if (!NT_STATUS_IS_OK(status
)) {
204 printf("(%d) ERROR: write failed on handle %d, fd %d "
205 "error %s\n", line_count
, handle
, ftable
[i
].fd
,
210 children
[nbio_id
].bytes_out
+= ret_size
;
213 void nb_readx(int handle
, int offset
, int size
, int ret_size
)
219 i
= find_handle(handle
);
220 status
= cli_read(c
, ftable
[i
].fd
, buf
, offset
, size
, &nread
);
221 if (!NT_STATUS_IS_OK(status
)) {
222 printf("(%d) ERROR: read failed on handle %d ofs=%d size=%d "
223 "fd %d nterror %s\n",
224 line_count
, handle
, offset
, size
,
225 ftable
[i
].fd
, nt_errstr(status
));
227 } else if (nread
!= ret_size
) {
228 printf("(%d) ERROR: read failed on handle %d ofs=%d size=%d "
229 "nread=%lu ret_size=%d fd %d\n",
230 line_count
, handle
, offset
, size
, (unsigned long)nread
,
231 ret_size
, ftable
[i
].fd
);
234 children
[nbio_id
].bytes_in
+= ret_size
;
237 void nb_close(int handle
)
240 i
= find_handle(handle
);
241 if (!NT_STATUS_IS_OK(cli_close(c
, ftable
[i
].fd
))) {
242 printf("(%d) close failed on handle %d\n", line_count
, handle
);
245 ftable
[i
].handle
= 0;
248 void nb_rmdir(const char *fname
)
252 status
= cli_rmdir(c
, fname
);
253 if (!NT_STATUS_IS_OK(status
)) {
254 printf("ERROR: rmdir %s failed (%s)\n",
255 fname
, nt_errstr(status
));
260 void nb_rename(const char *oldname
, const char *newname
)
264 status
= cli_rename(c
, oldname
, newname
);
265 if (!NT_STATUS_IS_OK(status
)) {
266 printf("ERROR: rename %s %s failed (%s)\n",
267 oldname
, newname
, nt_errstr(status
));
273 void nb_qpathinfo(const char *fname
)
275 cli_qpathinfo1(c
, fname
, NULL
, NULL
, NULL
, NULL
, NULL
);
278 void nb_qfileinfo(int fnum
)
281 i
= find_handle(fnum
);
282 cli_qfileinfo_basic(c
, ftable
[i
].fd
, NULL
, NULL
, NULL
, NULL
, NULL
,
286 void nb_qfsinfo(int level
)
288 uint64_t bsize
, total
, avail
;
289 /* this is not the right call - we need cli_qfsinfo() */
290 cli_disk_size(c
, &bsize
, &total
, &avail
);
293 static NTSTATUS
find_fn(const char *mnt
, struct file_info
*finfo
, const char *name
,
300 void nb_findfirst(const char *mask
)
302 cli_list(c
, mask
, 0, find_fn
, NULL
);
305 void nb_flush(int fnum
)
308 i
= find_handle(fnum
);
310 cli_flush(NULL
, c
, i
);
313 static int total_deleted
;
315 static NTSTATUS
delete_fn(const char *mnt
, struct file_info
*finfo
,
316 const char *name
, void *state
)
320 if (finfo
->name
[0] == '.') {
324 n
= SMB_STRDUP(name
);
326 if (asprintf(&s
, "%s%s", n
, finfo
->name
) == -1) {
328 printf("asprintf failed\n");
329 return NT_STATUS_NO_MEMORY
;
331 if (finfo
->mode
& FILE_ATTRIBUTE_DIRECTORY
) {
333 if (asprintf(&s2
, "%s\\*", s
) == -1) {
334 printf("asprintf failed\n");
337 return NT_STATUS_NO_MEMORY
;
339 status
= cli_list(c
, s2
, FILE_ATTRIBUTE_DIRECTORY
, delete_fn
, NULL
);
341 if (!NT_STATUS_IS_OK(status
)) {
356 void nb_deltree(const char *dname
)
359 if (asprintf(&mask
, "%s\\*", dname
) == -1) {
360 printf("asprintf failed\n");
365 cli_list(c
, mask
, FILE_ATTRIBUTE_DIRECTORY
, delete_fn
, NULL
);
369 if (total_deleted
) printf("WARNING: Cleaned up %d files\n", total_deleted
);
373 void nb_cleanup(void)
375 cli_rmdir(c
, "clients");
376 children
[nbio_id
].done
= 1;