0

I writing my first systemd service which is a radio manager service. It is oneshot and is meant to run at system boot and system shutdown. It will call rfkill to either block or unblock Wifi and/or bluetooth so it depends on NetworkManager. I would like to try and keep the service as a user service unless I have to make it a system service.

I have also setup the system so that user pi with loginctl so it lingers.

My problem is that the service is called at shutdown but wifi and bluetooth radios are not blocked as I would have expected. Any suggestions are appreciated. Thanks!!!!!

Here is my service file:

    [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

Here is my python script

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))

Edit: First I re-enabled and started my service. See timestamp 22:17:42 onwards

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 showed both interfaces were unblocked as expected. After sudo shutdown and boot up

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:~$ 

Edit: I turned on persistent systemd journal and I now see that my service is called at Shutdown but perhaps after the rfkill is shutdown or stops listening for commands. I have attached a truncated system log from shutdown with key points

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.
3
  • What reason do you have to believe that the rfkill setting on your raspberry pi is persistent over a reboot? On most hardware it is not (I guess Thinkpad laptops were the main exception).
    – sourcejedi
    Commented Aug 22, 2020 at 9:21
  • @sourcejedi I believe you are correct and the rfkill setting is not persistent over a reboot. Whether that is because my NetworkManager wifi connection is autoconnect or for some other reason I don't know. I think I'll simplify my service and shutdown wifi at startup if it is not needed. It will serve the same purpose and be less complicated. Bluetooth with rfkill is persistent. And then perhaps I don't need to make it a system service either. Thanks for the help! Commented Aug 24, 2020 at 2:18
  • I'd be surprised if the bluetooth hardware is doing that. Ah, make sure you don't get into a fight with systemd-rfkill.service. It might be simplest to disable it if you want a different policy. Otherwise, check your service is strictly ordered relative to it to avoid unpredictable results.
    – sourcejedi
    Commented Aug 24, 2020 at 9:24

1 Answer 1

1

After=network.target

User units cannot depend on system units. This includes "ordering dependencies" like After=. network.target is a system unit. So this line will not do anything.

On the other hand, user units run inside the system unit [email protected]. This unit is already (indirectly) ordered After=network.target.

On the gripping hand, I think polkit.service will quit immediately on shutdown, and probably cannot be re-activated. Perhaps that is your problem. polkit is used to grant permissions like allowing logged-in users to manipulate rfkill.

7
  • If that is the case then I think you are saying I need to make this a system service. Correct? I read this superuser.com/questions/1410888/… and someone had to create 2 services. If I have to create a system service can the python script still exist in a user space directory or does it need to be moved to something like /usr/local/lib/ Commented Aug 20, 2020 at 21:01
  • And yes you are correct. NetworkManager is how I'm getting access to rfkill Commented Aug 20, 2020 at 21:05
  • @MartinJoseph might be polkit.service though, that's needed to grant permission? Edit your question to show what journalctl --user-unit=name-of-my-service-here shows about your unit after you shut down (and started up again, obviously :-).
    – sourcejedi
    Commented Aug 20, 2020 at 21:11
  • Done. I've updated the question but journalctl is not set to log persistent which perhaps I need to do to see the shutdown behavior. Commented Aug 20, 2020 at 21:33
  • I saw you updated answer. It seems like all signs point to a system service and not a user service. I'll give that a shot Commented Aug 20, 2020 at 21:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .