之前自己研究实现了桌面程序以root权限运行的方式[1]。发现gparted程序不是这样实现的。
$ cat /usr/share/applications/gparted.desktop
[Desktop Entry]
Name=GParted
GenericName=Partition Editor
X-GNOME-FullName=GParted Partition Editor
Comment=Create, reorganize, and delete partitions
Exec=/usr/bin/gparted %f
Icon=gparted
Terminal=false
Type=Application
Categories=GNOME;System;Filesystem;X-Fedora;GTK;
Keywords=Partition;
StartupNotify=true
X-Desktop-File-Install-Version=0.26
它直接就是执行 /usr/bin/gparted。在命令行手动运行gparted也能弹出root输入框。使用file查看garpted。
$ file /usr/bin/gparted
/usr/bin/gparted: a /usr/bin/sh script, ASCII text executable
发现它是一个脚本。打开发现,这个是个wrapper,它实现了登陆,真正的程序是/usr/libexec/gpartedbin
脚本里面判断非root,和登陆的代码如下:
#
# For non-root users try to get authorisation to run GParted as root.
#
if test "x`id -u`" != "x0"; then
#
# If there is no configured SU program run gpartedbin as
# non-root to display the graphical error about needing root
# privileges.
#
if test "xpkexec --disable-internal-agent" = "x"; then
echo "Root privileges are required for running gparted."
$BASE_CMD
exit 1
fi
#
# Interim workaround to allow GParted run by root access to the
# X11 display server under Wayland. If configured with
# './configure --enable-xhost-root', the xhost command is
# available and root has not been granted access to the X11
# display via xhost, then grant access.
#
ENABLE_XHOST_ROOT=yes
GRANTED_XHOST_ROOT=no
if test "x$ENABLE_XHOST_ROOT" = 'xyes' && xhost 1> /dev/null 2>&1; then
if ! xhost | grep -qi 'SI:localuser:root$'; then
xhost +SI:localuser:root
GRANTED_XHOST_ROOT=yes
fi
fi
#
# Run gparted as root.
#
pkexec --disable-internal-agent '/usr/bin/gparted' "$@"
status=$?
#
# Revoke root access to the X11 display, only if we granted it.
#
if test "x$GRANTED_XHOST_ROOT" = 'xyes'; then
xhost -SI:localuser:root
fi
exit $status
fi
参考
[1] run desktop gui application as root,link