Path: blob/main/tools/test/stress2/testcases/openat/openat.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 <err.h>30#include <fcntl.h>31#include <stdio.h>32#include <stdlib.h>33#include <string.h>34#include <unistd.h>3536#include "stress.h"3738static char path1[128];39static char path2[] = "tmp";4041static char rpath[128];42static char apath[128];4344static int fd;45static int freespace;4647int48setup(int nb)49{50int64_t bl;51int64_t in;52int64_t reserve_bl;53int64_t reserve_in;5455umask(0);5657if (nb == 0) {58getdf(&bl, &in);5960/* Resource requirements: */61reserve_in = 4 * op->incarnations;62reserve_bl = 8192 * op->incarnations;63freespace = (reserve_bl <= bl && reserve_in <= in);64if (!freespace)65reserve_bl = reserve_in = 0;6667if (op->verbose > 1)68printf("openat(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",69op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);70reservedf(reserve_bl, reserve_in);71putval(freespace);72} else {73freespace = getval();74}75if (!freespace)76exit(0);7778sprintf(path1,"%s.%05d", getprogname(), getpid());79if (mkdir(path1, 0770) < 0)80err(1, "mkdir(%s), %s:%d", path1, __FILE__, __LINE__);81if (chdir(path1) == -1)82err(1, "chdir(%s), %s:%d", path1, __FILE__, __LINE__);8384if (mkdir(path2, 0770) < 0)85err(1, "mkdir(%s), %s:%d", path2, __FILE__, __LINE__);86if (chdir(path2) == -1)87err(1, "chdir(%s), %s:%d", path2, __FILE__, __LINE__);88if (getcwd(apath, sizeof(apath)) == NULL)89err(1, "getcwd(%s), %s:%d", path2, __FILE__, __LINE__);9091if (chdir("..") == -1)92err(1, "chdir(%s), %s:%d", path1, __FILE__, __LINE__);9394if ((fd = open(path2, O_RDONLY)) == -1)95err(1, "open(%s), %s:%d", path2, __FILE__, __LINE__);9697strcpy(rpath, "tmp");98return (0);99}100101void102cleanup(void)103{104if (rmdir(path2) == -1)105warn("rmdir(%s), %s:%d", path2, __FILE__, __LINE__);106(void)chdir("..");107if (rmdir(path1) == -1)108warn("rmdir(%s), %s:%d", path1, __FILE__, __LINE__);109}110111static void112test_openat(void)113{114pid_t pid;115int i;116int tfd;117char file[128];118char p[128];119120pid = getpid();121for (i = 0; i < 100 && done_testing == 0; i++) {122sprintf(file,"p%05d.%05d", pid, i);123if ((tfd = openat(fd, file, O_RDONLY|O_CREAT, 0660)) == -1)124err(1, "openat(%s), %s:%d", file, __FILE__, __LINE__);125close(tfd);126strcpy(p, "tmp/");127strcat(p, file);128if (unlink(p) == -1)129err(1, "unlink(%s), %s:%d", p, __FILE__, __LINE__);130}131}132133static void134test_renameat(void)135{136pid_t pid;137int i;138int tfd;139char file[128];140char file2[128];141142pid = getpid();143for (i = 0; i < 100 && done_testing == 0; i++) {144sprintf(file,"p%05d.%05d", pid, i);145if ((tfd = openat(fd, file, O_RDONLY|O_CREAT, 0660)) == -1)146err(1, "openat(%s), %s:%d", file, __FILE__, __LINE__);147close(tfd);148149sprintf(file2,"p%05d.%05d.togo", pid, i);150if (renameat(fd, file, fd, file2) == -1)151err(1, "renameat(%s)", file2);152153sprintf(file2,"tmp/p%05d.%05d.togo", pid, i);154if (unlink(file2) == -1)155err(1, "unlink(%s), %s:%d", file2, __FILE__, __LINE__);156}157}158159static void160test_unlinkat(void)161{162pid_t pid;163int i;164int tfd;165char file[128];166167pid = getpid();168for (i = 0; i < 100 && done_testing == 0; i++) {169sprintf(file,"p%05d.%05d", pid, i);170if ((tfd = openat(fd, file, O_RDONLY|O_CREAT, 0660)) == -1)171err(1, "openat(%s), %s:%d", file, __FILE__, __LINE__);172close(tfd);173if (unlinkat(fd, file, 0) == -1)174err(1, "unlinkat(%s), %s:%d", file, __FILE__, __LINE__);175}176}177178int179test(void)180{181test_openat();182test_renameat();183test_unlinkat();184185return (0);186}187188189