Permalink
Browse files

Support systemd socket activation.

  • Loading branch information...
1 parent b02ff81 commit 4dc15862a73f68a661da32e4b49ae540fcdf98ac @kr committed Sep 21, 2010
Showing with 741 additions and 2 deletions.
  1. +2 −0 Makefile.am
  2. +11 −0 beanstalkd.c
  3. +28 −0 net.c
  4. +435 −0 sd-daemon.c
  5. +261 −0 sd-daemon.h
  6. +4 −2 util.c
View
@@ -14,6 +14,7 @@ aux_sources = \
pq.c \
primes.c \
prot.c \
+ sd-daemon.c \
tube.c \
util.c
beanstalkd_SOURCES = beanstalkd.c $(aux_sources)
@@ -45,6 +46,7 @@ EXTRA_DIST = cut.c $(tests) $(scripts) $(readme) cut.h sh-tests check.sh check-o
primes.h \
prot.h \
stat.h \
+ sd-daemon.h \
tube.h \
util.h
View
@@ -35,6 +35,7 @@
#include <limits.h>
#include "net.h"
+#include "sd-daemon.h"
#include "util.h"
#include "prot.h"
#include "binlog.h"
@@ -203,6 +204,14 @@ require_arg(char *opt, char *arg)
}
static void
+warn_systemd_ignored_option(char *opt, char *arg)
+{
+ if (sd_listen_fds(0) > 0) {
+ warnx("inherited listen fd; ignoring option: %s %s", opt, arg);
+ }
+}
+
+static void
opts(int argc, char **argv)
{
int i;
@@ -216,9 +225,11 @@ opts(int argc, char **argv)
break;
case 'p':
port = require_arg("-p", argv[++i]);
+ warn_systemd_ignored_option("-p", argv[i]);
break;
case 'l':
host_addr = require_arg("-l", argv[++i]);
+ warn_systemd_ignored_option("-l", argv[i]);
break;
case 'z':
job_data_size_limit = parse_size_t(require_arg("-z",
View
@@ -21,6 +21,7 @@
#include <errno.h>
#include "net.h"
+#include "sd-daemon.h"
#include "util.h"
static int listen_socket = -1;
@@ -36,6 +37,33 @@ make_server_socket(char *host, char *port)
struct linger linger = {0, 0};
struct addrinfo *airoot, *ai, hints;
+ /* See if we got a listen fd from systemd. If so, all socket options etc
+ * are already set, so we check that the fd is a TCP listen socket and
+ * return. */
+ r = sd_listen_fds(1);
+ if (r < 0) {
+ return twarn("sd_listen_fds"), -1;
+ }
+ if (r > 0) {
+ if (r > 1) {
+ twarnx("inherited more than one listen socket;"
+ " ignoring all but the first");
+ r = 1;
+ }
+ fd = SD_LISTEN_FDS_START;
+ r = sd_is_socket_inet(fd, 0, SOCK_STREAM, 1, 0);
+ if (r < 0) {
+ errno = -r;
+ twarn("sd_is_socket_inet");
+ return -1;
+ }
+ if (!r) {
+ twarnx("inherited fd is not a TCP listen socket");
+ return -1;
+ }
+ return listen_socket = fd;
+ }
+
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
Oops, something went wrong.

0 comments on commit 4dc1586

Please sign in to comment.