|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Arran Cudbard-Bell <a.cudbardb@freeradius.org> |
| 3 | + * |
| 4 | + * Permission to use, copy, modify, and distribute this software for any |
| 5 | + * purpose with or without fee is hereby granted, provided that the above |
| 6 | + * copyright notice and this permission notice appear in all copies. |
| 7 | + * |
| 8 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "common.h" |
| 18 | + |
| 19 | +struct trigger_args { |
| 20 | + int kqfd; |
| 21 | + uintptr_t ident; |
| 22 | +}; |
| 23 | + |
| 24 | +static void * |
| 25 | +close_kqueue(void *arg) |
| 26 | +{ |
| 27 | + /* Sleep until we're fairly sure the other thread is waiting */ |
| 28 | + sleep(1); |
| 29 | + |
| 30 | + /* Close the waiting kqueue from this thread */ |
| 31 | + if (close(*((int *)arg)) != 0) |
| 32 | + die("close failed"); |
| 33 | + |
| 34 | + return NULL; |
| 35 | +} |
| 36 | + |
| 37 | +static void |
| 38 | +test_kevent_threading_close(struct test_context *ctx) |
| 39 | +{ |
| 40 | + struct kevent kev, ret[1]; |
| 41 | + pthread_t th; |
| 42 | + int kqfd = kqueue(); |
| 43 | + |
| 44 | + /* Add the event, then trigger it from another thread */ |
| 45 | + kevent_add(kqfd, &kev, 1, EVFILT_USER, EV_ADD | EV_CLEAR, 0, 0, NULL); |
| 46 | + |
| 47 | + if (pthread_create(&th, NULL, close_kqueue, &kqfd) != 0) |
| 48 | + die("failed creating thread"); |
| 49 | + |
| 50 | + /* Wait for event (should be interrupted by close) */ |
| 51 | + if (kevent(kqfd, NULL, 0, ret, 1, NULL) == -1) { |
| 52 | + if (errno != EBADF) |
| 53 | + die("kevent failed with the wrong errno"); |
| 54 | + } else { |
| 55 | + die("kevent did not fail"); |
| 56 | + } |
| 57 | + |
| 58 | + /* Subsequent calls should also fail with EBADF */ |
| 59 | + if (kevent(kqfd, NULL, 0, ret, 1, NULL) == -1) { |
| 60 | + if (errno != EBADF) |
| 61 | + die("kevent failed with the wrong errno (second call)"); |
| 62 | + } else { |
| 63 | + die("kevent did not fail (second call)"); |
| 64 | + } |
| 65 | + |
| 66 | + if (pthread_join(th, NULL) != 0) |
| 67 | + die("pthread_join failed"); |
| 68 | +} |
| 69 | + |
| 70 | +void |
| 71 | +test_threading(struct test_context *ctx) |
| 72 | +{ |
| 73 | + test(kevent_threading_close, ctx); |
| 74 | +} |
0 commit comments