[systemd-devel] Problem with sd_listen_fds(0)

Donat Zenichev donat.zenichev at gmail.com
Thu Jun 27 21:27:23 UTC 2019


Hello Mantas.
You was right, the logic of my code kept itself away from accepting a
connection, when working with the control socket passed by the systemd.
I hardcoded accept() function and this worked out - a command was read.
Thanks for your attention!

чт, 27 июн. 2019 г. в 13:44, Mantas Mikulėnas <grawity at gmail.com>:

> On Thu, Jun 27, 2019 at 1:29 PM Donat Zenichev <donat.zenichev at gmail.com>
> wrote:
>
>> Hi systemd community.
>> Recently I've come accross one interesting problem with API of systemd.
>> The problem is that, I'm not able to read commands comming to the AF_UNIX
>> socket, that was created by systemd and passed to my program using
>> sd_listen_fds(0) function.
>>
>> What actually program requires, is AF_UNIX (AF_LOCAL) socket of type
>> SOCK_STREAM.
>> In case if I create this socket manually, using socket(AF_LOCAL,
>> SOCK_STREAM, 0) function, it works out as expected.
>>
>> But, when I create a socket using systemd socket unit, reading fails with
>> "-1" return value.
>>
>> What I actually do, is that I check if there are some sockets created for
>> me using sd_listen_fds(0), and in case it returns 1 (so 1 socket created),
>> program goes further and sets a file descriptor value to fd =
>> SD_LISTEN_FDS_START + 0;
>> Since the SD_LISTEN_FDS_START is pre-defined to value '3', my file
>> descriptor will always be '3'.
>>
>> But when it comes to reading, with read() function, it fails with the
>> errno EINVAL - Invalid argument.
>> It's not a matter of a other parameters than a file descriptor, since
>> with manual creation of socket it works out as I said.
>>
>> An initializing of the socket descriptor, when creating it by systemd
>> passes as normal, so program starts.
>> But any further command received on fd fails with '-1' return value.
>>
>
> As this is a stream socket, by default the program will receive the
> *listener* socket. Systemd will start your service as soon as someone
> connects, but your program still needs to accept() each connection first.
>
>
> --
> Mantas Mikulėnas
>


-- 
-- 
BR, Donat Zenichev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/systemd-devel/attachments/20190628/308604d4/attachment.html>


More information about the systemd-devel mailing list