pylibsmb: Return reparse_tag from directory listing
[Samba.git] / source3 / utils / dbwrap_torture.c
blobec33853720ade7c0ec7c0747898ccf4b2a437f66
1 /*
2 Samba Linux/Unix CIFS implementation
4 simple tool to test persistent databases
6 Copyright (C) Michael Adam 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "lib/cmdline/cmdline.h"
25 #include "dbwrap/dbwrap.h"
26 #include "dbwrap/dbwrap_open.h"
27 #include "messages.h"
28 #include "lib/util/util_tdb.h"
29 #include "lib/param/param.h"
31 #if 0
32 #include "lib/events/events.h"
33 #include "system/filesys.h"
34 #include "popt.h"
35 #include "cmdline.h"
37 #include <sys/time.h>
38 #include <time.h>
39 #endif
41 #define DEFAULT_DB_NAME "transaction.tdb"
43 static int timelimit = 10;
44 static int torture_delay = 0;
45 static int verbose = 0;
46 static int no_trans = 0;
47 static const char *db_name = DEFAULT_DB_NAME;
50 static unsigned int pnn;
52 static TDB_DATA old_data;
54 static int success = true;
56 static void print_counters(void)
58 int i;
59 uint32_t *old_counters;
61 printf("[%4u] Counters: ", (unsigned int)getpid());
62 old_counters = (uint32_t *)old_data.dptr;
63 for (i=0; i < old_data.dsize/sizeof(uint32_t); i++) {
64 printf("%6u ", old_counters[i]);
66 printf("\n");
69 static void each_second(struct tevent_context *ev,
70 struct tevent_timer *te,
71 struct timeval t,
72 void *private_data)
74 struct db_context *db = talloc_get_type(private_data, struct db_context);
76 print_counters();
78 tevent_add_timer(ev, db, timeval_current_ofs(1, 0), each_second, db);
81 static bool check_counters(struct db_context *db, TDB_DATA data)
83 int i;
84 uint32_t *counters, *old_counters;
86 counters = (uint32_t *)data.dptr;
87 old_counters = (uint32_t *)old_data.dptr;
89 /* check that all the counters are monotonic increasing */
90 for (i=0; i < old_data.dsize/sizeof(uint32_t); i++) {
91 if (counters[i] < old_counters[i]) {
92 printf("[%4u] ERROR: counters has decreased for node %u From %u to %u\n",
93 (unsigned int)getpid(), i, old_counters[i], counters[i]);
94 success = false;
95 return false;
99 if (old_data.dsize != data.dsize) {
100 old_data.dsize = data.dsize;
101 old_data.dptr = (unsigned char*)talloc_realloc_size(db, old_data.dptr, old_data.dsize);
104 memcpy(old_data.dptr, data.dptr, data.dsize);
105 if (verbose) print_counters();
107 return true;
111 static void do_sleep(unsigned int sec)
113 unsigned int i;
115 if (sec == 0) {
116 return;
119 for (i=0; i<sec; i++) {
120 if (verbose) printf(".");
121 sleep(1);
124 if (verbose) printf("\n");
127 static void test_store_records(struct db_context *db, struct tevent_context *ev)
129 TDB_DATA key;
130 uint32_t *counters;
131 TALLOC_CTX *tmp_ctx = talloc_stackframe();
132 struct timeval start;
134 key = string_term_tdb_data("testkey");
136 start = timeval_current();
137 while ((timelimit == 0) || (timeval_elapsed(&start) < timelimit)) {
138 struct db_record *rec;
139 TDB_DATA data;
140 TDB_DATA value;
141 int ret;
142 NTSTATUS status;
144 if (!no_trans) {
145 if (verbose) DEBUG(1, ("starting transaction\n"));
146 ret = dbwrap_transaction_start(db);
147 if (ret != 0) {
148 DEBUG(0, ("Failed to start transaction on node "
149 "%d\n", pnn));
150 goto fail;
152 if (verbose) DEBUG(1, ("transaction started\n"));
153 do_sleep(torture_delay);
156 if (verbose) DEBUG(1, ("calling fetch_lock\n"));
157 rec = dbwrap_fetch_locked(db, tmp_ctx, key);
158 if (rec == NULL) {
159 DEBUG(0, ("Failed to fetch record\n"));
160 goto fail;
162 if (verbose) DEBUG(1, ("fetched record ok\n"));
163 do_sleep(torture_delay);
164 value = dbwrap_record_get_value(rec);
166 data.dsize = MAX(value.dsize, sizeof(uint32_t) * (pnn+1));
167 data.dptr = (unsigned char *)talloc_zero_size(tmp_ctx,
168 data.dsize);
169 if (data.dptr == NULL) {
170 DEBUG(0, ("Failed to allocate data\n"));
171 goto fail;
173 memcpy(data.dptr, value.dptr, value.dsize);
175 counters = (uint32_t *)data.dptr;
177 /* bump our counter */
178 counters[pnn]++;
180 if (verbose) DEBUG(1, ("storing data\n"));
181 status = dbwrap_record_store(rec, data, TDB_REPLACE);
182 if (!NT_STATUS_IS_OK(status)) {
183 DEBUG(0, ("Failed to store record\n"));
184 if (!no_trans) {
185 ret = dbwrap_transaction_cancel(db);
186 if (ret != 0) {
187 DEBUG(0, ("Error cancelling transaction.\n"));
190 goto fail;
192 talloc_free(rec);
193 if (verbose) DEBUG(1, ("stored data ok\n"));
194 do_sleep(torture_delay);
196 if (!no_trans) {
197 if (verbose) DEBUG(1, ("calling transaction_commit\n"));
198 ret = dbwrap_transaction_commit(db);
199 if (ret != 0) {
200 DEBUG(0, ("Failed to commit transaction\n"));
201 goto fail;
203 if (verbose) DEBUG(1, ("transaction committed\n"));
206 /* store the counters and verify that they are sane */
207 if (verbose || (pnn == 0)) {
208 if (!check_counters(db, data)) {
209 goto fail;
212 talloc_free(data.dptr);
214 do_sleep(torture_delay);
217 goto done;
219 fail:
220 success = false;
222 done:
223 talloc_free(tmp_ctx);
224 return;
228 main program
230 int main(int argc, const char *argv[])
232 TALLOC_CTX *mem_ctx;
233 struct tevent_context *ev_ctx;
234 struct messaging_context *msg_ctx;
235 struct db_context *db;
237 int unsafe_writes = 0;
238 struct poptOption popt_options[] = {
239 POPT_AUTOHELP
240 POPT_COMMON_SAMBA
241 { "timelimit", 't', POPT_ARG_INT, &timelimit, 0, "timelimit", "INTEGER" },
242 { "delay", 'D', POPT_ARG_INT, &torture_delay, 0, "delay (in seconds) between operations", "INTEGER" },
243 { "verbose", 'v', POPT_ARG_NONE, &verbose, 0, "switch on verbose mode", NULL },
244 { "db-name", 'N', POPT_ARG_STRING, &db_name, 0, "name of the test db", "NAME" },
245 { "no-trans", 'n', POPT_ARG_NONE, &no_trans, 0, "use fetch_lock/record store instead of transactions", NULL },
246 { "unsafe-writes", 'u', POPT_ARG_NONE, &unsafe_writes, 0, "do not use tdb transactions when writing", NULL },
247 POPT_COMMON_VERSION
248 POPT_TABLEEND
250 int opt;
251 const char **extra_argv;
252 int extra_argc = 0;
253 poptContext pc;
254 int tdb_flags;
255 bool ok;
256 int ret = 1;
257 struct loadparm_context *lp_ctx = NULL;
259 mem_ctx = talloc_stackframe();
261 if (verbose) {
262 setbuf(stdout, (char *)NULL); /* don't buffer */
263 } else {
264 setlinebuf(stdout);
267 smb_init_locale();
269 ok = samba_cmdline_init(mem_ctx,
270 SAMBA_CMDLINE_CONFIG_CLIENT,
271 false /* require_smbconf */);
272 if (!ok) {
273 DBG_ERR("Failed to init cmdline parser!\n");
274 TALLOC_FREE(mem_ctx);
275 exit(1);
277 lp_ctx = samba_cmdline_get_lp_ctx();
278 lpcfg_set_cmdline(lp_ctx, "log level", "0");
280 pc = samba_popt_get_context(getprogname(),
281 argc,
282 argv,
283 popt_options,
284 POPT_CONTEXT_KEEP_FIRST);
285 if (pc == NULL) {
286 DBG_ERR("Failed to setup popt context!\n");
287 TALLOC_FREE(mem_ctx);
288 exit(1);
291 while ((opt = poptGetNextOpt(pc)) != -1) {
292 switch (opt) {
293 default:
294 fprintf(stderr, "Invalid option %s: %s\n",
295 poptBadOption(pc, 0), poptStrerror(opt));
296 goto done;
300 /* setup the remaining options for the main program to use */
301 extra_argv = poptGetArgs(pc);
302 if (extra_argv) {
303 extra_argv++;
304 while (extra_argv[extra_argc]) extra_argc++;
307 ev_ctx = samba_tevent_context_init(mem_ctx);
308 if (ev_ctx == NULL) {
309 d_fprintf(stderr, "ERROR: could not init event context\n");
310 goto done;
313 msg_ctx = messaging_init(mem_ctx, ev_ctx);
314 if (msg_ctx == NULL) {
315 d_fprintf(stderr, "ERROR: could not init messaging context\n");
316 goto done;
319 if (unsafe_writes == 1) {
320 tdb_flags = TDB_NOSYNC;
321 } else {
322 tdb_flags = TDB_DEFAULT;
325 if (no_trans) {
326 tdb_flags |= TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
329 db = db_open(mem_ctx, db_name, 0, tdb_flags, O_RDWR | O_CREAT, 0644,
330 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
332 if (db == NULL) {
333 d_fprintf(stderr, "failed to open db '%s': %s\n", db_name,
334 strerror(errno));
335 goto done;
338 if (get_my_vnn() == NONCLUSTER_VNN) {
339 set_my_vnn(0);
341 pnn = get_my_vnn();
343 printf("Starting test on node %u. running for %u seconds. "
344 "sleep delay: %u seconds.\n", pnn, timelimit, torture_delay);
346 if (!verbose && (pnn == 0)) {
347 tevent_add_timer(ev_ctx, db, timeval_current_ofs(1, 0), each_second, db);
350 test_store_records(db, ev_ctx);
352 if (verbose || (pnn == 0)) {
353 if (success != true) {
354 printf("The test FAILED\n");
355 ret = 2;
356 } else {
357 printf("SUCCESS!\n");
358 ret = 0;
362 done:
363 poptFreeContext(pc);
364 talloc_free(mem_ctx);
365 return ret;