Subject: Re: [PATCH] samples/bpf: Fix tc and ip path in xdp2skb_meta.sh

From: Jesper Dangaard Brouer <[email-masked]>
Date: Mon, 9 Jul 2018 17:40:29 +0200

On Tue, 10 Jul 2018 00:04:18 +0900
Taeung Song <[email-masked]> wrote:

> The below path error can occur:
> 
>   # ./xdp2skb_meta.sh --dev eth0 --list
>   ./xdp2skb_meta.sh: line 61: /usr/sbin/tc: No such file or directory
> 
>   # which tc
>   /sbin/tc
> 
> So use 'which' command instead of absolute path of tc and ip
> 
> Fixes: 36e04a2d78d9 ("samples/bpf: xdp2skb_meta shows transferring info from XDP to SKB")
> Cc: Jesper Dangaard Brouer <[email-masked]>
> Signed-off-by: Taeung Song <[email-masked]>
> ---
>  samples/bpf/xdp2skb_meta.sh | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/samples/bpf/xdp2skb_meta.sh b/samples/bpf/xdp2skb_meta.sh
> index b9c9549c4c27..67cf7b5f336d 100755
> --- a/samples/bpf/xdp2skb_meta.sh
> +++ b/samples/bpf/xdp2skb_meta.sh
> @@ -16,8 +16,8 @@
>  BPF_FILE=xdp2skb_meta_kern.o
>  DIR=$(dirname $0)
>  
> -export TC=/usr/sbin/tc
> -export IP=/usr/sbin/ip
> +export TC=`which tc`
> +export IP=`which ip`

This is not a good solution, as 'which' can return something else.
E.g. on my system I've aliased 'tc' to 'sudo tc', and `which tc` returns:

$ which tc
alias tc='sudo tc'
	/usr/bin/sudo

The easiest solution is to simply do:

 export TC=tc
 export IP=ip

The more fancy solution is to allow callers to redefine $IP and $TC:

[ -z "$TC" ] && TC=tc
[ -z "$IP" ] && IP=ip

And then you should also fix the use of 'basename', see below patch...
-- 
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Principal Kernel Engineer at Red Hat
  LinkedIn: http://www.linkedin.com/in/brouer

diff --git a/samples/bpf/xdp2skb_meta.sh b/samples/bpf/xdp2skb_meta.sh
index b9c9549c4c27..4bde9d066c46 100755
--- a/samples/bpf/xdp2skb_meta.sh
+++ b/samples/bpf/xdp2skb_meta.sh
@@ -16,8 +16,8 @@
 BPF_FILE=xdp2skb_meta_kern.o
 DIR=$(dirname $0)
 
-export TC=/usr/sbin/tc
-export IP=/usr/sbin/ip
+[ -z "$TC" ] && TC=tc
+[ -z "$IP" ] && IP=ip
 
 function usage() {
     echo ""
@@ -53,7 +53,7 @@ function _call_cmd() {
     local allow_fail="$2"
     shift 2
     if [[ -n "$VERBOSE" ]]; then
-	echo "$(basename $cmd) $@"
+	echo "$cmd $@"
     fi
     if [[ -n "$DRYRUN" ]]; then
 	return