Re: [systemd-devel] Launching script that needs network before suspend

2022-01-31 Thread Mantas Mikulėnas
On Mon, Jan 31, 2022 at 2:01 PM Lennart Poettering 
wrote:

> On So, 23.01.22 22:13, Tomáš Hnyk (tomash...@gmail.com) wrote:
>
> > Hello,
> > I have my computer hooked up to an AVR that runs my home cinema and
> ideally
> > I would like the computer to turn off the AVR when I turn it off or
> suspend
> > it. The only way to do this is over network and I wrote a simple script
> that
> > does just that. Hooking it to shutdown was quite easy using
> network.target
> > that is defined when shutting down.
> >
> >
> > I am struggling to make it work with suspend though. When I look at the
> > logs, terminating network seems to be the first thing that happens when
> > suspend is invoked.
>
> That shouldn't happen. Normally networking shouldn't be shut down
> during suspend. If your network management solution does this
> explicitly, I am puzzled, why it would do that.
>

NetworkManager does that, especially for Wi-Fi; I don't remember the
rationale though. (It uses systemd's delay inhibitors.)

-- 
Mantas Mikulėnas


Re: [systemd-devel] Launching script that needs network before suspend

2022-01-31 Thread Lennart Poettering
On So, 23.01.22 22:13, Tomáš Hnyk (tomash...@gmail.com) wrote:

> Hello,
> I have my computer hooked up to an AVR that runs my home cinema and ideally
> I would like the computer to turn off the AVR when I turn it off or suspend
> it. The only way to do this is over network and I wrote a simple script that
> does just that. Hooking it to shutdown was quite easy using network.target
> that is defined when shutting down.
>
>
> I am struggling to make it work with suspend though. When I look at the
> logs, terminating network seems to be the first thing that happens when
> suspend is invoked.

That shouldn't happen. Normally networking shouldn't be shut down
during suspend. If your network management solution does this
explicitly, I am puzzled, why it would do that.

> I tried putting the script to
> /usr/lib/systemd/system-sleep/ and it runs, but only after network si down,
> so it fails. Running the script with systemd-inhibit
> (ExecStart=/usr/bin/systemd-inhibit --what=sleep my_script) tells me that
> "Failed to inhibit: The operation inhibition has been requested for is
> already running".

Inhibitors are designed to be taken by long running apps, and not by
the stuff that is run when you are already suspending. i.e. it's too
late then: if you want to temporarily stall suspending to do your
stuff, it's already too late once these scripts are invoked, because
they are invoked once it was decided to actually go into suspend now.

> Is there a way to make this work with service files by specifying that the
> script needs to be run before network is shut down or would I need to run a
> daemon listening for PrepareForSleep as here:
> https://github.com/davidn/av/blob/master/av ?

Usually that's what you do, yes: you take an inhibitor lock while you
are running, and wait until you are informed about system suspend,
then you do your thing, and release the lock once you are done at
which point the suspend continues.

Lennart

--
Lennart Poettering, Berlin


Re: [systemd-devel] Launching script that needs network before suspend

2022-01-25 Thread Tomáš Hnyk
On Monday 24. January 2022, 18:56:59 (+01:00), Andrei Borzenkov wrote:

> On 24.01.2022 17:37, Tomáš Hnyk wrote:
> >
> >
> > On Monday 24. January 2022, 13:50:48 (+01:00), Andrei Borzenkov wrote:
> >
>
> I posted it in response to the list and you sent personal reply. Please use
> reply to all.
Sorry about that, my mistake!

> >> On Mon, Jan 24, 2022 at 1:14 AM Tomáš Hnyk  wrote:
> >>>
> >>> Hello,
> >>> I have my computer hooked up to an AVR that runs my home cinema and 
> >>> ideally I would like the computer to turn off the AVR when I turn it off 
> >>> or suspend it. The only way to do this is over network and I wrote a 
> >>> simple script that does just that. Hooking it to shutdown was quite easy 
> >>> using network.target that is defined when shutting down.
> >>>
> >>> I am struggling to make it work with suspend though. When I look at the 
> >>> logs, terminating network seems to be the first thing that happens when 
> >>> suspend is invoked. I tried putting the script to 
> >>> /usr/lib/systemd/system-sleep/ and it runs, but only after network si 
> >>> down, so it fails. Running the script with systemd-inhibit 
> >>> (ExecStart=/usr/bin/systemd-inhibit --what=sleep my_script) tells me that 
> >>> "Failed to inhibit: The operation inhibition has been requested for is 
> >>> already running".
> >>>
> >>
> >> What network management program are you using?
> > Network Manager. I tried putting "systemctl start NetworkManager.service" 
> > into the /usr/lib/systemd/system-sleep/ hook but even though the log says 
> > network is reestablished, the script still fails.
>
> NetworkManager has own hooks. Unfortunately there is no hook that runs on
> suspend/resume - only on interface up/down. You may want to post to NM list
> asking whether something like this could be implemented (but it will take
> a long time).
>
> Is performing your action on interface down acceptable? If interface stays
> up all the time except during suspend or shutdown it looks like workaround.

Indeed this works for my setup, wifi is always up. Putting a script to
/etc/NetworkManager/dispatcher.d/pre-down.d executes the script succesfully 
before suspend. Thank you for the hint!


>
> >
> >>
> >>> Is there a way to make this work with service files by specifying that 
> >>> the script needs to be run before network is shut down or would I need to 
> >>> run a daemon listening for PrepareForSleep as here: 
> >>> https://github.com/davidn/av/blob/master/av ?
> >>>
> >>
> >> Yes, this is probably the only generic solution.
> >>
> > I tried the script I linked and it does not work either (there seems to be 
> > a race condition that I am losing). What I am not clear about inhibiting is 
> > this: when PrepareForSleep is called and I have a script hooked into it 
> > that inhibits sleep, does it inhibit sleep from finishing or from running 
> > everything that is triggered by it? To me it seems it will just delay going 
> > to sleep, but shutting the network down is not inhibited.
>
> NetworkManage takes sleep lock and subscribes to PrepareForSleep()
> signal, so your daemon races with NetworkManager. There is no way
> to change it - various programs subscribed to PrepareForSleep() are
> not aware of each other at all.
>
> These programs run outside of systemd unit management so you cannot
> use "normal" systemd dependencies to order them.
I see. I assumed systemd ruled it all by now:-). I raised it in their gitlab 
issue tracker: 
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/910

