Loading...
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 | /* * Copyright (c) 2017-2024 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <assert.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <unistd.h> #include <uuid/uuid.h> #include <sys/select.h> #include <poll.h> #include <sys/event.h> #include <sys/sysctl.h> #include <pthread.h> #include <darwintest.h> #include "skywalk_test_driver.h" #include "skywalk_test_common.h" #include "skywalk_test_utils.h" /* Gets channel waiting in kevent/poll/select and then shuts down channel */ static void * thread1(void *unused) { int error; usleep(100000); /* Make sure main thread gets into kevent */ //sleep(5); /* Make sure main thread gets into kevent */ T_LOG("shutdown sockets\n"); error = pid_shutdown_sockets(getpid(), SHUTDOWN_SOCKET_LEVEL_DISCONNECT_ALL); SKTC_ASSERT_ERR(!error); return NULL; } static int skt_shutdown2_common(int argc, char *argv[], int method) { int error; channel_t channel; uuid_t channel_uuid; int channelfd; fd_set rfdset, efdset; struct pollfd fds; int kq; pthread_t thread; struct kevent kev; error = uuid_parse(argv[3], channel_uuid); SKTC_ASSERT_ERR(!error); channel = sktu_channel_create_extended(channel_uuid, 0, CHANNEL_DIR_TX_RX, CHANNEL_RING_ID_ANY, NULL, -1, -1, -1, -1, -1, -1, 1, -1, -1); assert(channel); channelfd = os_channel_get_fd(channel); assert(channelfd != -1); error = pthread_create(&thread, NULL, &thread1, NULL); SKTC_ASSERT_ERR(error == 0); switch (method) { case 0: FD_ZERO(&rfdset); FD_ZERO(&efdset); FD_SET(channelfd, &rfdset); FD_SET(channelfd, &efdset); error = select(channelfd + 1, &rfdset, NULL, &efdset, NULL); SKTC_ASSERT_ERR(error != -1); SKTC_ASSERT_ERR(error == 1); assert(!FD_ISSET(channelfd, &rfdset)); assert(FD_ISSET(channelfd, &efdset)); break; case 1: fds.fd = channelfd; fds.events = POLLRDNORM; fds.revents = 0; error = poll(&fds, 1, -1); SKTC_ASSERT_ERR(error != -1); SKTC_ASSERT_ERR(error == 1); assert(fds.fd == channelfd); assert(fds.events == POLLRDNORM); assert(fds.revents == (POLLRDNORM | POLLHUP)); break; case 2: kq = kqueue(); assert(kq != -1); EV_SET(&kev, channelfd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, NULL); error = kevent(kq, &kev, 1, &kev, 1, NULL); SKTC_ASSERT_ERR(error != -1); SKTC_ASSERT_ERR(error == 1); assert(kev.filter == EVFILT_READ); assert(kev.ident == channelfd); assert(kev.udata == NULL); assert(kev.flags & EV_EOF); assert(kev.data == 0); close(kq); break; default: abort(); } os_channel_destroy(channel); error = pthread_join(thread, NULL); SKTC_ASSERT_ERR(error == 0); return 0; } static int skt_shutdown2_select_main(int argc, char *argv[]) { return skt_shutdown2_common(argc, argv, 0); } static int skt_shutdown2_poll_main(int argc, char *argv[]) { return skt_shutdown2_common(argc, argv, 1); } static int skt_shutdown2_kqueue_main(int argc, char *argv[]) { return skt_shutdown2_common(argc, argv, 2); } struct skywalk_test skt_shutdown2us = { "shutdown2us", "shuts down channel on upipe while in select", SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE, skt_shutdown2_select_main, SKTC_GENERIC_UPIPE_ARGV, sktc_generic_upipe_nexus_init, sktc_cleanup_nexus, }; struct skywalk_test skt_shutdown2ks = { "shutdown2ks", "shuts down channel on kpipe while in select", SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_KERNEL_PIPE | SK_FEATURE_NEXUS_KERNEL_PIPE_LOOPBACK, skt_shutdown2_select_main, SKTC_GENERIC_KPIPE_ARGV, sktc_generic_kpipe_init, sktc_generic_kpipe_fini, }; struct skywalk_test skt_shutdown2up = { "shutdown2up", "shuts down channel on upipe while in poll", SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE, skt_shutdown2_poll_main, SKTC_GENERIC_UPIPE_ARGV, sktc_generic_upipe_nexus_init, sktc_cleanup_nexus, }; struct skywalk_test skt_shutdown2kp = { "shutdown2kp", "shuts down channel on kpipe while in poll", SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_KERNEL_PIPE | SK_FEATURE_NEXUS_KERNEL_PIPE_LOOPBACK, skt_shutdown2_poll_main, SKTC_GENERIC_KPIPE_ARGV, sktc_generic_kpipe_init, sktc_generic_kpipe_fini, }; struct skywalk_test skt_shutdown2uk = { "shutdown2uk", "shuts down channel on upipe while in kqueue", SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_USER_PIPE, skt_shutdown2_kqueue_main, SKTC_GENERIC_UPIPE_ARGV, sktc_generic_upipe_nexus_init, sktc_cleanup_nexus, }; struct skywalk_test skt_shutdown2kk = { "shutdown2kk", "shuts down channel on kpipe while in kqueue", SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_KERNEL_PIPE | SK_FEATURE_NEXUS_KERNEL_PIPE_LOOPBACK, skt_shutdown2_kqueue_main, SKTC_GENERIC_KPIPE_ARGV, sktc_generic_kpipe_init, sktc_generic_kpipe_fini, }; |