Re: [systemd-devel] PAMName=login, systemctl stop

2023-08-04 Thread Tomasz Pala
On 03.07.2023 14:17, Lennart Poettering wrote:

> logind's session ID you'll find in the $XDG_SESSION_ID env var.
>
> you can use this to kill your own session:
>
>   loginctl kill-session $XDG_SESSION_ID
>
> if you want to know which systemd unit your process belongs to use:
>
>   ps --pid $$ -o unit=

I was struggling with the same problem a few months ago and came up with (bear 
up):


ExecStop=/bin/sh -c "loginctl kill-session --signal SIGTERM `systemctl -n1 -o 
export status \"$MAINPID\" | grep _SYSTEMD_SESSION | cut -f2 -d=`"
ExecStopPost=/bin/sh -c "loginctl terminate-session `systemctl -n1 -o 
export status \"$MAINPID\" | grep _SYSTEMD_SESSION | cut -f2 -d=`"


This worked well (I'm not using it though, it was only some test setup),
but I was wondering if systemd couldn't help somehow, e.g. exposing
$_SYSTEMD_SESSION variable directly;

ExecStop=loginctl kill-session --signal SIGTERM $_SYSTEMD_SESSION

doesn't look so scary anymore. Another idea was to ask for new feature:

KillMode=session

which could do exactly this.

-- 
Tomasz Pala 


Re: [systemd-devel] PAMName=login, systemctl stop

2023-07-04 Thread Lennart Poettering
On Mo, 03.07.23 15:21, Andrei Borzenkov (arvidj...@gmail.com) wrote:

> On 03.07.2023 14:17, Lennart Poettering wrote:
> > On Mo, 03.07.23 10:58, Valentijn Sessink (valent...@sessink.nl) wrote:
> >
> > > Now my remaining question is probably so very basic, that you sort of 
> > > missed
> > > it: my "session" is started from a script and so I'm probably able to stop
> > > all services in the session from within this script - if I knew how to 
> > > find
> > > "it": "it" being "the thing to stop", i.e. "my own session" or something
> > > like that. I.e. the script should kill itself and all its children.
> >
> > logind's session ID you'll find in the $XDG_SESSION_ID env var.
> >
> > you can use this to kill your own session:
> >
> >  loginctl kill-session $XDG_SESSION_ID
> >
>
> It is not defined here on Ubuntu 22.04 with GNOME 42 that is using systemd
> for session management. It *is* defined in GDM session worker, but as actual
> session processes are not children of it (they are started by systemd) this
> variable is not inherited.

GNOME doesn#t run in a logind session, but as services off the
user@.service instead, i.e. in session context rather than login
session context. Hence it doesn't have XDG_SESSION_ID set.

This is different if you use PAMName= for arbitrary services. That
means you get a proper session allocated, and hence also
XDG_SESSION_ID set.

Lennart

--
Lennart Poettering, Berlin


Re: [systemd-devel] PAMName=login, systemctl stop

2023-07-03 Thread Andrei Borzenkov

On 03.07.2023 14:17, Lennart Poettering wrote:

On Mo, 03.07.23 10:58, Valentijn Sessink (valent...@sessink.nl) wrote:


Now my remaining question is probably so very basic, that you sort of missed
it: my "session" is started from a script and so I'm probably able to stop
all services in the session from within this script - if I knew how to find
"it": "it" being "the thing to stop", i.e. "my own session" or something
like that. I.e. the script should kill itself and all its children.


logind's session ID you'll find in the $XDG_SESSION_ID env var.

you can use this to kill your own session:

 loginctl kill-session $XDG_SESSION_ID



It is not defined here on Ubuntu 22.04 with GNOME 42 that is using 
systemd for session management. It *is* defined in GDM session worker, 
but as actual session processes are not children of it (they are started 
by systemd) this variable is not inherited.



if you want to know which systemd unit your process belongs to use:

 ps --pid $$ -o unit=

Lennart

--
Lennart Poettering, Berlin




Re: [systemd-devel] PAMName=login, systemctl stop

2023-07-03 Thread Lennart Poettering
On Mo, 03.07.23 10:58, Valentijn Sessink (valent...@sessink.nl) wrote:

> Now my remaining question is probably so very basic, that you sort of missed
> it: my "session" is started from a script and so I'm probably able to stop
> all services in the session from within this script - if I knew how to find
> "it": "it" being "the thing to stop", i.e. "my own session" or something
> like that. I.e. the script should kill itself and all its children.

logind's session ID you'll find in the $XDG_SESSION_ID env var.

you can use this to kill your own session:

loginctl kill-session $XDG_SESSION_ID

if you want to know which systemd unit your process belongs to use:

ps --pid $$ -o unit=

Lennart

--
Lennart Poettering, Berlin


Re: [systemd-devel] PAMName=login, systemctl stop

2023-07-03 Thread Valentijn Sessink

Hi Lennart, hi list,

On 03-07-2023 10:47, Lennart Poettering wrote:

I can get the CGroup and session scope with "systemctl status ";
but I still can't figure out what to stop then. So how can I finish this
"spawned off" session - the one that started from a "PAMName=login" option
in the service file?

I am not sure I grok your question.


From your answer I'd guess you did.


But we do not provide a way
currently to associate session scope units with the service units they
have been created by. There's no nice way to go from one to ther
other.


OK, that's important information.

Now my remaining question is probably so very basic, that you sort of 
missed it: my "session" is started from a script and so I'm probably 
able to stop all services in the session from within this script - if I 
knew how to find "it": "it" being "the thing to stop", i.e. "my own 
session" or something like that. I.e. the script should kill itself and 
all its children.


Would that be possible? I simply don't know how to "find my own session 
and kill it" from within my own session - and I wouldn't know how to 
stop it either.


To rephrase: how should a session commit suicide if it really wants to? ;-)

