xref: /freebsd/tools/test/stress2/misc/beneath.sh (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm#
4*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
58a272653SPeter Holm#
68a272653SPeter Holm# Copyright (c) 2021 Konstantin Belousov <kib@FreeBSD.org>
78a272653SPeter Holm#
88a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
98a272653SPeter Holm# modification, are permitted provided that the following conditions
108a272653SPeter Holm# are met:
118a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
128a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
138a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
148a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
158a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
168a272653SPeter Holm#
178a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
188a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
218a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278a272653SPeter Holm# SUCH DAMAGE.
288a272653SPeter Holm#
298a272653SPeter Holm
30e0fd837aSPeter Holm# Test of open(2) with the O_RESOLVE_BENEATH flag.
318a272653SPeter Holm
328a272653SPeter Holm# userret: returning with the following locks held:
338a272653SPeter Holm# shared lockmgr ufs (ufs) r = 0 (0xfffff804ec0d2a48) locked @
348a272653SPeter Holm# kern/vfs_subr.c:2590 seen in WiP code:
358a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/kostik1126.txt
368a272653SPeter Holm
378a272653SPeter Holmtop=/tmp/beneath.d
388a272653SPeter Holmmkdir -p $top
398a272653SPeter Holmcat > $top/beneath.c <<EOF
408a272653SPeter Holm/* $Id: beneath.c,v 1.1 2018/10/13 16:53:02 kostik Exp kostik $ */
418a272653SPeter Holm
428a272653SPeter Holm#include <sys/stat.h>
438a272653SPeter Holm#include <errno.h>
448a272653SPeter Holm#include <fcntl.h>
458a272653SPeter Holm#include <stdio.h>
468a272653SPeter Holm#include <string.h>
478a272653SPeter Holm#include <unistd.h>
488a272653SPeter Holm
498a272653SPeter Holmint
508a272653SPeter Holmmain(int argc, char *argv[])
518a272653SPeter Holm{
528a272653SPeter Holm	struct stat st;
538a272653SPeter Holm	char *name;
548a272653SPeter Holm	int error, fd, i;
558a272653SPeter Holm
568a272653SPeter Holm	for (i = 1; i < argc; i++) {
578a272653SPeter Holm		name = argv[i];
588a272653SPeter Holm		alarm(120);
59e0fd837aSPeter Holm		fd = open(name, O_RDONLY | O_RESOLVE_BENEATH);
608a272653SPeter Holm		if (fd == -1) {
618a272653SPeter Holm			fprintf(stderr, "open(\"%s\") failed, error %d %s\n",
628a272653SPeter Holm			    name, errno, strerror(errno));
638a272653SPeter Holm		} else {
648a272653SPeter Holm			fprintf(stderr, "open(\"%s\") succeeded\n", name);
658a272653SPeter Holm			close(fd);
668a272653SPeter Holm		}
67e0fd837aSPeter Holm		error = fstatat(AT_FDCWD, name, &st, AT_RESOLVE_BENEATH);
688a272653SPeter Holm		if (error == -1){
698a272653SPeter Holm			fprintf(stderr, "stat(\"%s\") failed, error %d %s\n",
708a272653SPeter Holm			    name, errno, strerror(errno));
718a272653SPeter Holm		} else {
728a272653SPeter Holm			fprintf(stderr, "stat(\"%s\") succeeded\n", name);
738a272653SPeter Holm		}
748a272653SPeter Holm	}
758a272653SPeter Holm}
768a272653SPeter HolmEOF
778a272653SPeter Holmcc -o $top/beneath -Wall -Wextra $top/beneath.c || exit 1
788a272653SPeter Holmrm $top/beneath.c
798a272653SPeter Holm
808a272653SPeter Holm# Test with two directories as arguments:
818a272653SPeter Holmcd $top
828a272653SPeter Holmmkdir -p a/b
838a272653SPeter Holm./beneath a/b
848a272653SPeter Holm./beneath $top/a/b
858a272653SPeter Holmtouch $top/a/c
868a272653SPeter Holm./beneath a/c
878a272653SPeter Holm./beneath $top/a/c
888a272653SPeter Holm./beneath a/d
898a272653SPeter Holm./beneath $top/a/d
908a272653SPeter Holm
918a272653SPeter Holm# CWD is still $top for this test
928a272653SPeter Holmtop2=/var/tmp/beneath.d
938a272653SPeter Holmmkdir -p $top2
948a272653SPeter Holmmkdir -p $top2/a/b
958a272653SPeter Holm./beneath $top2/a/b > /dev/null 2>&1
968a272653SPeter Holm
978a272653SPeter Holmtouch $top2/a/c
988a272653SPeter Holm./beneath $top2/a/c > /dev/null 2>&1
998a272653SPeter Holm
1008a272653SPeter Holm# Other CWDs
1018a272653SPeter Holm(cd /etc; find . | head -1000 | xargs $top/beneath) > /dev/null 2>&1
1028a272653SPeter Holm(cd /var; find . | head -1000 | xargs $top/beneath) > /dev/null 2>&1
1038a272653SPeter Holm
1048a272653SPeter Holmrm -rf $top $top2
1058a272653SPeter Holmexit 0
106