9d8d5397276f00803ce8ba5ff0f2ddae7056dbae
[kai/samba.git] / source / utils / locktest.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4    mask_match tester
5    Copyright (C) Andrew Tridgell 1999
6    
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.
11    
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.
16    
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.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25
26 static fstring password;
27 static fstring username;
28 static fstring workgroup;
29 static int got_pass;
30 static int numops = 1000;
31 static BOOL showall;
32
33 #define FILENAME "locktest.dat"
34 #define LOCKRANGE 100
35
36 #define READ_PCT 50
37 #define LOCK_PCT 45
38 #define UNLOCK_PCT 45
39
40 struct preset {
41         int r1, r2;
42         int conn, f;
43         int start, len;
44         int rw;
45 } preset[] = {
46 {86, 37, 0, 1,  0, 3, WRITE_LOCK},
47 {46, 21, 0, 1,  1, 1, READ_LOCK},
48 {51, 35, 0, 0, 10, 1, WRITE_LOCK},
49 {69, 97, 0, 1,  0, 0, 0},
50 {35, 27, 1, 1,  0, 3, READ_LOCK},
51         };
52
53 /* each server has two connections open to it. Each connection has two file
54    descriptors open on the file - 8 file descriptors in total 
55
56    we then do random locking ops in tamdem on the 4 fnums from each
57    server and ensure that the results match
58  */
59 static void test_locks(struct cli_state *cli[2][2])
60 {
61         int fnum[2][2][2];
62         int server, conn, f, n; 
63
64         cli_unlink(cli[0][0], FILENAME);
65         cli_unlink(cli[1][0], FILENAME);
66
67         for (server=0;server<2;server++)
68         for (conn=0;conn<2;conn++)
69         for (f=0;f<2;f++) {
70                 fnum[server][conn][f] = cli_open(cli[server][conn], FILENAME,
71                                                  O_RDWR|O_CREAT,
72                                                  DENY_NONE);
73                 if (fnum[server][conn][f] == -1) {
74                         fprintf(stderr,"Failed to open fnum[%d][%d][%d]\n",
75                                 server, conn, f);
76                         return;
77                 }
78         }
79
80         for (n=0; n<numops; n++) {
81                 int start, len, op, r1, r2;
82                 BOOL ret1, ret2;
83
84                 if (n < sizeof(preset) / sizeof(preset[0])) {
85                         conn = preset[n].conn;
86                         f = preset[n].f;
87                         start = preset[n].start;
88                         len = preset[n].len;
89                         r1 = preset[n].r1;
90                         r2 = preset[n].r2;
91                 } else {
92                         conn = random() % 2;
93                         f = random() % 2;
94                         start = random() % (LOCKRANGE-1);
95                         len = 1 + random() % (LOCKRANGE-start);
96
97                         r1 = random() % 100;
98                         r2 = random() % 100;
99                 }
100
101                 if (r1 < READ_PCT) {
102                         op = READ_LOCK; 
103                 } else {
104                         op = WRITE_LOCK; 
105                 }
106
107
108                 if (r2 < LOCK_PCT) {
109                         /* set a lock */
110                         ret1 = cli_lock(cli[0][conn], 
111                                         fnum[0][conn][f],
112                                         start, len, 0, op);
113                         ret2 = cli_lock(cli[1][conn], 
114                                         fnum[1][conn][f],
115                                         start, len, 0, op);
116                         if (showall || ret1 != ret2) {
117                                 printf("%5d r1=%d r2=%d lock   conn=%d f=%d %d:%d op=%s -> %d:%d\n",
118                                        n, r1, r2, conn, f, start, len, op==READ_LOCK?"READ_LOCK":"WRITE_LOCK",
119                                        ret1, ret2);
120                         }
121                         if (ret1 != ret2) return;
122                 } else if (r2 < LOCK_PCT+UNLOCK_PCT) {
123                         /* unset a lock */
124                         /* set a lock */
125                         ret1 = cli_unlock(cli[0][conn], 
126                                           fnum[0][conn][f],
127                                           start, len);
128                         ret2 = cli_unlock(cli[1][conn], 
129                                           fnum[1][conn][f],
130                                           start, len);
131                         if (showall || ret1 != ret2) {
132                                 printf("%5d r1=%d r2=%d unlock conn=%d f=%d %d:%d       -> %d:%d\n",
133                                        n, r1, r2, conn, f, start, len,
134                                        ret1, ret2);
135                         }
136                 } else {
137                         /* reopen the file */
138                         cli_close(cli[0][conn], fnum[0][conn][f]);
139                         cli_close(cli[1][conn], fnum[1][conn][f]);
140                         fnum[0][conn][f] = cli_open(cli[0][conn], FILENAME,
141                                                     O_RDWR|O_CREAT,
142                                                     DENY_NONE);
143                         fnum[1][conn][f] = cli_open(cli[1][conn], FILENAME,
144                                                     O_RDWR|O_CREAT,
145                                                     DENY_NONE);
146                         if (fnum[0][conn][f] == -1) {
147                                 printf("failed to reopen on share1\n");
148                                 return;
149                         }
150                         if (fnum[1][conn][f] == -1) {
151                                 printf("failed to reopen on share2\n");
152                                 return;
153                         }
154                         if (showall) {
155                                 printf("%5d r1=%d r2=%d reopen conn=%d f=%d\n",
156                                        n, r1, r2, conn, f);
157                         }
158                 }
159                 if (n % 100 == 0) {
160                         printf("%d\n", n);
161                 }
162         }
163 }
164
165
166 /***************************************************** 
167 return a connection to a server
168 *******************************************************/
169 struct cli_state *connect_one(char *share)
170 {
171         struct cli_state *c;
172         struct nmb_name called, calling;
173         char *server_n;
174         fstring server;
175         struct in_addr ip;
176         extern struct in_addr ipzero;
177
178         fstrcpy(server,share+2);
179         share = strchr(server,'\\');
180         if (!share) return NULL;
181         *share = 0;
182         share++;
183
184         server_n = server;
185         
186         ip = ipzero;
187
188         make_nmb_name(&calling, "locktest", 0x0);
189         make_nmb_name(&called , server, 0x20);
190
191  again:
192         ip = ipzero;
193
194         /* have to open a new connection */
195         if (!(c=cli_initialise(NULL)) || (cli_set_port(c, 139) == 0) ||
196             !cli_connect(c, server_n, &ip)) {
197                 DEBUG(0,("Connection to %s failed\n", server_n));
198                 return NULL;
199         }
200
201         if (!cli_session_request(c, &calling, &called)) {
202                 DEBUG(0,("session request to %s failed\n", called.name));
203                 cli_shutdown(c);
204                 if (strcmp(called.name, "*SMBSERVER")) {
205                         make_nmb_name(&called , "*SMBSERVER", 0x20);
206                         goto again;
207                 }
208                 return NULL;
209         }
210
211         DEBUG(4,(" session request ok\n"));
212
213         if (!cli_negprot(c)) {
214                 DEBUG(0,("protocol negotiation failed\n"));
215                 cli_shutdown(c);
216                 return NULL;
217         }
218
219         if (!got_pass) {
220                 char *pass = getpass("Password: ");
221                 if (pass) {
222                         pstrcpy(password, pass);
223                 }
224         }
225
226         if (!cli_session_setup(c, username, 
227                                password, strlen(password),
228                                password, strlen(password),
229                                workgroup)) {
230                 DEBUG(0,("session setup failed: %s\n", cli_errstr(c)));
231                 return NULL;
232         }
233
234         /*
235          * These next two lines are needed to emulate
236          * old client behaviour for people who have
237          * scripts based on client output.
238          * QUESTION ? Do we want to have a 'client compatibility
239          * mode to turn these on/off ? JRA.
240          */
241
242         if (*c->server_domain || *c->server_os || *c->server_type)
243                 DEBUG(1,("Domain=[%s] OS=[%s] Server=[%s]\n",
244                         c->server_domain,c->server_os,c->server_type));
245         
246         DEBUG(4,(" session setup ok\n"));
247
248         if (!cli_send_tconX(c, share, "?????",
249                             password, strlen(password)+1)) {
250                 DEBUG(0,("tree connect failed: %s\n", cli_errstr(c)));
251                 cli_shutdown(c);
252                 return NULL;
253         }
254
255         DEBUG(4,(" tconx ok\n"));
256
257         return c;
258 }
259
260
261 static void usage(void)
262 {
263         printf(
264 "Usage:\n\
265   locktest //server1/share1 //server2/share2 [options..]\n\
266   options:\n\
267         -U user%%pass\n\
268         -s seed\n\
269         -o numops\n\
270         -a          (show all ops)\n\
271 ");
272 }
273
274 /****************************************************************************
275   main program
276 ****************************************************************************/
277  int main(int argc,char *argv[])
278 {
279         char *share1, *share2;
280         struct cli_state *cli[2][2];
281         extern char *optarg;
282         extern int optind;
283         extern FILE *dbf;
284         int opt;
285         char *p;
286         int seed;
287         static pstring servicesf = CONFIGFILE;
288
289         setlinebuf(stdout);
290
291         dbf = stderr;
292
293         if (argv[1][0] == '-' || argc < 3) {
294                 usage();
295                 exit(1);
296         }
297
298         share1 = argv[1];
299         share2 = argv[2];
300
301         all_string_sub(share1,"/","\\",0);
302         all_string_sub(share2,"/","\\",0);
303
304         setup_logging(argv[0],True);
305
306         argc -= 2;
307         argv += 2;
308
309         TimeInit();
310         charset_initialise();
311
312         lp_load(servicesf,True,False,False);
313         load_interfaces();
314
315         if (getenv("USER")) {
316                 pstrcpy(username,getenv("USER"));
317         }
318
319         seed = time(NULL);
320
321         while ((opt = getopt(argc, argv, "U:s:ho:a")) != EOF) {
322                 switch (opt) {
323                 case 'U':
324                         pstrcpy(username,optarg);
325                         p = strchr(username,'%');
326                         if (p) {
327                                 *p = 0;
328                                 pstrcpy(password, p+1);
329                                 got_pass = 1;
330                         }
331                         break;
332                 case 's':
333                         seed = atoi(optarg);
334                         break;
335                 case 'o':
336                         numops = atoi(optarg);
337                         break;
338                 case 'a':
339                         showall = True;
340                         break;
341                 case 'h':
342                         usage();
343                         exit(1);
344                 default:
345                         printf("Unknown option %c (%d)\n", (char)opt, opt);
346                         exit(1);
347                 }
348         }
349
350         argc -= optind;
351         argv += optind;
352
353         DEBUG(0,("seed=%d\n", seed));
354         srandom(seed);
355
356         cli[0][0] = connect_one(share1);
357         cli[0][1] = connect_one(share1);
358         if (!cli[0][0] || !cli[0][1]) {
359                 DEBUG(0,("Failed to connect to %s\n", share1));
360                 exit(1);
361         }
362
363         cli[1][0] = connect_one(share2);
364         cli[1][1] = connect_one(share2);
365         if (!cli[1][0] || !cli[1][1]) {
366                 DEBUG(0,("Failed to connect to %s\n", share2));
367                 exit(1);
368         }
369
370         test_locks(cli);
371
372         return(0);
373 }