mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Rong Tao <rtoax@foxmail.com>
To: qmo@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org
Cc: rongtao@cest.ccn, rtoax@foxmail.com, Rong Tao <rongtao@cestc.cn>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>, Tao Chen <chen.dylane@gmail.com>,
	Mykyta Yatsenko <yatsenko@meta.com>, Daniel Xu <dxu@dxuuu.xyz>,
	bpf@vger.kernel.org (open list:BPF [TOOLING] (bpftool)),
	linux-kernel@vger.kernel.org (open list)
Subject: [PATCH bpf-next] bpftool: bash-completion: Add nopasswd sudo prefix for bpftool
Date: Wed, 12 Feb 2025 18:14:30 +0800	[thread overview]
Message-ID: <tencent_515567355C0AA854BDA68C3A219A18040B0A@qq.com> (raw)

From: Rong Tao <rongtao@cestc.cn>

In the bpftool script of bash-completion, many bpftool commands require
superuser privileges to execute. Otherwise, Operation not permission will
be displayed. Here, we check whether ordinary users are exempt from
entering the sudo password. If so, we need to add the sudo prefix to the
bpftool command to be executed. In this way, we can obtain the correct
command completion content instead of the wrong one.

For example, when updating array_of_maps, the wrong 'hex' is completed:

    $ sudo bpftool map update name arr_maps key 0 0 0 0 value [tab]
    $ sudo bpftool map update name arr_maps key 0 0 0 0 value hex

However, what we need is "id name pinned". Similarly, there is the same
problem in getting the map 'name' and 'id':

    $ sudo bpftool map show name [tab] < get nothing
    $ sudo bpftool map show id [tab]   < get nothing

This commit fixes the issue.

    $ sudo bpftool map update name arr_maps key 0 0 0 0 value [tab]
    id      name    pinned

    $ sudo bpftool map show name
    arr_maps         cgroup_hash      inner_arr1       inner_arr2

    $ sudo bpftool map show id
    11    1383  4091  4096

Signed-off-by: Rong Tao <rongtao@cestc.cn>
---
 tools/bpf/bpftool/bash-completion/bpftool | 29 +++++++++++++++--------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
index 1ce409a6cbd9..25fb859cdfa4 100644
--- a/tools/bpf/bpftool/bash-completion/bpftool
+++ b/tools/bpf/bpftool/bash-completion/bpftool
@@ -5,6 +5,15 @@
 #
 # Author: Quentin Monnet <quentin.monnet@netronome.com>
 
+# In the bpftool script of bash-completion, many bpftool commands require
+# superuser privileges to be executed. Otherwise, EPERM will occur. Here,
+# it is detected whether ordinary users are exempt from sudo passwords. If
+# so, it is necessary to add the "sudo" prefix to the required bpftool
+# command execution.
+if sudo --non-interactive true 2>/dev/null; then
+    _sudo=sudo
+fi
+
 # Takes a list of words in argument; each one of them is added to COMPREPLY if
 # it is not already present on the command line. Returns no value.
 _bpftool_once_attr()
@@ -46,7 +55,7 @@ _bpftool_one_of_list()
 
 _bpftool_get_map_ids()
 {
-    COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \
+    COMPREPLY+=( $( compgen -W "$( ${_sudo} bpftool -jp map  2>&1 | \
         command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
 }
 
@@ -54,14 +63,14 @@ _bpftool_get_map_ids()
 _bpftool_get_map_ids_for_type()
 {
     local type="$1"
-    COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \
+    COMPREPLY+=( $( compgen -W "$( ${_sudo} bpftool -jp map  2>&1 | \
         command grep -C2 "$type" | \
         command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
 }
 
 _bpftool_get_map_names()
 {
-    COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \
+    COMPREPLY+=( $( compgen -W "$( ${_sudo} bpftool -jp map  2>&1 | \
         command sed -n 's/.*"name": \(.*\),$/\1/p' )" -- "$cur" ) )
 }
 
@@ -69,38 +78,38 @@ _bpftool_get_map_names()
 _bpftool_get_map_names_for_type()
 {
     local type="$1"
-    COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \
+    COMPREPLY+=( $( compgen -W "$( ${_sudo} bpftool -jp map  2>&1 | \
         command grep -C2 "$type" | \
         command sed -n 's/.*"name": \(.*\),$/\1/p' )" -- "$cur" ) )
 }
 
 _bpftool_get_prog_ids()
 {
-    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
+    COMPREPLY+=( $( compgen -W "$( ${_sudo} bpftool -jp prog 2>&1 | \
         command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
 }
 
 _bpftool_get_prog_tags()
 {
-    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
+    COMPREPLY+=( $( compgen -W "$( ${_sudo} bpftool -jp prog 2>&1 | \
         command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) )
 }
 
 _bpftool_get_prog_names()
 {
-    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
+    COMPREPLY+=( $( compgen -W "$( ${_sudo} bpftool -jp prog 2>&1 | \
         command sed -n 's/.*"name": "\(.*\)",$/\1/p' )" -- "$cur" ) )
 }
 
 _bpftool_get_btf_ids()
 {
-    COMPREPLY+=( $( compgen -W "$( bpftool -jp btf 2>&1 | \
+    COMPREPLY+=( $( compgen -W "$( ${_sudo} bpftool -jp btf 2>&1 | \
         command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
 }
 
 _bpftool_get_link_ids()
 {
-    COMPREPLY+=( $( compgen -W "$( bpftool -jp link 2>&1 | \
+    COMPREPLY+=( $( compgen -W "$( ${_sudo} bpftool -jp link 2>&1 | \
         command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
 }
 
@@ -156,7 +165,7 @@ _bpftool_map_guess_map_type()
     [[ -z $ref ]] && return 0
 
     local type
-    type=$(bpftool -jp map show $keyword $ref | \
+    type=$(${_sudo} bpftool -jp map show $keyword $ref | \
         command sed -n 's/.*"type": "\(.*\)",$/\1/p')
     [[ -n $type ]] && printf $type
 }
-- 
2.48.1


             reply	other threads:[~2025-02-12 10:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-12 10:14 Rong Tao [this message]
2025-02-12 11:00 ` Quentin Monnet
2025-02-12 11:21   ` Rong Tao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tencent_515567355C0AA854BDA68C3A219A18040B0A@qq.com \
    --to=rtoax@foxmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=chen.dylane@gmail.com \
    --cc=daniel@iogearbox.net \
    --cc=dxu@dxuuu.xyz \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=qmo@kernel.org \
    --cc=rongtao@cest.ccn \
    --cc=rongtao@cestc.cn \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=yatsenko@meta.com \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®