[systemd-devel] sd_listen_fds: find file descriptor

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Thu Dec 15 11:49:13 PST 2011


On 12/15/2011 07:53 PM, Michael D. Berger wrote:
> [...]
>>
>> Hmm? You should usually consume all fds you get passed. And
>> if you cannot handle more than one, then exit quickly with an
>> error message. And if any of the fds is invalid, then print
>> an error message and leave, too.
>>
>> This is what I'd usually use:
>>
>>
>> int fd, n;
>>
>> n = sd_listen_fds();
>> if (n<  0)  {
>>          fprintf(stderr, "sd_listen_fds(): %s\n", strerror(-n));
>>          exit(1);
>> }
>>
>> for (fd = SD_LISTEN_FDS_START; fd<  SD_LISTEN_FDS_START + n; fd ++) {
>>          if (sd_is_socket_inet(fd, ...)<= 0) {
>>                  fprintf(stderr, "Invalid socket passed.\n");
>>                  exit(1);
>>          }
>>
>>          /* do something with the fd... */ }
>>
>> (This is for the generic case where the service can handle
>> more than one fd. If you can handle ony one, then check
>> whether n == 1 and if it isn't exit saying you can't deal
>> with more than one fd.)
>>
>> Lennart
>>
>
> What you say is clear, but I am perplexed by the fact that
> in a case where there will be one handle, that handle will
> always be 3.  How do we know that 3 is not being used by
> something else?  I thought that the system has to choose
> a handle that is available.
Hi,
"the system", i.e. systemd, controls exactly what file descriptors are 
passed to the child process. In this case "the system" chooses to always 
use 3 for the first socket to pass to the child. It can, because the 
sockets are opened before the child is executed and their numbering is 
controlled by the parent. So even if the child has a library which opens 
a file very early, it will always be later the the time that systemd 
picks it file descriptors.

HTH,
Zbyszek

>
> Mike.
> --
> Michael D. Berger
> m.d.berger at ieee.org
> http://www.rosemike.net/
>
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel
>



More information about the systemd-devel mailing list