2 * systemd socket activation support
4 * Copyright 2017 Red Hat, Inc. and/or its affiliates
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)
29 s = getenv("LISTEN_PID");
33 err = qemu_strtoul(s, NULL, 10, &pid);
37 if (pid != getpid()) {
41 s = getenv("LISTEN_FDS");
45 err = qemu_strtoul(s, NULL, 10, &nr_fds);
49 assert(nr_fds <= UINT_MAX);
51 /* So these are not passed to any child processes we might start. */
52 unsetenv("LISTEN_FDS");
53 unsetenv("LISTEN_PID");
55 /* So the file descriptors don't leak into child processes. */
56 for (i = 0; i < nr_fds; ++i) {
57 fd = FIRST_SOCKET_ACTIVATION_FD + i;
58 f = fcntl(fd, F_GETFD);
59 if (f == -1 || fcntl(fd, F_SETFD, f | FD_CLOEXEC) == -1) {
60 /* If we cannot set FD_CLOEXEC then it probably means the file
61 * descriptor is invalid, so socket activation has gone wrong
64 error_report("Socket activation failed: "
65 "invalid file descriptor fd = %d: %s",
66 fd, g_strerror(errno));
71 return (unsigned int) nr_fds;
75 unsigned int check_socket_activation(void)