Path: blob/main/tests/sys/kern/pipe/pipe_reverse_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 <assert.h>29#include <err.h>30#include <errno.h>31#include <stdio.h>32#include <stdlib.h>33#include <string.h>34#include <unistd.h>3536/*37* This program simply tests writing through the reverse direction of38* a pipe. Nothing too fancy, it's only needed because most pipe-using39* programs never touch the reverse direction (it doesn't exist on40* Linux.)41*/4243int44main(void)45{46char buffer[65535], buffer2[65535], go[] = "go", go2[] = "go2";47int desc[2], ipc_coord[2];48size_t i;49ssize_t total;50int buggy, error;51pid_t new_pid;5253buggy = 0;54total = 0;5556error = pipe(desc);57if (error == -1)58err(1, "Couldn't allocate data pipe");5960error = pipe(ipc_coord);61if (error == -1)62err(1, "Couldn't allocate IPC coordination pipe");6364buffer[0] = 'A';6566for (i = 1; i < (int)sizeof(buffer); i++) {67buffer[i] = buffer[i - 1] + 1;68if (buffer[i] > 'Z')69buffer[i] = 'A';70}7172new_pid = fork();73assert(new_pid != -1);7475#define SYNC_R(i, _buf) do { \76int _error = errno; \77warnx("%d: waiting for synchronization", __LINE__); \78if (read(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \79err(1, "failed to synchronize (%s)", (i == 0 ? "parent" : "child")); \80errno = _error; \81} while(0)8283#define SYNC_W(i, _buf) do { \84int _error = errno; \85warnx("%d: sending synchronization", __LINE__); \86if (write(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \87err(1, "failed to synchronize (%s)", (i == 0 ? "child" : "parent")); \88errno = _error; \89} while(0)9091#define WRITE(s) do { \92ssize_t _size; \93if ((_size = write(desc[1], &buffer[total], s)) != s) \94warn("short write; wrote %zd, expected %d", _size, s); \95total += _size; \96} while(0)9798if (new_pid == 0) {99SYNC_R(0, go);100for (i = 0; i < 8; i++)101WRITE(4096);102103SYNC_W(0, go2);104SYNC_R(0, go);105106for (i = 0; i < 2; i++)107WRITE(4096);108109SYNC_W(0, go2);110111_exit(0);112}113114SYNC_W(1, go);115SYNC_R(1, go2);116117error = read(desc[0], &buffer2, 8 * 4096);118total += error;119printf("Read %d bytes\n", error);120121SYNC_W(1, go);122SYNC_R(1, go2);123124error = read(desc[0], &buffer2[total], 2 * 4096);125total += error;126printf("Read %d bytes, done\n", error);127128if (memcmp(buffer, buffer2, total) != 0) {129for (i = 0; i < (size_t)total; i++) {130if (buffer[i] != buffer2[i]) {131buggy = 1;132printf("Location %zu input: %hhx "133"output: %hhx\n",134i, buffer[i], buffer2[i]);135}136}137}138139waitpid(new_pid, NULL, 0);140141if ((buggy == 1) || (total != 10 * 4096))142errx(1, "FAILED");143else144printf("SUCCESS\n");145146exit(0);147}148149150