subunit: Support formatting compatible with upstream subunit, for consistency.
[sfrench/samba-autobuild/.git] / source4 / torture / locktest.c
index ebc325fe2b368c6dbe78cfa79825808d1310daf2..b3ee8fc28f9d47b8105d4f35fd7bf1074a29f9d0 100644 (file)
 
 #include "includes.h"
 #include "lib/cmdline/popt_common.h"
+#include "lib/events/events.h"
 #include "system/filesys.h"
 #include "system/time.h"
-#include "pstring.h"
 #include "auth/credentials/credentials.h"
 #include "auth/gensec/gensec.h"
 #include "libcli/libcli.h"
 #include "param/param.h"
-#include "dynconfig.h"
 #include "libcli/resolve/resolve.h"
 
 static int numops = 1000;
@@ -34,9 +33,9 @@ static int showall;
 static int analyze;
 static int hide_unlock_fails;
 static int use_oplocks;
-static uint_t lock_range = 100;
-static uint_t lock_base = 0;
-static uint_t min_length = 0;
+static unsigned int lock_range = 100;
+static unsigned int lock_base = 0;
+static unsigned int min_length = 0;
 static int exact_error_codes;
 static int zero_zero;
 
@@ -107,20 +106,24 @@ static struct record *recorded;
 /***************************************************** 
 return a connection to a server
 *******************************************************/
