libsmb: Convert cli_NetServerEnum() away from cli_api()
[samba4-gss.git] / source3 / torture / test_notify.c
blobb265845f8222db25290c9da23fcd5cee26edf936
1 /*
2 Unix SMB/CIFS implementation.
3 Scalability test for notifies
4 Copyright (C) Volker Lendecke 2012
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "torture/proto.h"
22 #include "libsmb/libsmb.h"
23 #include "lib/util/tevent_ntstatus.h"
24 #include "libcli/security/security.h"
25 #include "lib/tevent_barrier.h"
27 extern int torture_nprocs, torture_numops;
29 struct wait_for_one_notify_state {
30 struct tevent_context *ev;
31 struct cli_state *cli;
32 uint16_t dnum;
33 uint32_t filter;
34 bool recursive;
35 unsigned *num_notifies;
38 static void wait_for_one_notify_opened(struct tevent_req *subreq);
39 static void wait_for_one_notify_chkpath_done(struct tevent_req *subreq);
40 static void wait_for_one_notify_done(struct tevent_req *subreq);
41 static void wait_for_one_notify_closed(struct tevent_req *subreq);
43 static struct tevent_req *wait_for_one_notify_send(TALLOC_CTX *mem_ctx,
44 struct tevent_context *ev,
45 struct cli_state *cli,
46 const char *path,
47 uint32_t filter,
48 bool recursive,
49 unsigned *num_notifies)
51 struct tevent_req *req, *subreq;
52 struct wait_for_one_notify_state *state;
54 req = tevent_req_create(mem_ctx, &state,
55 struct wait_for_one_notify_state);
56 if (req == NULL) {
57 return NULL;
59 state->ev = ev;
60 state->cli = cli;
61 state->filter = filter;
62 state->recursive = recursive;
63 state->num_notifies = num_notifies;
65 subreq = cli_ntcreate_send(
66 state, state->ev, state->cli, path, 0,
67 MAXIMUM_ALLOWED_ACCESS,
68 0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
69 FILE_OPEN, FILE_DIRECTORY_FILE,
70 SMB2_IMPERSONATION_IMPERSONATION, 0);
71 if (tevent_req_nomem(subreq, req)) {
72 return tevent_req_post(req, ev);
74 tevent_req_set_callback(subreq, wait_for_one_notify_opened, req);
75 return req;
78 static void wait_for_one_notify_opened(struct tevent_req *subreq)
80 struct tevent_req *req = tevent_req_callback_data(
81 subreq, struct tevent_req);
82 struct wait_for_one_notify_state *state = tevent_req_data(
83 req, struct wait_for_one_notify_state);
84 NTSTATUS status;
86 status = cli_ntcreate_recv(subreq, &state->dnum, NULL);
87 TALLOC_FREE(subreq);
88 if (tevent_req_nterror(req, status)) {
89 return;
91 subreq = cli_notify_send(state, state->ev, state->cli, state->dnum,
92 0xffff, state->filter, state->recursive);
93 if (tevent_req_nomem(subreq, req)) {
94 return;
96 tevent_req_set_callback(subreq, wait_for_one_notify_done, req);
99 * To make sure the notify received at the server, we do another no-op
100 * that is replied to.
102 subreq = cli_chkpath_send(state, state->ev, state->cli, "\\");
103 if (tevent_req_nomem(subreq, req)) {
104 return;
106 tevent_req_set_callback(subreq, wait_for_one_notify_chkpath_done, req);
109 static void wait_for_one_notify_chkpath_done(struct tevent_req *subreq)
111 struct tevent_req *req = tevent_req_callback_data(
112 subreq, struct tevent_req);
113 struct wait_for_one_notify_state *state = tevent_req_data(
114 req, struct wait_for_one_notify_state);
115 NTSTATUS status;
117 status = cli_chkpath_recv(subreq);
118 TALLOC_FREE(subreq);
119 if (tevent_req_nterror(req, status)) {
120 return;
122 *state->num_notifies += 1;
125 static void wait_for_one_notify_done(struct tevent_req *subreq)
127 struct tevent_req *req = tevent_req_callback_data(
128 subreq, struct tevent_req);
129 struct wait_for_one_notify_state *state = tevent_req_data(
130 req, struct wait_for_one_notify_state);
131 uint32_t num_changes;
132 struct notify_change *changes;
133 NTSTATUS status;
135 status = cli_notify_recv(subreq, state, &num_changes, &changes);
136 TALLOC_FREE(subreq);
137 if (tevent_req_nterror(req, status)) {
138 return;
140 subreq = cli_close_send(state, state->ev, state->cli, state->dnum, 0);
141 if (tevent_req_nomem(subreq, req)) {
142 return;
144 tevent_req_set_callback(subreq, wait_for_one_notify_closed, req);
147 static void wait_for_one_notify_closed(struct tevent_req *subreq)
149 struct tevent_req *req = tevent_req_callback_data(
150 subreq, struct tevent_req);
151 struct wait_for_one_notify_state *state = tevent_req_data(
152 req, struct wait_for_one_notify_state);
153 NTSTATUS status;
155 status = cli_close_recv(subreq);
156 TALLOC_FREE(subreq);
157 if (tevent_req_nterror(req, status)) {
158 return;
160 *state->num_notifies -= 1;
161 tevent_req_done(req);
164 static NTSTATUS wait_for_one_notify_recv(struct tevent_req *req)
166 return tevent_req_simple_recv_ntstatus(req);
169 static void notify_bench2_done(struct tevent_req *req);
171 bool run_notify_bench2(int dummy)
173 struct cli_state *cli;
174 struct cli_state **clis;
175 struct tevent_context *ev;
176 unsigned num_notifies = 0;
177 NTSTATUS status;
178 int i;
180 if (!torture_open_connection(&cli, 0)) {
181 return false;
184 printf("starting notify bench 2 test\n");
186 cli_rmdir(cli, "\\notify.dir\\subdir");
187 cli_rmdir(cli, "\\notify.dir");
189 status = cli_mkdir(cli, "\\notify.dir");
190 if (!NT_STATUS_IS_OK(status)) {
191 printf("mkdir failed : %s\n", nt_errstr(status));
192 return false;
195 clis = talloc_array(talloc_tos(), struct cli_state *, torture_nprocs);
196 if (clis == NULL) {
197 printf("talloc failed\n");
198 return false;
201 ev = samba_tevent_context_init(talloc_tos());
202 if (ev == NULL) {
203 printf("tevent_context_create failed\n");
204 return false;
207 for (i=0; i<torture_nprocs; i++) {
208 int j;
209 if (!torture_open_connection(&clis[i], i)) {
210 return false;
213 for (j=0; j<torture_numops; j++) {
214 struct tevent_req *req;
215 req = wait_for_one_notify_send(
216 talloc_tos(), ev, clis[i], "\\notify.dir",
217 FILE_NOTIFY_CHANGE_ALL, true,
218 &num_notifies);
219 if (req == NULL) {
220 printf("wait_for_one_notify_send failed\n");
221 return false;
223 tevent_req_set_callback(req, notify_bench2_done, NULL);
227 while (num_notifies < (unsigned)(torture_nprocs * torture_numops)) {
228 int ret;
229 ret = tevent_loop_once(ev);
230 if (ret != 0) {
231 printf("tevent_loop_once failed: %s\n",
232 strerror(errno));
233 return false;
237 cli_mkdir(cli, "\\notify.dir\\subdir");
239 while (num_notifies > 0) {
240 int ret;
241 ret = tevent_loop_once(ev);
242 if (ret != 0) {
243 printf("tevent_loop_once failed: %s\n",
244 strerror(errno));
245 return false;
249 return true;
252 static void notify_bench2_done(struct tevent_req *req)
254 NTSTATUS status;
256 status = wait_for_one_notify_recv(req);
257 TALLOC_FREE(req);
258 if (!NT_STATUS_IS_OK(status)) {
259 printf("wait_for_one_notify returned %s\n",
260 nt_errstr(status));
265 * This test creates a subdirectory. It then waits on a barrier before the
266 * notify is sent. Then it creates the notify. It then waits for another
267 * barrier, so that all of the notifies have gone through. It then creates
268 * another subdirectory, which will trigger notifications to be sent. When the
269 * notifies have been received, it waits once more before everything is
270 * cleaned up.
273 struct notify_bench3_state {
274 struct tevent_context *ev;
275 struct cli_state *cli;
276 const char *dir;
277 uint16_t dnum;
278 const char *subdir_path;
279 uint16_t subdir_dnum;
280 int wait_timeout;
281 struct tevent_barrier *small;
282 struct tevent_barrier *large;
285 static void notify_bench3_mkdir1_done(struct tevent_req *subreq);
286 static void notify_bench3_before_notify(struct tevent_req *subreq);
287 static void notify_bench3_chkpath_done(struct tevent_req *subreq);
288 static void notify_bench3_before_mkdir2(struct tevent_req *subreq);
289 static void notify_bench3_notify_done(struct tevent_req *subreq);
290 static void notify_bench3_notifies_done(struct tevent_req *subreq);
291 static void notify_bench3_mksubdir_done(struct tevent_req *subreq);
292 static void notify_bench3_before_close_subdir(struct tevent_req *subreq);
293 static void notify_bench3_close_subdir_done(struct tevent_req *subreq);
294 static void notify_bench3_deleted_subdir(struct tevent_req *subreq);
295 static void notify_bench3_deleted_subdirs(struct tevent_req *subreq);
296 static void notify_bench3_del_on_close_set(struct tevent_req *subreq);
297 static void notify_bench3_closed(struct tevent_req *subreq);
299 static struct tevent_req *notify_bench3_send(
300 TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct cli_state *cli,
301 const char *dir, const char *subdir_path,
302 struct tevent_barrier *small, struct tevent_barrier *large)
304 struct tevent_req *req, *subreq;
305 struct notify_bench3_state *state;
307 req = tevent_req_create(mem_ctx, &state, struct notify_bench3_state);
308 if (req == NULL) {
309 return NULL;
311 state->ev = ev;
312 state->cli = cli;
313 state->dir = dir;
314 state->subdir_path = subdir_path;
315 state->small = small;
316 state->large = large;
318 subreq = cli_ntcreate_send(
319 state, state->ev, state->cli, state->dir, 0,
320 MAXIMUM_ALLOWED_ACCESS, 0,
321 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
322 FILE_OPEN_IF, FILE_DIRECTORY_FILE,
323 SMB2_IMPERSONATION_IMPERSONATION, 0);
324 if (tevent_req_nomem(subreq, req)) {
325 return tevent_req_post(req, ev);
327 tevent_req_set_callback(subreq, notify_bench3_mkdir1_done, req);
328 return req;
331 static void notify_bench3_mkdir1_done(struct tevent_req *subreq)
333 struct tevent_req *req = tevent_req_callback_data(
334 subreq, struct tevent_req);
335 struct notify_bench3_state *state = tevent_req_data(
336 req, struct notify_bench3_state);
337 NTSTATUS status;
339 status = cli_ntcreate_recv(subreq, &state->dnum, NULL);
340 TALLOC_FREE(subreq);
341 if (tevent_req_nterror(req, status)) {
342 return;
344 subreq = tevent_barrier_wait_send(state, state->ev, state->small);
345 if (tevent_req_nomem(subreq, req)) {
346 return;
348 tevent_req_set_callback(subreq, notify_bench3_before_notify, req);
351 static void notify_bench3_before_notify(struct tevent_req *subreq)
353 struct tevent_req *req = tevent_req_callback_data(
354 subreq, struct tevent_req);
355 struct notify_bench3_state *state = tevent_req_data(
356 req, struct notify_bench3_state);
357 int ret;
359 ret = tevent_barrier_wait_recv(subreq);
360 TALLOC_FREE(subreq);
361 if (ret != 0) {
362 tevent_req_nterror(req, map_nt_error_from_unix(ret));
363 return;
365 subreq = cli_notify_send(state, state->ev, state->cli, state->dnum,
366 0xffff, FILE_NOTIFY_CHANGE_ALL, true);
367 if (tevent_req_nomem(subreq, req)) {
368 return;
370 tevent_req_set_callback(subreq, notify_bench3_notify_done, req);
373 * To make sure the notify received at the server, we do another no-op
374 * that is replied to.
376 subreq = cli_chkpath_send(state, state->ev, state->cli, "\\");
377 if (tevent_req_nomem(subreq, req)) {
378 return;
380 tevent_req_set_callback(subreq, notify_bench3_chkpath_done, req);
383 static void notify_bench3_notify_done(struct tevent_req *subreq)
385 struct tevent_req *req = tevent_req_callback_data(
386 subreq, struct tevent_req);
387 struct notify_bench3_state *state = tevent_req_data(
388 req, struct notify_bench3_state);
389 uint32_t num_changes;
390 struct notify_change *changes;
391 NTSTATUS status;
393 status = cli_notify_recv(subreq, state, &num_changes, &changes);
394 TALLOC_FREE(subreq);
395 if (tevent_req_nterror(req, status)) {
396 return;
398 subreq = tevent_barrier_wait_send(state, state->ev, state->large);
399 if (tevent_req_nomem(subreq, req)) {
400 return;
402 tevent_req_set_callback(subreq, notify_bench3_notifies_done, req);
405 static void notify_bench3_notifies_done(struct tevent_req *subreq)
407 struct tevent_req *req = tevent_req_callback_data(
408 subreq, struct tevent_req);
409 int ret;
411 ret = tevent_barrier_wait_recv(subreq);
412 TALLOC_FREE(subreq);
413 if (ret != 0) {
414 tevent_req_nterror(req, map_nt_error_from_unix(ret));
415 return;
419 static void notify_bench3_chkpath_done(struct tevent_req *subreq)
421 struct tevent_req *req = tevent_req_callback_data(
422 subreq, struct tevent_req);
423 struct notify_bench3_state *state = tevent_req_data(
424 req, struct notify_bench3_state);
425 NTSTATUS status;
427 status = cli_chkpath_recv(subreq);
428 TALLOC_FREE(subreq);
429 if (tevent_req_nterror(req, status)) {
430 return;
432 if (state->subdir_path == NULL) {
433 return;
435 subreq = tevent_barrier_wait_send(state, state->ev, state->small);
436 if (tevent_req_nomem(subreq, req)) {
437 return;
439 tevent_req_set_callback(subreq, notify_bench3_before_mkdir2, req);
442 static void notify_bench3_before_mkdir2(struct tevent_req *subreq)
444 struct tevent_req *req = tevent_req_callback_data(
445 subreq, struct tevent_req);
446 struct notify_bench3_state *state = tevent_req_data(
447 req, struct notify_bench3_state);
448 int ret;
450 ret = tevent_barrier_wait_recv(subreq);
451 TALLOC_FREE(subreq);
452 if (ret != 0) {
453 tevent_req_nterror(req, map_nt_error_from_unix(ret));
454 return;
456 subreq = cli_ntcreate_send(
457 state, state->ev, state->cli, state->subdir_path, 0,
458 MAXIMUM_ALLOWED_ACCESS, 0,
459 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
460 FILE_CREATE,
461 FILE_DIRECTORY_FILE,
462 SMB2_IMPERSONATION_IMPERSONATION, 0);
463 if (tevent_req_nomem(subreq, req)) {
464 return;
466 tevent_req_set_callback(subreq, notify_bench3_mksubdir_done, req);
469 static void notify_bench3_mksubdir_done(struct tevent_req *subreq)
471 struct tevent_req *req = tevent_req_callback_data(
472 subreq, struct tevent_req);
473 struct notify_bench3_state *state = tevent_req_data(
474 req, struct notify_bench3_state);
475 NTSTATUS status;
477 status = cli_ntcreate_recv(subreq, &state->subdir_dnum, NULL);
478 TALLOC_FREE(subreq);
479 if (tevent_req_nterror(req, status)) {
480 return;
482 subreq = tevent_barrier_wait_send(state, state->ev, state->large);
483 if (tevent_req_nomem(subreq, req)) {
484 return;
486 tevent_req_set_callback(subreq, notify_bench3_before_close_subdir,
487 req);
490 static void notify_bench3_before_close_subdir(struct tevent_req *subreq)
492 struct tevent_req *req = tevent_req_callback_data(
493 subreq, struct tevent_req);
494 struct notify_bench3_state *state = tevent_req_data(
495 req, struct notify_bench3_state);
496 int ret;
498 ret = tevent_barrier_wait_recv(subreq);
499 TALLOC_FREE(subreq);
500 if (ret != 0) {
501 tevent_req_nterror(req, map_nt_error_from_unix(ret));
502 return;
504 subreq = cli_close_send(state,
505 state->ev,
506 state->cli,
507 state->subdir_dnum,
509 if (tevent_req_nomem(subreq, req)) {
510 return;
512 tevent_req_set_callback(subreq, notify_bench3_close_subdir_done, req);
515 static void notify_bench3_close_subdir_done(struct tevent_req *subreq)
517 struct tevent_req *req = tevent_req_callback_data(
518 subreq, struct tevent_req);
519 struct notify_bench3_state *state = tevent_req_data(
520 req, struct notify_bench3_state);
521 NTSTATUS status;
523 status = cli_close_recv(subreq);
524 TALLOC_FREE(subreq);
525 if (tevent_req_nterror(req, status)) {
526 return;
528 subreq = cli_rmdir_send(state, state->ev, state->cli,
529 state->subdir_path);
530 if (tevent_req_nomem(subreq, req)) {
531 return;
533 tevent_req_set_callback(subreq, notify_bench3_deleted_subdir, req);
536 static void notify_bench3_deleted_subdir(struct tevent_req *subreq)
538 struct tevent_req *req = tevent_req_callback_data(
539 subreq, struct tevent_req);
540 struct notify_bench3_state *state = tevent_req_data(
541 req, struct notify_bench3_state);
542 NTSTATUS status;
544 status = cli_rmdir_recv(subreq);
545 TALLOC_FREE(subreq);
546 if (tevent_req_nterror(req, status)) {
547 return;
549 subreq = tevent_barrier_wait_send(state, state->ev, state->small);
550 if (tevent_req_nomem(subreq, req)) {
551 return;
553 tevent_req_set_callback(subreq, notify_bench3_deleted_subdirs, req);
556 static void notify_bench3_deleted_subdirs(struct tevent_req *subreq)
558 struct tevent_req *req = tevent_req_callback_data(
559 subreq, struct tevent_req);
560 struct notify_bench3_state *state = tevent_req_data(
561 req, struct notify_bench3_state);
562 int ret;
564 ret = tevent_barrier_wait_recv(subreq);
565 TALLOC_FREE(subreq);
566 if (ret != 0) {
567 tevent_req_nterror(req, map_nt_error_from_unix(ret));
568 return;
570 subreq = cli_nt_delete_on_close_send(state, state->ev, state->cli,
571 state->dnum, true);
572 if (tevent_req_nomem(subreq, req)) {
573 return;
575 tevent_req_set_callback(subreq, notify_bench3_del_on_close_set, req);
578 static void notify_bench3_del_on_close_set(struct tevent_req *subreq)
580 struct tevent_req *req = tevent_req_callback_data(
581 subreq, struct tevent_req);
582 struct notify_bench3_state *state = tevent_req_data(
583 req, struct notify_bench3_state);
584 NTSTATUS status;
586 status = cli_nt_delete_on_close_recv(subreq);
587 TALLOC_FREE(subreq);
588 if (tevent_req_nterror(req, status)) {
589 return;
592 subreq = cli_close_send(state, state->ev, state->cli, state->dnum, 0);
593 if (tevent_req_nomem(subreq, req)) {
594 return;
596 tevent_req_set_callback(subreq, notify_bench3_closed, req);
599 static void notify_bench3_closed(struct tevent_req *subreq)
601 struct tevent_req *req = tevent_req_callback_data(
602 subreq, struct tevent_req);
603 NTSTATUS status;
605 status = cli_close_recv(subreq);
606 TALLOC_FREE(subreq);
607 if (tevent_req_nterror(req, status)) {
608 return;
610 tevent_req_done(req);
613 static NTSTATUS notify_bench3_recv(struct tevent_req *req)
615 return tevent_req_simple_recv_ntstatus(req);
618 static void notify_bench3_done(struct tevent_req *req)
620 unsigned *num_done = (unsigned *)tevent_req_callback_data_void(req);
621 NTSTATUS status;
623 status = notify_bench3_recv(req);
624 TALLOC_FREE(req);
625 if (!NT_STATUS_IS_OK(status)) {
626 d_printf("notify_bench3 returned %s\n", nt_errstr(status));
628 *num_done += 1;
631 static void notify_bench3_barrier_cb(void *private_data)
633 struct timeval *ts = (struct timeval *)private_data;
634 struct timeval now;
636 GetTimeOfDay(&now);
637 printf("barrier triggered: %f\n", timeval_elapsed2(ts, &now));
638 GetTimeOfDay(ts);
641 bool run_notify_bench3(int dummy)
643 struct cli_state **clis;
644 struct tevent_context *ev;
645 struct tevent_barrier *small;
646 struct tevent_barrier *large;
647 int i;
648 unsigned num_done = 0;
649 struct timeval ts, now;
651 clis = talloc_array(talloc_tos(), struct cli_state *, torture_nprocs);
652 if (clis == NULL) {
653 printf("talloc failed\n");
654 return false;
657 GetTimeOfDay(&ts);
659 small = tevent_barrier_init(
660 talloc_tos(), torture_nprocs * torture_numops,
661 notify_bench3_barrier_cb, &ts);
662 if (small == NULL) {
663 return false;
666 large = tevent_barrier_init(
667 talloc_tos(), 2 * torture_nprocs * torture_numops,
668 notify_bench3_barrier_cb, &ts);
669 if (large == NULL) {
670 return false;
673 ev = samba_tevent_context_init(talloc_tos());
674 if (ev == NULL) {
675 printf("tevent_context_create failed\n");
676 return false;
679 for (i=0; i<torture_nprocs; i++) {
680 if (!torture_open_connection(&clis[i], i)) {
681 return false;
685 for (i=0; i<torture_nprocs; i++) {
686 int j;
687 for (j=0; j<torture_numops; j++) {
688 int idx = i * torture_numops + j;
689 struct tevent_req *req;
690 char *dirname, *subdirname;
692 dirname = talloc_asprintf(
693 talloc_tos(), "\\dir%.8d", idx);
694 if (dirname == NULL) {
695 return false;
697 subdirname = talloc_asprintf(
698 talloc_tos(), "\\dir%.8d\\subdir",
699 (idx + torture_numops + 1) %
700 (torture_nprocs * torture_numops));
701 if (subdirname == NULL) {
702 return false;
705 req = notify_bench3_send(
706 talloc_tos(), ev, clis[i], dirname,
707 subdirname, small, large);
708 if (req == NULL) {
709 return false;
711 tevent_req_set_callback(req, notify_bench3_done,
712 &num_done);
716 while (num_done < (unsigned)(torture_nprocs * torture_numops)) {
717 int ret;
718 ret = tevent_loop_once(ev);
719 if (ret != 0) {
720 printf("tevent_loop_once failed: %s\n",
721 strerror(errno));
722 return false;
726 GetTimeOfDay(&now);
727 printf("turndow: %f\n", timeval_elapsed2(&ts, &now));
728 TALLOC_FREE(small);
729 TALLOC_FREE(large);
730 return true;