Guest User

Untitled

a guest
Aug 13th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.94 KB | None | 0 0
  1. diff -r b8803a1e2dc2 -r f36a78324ec2 Makefile.am
  2. --- a/Makefile.am Sat Oct 16 18:33:42 2010 +0100
  3. +++ b/Makefile.am Wed Oct 20 12:53:52 2010 +0200
  4. @@ -49,6 +49,15 @@
  5. -e "s|^\(dovecot_moduledir\)=|\1=$(moduledir)|" \
  6. > dovecot-config
  7.  
  8. +if HAVE_SYSTEMD
  9. +%.service: %.service.in
  10. + $(AM_V_GEN)sed -e 's,@sbindir\@,$(sbindir),g' $< > $@
  11. +
  12. +systemdsystemunit_DATA = \
  13. + dovecot.socket \
  14. + dovecot.service
  15. +endif
  16. +
  17. install-exec-hook:
  18. rm $(DESTDIR)$(pkglibdir)/dovecot-config && \
  19. grep -v '^LIBDOVECOT_.*_INCLUDE' dovecot-config | \
  20. @@ -62,6 +71,9 @@
  21. > $(DESTDIR)$(pkglibdir)/dovecot-config
  22.  
  23. CLEANFILES = $(datafiles)
  24. +if HAVE_SYSTEMD
  25. +CLEANFILES += $systedmsystemunit_DATA
  26. +endif
  27.  
  28. DISTCLEANFILES = $(top_builddir)/dovecot-version.h
  29.  
  30. diff -r b8803a1e2dc2 -r f36a78324ec2 configure.in
  31. --- a/configure.in Sat Oct 16 18:33:42 2010 +0100
  32. +++ b/configure.in Wed Oct 20 12:53:52 2010 +0200
  33. @@ -2628,6 +2628,16 @@
  34. AC_SUBST(RUN_TEST)
  35. AC_SUBST(abs_top_builddir)
  36.  
  37. +PKG_PROG_PKG_CONFIG
  38. +AC_ARG_WITH([systemdsystemunitdir],
  39. + AS_HELP_STRING([--with-systemdsystemunitdir=DIR], [Directory for systemd service files]),
  40. + [], [with_systemdsystemunitdir=$($PKG_CONFIG --variable=systemdsystemunitdir systemd)])
  41. +if test "x$with_systemdsystemunitdir" != xno; then
  42. + AC_SUBST([systemdsystemunitdir], [$with_systemdsystemunitdir])
  43. + AC_DEFINE(HAVE_SYSTEMD,, Define if you want to use systemd socket activation)
  44. +fi
  45. +AM_CONDITIONAL(HAVE_SYSTEMD, [test -n "$with_systemdsystemunitdir" -a "x$with_systemdsystemunitdir" != xno ])
  46. +
  47. AC_CONFIG_HEADERS([config.h])
  48. AC_CONFIG_FILES([
  49. Makefile
  50. diff -r b8803a1e2dc2 -r f36a78324ec2 dovecot.service.in
  51. --- /dev/null Thu Jan 01 00:00:00 1970 +0000
  52. +++ b/dovecot.service.in Wed Oct 20 12:53:52 2010 +0200
  53. @@ -0,0 +1,8 @@
  54. +[Unit]
  55. +Description=Dovecot IMAP/POP3 email server
  56. +After=local-fs.target network.target
  57. +
  58. +[Service]
  59. +Type=simple
  60. +ExecStart=@sbindir@/dovecot -F
  61. +NonBlocking=yes
  62. diff -r b8803a1e2dc2 -r f36a78324ec2 dovecot.socket
  63. --- /dev/null Thu Jan 01 00:00:00 1970 +0000
  64. +++ b/dovecot.socket Wed Oct 20 12:53:52 2010 +0200
  65. @@ -0,0 +1,15 @@
  66. +[Unit]
  67. +Description=Dovecot IMAP/POP3 email server activation socket
  68. +
  69. +[Socket]
  70. +#dovecot expects separate IPv4 and IPv6 sockets
  71. +BindIPv6Only=ipv6-only
  72. +ListenStream=0.0.0.0:143
  73. +ListenStream=[::]:143
  74. +ListenStream=0.0.0.0:993
  75. +ListenStream=[::]:993
  76. +KeepAlive=true
  77. +
  78. +[Install]
  79. +WantedBy=sockets.target
  80. +
  81. diff -r b8803a1e2dc2 -r f36a78324ec2 src/lib-master/master-service.c
  82. --- a/src/lib-master/master-service.c Sat Oct 16 18:33:42 2010 +0100
  83. +++ b/src/lib-master/master-service.c Wed Oct 20 12:53:52 2010 +0200
  84. @@ -397,6 +397,9 @@
  85. #ifdef DEBUG
  86. bool gdb = getenv("GDB") != NULL;
  87. #endif
  88. +#ifdef HAVE_SYSTEMD
  89. + const char *env_listen_pid, *env_listen_fds;
  90. +#endif
  91.  
  92. user = getenv("USER");
  93. if (user != NULL)
  94. @@ -407,6 +410,17 @@
  95. home = preserve_home ? getenv("HOME") : NULL;
  96. if (home != NULL)
  97. home = t_strconcat("HOME=", home, NULL);
  98. +#ifdef HAVE_SYSTEMD
  99. + /* save LISTEN_PID and LISTEN_FDS since systemd socket activation
  100. + * needs that to work
  101. + */
  102. + env_listen_pid = getenv("LISTEN_PID");
  103. + if (env_listen_pid != NULL)
  104. + env_listen_pid = t_strconcat("LISTEN_PID=", env_listen_pid, NULL);
  105. + env_listen_fds = getenv("LISTEN_FDS");
  106. + if (env_listen_fds != NULL)
  107. + env_listen_fds = t_strconcat("LISTEN_FDS=", env_listen_fds, NULL);
  108. +#endif
  109.  
  110. /* Note that if the original environment was set with env_put(), the
  111. environment strings will be invalid after env_clean(). That's why
  112. @@ -419,6 +433,10 @@
  113. #ifdef DEBUG
  114. if (gdb) env_put("GDB=1");
  115. #endif
  116. +#ifdef HAVE_SYSTEMD
  117. + if (env_listen_pid != NULL) env_put(env_listen_pid);
  118. + if (env_listen_fds != NULL) env_put(env_listen_fds);
  119. +#endif
  120. }
  121.  
  122. void master_service_set_client_limit(struct master_service *service,
  123. diff -r b8803a1e2dc2 -r f36a78324ec2 src/master/Makefile.am
  124. --- a/src/master/Makefile.am Sat Oct 16 18:33:42 2010 +0100
  125. +++ b/src/master/Makefile.am Wed Oct 20 12:53:52 2010 +0200
  126. @@ -41,3 +41,10 @@
  127. service-process.h \
  128. service-process-notify.h \
  129. service.h
  130. +
  131. +HAVE_SYSTEMD=1
  132. +if HAVE_SYSTEMD
  133. +dovecot_SOURCES += sd-daemon.c
  134. +noinst_HEADERS += sd-daemon.h
  135. +endif
  136. +
  137. diff -r b8803a1e2dc2 -r f36a78324ec2 src/master/main.c
  138. --- a/src/master/main.c Sat Oct 16 18:33:42 2010 +0100
  139. +++ b/src/master/main.c Wed Oct 20 12:53:52 2010 +0200
  140. @@ -601,6 +601,9 @@
  141. struct master_settings *set;
  142. unsigned int child_process_env_idx = 0;
  143. const char *error, *env_tz, *doveconf_arg = NULL;
  144. +#ifdef HAVE_SYSTEMD
  145. + const char *env_listen_pid, *env_listen_fds;
  146. +#endif
  147. failure_callback_t *orig_info_callback, *orig_debug_callback;
  148. bool foreground = FALSE, ask_key_pass = FALSE;
  149. bool doubleopts[argc];
  150. @@ -732,6 +735,14 @@
  151. correctly. */
  152. env_tz = getenv("TZ");
  153.  
  154. +#ifdef HAVE_SYSTEMD
  155. + /* save LISTEN_PID and LISTEN_FDS since systemd socket activation
  156. + * needs that to work
  157. + */
  158. + env_listen_pid = getenv("LISTEN_PID");
  159. + env_listen_fds = getenv("LISTEN_FDS");
  160. +#endif
  161. +
  162. /* clean up the environment of everything */
  163. env_clean();
  164.  
  165. @@ -742,6 +753,20 @@
  166. env_put(env);
  167. child_process_env[child_process_env_idx++] = env;
  168. }
  169. +
  170. +#ifdef HAVE_SYSTEMD
  171. + /* put back systemd environment */
  172. + if (env_listen_pid != NULL) {
  173. + const char *env = t_strconcat("LISTEN_PID=", env_listen_pid, NULL);
  174. +
  175. + env_put(env);
  176. + }
  177. + if (env_listen_fds != NULL) {
  178. + const char *env = t_strconcat("LISTEN_FDS=", env_listen_fds, NULL);
  179. +
  180. + env_put(env);
  181. + }
  182. +#endif
  183. i_assert(child_process_env_idx <
  184. sizeof(child_process_env) / sizeof(child_process_env[0]));
  185. child_process_env[child_process_env_idx] = NULL;
  186. diff -r b8803a1e2dc2 -r f36a78324ec2 src/master/service-listen.c
  187. --- a/src/master/service-listen.c Sat Oct 16 18:33:42 2010 +0100
  188. +++ b/src/master/service-listen.c Wed Oct 20 12:53:52 2010 +0200
  189. @@ -2,9 +2,11 @@
  190.  
  191. #include "common.h"
  192. #include "array.h"
  193. +#include "env-util.h"
  194. #include "fd-set-nonblock.h"
  195. #include "fd-close-on-exec.h"
  196. #include "network.h"
  197. +#include "sd-daemon.h"
  198. #include "service.h"
  199. #include "service-listen.h"
  200.  
  201. @@ -148,19 +150,56 @@
  202. const struct inet_listener_settings *set = l->set.inetset.set;
  203. unsigned int port = set->port;
  204. int fd;
  205. + int n;
  206.  
  207. - fd = net_listen(&l->set.inetset.ip, &port,
  208. - service_get_backlog(service));
  209. - if (fd < 0) {
  210. - service_error(service, "listen(%s, %u) failed: %m",
  211. - l->inet_address, set->port);
  212. - return errno == EADDRINUSE ? 0 : -1;
  213. + /* We unfortunately have to resort to this hack to workaround
  214. + * sd-listen-daemon.c PID check since dovecot binary starts by
  215. + * using execv to run doveconf, which then spawn again dovecot
  216. + * and this is this dovecot instance that will call this code,
  217. + * so it will no longer match the PID that systemd expects
  218. + */
  219. + if (getenv("LISTEN_PID") != NULL) {
  220. + const char *env = t_strdup_printf("LISTEN_PID=%d", getpid());
  221. + env_put(env);
  222. }
  223. - net_set_nonblock(fd, TRUE);
  224. - fd_close_on_exec(fd, TRUE);
  225. + n = sd_listen_fds(0);
  226. + if (n < 0) {
  227. + errno = -n;
  228. + goto failure;
  229. + } else if (n > 0) {
  230. + for (fd = SD_LISTEN_FDS_START;
  231. + fd <= SD_LISTEN_FDS_START + n - 1;
  232. + fd++) {
  233. + if (sd_is_socket_inet(fd, l->set.inetset.ip.family,
  234. + SOCK_STREAM, 1, port))
  235. + break;
  236. + }
  237. + if (fd > SD_LISTEN_FDS_START + n - 1) {
  238. + /* when systemd didn't provide a usable socket,
  239. + * fall back to the regular socket creation code
  240. + */
  241. + fd = -1;
  242. + }
  243. + } else {
  244. + fd = -1;
  245. + }
  246. +
  247. + if (fd == -1) {
  248. + fd = net_listen(&l->set.inetset.ip, &port,
  249. + service_get_backlog(service));
  250. + if (fd < 0)
  251. + goto failure;
  252. + net_set_nonblock(fd, TRUE);
  253. + fd_close_on_exec(fd, TRUE);
  254. + }
  255.  
  256. l->fd = fd;
  257. return 1;
  258. +
  259. +failure:
  260. + service_error(service, "listen(%s, %u) failed: %m",
  261. + l->inet_address, set->port);
  262. + return errno == EADDRINUSE ? 0 : -1;
  263. }
  264.  
  265. static int service_listen(struct service *service)
Add Comment
Please, Sign In to add comment