summaryrefslogtreecommitdiff
path: root/source3/utils/torture.c
blob: 0eb9a256fcc09c1b64b1a08f29af5eb20eed5816 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   SMB torture tester
   Copyright (C) Andrew Tridgell 1997
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifdef SYSLOG
#undef SYSLOG
#endif

#include "includes.h"

static struct cli_state cli;
static fstring host, workgroup, share, password, username, myname;
static char *sockops="";


static struct timeval tp1,tp2;

static void start_timer()
{
	gettimeofday(&tp1,NULL);
}

static double end_timer()
{
	gettimeofday(&tp2,NULL);
	return((tp2.tv_sec - tp1.tv_sec) + 
	       (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
}


static int open_connection(void)
{
	if (!cli_initialise(&cli) || !cli_connect(&cli, host, NULL)) {
		printf("Failed to connect with %s\n", host);
	}

	if (!cli_session_request(&cli, host, 0x20, myname)) {
		printf("%s rejected the session\n",host);
		cli_shutdown(&cli);
		return -1;
	}

	if (!cli_negprot(&cli)) {
		printf("%s rejected the negprot (%s)\n",host, cli_errstr(&cli));
		cli_shutdown(&cli);
		return -1;
	}

	if (!cli_session_setup(&cli, username, password, strlen(password),
			       "", 0, workgroup)) {
		printf("%s rejected the sessionsetup (%s)\n", host, cli_errstr(&cli));
		cli_shutdown(&cli);
		return -1;
	}

	if (!cli_send_tconX(&cli, share, "A:", password, strlen(password)+1)) {
		printf("%s refused tree connect (%s)\n", host, cli_errstr(&cli));
		cli_shutdown(&cli);
		return -1;
	}

	return 0;
}



static void close_connection(void)
{
	if (!cli_tdis(&cli)) {
		printf("tdis failed (%s)\n", cli_errstr(&cli));
	}

	cli_shutdown(&cli);
}




static BOOL wait_lock(int fnum, uint32 offset, uint32 len)
{
	while (!cli_lock(&cli, fnum, offset, len, -1)) {
		int eclass, num;
		cli_error(&cli, &eclass, &num);
		if (eclass != ERRDOS || num != ERRlock) {
			printf("lock failed (%s)\n", 
			       cli_errstr(&cli));
			return False;
		}
	}
	return True;
}


static int rw_torture(int numops)
{
	char *lockfname = "\\torture.lck";
	fstring fname;
	int fnum;
	int fnum2;
	int pid2, pid = getpid();
	int i;

	fnum2 = cli_open(&cli, lockfname, O_RDWR | O_CREAT | O_EXCL, 
			 DENY_NONE);
	if (fnum2 == -1)
		fnum2 = cli_open(&cli, lockfname, O_RDWR, DENY_NONE);
	if (fnum2 == -1) {
		printf("open of %s failed (%s)\n", lockfname, cli_errstr(&cli));
		return -1;
	}


	for (i=0;i<numops;i++) {
		unsigned n = (unsigned)random()%10;
		if (i % 10 == 0) {
			printf("%d\r", i); fflush(stdout);
		}
		sprintf(fname,"\\torture.%u", n);

		if (!wait_lock(fnum2, n*sizeof(int), sizeof(int))) {
			return -1;
		}

		fnum = cli_open(&cli, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL);
		if (fnum == -1) {
			printf("open failed (%s)\n", cli_errstr(&cli));
			break;
		}

		if (cli_write(&cli, fnum, (char *)&pid, 0, sizeof(pid)) != sizeof(pid)) {
			printf("write failed (%s)\n", cli_errstr(&cli));
		}

		pid2 = 0;

		if (cli_read(&cli, fnum, (char *)&pid2, 0, sizeof(pid)) != sizeof(pid)) {
			printf("read failed (%s)\n", cli_errstr(&cli));
		}

		if (pid2 != pid) {
			printf("data corruption!\n");
		}

		if (!cli_close(&cli, fnum)) {
			printf("close failed (%s)\n", cli_errstr(&cli));
		}

		if (!cli_unlink(&cli, fname)) {
			printf("unlink failed (%s)\n", cli_errstr(&cli));
		}

		if (!cli_unlock(&cli, fnum2, n*sizeof(int), sizeof(int), -1)) {
			printf("unlock failed (%s)\n", cli_errstr(&cli));
		}
	}

	printf("%d\n", i);

	return 0;
}

static void usage(void)
{
	printf("Usage: smbtorture \\\\server\\share <options>\n");

	printf("\t-U user%%pass\n");
	printf("\t-N numprocs\n");
	printf("\t-n my_netbios_name\n");
	printf("\t-W workgroup\n");
	printf("\t-o num_operations\n");
	printf("\t-O socket_options\n");
	printf("\n");

	exit(1);
}



static void run_torture(int numops)
{
	if (open_connection() == 0) {
		cli_sockopt(&cli, sockops);

		printf("pid %d OK\n", getpid());

		rw_torture(numops);

		close_connection();
	}
}


static void create_procs(int nprocs, int numops)
{
	int i, status;

	for (i=0;i<nprocs;i++) {
		if (fork() == 0) {
			int mypid = getpid();
			srandom(mypid ^ time(NULL));
			run_torture(numops);
			_exit(0);
		}
	}

	for (i=0;i<nprocs;i++)
		waitpid(0, &status, 0);
}



/****************************************************************************
  main program
****************************************************************************/
 int main(int argc,char *argv[])
{
	int nprocs=1, numops=100;
	int opt;
	char *p;
	int gotpass = 0;
	extern char *optarg;
	extern int optind;
	extern FILE *dbf;

	dbf = stdout;

	charset_initialise();

	if (argc < 2) {
		usage();
	}

	if (strncmp(argv[1], "\\\\", 2)) {
		usage();
	}

	fstrcpy(host, &argv[1][2]);
	p = strchr(&host[2],'\\');
	if (!p) {
		usage();
	}
	*p = 0;
	fstrcpy(share, p+1);

	get_myname(myname,NULL);

	argc--;
	argv++;


	while ((opt = getopt(argc, argv, "hW:U:n:N:O:o:")) != EOF) {
		switch (opt) {
		case 'W':
			fstrcpy(workgroup,optarg);
			break;
		case 'N':
			nprocs = atoi(optarg);
			break;
		case 'o':
			numops = atoi(optarg);
			break;
		case 'O':
			sockops = optarg;
			break;
		case 'n':
			fstrcpy(myname, optarg);
			break;
		case 'U':
			strcpy(username,optarg);
			p = strchr(username,'%');
			if (p) {
				*p = 0;
				strcpy(password, p+1);
				gotpass = 1;
			}
			break;
		default:
			printf("Unknown option %c (%d)\n", (char)opt, opt);
			usage();
		}
	}


	while (!gotpass) {
		p = getpass("Password:");
		if (p) {
			strcpy(password, p);
			gotpass = 1;
		}
	}

	printf("host=%s share=%s user=%s myname=%s\n", 
	       host, share, username, myname);

	start_timer();
	create_procs(nprocs, numops);
	printf("rw_torture: %g secs\n", end_timer());

	return(0);
}