Path: blob/main/tools/test/stress2/testcases/creat/creat.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/mount.h>30#include <err.h>31#include <errno.h>32#include <fcntl.h>33#include <stdio.h>34#include <stdlib.h>35#include <unistd.h>3637#include "stress.h"3839static char path[128];40static unsigned long size;4142int43setup(int nb)44{45int pct;46int64_t in;47int64_t bl;48int64_t reserve_in;49int64_t reserve_bl;5051umask(0);52path[0] = 0;53if (nb == 0) {54getdf(&bl, &in);55size = in / op->incarnations;5657pct = 90;58if (op->hog == 0)59pct = random_int(1, 90);60size = size / 100 * pct + 1;6162if (size > 1000)63size = 1000; /* Due to Soft Update lag */6465/* Resource requirements: */66while (size > 0) {67reserve_in = 1 * size * op->incarnations + size;68reserve_bl = 29 * size * op->incarnations;69if (reserve_bl <= bl && reserve_in <= in)70break;71size--;72}73if (size == 0)74reserve_bl = reserve_in = 0;7576if (op->verbose > 1)77printf("creat(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",78size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);79reservedf(reserve_bl, reserve_in);80putval(size);81} else {82size = getval();83}8485if (size == 0)86exit(0);87sprintf(path,"%s.%05d", getprogname(), getpid());88if (mkdir(path, 0770) < 0)89err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);9091if (chdir(path) == -1)92err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);9394return (0);95}9697void98cleanup(void)99{100(void)chdir("..");101if (path[0] != 0 && rmdir(path) == -1) {102warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);103}104}105106int107test(void)108{109int fd, i, j;110pid_t pid;111char file[128];112113pid = getpid();114for (j = 0; j < (int)size && done_testing == 0; j++) {115sprintf(file,"p%05d.%05d", pid, j);116if ((fd = creat(file, 0660)) == -1) {117if (errno != EINTR) {118warn("creat(%s). %s:%d", file, __FILE__, __LINE__);119break;120}121}122if (fd != -1 && close(fd) == -1)123err(2, "close(%d)", j);124125}126127for (i = --j; i >= 0; i--) {128sprintf(file,"p%05d.%05d", pid, i);129if (unlink(file) == -1)130warn("unlink(%s)", file);131132}133134return (0);135}136137138