2
0

systemd.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * systemd socket activation support
  3. *
  4. * Copyright 2017 Red Hat, Inc. and/or its affiliates
  5. *
  6. * Authors:
  7. * Richard W.M. Jones <rjones@redhat.com>
  8. *
  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.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qemu/systemd.h"
  14. #include "qemu/cutils.h"
  15. #include "qemu/error-report.h"
  16. #ifndef _WIN32
  17. unsigned int check_socket_activation(void)
  18. {
  19. const char *s;
  20. unsigned long pid;
  21. unsigned long nr_fds;
  22. unsigned int i;
  23. int fd;
  24. int f;
  25. int err;
  26. s = getenv("LISTEN_PID");
  27. if (s == NULL) {
  28. return 0;
  29. }
  30. err = qemu_strtoul(s, NULL, 10, &pid);
  31. if (err) {
  32. return 0;
  33. }
  34. if (pid != getpid()) {
  35. return 0;
  36. }
  37. s = getenv("LISTEN_FDS");
  38. if (s == NULL) {
  39. return 0;
  40. }
  41. err = qemu_strtoul(s, NULL, 10, &nr_fds);
  42. if (err) {
  43. return 0;
  44. }
  45. assert(nr_fds <= UINT_MAX);
  46. /* So these are not passed to any child processes we might start. */
  47. unsetenv("LISTEN_FDS");
  48. unsetenv("LISTEN_PID");
  49. /* So the file descriptors don't leak into child processes. */
  50. for (i = 0; i < nr_fds; ++i) {
  51. fd = FIRST_SOCKET_ACTIVATION_FD + i;
  52. f = fcntl(fd, F_GETFD);
  53. if (f == -1 || fcntl(fd, F_SETFD, f | FD_CLOEXEC) == -1) {
  54. /* If we cannot set FD_CLOEXEC then it probably means the file
  55. * descriptor is invalid, so socket activation has gone wrong
  56. * and we should exit.
  57. */
  58. error_report("Socket activation failed: "
  59. "invalid file descriptor fd = %d: %s",
  60. fd, g_strerror(errno));
  61. exit(EXIT_FAILURE);
  62. }
  63. }
  64. return (unsigned int) nr_fds;
  65. }
  66. #else /* !_WIN32 */
  67. unsigned int check_socket_activation(void)
  68. {
  69. return 0;
  70. }
  71. #endif