AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-255730

AlmuHS's questions

Martin Hope
AlmuHS
Asked: 2018-02-26 11:59:12 +0800 CST

如何允许使用 pkexec 作为另一个用户运行 notify-send?

  • 2

作为这个问题的延续(如何使用 polkit 0.106 发送通知?),我发现我必须以notify-send要发送通知的用户身份执行。

但是,使用我当前的配置,我不能这样做,因为 polkit 以polkitd用户身份执行脚本,而且我不能su $user没有已知的用户密码。

因此,我需要创建一个新的 polkit 操作,以允许notify-send从 polkitd 以其他用户身份执行。

我的 polkit 规则是这样的:

polkit.addRule(function(action, subject) {
     if (action.id == "org.freedesktop.consolekit.system.stop" ||
        action.id == "org.freedesktop.login1.power-off" ||
        action.id == "org.freedesktop.login1.power-off-multiple-sessions" || 
        action.id == "org.xfce.session.xfsm-shutdown-helper")  
     {

        try{    
            polkit.spawn(["/usr/bin/pendrive-reminder/check_pendrive.sh", subject.user]);        
            return polkit.Result.YES;

        }catch(error){
            polkit.spawn(["/usr/bin/pendrive-reminder/send_notify.sh", subject.user]);
           return polkit.Result.NO;
        }
    }
});

此 polkit 规则必须在关机菜单中锁定关机选项,并显示带有notify-send,send_notify.sh脚本的通知,该脚本执行以下操作:

#!/bin/bash

export DISPLAY=":0"

user=$1
pkexec --user $user notify-send  "Pendrive Reminder" "Shutdown lock enabled. Disconnect pendrive to enable shutdown" -u critical

exit 0

我试图添加这个 polkit 策略文件:

<policyconfig>
    <action id="org.freedesktop.notify-send">
    <description>Launch notify-send command</description>
    <defaults>
        <allow_any>yes</allow_any>
        <allow_inactive>yes</allow_inactive>
        <allow_active>yes</allow_active>
    </defaults>
   <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/notify-send</annotate>
   <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
   </action>
</policyconfig>

我把这个文件放在/usr/share/polkit-1/actions/org.freedesktop.policykit.notify-send.policy

但是,将策略文件放入/usr/share/polkit-1/rules.d/并按下关机按钮后,关机菜单需要很长时间才能显示,并且没有出现通知。关机选项已正确锁定

如何让 polkit 可以从我的脚本中调用 notify-send?

linux polkit
  • 1 个回答
  • 954 Views
Martin Hope
AlmuHS
Asked: 2018-02-20 07:30:32 +0800 CST

如何使用 polkit 0.106 发送通知?

  • 2

我正在开发一个应用程序,不要忘记 pendrive。

如果 pendrive 连接到机器,此应用程序必须锁定关机。在这种形式中,如果用户想在连接笔式驱动器时关闭系统,系统会显示一条通知,提醒它必须断开笔式驱动器才能解锁关机。

为了检测关机事件,我设置了一个 polkit 规则,它调用一个脚本来检查是否有任何 pendrive 连接到系统。

如果有任何 pendrive 连接,则 polkit 规则通过脚本调用 notify-send,该脚本send_notify.sh执行以下命令:

notify-send "Pendrive-Reminder" "Extract Pendrive to enable shutdown" -t 5000

polkit 规则是这样的:

polkit.addRule(function(action, subject) {
    if (action.id == "org.freedesktop.consolekit.system.stop" ||
        action.id == "org.freedesktop.login1.power-off" ||
        action.id == "org.freedesktop.login1.power-off-multiple-sessions" || 
        action.id == "org.xfce.session.xfsm-shutdown-helper")  
    {

        try{    
            polkit.spawn(["/usr/bin/pendrive-reminder/check_pendrive.sh", subject.user]);        
            return polkit.Result.YES;

        }catch(error){
            polkit.spawn(["/usr/bin/pendrive-reminder/send_notify.sh", subject.user]);
        return polkit.Result.NO;
     }
   }
  }

但。设置此 polkit 规则并按下关机按钮后,我的用户没有收到任何通知。

我调试了规则并检查了第二个脚本它已执行,但notify-send没有向我的用户显示通知。

我该如何解决这个问题?

更新:

我试图修改脚本是这样的:

#!/bin/bash

user=$1

XAUTHORITY="/home/$user/.Xauthority"
DISPLAY=$( who | grep -m1 $user.*\( | awk '{print $5}' | sed 's/[(|)]//g')

notify-send "Extract Pendrive to enable shutdown" -t 5000

exit 0

用户由 pòlkit 作为参数传递

但问题还在继续

更新:我刚刚看到这个错误https://bugs.launchpad.net/ubuntu/+source/libnotify/+bug/160598不允许以 root 身份发送通知。

稍后我将测试修改解决方法更改用户

UPDATE2:将代码更改为此之后。问题继续存在:

#!/bin/bash

export XAUTHORITY="/home/$user/.Xauthority"
export DISPLAY=$(cat "/tmp/display.$user")

user=$1
su $user -c 'notify-send "Pendrive Reminder" "Shutdown lock enabled. Disconnect pendrive to enable shutdown" -u critical'
linux shutdown
  • 3 个回答
  • 538 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve