Path: blob/main/tools/test/stress2/testcases/link/link.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>2930#include <err.h>31#include <errno.h>32#include <fcntl.h>33#include <stdio.h>34#include <stdlib.h>35#include <string.h>36#include <unistd.h>3738#include "stress.h"3940static unsigned long size;41static char path[128];4243int44setup(int nb)45{46int64_t bl;47int64_t in;48int64_t reserve_bl;49int64_t reserve_in;50int pct;5152umask(0);53path[0] = 0;54if (nb == 0) {55getdf(&bl, &in);56size = in / op->incarnations;5758pct = 90;59if (op->hog == 0)60pct = random_int(1, 90);61size = size / 100 * pct + 1;6263if (size > 32000 && op->hog == 0)64size = 32000; /* arbitrary limit number of files pr. dir */6566/* Resource requirements: */67reserve_in = 2 * op->incarnations + 7;68reserve_bl = 26 * size * op->incarnations;69if (reserve_in > in || reserve_bl > bl)70size = reserve_in = reserve_bl = 0;7172if (op->verbose > 1)73printf("link(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",74size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);75reservedf(reserve_bl, reserve_in);76putval(size);77} else {78size = getval();79}80if (size == 0)81exit (1);8283sprintf(path,"%s.%05d", getprogname(), getpid());84if (mkdir(path, 0770) < 0)85err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);8687if (chdir(path) == -1)88err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);8990return (0);91}9293void94cleanup(void)95{96if (size == 0)97return;98(void)chdir("..");99if (path[0] != 0 && rmdir(path) == -1)100warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);101}102103int104test(void)105{106pid_t pid;107int fd, i, j;108char file[128];109char lfile[128];110111pid = getpid();112for (j = 0; j < (int)size && done_testing == 0; j++) {113sprintf(file,"p%05d.%05d", pid, j);114if (j == 0) {115if ((fd = creat(file, 0660)) == -1) {116if (errno != EINTR) {117warn("creat(%s)", file);118break;119}120}121if (fd != -1 && close(fd) == -1)122err(2, "close(%d)", j);123strcpy(lfile, file);124} else {125if (link(lfile, file) == -1) {126if (errno != EINTR) {127warn("link(%s, %s)", lfile, file);128break;129}130}131}132133}134135for (i = --j; i >= 0; i--) {136sprintf(file,"p%05d.%05d", pid, i);137if (unlink(file) == -1)138err(3, "unlink(%s)", file);139140}141142return (0);143}144145146