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 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 | #ifdef T_NAMESPACE #undef T_NAMESPACE #endif /* T_NAMESPACE */ #include <Block.h> #include <darwintest.h> #include <dispatch/dispatch.h> #include <err.h> #include <fcntl.h> #include <limits.h> #include <signal.h> #include <stdbool.h> #include <stdlib.h> #include <stdint.h> #include <unistd.h> #include <util.h> T_GLOBAL_META( T_META_NAMESPACE("xnu.kevent"), T_META_CHECK_LEAKS(false), T_META_RUN_CONCURRENTLY(true)); #define TIMEOUT_SECS 10 static int child_ready[2]; static void child_tty_client(void) { dispatch_source_t src; char buf[16] = ""; ssize_t bytes_wr; src = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)STDIN_FILENO, 0, NULL); if (!src) { exit(1); } dispatch_source_set_event_handler(src, ^{}); dispatch_activate(src); close(child_ready[0]); snprintf(buf, sizeof(buf), "%ds", getpid()); bytes_wr = write(child_ready[1], buf, strlen(buf)); if (bytes_wr < 0) { err(1, "failed to write on child ready pipe"); } dispatch_main(); } static void pty_master(void) { pid_t child_pid; int ret; child_pid = fork(); if (child_pid == 0) { child_tty_client(); } ret = setpgid(child_pid, child_pid); if (ret < 0) { exit(1); } ret = tcsetpgrp(STDIN_FILENO, child_pid); if (ret < 0) { exit(1); } sleep(TIMEOUT_SECS); exit(1); } T_DECL(pty_master_teardown, "try removing a TTY master out from under a PTY slave holding a kevent", T_META_ASROOT(true)) { __block pid_t master_pid; char buf[16] = ""; char *end; ssize_t bytes_rd; size_t buf_len = 0; unsigned long slave_pid; int master_fd; char pty_filename[PATH_MAX]; int status; T_SETUPBEGIN; T_ASSERT_POSIX_SUCCESS(pipe(child_ready), NULL); master_pid = forkpty(&master_fd, pty_filename, NULL, NULL); if (master_pid == 0) { pty_master(); __builtin_unreachable(); } T_ASSERT_POSIX_SUCCESS(master_pid, "forked child master PTY with pid %d, at pty %s", master_pid, pty_filename); close(child_ready[1]); end = buf; do { bytes_rd = read(child_ready[0], end, sizeof(buf) - buf_len); T_ASSERT_POSIX_SUCCESS(bytes_rd, "read on pipe between master and runner"); buf_len += (size_t)bytes_rd; T_LOG("runner read %zd bytes", bytes_rd); end += bytes_rd; } while (bytes_rd != 0 && *(end - 1) != 's'); slave_pid = strtoul(buf, &end, 0); if (buf == end) { T_ASSERT_FAIL("could not parse child PID from master pipe"); } T_LOG("got pid %lu for slave process from master", slave_pid); T_SETUPEND; T_LOG("sending fatal signal to master"); T_ASSERT_POSIX_SUCCESS(kill(master_pid, SIGKILL), NULL); T_LOG("sending fatal signal to slave"); (void)kill((int)slave_pid, SIGKILL); T_ASSERT_POSIX_SUCCESS(waitpid(master_pid, &status, 0), NULL); T_ASSERT_TRUE(WIFSIGNALED(status), "master PID was signaled"); (void)waitpid((int)slave_pid, &status, 0); } volatile static bool writing = true; static void * reader_thread(void *arg) { int fd = (int)arg; char c; T_SETUPBEGIN; T_QUIET; T_ASSERT_GT(fd, 0, "reader thread received valid fd"); T_SETUPEND; for (;;) { ssize_t rdsize = read(fd, &c, sizeof(c)); if (rdsize == -1) { if (errno == EINTR) { continue; } else if (errno == EBADF) { T_LOG("reader got an error (%s), shutting down", strerror(errno)); return NULL; } else { T_ASSERT_POSIX_SUCCESS(rdsize, "read on PTY"); } } else if (rdsize == 0) { return NULL; } } return NULL; } static void * writer_thread(void *arg) { int fd = (int)arg; char c[4096]; memset(c, 'a', sizeof(c)); T_SETUPBEGIN; T_QUIET; T_ASSERT_GT(fd, 0, "writer thread received valid fd"); T_SETUPEND; while (writing) { ssize_t wrsize = write(fd, c, sizeof(c)); if (wrsize == -1) { if (errno == EINTR) { continue; } else { T_LOG("writer got an error (%s), shutting down", strerror(errno)); return NULL; } } } return NULL; } #define ATTACH_ITERATIONS 10000 static int attach_master, attach_slave; static pthread_t reader, writer; static void redispatch(dispatch_group_t grp, dispatch_source_type_t type, int fd) { __block int iters = 0; __block void (^redispatch_blk)(void) = Block_copy(^{ if (iters++ > ATTACH_ITERATIONS) { return; } else if (iters == ATTACH_ITERATIONS) { dispatch_group_leave(grp); T_PASS("created %d %s sources on busy PTY", iters, type == DISPATCH_SOURCE_TYPE_READ ? "read" : "write"); } dispatch_source_t src = dispatch_source_create( type, (uintptr_t)fd, 0, dispatch_get_main_queue()); dispatch_source_set_event_handler(src, ^{ dispatch_cancel(src); }); dispatch_source_set_cancel_handler(src, redispatch_blk); dispatch_activate(src); }); dispatch_group_enter(grp); dispatch_async(dispatch_get_main_queue(), redispatch_blk); } T_DECL(attach_while_tty_wakeups, "try to attach knotes while a TTY is getting wakeups") { dispatch_group_t grp = dispatch_group_create(); T_SETUPBEGIN; T_ASSERT_POSIX_SUCCESS(openpty(&attach_master, &attach_slave, NULL, NULL, NULL), NULL); T_ASSERT_POSIX_ZERO(pthread_create(&reader, NULL, reader_thread, (void *)(uintptr_t)attach_master), NULL); T_ASSERT_POSIX_ZERO(pthread_create(&writer, NULL, writer_thread, (void *)(uintptr_t)attach_slave), NULL); T_SETUPEND; redispatch(grp, DISPATCH_SOURCE_TYPE_READ, attach_master); redispatch(grp, DISPATCH_SOURCE_TYPE_WRITE, attach_slave); dispatch_group_notify(grp, dispatch_get_main_queue(), ^{ T_LOG("both reader and writer sources cleaned up"); T_END; }); dispatch_main(); } T_DECL(master_read_data_set, "check that the data is set on read sources of master fds") { int master = -1, slave = -1; T_SETUPBEGIN; T_ASSERT_POSIX_SUCCESS(openpty(&master, &slave, NULL, NULL, NULL), NULL); T_QUIET; T_ASSERT_GE(master, 0, "master fd is valid"); T_QUIET; T_ASSERT_GE(slave, 0, "slave fd is valid"); dispatch_source_t src = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)master, 0, dispatch_get_main_queue()); dispatch_source_set_event_handler(src, ^{ unsigned long len = dispatch_source_get_data(src); T_EXPECT_GT(len, (unsigned long)0, "the amount of data to read was set for the master source"); dispatch_cancel(src); }); dispatch_source_set_cancel_handler(src, ^{ dispatch_release(src); T_END; }); dispatch_activate(src); T_SETUPEND; // Let's not fill up the TTY's buffer, otherwise write(2) will block. char buf[512] = ""; int ret = 0; while ((ret = write(slave, buf, sizeof(buf)) == -1 && errno == EAGAIN)) { ; } T_ASSERT_POSIX_SUCCESS(ret, "slave wrote data"); dispatch_main(); } |