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
|
/*
Infrastructure for event context wrappers
Copyright (C) Stefan Metzmacher 2014
** 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/>.
*/
#include "replace.h"
#ifdef HAVE_PTHREAD
#include "system/threads.h"
#endif
#define TEVENT_DEPRECATED 1
#include "tevent.h"
#include "tevent_internal.h"
#include "tevent_util.h"
static int tevent_wrapper_glue_context_init(struct tevent_context *ev)
{
tevent_abort(ev, "tevent_wrapper_glue_context_init() called");
errno = ENOSYS;
return -1;
}
static struct tevent_fd *tevent_wrapper_glue_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)
{
struct tevent_wrapper_glue *glue = ev->wrapper.glue;
struct tevent_fd *fde = NULL;
if (glue->destroyed) {
tevent_abort(ev, "add_fd wrapper use after free");
return NULL;
}
if (glue->main_ev == NULL) {
errno = EINVAL;
return NULL;
}
fde = _tevent_add_fd(glue->main_ev, mem_ctx, fd, flags,
handler, private_data,
handler_name, location);
if (fde == NULL) {
return NULL;
}
fde->wrapper = glue;
return fde;
}
static struct tevent_timer *tevent_wrapper_glue_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)
{
struct tevent_wrapper_glue *glue = ev->wrapper.glue;
struct tevent_timer *te = NULL;
if (glue->destroyed) {
tevent_abort(ev, "add_timer wrapper use after free");
return NULL;
}
if (glue->main_ev == NULL) {
errno = EINVAL;
return NULL;
}
te = _tevent_add_timer(glue->main_ev, mem_ctx, next_event,
handler, private_data,
handler_name, location);
if (te == NULL) {
return NULL;
}
te->wrapper = glue;
return te;
}
static void tevent_wrapper_glue_schedule_immediate(struct tevent_immediate *im,
struct tevent_context *ev,
tevent_immediate_handler_t handler,
void *private_data,
const char *handler_name,
const char *location)
{
struct tevent_wrapper_glue *glue = ev->wrapper.glue;
if (glue->destroyed) {
tevent_abort(ev, "scheduke_immediate wrapper use after free");
return;
}
if (glue->main_ev == NULL) {
tevent_abort(ev, location);
errno = EINVAL;
return;
}
_tevent_schedule_immediate(im, glue->main_ev,
handler, private_data,
handler_name, location);
im->wrapper = glue;
return;
}
static struct tevent_signal *tevent_wrapper_glue_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)
{
struct tevent_wrapper_glue *glue = ev->wrapper.glue;
struct tevent_signal *se = NULL;
if (glue->destroyed) {
tevent_abort(ev, "add_signal wrapper use after free");
return NULL;
}
if (glue->main_ev == NULL) {
errno = EINVAL;
return NULL;
}
se = _tevent_add_signal(glue->main_ev, mem_ctx,
signum, sa_flags,
handler, private_data,
handler_name, location);
if (se == NULL) {
return NULL;
}
se->wrapper = glue;
return se;
}
static int tevent_wrapper_glue_loop_once(struct tevent_context *ev, const char *location)
{
tevent_abort(ev, "tevent_wrapper_glue_loop_once() called");
errno = ENOSYS;
return -1;
}
static int tevent_wrapper_glue_loop_wait(struct tevent_context *ev, const char *location)
{
tevent_abort(ev, "tevent_wrapper_glue_loop_wait() called");
errno = ENOSYS;
return -1;
}
static const struct tevent_ops tevent_wrapper_glue_ops = {
.context_init = tevent_wrapper_glue_context_init,
.add_fd = tevent_wrapper_glue_add_fd,
.set_fd_close_fn = tevent_common_fd_set_close_fn,
.get_fd_flags = tevent_common_fd_get_flags,
.set_fd_flags = tevent_common_fd_set_flags,
.add_timer = tevent_wrapper_glue_add_timer,
.schedule_immediate = tevent_wrapper_glue_schedule_immediate,
.add_signal = tevent_wrapper_glue_add_signal,
.loop_once = tevent_wrapper_glue_loop_once,
.loop_wait = tevent_wrapper_glue_loop_wait,
};
static int tevent_wrapper_context_destructor(struct tevent_context *wrap_ev)
{
struct tevent_wrapper_glue *glue = wrap_ev->wrapper.glue;
struct tevent_context *main_ev = NULL;
struct tevent_fd *fd = NULL, *fn = NULL;
struct tevent_timer *te = NULL, *tn = NULL;
struct tevent_immediate *ie = NULL, *in = NULL;
struct tevent_signal *se = NULL, *sn = NULL;
#ifdef HAVE_PTHREAD
struct tevent_threaded_context *tctx = NULL, *tctxn = NULL;
#endif
if (glue == NULL) {
tevent_abort(wrap_ev,
"tevent_wrapper_context_destructor() active on main");
/* static checker support, return below is never reached */
return -1;
}
if (glue->destroyed && glue->busy) {
tevent_common_check_double_free(wrap_ev,
"tevent_context wrapper double free");
}
glue->destroyed = true;
if (glue->busy) {
return -1;
}
main_ev = glue->main_ev;
if (main_ev == NULL) {
return 0;
}
TEVENT_DEBUG(wrap_ev, TEVENT_DEBUG_TRACE,
"Destroying wrapper context %p \"%s\"\n",
wrap_ev, talloc_get_name(glue->private_state));
glue->main_ev = NULL;
DLIST_REMOVE(main_ev->wrapper.list, glue);
#ifdef HAVE_PTHREAD
for (tctx = main_ev->threaded_contexts; tctx != NULL; tctx = tctxn) {
int ret;
tctxn = tctx->next;
if (tctx->event_ctx != glue->wrap_ev) {
continue;
}
ret = pthread_mutex_lock(&tctx->event_ctx_mutex);
if (ret != 0) {
abort();
}
/*
* Indicate to the thread that the tevent_context is
* gone. The counterpart of this is in
* _tevent_threaded_schedule_immediate, there we read
* this under the threaded_context's mutex.
*/
tctx->event_ctx = NULL;
ret = pthread_mutex_unlock(&tctx->event_ctx_mutex);
if (ret != 0) {
abort();
}
DLIST_REMOVE(main_ev->threaded_contexts, tctx);
}
#endif
for (fd = main_ev->fd_events; fd; fd = fn) {
fn = fd->next;
if (fd->wrapper != glue) {
continue;
}
tevent_fd_set_flags(fd, 0);
tevent_common_fd_disarm(fd);
}
for (te = main_ev->timer_events; te; te = tn) {
tn = te->next;
if (te->wrapper != glue) {
continue;
}
te->wrapper = NULL;
te->event_ctx = NULL;
if (main_ev->last_zero_timer == te) {
main_ev->last_zero_timer = DLIST_PREV(te);
}
DLIST_REMOVE(main_ev->timer_events, te);
}
for (ie = main_ev->immediate_events; ie; ie = in) {
in = ie->next;
if (ie->wrapper != glue) {
continue;
}
ie->wrapper = NULL;
ie->event_ctx = NULL;
ie->cancel_fn = NULL;
DLIST_REMOVE(main_ev->immediate_events, ie);
}
for (se = main_ev->signal_events; se; se = sn) {
sn = se->next;
if (se->wrapper != glue) {
continue;
}
se->wrapper = NULL;
tevent_cleanup_pending_signal_handlers(se);
}
return 0;
}
struct tevent_context *_tevent_context_wrapper_create(struct tevent_context *main_ev,
TALLOC_CTX *mem_ctx,
const struct tevent_wrapper_ops *ops,
void *pstate,
size_t psize,
const char *type,
const char *location)
{
void **ppstate = (void **)pstate;
struct tevent_context *ev = NULL;
if (main_ev->wrapper.glue != NULL) {
/*
* stacking of wrappers is not supported
*/
tevent_debug(main_ev->wrapper.glue->main_ev, TEVENT_DEBUG_FATAL,
"%s: %s() stacking not allowed\n",
__func__, location);
errno = EINVAL;
return NULL;
}
if (main_ev->nesting.allowed) {
/*
* wrappers conflict with nesting
*/
tevent_debug(main_ev, TEVENT_DEBUG_FATAL,
"%s: %s() conflicts with nesting\n",
__func__, location);
errno = EINVAL;
return NULL;
}
ev = talloc_zero(mem_ctx, struct tevent_context);
if (ev == NULL) {
return NULL;
}
ev->ops = &tevent_wrapper_glue_ops;
ev->wrapper.glue = talloc_zero(ev, struct tevent_wrapper_glue);
if (ev->wrapper.glue == NULL) {
talloc_free(ev);
return NULL;
}
talloc_set_destructor(ev, tevent_wrapper_context_destructor);
ev->wrapper.glue->wrap_ev = ev;
ev->wrapper.glue->main_ev = main_ev;
ev->wrapper.glue->ops = ops;
ev->wrapper.glue->private_state = talloc_zero_size(ev->wrapper.glue, psize);
if (ev->wrapper.glue->private_state == NULL) {
talloc_free(ev);
return NULL;
}
talloc_set_name_const(ev->wrapper.glue->private_state, type);
DLIST_ADD_END(main_ev->wrapper.list, ev->wrapper.glue);
*ppstate = ev->wrapper.glue->private_state;
return ev;
}
bool tevent_context_is_wrapper(struct tevent_context *ev)
{
if (ev->wrapper.glue != NULL) {
return true;
}
return false;
}
_PRIVATE_
struct tevent_context *tevent_wrapper_main_ev(struct tevent_context *ev)
{
if (ev == NULL) {
return NULL;
}
if (ev->wrapper.glue == NULL) {
return ev;
}
return ev->wrapper.glue->main_ev;
}
/*
* 32 stack elements should be more than enough
*
* e.g. Samba uses just 8 elements for [un]become_{root,user}()
*/
#define TEVENT_WRAPPER_STACK_SIZE 32
static struct tevent_wrapper_stack {
const void *ev_ptr;
const struct tevent_wrapper_glue *wrapper;
} wrapper_stack[TEVENT_WRAPPER_STACK_SIZE];
static size_t wrapper_stack_idx;
_PRIVATE_
void tevent_wrapper_push_use_internal(struct tevent_context *ev,
struct tevent_wrapper_glue *wrapper)
{
/*
* ev and wrapper need to belong together!
* It's also fine to only have a raw ev
* without a wrapper.
*/
if (unlikely(ev->wrapper.glue != wrapper)) {
tevent_abort(ev, "tevent_wrapper_push_use_internal() invalid arguments");
return;
}
if (wrapper != NULL) {
if (unlikely(wrapper->busy)) {
tevent_abort(ev, "wrapper already busy!");
return;
}
wrapper->busy = true;
}
if (unlikely(wrapper_stack_idx >= TEVENT_WRAPPER_STACK_SIZE)) {
tevent_abort(ev, "TEVENT_WRAPPER_STACK_SIZE overflow");
return;
}
wrapper_stack[wrapper_stack_idx] = (struct tevent_wrapper_stack) {
.ev_ptr = ev,
.wrapper = wrapper,
};
wrapper_stack_idx++;
}
_PRIVATE_
void tevent_wrapper_pop_use_internal(const struct tevent_context *__ev_ptr,
struct tevent_wrapper_glue *wrapper)
{
struct tevent_context *main_ev = NULL;
/*
* Note that __ev_ptr might a a stale pointer and should not
* be touched, we just compare the pointer value in order
* to enforce the stack order.
*/
if (wrapper != NULL) {
main_ev = wrapper->main_ev;
}
if (unlikely(wrapper_stack_idx == 0)) {
tevent_abort(main_ev, "tevent_wrapper stack already empty");
return;
}
wrapper_stack_idx--;
if (wrapper != NULL) {
wrapper->busy = false;
}
if (wrapper_stack[wrapper_stack_idx].ev_ptr != __ev_ptr) {
tevent_abort(main_ev, "tevent_wrapper_pop_use mismatch ev!");
return;
}
if (wrapper_stack[wrapper_stack_idx].wrapper != wrapper) {
tevent_abort(main_ev, "tevent_wrapper_pop_use mismatch wrap!");
return;
}
if (wrapper == NULL) {
return;
}
if (wrapper->destroyed) {
/*
* Notice that we can't use TALLOC_FREE()
* here because wrapper is a talloc child
* of wrapper->wrap_ev.
*/
talloc_free(wrapper->wrap_ev);
}
}
bool _tevent_context_push_use(struct tevent_context *ev,
const char *location)
{
bool ok;
if (ev->wrapper.glue == NULL) {
tevent_wrapper_push_use_internal(ev, NULL);
return true;
}
if (ev->wrapper.glue->main_ev == NULL) {
return false;
}
tevent_wrapper_push_use_internal(ev, ev->wrapper.glue);
ok = ev->wrapper.glue->ops->before_use(ev->wrapper.glue->wrap_ev,
ev->wrapper.glue->private_state,
ev->wrapper.glue->main_ev,
location);
if (!ok) {
tevent_wrapper_pop_use_internal(ev, ev->wrapper.glue);
return false;
}
return true;
}
void _tevent_context_pop_use(struct tevent_context *ev,
const char *location)
{
tevent_wrapper_pop_use_internal(ev, ev->wrapper.glue);
if (ev->wrapper.glue == NULL) {
return;
}
if (ev->wrapper.glue->main_ev == NULL) {
return;
}
ev->wrapper.glue->ops->after_use(ev->wrapper.glue->wrap_ev,
ev->wrapper.glue->private_state,
ev->wrapper.glue->main_ev,
location);
}
bool tevent_context_same_loop(struct tevent_context *ev1,
struct tevent_context *ev2)
{
struct tevent_context *main_ev1 = tevent_wrapper_main_ev(ev1);
struct tevent_context *main_ev2 = tevent_wrapper_main_ev(ev2);
if (main_ev1 == NULL) {
return false;
}
if (main_ev1 == main_ev2) {
return true;
}
return false;
}
|