-static struct smbcli_state *connect_one(struct loadparm_context *lp_ctx,
+static struct smbcli_state *connect_one(struct tevent_context *ev,
+                                       struct loadparm_context *lp_ctx,
+                                       TALLOC_CTX *mem_ctx,
                                        char *share, int snum, int conn)
 {
        struct smbcli_state *c;
-       fstring server, myname;
+       char *server, *myname;
        NTSTATUS status;
        int retries = 10;
        struct smbcli_options options;
+       struct smbcli_session_options session_options;
 
        lp_smbcli_options(lp_ctx, &options);
+       lp_smbcli_session_options(lp_ctx, &session_options);
 
        printf("connect_one(%s, %d, %d)\n", share, snum, conn);
 
-       fstrcpy(server,share+2);
+       server = talloc_strdup(mem_ctx, share+2);
        share = strchr_m(server,'\\');
        if (!share) return NULL;
        *share = 0;
@@ -133,7 +136,7 @@ static struct smbcli_state *connect_one(struct loadparm_context *lp_ctx,
                p = lp_parm_string(lp_ctx, NULL, "torture", "unclist");
                if (p) {
                        char *h, *s;
-                       unc_list = file_lines_load(p, &num_unc_names, NULL);
+                       unc_list = file_lines_load(p, &num_unc_names, 0, NULL);
                        if (!unc_list || num_unc_names <= 0) {
                                printf("Failed to load unc names list from '%s'\n", p);
                                exit(1);
@@ -145,13 +148,13 @@ static struct smbcli_state *connect_one(struct loadparm_context *lp_ctx,
                                       unc_list[conn % num_unc_names]);
                                exit(1);
                        }
-                       fstrcpy(server, h);
-                       fstrcpy(share, s);
+                       server = talloc_strdup(mem_ctx, h);
+                       share = talloc_strdup(mem_ctx, s);
                }
        }
 
 
-       slprintf(myname,sizeof(myname), "lock-%u-%u", getpid(), snum);
+       myname = talloc_asprintf(mem_ctx, "lock-%u-%u", getpid(), snum);
        cli_credentials_set_workstation(servers[snum], myname, CRED_SPECIFIED);
 
        do {
@@ -160,9 +163,12 @@ static struct smbcli_state *connect_one(struct loadparm_context *lp_ctx,
                                                server, 
                                                lp_smb_ports(lp_ctx),
                                                share, NULL,
+                                               lp_socket_options(lp_ctx),
                                                servers[snum], 
                                                lp_resolve_context(lp_ctx),
-                                               NULL, &options);
+                                               ev, &options, &session_options,
+                                               lp_iconv_convenience(lp_ctx),
+                                               lp_gensec_settings(mem_ctx, lp_ctx));
                if (!NT_STATUS_IS_OK(status)) {
                        sleep(2);
                }
@@ -176,7 +182,9 @@ static struct smbcli_state *connect_one(struct loadparm_context *lp_ctx,
 }
 
 
-static void reconnect(struct loadparm_context *lp_ctx,
+static void reconnect(struct tevent_context *ev,
+                     struct loadparm_context *lp_ctx,
+                         TALLOC_CTX *mem_ctx,
                      struct smbcli_state *cli[NSERVERS][NCONNECTIONS], int fnum[NSERVERS][NCONNECTIONS][NFILES],
                      char *share[NSERVERS])
 {
@@ -193,7 +201,7 @@ static void reconnect(struct loadparm_context *lp_ctx,
                        }
                        talloc_free(cli[server][conn]);
                }
-               cli[server][conn] = connect_one(lp_ctx, share[server], 
+               cli[server][conn] = connect_one(ev, lp_ctx, mem_ctx, share[server], 
                                                server, conn);
                if (!cli[server][conn]) {
                        DEBUG(0,("Failed to connect to %s\n", share[server]));
@@ -208,8 +216,8 @@ static bool test_one(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
                     int fnum[NSERVERS][NCONNECTIONS][NFILES],
                     struct record *rec)
 {
-       uint_t conn = rec->conn;
-       uint_t f = rec->f;
+       unsigned int conn = rec->conn;
+       unsigned int f = rec->f;
        uint64_t start = rec->start;
        uint64_t len = rec->len;
        enum brl_type op = rec->lock_type;
@@ -377,7 +385,7 @@ static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
                   int n)
 {
        int i;
-       printf("testing %u ...\n", n);
+       printf("Testing %u ...\n", n);
        for (i=0; i<n; i++) {
                if (i && i % 100 == 0) {
                        printf("%u\n", i);
@@ -396,7 +404,10 @@ static int retest(struct smbcli_state *cli[NSERVERS][NCONNECTIONS],
    we then do random locking ops in tamdem on the 4 fnums from each
    server and ensure that the results match
  */
-static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
+static int test_locks(struct tevent_context *ev,
+                     struct loadparm_context *lp_ctx,
+                         TALLOC_CTX *mem_ctx,
+                     char *share[NSERVERS])
 {
        struct smbcli_state *cli[NSERVERS][NCONNECTIONS];
        int fnum[NSERVERS][NCONNECTIONS][NFILES];
@@ -415,7 +426,7 @@ static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
 #endif
                        recorded[n].conn = random() % NCONNECTIONS;
                        recorded[n].f = random() % NFILES;
-                       recorded[n].start = lock_base + ((uint_t)random() % (lock_range-1));
+                       recorded[n].start = lock_base + ((unsigned int)random() % (lock_range-1));
                        recorded[n].len =  min_length +
                                random() % (lock_range-(recorded[n].start-lock_base));
                        recorded[n].start *= RANGE_MULTIPLE;
@@ -447,11 +458,16 @@ static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
 #endif
        }
 
-       reconnect(lp_ctx, cli, fnum, share);
+       reconnect(ev, lp_ctx, mem_ctx, cli, fnum, share);
        open_files(cli, fnum);
        n = retest(cli, fnum, numops);
 
-       if (n == numops || !analyze) return;
+       if (n == numops || !analyze) {
+               if (n != numops) {
+                       return 1;
+               }
+               return 0;
+       }
        n++;
 
        skip = n/2;
@@ -460,7 +476,7 @@ static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
                n1 = n;
 
                close_files(cli, fnum);
-               reconnect(lp_ctx, cli, fnum, share);
+               reconnect(ev, lp_ctx, mem_ctx, cli, fnum, share);
                open_files(cli, fnum);
 
                for (i=0;i<n-skip;i+=skip) {
@@ -498,7 +514,7 @@ static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
        }
 
        close_files(cli, fnum);
-       reconnect(lp_ctx, cli, fnum, share);
+       reconnect(ev, lp_ctx, mem_ctx, cli, fnum, share);
        open_files(cli, fnum);
        showall = true;
        n1 = retest(cli, fnum, n);
@@ -517,11 +533,13 @@ static void test_locks(struct loadparm_context *lp_ctx, char *share[NSERVERS])
                       (double)recorded[i].len,
                       recorded[i].needed);
        }       
+
+       return 1;
 }
 
 
 
-_NORETURN_ static void usage(poptContext pc)
+static void usage(poptContext pc)
 {
        printf("Usage:\n\tlocktest //server1/share1 //server2/share2 [options..]\n");
        poptPrintUsage(pc, stdout, 0);
@@ -536,6 +554,7 @@ _NORETURN_ static void usage(poptContext pc)
        int opt;
        int seed, server;
        int username_count=0;
+       struct tevent_context *ev;
        struct loadparm_context *lp_ctx;
        poptContext pc;
        int argc_new, i;
@@ -566,11 +585,12 @@ _NORETURN_ static void usage(poptContext pc)
        setlinebuf(stdout);
        seed = time(NULL);
 
-       pc = poptGetContext("locjtest", argc, (const char **) argv, long_options, 
+       pc = poptGetContext("locktest", argc, (const char **) argv, long_options, 
                            POPT_CONTEXT_KEEP_FIRST);
 
        poptSetOtherOptionHelp(pc, "<unc1> <unc2>");
 
+       lp_ctx = cmdline_lp_ctx;
        servers[0] = cli_credentials_init(talloc_autofree_context());
        servers[1] = cli_credentials_init(talloc_autofree_context());
        cli_credentials_guess(servers[0], lp_ctx);
@@ -623,17 +643,14 @@ _NORETURN_ static void usage(poptContext pc)
                servers[1] = servers[0];
        }
 
-       gensec_init(lp_ctx);
+       ev = s4_event_context_init(talloc_autofree_context());
 
-       argc -= optind;
-       argv += optind;
+       gensec_init(lp_ctx);
 
        DEBUG(0,("seed=%u base=%d range=%d min_length=%d\n", 
                 seed, lock_base, lock_range, min_length));
        srandom(seed);
 
-       test_locks(lp_ctx, share);
-
-       return(0);
+       return test_locks(ev, lp_ctx, NULL, share);
 }