r14527: Fix build problems.
[kai/samba.git] / source4 / torture / torture.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Andrew Tridgell 1997-2003
5    Copyright (C) Jelmer Vernooij 2006
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 #include "includes.h"
23 #include "lib/cmdline/popt_common.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "system/time.h"
26 #include "system/wait.h"
27 #include "system/filesys.h"
28 #include "libcli/raw/ioctl.h"
29 #include "libcli/libcli.h"
30 #include "lib/ldb/include/ldb.h"
31 #include "lib/events/events.h"
32 #include "libcli/resolve/resolve.h"
33 #include "auth/credentials/credentials.h"
34 #include "libcli/ldap/ldap_client.h"
35 #include "librpc/gen_ndr/ndr_nbt.h"
36
37 #include "torture/raw/proto.h"
38 #include "torture/smb2/proto.h"
39 #include "torture/rap/proto.h"
40 #include "torture/auth/proto.h"
41 #include "torture/local/proto.h"
42 #include "torture/nbench/proto.h"
43 #include "torture/ldap/proto.h"
44 #include "torture/com/proto.h"
45 #include "torture/nbt/proto.h"
46 #include "torture/libnet/proto.h"
47 #include "torture/torture.h"
48 #include "torture/util.h"
49 #include "build.h"
50 #include "dlinklist.h"
51
52 _PUBLIC_ int torture_nprocs=4;
53 _PUBLIC_ int torture_numops=10;
54 _PUBLIC_ int torture_entries=1000;
55 _PUBLIC_ int torture_failures=1;
56 _PUBLIC_ int torture_seed=0;
57 _PUBLIC_ BOOL use_oplocks;
58 static int procnum; /* records process count number when forking */
59 static struct smbcli_state *current_cli;
60 _PUBLIC_ BOOL use_level_II_oplocks;
61 _PUBLIC_ BOOL torture_showall = False;
62
63
64
65
66 static BOOL wait_lock(struct smbcli_state *c, int fnum, uint32_t offset, uint32_t len)
67 {
68         while (NT_STATUS_IS_ERR(smbcli_lock(c->tree, fnum, offset, len, -1, WRITE_LOCK))) {
69                 if (!check_error(__location__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
70         }
71         return True;
72 }
73
74
75 static BOOL rw_torture(struct smbcli_state *c)
76 {
77         const char *lockfname = "\\torture.lck";
78         char *fname;
79         int fnum;
80         int fnum2;
81         pid_t pid2, pid = getpid();
82         int i, j;
83         uint8_t buf[1024];
84         BOOL correct = True;
85
86         fnum2 = smbcli_open(c->tree, lockfname, O_RDWR | O_CREAT | O_EXCL, 
87                          DENY_NONE);
88         if (fnum2 == -1)
89                 fnum2 = smbcli_open(c->tree, lockfname, O_RDWR, DENY_NONE);
90         if (fnum2 == -1) {
91                 printf("open of %s failed (%s)\n", lockfname, smbcli_errstr(c->tree));
92                 return False;
93         }
94
95
96         for (i=0;i<torture_numops;i++) {
97                 uint_t n = (uint_t)random()%10;
98                 if (i % 10 == 0) {
99                         printf("%d\r", i); fflush(stdout);
100                 }
101                 asprintf(&fname, "\\torture.%u", n);
102
103                 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
104                         return False;
105                 }
106
107                 fnum = smbcli_open(c->tree, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL);
108                 if (fnum == -1) {
109                         printf("open failed (%s)\n", smbcli_errstr(c->tree));
110                         correct = False;
111                         break;
112                 }
113
114                 if (smbcli_write(c->tree, fnum, 0, &pid, 0, sizeof(pid)) != sizeof(pid)) {
115                         printf("write failed (%s)\n", smbcli_errstr(c->tree));
116                         correct = False;
117                 }
118
119                 for (j=0;j<50;j++) {
120                         if (smbcli_write(c->tree, fnum, 0, buf, 
121                                       sizeof(pid)+(j*sizeof(buf)), 
122                                       sizeof(buf)) != sizeof(buf)) {
123                                 printf("write failed (%s)\n", smbcli_errstr(c->tree));
124                                 correct = False;
125                         }
126                 }
127
128                 pid2 = 0;
129
130                 if (smbcli_read(c->tree, fnum, &pid2, 0, sizeof(pid)) != sizeof(pid)) {
131                         printf("read failed (%s)\n", smbcli_errstr(c->tree));
132                         correct = False;
133                 }
134
135                 if (pid2 != pid) {
136                         printf("data corruption!\n");
137                         correct = False;
138                 }
139
140                 if (NT_STATUS_IS_ERR(smbcli_close(c->tree, fnum))) {
141                         printf("close failed (%s)\n", smbcli_errstr(c->tree));
142                         correct = False;
143                 }
144
145                 if (NT_STATUS_IS_ERR(smbcli_unlink(c->tree, fname))) {
146                         printf("unlink failed (%s)\n", smbcli_errstr(c->tree));
147                         correct = False;
148                 }
149
150                 if (NT_STATUS_IS_ERR(smbcli_unlock(c->tree, fnum2, n*sizeof(int), sizeof(int)))) {
151                         printf("unlock failed (%s)\n", smbcli_errstr(c->tree));
152                         correct = False;
153                 }
154                 free(fname);
155         }
156
157         smbcli_close(c->tree, fnum2);
158         smbcli_unlink(c->tree, lockfname);
159
160         printf("%d\n", i);
161
162         return correct;
163 }
164
165 static BOOL run_torture(struct smbcli_state *cli, int dummy)
166 {
167         BOOL ret;
168
169         ret = rw_torture(cli);
170         
171         if (!torture_close_connection(cli)) {
172                 ret = False;
173         }
174
175         return ret;
176 }
177
178
179 /*
180   see how many RPC pipes we can open at once
181 */
182 static BOOL run_pipe_number(void)
183 {
184         struct smbcli_state *cli1;
185         const char *pipe_name = "\\WKSSVC";
186         int fnum;
187         int num_pipes = 0;
188
189         printf("starting pipenumber test\n");
190         if (!torture_open_connection(&cli1)) {
191                 return False;
192         }
193
194         while(1) {
195                 fnum = smbcli_nt_create_full(cli1->tree, pipe_name, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
196                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, NTCREATEX_DISP_OPEN_IF, 0, 0);
197
198                 if (fnum == -1) {
199                         printf("Open of pipe %s failed with error (%s)\n", pipe_name, smbcli_errstr(cli1->tree));
200                         break;
201                 }
202                 num_pipes++;
203                 printf("%d\r", num_pipes);
204                 fflush(stdout);
205         }
206
207         printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
208         torture_close_connection(cli1);
209         return True;
210 }
211
212
213
214
215 /*
216   open N connections to the server and just hold them open
217   used for testing performance when there are N idle users
218   already connected
219  */
220  static BOOL torture_holdcon(void)
221 {
222         int i;
223         struct smbcli_state **cli;
224         int num_dead = 0;
225
226         printf("Opening %d connections\n", torture_numops);
227         
228         cli = malloc_array_p(struct smbcli_state *, torture_numops);
229
230         for (i=0;i<torture_numops;i++) {
231                 if (!torture_open_connection(&cli[i])) {
232                         return False;
233                 }
234                 printf("opened %d connections\r", i);
235                 fflush(stdout);
236         }
237
238         printf("\nStarting pings\n");
239
240         while (1) {
241                 for (i=0;i<torture_numops;i++) {
242                         NTSTATUS status;
243                         if (cli[i]) {
244                                 status = smbcli_chkpath(cli[i]->tree, "\\");
245                                 if (!NT_STATUS_IS_OK(status)) {
246                                         printf("Connection %d is dead\n", i);
247                                         cli[i] = NULL;
248                                         num_dead++;
249                                 }
250                                 usleep(100);
251                         }
252                 }
253
254                 if (num_dead == torture_numops) {
255                         printf("All connections dead - finishing\n");
256                         break;
257                 }
258
259                 printf(".");
260                 fflush(stdout);
261         }
262
263         return True;
264 }
265
266 /*
267 test how many open files this server supports on the one socket
268 */
269 static BOOL run_maxfidtest(struct smbcli_state *cli, int dummy)
270 {
271 #define MAXFID_TEMPLATE "\\maxfid\\fid%d\\maxfid.%d.%d"
272         char *fname;
273         int fnums[0x11000], i;
274         int retries=4, maxfid;
275         BOOL correct = True;
276
277         if (retries <= 0) {
278                 printf("failed to connect\n");
279                 return False;
280         }
281
282         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
283                 printf("Failed to deltree \\maxfid - %s\n",
284                        smbcli_errstr(cli->tree));
285                 return False;
286         }
287         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\maxfid"))) {
288                 printf("Failed to mkdir \\maxfid, error=%s\n", 
289                        smbcli_errstr(cli->tree));
290                 return False;
291         }
292
293         printf("Testing maximum number of open files\n");
294
295         for (i=0; i<0x11000; i++) {
296                 if (i % 1000 == 0) {
297                         asprintf(&fname, "\\maxfid\\fid%d", i/1000);
298                         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, fname))) {
299                                 printf("Failed to mkdir %s, error=%s\n", 
300                                        fname, smbcli_errstr(cli->tree));
301                                 return False;
302                         }
303                         free(fname);
304                 }
305                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
306                 if ((fnums[i] = smbcli_open(cli->tree, fname, 
307                                         O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
308                     -1) {
309                         printf("open of %s failed (%s)\n", 
310                                fname, smbcli_errstr(cli->tree));
311                         printf("maximum fnum is %d\n", i);
312                         break;
313                 }
314                 free(fname);
315                 printf("%6d\r", i);
316         }
317         printf("%6d\n", i);
318         i--;
319
320         maxfid = i;
321
322         printf("cleaning up\n");
323         for (i=0;i<maxfid/2;i++) {
324                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
325                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[i]))) {
326                         printf("Close of fnum %d failed - %s\n", fnums[i], smbcli_errstr(cli->tree));
327                 }
328                 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
329                         printf("unlink of %s failed (%s)\n", 
330                                fname, smbcli_errstr(cli->tree));
331                         correct = False;
332                 }
333                 free(fname);
334
335                 asprintf(&fname, MAXFID_TEMPLATE, (maxfid-i)/1000, maxfid-i,(int)getpid());
336                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[maxfid-i]))) {
337                         printf("Close of fnum %d failed - %s\n", fnums[maxfid-i], smbcli_errstr(cli->tree));
338                 }
339                 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
340                         printf("unlink of %s failed (%s)\n", 
341                                fname, smbcli_errstr(cli->tree));
342                         correct = False;
343                 }
344                 free(fname);
345
346                 printf("%6d %6d\r", i, maxfid-i);
347         }
348         printf("%6d\n", 0);
349
350         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
351                 printf("Failed to deltree \\maxfid - %s\n",
352                        smbcli_errstr(cli->tree));
353                 return False;
354         }
355
356         printf("maxfid test finished\n");
357         if (!torture_close_connection(cli)) {
358                 correct = False;
359         }
360         return correct;
361 #undef MAXFID_TEMPLATE
362 }
363
364
365
366 /*
367   sees what IOCTLs are supported
368  */
369 static BOOL torture_ioctl_test(void)
370 {
371         struct smbcli_state *cli;
372         uint16_t device, function;
373         int fnum;
374         const char *fname = "\\ioctl.dat";
375         NTSTATUS status;
376         union smb_ioctl parms;
377         TALLOC_CTX *mem_ctx;
378
379         if (!torture_open_connection(&cli)) {
380                 return False;
381         }
382
383         mem_ctx = talloc_init("ioctl_test");
384
385         printf("starting ioctl test\n");
386
387         smbcli_unlink(cli->tree, fname);
388
389         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
390         if (fnum == -1) {
391                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
392                 return False;
393         }
394
395         parms.ioctl.level = RAW_IOCTL_IOCTL;
396         parms.ioctl.in.file.fnum = fnum;
397         parms.ioctl.in.request = IOCTL_QUERY_JOB_INFO;
398         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
399         printf("ioctl job info: %s\n", smbcli_errstr(cli->tree));
400
401         for (device=0;device<0x100;device++) {
402                 printf("testing device=0x%x\n", device);
403                 for (function=0;function<0x100;function++) {
404                         parms.ioctl.in.request = (device << 16) | function;
405                         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
406
407                         if (NT_STATUS_IS_OK(status)) {
408                                 printf("ioctl device=0x%x function=0x%x OK : %d bytes\n", 
409                                         device, function, (int)parms.ioctl.out.blob.length);
410                         }
411                 }
412         }
413
414         if (!torture_close_connection(cli)) {
415                 return False;
416         }
417
418         return True;
419 }
420
421
422 static void sigcont(int sig)
423 {
424 }
425
426 double torture_create_procs(BOOL (*fn)(struct smbcli_state *, int), BOOL *result)
427 {
428         int i, status;
429         volatile pid_t *child_status;
430         volatile BOOL *child_status_out;
431         int synccount;
432         int tries = 8;
433         double start_time_limit = 10 + (torture_nprocs * 1.5);
434         char **unc_list = NULL;
435         const char *p;
436         int num_unc_names = 0;
437         struct timeval tv;
438
439         *result = True;
440
441         synccount = 0;
442
443         signal(SIGCONT, sigcont);
444
445         child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*torture_nprocs);
446         if (!child_status) {
447                 printf("Failed to setup shared memory\n");
448                 return -1;
449         }
450
451         child_status_out = (volatile BOOL *)shm_setup(sizeof(BOOL)*torture_nprocs);
452         if (!child_status_out) {
453                 printf("Failed to setup result status shared memory\n");
454                 return -1;
455         }
456
457         p = lp_parm_string(-1, "torture", "unclist");
458         if (p) {
459                 unc_list = file_lines_load(p, &num_unc_names, NULL);
460                 if (!unc_list || num_unc_names <= 0) {
461                         printf("Failed to load unc names list from '%s'\n", p);
462                         exit(1);
463                 }
464         }
465
466         for (i = 0; i < torture_nprocs; i++) {
467                 child_status[i] = 0;
468                 child_status_out[i] = True;
469         }
470
471         tv = timeval_current();
472
473         for (i=0;i<torture_nprocs;i++) {
474                 procnum = i;
475                 if (fork() == 0) {
476                         char *myname;
477                         char *hostname=NULL, *sharename;
478
479                         pid_t mypid = getpid();
480                         srandom(((int)mypid) ^ ((int)time(NULL)));
481
482                         asprintf(&myname, "CLIENT%d", i);
483                         lp_set_cmdline("netbios name", myname);
484                         free(myname);
485
486
487                         if (unc_list) {
488                                 if (!smbcli_parse_unc(unc_list[i % num_unc_names],
489                                                       NULL, &hostname, &sharename)) {
490                                         printf("Failed to parse UNC name %s\n",
491                                                unc_list[i % num_unc_names]);
492                                         exit(1);
493                                 }
494                         }
495
496                         while (1) {
497                                 if (hostname) {
498                                         if (torture_open_connection_share(NULL,
499                                                                           &current_cli,
500                                                                           hostname, 
501                                                                           sharename,
502                                                                           NULL)) {
503                                                 break;
504                                         }
505                                 } else if (torture_open_connection(&current_cli)) {
506                                                 break;
507                                 }
508                                 if (tries-- == 0) {
509                                         printf("pid %d failed to start\n", (int)getpid());
510                                         _exit(1);
511                                 }
512                                 msleep(100);    
513                         }
514
515                         child_status[i] = getpid();
516
517                         pause();
518
519                         if (child_status[i]) {
520                                 printf("Child %d failed to start!\n", i);
521                                 child_status_out[i] = 1;
522                                 _exit(1);
523                         }
524
525                         child_status_out[i] = fn(current_cli, i);
526                         _exit(0);
527                 }
528         }
529
530         do {
531                 synccount = 0;
532                 for (i=0;i<torture_nprocs;i++) {
533                         if (child_status[i]) synccount++;
534                 }
535                 if (synccount == torture_nprocs) break;
536                 msleep(100);
537         } while (timeval_elapsed(&tv) < start_time_limit);
538
539         if (synccount != torture_nprocs) {
540                 printf("FAILED TO START %d CLIENTS (started %d)\n", torture_nprocs, synccount);
541                 *result = False;
542                 return timeval_elapsed(&tv);
543         }
544
545         printf("Starting %d clients\n", torture_nprocs);
546
547         /* start the client load */
548         tv = timeval_current();
549         for (i=0;i<torture_nprocs;i++) {
550                 child_status[i] = 0;
551         }
552
553         printf("%d clients started\n", torture_nprocs);
554
555         kill(0, SIGCONT);
556
557         for (i=0;i<torture_nprocs;i++) {
558                 int ret;
559                 while ((ret=waitpid(0, &status, 0)) == -1 && errno == EINTR) /* noop */ ;
560                 if (ret == -1 || WEXITSTATUS(status) != 0) {
561                         *result = False;
562                 }
563         }
564
565         printf("\n");
566         
567         for (i=0;i<torture_nprocs;i++) {
568                 if (!child_status_out[i]) {
569                         *result = False;
570                 }
571         }
572         return timeval_elapsed(&tv);
573 }
574
575 #define FLAG_MULTIPROC 1
576
577 static struct {
578         const char *name;
579         BOOL (*fn)(void);
580         BOOL (*multi_fn)(struct smbcli_state *, int );
581 } builtin_torture_ops[] = {
582         /* benchmarking tests */
583         {"BENCH-HOLDCON",  torture_holdcon, 0},
584         {"BENCH-NBENCH",  torture_nbench, 0},
585         {"BENCH-TORTURE", NULL, run_torture},
586         {"BENCH-NBT",     torture_bench_nbt, 0},
587         {"BENCH-WINS",    torture_bench_wins, 0},
588         {"BENCH-CLDAP",   torture_bench_cldap, 0},
589
590         /* RAW smb tests */
591         {"RAW-QFSINFO", torture_raw_qfsinfo, 0},
592         {"RAW-QFILEINFO", torture_raw_qfileinfo, 0},
593         {"RAW-SFILEINFO", torture_raw_sfileinfo, 0},
594         {"RAW-SFILEINFO-BUG", torture_raw_sfileinfo_bug, 0},
595         {"RAW-SEARCH", torture_raw_search, 0},
596         {"RAW-CLOSE", torture_raw_close, 0},
597         {"RAW-OPEN", torture_raw_open, 0},
598         {"RAW-MKDIR", torture_raw_mkdir, 0},
599         {"RAW-OPLOCK", torture_raw_oplock, 0},
600         {"RAW-NOTIFY", torture_raw_notify, 0},
601         {"RAW-MUX", torture_raw_mux, 0},
602         {"RAW-IOCTL", torture_raw_ioctl, 0},
603         {"RAW-CHKPATH", torture_raw_chkpath, 0},
604         {"RAW-UNLINK", torture_raw_unlink, 0},
605         {"RAW-READ", torture_raw_read, 0},
606         {"RAW-WRITE", torture_raw_write, 0},
607         {"RAW-LOCK", torture_raw_lock, 0},
608         {"RAW-CONTEXT", torture_raw_context, 0},
609         {"RAW-RENAME", torture_raw_rename, 0},
610         {"RAW-SEEK", torture_raw_seek, 0},
611         {"RAW-EAS", torture_raw_eas, 0},
612         {"RAW-EAMAX", torture_max_eas, 0},
613         {"RAW-STREAMS", torture_raw_streams, 0},
614         {"RAW-ACLS", torture_raw_acls, 0},
615         {"RAW-RAP", torture_raw_rap, 0},
616         {"RAW-COMPOSITE", torture_raw_composite, 0},
617
618         /* SMB2 tests */
619         {"SMB2-CONNECT", torture_smb2_connect, 0},
620         {"SMB2-SCAN", torture_smb2_scan, 0},
621         {"SMB2-SCANGETINFO", torture_smb2_getinfo_scan, 0},
622         {"SMB2-SCANSETINFO", torture_smb2_setinfo_scan, 0},
623         {"SMB2-SCANFIND", torture_smb2_find_scan, 0},
624         {"SMB2-GETINFO", torture_smb2_getinfo, 0},
625         {"SMB2-SETINFO", torture_smb2_setinfo, 0},
626         {"SMB2-FIND", torture_smb2_find, 0},
627
628         /* protocol scanners */
629         {"SCAN-MAXFID", NULL, run_maxfidtest},
630         {"SCAN-PIPE_NUMBER", run_pipe_number, 0},
631         {"SCAN-IOCTL",  torture_ioctl_test, 0},
632         {"SCAN-RAP",  torture_rap_scan, 0},
633
634         /* local (no server) testers */
635         {"LOCAL-NTLMSSP", torture_ntlmssp_self_check, 0},
636         {"LOCAL-ICONV", torture_local_iconv, 0},
637         {"LOCAL-TALLOC", torture_local_talloc, 0},
638         {"LOCAL-MESSAGING", torture_local_messaging, 0},
639         {"LOCAL-IRPC",  torture_local_irpc, 0},
640         {"LOCAL-BINDING", torture_local_binding_string, 0},
641         {"LOCAL-STRLIST", torture_local_util_strlist, 0},
642         {"LOCAL-FILE", torture_local_util_file, 0},
643         {"LOCAL-IDTREE", torture_local_idtree, 0},
644         {"LOCAL-SOCKET", torture_local_socket, 0},
645         {"LOCAL-PAC", torture_pac, 0},
646         {"LOCAL-REGISTRY", torture_registry, 0},
647         {"LOCAL-RESOLVE", torture_local_resolve, 0},
648         {"LOCAL-SDDL", torture_local_sddl, 0},
649         {"LOCAL-NDR", torture_local_ndr, 0},
650
651         /* ldap testers */
652         {"LDAP-BASIC", torture_ldap_basic, 0},
653         {"LDAP-CLDAP", torture_cldap, 0},
654
655         /* nbt tests */
656         {"NBT-REGISTER", torture_nbt_register, 0},
657         {"NBT-WINS", torture_nbt_wins, 0},
658         {"NBT-DGRAM", torture_nbt_dgram, 0},
659         {"NBT-BROWSE", torture_nbt_browse, 0},
660         {"NBT-WINSREPLICATION-SIMPLE", torture_nbt_winsreplication_simple, 0},
661         {"NBT-WINSREPLICATION-REPLICA", torture_nbt_winsreplication_replica, 0},
662         {"NBT-WINSREPLICATION-OWNED", torture_nbt_winsreplication_owned, 0},
663         
664         {NULL, NULL, 0}};
665
666 static void register_builtin_ops(void)
667 {
668         int i;
669         for (i = 0; builtin_torture_ops[i].name; i++) {
670                 register_torture_op(builtin_torture_ops[i].name, 
671                                                         builtin_torture_ops[i].fn, 
672                                                         builtin_torture_ops[i].multi_fn);
673         }
674 }
675
676 struct torture_op *torture_ops = NULL;
677
678 _PUBLIC_ NTSTATUS register_torture_op(const char *name, BOOL (*fn)(void), BOOL (*multi_fn)(struct smbcli_state *, int ))
679 {
680         struct torture_op *op, *p;
681         
682         op = talloc(talloc_autofree_context(), struct torture_op);
683
684         op->name = talloc_strdup(op, name);
685         op->fn = fn;
686         op->multi_fn = multi_fn;
687
688         for (p = torture_ops; p; p = p->next) {
689                 if (strcmp(p->name, op->name) == 0) {
690                         /* Check for duplicates */
691                         DEBUG(0,("There already is a torture op registered with the name %s!\n", name));
692                         talloc_free(op);
693                         return NT_STATUS_OBJECT_NAME_COLLISION;
694                 }
695
696                 if (strcmp(p->name, op->name) < 0 && 
697                         (!p->next || strcmp(p->next->name, op->name) > 0)) {
698                         DLIST_ADD_AFTER(torture_ops, op, p);
699                         return NT_STATUS_OK;
700                 }
701         }
702
703         DLIST_ADD(torture_ops, op);
704         
705         return NT_STATUS_OK;
706 }
707
708 int torture_init(void)
709 {
710         init_module_fn static_init[] = STATIC_torture_MODULES;
711         init_module_fn *shared_init = load_samba_modules(NULL, "torture");
712         
713         register_builtin_ops();
714
715         run_init_functions(static_init);
716         run_init_functions(shared_init);
717
718         talloc_free(shared_init);
719
720         return 0;
721 }