Path: blob/main/tests/sys/kern/pipe/pipe_fstat_bug_test.c
39488 views
/*1Copyright (C) 2004 Michael J. Silbersack. All rights reserved.23Redistribution and use in source and binary forms, with or without4modification, are permitted provided that the following conditions5are met:61. Redistributions of source code must retain the above copyright7notice, this list of conditions and the following disclaimer.82. Redistributions in binary form must reproduce the above copyright9notice, this list of conditions and the following disclaimer in the10documentation and/or other materials provided with the distribution.1112THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND13ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE16FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)19HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT20LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY21OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF22SUCH DAMAGE.23*/2425#include <sys/types.h>26#include <sys/stat.h>27#include <sys/wait.h>28#include <sys/event.h>29#include <assert.h>30#include <err.h>31#include <errno.h>32#include <inttypes.h>33#include <stdio.h>34#include <stdlib.h>35#include <unistd.h>3637/*38* The goal of this program is to see if fstat reports the correct39* data count for a pipe. Prior to revision 1.172 of sys_pipe.c,40* 0 would be returned once the pipe entered direct write mode.41*42* Linux (2.6) always returns zero, so it's not a valuable platform43* for comparison.44*/4546int47main(void)48{49char buffer[32768], buffer2[32768], go[] = "go", go2[] = "go2";50int desc[2], ipc_coord[2];51struct kevent event, ke;52ssize_t error;53int successes = 0;54struct stat status;55pid_t new_pid;56int kq;5758error = pipe(desc);59if (error == -1)60err(1, "Couldn't allocate data pipe");6162error = pipe(ipc_coord);63if (error == -1)64err(1, "Couldn't allocate IPC coordination pipe");6566new_pid = fork();67assert(new_pid != -1);6869close(new_pid == 0 ? desc[0] : desc[1]);7071#define SYNC_R(i, _buf) do { \72int _error = errno; \73warnx("%d: waiting for synchronization", __LINE__); \74if (read(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \75err(1, "failed to synchronize (%s)", (i == 0 ? "parent" : "child")); \76errno = _error; \77} while(0)7879#define SYNC_W(i, _buf) do { \80int _error = errno; \81warnx("%d: sending synchronization", __LINE__); \82if (write(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \83err(1, "failed to synchronize (%s)", (i == 0 ? "child" : "parent")); \84errno = _error; \85} while(0)8687#define WRITE(s) do { \88ssize_t _size; \89if ((_size = write(desc[1], &buffer, s)) != s) \90warn("short write; wrote %zd, expected %d", _size, s); \91} while(0)9293if (new_pid == 0) {9495SYNC_R(0, go);96WRITE(145);97SYNC_W(0, go2);9899SYNC_R(0, go);100WRITE(2048);101SYNC_W(0, go2);102103SYNC_R(0, go);104WRITE(4096);105SYNC_W(0, go2);106107SYNC_R(0, go);108WRITE(8191);109SYNC_W(0, go2);110111SYNC_R(0, go);112SYNC_W(0, go2); /* XXX: why is this required? */113WRITE(8192);114SYNC_W(0, go2);115116close(ipc_coord[0]);117close(ipc_coord[1]);118119_exit(0);120}121122kq = kqueue();123if (kq == -1)124_exit(1);125126EV_SET(&ke, desc[0], EVFILT_READ, EV_ADD, 0, 0, NULL);127128/* Attach event to the kqueue. */129if (kevent(kq, &ke, 1, NULL, 0, NULL) != 0)130_exit(2);131132while (successes < 5) {133SYNC_W(1, go);134SYNC_R(1, go2);135136/* Ensure data is available to read */137if (kevent(kq, NULL, 0, &event, 1, NULL) != 1)138_exit(3);139140fstat(desc[0], &status);141error = read(desc[0], &buffer2, sizeof(buffer2));142143if (status.st_size != error)144err(1, "FAILURE: stat size %jd read size %zd",145(intmax_t)status.st_size, error);146if (error > 0) {147printf("SUCCESS at stat size %jd read size %zd\n",148(intmax_t)status.st_size, error);149successes++;150}151}152153exit(0);154}155156157