Re: [systemd-devel] Stopping services started by Systemd socket

2018-01-23 Thread Lennart Poettering
On Mo, 22.01.18 20:06, Liam Kelly (liamkell...@gmail.com) wrote:

> How does Systemd communicate to socket activated application that
> the connection has been closed? How can I modify my application to
> detect this event if it cannot be configured to be closed
> automatically?

systemd does not. You get the original kernel socket passed and you'll
see EOF/POLLHUP on it when the connection is terminated. systemd won't
signal anything there, and in fact it's entirely up to your app to
exit or stick around after EOF/POLLHUP. 

> We are trying to add network support to legacy code using Systemd
> sockets. Using the 0pointer tutorials, we were able to configure a
> listening TCP port and launch an instance of the application when a
> TCP connection came in. The problem is that when the connection is
> closed, the service is still running.

Just call exit() from your app's sources as soon as you get read()
returning 0 on the socket (which is how the kernel reports EOF
in-line), or poll() returning POLLHUP on it (which is how the kernel
reports EOF out-of-line), depending on your context.

Lennart

-- 
Lennart Poettering, Red Hat
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] Stopping services started by Systemd socket

2018-01-23 Thread Jérémy Rosen

that's not really an answer to your question but...

have you looked at systemd-socket-proxyd ? it's a simple program that is 
meant to be started by a socket and will redirect all traffic to another 
local port.


Properly used it allows a traditional network daemon to be started on 
demand. I won't go in the details, it's pretty well explained in the man 
page.


Depending on why you are converting your software, it could be something 
interesting to look at...


On 23/01/2018 08:18, Mantas Mikulėnas wrote:

On Tue, Jan 23, 2018 at 3:06 AM, Liam Kelly  wrote:


How does Systemd communicate to socket activated application that the
connection has been closed? How can I modify my application to detect this
event if it cannot be configured to be closed automatically?



We are trying to add network support to legacy code using Systemd sockets.
Using the 0pointer tutorials, we were able to configure a listening TCP
port and launch an instance of the application when a TCP connection came
in. The problem is that when the connection is closed, the service is still
running.



The systemct list-units and netstat -tuapn outputs are what you would
expect when the connection is established



 systemctl list-units:

 myapp@5-192.168.0.75:10001-192.168.210.102:19983.service loaded
active running   My App

 netstat -tuapn:

 tcp0  0 192.168.0.75:10001  192.168.210.102:19983
ESTABLISHED 1/init

However, once the client closes the connection, the socket is closed and
removed, but the application is still running as a service:


It is no different from any other type of networked service. Whether you
opened the socket yourself, or received it from a superserver, makes no
difference.

For example, if your program uses poll(), the kernel reports POLLHUP on a
closed socket. If the program uses read() or recv(), 0 bytes indicates that
the socket is closed. If the program uses simple stdio (inetd style), it's
enough to check for EOF on reads from stdin.



___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


--
SMILE 

20 rue des Jardins
92600 Asnières-sur-Seine


*Jérémy ROSEN*
Architecte technique
Responsable de l'expertise Smile-ECS

email jeremy.ro...@smile.fr 
phone +33141402967
url http://www.smile.eu

Twitter  Facebook 
 LinkedIn 
 Github 




Découvrez l’univers Smile, rendez-vous sur smile.eu 



eco Pour la planète, n'imprimez ce mail que si c'est nécessaire
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel


Re: [systemd-devel] Stopping services started by Systemd socket

2018-01-22 Thread Mantas Mikulėnas
On Tue, Jan 23, 2018 at 3:06 AM, Liam Kelly  wrote:

> How does Systemd communicate to socket activated application that the
> connection has been closed? How can I modify my application to detect this
> event if it cannot be configured to be closed automatically?
>
>
>
> We are trying to add network support to legacy code using Systemd sockets.
> Using the 0pointer tutorials, we were able to configure a listening TCP
> port and launch an instance of the application when a TCP connection came
> in. The problem is that when the connection is closed, the service is still
> running.
>
>
>
> The systemct list-units and netstat -tuapn outputs are what you would
> expect when the connection is established
>
>
>
> systemctl list-units:
>
> myapp@5-192.168.0.75:10001-192.168.210.102:19983.service loaded
> active running   My App
>
> netstat -tuapn:
>
> tcp0  0 192.168.0.75:10001  192.168.210.102:19983
> ESTABLISHED 1/init
>
> However, once the client closes the connection, the socket is closed and
> removed, but the application is still running as a service:
>

It is no different from any other type of networked service. Whether you
opened the socket yourself, or received it from a superserver, makes no
difference.

For example, if your program uses poll(), the kernel reports POLLHUP on a
closed socket. If the program uses read() or recv(), 0 bytes indicates that
the socket is closed. If the program uses simple stdio (inetd style), it's
enough to check for EOF on reads from stdin.

-- 
Mantas Mikulėnas
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/systemd-devel