2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1997
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 static struct cli_state cli
;
29 static fstring host
, workgroup
, share
, password
, username
, myname
;
30 static char *sockops
="";
33 static struct timeval tp1
,tp2
;
35 static void start_timer()
37 gettimeofday(&tp1
,NULL
);
40 static double end_timer()
42 gettimeofday(&tp2
,NULL
);
43 return((tp2
.tv_sec
- tp1
.tv_sec
) +
44 (tp2
.tv_usec
- tp1
.tv_usec
)*1.0e-6);
48 static int open_connection(void)
50 if (!cli_initialise(&cli
) || !cli_connect(&cli
, host
, NULL
)) {
51 printf("Failed to connect with %s\n", host
);
54 if (!cli_session_request(&cli
, host
, 0x20, myname
)) {
55 printf("%s rejected the session\n",host
);
60 if (!cli_negprot(&cli
)) {
61 printf("%s rejected the negprot (%s)\n",host
, cli_errstr(&cli
));
66 if (!cli_session_setup(&cli
, username
, password
, strlen(password
),
68 printf("%s rejected the sessionsetup (%s)\n", host
, cli_errstr(&cli
));
73 if (!cli_send_tconX(&cli
, share
, "A:", password
, strlen(password
)+1)) {
74 printf("%s refused tree connect (%s)\n", host
, cli_errstr(&cli
));
84 static void close_connection(void)
86 if (!cli_tdis(&cli
)) {
87 printf("tdis failed (%s)\n", cli_errstr(&cli
));
96 static BOOL
wait_lock(int fnum
, uint32 offset
, uint32 len
)
98 while (!cli_lock(&cli
, fnum
, offset
, len
, -1)) {
100 cli_error(&cli
, &eclass
, &num
);
101 if (eclass
!= ERRDOS
|| num
!= ERRlock
) {
102 printf("lock failed (%s)\n",
111 static int rw_torture(int numops
)
113 char *lockfname
= "\\torture.lck";
117 int pid2
, pid
= getpid();
120 fnum2
= cli_open(&cli
, lockfname
, O_RDWR
| O_CREAT
| O_EXCL
,
123 fnum2
= cli_open(&cli
, lockfname
, O_RDWR
, DENY_NONE
);
125 printf("open of %s failed (%s)\n", lockfname
, cli_errstr(&cli
));
130 for (i
=0;i
<numops
;i
++) {
131 unsigned n
= (unsigned)random()%10;
133 printf("%d\r", i
); fflush(stdout
);
135 sprintf(fname
,"\\torture.%u", n
);
137 if (!wait_lock(fnum2
, n
*sizeof(int), sizeof(int))) {
141 fnum
= cli_open(&cli
, fname
, O_RDWR
| O_CREAT
| O_TRUNC
, DENY_ALL
);
143 printf("open failed (%s)\n", cli_errstr(&cli
));
147 if (cli_write(&cli
, fnum
, (char *)&pid
, 0, sizeof(pid
)) != sizeof(pid
)) {
148 printf("write failed (%s)\n", cli_errstr(&cli
));
153 if (cli_read(&cli
, fnum
, (char *)&pid2
, 0, sizeof(pid
)) != sizeof(pid
)) {
154 printf("read failed (%s)\n", cli_errstr(&cli
));
158 printf("data corruption!\n");
161 if (!cli_close(&cli
, fnum
)) {
162 printf("close failed (%s)\n", cli_errstr(&cli
));
165 if (!cli_unlink(&cli
, fname
)) {
166 printf("unlink failed (%s)\n", cli_errstr(&cli
));
169 if (!cli_unlock(&cli
, fnum2
, n
*sizeof(int), sizeof(int), -1)) {
170 printf("unlock failed (%s)\n", cli_errstr(&cli
));
179 static void usage(void)
181 printf("Usage: smbtorture \\\\server\\share <options>\n");
183 printf("\t-U user%%pass\n");
184 printf("\t-N numprocs\n");
185 printf("\t-n my_netbios_name\n");
186 printf("\t-W workgroup\n");
187 printf("\t-o num_operations\n");
188 printf("\t-O socket_options\n");
196 static void run_torture(int numops
)
198 if (open_connection() == 0) {
199 cli_sockopt(&cli
, sockops
);
201 printf("pid %d OK\n", getpid());
210 static void create_procs(int nprocs
, int numops
)
214 for (i
=0;i
<nprocs
;i
++) {
216 int mypid
= getpid();
217 srandom(mypid
^ time(NULL
));
223 for (i
=0;i
<nprocs
;i
++)
224 waitpid(0, &status
, 0);
229 /****************************************************************************
231 ****************************************************************************/
232 int main(int argc
,char *argv
[])
234 int nprocs
=1, numops
=100;
244 charset_initialise();
250 if (strncmp(argv
[1], "\\\\", 2)) {
254 fstrcpy(host
, &argv
[1][2]);
255 p
= strchr(&host
[2],'\\');
262 get_myname(myname
,NULL
);
268 while ((opt
= getopt(argc
, argv
, "hW:U:n:N:O:o:")) != EOF
) {
271 fstrcpy(workgroup
,optarg
);
274 nprocs
= atoi(optarg
);
277 numops
= atoi(optarg
);
283 fstrcpy(myname
, optarg
);
286 strcpy(username
,optarg
);
287 p
= strchr(username
,'%');
290 strcpy(password
, p
+1);
295 printf("Unknown option %c (%d)\n", (char)opt
, opt
);
302 p
= getpass("Password:");
309 printf("host=%s share=%s user=%s myname=%s\n",
310 host
, share
, username
, myname
);
313 create_procs(nprocs
, numops
);
314 printf("rw_torture: %g secs\n", end_timer());