2 Unix SMB/CIFS implementation.
4 testing of the events subsystem
6 Copyright (C) Stefan Metzmacher 2006-2009
7 Copyright (C) Jeremy Allison 2013
9 ** NOTE! The following LGPL license applies to the tevent
10 ** library. This does NOT imply that all of Samba is released
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #define TEVENT_DEPRECATED 1
30 #include "system/filesys.h"
31 #include "system/select.h"
32 #include "system/network.h"
33 #include "torture/torture.h"
34 #include "torture/local/proto.h"
36 #include "system/threads.h"
40 static struct tevent_context *
41 test_tevent_context_init(TALLOC_CTX *mem_ctx)
43 struct tevent_context *ev = NULL;
45 ev = tevent_context_init(mem_ctx);
47 samba_tevent_set_debug(ev, "<default>");
53 static struct tevent_context *
54 test_tevent_context_init_byname(TALLOC_CTX *mem_ctx, const char *name)
56 struct tevent_context *ev = NULL;
58 ev = tevent_context_init_byname(mem_ctx, name);
60 samba_tevent_set_debug(ev, name);
68 static void do_read(int fd, void *buf, size_t count)
73 ret = read(fd, buf, count);
74 } while (ret == -1 && errno == EINTR);
77 static void fde_handler_read(struct tevent_context *ev_ctx, struct tevent_fd *f,
78 uint16_t flags, void *private_data)
80 int *fd = (int *)private_data;
83 kill(getpid(), SIGUSR1);
85 kill(getpid(), SIGALRM);
87 do_read(fd[0], &c, 1);
91 static void do_write(int fd, void *buf, size_t count)
96 ret = write(fd, buf, count);
97 } while (ret == -1 && errno == EINTR);
100 static void fde_handler_write(struct tevent_context *ev_ctx, struct tevent_fd *f,
101 uint16_t flags, void *private_data)
103 int *fd = (int *)private_data;
106 do_write(fd[1], &c, 1);
110 /* This will only fire if the fd's returned from pipe() are bi-directional. */
111 static void fde_handler_read_1(struct tevent_context *ev_ctx, struct tevent_fd *f,
112 uint16_t flags, void *private_data)
114 int *fd = (int *)private_data;
117 kill(getpid(), SIGUSR1);
119 kill(getpid(), SIGALRM);
121 do_read(fd[1], &c, 1);
125 /* This will only fire if the fd's returned from pipe() are bi-directional. */
126 static void fde_handler_write_1(struct tevent_context *ev_ctx, struct tevent_fd *f,
127 uint16_t flags, void *private_data)
129 int *fd = (int *)private_data;
131 do_write(fd[0], &c, 1);
134 static void finished_handler(struct tevent_context *ev_ctx, struct tevent_timer *te,
135 struct timeval tval, void *private_data)
137 int *finished = (int *)private_data;
141 static void count_handler(struct tevent_context *ev_ctx, struct tevent_signal *te,
142 int signum, int count, void *info, void *private_data)
144 int *countp = (int *)private_data;
148 static bool test_event_context(struct torture_context *test,
149 const void *test_data)
151 struct tevent_context *ev_ctx;
152 int fd[2] = { -1, -1 };
153 const char *backend = (const char *)test_data;
154 int alarm_count=0, info_count=0;
155 struct tevent_fd *fde_read;
156 struct tevent_fd *fde_read_1;
157 struct tevent_fd *fde_write;
158 struct tevent_fd *fde_write_1;
160 struct tevent_signal *se1 = NULL;
163 struct tevent_signal *se2 = NULL;
166 struct tevent_signal *se3 = NULL;
172 ev_ctx = test_tevent_context_init_byname(test, backend);
173 if (ev_ctx == NULL) {
174 torture_comment(test, "event backend '%s' not supported\n", backend);
178 torture_comment(test, "backend '%s' - %s\n",
179 backend, __FUNCTION__);
186 torture_assert_int_equal(test, ret, 0, "pipe failed");
188 fde_read = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_READ,
189 fde_handler_read, fd);
190 fde_write_1 = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_WRITE,
191 fde_handler_write_1, fd);
193 fde_write = tevent_add_fd(ev_ctx, ev_ctx, fd[1], TEVENT_FD_WRITE,
194 fde_handler_write, fd);
195 fde_read_1 = tevent_add_fd(ev_ctx, ev_ctx, fd[1], TEVENT_FD_READ,
196 fde_handler_read_1, fd);
198 tevent_fd_set_auto_close(fde_read);
199 tevent_fd_set_auto_close(fde_write);
201 tevent_add_timer(ev_ctx, ev_ctx, timeval_current_ofs(2,0),
202 finished_handler, &finished);
205 se1 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESTART, count_handler, &alarm_count);
206 torture_assert(test, se1 != NULL, "failed to setup se1");
209 se2 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESETHAND, count_handler, &alarm_count);
210 torture_assert(test, se2 != NULL, "failed to setup se2");
213 se3 = tevent_add_signal(ev_ctx, ev_ctx, SIGUSR1, SA_SIGINFO, count_handler, &info_count);
214 torture_assert(test, se3 != NULL, "failed to setup se3");
217 t = timeval_current();
220 if (tevent_loop_once(ev_ctx) == -1) {
222 torture_fail(test, talloc_asprintf(test, "Failed event loop %s\n", strerror(errno)));
227 talloc_free(fde_read_1);
228 talloc_free(fde_write_1);
229 talloc_free(fde_read);
230 talloc_free(fde_write);
232 while (alarm_count < fde_count+1) {
233 if (tevent_loop_once(ev_ctx) == -1) {
238 torture_comment(test, "Got %.2f pipe events/sec\n", fde_count/timeval_elapsed(&t));
244 torture_assert_int_equal(test, alarm_count, 1+fde_count, "alarm count mismatch");
248 * we do not call talloc_free(se2)
249 * because it is already gone,
250 * after triggering the event handler.
256 torture_assert_int_equal(test, info_count, fde_count, "info count mismatch");
264 struct test_event_fd1_state {
265 struct torture_context *tctx;
267 struct tevent_context *ev;
269 struct tevent_timer *te;
270 struct tevent_fd *fde0;
271 struct tevent_fd *fde1;
281 static void test_event_fd1_fde_handler(struct tevent_context *ev_ctx,
282 struct tevent_fd *fde,
286 struct test_event_fd1_state *state =
287 (struct test_event_fd1_state *)private_data;
289 if (state->drain_done) {
290 state->finished = true;
291 state->error = __location__;
299 if (!(flags & TEVENT_FD_READ)) {
300 state->finished = true;
301 state->error = __location__;
305 ret = read(state->sock[0], &c, 1);
313 tevent_fd_set_flags(fde, 0);
314 state->drain_done = true;
318 if (!state->got_write) {
321 if (flags != TEVENT_FD_WRITE) {
322 state->finished = true;
323 state->error = __location__;
326 state->got_write = true;
329 * we write to the other socket...
331 do_write(state->sock[1], &c, 1);
332 TEVENT_FD_NOT_WRITEABLE(fde);
333 TEVENT_FD_READABLE(fde);
337 if (!state->got_read) {
338 if (flags != TEVENT_FD_READ) {
339 state->finished = true;
340 state->error = __location__;
343 state->got_read = true;
345 TEVENT_FD_NOT_READABLE(fde);
349 state->finished = true;
350 state->error = __location__;
354 static void test_event_fd1_finished(struct tevent_context *ev_ctx,
355 struct tevent_timer *te,
359 struct test_event_fd1_state *state =
360 (struct test_event_fd1_state *)private_data;
362 if (state->drain_done) {
363 state->finished = true;
367 if (!state->got_write) {
368 state->finished = true;
369 state->error = __location__;
373 if (!state->got_read) {
374 state->finished = true;
375 state->error = __location__;
380 if (state->loop_count > 3) {
381 state->finished = true;
382 state->error = __location__;
386 state->got_write = false;
387 state->got_read = false;
389 tevent_fd_set_flags(state->fde0, TEVENT_FD_WRITE);
391 if (state->loop_count > 2) {
393 TALLOC_FREE(state->fde1);
394 TEVENT_FD_READABLE(state->fde0);
397 state->te = tevent_add_timer(state->ev, state->ev,
398 timeval_current_ofs(0,2000),
399 test_event_fd1_finished, state);
402 static bool test_event_fd1(struct torture_context *tctx,
403 const void *test_data)
405 struct test_event_fd1_state state;
410 state.backend = (const char *)test_data;
412 state.ev = test_tevent_context_init_byname(tctx, state.backend);
413 if (state.ev == NULL) {
414 torture_skip(tctx, talloc_asprintf(tctx,
415 "event backend '%s' not supported\n",
420 torture_comment(tctx, "backend '%s' - %s\n",
421 state.backend, __FUNCTION__);
424 * This tests the following:
426 * It monitors the state of state.sock[0]
427 * with tevent_fd, but we never read/write on state.sock[0]
428 * while state.sock[1] * is only used to write a few bytes.
431 * - we wait only for TEVENT_FD_WRITE on state.sock[0]
432 * - we write 1 byte to state.sock[1]
433 * - we wait only for TEVENT_FD_READ on state.sock[0]
434 * - we disable events on state.sock[0]
435 * - the timer event restarts the loop
436 * Then we close state.sock[1]
438 * - we wait for TEVENT_FD_READ/WRITE on state.sock[0]
439 * - we try to read 1 byte
440 * - if the read gets an error of returns 0
441 * we disable the event handler
442 * - the timer finishes the test
447 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, state.sock);
448 torture_assert(tctx, ret == 0, "socketpair() failed");
450 state.te = tevent_add_timer(state.ev, state.ev,
451 timeval_current_ofs(0,10000),
452 test_event_fd1_finished, &state);
453 state.fde0 = tevent_add_fd(state.ev, state.ev,
454 state.sock[0], TEVENT_FD_WRITE,
455 test_event_fd1_fde_handler, &state);
456 /* state.fde1 is only used to auto close */
457 state.fde1 = tevent_add_fd(state.ev, state.ev,
459 test_event_fd1_fde_handler, &state);
461 tevent_fd_set_auto_close(state.fde0);
462 tevent_fd_set_auto_close(state.fde1);
464 while (!state.finished) {
466 if (tevent_loop_once(state.ev) == -1) {
467 talloc_free(state.ev);
468 torture_fail(tctx, talloc_asprintf(tctx,
469 "Failed event loop %s\n",
474 talloc_free(state.ev);
476 torture_assert(tctx, state.error == NULL, talloc_asprintf(tctx,
482 struct test_event_fd2_state {
483 struct torture_context *tctx;
485 struct tevent_context *ev;
486 struct tevent_timer *te;
487 struct test_event_fd2_sock {
488 struct test_event_fd2_state *state;
490 struct tevent_fd *fde;
499 static void test_event_fd2_sock_handler(struct tevent_context *ev_ctx,
500 struct tevent_fd *fde,
504 struct test_event_fd2_sock *cur_sock =
505 (struct test_event_fd2_sock *)private_data;
506 struct test_event_fd2_state *state = cur_sock->state;
507 struct test_event_fd2_sock *oth_sock = NULL;
511 if (cur_sock == &state->sock0) {
512 oth_sock = &state->sock1;
514 oth_sock = &state->sock0;
517 if (oth_sock->num_written == 1) {
518 if (flags != (TEVENT_FD_READ | TEVENT_FD_WRITE)) {
519 state->finished = true;
520 state->error = __location__;
525 if (cur_sock->num_read == oth_sock->num_written) {
526 state->finished = true;
527 state->error = __location__;
531 if (!(flags & TEVENT_FD_READ)) {
532 state->finished = true;
533 state->error = __location__;
537 if (oth_sock->num_read >= PIPE_BUF) {
539 * On Linux we become writable once we've read
540 * one byte. On Solaris we only become writable
541 * again once we've read 4096 bytes. PIPE_BUF
542 * is probably a safe bet to test against.
544 * There should be room to write a byte again
546 if (!(flags & TEVENT_FD_WRITE)) {
547 state->finished = true;
548 state->error = __location__;
553 if ((flags & TEVENT_FD_WRITE) && !cur_sock->got_full) {
554 v = (uint8_t)cur_sock->num_written;
555 ret = write(cur_sock->fd, &v, 1);
557 state->finished = true;
558 state->error = __location__;
561 cur_sock->num_written++;
562 if (cur_sock->num_written > 0x80000000) {
563 state->finished = true;
564 state->error = __location__;
570 if (!cur_sock->got_full) {
571 cur_sock->got_full = true;
573 if (!oth_sock->got_full) {
576 * lets wait for oth_sock
579 tevent_fd_set_flags(cur_sock->fde, 0);
584 * oth_sock waited for cur_sock,
587 tevent_fd_set_flags(oth_sock->fde,
588 TEVENT_FD_READ|TEVENT_FD_WRITE);
591 ret = read(cur_sock->fd, &v, 1);
593 state->finished = true;
594 state->error = __location__;
597 c = (uint8_t)cur_sock->num_read;
599 state->finished = true;
600 state->error = __location__;
603 cur_sock->num_read++;
605 if (cur_sock->num_read < oth_sock->num_written) {
606 /* there is more to read */
610 * we read everything, we need to remove TEVENT_FD_WRITE
613 TEVENT_FD_NOT_WRITEABLE(cur_sock->fde);
615 if (oth_sock->num_read == cur_sock->num_written) {
617 * both directions are finished
619 state->finished = true;
625 static void test_event_fd2_finished(struct tevent_context *ev_ctx,
626 struct tevent_timer *te,
630 struct test_event_fd2_state *state =
631 (struct test_event_fd2_state *)private_data;
634 * this should never be triggered
636 state->finished = true;
637 state->error = __location__;
640 static bool test_event_fd2(struct torture_context *tctx,
641 const void *test_data)
643 struct test_event_fd2_state state;
649 state.backend = (const char *)test_data;
651 state.ev = test_tevent_context_init_byname(tctx, state.backend);
652 if (state.ev == NULL) {
653 torture_skip(tctx, talloc_asprintf(tctx,
654 "event backend '%s' not supported\n",
659 torture_comment(tctx, "backend '%s' - %s\n",
660 state.backend, __FUNCTION__);
663 * This tests the following
665 * - We write 1 byte to each socket
666 * - We wait for TEVENT_FD_READ/WRITE on both sockets
667 * - When we get TEVENT_FD_WRITE we write 1 byte
668 * until both socket buffers are full, which
669 * means both sockets only get TEVENT_FD_READ.
670 * - Then we read 1 byte until we have consumed
671 * all bytes the other end has written.
675 socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
678 * the timer should never expire
680 state.te = tevent_add_timer(state.ev, state.ev,
681 timeval_current_ofs(600, 0),
682 test_event_fd2_finished, &state);
683 state.sock0.state = &state;
684 state.sock0.fd = sock[0];
685 state.sock0.fde = tevent_add_fd(state.ev, state.ev,
687 TEVENT_FD_READ | TEVENT_FD_WRITE,
688 test_event_fd2_sock_handler,
690 state.sock1.state = &state;
691 state.sock1.fd = sock[1];
692 state.sock1.fde = tevent_add_fd(state.ev, state.ev,
694 TEVENT_FD_READ | TEVENT_FD_WRITE,
695 test_event_fd2_sock_handler,
698 tevent_fd_set_auto_close(state.sock0.fde);
699 tevent_fd_set_auto_close(state.sock1.fde);
701 do_write(state.sock0.fd, &c, 1);
702 state.sock0.num_written++;
703 do_write(state.sock1.fd, &c, 1);
704 state.sock1.num_written++;
706 while (!state.finished) {
708 if (tevent_loop_once(state.ev) == -1) {
709 talloc_free(state.ev);
710 torture_fail(tctx, talloc_asprintf(tctx,
711 "Failed event loop %s\n",
716 talloc_free(state.ev);
718 torture_assert(tctx, state.error == NULL, talloc_asprintf(tctx,
724 struct test_wrapper_state {
725 struct torture_context *tctx;
727 int num_wrap_handlers;
730 static bool test_wrapper_before_use(struct tevent_context *wrap_ev,
732 struct tevent_context *main_ev,
733 const char *location)
735 struct test_wrapper_state *state =
736 talloc_get_type_abort(private_data,
737 struct test_wrapper_state);
739 torture_comment(state->tctx, "%s\n", __func__);
740 state->num_wrap_handlers++;
744 static void test_wrapper_after_use(struct tevent_context *wrap_ev,
746 struct tevent_context *main_ev,
747 const char *location)
749 struct test_wrapper_state *state =
750 talloc_get_type_abort(private_data,
751 struct test_wrapper_state);
753 torture_comment(state->tctx, "%s\n", __func__);
754 state->num_wrap_handlers++;
757 static void test_wrapper_before_fd_handler(struct tevent_context *wrap_ev,
759 struct tevent_context *main_ev,
760 struct tevent_fd *fde,
762 const char *handler_name,
763 const char *location)
765 struct test_wrapper_state *state =
766 talloc_get_type_abort(private_data,
767 struct test_wrapper_state);
769 torture_comment(state->tctx, "%s\n", __func__);
770 state->num_wrap_handlers++;
773 static void test_wrapper_after_fd_handler(struct tevent_context *wrap_ev,
775 struct tevent_context *main_ev,
776 struct tevent_fd *fde,
778 const char *handler_name,
779 const char *location)
781 struct test_wrapper_state *state =
782 talloc_get_type_abort(private_data,
783 struct test_wrapper_state);
785 torture_comment(state->tctx, "%s\n", __func__);
786 state->num_wrap_handlers++;
789 static void test_wrapper_before_timer_handler(struct tevent_context *wrap_ev,
791 struct tevent_context *main_ev,
792 struct tevent_timer *te,
793 struct timeval requested_time,
794 struct timeval trigger_time,
795 const char *handler_name,
796 const char *location)
798 struct test_wrapper_state *state =
799 talloc_get_type_abort(private_data,
800 struct test_wrapper_state);
802 torture_comment(state->tctx, "%s\n", __func__);
803 state->num_wrap_handlers++;
806 static void test_wrapper_after_timer_handler(struct tevent_context *wrap_ev,
808 struct tevent_context *main_ev,
809 struct tevent_timer *te,
810 struct timeval requested_time,
811 struct timeval trigger_time,
812 const char *handler_name,
813 const char *location)
815 struct test_wrapper_state *state =
816 talloc_get_type_abort(private_data,
817 struct test_wrapper_state);
819 torture_comment(state->tctx, "%s\n", __func__);
820 state->num_wrap_handlers++;
823 static void test_wrapper_before_immediate_handler(struct tevent_context *wrap_ev,
825 struct tevent_context *main_ev,
826 struct tevent_immediate *im,
827 const char *handler_name,
828 const char *location)
830 struct test_wrapper_state *state =
831 talloc_get_type_abort(private_data,
832 struct test_wrapper_state);
834 torture_comment(state->tctx, "%s\n", __func__);
835 state->num_wrap_handlers++;
838 static void test_wrapper_after_immediate_handler(struct tevent_context *wrap_ev,
840 struct tevent_context *main_ev,
841 struct tevent_immediate *im,
842 const char *handler_name,
843 const char *location)
845 struct test_wrapper_state *state =
846 talloc_get_type_abort(private_data,
847 struct test_wrapper_state);
849 torture_comment(state->tctx, "%s\n", __func__);
850 state->num_wrap_handlers++;
853 static void test_wrapper_before_signal_handler(struct tevent_context *wrap_ev,
855 struct tevent_context *main_ev,
856 struct tevent_signal *se,
860 const char *handler_name,
861 const char *location)
863 struct test_wrapper_state *state =
864 talloc_get_type_abort(private_data,
865 struct test_wrapper_state);
867 torture_comment(state->tctx, "%s\n", __func__);
868 state->num_wrap_handlers++;
871 static void test_wrapper_after_signal_handler(struct tevent_context *wrap_ev,
873 struct tevent_context *main_ev,
874 struct tevent_signal *se,
878 const char *handler_name,
879 const char *location)
881 struct test_wrapper_state *state =
882 talloc_get_type_abort(private_data,
883 struct test_wrapper_state);
885 torture_comment(state->tctx, "%s\n", __func__);
886 state->num_wrap_handlers++;
889 static const struct tevent_wrapper_ops test_wrapper_ops = {
890 .name = "test_wrapper",
891 .before_use = test_wrapper_before_use,
892 .after_use = test_wrapper_after_use,
893 .before_fd_handler = test_wrapper_before_fd_handler,
894 .after_fd_handler = test_wrapper_after_fd_handler,
895 .before_timer_handler = test_wrapper_before_timer_handler,
896 .after_timer_handler = test_wrapper_after_timer_handler,
897 .before_immediate_handler = test_wrapper_before_immediate_handler,
898 .after_immediate_handler = test_wrapper_after_immediate_handler,
899 .before_signal_handler = test_wrapper_before_signal_handler,
900 .after_signal_handler = test_wrapper_after_signal_handler,
903 static void test_wrapper_timer_handler(struct tevent_context *ev,
904 struct tevent_timer *te,
908 struct test_wrapper_state *state =
909 (struct test_wrapper_state *)private_data;
912 torture_comment(state->tctx, "timer handler\n");
919 static void test_wrapper_fd_handler(struct tevent_context *ev,
920 struct tevent_fd *fde,
921 unsigned short fd_flags,
924 struct test_wrapper_state *state =
925 (struct test_wrapper_state *)private_data;
927 torture_comment(state->tctx, "fd handler\n");
934 static void test_wrapper_immediate_handler(struct tevent_context *ev,
935 struct tevent_immediate *im,
938 struct test_wrapper_state *state =
939 (struct test_wrapper_state *)private_data;
944 torture_comment(state->tctx, "immediate handler\n");
948 static void test_wrapper_signal_handler(struct tevent_context *ev,
949 struct tevent_signal *se,
955 struct test_wrapper_state *state =
956 (struct test_wrapper_state *)private_data;
958 torture_comment(state->tctx, "signal handler\n");
965 static bool test_wrapper(struct torture_context *tctx,
966 const void *test_data)
968 struct test_wrapper_state *state = NULL;
969 int sock[2] = { -1, -1};
971 const int num_events = 4;
972 const char *backend = (const char *)test_data;
973 struct tevent_context *ev = NULL;
974 struct tevent_context *wrap_ev = NULL;
975 struct tevent_fd *fde = NULL;
976 struct tevent_timer *te = NULL;
977 struct tevent_signal *se = NULL;
978 struct tevent_immediate *im = NULL;
983 ev = test_tevent_context_init_byname(tctx, backend);
985 torture_skip(tctx, talloc_asprintf(tctx,
986 "event backend '%s' not supported\n",
991 torture_comment(tctx, "tevent backend '%s'\n", backend);
993 wrap_ev = tevent_context_wrapper_create(
994 ev, ev, &test_wrapper_ops, &state, struct test_wrapper_state);
995 torture_assert_not_null_goto(tctx, wrap_ev, ok, done,
996 "tevent_context_wrapper_create failed\n");
997 *state = (struct test_wrapper_state) {
1001 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
1002 torture_assert_goto(tctx, ret == 0, ok, done, "socketpair failed\n");
1004 te = tevent_add_timer(wrap_ev, wrap_ev,
1005 timeval_current_ofs(0, 0),
1006 test_wrapper_timer_handler, state);
1007 torture_assert_not_null_goto(tctx, te, ok, done,
1008 "tevent_add_timer failed\n");
1010 fde = tevent_add_fd(wrap_ev, wrap_ev,
1013 test_wrapper_fd_handler,
1015 torture_assert_not_null_goto(tctx, fde, ok, done,
1016 "tevent_add_fd failed\n");
1018 im = tevent_create_immediate(wrap_ev);
1019 torture_assert_not_null_goto(tctx, im, ok, done,
1020 "tevent_create_immediate failed\n");
1022 se = tevent_add_signal(wrap_ev, wrap_ev,
1025 test_wrapper_signal_handler,
1027 torture_assert_not_null_goto(tctx, se, ok, done,
1028 "tevent_add_signal failed\n");
1030 do_write(sock[0], &c, 1);
1031 kill(getpid(), SIGUSR1);
1032 tevent_schedule_immediate(im,
1034 test_wrapper_immediate_handler,
1037 ret2 = tevent_context_push_use(wrap_ev);
1038 torture_assert_goto(tctx, ret2, ok, done, "tevent_context_push_use(wrap_ev) failed\n");
1039 ret2 = tevent_context_push_use(ev);
1040 torture_assert_goto(tctx, ret2, ok, pop_use, "tevent_context_push_use(ev) failed\n");
1041 tevent_context_pop_use(ev);
1042 tevent_context_pop_use(wrap_ev);
1044 ret = tevent_loop_wait(ev);
1045 torture_assert_int_equal_goto(tctx, ret, 0, ok, done, "tevent_loop_wait failed\n");
1047 torture_comment(tctx, "Num events: %d\n", state->num_events);
1048 torture_comment(tctx, "Num wrap handlers: %d\n",
1049 state->num_wrap_handlers);
1051 torture_assert_int_equal_goto(tctx, state->num_events, num_events, ok, done,
1052 "Wrong event count\n");
1053 torture_assert_int_equal_goto(tctx, state->num_wrap_handlers,
1055 ok, done, "Wrong wrapper count\n");
1060 TALLOC_FREE(wrap_ev);
1063 if (sock[0] != -1) {
1066 if (sock[1] != -1) {
1071 tevent_context_pop_use(wrap_ev);
1075 static void test_free_wrapper_signal_handler(struct tevent_context *ev,
1076 struct tevent_signal *se,
1082 struct torture_context *tctx =
1083 talloc_get_type_abort(private_data,
1084 struct torture_context);
1086 torture_comment(tctx, "signal handler\n");
1091 * signal handlers have highest priority in tevent, so this signal
1092 * handler will always be started before the other handlers
1093 * below. Freeing the (wrapper) event context here tests that the
1094 * wrapper implementation correclty handles the wrapper ev going away
1095 * with pending events.
1101 static void test_free_wrapper_fd_handler(struct tevent_context *ev,
1102 struct tevent_fd *fde,
1103 unsigned short fd_flags,
1107 * This should never be called as
1108 * test_free_wrapper_signal_handler()
1109 * already destroyed the wrapper tevent_context.
1114 static void test_free_wrapper_immediate_handler(struct tevent_context *ev,
1115 struct tevent_immediate *im,
1119 * This should never be called as
1120 * test_free_wrapper_signal_handler()
1121 * already destroyed the wrapper tevent_context.
1126 static void test_free_wrapper_timer_handler(struct tevent_context *ev,
1127 struct tevent_timer *te,
1132 * This should never be called as
1133 * test_free_wrapper_signal_handler()
1134 * already destroyed the wrapper tevent_context.
1139 static bool test_free_wrapper(struct torture_context *tctx,
1140 const void *test_data)
1142 struct test_wrapper_state *state = NULL;
1143 int sock[2] = { -1, -1};
1145 const char *backend = (const char *)test_data;
1146 TALLOC_CTX *frame = talloc_stackframe();
1147 struct tevent_context *ev = NULL;
1148 struct tevent_context *wrap_ev = NULL;
1149 struct tevent_fd *fde = NULL;
1150 struct tevent_timer *te = NULL;
1151 struct tevent_signal *se = NULL;
1152 struct tevent_immediate *im = NULL;
1156 ev = test_tevent_context_init_byname(frame, backend);
1158 torture_skip(tctx, talloc_asprintf(tctx,
1159 "event backend '%s' not supported\n",
1164 torture_comment(tctx, "tevent backend '%s'\n", backend);
1166 wrap_ev = tevent_context_wrapper_create(
1167 ev, ev, &test_wrapper_ops, &state, struct test_wrapper_state);
1168 torture_assert_not_null_goto(tctx, wrap_ev, ok, done,
1169 "tevent_context_wrapper_create failed\n");
1170 *state = (struct test_wrapper_state) {
1174 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
1175 torture_assert_goto(tctx, ret == 0, ok, done, "socketpair failed\n");
1177 fde = tevent_add_fd(wrap_ev, frame,
1180 test_free_wrapper_fd_handler,
1182 torture_assert_not_null_goto(tctx, fde, ok, done,
1183 "tevent_add_fd failed\n");
1185 te = tevent_add_timer(wrap_ev, frame,
1186 timeval_current_ofs(0, 0),
1187 test_free_wrapper_timer_handler, NULL);
1188 torture_assert_not_null_goto(tctx, te, ok, done,
1189 "tevent_add_timer failed\n");
1191 im = tevent_create_immediate(frame);
1192 torture_assert_not_null_goto(tctx, im, ok, done,
1193 "tevent_create_immediate failed\n");
1195 se = tevent_add_signal(wrap_ev, frame,
1198 test_free_wrapper_signal_handler,
1200 torture_assert_not_null_goto(tctx, se, ok, done,
1201 "tevent_add_signal failed\n");
1203 do_write(sock[0], &c, 1);
1204 kill(getpid(), SIGUSR1);
1205 tevent_schedule_immediate(im,
1207 test_free_wrapper_immediate_handler,
1210 ret = tevent_loop_wait(ev);
1211 torture_assert_goto(tctx, ret == 0, ok, done, "tevent_loop_wait failed\n");
1218 if (sock[0] != -1) {
1221 if (sock[1] != -1) {
1229 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
1230 static bool do_shutdown = false;
1232 static void test_event_threaded_lock(void)
1235 ret = pthread_mutex_lock(&threaded_mutex);
1239 static void test_event_threaded_unlock(void)
1242 ret = pthread_mutex_unlock(&threaded_mutex);
1246 static void test_event_threaded_trace(enum tevent_trace_point point,
1250 case TEVENT_TRACE_BEFORE_WAIT:
1251 test_event_threaded_unlock();
1253 case TEVENT_TRACE_AFTER_WAIT:
1254 test_event_threaded_lock();
1256 case TEVENT_TRACE_BEFORE_LOOP_ONCE:
1257 case TEVENT_TRACE_AFTER_LOOP_ONCE:
1262 static void test_event_threaded_timer(struct tevent_context *ev,
1263 struct tevent_timer *te,
1264 struct timeval current_time,
1270 static void *test_event_poll_thread(void *private_data)
1272 struct tevent_context *ev = (struct tevent_context *)private_data;
1274 test_event_threaded_lock();
1278 ret = tevent_loop_once(ev);
1281 test_event_threaded_unlock();
1288 static void test_event_threaded_read_handler(struct tevent_context *ev,
1289 struct tevent_fd *fde,
1293 int *pfd = (int *)private_data;
1297 if ((flags & TEVENT_FD_READ) == 0) {
1302 nread = read(*pfd, &c, 1);
1303 } while ((nread == -1) && (errno == EINTR));
1308 static bool test_event_context_threaded(struct torture_context *test,
1309 const void *test_data)
1311 struct tevent_context *ev;
1312 struct tevent_timer *te;
1313 struct tevent_fd *fde;
1314 pthread_t poll_thread;
1319 ev = test_tevent_context_init_byname(test, "poll_mt");
1320 torture_assert(test, ev != NULL, "poll_mt not supported");
1322 tevent_set_trace_callback(ev, test_event_threaded_trace, NULL);
1324 te = tevent_add_timer(ev, ev, timeval_current_ofs(5, 0),
1325 test_event_threaded_timer, NULL);
1326 torture_assert(test, te != NULL, "Could not add timer");
1328 ret = pthread_create(&poll_thread, NULL, test_event_poll_thread, ev);
1329 torture_assert(test, ret == 0, "Could not create poll thread");
1332 torture_assert(test, ret == 0, "Could not create pipe");
1336 test_event_threaded_lock();
1338 fde = tevent_add_fd(ev, ev, fds[0], TEVENT_FD_READ,
1339 test_event_threaded_read_handler, &fds[0]);
1340 torture_assert(test, fde != NULL, "Could not add fd event");
1342 test_event_threaded_unlock();
1346 do_write(fds[1], &c, 1);
1350 test_event_threaded_lock();
1352 test_event_threaded_unlock();
1354 do_write(fds[1], &c, 1);
1356 ret = pthread_join(poll_thread, NULL);
1357 torture_assert(test, ret == 0, "pthread_join failed");
1362 #define NUM_TEVENT_THREADS 100
1364 /* Ugly, but needed for torture_comment... */
1365 static struct torture_context *thread_test_ctx;
1366 static pthread_t thread_map[NUM_TEVENT_THREADS];
1367 static unsigned thread_counter;
1369 /* Called in master thread context */
1370 static void callback_nowait(struct tevent_context *ev,
1371 struct tevent_immediate *im,
1374 pthread_t *thread_id_ptr =
1375 talloc_get_type_abort(private_ptr, pthread_t);
1378 for (i = 0; i < NUM_TEVENT_THREADS; i++) {
1379 if (pthread_equal(*thread_id_ptr,
1384 torture_comment(thread_test_ctx,
1385 "Callback %u from thread %u\n",
1391 /* Blast the master tevent_context with a callback, no waiting. */
1392 static void *thread_fn_nowait(void *private_ptr)
1394 struct tevent_thread_proxy *master_tp =
1395 talloc_get_type_abort(private_ptr, struct tevent_thread_proxy);
1396 struct tevent_immediate *im;
1397 pthread_t *thread_id_ptr;
1399 im = tevent_create_immediate(NULL);
1403 thread_id_ptr = talloc(NULL, pthread_t);
1404 if (thread_id_ptr == NULL) {
1407 *thread_id_ptr = pthread_self();
1409 tevent_thread_proxy_schedule(master_tp,
1416 static void timeout_fn(struct tevent_context *ev,
1417 struct tevent_timer *te,
1418 struct timeval tv, void *p)
1420 thread_counter = NUM_TEVENT_THREADS * 10;
1423 static bool test_multi_tevent_threaded(struct torture_context *test,
1424 const void *test_data)
1427 struct tevent_context *master_ev;
1428 struct tevent_thread_proxy *tp;
1430 talloc_disable_null_tracking();
1432 /* Ugly global stuff. */
1433 thread_test_ctx = test;
1436 master_ev = test_tevent_context_init(NULL);
1437 if (master_ev == NULL) {
1441 tp = tevent_thread_proxy_create(master_ev);
1444 talloc_asprintf(test,
1445 "tevent_thread_proxy_create failed\n"));
1446 talloc_free(master_ev);
1450 for (i = 0; i < NUM_TEVENT_THREADS; i++) {
1451 int ret = pthread_create(&thread_map[i],
1457 talloc_asprintf(test,
1458 "Failed to create thread %i, %d\n",
1464 /* Ensure we don't wait more than 10 seconds. */
1465 tevent_add_timer(master_ev,
1467 timeval_current_ofs(10,0),
1471 while (thread_counter < NUM_TEVENT_THREADS) {
1472 int ret = tevent_loop_once(master_ev);
1473 torture_assert(test, ret == 0, "tevent_loop_once failed");
1476 torture_assert(test, thread_counter == NUM_TEVENT_THREADS,
1477 "thread_counter fail\n");
1479 talloc_free(master_ev);
1483 struct reply_state {
1484 struct tevent_thread_proxy *reply_tp;
1485 pthread_t thread_id;
1489 static void thread_timeout_fn(struct tevent_context *ev,
1490 struct tevent_timer *te,
1491 struct timeval tv, void *p)
1493 int *p_finished = (int *)p;
1498 /* Called in child-thread context */
1499 static void thread_callback(struct tevent_context *ev,
1500 struct tevent_immediate *im,
1503 struct reply_state *rsp =
1504 talloc_get_type_abort(private_ptr, struct reply_state);
1506 talloc_steal(ev, rsp);
1507 *rsp->p_finished = 1;
1510 /* Called in master thread context */
1511 static void master_callback(struct tevent_context *ev,
1512 struct tevent_immediate *im,
1515 struct reply_state *rsp =
1516 talloc_get_type_abort(private_ptr, struct reply_state);
1519 talloc_steal(ev, rsp);
1521 for (i = 0; i < NUM_TEVENT_THREADS; i++) {
1522 if (pthread_equal(rsp->thread_id,
1527 torture_comment(thread_test_ctx,
1528 "Callback %u from thread %u\n",
1531 /* Now reply to the thread ! */
1532 tevent_thread_proxy_schedule(rsp->reply_tp,
1540 static void *thread_fn_1(void *private_ptr)
1542 struct tevent_thread_proxy *master_tp =
1543 talloc_get_type_abort(private_ptr, struct tevent_thread_proxy);
1544 struct tevent_thread_proxy *tp;
1545 struct tevent_immediate *im;
1546 struct tevent_context *ev;
1547 struct reply_state *rsp;
1551 ev = tevent_context_init(NULL);
1556 tp = tevent_thread_proxy_create(ev);
1562 im = tevent_create_immediate(ev);
1568 rsp = talloc(ev, struct reply_state);
1574 rsp->thread_id = pthread_self();
1576 rsp->p_finished = &finished;
1578 /* Introduce a little randomness into the mix.. */
1579 usleep(random() % 7000);
1581 tevent_thread_proxy_schedule(master_tp,
1586 /* Ensure we don't wait more than 10 seconds. */
1587 tevent_add_timer(ev,
1589 timeval_current_ofs(10,0),
1593 while (finished == 0) {
1594 ret = tevent_loop_once(ev);
1604 * NB. We should talloc_free(ev) here, but if we do
1605 * we currently get hit by helgrind Fix #323432
1606 * "When calling pthread_cond_destroy or pthread_mutex_destroy
1607 * with initializers as argument Helgrind (incorrectly) reports errors."
1609 * http://valgrind.10908.n7.nabble.com/Helgrind-3-9-0-false-positive-
1610 * with-pthread-mutex-destroy-td47757.html
1612 * Helgrind doesn't understand that the request/reply
1613 * messages provide synchronization between the lock/unlock
1614 * in tevent_thread_proxy_schedule(), and the pthread_destroy()
1615 * when the struct tevent_thread_proxy object is talloc_free'd.
1617 * As a work-around for now return ev for the parent thread to free.
1622 static bool test_multi_tevent_threaded_1(struct torture_context *test,
1623 const void *test_data)
1626 struct tevent_context *master_ev;
1627 struct tevent_thread_proxy *master_tp;
1630 talloc_disable_null_tracking();
1632 /* Ugly global stuff. */
1633 thread_test_ctx = test;
1636 master_ev = test_tevent_context_init(NULL);
1637 if (master_ev == NULL) {
1641 master_tp = tevent_thread_proxy_create(master_ev);
1642 if (master_tp == NULL) {
1644 talloc_asprintf(test,
1645 "tevent_thread_proxy_create failed\n"));
1646 talloc_free(master_ev);
1650 for (i = 0; i < NUM_TEVENT_THREADS; i++) {
1651 ret = pthread_create(&thread_map[i],
1657 talloc_asprintf(test,
1658 "Failed to create thread %i, %d\n",
1664 while (thread_counter < NUM_TEVENT_THREADS) {
1665 ret = tevent_loop_once(master_ev);
1666 torture_assert(test, ret == 0, "tevent_loop_once failed");
1669 /* Wait for all the threads to finish - join 'em. */
1670 for (i = 0; i < NUM_TEVENT_THREADS; i++) {
1672 ret = pthread_join(thread_map[i], &retval);
1673 torture_assert(test, ret == 0, "pthread_join failed");
1674 /* Free the child thread event context. */
1675 talloc_free(retval);
1678 talloc_free(master_ev);
1682 struct threaded_test_2 {
1683 struct tevent_threaded_context *tctx;
1684 struct tevent_immediate *im;
1685 pthread_t thread_id;
1688 static void master_callback_2(struct tevent_context *ev,
1689 struct tevent_immediate *im,
1690 void *private_data);
1692 static void *thread_fn_2(void *private_data)
1694 struct threaded_test_2 *state = private_data;
1696 state->thread_id = pthread_self();
1698 usleep(random() % 7000);
1700 tevent_threaded_schedule_immediate(
1701 state->tctx, state->im, master_callback_2, state);
1706 static void master_callback_2(struct tevent_context *ev,
1707 struct tevent_immediate *im,
1710 struct threaded_test_2 *state = private_data;
1713 for (i = 0; i < NUM_TEVENT_THREADS; i++) {
1714 if (pthread_equal(state->thread_id, thread_map[i])) {
1718 torture_comment(thread_test_ctx,
1719 "Callback_2 %u from thread %u\n",
1725 static bool test_multi_tevent_threaded_2(struct torture_context *test,
1726 const void *test_data)
1730 struct tevent_context *ev;
1731 struct tevent_threaded_context *tctx;
1734 thread_test_ctx = test;
1737 ev = test_tevent_context_init(test);
1738 torture_assert(test, ev != NULL, "tevent_context_init failed");
1741 * tevent_re_initialise used to have a bug where it did not
1742 * re-initialise the thread support after taking it
1743 * down. Exercise that code path.
1745 ret = tevent_re_initialise(ev);
1746 torture_assert(test, ret == 0, "tevent_re_initialise failed");
1748 tctx = tevent_threaded_context_create(ev, ev);
1749 torture_assert(test, tctx != NULL,
1750 "tevent_threaded_context_create failed");
1752 for (i=0; i<NUM_TEVENT_THREADS; i++) {
1753 struct threaded_test_2 *state;
1755 state = talloc(ev, struct threaded_test_2);
1756 torture_assert(test, state != NULL, "talloc failed");
1759 state->im = tevent_create_immediate(state);
1760 torture_assert(test, state->im != NULL,
1761 "tevent_create_immediate failed");
1763 ret = pthread_create(&thread_map[i], NULL, thread_fn_2, state);
1764 torture_assert(test, ret == 0, "pthread_create failed");
1767 while (thread_counter < NUM_TEVENT_THREADS) {
1768 ret = tevent_loop_once(ev);
1769 torture_assert(test, ret == 0, "tevent_loop_once failed");
1772 /* Wait for all the threads to finish - join 'em. */
1773 for (i = 0; i < NUM_TEVENT_THREADS; i++) {
1775 ret = pthread_join(thread_map[i], &retval);
1776 torture_assert(test, ret == 0, "pthread_join failed");
1777 /* Free the child thread event context. */
1785 struct test_cached_pid_thread_state {
1786 pid_t thread_cached_pid;
1790 static void *test_cached_pid_thread(void *private_data)
1792 struct test_cached_pid_thread_state *state =
1793 (struct test_cached_pid_thread_state *)private_data;
1795 state->thread_cached_pid = tevent_cached_getpid();
1796 state->thread_pid = getpid();
1802 static bool test_cached_pid(struct torture_context *test,
1803 const void *test_data)
1805 pid_t parent_pid = getpid();
1810 torture_assert(test, tevent_cached_getpid() == parent_pid, "tevent_cached_getpid()");
1814 struct test_cached_pid_thread_state state = { .thread_cached_pid = -1, };
1816 void *retval = NULL;
1819 ret = pthread_create(&thread, NULL, test_cached_pid_thread, &state);
1820 torture_assert(test, ret == 0, "pthread_create failed");
1822 ret = pthread_join(thread, &retval);
1823 torture_assert(test, ret == 0, "pthread_join failed");
1825 torture_assert(test, state.thread_pid == parent_pid, "getpid() in thread");
1826 torture_assert(test, state.thread_cached_pid == parent_pid, "tevent_cached_getpid() in thread");
1828 #endif /* HAVE_PTHREAD */
1831 if (child_pid == 0) {
1833 pid_t pid = getpid();
1834 pid_t cached_pid = tevent_cached_getpid();
1836 if (parent_pid == pid) {
1839 if (pid != cached_pid) {
1844 torture_assert(test, child_pid > 0, "fork failed");
1846 finished_pid = waitpid(child_pid, &child_status, 0);
1847 torture_assert(test, finished_pid == child_pid, "wrong child");
1848 torture_assert(test, child_status == 0, "child_status");
1853 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
1855 struct torture_suite *suite = torture_suite_create(mem_ctx, "event");
1856 const char **list = tevent_backend_list(suite);
1859 for (i=0;list && list[i];i++) {
1860 struct torture_suite *backend_suite;
1862 backend_suite = torture_suite_create(mem_ctx, list[i]);
1864 torture_suite_add_simple_tcase_const(backend_suite,
1867 (const void *)list[i]);
1868 torture_suite_add_simple_tcase_const(backend_suite,
1871 (const void *)list[i]);
1872 torture_suite_add_simple_tcase_const(backend_suite,
1875 (const void *)list[i]);
1876 torture_suite_add_simple_tcase_const(backend_suite,
1879 (const void *)list[i]);
1880 torture_suite_add_simple_tcase_const(backend_suite,
1883 (const void *)list[i]);
1885 torture_suite_add_suite(suite, backend_suite);
1889 torture_suite_add_simple_tcase_const(suite, "threaded_poll_mt",
1890 test_event_context_threaded,
1893 torture_suite_add_simple_tcase_const(suite, "multi_tevent_threaded",
1894 test_multi_tevent_threaded,
1897 torture_suite_add_simple_tcase_const(suite, "multi_tevent_threaded_1",
1898 test_multi_tevent_threaded_1,
1901 torture_suite_add_simple_tcase_const(suite, "multi_tevent_threaded_2",
1902 test_multi_tevent_threaded_2,
1907 torture_suite_add_simple_tcase_const(suite, "tevent_cached_getpid",