1#!/bin/sh 2 3# Test of RENAME_EXCHANGE for tmpfs, cross directory version 4 5. ../default.cfg 6set -u 7prog=$(basename "$0" .sh) 8cat > /tmp/$prog.c <<EOF 9#include <sys/param.h> 10#include <sys/stat.h> 11#include <assert.h> 12#include <err.h> 13#include <errno.h> 14#include <fcntl.h> 15#include <stdio.h> 16#include <stdlib.h> 17#include <time.h> 18#include <unistd.h> 19 20#if ! defined(AT_RENAME_EXCHANGE) 21#define AT_RENAME_EXCHANGE 0x0002 22#endif 23 24static char *f1, *f2; 25 26void 27test1(void) 28{ 29 struct stat s, s1, s2; 30 time_t start; 31 32 if (stat(f1, &s1) < 0) 33 err(1, "stat(%s)", f1); 34 if (stat(f2, &s2) < 0) 35 err(1, "stat(%s)", f2); 36 assert(s1.st_size != s2.st_size); 37 start = time(NULL); 38 while (time(NULL) - start < 30) { 39 if (renameat2(AT_FDCWD, f1, AT_FDCWD, f2, RENAME_EXCHANGE) == -1) 40 err(1, "%s: rename2(%s, %s)", __func__, f1, f2); 41 42 if (stat(f1, &s) < 0) 43 err(1, "statfs(%s)", f1); 44 if (s.st_size != s2.st_size) 45 errx(1, "Got size %jd, expected %jd", (uintmax_t)s.st_size, (uintmax_t)s2.st_size); 46 47 if (renameat2(AT_FDCWD, f2, AT_FDCWD, f1, RENAME_EXCHANGE) == -1) 48 err(1, "%s: rename2(%s, %s)", __func__, f2, f1); 49 } 50} 51 52int 53main(int argc, char *argv[]) 54{ 55 56 if (argc != 3) { 57 fprintf(stderr, "Usage: %s <file1> <file2>\n", argv[0]); 58 exit(1); 59 } 60 f1 = argv[1]; 61 f2 = argv[2]; 62 63 test1(); 64} 65EOF 66cc -o /tmp/$prog -Wall -Wextra -g -O0 /tmp/$prog.c || exit 1 67 68mount | grep -q "on $mntpoint " && umount $mntpoint 69size=2g 70mount -o size=${size}m -t tmpfs tmpfs $mntpoint 71 72../testcases/swap/swap -t 5m -i 20 > /dev/null & 73sleep 1 74 75mkdir $mntpoint/d1 $mntpoint/d2 76echo 1 > $mntpoint/d1/f1 77echo 22 > $mntpoint/d2/f2 78/tmp/$prog $mntpoint/d1/f1 $mntpoint/d2/f2 79 80while pkill swap; do sleep .5; done 81wait 82 83umount $mntpoint 84exit 0 85