Path: blob/main/tools/test/stress2/testcases/mkfifo/mkfifo.c
39566 views
/*-1* Copyright (c) 2008 Peter Holm <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25*/2627#include <sys/param.h>28#include <sys/stat.h>29#include <sys/wait.h>3031#include <err.h>32#include <fcntl.h>33#include <signal.h>34#include <stdio.h>35#include <stdlib.h>36#include <string.h>37#include <unistd.h>3839#include "stress.h"4041static char path[MAXPATHLEN+1];42static int bufsize, freespace;4344static void45reader(void) {46fd_set set;47struct timeval tv;48int *buf, fd, n;4950setproctitle("reader");51if ((fd = open(path, O_RDONLY | O_NONBLOCK)) < 0)52err(1, "open(%s)", path);53if ((buf = malloc(bufsize)) == NULL)54err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);55n = 0;56FD_ZERO(&set);57FD_SET(fd, &set);58tv.tv_sec = 10;59tv.tv_usec = 0;60if (select(fd + 1, &set, NULL, NULL, &tv) == 1) {61if ((n = read(fd, buf, bufsize)) < 0)62err(1, "read(), %s:%d", __FILE__, __LINE__);63}64close(fd);65free(buf);66}6768static void69writer(void) {70int *buf, fd;7172setproctitle("writer");73if ((fd = open(path, O_WRONLY)) < 0) {74unlink(path);75err(1, "open(%s)", path);76}77if ((buf = malloc(bufsize)) == NULL)78err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);79memset(buf, 0, bufsize);8081if (write(fd, buf, bufsize) < 0)82err(1, "write(%d), %s:%d", fd, __FILE__, __LINE__);83close(fd);84free(buf);85}8687int88setup(int nb)89{90int64_t bl;91int64_t in;92int64_t reserve_bl;93int64_t reserve_in;9495if (nb == 0) {96getdf(&bl, &in);9798/* Resource requirements: */99reserve_in = 200 * op->incarnations;100reserve_bl = 2048 * op->incarnations;101freespace = (reserve_bl <= bl && reserve_in <= in);102if (!freespace)103reserve_bl = reserve_in = 0;104105if (op->verbose > 1)106printf("mkfifo(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",107op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);108reservedf(reserve_bl, reserve_in);109putval(freespace);110fflush(stdout);111} else {112freespace = getval();113}114if (!freespace)115_exit(0);116bufsize = 2 << random_int(2, 12);117118return (0);119}120121void122cleanup(void)123{124}125126int127test(void)128{129pid_t pid;130int i, status;131132for (i = 0; i < 100; i++) {133if (sprintf(path, "fifo.%d.%d", getpid(), i) < 0)134err(1, "sprintf()");135if (mkfifo(path, 0600) < 0)136err(1, "mkfifo(%s)", path);137}138for (i = 0; i < 100; i++) {139if (sprintf(path, "fifo.%d.%d", getpid(), i) < 0)140err(1, "sprintf()");141if (unlink(path) < 0)142err(1, "unlink(%s)", path);143}144145if (sprintf(path, "fifo.%d", getpid()) < 0)146err(1, "sprintf()");147if (mkfifo(path, 0600) < 0)148err(1, "mkfifo(%s)", path);149150if ((pid = fork()) == 0) {151writer();152_exit(EXIT_SUCCESS);153154} else if (pid > 0) {155reader();156kill(pid, SIGINT);157if (waitpid(pid, &status, 0) == -1)158warn("waitpid(%d)", pid);159} else160err(1, "fork(), %s:%d", __FILE__, __LINE__);161162unlink(path);163164return (0);165}166167168