xref: /freebsd/tools/test/stress2/misc/execpath.sh (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
197a74bbeSPeter Holm#!/bin/sh
297a74bbeSPeter Holm
397a74bbeSPeter Holm#
4*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
597a74bbeSPeter Holm#
697a74bbeSPeter Holm# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
797a74bbeSPeter Holm#
897a74bbeSPeter Holm# Redistribution and use in source and binary forms, with or without
997a74bbeSPeter Holm# modification, are permitted provided that the following conditions
1097a74bbeSPeter Holm# are met:
1197a74bbeSPeter Holm# 1. Redistributions of source code must retain the above copyright
1297a74bbeSPeter Holm#    notice, this list of conditions and the following disclaimer.
1397a74bbeSPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
1497a74bbeSPeter Holm#    notice, this list of conditions and the following disclaimer in the
1597a74bbeSPeter Holm#    documentation and/or other materials provided with the distribution.
1697a74bbeSPeter Holm#
1797a74bbeSPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1897a74bbeSPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1997a74bbeSPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2097a74bbeSPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2197a74bbeSPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2297a74bbeSPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2397a74bbeSPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2497a74bbeSPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2597a74bbeSPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2697a74bbeSPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2797a74bbeSPeter Holm# SUCH DAMAGE.
2897a74bbeSPeter Holm#
2997a74bbeSPeter Holm
3097a74bbeSPeter Holm# Bug 248184 - readlink("/proc/curproc/file") returns arbitrary correct name for programs with more than one link (name)
3197a74bbeSPeter Holm# Test scenario for D32611 "exec: provide right hardlink name in AT_EXECPATH"
3297a74bbeSPeter Holm
3397a74bbeSPeter Holm. ../default.cfg
3497a74bbeSPeter Holm[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
3597a74bbeSPeter Holm
3697a74bbeSPeter Holmdir=/tmp
3797a74bbeSPeter Holmodir=`pwd`
3897a74bbeSPeter Holmcd $dir
3997a74bbeSPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/execpath.c
4097a74bbeSPeter Holmmycc -o execpath -Wall -Wextra -O0 -g execpath.c || exit 1
4197a74bbeSPeter Holmrm -f execpath.c
4297a74bbeSPeter Holmcd $odir
4397a74bbeSPeter Holm
4497a74bbeSPeter Holmset -e
4597a74bbeSPeter Holmmount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
4697a74bbeSPeter Holm[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
4797a74bbeSPeter Holmmdconfig -a -t swap -s 2g -u $mdstart
48608c97bfSPeter Holmnewfs $newfs_flags md$mdstart > /dev/null
49608c97bfSPeter Holmmount /dev/md$mdstart $mntpoint
5097a74bbeSPeter Holmset +e
5197a74bbeSPeter Holm
5297a74bbeSPeter Holmcd $mntpoint
5397a74bbeSPeter Holmmkdir d1 d2
5497a74bbeSPeter Holmcp /bin/sleep .
5597a74bbeSPeter Holmln sleep d1/sleep
5697a74bbeSPeter Holmln sleep d2/sleep
5797a74bbeSPeter Holms=0
5897a74bbeSPeter Holmfor p in $mntpoint/sleep $mntpoint/d1/sleep $mntpoint/d2/sleep; do
5997a74bbeSPeter Holm	(cd `dirname $p`; ./`basename $p` 10) &
6097a74bbeSPeter Holm	/bin/sleep .2
6197a74bbeSPeter Holm	path=`procstat binary $! | awk "/$!/ {print \\$NF}"`
6297a74bbeSPeter Holm	kill $!
6397a74bbeSPeter Holm	wait
6497a74bbeSPeter Holm	[ "$path" != "$p" ] && { s=1; echo "fail path=$path : prog=$p"; }
6597a74bbeSPeter Holmdone
6697a74bbeSPeter Holmmv /tmp/execpath d1
6797a74bbeSPeter Holmln d1/execpath d2/execpath
6897a74bbeSPeter Holm
6997a74bbeSPeter Holmr=`./d1/execpath`
7097a74bbeSPeter Holmecho $r | grep -q "/d1/" || { s=1; echo "fail: $r. Expected d1 @ 1"; }
7197a74bbeSPeter Holmr=`./d2/execpath`
7297a74bbeSPeter Holmecho $r | grep -q "/d2/" || { s=1; echo "fail: $r. Expected d2 @ 2"; }
7397a74bbeSPeter Holmr=`(cd d1; ./execpath)`
7497a74bbeSPeter Holmecho $r | grep -q "/d1/" || { s=1; echo "fail: $r. Expected d1 @ 3"; }
7597a74bbeSPeter Holmr=`(cd d2; ./execpath)`
7697a74bbeSPeter Holmecho $r | grep -q "/d2/" || { s=1; echo "fail: $r. Expected d2 @ 4"; }
7797a74bbeSPeter Holm
7897a74bbeSPeter Holmcd $odir
7997a74bbeSPeter Holm
8097a74bbeSPeter Holmfor i in `jot 6`; do
8197a74bbeSPeter Holm	mount | grep -q "on $mntpoint " || break
8297a74bbeSPeter Holm	umount $mntpoint && break || sleep 10
8397a74bbeSPeter Holm	[ $i -eq 6 ] &&
8497a74bbeSPeter Holm	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
8597a74bbeSPeter Holmdone
8697a74bbeSPeter Holmmdconfig -d -u $mdstart
8797a74bbeSPeter Holmexit $s
8897a74bbeSPeter Holm
8997a74bbeSPeter HolmEOF
9097a74bbeSPeter Holm/* Test scenario by Tobias Kortkamp <tobik@freebsd.org> */
9197a74bbeSPeter Holm
9297a74bbeSPeter Holm#include <sys/auxv.h>
9397a74bbeSPeter Holm#include <limits.h>
9497a74bbeSPeter Holm#include <stdio.h>
9597a74bbeSPeter Holm
9697a74bbeSPeter Holmint
9797a74bbeSPeter Holmmain(void)
9897a74bbeSPeter Holm{
9997a74bbeSPeter Holm	char pathname[PATH_MAX];
10097a74bbeSPeter Holm
10197a74bbeSPeter Holm	elf_aux_info(AT_EXECPATH, pathname, PATH_MAX);
10297a74bbeSPeter Holm	puts(pathname);
10397a74bbeSPeter Holm	return 0;
10497a74bbeSPeter Holm}
105