Mailing List Archive

systemd unit executing but not persistent later in boot
Hi all,

Still new to systemd and am struggling with a custom unit file.

Some background:

I have a HTPC that requires loading a custom keymap in order for the
remote to work. It sets up an alternate protocol that the driver
supports but not defaults to.

In short:

# ir-keytable
Found /sys/class/rc/rc0/ with:
Name: iMON Remote (15c2:0038)
Driver: imon
Default keymap: rc-imon-pad
Input device: /dev/input/event8
LIRC device: /dev/lirc0
Supported kernel protocols: rc-6 imon
--> Enabled kernel protocols: imon
bus: 3, vendor/product: 15c2:0038, version: 0x0001
Repeat delay = 500 ms, repeat period = 125 ms


This protocol needs to change to rc-6. This is done by a simple command:

/usr/bin/ir-keytable -c -p rc-6 -w /etc/rc_keymaps/imon_mce.toml

So my thought was to create a custom Unit:

[Unit]

Description=Add custom keymap to iMon remote

[Service]

ExecStart=/bin/bash -c "exec /usr/bin/ir-keytable -c -p rc-6 -w
/etc/rc_keymaps/imon_mce.toml"

I've called it ir-key-map.service.

I can see through the status that it is indeed running and applying the
keymaps and protocol changes:

# systemctl status ir-key-map
? ir-key-map.service - Add custom keymap to iMon remote
Loaded: loaded (/etc/systemd/system/ir-key-map.service; enabled;
preset: disabled)
Active: inactive (dead) since Sat 2024-04-06 18:17:43 PDT; 6min ago
Duration: 46ms
Main PID: 292 (code=exited, status=0/SUCCESS)
CPU: 12ms

Apr 06 18:17:43 htpclivingrm systemd[1]: Started Add custom keymap to
iMon remote.
Apr 06 18:17:43 htpclivingrm bash[292]: Read imon_mce table
Apr 06 18:17:43 htpclivingrm bash[292]: Old keytable cleared
Apr 06 18:17:43 htpclivingrm bash[292]: Wrote 76 keycode(s) to driver
--> Apr 06 18:17:43 htpclivingrm bash[292]: Protocols changed to rc-6
Apr 06 18:17:43 htpclivingrm systemd[1]: ir-key-map.service:
Deactivated successfully.

As the output says, it ran successfully.

lircd also is running after this unit like it's supposed to (snipped
from `journalctl --unit lircd`:

Apr 06 18:17:43 htpclivingrm systemd[1]: Starting Flexible IR remote
input/output application support...

However, the remote does not work.

When I log in and check:

# ir-keytable
Found /sys/class/rc/rc0/ with:
Name: iMON Remote (15c2:0038)
Driver: imon
Default keymap: rc-imon-pad
Input device: /dev/input/event8
LIRC device: /dev/lirc0
Supported kernel protocols: rc-6 imon
ARGH --> Enabled kernel protocols: imon
bus: 3, vendor/product: 15c2:0038, version: 0x0001
Repeat delay = 500 ms, repeat period = 125 ms

It's like the unit is running in an isolated environment or something
which is not at all helpful.

What's even stranger is if I manually start it and restart lircd it works!

`systemctl start ir-key-map`
`systemctl restart lircd`

...and it is set up normally and the remote works:

# systemctl start ir-key-map
# systemctl restart lircd
# ir-keytable
Found /sys/class/rc/rc0/ with:
Name: iMON Remote (15c2:0038)
Driver: imon
Default keymap: rc-imon-pad
Input device: /dev/input/event8
LIRC device: /dev/lirc0
Supported kernel protocols: rc-6 imon
YAY --> Enabled kernel protocols: rc-6
bus: 3, vendor/product: 15c2:0038, version: 0x0001
Repeat delay = 500 ms, repeat period = 125 ms

So why does this not work at boot time? Are these units spawned in an
environment on their own and thusly not modifying the main system?

I'm quite confused.

In openrc I just edited /etc/init.d/lircd and put the ir-keytable
command in it before lircd was started and it was working fine.

Can someone give some pointers?

Dan
Re: systemd unit executing but not persistent later in boot [ In reply to ]
On Sat, 6 Apr 2024 at 21:32, Daniel Frey <djqfrey@gmail.com> wrote:
> What's even stranger is if I manually start it and restart lircd it works!

Have you tried adding Before=lircd.service in the [Unit] section?
Re: systemd unit executing but not persistent later in boot [ In reply to ]
On 4/6/24 18:38, Andrew Udvare wrote:
> On Sat, 6 Apr 2024 at 21:32, Daniel Frey <djqfrey@gmail.com> wrote:
>> What's even stranger is if I manually start it and restart lircd it works!
>
> Have you tried adding Before=lircd.service in the [Unit] section?
>

I did, it made no difference.

From what I can tell it is running before lircd.

I have disabled lircd. Ran daemon-reload and rebooted.

For some reason, systemctl is saying it has run and exited successfully
according to the timestamps:

# systemctl status ir-key-map
? ir-key-map.service - Add custom keymap to iMon remote
Loaded: loaded (/etc/systemd/system/ir-key-map.service; enabled;
preset: disabled)
Active: inactive (dead) since Sun 2024-04-07 08:56:18 PDT; 1min
36s ago
Duration: 48ms
Process: 295 ExecStart=/usr/bin/ir-keytable -c -p rc-6 -w
/etc/rc_keymaps/imon_mce.toml (code=exited, status=0/SUCCESS)
Main PID: 295 (code=exited, status=0/SUCCESS)
CPU: 19ms