Thanks again for the assistence!
Tomas


Re: [systemd-devel] Launching script that needs network before suspend

2022-01-24 Thread Andrei Borzenkov
On 24.01.2022 17:37, Tomáš Hnyk wrote:
> 
> 
> On Monday 24. January 2022, 13:50:48 (+01:00), Andrei Borzenkov wrote:
> 

I posted it in response to the list and you sent personal reply. Please use
reply to all.

>> On Mon, Jan 24, 2022 at 1:14 AM Tomáš Hnyk  wrote:
>>>
>>> Hello,
>>> I have my computer hooked up to an AVR that runs my home cinema and ideally 
>>> I would like the computer to turn off the AVR when I turn it off or suspend 
>>> it. The only way to do this is over network and I wrote a simple script 
>>> that does just that. Hooking it to shutdown was quite easy using 
>>> network.target that is defined when shutting down.
>>>
>>> I am struggling to make it work with suspend though. When I look at the 
>>> logs, terminating network seems to be the first thing that happens when 
>>> suspend is invoked. I tried putting the script to 
>>> /usr/lib/systemd/system-sleep/ and it runs, but only after network si down, 
>>> so it fails. Running the script with systemd-inhibit 
>>> (ExecStart=/usr/bin/systemd-inhibit --what=sleep my_script) tells me that 
>>> "Failed to inhibit: The operation inhibition has been requested for is 
>>> already running".
>>>
>>
>> What network management program are you using?
> Network Manager. I tried putting "systemctl start NetworkManager.service" 
> into the /usr/lib/systemd/system-sleep/ hook but even though the log says 
> network is reestablished, the script still fails.

