用于调用 rfkill(来自 NetworkManager)的用户 systemd 服务在关机时不起作用

用于调用 rfkill(来自 NetworkManager)的用户 systemd 服务在关机时不起作用

我编写了我的第一个 systemd 服务,它是一个无线电管理器服务。它是一次性的,旨在在系统启动和关闭时运行。它将调用 rfkill 来阻止或取消阻止 Wifi 和/或蓝牙,因此它取决于 NetworkManager。我想尝试将该服务保留为用户服务,除非我必须将其设为系统服务。

我还设置了系统,以便用户 pi 与 loginctl 保持一致。

我的问题是,该服务在关闭时被调用,但 wifi 和蓝牙无线电并未像我预期的那样被阻止。任何建议表示赞赏。谢谢!!!!!

这是我的服务文件:

    [Unit]
    Description=RAL Radio Manager BT and Wifi Service
    After=network.target

    [Service]
    Type=oneshot

    WorkingDirectory=/home/pi/RAL/radio-manager
    Environment=PYTHONUNBUFFERED=1
    ExecStart=/usr/bin/python3 /home/pi/RAL/radio-manager/radio_manager.py -bs=1
    ExecStop=/usr/bin/python3 /home/pi/RAL/radio-manager/radio_manager.py -bs=0
    RemainAfterExit=yes
    Restart=no

    [Install]
    #WantedBy=multi-user.target
    WantedBy=default.target

这是我的 python 脚本

def radio_manager(args):
    MAGNET_OR_SWITCH_GPIO = 4
    PROGRAM_EXIT_GPIO = 22    
    
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM )                               # .BCM refs the GPIO by the GPIO#, .BOARD by Pin number
    GPIO.setup( MAGNET_OR_SWITCH_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)      # RPi Header Pin  7,  GPIO4, reads the onboard swx state S2.    
    GPIO.setup(PROGRAM_EXIT_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_DOWN )    
    
    #radio_list = rfkill.rfkill_list()
    
    if (args.boot_state == 0):
        #Power Down
        
        rfkill.rfkill_blockby('wlan')
        print ("Blocking WIFI!!!!!")

        rfkill.rfkill_blockby('bluetooth')
        print ("Blocking Bluetooth!!!!!")
            
    
    elif(args.boot_state == 1):
        #Power Up
            program_exit_pin = GPIO.input(PROGRAM_EXIT_GPIO)
            user_requests_wifi = os.path.exists(WIFI_ON_FILE_PATH)
            if (program_exit_pin or user_requests_wifi):
                rfkill.rfkill_unblockby('wlan')
                print ("UnBlocking WIFI!!!!!")


            magnet_pin = GPIO.input(MAGNET_OR_SWITCH_GPIO)
            if(magnet_pin == 0):
                rfkill.rfkill_unblockby('bluetooth')
                print ("UnBlocking Bluetooth!!!!!")

    return

def main(argv):
   
     args=cli(argv)
 
     radio_manager(args)    
    
     return 0

if __name__ == '__main__':
     sys.exit(main(sys.argv))

编辑:首先我重新启用并启动我的服务。查看时间戳 22:17:42 以后

pi@raspberrypi:~$ systemctl --user enable radio_manager.service
Created symlink /home/pi/.config/systemd/user/default.target.wants/radio_manager.service â /home/pi/.config/systemd/user/radio_manager.service.
pi@raspberrypi:~$ systemctl --user start radio_manager.service
pi@raspberrypi:~$ journalctl --user-unit radio_manager.service
-- Logs begin at Thu 2020-08-20 20:04:47 BST, end at Thu 2020-08-20 22:17:43 BST
Aug 20 20:05:14 raspberrypi systemd[370]: Starting RAL Radio Manager BT and Wifi
Aug 20 20:05:16 raspberrypi systemd[370]: Started RAL Radio Manager BT and Wifi 
Aug 20 21:42:49 raspberrypi systemd[370]: Stopping RAL Radio Manager BT and Wifi
Aug 20 21:42:51 raspberrypi python3[650]: Blocking WIFI!!!!!
Aug 20 21:42:51 raspberrypi python3[650]: Blocking Bluetooth!!!!!
Aug 20 21:42:51 raspberrypi systemd[370]: radio_manager.service: Succeeded.
Aug 20 21:42:51 raspberrypi systemd[370]: Stopped RAL Radio Manager BT and Wifi 
Aug 20 22:17:42 raspberrypi systemd[370]: Starting RAL Radio Manager BT and Wifi
Aug 20 22:17:43 raspberrypi systemd[370]: Started RAL Radio Manager BT and Wifi

