sd-daemon.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
  2. #ifndef SNMPD_SD_DAEMON_H
  3. #define SNMPD_SD_DAEMON_H
  4. /***
  5. Copyright 2010 Lennart Poettering
  6. Permission is hereby granted, free of charge, to any person
  7. obtaining a copy of this software and associated documentation files
  8. (the "Software"), to deal in the Software without restriction,
  9. including without limitation the rights to use, copy, modify, merge,
  10. publish, distribute, sublicense, and/or sell copies of the Software,
  11. and to permit persons to whom the Software is furnished to do so,
  12. subject to the following conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. SOFTWARE.
  23. ***/
  24. #ifdef HAVE_SYS_TYPES_H
  25. #include <sys/types.h>
  26. #endif
  27. #ifdef HAVE_INTTYPES_H
  28. #include <inttypes.h>
  29. #endif
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /*
  34. Reference implementation of a few systemd related interfaces for
  35. writing daemons. These interfaces are trivial to implement. To
  36. simplify porting we provide this reference implementation.
  37. Applications are welcome to reimplement the algorithms described
  38. here if they do not want to include these two source files.
  39. The following functionality is provided:
  40. - Support for logging with log levels on stderr
  41. - File descriptor passing for socket-based activation
  42. - Daemon startup and status notification
  43. - Detection of systemd boots
  44. You may compile this with -DDISABLE_SYSTEMD to disable systemd
  45. support. This makes all those calls NOPs that are directly related to
  46. systemd (i.e. only sd_is_xxx() will stay useful).
  47. Since this is drop-in code we don't want any of our symbols to be
  48. exported in any case. Hence we declare hidden visibility for all of
  49. them.
  50. You may find an up-to-date version of these source files online:
  51. http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.h
  52. http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.c
  53. This should compile on non-Linux systems, too, but with the
  54. exception of the sd_is_xxx() calls all functions will become NOPs.
  55. See sd-daemon(7) for more information.
  56. */
  57. /*
  58. Returns how many file descriptors have been passed, or a negative
  59. errno code on failure. Optionally, removes the $LISTEN_FDS and
  60. $LISTEN_PID file descriptors from the environment (recommended, but
  61. problematic in threaded environments). If r is the return value of
  62. this function you'll find the file descriptors passed as fds
  63. SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative
  64. errno style error code on failure. This function call ensures that
  65. the FD_CLOEXEC flag is set for the passed file descriptors, to make
  66. sure they are not passed on to child processes. If FD_CLOEXEC shall
  67. not be set, the caller needs to unset it after this call for all file
  68. descriptors that are used.
  69. See sd_listen_fds(3) for more information.
  70. */
  71. int netsnmp_sd_listen_fds(int unset_environment);
  72. /*
  73. Informs systemd about changed daemon state. This takes a number of
  74. newline separated environment-style variable assignments in a
  75. string. The following variables are known:
  76. READY=1 Tells systemd that daemon startup is finished (only
  77. relevant for services of Type=notify). The passed
  78. argument is a boolean "1" or "0". Since there is
  79. little value in signaling non-readiness the only
  80. value daemons should send is "READY=1".
  81. STATUS=... Passes a single-line status string back to systemd
  82. that describes the daemon state. This is free-from
  83. and can be used for various purposes: general state
  84. feedback, fsck-like programs could pass completion
  85. percentages and failing programs could pass a human
  86. readable error message. Example: "STATUS=Completed
  87. 66% of file system check..."
  88. ERRNO=... If a daemon fails, the errno-style error code,
  89. formatted as string. Example: "ERRNO=2" for ENOENT.
  90. BUSERROR=... If a daemon fails, the D-Bus error-style error
  91. code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut"
  92. MAINPID=... The main pid of a daemon, in case systemd did not
  93. fork off the process itself. Example: "MAINPID=4711"
  94. Daemons can choose to send additional variables. However, it is
  95. recommended to prefix variable names not listed above with X_.
  96. Returns a negative errno-style error code on failure. Returns > 0
  97. if systemd could be notified, 0 if it couldn't possibly because
  98. systemd is not running.
  99. Example: When a daemon finished starting up, it could issue this
  100. call to notify systemd about it:
  101. sd_notify(0, "READY=1");
  102. See sd_notifyf() for more complete examples.
  103. See sd_notify(3) for more information.
  104. */
  105. int netsnmp_sd_notify(int unset_environment, const char *state);
  106. /**
  107. * Find an socket with given parameters. See man sd_is_socket_inet for
  108. * description of the arguments.
  109. *
  110. * Returns the file descriptor if it is found, 0 otherwise.
  111. */
  112. int netsnmp_sd_find_inet_socket(int family, int type, int listening, int port);
  113. /**
  114. * Find an unix socket with given parameters. See man sd_is_socket_unix for
  115. * description of the arguments.
  116. *
  117. * Returns the file descriptor if it is found, 0 otherwise.
  118. */
  119. int
  120. netsnmp_sd_find_unix_socket(int type, int listening, const char *path);
  121. #ifdef __cplusplus
  122. }
  123. #endif
  124. #endif /* SNMPD_SD_DAEMON_H */