1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. 4f08c3bdfSopenharmony_ci * http://www.sgi.com 5f08c3bdfSopenharmony_ci * 6f08c3bdfSopenharmony_ci * AUTHOR : Richard Logan 7f08c3bdfSopenharmony_ci * CO-PILOT : William Roske 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * 1.) select(2) to fd of regular file with no I/O and small timeout 10f08c3bdfSopenharmony_ci * 2.) select(2) to fd of system pipe with no I/O and small timeout 11f08c3bdfSopenharmony_ci * 3.) select(2) of fd of a named-pipe (FIFO) with no I/O and small timeout value 12f08c3bdfSopenharmony_ci */ 13f08c3bdfSopenharmony_ci 14f08c3bdfSopenharmony_ci#include <unistd.h> 15f08c3bdfSopenharmony_ci#include <errno.h> 16f08c3bdfSopenharmony_ci#include <sys/time.h> 17f08c3bdfSopenharmony_ci#include <sys/types.h> 18f08c3bdfSopenharmony_ci#include <fcntl.h> 19f08c3bdfSopenharmony_ci#include "select_var.h" 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_cistatic fd_set readfds_reg, readfds_pipe, writefds_pipe, readfds_npipe, writefds_npipe; 22f08c3bdfSopenharmony_cistatic int fd_reg, fds_pipe[2], fd_npipe; 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_cistatic struct tcases { 25f08c3bdfSopenharmony_ci int *nfds; 26f08c3bdfSopenharmony_ci fd_set *readfds; 27f08c3bdfSopenharmony_ci fd_set *writefds; 28f08c3bdfSopenharmony_ci int *readfd; 29f08c3bdfSopenharmony_ci int *writefd; 30f08c3bdfSopenharmony_ci char *desc; 31f08c3bdfSopenharmony_ci} tests[] = { 32f08c3bdfSopenharmony_ci {&fd_reg, &readfds_reg, NULL, &fd_reg, NULL, "with regular file"}, 33f08c3bdfSopenharmony_ci {&fds_pipe[1], &readfds_pipe, &writefds_pipe, &fds_pipe[0], &fds_pipe[1], "with system pipe"}, 34f08c3bdfSopenharmony_ci {&fd_npipe, &readfds_npipe, &writefds_npipe, &fd_npipe, &fd_npipe, "with named pipe (FIFO)"}, 35f08c3bdfSopenharmony_ci}; 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_cistatic void run(unsigned int n) 38f08c3bdfSopenharmony_ci{ 39f08c3bdfSopenharmony_ci struct tcases *tc = &tests[n]; 40f08c3bdfSopenharmony_ci struct timeval timeout; 41f08c3bdfSopenharmony_ci char buf; 42f08c3bdfSopenharmony_ci int exp_ret = 1; 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ci timeout.tv_sec = 0; 45f08c3bdfSopenharmony_ci timeout.tv_usec = 100000; 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci if (tc->writefd) { 48f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ANY, *tc->writefd, &buf, sizeof(buf)); 49f08c3bdfSopenharmony_ci exp_ret++; 50f08c3bdfSopenharmony_ci } 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci TEST(do_select(*tc->nfds + 1, tc->readfds, tc->writefds, 0, &timeout)); 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_ci if (TST_RET == -1) { 55f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "select() %s failed", tc->desc); 56f08c3bdfSopenharmony_ci return; 57f08c3bdfSopenharmony_ci } 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_ci if (!TST_RET) { 60f08c3bdfSopenharmony_ci tst_res(TFAIL, "select() %s timed out", tc->desc); 61f08c3bdfSopenharmony_ci return; 62f08c3bdfSopenharmony_ci } 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci if (TST_RET != exp_ret) { 65f08c3bdfSopenharmony_ci tst_res(TFAIL, "select() %s returned %lu expected %d", 66f08c3bdfSopenharmony_ci tc->desc, TST_RET, exp_ret); 67f08c3bdfSopenharmony_ci return; 68f08c3bdfSopenharmony_ci } 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ci tst_res(TPASS, "select() %s returned %i", tc->desc, exp_ret); 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ci if (FD_ISSET(*tc->readfd, tc->readfds)) 73f08c3bdfSopenharmony_ci tst_res(TPASS, "readfds bit %i is set", *tc->readfd); 74f08c3bdfSopenharmony_ci else 75f08c3bdfSopenharmony_ci tst_res(TFAIL, "readfds bit %i is not set", *tc->readfd); 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_ci if (!tc->writefd) 78f08c3bdfSopenharmony_ci return; 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci if (FD_ISSET(*tc->writefd, tc->writefds)) 81f08c3bdfSopenharmony_ci tst_res(TPASS, "writefds bit %i is set", *tc->writefd); 82f08c3bdfSopenharmony_ci else 83f08c3bdfSopenharmony_ci tst_res(TPASS, "writefds bit %i is not set", *tc->writefd); 84f08c3bdfSopenharmony_ci} 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_cistatic void setup(void) 87f08c3bdfSopenharmony_ci{ 88f08c3bdfSopenharmony_ci select_info(); 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_ci /* Regular file */ 91f08c3bdfSopenharmony_ci fd_reg = SAFE_OPEN("tmpfile1", O_CREAT | O_RDWR, 0777); 92f08c3bdfSopenharmony_ci FD_ZERO(&readfds_reg); 93f08c3bdfSopenharmony_ci FD_SET(fd_reg, &readfds_reg); 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_ci /* System pipe*/ 96f08c3bdfSopenharmony_ci SAFE_PIPE(fds_pipe); 97f08c3bdfSopenharmony_ci FD_ZERO(&readfds_pipe); 98f08c3bdfSopenharmony_ci FD_ZERO(&writefds_pipe); 99f08c3bdfSopenharmony_ci FD_SET(fds_pipe[0], &readfds_pipe); 100f08c3bdfSopenharmony_ci FD_SET(fds_pipe[1], &writefds_pipe); 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_ci /* Named pipe (FIFO) */ 103f08c3bdfSopenharmony_ci SAFE_MKFIFO("tmpfile2", 0666); 104f08c3bdfSopenharmony_ci fd_npipe = SAFE_OPEN("tmpfile2", O_RDWR); 105f08c3bdfSopenharmony_ci FD_ZERO(&readfds_npipe); 106f08c3bdfSopenharmony_ci FD_ZERO(&writefds_npipe); 107f08c3bdfSopenharmony_ci FD_SET(fd_npipe, &readfds_npipe); 108f08c3bdfSopenharmony_ci FD_SET(fd_npipe, &writefds_npipe); 109f08c3bdfSopenharmony_ci} 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_cistatic void cleanup(void) 112f08c3bdfSopenharmony_ci{ 113f08c3bdfSopenharmony_ci SAFE_UNLINK("tmpfile2"); 114f08c3bdfSopenharmony_ci} 115f08c3bdfSopenharmony_ci 116f08c3bdfSopenharmony_cistatic struct tst_test test = { 117f08c3bdfSopenharmony_ci .test = run, 118f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tests), 119f08c3bdfSopenharmony_ci .test_variants = TEST_VARIANTS, 120f08c3bdfSopenharmony_ci .setup = setup, 121f08c3bdfSopenharmony_ci .cleanup = cleanup, 122f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 123f08c3bdfSopenharmony_ci}; 124