systemd_support.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (C) 2011 Davide Cavalca <davide@geexbox.org>
  3. *
  4. * Based on http://cgit.freedesktop.org/systemd/tree/src/sd-daemon.c
  5. * Copyright 2010 Lennart Poettering
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation files
  9. * (the "Software"), to deal in the Software without restriction,
  10. * including without limitation the rights to use, copy, modify, merge,
  11. * publish, distribute, sublicense, and/or sell copies of the Software,
  12. * and to permit persons to whom the Software is furnished to do so,
  13. * subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  22. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  23. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. */
  27. #include "libbb.h"
  28. //config:config FEATURE_SYSTEMD
  29. //config: bool "Enable systemd support"
  30. //config: default y
  31. //config: help
  32. //config: If you plan to use busybox daemons on a system where daemons
  33. //config: are controlled by systemd, enable this option.
  34. //config: If you don't use systemd, it is still safe to enable it,
  35. //config: but the downside is increased code size.
  36. //kbuild:lib-$(CONFIG_FEATURE_SYSTEMD) += systemd_support.o
  37. int sd_listen_fds(void)
  38. {
  39. const char *e;
  40. int n;
  41. int fd;
  42. e = getenv("LISTEN_PID");
  43. if (!e)
  44. return 0;
  45. n = xatoi_positive(e);
  46. /* Is this for us? */
  47. if (getpid() != (pid_t) n)
  48. return 0;
  49. e = getenv("LISTEN_FDS");
  50. if (!e)
  51. return 0;
  52. n = xatoi_positive(e);
  53. for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++)
  54. close_on_exec_on(fd);
  55. return n;
  56. }