Apr 07 08:56:18 htpclivingrm systemd[1]: Started Add custom keymap to
iMon remote.
Apr 07 08:56:18 htpclivingrm ir-keytable[295]: Read imon_mce table
Apr 07 08:56:18 htpclivingrm ir-keytable[295]: Old keytable cleared
Apr 07 08:56:18 htpclivingrm ir-keytable[295]: Wrote 76 keycode(s) to driver
Apr 07 08:56:18 htpclivingrm ir-keytable[295]: Protocols changed to rc-6
Apr 07 08:56:18 htpclivingrm systemd[1]: ir-key-map.service: Deactivated
successfully.


But, it doesn't actually change the protocol. It says it has
"ir-keytable[295]: Protocols changed to rc-6" but when you check
manually as root it really hasn't:

# ir-keytable
Found /sys/class/rc/rc0/ with:
Name: iMON Remote (15c2:0038)
Driver: imon
Default keymap: rc-imon-pad
Input device: /dev/input/event8
LIRC device: /dev/lirc0
Supported kernel protocols: rc-6 imon
Enabled kernel protocols: imon
bus: 3, vendor/product: 15c2:0038, version: 0x0001
Repeat delay = 500 ms, repeat period = 125 ms

...and I have no idea why. Until this works, lircd will not work.

If you run it manually, it will update it. It shows in the log and more
importantly, if you check with ir-keytable you can see it was successful.

It's bizarre.

I even tried adding User= and Group= in the [Service] context and it
made no difference.

Dan
Re: systemd unit executing but not persistent later in boot [ In reply to ]
On 4/6/24 18:38, Andrew Udvare wrote:
> On Sat, 6 Apr 2024 at 21:32, Daniel Frey <djqfrey@gmail.com> wrote:
>> What's even stranger is if I manually start it and restart lircd it works!
>
> Have you tried adding Before=lircd.service in the [Unit] section?
>

OK, I have finally sorted out the IR keymap/lircd/irexec/LCDd problems.

It turns out the systemd units as shipped just simply do not work, at
least for my hardware.

The custom unit I made to apply a new keytable and protocol change was
working fine. After a lot of reading about systemd units and also poring
through logs, I discovered udev was triggering something related to the
LCD/IR module - my custom unit was running before this happened but the
udev trigger reset it to defaults (for whatever reason.)

In order to fix that, I had to change my unit to this:

[Unit]
Description=Add custom keymap to iMon remote
Requires=systemd-udevd.service systemd-udev-settle.service
systemd-udev-trigger.service
After=systemd-udevd.service systemd-udev-settle.service
systemd-udev-trigger.service
Before=lircd.service inputlircd.service irexec.service

[Service]
RemainAfterExit=true
ExecStart=/usr/bin/ir-keytable -c -p rc-6 -w /etc/rc_keymaps/imon_mce.toml

[Install]
WantedBy=multi-user.target
RequiredBy=lircd.service irexec.service


This made the keymap change work reliably.

After this lircd was working normally.

I use lircd so I can use irexec to listen for the power button and just
shut the computer off. Kodi is too finicky and just wouldn't turn off or
it would just logout, even with the polkit policy in the wiki applied.

Now, starting irexec didn't work the the shipped systemd unit either, it
would just exit and not stay running. I fixed that with this custom unit:

[Unit]
Documentation=man:irexec(1)
Documentation=http://lirc.org/html/configure.html
Documentation=http://lirc.org/html/configure.html#lircrc_format
Description=Handle events from IR remotes decoded by lircd(8)
After=lircd.service
Requires=lircd.service

[Service]
; user=lirc
; group=lirc

; Hardening opts, see systemd.exec(5). Doesn't add much unless
; not running as root. If these are applicable or not depends on
; what commands irexec.lircrc invokes.
;
; NoNewPrivileges=true
; MemoryDenyWriteExecute=true
; PrivateTmp=true
; ProtectHome=true
; ProtectSystem=full


Type=forking
ExecStart=/usr/bin/irexec --daemon /etc/lirc/irexec.lircrc

[Install]
WantedBy=multi-user.target


So irexec would start and actually stay running. I did also tell it to
start after lircd (although I'm not 100% sure this matters - it may not.)

Of course the LCD unit wouldn't start and stay running either, again the
systemd unit shipped didn't work so I customized it:

[Unit]
Description=LCDProc (LCDd)
After=network-online.target lircd.service
Requires=lircd.service

[Service]
ExecStart=/usr/sbin/LCDd -c /etc/LCDd.conf
Type=forking

[Install]
WantedBy=multi-user.target

After all this, then Kodi wouldn't start up. I figured out quickly this
was caused by lightdm starting before the network was ready, fixed with
this custom unit:

[Unit]
Description=Light Display Manager
Documentation=man:lightdm(1)
After=systemd-user-sessions.service
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/sbin/lightdm
Restart=always
IgnoreSIGPIPE=no
BusName=org.freedesktop.DisplayManager

[Install]
Alias=display-manager.service


Hopefully this may be helpful to others with HTPCs switching to systemd
from openrc. In openrc nearly all of this customization isn't needed - I
only modified /etc/init.d/lircd in openrc in order to load the keymap
before lircd started.

Dan