cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob ad9f06b18c95aba51c3a4a11775aba69a3ea63a4 3298 bytes (raw)
$ git show st-wip-broken:inherit.c	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
 
/*
 * Copyright (C) 2012-2013, Eric Wong <normalperson@yhbt.net>
 * License: GPLv3 or later (see COPYING for details)
 */
#include "cmogstored.h"
static Hash_table *listeners; /* yes, we'll scale to 10K listen sockets, L10K! */

struct listener {
	union mog_sockaddr msa;
	socklen_t len;
	int fd;
};

static bool listener_cmp(const void *a, const void *b)
{
	const struct listener *la = a;
	const struct listener *lb = b;

	return (la->len == lb->len) &&
	       (memcmp(&la->msa, &lb->msa, lb->len) == 0);
}

static size_t listener_hash(const void *x, size_t tablesize)
{
	const struct listener *l = x;
	size_t value = 0;
	socklen_t i;

	for (i = 0; i < l->len; i++)
		value = (value * 31 + l->msa.bytes[i]) % tablesize;

	return value;
}

static void register_listen_fd(int fd)
{
	struct listener tmp;
	struct listener *ins;
	struct mog_ni ni;
	struct mog_packaddr mpa;
	struct sockaddr *sa = &tmp.msa.sa;

	tmp.len = (socklen_t)sizeof(tmp.msa);
	if (getsockname(fd, sa, &tmp.len) != 0)
		die_errno("getsockname(fd=%d) failed", fd);

	if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6)
		die("invalid address family=%d (not AF_INET/AF_INET6)",
		    (int)sa->sa_family);

	mog_packaddr_init(&mpa, &tmp.msa, tmp.len);

	mog_nameinfo(&mpa, &ni);

	syslog(LOG_INFO, "inherited %s%s on fd=%d", ni.ni_host, ni.ni_serv, fd);

	ins = xmalloc(sizeof(*ins));
	*ins = tmp;
	ins->fd = fd;

	switch (hash_insert_if_absent(listeners, ins, NULL)) {
	case 0:
		die("duplicate listener %s%s on fd=%d",
			ni.ni_host, ni.ni_serv, fd);
		break;
	case 1: /* success */
		return;
	default:
		mog_oom();
	}
}

static void listeners_cleanup(void)
{
	if (!listeners)
		return;
	hash_free(listeners);
	listeners = NULL;
}

static bool listener_close_each(void *_l, void *unused)
{
	struct listener *l = _l;

	syslog(LOG_INFO, "closing unused inherited fd=%d", l->fd);
	mog_close(l->fd);
	l->fd = -1;

	return true;
}

/* close all inherited listeners we do not need */
void mog_inherit_cleanup(void)
{
	if (!listeners)
		return;

	hash_do_for_each(listeners, listener_close_each, NULL);
	listeners_cleanup();
}

/* returns the FD belonging to the address if it was inherited */
int mog_inherit_get(struct sockaddr *addr, socklen_t len)
{
	struct listener l;
	struct listener *in;
	int fd = -1;

	if (!listeners)
		return fd;

	l.len = len;
	memcpy(&l.msa.bytes, addr, len);

	in = hash_delete(listeners, &l);
	if (in) {
		fd = in->fd;
		free(in);
		CHECK(int, 0, mog_set_cloexec(fd, true));
	}

	return fd;
}

void mog_inherit_init(void)
{
	char *orig = getenv("CMOGSTORED_FD");
	char *fds;
	char *tip;
	char *end;
	unsigned long fd;
	unsigned endbyte;

	if (orig == NULL)
		return;

	listeners = hash_initialize(3, NULL, listener_hash, listener_cmp, free);
	mog_oom_if_null(listeners);
	atexit(listeners_cleanup);

	fds = xstrdup(orig);
	tip = fds;

	for (;;) {
		errno = 0;
		fd = strtoul(tip, &end, 10);
		if (errno == 0) {
			if (fd > INT_MAX)
				die("inherited fd=%lu is too large", fd);
			register_listen_fd((int)fd);
		} else {
			die_errno("strtuol failed to parse: %s", tip);
		}

		/* the end (or error) */
		endbyte = *end;
		if (endbyte != ',')
			break;

		tip = end + 1; /* more FDs to parse */
	}

	free(fds);

	if (endbyte != '\0')
		die("CMOGSTORED_FD contained invalid byte: %u", endbyte);
}

git clone https://yhbt.net/cmogstored.git