All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next] bpftool: bash-completion: Add nopasswd sudo prefix for bpftool
@ 2025-02-12 10:14 Rong Tao
  2025-02-12 11:00 ` Quentin Monnet
  0 siblings, 1 reply; 3+ messages in thread
From: Rong Tao @ 2025-02-12 10:14 UTC (permalink / raw)
  To: qmo, ast, daniel, andrii
  Cc: rongtao, rtoax, Rong Tao, Martin KaFai Lau, Eduard Zingerman,
	Song Liu, Yonghong Song, John Fastabend, KP Singh,
	Stanislav Fomichev, Hao Luo, Jiri Olsa, Tao Chen, Mykyta Yatsenko,
	Daniel Xu, open list:BPF [TOOLING] (bpftool), open list

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-02-12 11:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-12 10:14 [PATCH bpf-next] bpftool: bash-completion: Add nopasswd sudo prefix for bpftool Rong Tao
2025-02-12 11:00 ` Quentin Monnet
2025-02-12 11:21   ` Rong Tao

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.