NetworkManager has own hooks. Unfortunately there is no hook that runs on
suspend/resume - only on interface up/down. You may want to post to NM list
asking whether something like this could be implemented (but it will take
a long time).

Is performing your action on interface down acceptable? If interface stays
up all the time except during suspend or shutdown it looks like workaround.

> 
>>
>>> Is there a way to make this work with service files by specifying that the 
>>> script needs to be run before network is shut down or would I need to run a 
>>> daemon listening for PrepareForSleep as here: 
>>> https://github.com/davidn/av/blob/master/av ?
>>>
>>
>> Yes, this is probably the only generic solution.
>>
> I tried the script I linked and it does not work either (there seems to be a 
> race condition that I am losing). What I am not clear about inhibiting is 
> this: when PrepareForSleep is called and I have a script hooked into it that 
> inhibits sleep, does it inhibit sleep from finishing or from running 
> everything that is triggered by it? To me it seems it will just delay going 
> to sleep, but shutting the network down is not inhibited.

NetworkManage takes sleep lock and subscribes to PrepareForSleep()
signal, so your daemon races with NetworkManager. There is no way
to change it - various programs subscribed to PrepareForSleep() are
not aware of each other at all.

These programs run outside of systemd unit management so you cannot
use "normal" systemd dependencies to order them.


Re: [systemd-devel] Launching script that needs network before suspend

2022-01-24 Thread Andrei Borzenkov
On Mon, Jan 24, 2022 at 1:14 AM Tomáš Hnyk  wrote:
>
> Hello,
> I have my computer hooked up to an AVR that runs my home cinema and ideally I 
> would like the computer to turn off the AVR when I turn it off or suspend it. 
> The only way to do this is over network and I wrote a simple script that does 
> just that. Hooking it to shutdown was quite easy using network.target that is 
> defined when shutting down.
>
> I am struggling to make it work with suspend though. When I look at the logs, 
> terminating network seems to be the first thing that happens when suspend is 
> invoked. I tried putting the script to /usr/lib/systemd/system-sleep/ and it 
> runs, but only after network si down, so it fails. Running the script with 
> systemd-inhibit (ExecStart=/usr/bin/systemd-inhibit --what=sleep my_script) 
> tells me that "Failed to inhibit: The operation inhibition has been requested 
> for is already running".
>

What network management program are you using?

> Is there a way to make this work with service files by specifying that the 
> script needs to be run before network is shut down or would I need to run a 
> daemon listening for PrepareForSleep as here: 
> https://github.com/davidn/av/blob/master/av ?
>

Yes, this is probably the only generic solution.


[systemd-devel] Launching script that needs network before suspend

2022-01-23 Thread Tomáš Hnyk

Hello,
I have my computer hooked up to an AVR that runs my home cinema and ideally 
I would like the computer to turn off the AVR when I turn it off or suspend 
it. The only way to do this is over network and I wrote a simple script 
that does just that. Hooking it to shutdown was quite easy using 
network.target that is defined when shutting down. 



I am struggling to make it work with suspend though. When I look at the 
logs, terminating network seems to be the first thing that happens when 
suspend is invoked. I tried putting the script to 
/usr/lib/systemd/system-sleep/ and it runs, but only after network si down, 
so it fails. Running the script with systemd-inhibit 
(ExecStart=/usr/bin/systemd-inhibit --what=sleep my_script) tells me that 
"Failed to inhibit: The operation inhibition has been requested for is 
already running".



Is there a way to make this work with service files by specifying that the 
script needs to be run before network is shut down or would I need to run a 
daemon listening for PrepareForSleep as here: 
https://github.com/davidn/av/blob/master/av ?



Kind regards,
Tomas