forked from samba-team/samba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tevent.h
2019 lines (1862 loc) · 61.3 KB
/
tevent.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Unix SMB/CIFS implementation.
generalised event loop handling
Copyright (C) Andrew Tridgell 2005
Copyright (C) Stefan Metzmacher 2005-2009
Copyright (C) Volker Lendecke 2008
** NOTE! The following LGPL license applies to the tevent
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TEVENT_H__
#define __TEVENT_H__
#include <stdint.h>
#include <talloc.h>
#include <sys/time.h>
#include <stdbool.h>
struct tevent_context;
struct tevent_ops;
struct tevent_fd;
struct tevent_timer;
struct tevent_immediate;
struct tevent_signal;
struct tevent_thread_proxy;
struct tevent_threaded_context;
/**
* @defgroup tevent The tevent API
*
* The tevent low-level API
*
* This API provides the public interface to manage events in the tevent
* mainloop. Functions are provided for managing low-level events such
* as timer events, fd events and signal handling.
*
* @{
*/
/* event handler types */
/**
* Called when a file descriptor monitored by tevent has
* data to be read or written on it.
*/
typedef void (*tevent_fd_handler_t)(struct tevent_context *ev,
struct tevent_fd *fde,
uint16_t flags,
void *private_data);
/**
* Called when tevent is ceasing the monitoring of a file descriptor.
*/
typedef void (*tevent_fd_close_fn_t)(struct tevent_context *ev,
struct tevent_fd *fde,
int fd,
void *private_data);
/**
* Called when a tevent timer has fired.
*/
typedef void (*tevent_timer_handler_t)(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval current_time,
void *private_data);
/**
* Called when a tevent immediate event is invoked.
*/
typedef void (*tevent_immediate_handler_t)(struct tevent_context *ctx,
struct tevent_immediate *im,
void *private_data);
/**
* Called after tevent detects the specified signal.
*/
typedef void (*tevent_signal_handler_t)(struct tevent_context *ev,
struct tevent_signal *se,
int signum,
int count,
void *siginfo,
void *private_data);
/**
* @brief Create a event_context structure.
*
* This must be the first events call, and all subsequent calls pass this
* event_context as the first element. Event handlers also receive this as
* their first argument.
*
* @param[in] mem_ctx The memory context to use.
*
* @return An allocated tevent context, NULL on error.
*
* @see tevent_context_init()
*/
struct tevent_context *tevent_context_init(TALLOC_CTX *mem_ctx);
/**
* @brief Create a event_context structure and select a specific backend.
*
* This must be the first events call, and all subsequent calls pass this
* event_context as the first element. Event handlers also receive this as
* their first argument.
*
* @param[in] mem_ctx The memory context to use.
*
* @param[in] name The name of the backend to use.
*
* @return An allocated tevent context, NULL on error.
*/
struct tevent_context *tevent_context_init_byname(TALLOC_CTX *mem_ctx, const char *name);
/**
* @brief Create a custom event context
*
* @param[in] mem_ctx The memory context to use.
* @param[in] ops The function pointer table of the backend.
* @param[in] additional_data The additional/private data to this instance
*
* @return An allocated tevent context, NULL on error.
*
*/
struct tevent_context *tevent_context_init_ops(TALLOC_CTX *mem_ctx,
const struct tevent_ops *ops,
void *additional_data);
/**
* @brief List available backends.
*
* @param[in] mem_ctx The memory context to use.
*
* @return A string vector with a terminating NULL element, NULL
* on error.
*/
const char **tevent_backend_list(TALLOC_CTX *mem_ctx);
/**
* @brief Set the default tevent backend.
*
* @param[in] backend The name of the backend to set.
*/
void tevent_set_default_backend(const char *backend);
#ifdef DOXYGEN
/**
* @brief Add a file descriptor based event.
*
* @param[in] ev The event context to work on.
*
* @param[in] mem_ctx The talloc memory context to use.
*
* @param[in] fd The file descriptor to base the event on.
*
* @param[in] flags #TEVENT_FD_READ or #TEVENT_FD_WRITE
*
* @param[in] handler The callback handler for the event.
*
* @param[in] private_data The private data passed to the callback handler.
*
* @return The file descriptor based event, NULL on error.
*
* @note To cancel the monitoring of a file descriptor, call talloc_free()
* on the object returned by this function.
*
* @note The caller should avoid closing the file descriptor before
* calling talloc_free()! Otherwise the behaviour is undefined which
* might result in crashes. See https://bugzilla.samba.org/show_bug.cgi?id=11141
* for an example.
*/
struct tevent_fd *tevent_add_fd(struct tevent_context *ev,
TALLOC_CTX *mem_ctx,
int fd,
uint16_t flags,
tevent_fd_handler_t handler,
void *private_data);
#else
struct tevent_fd *_tevent_add_fd(struct tevent_context *ev,
TALLOC_CTX *mem_ctx,
int fd,
uint16_t flags,
tevent_fd_handler_t handler,
void *private_data,
const char *handler_name,
const char *location);
#define tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
_tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data, \
#handler, __location__)
#endif
#ifdef DOXYGEN
/**
* @brief Add a timed event
*
* @param[in] ev The event context to work on.
*
* @param[in] mem_ctx The talloc memory context to use.
*
* @param[in] next_event Timeval specifying the absolute time to fire this
* event. This is not an offset.
*
* @param[in] handler The callback handler for the event.
*
* @param[in] private_data The private data passed to the callback handler.
*
* @return The newly-created timer event, or NULL on error.
*
* @note To cancel a timer event before it fires, call talloc_free() on the
* event returned from this function. This event is automatically
* talloc_free()-ed after its event handler files, if it hasn't been freed yet.
*
* @note Unlike some mainloops, tevent timers are one-time events. To set up
* a recurring event, it is necessary to call tevent_add_timer() again during
* the handler processing.
*
* @note Due to the internal mainloop processing, a timer set to run
* immediately will do so after any other pending timers fire, but before
* any further file descriptor or signal handling events fire. Callers should
* not rely on this behavior!
*/
struct tevent_timer *tevent_add_timer(struct tevent_context *ev,
TALLOC_CTX *mem_ctx,
struct timeval next_event,
tevent_timer_handler_t handler,
void *private_data);
#else
struct tevent_timer *_tevent_add_timer(struct tevent_context *ev,
TALLOC_CTX *mem_ctx,
struct timeval next_event,
tevent_timer_handler_t handler,
void *private_data,
const char *handler_name,
const char *location);
#define tevent_add_timer(ev, mem_ctx, next_event, handler, private_data) \
_tevent_add_timer(ev, mem_ctx, next_event, handler, private_data, \
#handler, __location__)
#endif
/**
* @brief Set the time a tevent_timer fires
*
* @param[in] te The timer event to reset
*
* @param[in] next_event Timeval specifying the absolute time to fire this
* event. This is not an offset.
*/
void tevent_update_timer(struct tevent_timer *te, struct timeval next_event);
#ifdef DOXYGEN
/**
* Initialize an immediate event object
*
* This object can be used to trigger an event to occur immediately after
* returning from the current event (before any other event occurs)
*
* @param[in] mem_ctx The talloc memory context to use as the parent
*
* @return An empty tevent_immediate object. Use tevent_schedule_immediate
* to populate and use it.
*
* @note Available as of tevent 0.9.8
*/
struct tevent_immediate *tevent_create_immediate(TALLOC_CTX *mem_ctx);
#else
struct tevent_immediate *_tevent_create_immediate(TALLOC_CTX *mem_ctx,
const char *location);
#define tevent_create_immediate(mem_ctx) \
_tevent_create_immediate(mem_ctx, __location__)
#endif
#ifdef DOXYGEN
/**
* Schedule an event for immediate execution. This event will occur
* immediately after returning from the current event (before any other
* event occurs)
*
* @param[in] im The tevent_immediate object to populate and use
* @param[in] ctx The tevent_context to run this event
* @param[in] handler The event handler to run when this event fires
* @param[in] private_data Data to pass to the event handler
*/
void tevent_schedule_immediate(struct tevent_immediate *im,
struct tevent_context *ctx,
tevent_immediate_handler_t handler,
void *private_data);
#else
void _tevent_schedule_immediate(struct tevent_immediate *im,
struct tevent_context *ctx,
tevent_immediate_handler_t handler,
void *private_data,
const char *handler_name,
const char *location);
#define tevent_schedule_immediate(im, ctx, handler, private_data) \
_tevent_schedule_immediate(im, ctx, handler, private_data, \
#handler, __location__);
#endif
#ifdef DOXYGEN
/**
* @brief Add a tevent signal handler
*
* tevent_add_signal() creates a new event for handling a signal the next
* time through the mainloop. It implements a very simple traditional signal
* handler whose only purpose is to add the handler event into the mainloop.
*
* @param[in] ev The event context to work on.
*
* @param[in] mem_ctx The talloc memory context to use.
*
* @param[in] signum The signal to trap
*
* @param[in] handler The callback handler for the signal.
*
* @param[in] sa_flags sigaction flags for this signal handler.
*
* @param[in] private_data The private data passed to the callback handler.
*
* @return The newly-created signal handler event, or NULL on error.
*
* @note To cancel a signal handler, call talloc_free() on the event returned
* from this function.
*
* @see tevent_num_signals, tevent_sa_info_queue_count
*/
struct tevent_signal *tevent_add_signal(struct tevent_context *ev,
TALLOC_CTX *mem_ctx,
int signum,
int sa_flags,
tevent_signal_handler_t handler,
void *private_data);
#else
struct tevent_signal *_tevent_add_signal(struct tevent_context *ev,
TALLOC_CTX *mem_ctx,
int signum,
int sa_flags,
tevent_signal_handler_t handler,
void *private_data,
const char *handler_name,
const char *location);
#define tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
_tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data, \
#handler, __location__)
#endif
/**
* @brief the number of supported signals
*
* This returns value of the configure time TEVENT_NUM_SIGNALS constant.
*
* The 'signum' argument of tevent_add_signal() must be less than
* TEVENT_NUM_SIGNALS.
*
* @see tevent_add_signal
*/
size_t tevent_num_signals(void);
/**
* @brief the number of pending realtime signals
*
* This returns value of TEVENT_SA_INFO_QUEUE_COUNT.
*
* The tevent internals remember the last TEVENT_SA_INFO_QUEUE_COUNT
* siginfo_t structures for SA_SIGINFO signals. If the system generates
* more some signals get lost.
*
* @see tevent_add_signal
*/
size_t tevent_sa_info_queue_count(void);
#ifdef DOXYGEN
/**
* @brief Pass a single time through the mainloop
*
* This will process any appropriate signal, immediate, fd and timer events
*
* @param[in] ev The event context to process
*
* @return Zero on success, nonzero if an internal error occurred
*/
int tevent_loop_once(struct tevent_context *ev);
#else
int _tevent_loop_once(struct tevent_context *ev, const char *location);
#define tevent_loop_once(ev) \
_tevent_loop_once(ev, __location__)
#endif
#ifdef DOXYGEN
/**
* @brief Run the mainloop
*
* The mainloop will run until there are no events remaining to be processed
*
* @param[in] ev The event context to process
*
* @return Zero if all events have been processed. Nonzero if an internal
* error occurred.
*/
int tevent_loop_wait(struct tevent_context *ev);
#else
int _tevent_loop_wait(struct tevent_context *ev, const char *location);
#define tevent_loop_wait(ev) \
_tevent_loop_wait(ev, __location__)
#endif
/**
* Assign a function to run when a tevent_fd is freed
*
* This function is a destructor for the tevent_fd. It does not automatically
* close the file descriptor. If this is the desired behavior, then it must be
* performed by the close_fn.
*
* @param[in] fde File descriptor event on which to set the destructor
* @param[in] close_fn Destructor to execute when fde is freed
*/
void tevent_fd_set_close_fn(struct tevent_fd *fde,
tevent_fd_close_fn_t close_fn);
/**
* Automatically close the file descriptor when the tevent_fd is freed
*
* This function calls close(fd) internally.
*
* @param[in] fde File descriptor event to auto-close
*/
void tevent_fd_set_auto_close(struct tevent_fd *fde);
/**
* Return the flags set on this file descriptor event
*
* @param[in] fde File descriptor event to query
*
* @return The flags set on the event. See #TEVENT_FD_READ and
* #TEVENT_FD_WRITE
*/
uint16_t tevent_fd_get_flags(struct tevent_fd *fde);
/**
* Set flags on a file descriptor event
*
* @param[in] fde File descriptor event to set
* @param[in] flags Flags to set on the event. See #TEVENT_FD_READ and
* #TEVENT_FD_WRITE
*/
void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
/**
* Query whether tevent supports signal handling
*
* @param[in] ev An initialized tevent context
*
* @return True if this platform and tevent context support signal handling
*/
bool tevent_signal_support(struct tevent_context *ev);
void tevent_set_abort_fn(void (*abort_fn)(const char *reason));
/* bits for file descriptor event flags */
/**
* Monitor a file descriptor for data to be read
*/
#define TEVENT_FD_READ 1
/**
* Monitor a file descriptor for writeability
*/
#define TEVENT_FD_WRITE 2
/**
* Convenience function for declaring a tevent_fd writable
*/
#define TEVENT_FD_WRITEABLE(fde) \
tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_WRITE)
/**
* Convenience function for declaring a tevent_fd readable
*/
#define TEVENT_FD_READABLE(fde) \
tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_READ)
/**
* Convenience function for declaring a tevent_fd non-writable
*/
#define TEVENT_FD_NOT_WRITEABLE(fde) \
tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_WRITE)
/**
* Convenience function for declaring a tevent_fd non-readable
*/
#define TEVENT_FD_NOT_READABLE(fde) \
tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_READ)
/**
* Debug level of tevent
*/
enum tevent_debug_level {
TEVENT_DEBUG_FATAL,
TEVENT_DEBUG_ERROR,
TEVENT_DEBUG_WARNING,
TEVENT_DEBUG_TRACE
};
/**
* @brief The tevent debug callbac.
*
* @param[in] context The memory context to use.
*
* @param[in] level The debug level.
*
* @param[in] fmt The format string.
*
* @param[in] ap The arguments for the format string.
*/
typedef void (*tevent_debug_fn)(void *context,
enum tevent_debug_level level,
const char *fmt,
va_list ap) PRINTF_ATTRIBUTE(3,0);
/**
* Set destination for tevent debug messages
*
* @param[in] ev Event context to debug
* @param[in] debug Function to handle output printing
* @param[in] context The context to pass to the debug function.
*
* @return Always returns 0 as of version 0.9.8
*
* @note Default is to emit no debug messages
*/
int tevent_set_debug(struct tevent_context *ev,
tevent_debug_fn debug,
void *context);
/**
* Designate stderr for debug message output
*
* @param[in] ev Event context to debug
*
* @note This function will only output TEVENT_DEBUG_FATAL, TEVENT_DEBUG_ERROR
* and TEVENT_DEBUG_WARNING messages. For TEVENT_DEBUG_TRACE, please define a
* function for tevent_set_debug()
*/
int tevent_set_debug_stderr(struct tevent_context *ev);
enum tevent_trace_point {
/**
* Corresponds to a trace point just before waiting
*/
TEVENT_TRACE_BEFORE_WAIT,
/**
* Corresponds to a trace point just after waiting
*/
TEVENT_TRACE_AFTER_WAIT,
#define TEVENT_HAS_LOOP_ONCE_TRACE_POINTS 1
/**
* Corresponds to a trace point just before calling
* the loop_once() backend function.
*/
TEVENT_TRACE_BEFORE_LOOP_ONCE,
/**
* Corresponds to a trace point right after the
* loop_once() backend function has returned.
*/
TEVENT_TRACE_AFTER_LOOP_ONCE,
};
typedef void (*tevent_trace_callback_t)(enum tevent_trace_point,
void *private_data);
/**
* Register a callback to be called at certain trace points
*
* @param[in] ev Event context
* @param[in] cb Trace callback
* @param[in] private_data Data to be passed to callback
*
* @note The callback will be called at trace points defined by
* tevent_trace_point. Call with NULL to reset.
*/
void tevent_set_trace_callback(struct tevent_context *ev,
tevent_trace_callback_t cb,
void *private_data);
/**
* Retrieve the current trace callback
*
* @param[in] ev Event context
* @param[out] cb Registered trace callback
* @param[out] private_data Registered data to be passed to callback
*
* @note This can be used to allow one component that wants to
* register a callback to respect the callback that another component
* has already registered.
*/
void tevent_get_trace_callback(struct tevent_context *ev,
tevent_trace_callback_t *cb,
void *private_data);
/**
* @}
*/
/**
* @defgroup tevent_request The tevent request functions.
* @ingroup tevent
*
* A tevent_req represents an asynchronous computation.
*
* The tevent_req group of API calls is the recommended way of
* programming async computations within tevent. In particular the
* file descriptor (tevent_add_fd) and timer (tevent_add_timed) events
* are considered too low-level to be used in larger computations. To
* read and write from and to sockets, Samba provides two calls on top
* of tevent_add_fd: tstream_read_packet_send/recv and tstream_writev_send/recv.
* These requests are much easier to compose than the low-level event
* handlers called from tevent_add_fd.
*
* A lot of the simplicity tevent_req has brought to the notoriously
* hairy async programming came via a set of conventions that every
* async computation programmed should follow. One central piece of
* these conventions is the naming of routines and variables.
*
* Every async computation needs a name (sensibly called "computation"
* down from here). From this name quite a few naming conventions are
* derived.
*
* Every computation that requires local state needs a
* @code
* struct computation_state {
* int local_var;
* };
* @endcode
* Even if no local variables are required, such a state struct should
* be created containing a dummy variable. Quite a few helper
* functions and macros (for example tevent_req_create()) assume such
* a state struct.
*
* An async computation is started by a computation_send
* function. When it is finished, its result can be received by a
* computation_recv function. For an example how to set up an async
* computation, see the code example in the documentation for
* tevent_req_create() and tevent_req_post(). The prototypes for _send
* and _recv functions should follow some conventions:
*
* @code
* struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
* struct tevent_req *ev,
* ... further args);
* int computation_recv(struct tevent_req *req, ... further output args);
* @endcode
*
* The "int" result of computation_recv() depends on the result the
* sync version of the function would have, "int" is just an example
* here.
*
* Another important piece of the conventions is that the program flow
* is interrupted as little as possible. Because a blocking
* sub-computation requires that the flow needs to continue in a
* separate function that is the logical sequel of some computation,
* it should lexically follow sending off the blocking
* sub-computation. Setting the callback function via
* tevent_req_set_callback() requires referencing a function lexically
* below the call to tevent_req_set_callback(), forward declarations
* are required. A lot of the async computations thus begin with a
* sequence of declarations such as
*
* @code
* static void computation_step1_done(struct tevent_req *subreq);
* static void computation_step2_done(struct tevent_req *subreq);
* static void computation_step3_done(struct tevent_req *subreq);
* @endcode
*
* It really helps readability a lot to do these forward declarations,
* because the lexically sequential program flow makes the async
* computations almost as clear to read as a normal, sync program
* flow.
*
* It is up to the user of the async computation to talloc_free it
* after it has finished. If an async computation should be aborted,
* the tevent_req structure can be talloc_free'ed. After it has
* finished, it should talloc_free'ed by the API user.
*
* @{
*/
/**
* An async request moves from TEVENT_REQ_INIT to
* TEVENT_REQ_IN_PROGRESS. All other states are valid after a request
* has finished.
*/
enum tevent_req_state {
/**
* We are creating the request
*/
TEVENT_REQ_INIT,
/**
* We are waiting the request to complete
*/
TEVENT_REQ_IN_PROGRESS,
/**
* The request is finished successfully
*/
TEVENT_REQ_DONE,
/**
* A user error has occurred. The user error has been
* indicated by tevent_req_error(), it can be retrieved via
* tevent_req_is_error().
*/
TEVENT_REQ_USER_ERROR,
/**
* Request timed out after the timeout set by tevent_req_set_endtime.
*/
TEVENT_REQ_TIMED_OUT,
/**
* An internal allocation has failed, or tevent_req_nomem has
* been given a NULL pointer as the first argument.
*/
TEVENT_REQ_NO_MEMORY,
/**
* The request has been received by the caller. No further
* action is valid.
*/
TEVENT_REQ_RECEIVED
};
/**
* @brief An async request
*/
struct tevent_req;
/**
* @brief A tevent request callback function.
*
* @param[in] req The tevent async request which executed this callback.
*/
typedef void (*tevent_req_fn)(struct tevent_req *req);
/**
* @brief Set an async request callback.
*
* See the documentation of tevent_req_post() for an example how this
* is supposed to be used.
*
* @param[in] req The async request to set the callback.
*
* @param[in] fn The callback function to set.
*
* @param[in] pvt A pointer to private data to pass to the async request
* callback.
*/
void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt);
#ifdef DOXYGEN
/**
* @brief Get the private data cast to the given type for a callback from
* a tevent request structure.
*
* @code
* static void computation_done(struct tevent_req *subreq) {
* struct tevent_req *req = tevent_req_callback_data(subreq, struct tevent_req);
* struct computation_state *state = tevent_req_data(req, struct computation_state);
* .... more things, eventually maybe call tevent_req_done(req);
* }
* @endcode
*
* @param[in] req The structure to get the callback data from.
*
* @param[in] type The type of the private callback data to get.
*
* @return The type casted private data set NULL if not set.
*/
void *tevent_req_callback_data(struct tevent_req *req, #type);
#else
void *_tevent_req_callback_data(struct tevent_req *req);
#define tevent_req_callback_data(_req, _type) \
talloc_get_type_abort(_tevent_req_callback_data(_req), _type)
#endif
#ifdef DOXYGEN
/**
* @brief Get the private data for a callback from a tevent request structure.
*
* @param[in] req The structure to get the callback data from.
*
* @param[in] req The structure to get the data from.
*
* @return The private data or NULL if not set.
*/
void *tevent_req_callback_data_void(struct tevent_req *req);
#else
#define tevent_req_callback_data_void(_req) \
_tevent_req_callback_data(_req)
#endif
#ifdef DOXYGEN
/**
* @brief Get the private data from a tevent request structure.
*
* When the tevent_req has been created by tevent_req_create, the
* result of tevent_req_data() is the state variable created by
* tevent_req_create() as a child of the req.
*
* @param[in] req The structure to get the private data from.
*
* @param[in] type The type of the private data
*
* @return The private data or NULL if not set.
*/
void *tevent_req_data(struct tevent_req *req, #type);
#else
void *_tevent_req_data(struct tevent_req *req);
#define tevent_req_data(_req, _type) \
talloc_get_type_abort(_tevent_req_data(_req), _type)
#endif
/**
* @brief The print function which can be set for a tevent async request.
*
* @param[in] req The tevent async request.
*
* @param[in] ctx A talloc memory context which can be uses to allocate
* memory.
*
* @return An allocated string buffer to print.
*
* Example:
* @code
* static char *my_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
* {
* struct my_data *data = tevent_req_data(req, struct my_data);
* char *result;
*
* result = tevent_req_default_print(mem_ctx, req);
* if (result == NULL) {
* return NULL;
* }
*
* return talloc_asprintf_append_buffer(result, "foo=%d, bar=%d",
* data->foo, data->bar);
* }
* @endcode
*/
typedef char *(*tevent_req_print_fn)(struct tevent_req *req, TALLOC_CTX *ctx);
/**
* @brief This function sets a print function for the given request.
*
* This function can be used to setup a print function for the given request.
* This will be triggered if the tevent_req_print() function was
* called on the given request.
*
* @param[in] req The request to use.
*
* @param[in] fn A pointer to the print function
*
* @note This function should only be used for debugging.
*/
void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn);
/**
* @brief The default print function for creating debug messages.
*
* The function should not be used by users of the async API,
* but custom print function can use it and append custom text
* to the string.
*
* @param[in] req The request to be printed.
*
* @param[in] mem_ctx The memory context for the result.
*
* @return Text representation of request.
*
*/
char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx);
/**
* @brief Print an tevent_req structure in debug messages.
*
* This function should be used by callers of the async API.
*
* @param[in] mem_ctx The memory context for the result.
*
* @param[in] req The request to be printed.
*
* @return Text representation of request.
*/
char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req);
/**
* @brief A typedef for a cancel function for a tevent request.
*
* @param[in] req The tevent request calling this function.
*
* @return True if the request could be canceled, false if not.
*/
typedef bool (*tevent_req_cancel_fn)(struct tevent_req *req);
/**
* @brief This function sets a cancel function for the given tevent request.
*
* This function can be used to setup a cancel function for the given request.
* This will be triggered if the tevent_req_cancel() function was
* called on the given request.
*
* @param[in] req The request to use.
*
* @param[in] fn A pointer to the cancel function.
*/
void tevent_req_set_cancel_fn(struct tevent_req *req, tevent_req_cancel_fn fn);
#ifdef DOXYGEN
/**
* @brief Try to cancel the given tevent request.
*
* This function can be used to cancel the given request.
*
* It is only possible to cancel a request when the implementation
* has registered a cancel function via the tevent_req_set_cancel_fn().
*
* @param[in] req The request to use.
*
* @return This function returns true is the request is cancelable,
* othererwise false is returned.
*
* @note Even if the function returns true, the caller need to wait
* for the function to complete normally.
* Only the _recv() function of the given request indicates
* if the request was really canceled.
*/
bool tevent_req_cancel(struct tevent_req *req);
#else
bool _tevent_req_cancel(struct tevent_req *req, const char *location);
#define tevent_req_cancel(req) \
_tevent_req_cancel(req, __location__)
#endif
/**
* @brief A typedef for a cleanup function for a tevent request.
*
* @param[in] req The tevent request calling this function.
*
* @param[in] req_state The current tevent_req_state.
*
*/
typedef void (*tevent_req_cleanup_fn)(struct tevent_req *req,
enum tevent_req_state req_state);
/**
* @brief This function sets a cleanup function for the given tevent request.
*
* This function can be used to setup a cleanup function for the given request.
* This will be triggered when the tevent_req_done() or tevent_req_error()
* function was called, before notifying the callers callback function,
* and also before scheduling the deferred trigger.
*
* This might be useful if more than one tevent_req belong together
* and need to finish both requests at the same time.
*
* The cleanup function is able to call tevent_req_done() or tevent_req_error()
* recursively, the cleanup function is only triggered the first time.
*
* The cleanup function is also called by tevent_req_received()
* (possibly triggered from tevent_req_destructor()) before destroying
* the private data of the tevent_req.
*
* @param[in] req The request to use.
*
* @param[in] fn A pointer to the cancel function.
*/
void tevent_req_set_cleanup_fn(struct tevent_req *req, tevent_req_cleanup_fn fn);
#ifdef DOXYGEN
/**
* @brief Create an async tevent request.
*
* The new async request will be initialized in state TEVENT_REQ_IN_PROGRESS.
*
* @code
* struct tevent_req *req;
* struct computation_state *state;
* req = tevent_req_create(mem_ctx, &state, struct computation_state);
* @endcode
*