Skip to content

Commit

Permalink
Support systemd socket activation.
Browse files Browse the repository at this point in the history
  • Loading branch information
kr committed Sep 21, 2010
1 parent b02ff81 commit 4dc1586
Show file tree
Hide file tree
Showing 6 changed files with 741 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions beanstalkd.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <limits.h>

#include "net.h"
#include "sd-daemon.h"
#include "util.h"
#include "prot.h"
#include "binlog.h"
Expand Down Expand Up @@ -202,6 +203,14 @@ require_arg(char *opt, char *arg)
return 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)
{
Expand All @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions net.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <errno.h>

#include "net.h"
#include "sd-daemon.h"
#include "util.h"

static int listen_socket = -1;
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 4dc1586

Please sign in to comment.