1*97a74bbeSPeter Holm#!/bin/sh 2*97a74bbeSPeter Holm 3*97a74bbeSPeter Holm# 4*97a74bbeSPeter Holm# SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5*97a74bbeSPeter Holm# 6*97a74bbeSPeter Holm# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org> 7*97a74bbeSPeter Holm# 8*97a74bbeSPeter Holm# Redistribution and use in source and binary forms, with or without 9*97a74bbeSPeter Holm# modification, are permitted provided that the following conditions 10*97a74bbeSPeter Holm# are met: 11*97a74bbeSPeter Holm# 1. Redistributions of source code must retain the above copyright 12*97a74bbeSPeter Holm# notice, this list of conditions and the following disclaimer. 13*97a74bbeSPeter Holm# 2. Redistributions in binary form must reproduce the above copyright 14*97a74bbeSPeter Holm# notice, this list of conditions and the following disclaimer in the 15*97a74bbeSPeter Holm# documentation and/or other materials provided with the distribution. 16*97a74bbeSPeter Holm# 17*97a74bbeSPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18*97a74bbeSPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*97a74bbeSPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*97a74bbeSPeter Holm# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21*97a74bbeSPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22*97a74bbeSPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23*97a74bbeSPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24*97a74bbeSPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25*97a74bbeSPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26*97a74bbeSPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27*97a74bbeSPeter Holm# SUCH DAMAGE. 28*97a74bbeSPeter Holm# 29*97a74bbeSPeter Holm 30*97a74bbeSPeter Holm# Bug 248184 - readlink("/proc/curproc/file") returns arbitrary correct name for programs with more than one link (name) 31*97a74bbeSPeter Holm# Test scenario for D32611 "exec: provide right hardlink name in AT_EXECPATH" 32*97a74bbeSPeter Holm 33*97a74bbeSPeter Holm. ../default.cfg 34*97a74bbeSPeter Holm[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1 35*97a74bbeSPeter Holm 36*97a74bbeSPeter Holmdir=/tmp 37*97a74bbeSPeter Holmodir=`pwd` 38*97a74bbeSPeter Holmcd $dir 39*97a74bbeSPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/execpath.c 40*97a74bbeSPeter Holmmycc -o execpath -Wall -Wextra -O0 -g execpath.c || exit 1 41*97a74bbeSPeter Holmrm -f execpath.c 42*97a74bbeSPeter Holmcd $odir 43*97a74bbeSPeter Holm 44*97a74bbeSPeter Holmset -e 45*97a74bbeSPeter Holmmount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint 46*97a74bbeSPeter Holm[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart 47*97a74bbeSPeter Holmmdconfig -a -t swap -s 2g -u $mdstart 48*97a74bbeSPeter Holmbsdlabel -w md$mdstart auto 49*97a74bbeSPeter Holmnewfs $newfs_flags md${mdstart}$part > /dev/null 50*97a74bbeSPeter Holmmount /dev/md${mdstart}$part $mntpoint 51*97a74bbeSPeter Holmset +e 52*97a74bbeSPeter Holm 53*97a74bbeSPeter Holmcd $mntpoint 54*97a74bbeSPeter Holmmkdir d1 d2 55*97a74bbeSPeter Holmcp /bin/sleep . 56*97a74bbeSPeter Holmln sleep d1/sleep 57*97a74bbeSPeter Holmln sleep d2/sleep 58*97a74bbeSPeter Holms=0 59*97a74bbeSPeter Holmfor p in $mntpoint/sleep $mntpoint/d1/sleep $mntpoint/d2/sleep; do 60*97a74bbeSPeter Holm (cd `dirname $p`; ./`basename $p` 10) & 61*97a74bbeSPeter Holm /bin/sleep .2 62*97a74bbeSPeter Holm path=`procstat binary $! | awk "/$!/ {print \\$NF}"` 63*97a74bbeSPeter Holm kill $! 64*97a74bbeSPeter Holm wait 65*97a74bbeSPeter Holm [ "$path" != "$p" ] && { s=1; echo "fail path=$path : prog=$p"; } 66*97a74bbeSPeter Holmdone 67*97a74bbeSPeter Holmmv /tmp/execpath d1 68*97a74bbeSPeter Holmln d1/execpath d2/execpath 69*97a74bbeSPeter Holm 70*97a74bbeSPeter Holmr=`./d1/execpath` 71*97a74bbeSPeter Holmecho $r | grep -q "/d1/" || { s=1; echo "fail: $r. Expected d1 @ 1"; } 72*97a74bbeSPeter Holmr=`./d2/execpath` 73*97a74bbeSPeter Holmecho $r | grep -q "/d2/" || { s=1; echo "fail: $r. Expected d2 @ 2"; } 74*97a74bbeSPeter Holmr=`(cd d1; ./execpath)` 75*97a74bbeSPeter Holmecho $r | grep -q "/d1/" || { s=1; echo "fail: $r. Expected d1 @ 3"; } 76*97a74bbeSPeter Holmr=`(cd d2; ./execpath)` 77*97a74bbeSPeter Holmecho $r | grep -q "/d2/" || { s=1; echo "fail: $r. Expected d2 @ 4"; } 78*97a74bbeSPeter Holm 79*97a74bbeSPeter Holmcd $odir 80*97a74bbeSPeter Holm 81*97a74bbeSPeter Holmfor i in `jot 6`; do 82*97a74bbeSPeter Holm mount | grep -q "on $mntpoint " || break 83*97a74bbeSPeter Holm umount $mntpoint && break || sleep 10 84*97a74bbeSPeter Holm [ $i -eq 6 ] && 85*97a74bbeSPeter Holm { echo FATAL; fstat -mf $mntpoint; exit 1; } 86*97a74bbeSPeter Holmdone 87*97a74bbeSPeter Holmmdconfig -d -u $mdstart 88*97a74bbeSPeter Holmexit $s 89*97a74bbeSPeter Holm 90*97a74bbeSPeter HolmEOF 91*97a74bbeSPeter Holm/* Test scenario by Tobias Kortkamp <tobik@freebsd.org> */ 92*97a74bbeSPeter Holm 93*97a74bbeSPeter Holm#include <sys/auxv.h> 94*97a74bbeSPeter Holm#include <limits.h> 95*97a74bbeSPeter Holm#include <stdio.h> 96*97a74bbeSPeter Holm 97*97a74bbeSPeter Holmint 98*97a74bbeSPeter Holmmain(void) 99*97a74bbeSPeter Holm{ 100*97a74bbeSPeter Holm char pathname[PATH_MAX]; 101*97a74bbeSPeter Holm 102*97a74bbeSPeter Holm elf_aux_info(AT_EXECPATH, pathname, PATH_MAX); 103*97a74bbeSPeter Holm puts(pathname); 104*97a74bbeSPeter Holm return 0; 105*97a74bbeSPeter Holm} 106