Path: blob/main/tests/sys/kern/pipe/big_pipe_test.c
39488 views
#include <sys/select.h>1#include <err.h>2#include <errno.h>3#include <fcntl.h>4#include <stdio.h>5#include <stdlib.h>6#include <string.h>7#include <unistd.h>89#define BIG_PIPE_SIZE 64*1024 /* From sys/pipe.h */1011/*12* Test for the non-blocking big pipe bug (write(2) returning13* EAGAIN while select(2) returns the descriptor as ready for write).14*/1516static void17write_frame(int fd, char *buf, unsigned long buflen)18{19fd_set wfd;20int i;2122while (buflen) {23FD_ZERO(&wfd);24FD_SET(fd, &wfd);25i = select(fd+1, NULL, &wfd, NULL, NULL);26if (i < 0)27err(1, "select failed");28if (i != 1) {29errx(1, "select returned unexpected value %d\n", i);30exit(1);31}32i = write(fd, buf, buflen);33if (i < 0) {34if (errno != EAGAIN)35warn("write failed");36exit(1);37}38buf += i;39buflen -= i;40}41}4243int44main(void)45{46/* any value over PIPE_SIZE should do */47char buf[BIG_PIPE_SIZE];48int i, flags, fd[2];4950if (pipe(fd) < 0)51errx(1, "pipe failed");5253flags = fcntl(fd[1], F_GETFL);54if (flags == -1 || fcntl(fd[1], F_SETFL, flags|O_NONBLOCK) == -1) {55printf("fcntl failed: %s\n", strerror(errno));56exit(1);57}5859switch (fork()) {60case -1:61err(1, "fork failed: %s\n", strerror(errno));62break;63case 0:64close(fd[1]);65for (;;) {66/* Any small size should do */67i = read(fd[0], buf, 256);68if (i == 0)69break;70if (i < 0)71err(1, "read");72}73exit(0);74default:75break;76}7778close(fd[0]);79memset(buf, 0, sizeof buf);80for (i = 0; i < 1000; i++)81write_frame(fd[1], buf, sizeof buf);8283printf("ok\n");84exit(0);85}868788