Path: blob/main/tests/sys/kern/pipe/pipe_wraparound_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 tests to make sure that wraparound writes and reads38* are working, assuming that 16K socket buffers are used. In order39* to really stress the pipe code with this test, kernel modifications40* nay be necessary.41*/4243int main (void)44{45char buffer[32768], buffer2[32768], go[] = "go", go2[] = "go2";46int desc[2], ipc_coord[2];47ssize_t error, total;48int buggy, i;49pid_t new_pid;5051buggy = 0;52total = 0;5354error = pipe(desc);55if (error == -1)56err(1, "Couldn't allocate data pipe");5758error = pipe(ipc_coord);59if (error == -1)60err(1, "Couldn't allocate IPC coordination pipe");6162buffer[0] = 'A';6364for (i = 1; i < (int)sizeof(buffer); i++) {65buffer[i] = buffer[i - 1] + 1;66if (buffer[i] > 'Z')67buffer[i] = 'A';68}6970new_pid = fork();71assert(new_pid != -1);7273#define SYNC_R(i, _buf) do { \74int _error = errno; \75warnx("%d: waiting for synchronization", __LINE__); \76if (read(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \77err(1, "failed to synchronize (%s)", (i == 0 ? "parent" : "child")); \78errno = _error; \79} while(0)8081#define SYNC_W(i, _buf) do { \82int _error = errno; \83warnx("%d: sending synchronization", __LINE__); \84if (write(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \85err(1, "failed to synchronize (%s)", (i == 0 ? "child" : "parent")); \86errno = _error; \87} while(0)8889#define WRITE(s) do { \90ssize_t _size; \91if ((_size = write(desc[1], &buffer[total], s)) != s) \92warn("short write; wrote %zd, expected %d", _size, s); \93total += _size; \94} while(0)9596if (new_pid == 0) {97WRITE(4096);98WRITE(4096);99WRITE(4000);100SYNC_W(0, go2);101102SYNC_R(0, go);103WRITE(3000);104WRITE(3000);105SYNC_W(0, go2);106107_exit(0);108}109110SYNC_R(1, go2);111error = read(desc[0], &buffer2, 8192);112total += error;113printf("Read %zd bytes\n", error);114SYNC_W(1, go);115SYNC_R(1, go2);116error = read(desc[0], &buffer2[total], 16384);117total += error;118printf("Read %zd bytes, done\n", error);119120if (memcmp(buffer, buffer2, total) != 0) {121for (i = 0; i < total; i++) {122if (buffer[i] != buffer2[i]) {123buggy = 1;124printf("Location %d input: %hhx output: %hhx\n",125i, buffer[i], buffer2[i]);126}127}128}129130waitpid(new_pid, NULL, 0);131132if (buggy)133errx(1, "FAILURE");134135printf("SUCCESS\n");136137exit(0);138}139140141