Does that help? (Or is the answer "no you can't - seek help" ;-)

Best regards,

Valentijn


Re: [systemd-devel] PAMName=login, systemctl stop

2023-07-03 Thread Lennart Poettering
On Fr, 30.06.23 15:11, Valentijn Sessink (valent...@sessink.nl) wrote:

> Hi,
>
> On 28-06-2023 18:04, Lennart Poettering wrote:
> > > "PAMName=login", then starts a script and a few (old X11 related) 
> > > programs.
> [...]>> Is there a way to amend the service file to stop these children?
> > Not really, from systemd's PoV they are part of the login session, not
> > the original service anymore.
>
> I have tried to get my head around this but I couldn't find how to proceed,
> i.e. how to kill of the session.
>
> I can get the CGroup and session scope with "systemctl status ";
> but I still can't figure out what to stop then. So how can I finish this
> "spawned off" session - the one that started from a "PAMName=login" option
> in the service file?

I am not sure I grok your question. But we do not provide a way
currently to associate session scope units with the service units they
have been created by. There's no nice way to go from one to ther
other.

You can terminate service units via "systemctl stop", and you can
terminate session scope units the same way, but there's no mechanism
to relate one to the other. And I am not sure that's even
unconditionally desirable, because typically scopes and the services
they are spawned from might have different lifecycles, i.e. the
sessions are fine with surviving if the invoking service dies.

Lennart

--
Lennart Poettering, Berlin


Re: [systemd-devel] PAMName=login, systemctl stop

2023-06-30 Thread Valentijn Sessink

Hi,

On 28-06-2023 18:04, Lennart Poettering wrote:

"PAMName=login", then starts a script and a few (old X11 related) programs.

[...]>> Is there a way to amend the service file to stop these children?

Not really, from systemd's PoV they are part of the login session, not
the original service anymore.


I have tried to get my head around this but I couldn't find how to 
proceed, i.e. how to kill of the session.


I can get the CGroup and session scope with "systemctl status 
"; but I still can't figure out what to stop then. So how can I 
finish this "spawned off" session - the one that started from a 
"PAMName=login" option in the service file?


Sorry if it's a dumb question. Any links to documentation are fine, too.

Best regards,

Valentijn
--
Durgerdamstraat 29, 1507 JL Zaandam; telefoon 075-7100071


Re: [systemd-devel] PAMName=login, systemctl stop

2023-06-28 Thread Lennart Poettering
On Do, 22.06.23 16:22, Valentijn Sessink (valent...@sessink.nl) wrote:

> Hello list,
>
> I have a rather simple service file that sets up a user session with
> "PAMName=login", then starts a script and a few (old X11 related) programs.
> Starting it is easy, but stopping the service will send SIGTERM only to the
> main process, because "Any child processes of that process will however be
> associated with the session scope unit only" - as per man systemd.exec(5).
>
> Is there a way to amend the service file to stop these children?

Not really, from systemd's PoV they are part of the login session, not
the original service anymore.

You can't have it both ways: be a session and a service. In system
it's either/or.

Lennart

--
Lennart Poettering, Berlin