rfkill 显示两个接口均按预期畅通无阻。 sudo 关闭并启动后

pi@raspberrypi:~$ journalctl --user-unit radio_manager
-- Logs begin at Thu 2020-08-20 22:22:23 BST, end at Thu 2020-08-20 22:25:04 BST
Aug 20 22:22:51 raspberrypi systemd[359]: Starting RAL Radio Manager BT and Wifi
Aug 20 22:22:53 raspberrypi systemd[359]: Started RAL Radio Manager BT and Wifi 
lines 1-3/3 (END)
pi@raspberrypi:~$ 
pi@raspberrypi:~$ rfkill list 
0: phy0: Wireless LAN
        Soft blocked: no
        Hard blocked: no
1: hci0: Bluetooth
        Soft blocked: no
        Hard blocked: no
pi@raspberrypi:~$ 

编辑:我打开了持久性 systemd 日志,现在我看到我的服务在关闭时被调用,但可能是在 rfkill 关闭或停止监听命令之后。我附上了关机时截断的系统日志,其中包含要点

Aug 21 16:18:23 raspberrypi sshd[522]: pam_unix(sshd:session): session opened fo
Aug 21 16:18:23 raspberrypi systemd-logind[253]: New session c1 of user pi.
Aug 21 16:18:23 raspberrypi systemd[1]: Started Session c1 of user pi.
Aug 21 16:18:30 raspberrypi systemd[1]: systemd-hostnamed.service: Succeeded.
Aug 21 16:23:20 raspberrypi sudo[560]:       pi : TTY=pts/0 ; PWD=/home/pi ; USE    <----------- Sudo shutdown now issued
Aug 21 16:23:20 raspberrypi sudo[560]: pam_unix(sudo:session): session opened fo
Aug 21 16:23:21 raspberrypi sudo[560]: pam_unix(sudo:session): session closed fo
Aug 21 16:23:21 raspberrypi systemd[1]: Unmounting RPC Pipe File System...
Aug 21 16:23:21 raspberrypi sshd[522]: pam_unix(sshd:session): session closed fo
Aug 21 16:23:21 raspberrypi sshd[522]: pam_systemd(sshd:session): Failed to rele
Aug 21 16:23:21 raspberrypi systemd[1]: Stopping Session c1 of user pi.
Aug 21 16:23:21 raspberrypi systemd[1]: systemd-rfkill.socket: Succeeded.           <------------ Is this the service I need to set the radio status using rfkill
Aug 21 16:23:21 raspberrypi systemd[1]: Closed Load/Save RF Kill Switch Status /
Aug 21 16:23:21 raspberrypi systemd[1]: Stopped target Bluetooth.
Aug 21 16:23:21 raspberrypi systemd[364]: run-rpc_pipefs.mount: Succeeded.
Aug 21 16:23:21 raspberrypi systemd[1]: Stopping Authorization Manager...




