1*8a272653SPeter Holm#!/bin/sh 2*8a272653SPeter Holm 3*8a272653SPeter Holm# 4*8a272653SPeter Holm# SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5*8a272653SPeter Holm# 6*8a272653SPeter Holm# Copyright (c) 2021 Konstantin Belousov <kib@FreeBSD.org> 7*8a272653SPeter Holm# 8*8a272653SPeter Holm# Redistribution and use in source and binary forms, with or without 9*8a272653SPeter Holm# modification, are permitted provided that the following conditions 10*8a272653SPeter Holm# are met: 11*8a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright 12*8a272653SPeter Holm# notice, this list of conditions and the following disclaimer. 13*8a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright 14*8a272653SPeter Holm# notice, this list of conditions and the following disclaimer in the 15*8a272653SPeter Holm# documentation and/or other materials provided with the distribution. 16*8a272653SPeter Holm# 17*8a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18*8a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*8a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*8a272653SPeter Holm# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21*8a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22*8a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23*8a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24*8a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25*8a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26*8a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27*8a272653SPeter Holm# SUCH DAMAGE. 28*8a272653SPeter Holm# 29*8a272653SPeter Holm 30*8a272653SPeter Holm# Test of open(2) with the O_BENEATH flag. 31*8a272653SPeter Holm 32*8a272653SPeter Holm# userret: returning with the following locks held: 33*8a272653SPeter Holm# shared lockmgr ufs (ufs) r = 0 (0xfffff804ec0d2a48) locked @ 34*8a272653SPeter Holm# kern/vfs_subr.c:2590 seen in WiP code: 35*8a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/kostik1126.txt 36*8a272653SPeter Holm 37*8a272653SPeter Holmtop=/tmp/beneath.d 38*8a272653SPeter Holmmkdir -p $top 39*8a272653SPeter Holmcat > $top/beneath.c <<EOF 40*8a272653SPeter Holm/* $Id: beneath.c,v 1.1 2018/10/13 16:53:02 kostik Exp kostik $ */ 41*8a272653SPeter Holm 42*8a272653SPeter Holm#include <sys/stat.h> 43*8a272653SPeter Holm#include <errno.h> 44*8a272653SPeter Holm#include <fcntl.h> 45*8a272653SPeter Holm#include <stdio.h> 46*8a272653SPeter Holm#include <string.h> 47*8a272653SPeter Holm#include <unistd.h> 48*8a272653SPeter Holm 49*8a272653SPeter Holm#ifndef O_BENEATH 50*8a272653SPeter Holm#define O_BENEATH 0x00400000 /* Fail if not under cwd */ 51*8a272653SPeter Holm#define AT_BENEATH 0x1000 /* Fail if not under dirfd */ 52*8a272653SPeter Holm#endif 53*8a272653SPeter Holm 54*8a272653SPeter Holmint 55*8a272653SPeter Holmmain(int argc, char *argv[]) 56*8a272653SPeter Holm{ 57*8a272653SPeter Holm struct stat st; 58*8a272653SPeter Holm char *name; 59*8a272653SPeter Holm int error, fd, i; 60*8a272653SPeter Holm 61*8a272653SPeter Holm for (i = 1; i < argc; i++) { 62*8a272653SPeter Holm name = argv[i]; 63*8a272653SPeter Holm alarm(120); 64*8a272653SPeter Holm fd = open(name, O_RDONLY | O_BENEATH); 65*8a272653SPeter Holm if (fd == -1) { 66*8a272653SPeter Holm fprintf(stderr, "open(\"%s\") failed, error %d %s\n", 67*8a272653SPeter Holm name, errno, strerror(errno)); 68*8a272653SPeter Holm } else { 69*8a272653SPeter Holm fprintf(stderr, "open(\"%s\") succeeded\n", name); 70*8a272653SPeter Holm close(fd); 71*8a272653SPeter Holm } 72*8a272653SPeter Holm error = fstatat(AT_FDCWD, name, &st, AT_BENEATH); 73*8a272653SPeter Holm if (error == -1){ 74*8a272653SPeter Holm fprintf(stderr, "stat(\"%s\") failed, error %d %s\n", 75*8a272653SPeter Holm name, errno, strerror(errno)); 76*8a272653SPeter Holm } else { 77*8a272653SPeter Holm fprintf(stderr, "stat(\"%s\") succeeded\n", name); 78*8a272653SPeter Holm } 79*8a272653SPeter Holm } 80*8a272653SPeter Holm} 81*8a272653SPeter HolmEOF 82*8a272653SPeter Holmcc -o $top/beneath -Wall -Wextra $top/beneath.c || exit 1 83*8a272653SPeter Holmrm $top/beneath.c 84*8a272653SPeter Holm 85*8a272653SPeter Holm# Test with two directories as arguments: 86*8a272653SPeter Holmcd $top 87*8a272653SPeter Holmmkdir -p a/b 88*8a272653SPeter Holm./beneath a/b 89*8a272653SPeter Holm./beneath $top/a/b 90*8a272653SPeter Holmtouch $top/a/c 91*8a272653SPeter Holm./beneath a/c 92*8a272653SPeter Holm./beneath $top/a/c 93*8a272653SPeter Holm./beneath a/d 94*8a272653SPeter Holm./beneath $top/a/d 95*8a272653SPeter Holm 96*8a272653SPeter Holm# CWD is still $top for this test 97*8a272653SPeter Holmtop2=/var/tmp/beneath.d 98*8a272653SPeter Holmmkdir -p $top2 99*8a272653SPeter Holmmkdir -p $top2/a/b 100*8a272653SPeter Holm./beneath $top2/a/b > /dev/null 2>&1 101*8a272653SPeter Holm 102*8a272653SPeter Holmtouch $top2/a/c 103*8a272653SPeter Holm./beneath $top2/a/c > /dev/null 2>&1 104*8a272653SPeter Holm 105*8a272653SPeter Holm# Other CWDs 106*8a272653SPeter Holm(cd /etc; find . | head -1000 | xargs $top/beneath) > /dev/null 2>&1 107*8a272653SPeter Holm(cd /var; find . | head -1000 | xargs $top/beneath) > /dev/null 2>&1 108*8a272653SPeter Holm 109*8a272653SPeter Holmrm -rf $top $top2 110*8a272653SPeter Holmexit 0 111