summaryrefslogtreecommitdiffstats
path: root/ctdb/common/sock_io.c
blob: 81e82c59ca01166d7714bc18bbb25c396c8f14de (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
   Generic Unix-domain Socket I/O

   Copyright (C) Amitay Isaacs  2016

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"
#include "system/filesys.h"
#include "system/network.h"

#include <talloc.h>
#include <tevent.h>

#include "lib/util/sys_rw.h"
#include "lib/util/debug.h"
#include "lib/util/blocking.h"

#include "common/logging.h"
#include "common/sock_io.h"

bool sock_clean(const char *sockpath)
{
	int ret;

	ret = unlink(sockpath);
	if (ret == 0) {
		D_WARNING("Removed stale socket %s\n", sockpath);
	} else if (errno != ENOENT) {
		D_ERR("Failed to remove stale socket %s\n", sockpath);
		return false;
	}

	return true;
}

int sock_connect(const char *sockpath)
{
	struct sockaddr_un addr;
	size_t len;
	int fd, ret;

	if (sockpath == NULL) {
		D_ERR("Invalid socket path\n");
		return -1;
	}

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	len = strlcpy(addr.sun_path, sockpath, sizeof(addr.sun_path));
	if (len >= sizeof(addr.sun_path)) {
		D_ERR("Socket path too long, len=%zu\n", strlen(sockpath));
		return -1;
	}

	fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (fd == -1) {
		D_ERR("socket() failed, errno=%d\n", errno);
		return -1;
	}

	ret = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
	if (ret == -1) {
		D_ERR("connect() failed, errno=%d\n", errno);
		close(fd);
		return -1;
	}

	return fd;
}

struct sock_queue {
	struct tevent_context *ev;
	sock_queue_callback_fn_t callback;
	void *private_data;
	int fd;

	struct tevent_immediate *im;
	struct tevent_queue *queue;
	struct tevent_fd *fde;
	uint8_t *buf;
	size_t buflen, begin, end;
};

/*
 * The reserved talloc headers, SOCK_QUEUE_OBJ_COUNT,
 * and the pre-allocated pool-memory SOCK_QUEUE_POOL_SIZE,
 * are used for the sub-objects queue->im, queue->queue, queue->fde
 * and queue->buf.
 * If the memory allocating sub-objects of struct sock_queue change,
 * those values need to be adjusted.
 */
#define SOCK_QUEUE_OBJ_COUNT 4
#define SOCK_QUEUE_POOL_SIZE 2048

static bool sock_queue_set_fd(struct sock_queue *queue, int fd);
static void sock_queue_handler(struct tevent_context *ev,
			       struct tevent_fd *fde, uint16_t flags,
			       void *private_data);
static void sock_queue_process(struct sock_queue *queue);
static void sock_queue_process_event(struct tevent_context *ev,
				     struct tevent_immediate *im,
				     void *private_data);

struct sock_queue *sock_queue_setup(TALLOC_CTX *mem_ctx,
				    struct tevent_context *ev,
				    int fd,
				    sock_queue_callback_fn_t callback,
				    void *private_data)
{
	struct sock_queue *queue;

	queue = talloc_pooled_object(mem_ctx, struct sock_queue,
				     SOCK_QUEUE_OBJ_COUNT, SOCK_QUEUE_POOL_SIZE);
	if (queue == NULL) {
		return NULL;
	}
	memset(queue, 0, sizeof(struct sock_queue));

	queue->ev = ev;
	queue->callback = callback;
	queue->private_data = private_data;

	queue->im = tevent_create_immediate(queue);
	if (queue->im == NULL) {
		talloc_free(queue);
		return NULL;
	}

	queue->queue = tevent_queue_create(queue, "out-queue");
	if (queue->queue == NULL) {
		talloc_free(queue);
		return NULL;
	}

	if (! sock_queue_set_fd(queue, fd)) {
		talloc_free(queue);
		return NULL;
	}

	return queue;
}

static bool sock_queue_set_fd(struct sock_queue *queue, int fd)
{
	TALLOC_FREE(queue->fde);
	queue->fd = fd;

	if (fd != -1) {
		int ret;

		ret = set_blocking(fd, false);
		if (ret != 0) {
			return false;
		}

		queue->fde = tevent_add_fd(queue->ev, queue, fd,
					   TEVENT_FD_READ,
					   sock_queue_handler, queue);
		if (queue->fde == NULL) {
			return false;
		}
		tevent_fd_set_auto_close(queue->fde);
	}

	return true;
}

static void sock_queue_handler(struct tevent_context *ev,
			       struct tevent_fd *fde, uint16_t flags,
			       void *private_data)
{
	struct sock_queue *queue = talloc_get_type_abort(
		private_data, struct sock_queue);
	int ret, num_ready;
	ssize_t nread;

	ret = ioctl(queue->fd, FIONREAD, &num_ready);
	if (ret != 0) {
		/* Ignore */
		return;
	}

	if (num_ready == 0) {
		/* descriptor has been closed */
		goto fail;
	}

	if ((size_t)num_ready > queue->buflen - queue->end) {
		queue->buf = talloc_realloc_size(queue, queue->buf,
						 queue->end + num_ready);
		if (queue->buf == NULL) {
			goto fail;
		}
		queue->buflen = queue->end + num_ready;
	}

	nread = sys_read(queue->fd, queue->buf + queue->end, num_ready);
	if (nread < 0) {
		goto fail;
	}
	queue->end += nread;

	sock_queue_process(queue);
	return;

fail:
	queue->callback(NULL, 0, queue->private_data);
}

static void sock_queue_process(struct sock_queue *queue)
{
	uint32_t pkt_size;

	if ((queue->end - queue->begin) < sizeof(uint32_t)) {
		/* not enough data */
		return;
	}

	pkt_size = *(uint32_t *)(queue->buf + queue->begin);
	if (pkt_size == 0) {
		D_ERR("Invalid packet of length 0\n");
		queue->callback(NULL, 0, queue->private_data);
		return;
	}

	if ((queue->end - queue->begin) < pkt_size) {
		/* not enough data */
		return;
	}

	queue->callback(queue->buf + queue->begin, pkt_size,
			queue->private_data);
	queue->begin += pkt_size;

	if (queue->begin < queue->end) {
		/* more data to be processed */
		tevent_schedule_immediate(queue->im, queue->ev,
					  sock_queue_process_event, queue);
	} else {
		TALLOC_FREE(queue->buf);
		queue->buflen = 0;
		queue->begin = 0;
		queue->end = 0;
	}
}

static void sock_queue_process_event(struct tevent_context *ev,
				     struct tevent_immediate *im,
				     void *private_data)
{
	struct sock_queue *queue = talloc_get_type_abort(
		private_data, struct sock_queue);

	sock_queue_process(queue);
}

struct sock_queue_write_state {
	uint8_t *pkt;
	uint32_t pkt_size;
};

static void sock_queue_trigger(struct tevent_req *req, void *private_data);

int sock_queue_write(struct sock_queue *queue, uint8_t *buf, size_t buflen)
{
	struct tevent_req *req;
	struct sock_queue_write_state *state;
	struct tevent_queue_entry *qentry;

	if (buflen >= INT32_MAX) {
		return -1;
	}

	req = tevent_req_create(queue, &state, struct sock_queue_write_state);
	if (req == NULL) {
		return -1;
	}

	state->pkt = buf;
	state->pkt_size = (uint32_t)buflen;

	qentry = tevent_queue_add_entry(queue->queue, queue->ev, req,
					sock_queue_trigger, queue);
	if (qentry == NULL) {
		talloc_free(req);
		return -1;
	}

	return 0;
}

static void sock_queue_trigger(struct tevent_req *req, void *private_data)
{
	struct sock_queue *queue = talloc_get_type_abort(
		private_data, struct sock_queue);
	struct sock_queue_write_state *state = tevent_req_data(
		req, struct sock_queue_write_state);
	size_t offset = 0;

	do {
		ssize_t nwritten;

		nwritten = sys_write(queue->fd, state->pkt + offset,
				     state->pkt_size - offset);
		if (nwritten < 0) {
			queue->callback(NULL, 0, queue->private_data);
			return;
		}
		offset += nwritten;

	} while (offset < state->pkt_size);

	tevent_req_done(req);
	talloc_free(req);
}