Aug 21 16:23:23 raspberrypi systemd[1]: Stopping Manage Sound Card State (restor
Aug 21 16:23:23 raspberrypi systemd[364]: Stopping RAL Radio Manager BT and Wifi    <------ SystemD executing ExecStop for my service
Aug 21 16:23:23 raspberrypi systemd[1]: Removed slice system-getty.slice.
Aug 21 16:23:23 raspberrypi systemd[1]: rc-local.service: Succeeded.
Aug 21 16:23:23 raspberrypi systemd[1]: Stopped /etc/rc.local Compatibility.
Aug 21 16:23:23 raspberrypi alsactl[290]: alsactl daemon stopped
Aug 21 16:23:23 raspberrypi systemd[1]: systemd-logind.service: Succeeded.
Aug 21 16:23:23 raspberrypi systemd[1]: Stopped Login Service.
Aug 21 16:23:23 raspberrypi systemd[1]: raspi-config.service: Succeeded.
Aug 21 16:23:23 raspberrypi systemd[1]: Stopped LSB: Switch to ondemand cpu gove
Aug 21 16:23:23 raspberrypi systemd[1]: alsa-state.service: Succeeded.
Aug 21 16:23:23 raspberrypi systemd[1]: Stopped Manage Sound Card State (restore
Aug 21 16:23:24 raspberrypi systemd[1]: hciuart.service: Succeeded.
Aug 21 16:23:24 raspberrypi systemd[1]: Stopped Configure Bluetooth Modems conne
Aug 21 16:23:24 raspberrypi wpa_supplicant[282]: wlan0: CTRL-EVENT-DISCONNECTED 
Aug 21 16:23:24 raspberrypi wpa_supplicant[282]: dbus: wpa_dbus_property_changed
Aug 21 16:23:24 raspberrypi dhclient[398]: receive_packet failed on wlan0: Netwo
Aug 21 16:23:25 raspberrypi python3[591]: Blocking WIFI!!!!!                        <------ My service is trying to Softblock WIFI
Aug 21 16:23:25 raspberrypi python3[591]: Blocking Bluetooth!!!!!                   <------ My service is trying to Softblock Bluetooth
Aug 21 16:23:24 raspberrypi wpa_supplicant[282]: wlan0: CTRL-EVENT-REGDOM-CHANGE
Aug 21 16:23:25 raspberrypi wpa_supplicant[282]: rfkill: WLAN soft blocked
Aug 21 16:23:25 raspberrypi wpa_supplicant[282]: rfkill: WLAN soft blocked
Aug 21 16:23:25 raspberrypi wpa_supplicant[282]: nl80211: Failed to open /proc/s
Aug 21 16:23:25 raspberrypi wpa_supplicant[282]: nl80211: Failed to set IPv4 uni
Aug 21 16:23:25 raspberrypi NetworkManager[269]: <info>  [1598023405.1102] manag
Aug 21 16:23:25 raspberrypi NetworkManager[269]: <info>  [1598023405.1197] devic
Aug 21 16:23:25 raspberrypi NetworkManager[269]: <info>  [1598023405.1415] dhcp4
Aug 21 16:23:25 raspberrypi NetworkManager[269]: <info>  [1598023405.1441] dhcp4
Aug 21 16:23:25 raspberrypi NetworkManager[269]: <info>  [1598023405.2310] dns-m
Aug 21 16:23:25 raspberrypi systemd[364]: radio_manager.service: Succeeded.
Aug 21 16:23:25 raspberrypi systemd[364]: Stopped RAL Radio Manager BT and Wifi     <------ Systemd stopped by service
Aug 21 16:23:25 raspberrypi systemd[364]: Stopped target Basic System.
Aug 21 16:23:25 raspberrypi systemd[364]: Stopped target Timers.
Aug 21 16:23:25 raspberrypi systemd[364]: Stopped target Sockets.

答案1

之后=网络.目标

用户单位不能依赖于系统单位。这包括“排序依赖项”,例如After=. network.target 是一个系统单元。所以这条线不会做任何事情。

另一方面,用户单元在系统单元内部运行[email protected]。该单元已在 After=network.target 之后(间接)订购。

在紧握的手上,我认为polkit.service关闭后会立即退出,并且可能无法重新激活。也许这就是你的问题。 polkit 用于授予权限,例如允许登录用户操作 rfkill。

相关内容