Path: blob/main/tools/test/stress2/testcases/symlink/symlink.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/mount.h>29#include <sys/stat.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{45int64_t in;46int64_t bl;47int64_t reserve_in;48int64_t reserve_bl;49int pct;5051umask(0);52if (nb == 0) {53getdf(&bl, &in);54size = in / op->incarnations;5556pct = 90;57if (op->hog == 0)58pct = random_int(1, 90);59size = size / 100 * pct + 1;6061if (size > 16000)62size = 16000; /* arbitrary limit number of files pr. dir */6364/* Resource requirements: */65while (size > 0) {66reserve_in = 1 * size * op->incarnations + op->incarnations;67reserve_bl = 26 * size * op->incarnations;68if (reserve_bl <= bl && reserve_in <= in)69break;70size = size / 2;71}72if (size == 0)73reserve_bl = reserve_in = 0;7475if (op->verbose > 1)76printf("symlink(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",77size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);78reservedf(reserve_bl, reserve_in);79putval(size);80} else {81size = getval();82}83if (size == 0)84exit(0);8586sprintf(path,"%s.%05d", getprogname(), getpid());87if (mkdir(path, 0770) < 0)88err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);8990if (chdir(path) == -1)91err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);9293return (0);94}9596void97cleanup(void)98{99if (path[0] != 0) {100(void)chdir("..");101if (rmdir(path) == -1) {102warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);103}104}105}106107int108test(void)109{110pid_t pid;111int i, j, error = 0;112char file[128];113114pid = getpid();115for (j = 0; j < (int)size && done_testing == 0; j++) {116sprintf(file,"p%05d.%05d", pid, j);117if (symlink("/tmp/not/there", file) == -1) {118if (errno != EINTR) {119warn("symlink(%s). %s.%d", file, __FILE__, __LINE__);120error = 1;121exit(1);122break;123}124}125}126127for (i = --j; i >= 0; i--) {128sprintf(file,"p%05d.%05d", pid, i);129if (unlink(file) == -1)130err(3, "unlink(%s)", file);131}132133if (error != 0)134exit(1);135136return (0);137}138139140