2 * systemd socket activation support
4 * Copyright 2017 Red Hat, Inc. and/or its affiliates
7 * Richard W.M. Jones <rjones@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu/systemd.h"
15 #include "qemu/cutils.h"
16 #include "qemu/error-report.h"
19 unsigned int check_socket_activation(void)
28 s
= getenv("LISTEN_PID");
32 err
= qemu_strtoul(s
, NULL
, 10, &pid
);
36 if (pid
!= getpid()) {
40 s
= getenv("LISTEN_FDS");
44 err
= qemu_strtoul(s
, NULL
, 10, &nr_fds
);
48 assert(nr_fds
<= UINT_MAX
);
50 /* So these are not passed to any child processes we might start. */
51 unsetenv("LISTEN_FDS");
52 unsetenv("LISTEN_PID");
54 /* So the file descriptors don't leak into child processes. */
55 for (i
= 0; i
< nr_fds
; ++i
) {
56 fd
= FIRST_SOCKET_ACTIVATION_FD
+ i
;
57 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
) == -1) {
58 /* If we cannot set FD_CLOEXEC then it probably means the file
59 * descriptor is invalid, so socket activation has gone wrong
62 error_report("Socket activation failed: "
63 "invalid file descriptor fd = %d: %m",
69 return (unsigned int) nr_fds
;
73 unsigned int check_socket_activation(void)