xref: /freebsd/tools/test/stress2/misc/beneath4.sh (revision 95eb4b873b6a8b527c5bd78d7191975dfca38998)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# Test of:
31# AT_BENEATH              0x1000  /* Fail if not under dirfd */
32# AT_RESOLVE_BENEATH      0x2000  /* As AT_BENEATH, but do not allow
33#                                    resolve to walk out of dirfd even
34
35dir=/tmp/beneath4.dir
36rm -rf $dir
37mkdir -p $dir
38here=`pwd`
39cd $dir
40
41cat > beneath4.c <<EOF
42#include <sys/stat.h>
43
44#include <err.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <unistd.h>
51
52int
53main(int argc, char *argv[])
54{
55	struct stat st;
56	int exp, fd, flag, r;
57	char *cwd, *dir, *obj, *s;
58
59	if (argc != 5) {
60		fprintf(stderr,
61		    "Usage: %s <dir> <test obj> <flag> <expected return>\n",
62		    argv[0]);
63		return (1);
64	}
65
66	cwd = getwd(NULL);
67	dir = argv[1];
68	obj = argv[2];
69	sscanf(argv[3], "%x", &flag);
70	exp = atoi(argv[4]);
71#if 0
72	if ((flag & AT_RESOLVE_BENEATH) == 0) {
73		fprintf(stderr, "Flag must be %#x or %#x\n",
74		    AT_BENEATH, AT_RESOLVE_BENEATH);
75		return (1);
76	}
77#endif
78	if ((fd = open(dir, O_DIRECTORY | O_RDONLY)) == -1)
79		err(1, "open(%s)", dir);
80
81	if (fstatat(fd, obj, &st, flag) == -1)
82		r = errno;
83	else
84		r = 0;
85	s = "FAIL";
86	if (r == exp)
87		s = "OK";
88	warn("cwd=%s, top=%s. flag=%0.6x. fstatf(%s) = %2d (expect %2d). %4s",
89	    cwd, dir, flag, obj, r, exp, s);
90
91	return (r == exp ? 0 : errno);
92}
93EOF
94cc -o beneath4 -Wall -Wextra -O2 -g beneath4.c || exit 1
95rm beneath4.c
96
97mkdir -p /tmp/beneath4.dir/a/a
98touch    /tmp/beneath4.dir/a/f
99ln       /tmp/beneath4.dir/a/f /tmp/beneath4.dir/a/c
100ln -s    /tmp/beneath4.dir/a/a /tmp/beneath4.dir/a/d
101ln -s    /tmp/beneath4.dir/a/b /tmp/beneath4.dir/a/e
102mkfifo   /tmp/beneath4.dir/a/fifo
103
104top=$dir/a
105
106cd $here
107s=0
108#ls -lR $dir
109#echo  AT_BENEATH
110#$dir/beneath4 $top a                         0x1000  0 || s=1
111#$dir/beneath4 $top b                         0x1000  2 || s=1
112#$dir/beneath4 $top c                         0x1000  0 || s=1
113#$dir/beneath4 $top d                         0x1000  0 || s=1
114#$dir/beneath4 $top e                         0x1000  2 || s=1
115#$dir/beneath4 $top fifo                      0x1000  0 || s=1
116#$dir/beneath4 $top $top/../../beneath4.d/a/a 0x1000 93 || s=1
117#$dir/beneath4 $top $top/..                   0x1000 93 || s=1
118#$dir/beneath4 $top ../a                      0x1000  0 || s=1
119
120printf "\nAT_RESOLVE_BENEATH\n"
121$dir/beneath4 $top a                         0x2000  0 || s=1
122$dir/beneath4 $top b                         0x2000  2 || s=1
123$dir/beneath4 $top c                         0x2000  0 || s=1
124$dir/beneath4 $top d                         0x2000 93 || s=1
125$dir/beneath4 $top e                         0x2000 93 || s=1
126$dir/beneath4 $top fifo                      0x2000  0 || s=1
127$dir/beneath4 $top $top/../../beneath4.d/a/a 0x2000 93 || s=1
128$dir/beneath4 $top $top/..                   0x2000 93 || s=1
129$dir/beneath4 $top ../a                      0x2000 93 || s=1
130printf "\nNo flag\n"
131$dir/beneath4 $top ../a                      0x0000  0 || s=1
132rm -rf $top
133exit $s
134