3 Utility functions to read/write blobs of data from a file descriptor
4 and handle the case where we might need multiple read/writes to get all the
7 Copyright (C) Andrew Tridgell 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "system/network.h"
25 #include "system/filesys.h"
31 #include "lib/util/dlinklist.h"
32 #include "lib/util/debug.h"
33 #include "lib/util/sys_rw.h"
35 #include "ctdb_private.h"
36 #include "ctdb_client.h"
38 #include "common/logging.h"
39 #include "common/common.h"
41 /* structures for packet queueing - see common/ctdb_io.c */
49 struct ctdb_queue_pkt {
50 struct ctdb_queue_pkt *next, *prev;
58 struct ctdb_context *ctdb;
59 struct tevent_immediate *im;
60 struct ctdb_buffer buffer; /* input buffer */
61 struct ctdb_queue_pkt *out_queue, *out_queue_tail;
62 uint32_t out_queue_length;
63 struct tevent_fd *fde;
67 ctdb_queue_cb_fn_t callback;
74 int ctdb_queue_length(struct ctdb_queue *queue)
76 return queue->out_queue_length;
79 static void queue_process(struct ctdb_queue *queue);
81 static void queue_process_event(struct tevent_context *ev, struct tevent_immediate *im,
84 struct ctdb_queue *queue = talloc_get_type(private_data, struct ctdb_queue);
90 * This function is used to process data in queue buffer.
92 * Queue callback function can end up freeing the queue, there should not be a
93 * loop processing packets from queue buffer. Instead set up a timed event for
94 * immediate run to process remaining packets from buffer.
96 static void queue_process(struct ctdb_queue *queue)
101 if (queue->buffer.length < sizeof(pkt_size)) {
105 pkt_size = *(uint32_t *)queue->buffer.data;
107 DEBUG(DEBUG_CRIT, ("Invalid packet of length 0\n"));
111 if (queue->buffer.length < pkt_size) {
112 if (pkt_size > queue->buffer_size) {
113 queue->buffer.extend = pkt_size;
118 /* Extract complete packet */
119 data = talloc_memdup(queue, queue->buffer.data, pkt_size);
121 D_ERR("read error alloc failed for %u\n", pkt_size);
125 /* Shift packet out from buffer */
126 if (queue->buffer.length > pkt_size) {
127 memmove(queue->buffer.data,
128 queue->buffer.data + pkt_size,
129 queue->buffer.length - pkt_size);
131 queue->buffer.length -= pkt_size;
133 if (queue->buffer.length > 0) {
134 /* There is more data to be processed, schedule an event */
135 tevent_schedule_immediate(queue->im, queue->ctdb->ev,
136 queue_process_event, queue);
138 if (queue->buffer.size > queue->buffer_size) {
139 TALLOC_FREE(queue->buffer.data);
140 queue->buffer.size = 0;
144 /* It is the responsibility of the callback to free 'data' */
145 queue->callback(data, pkt_size, queue->private_data);
149 queue->callback(NULL, 0, queue->private_data);
155 called when an incoming connection is readable
156 This function MUST be safe for reentry via the queue callback!
158 static void queue_io_read(struct ctdb_queue *queue)
164 /* check how much data is available on the socket for immediately
165 guaranteed nonblocking access.
166 as long as we are careful never to try to read more than this
167 we know all reads will be successful and will neither block
168 nor fail with a "data not available right now" error
170 if (ioctl(queue->fd, FIONREAD, &num_ready) != 0) {
173 if (num_ready == 0) {
174 /* the descriptor has been closed */
178 if (queue->buffer.data == NULL) {
179 /* starting fresh, allocate buf to read data */
180 queue->buffer.data = talloc_size(queue, queue->buffer_size);
181 if (queue->buffer.data == NULL) {
182 DEBUG(DEBUG_ERR, ("read error alloc failed for %u\n", num_ready));
185 queue->buffer.size = queue->buffer_size;
186 } else if (queue->buffer.extend > 0) {
187 /* extending buffer */
188 data = talloc_realloc_size(queue, queue->buffer.data, queue->buffer.extend);
190 DEBUG(DEBUG_ERR, ("read error realloc failed for %u\n", queue->buffer.extend));
193 queue->buffer.data = data;
194 queue->buffer.size = queue->buffer.extend;
195 queue->buffer.extend = 0;
198 num_ready = MIN(num_ready, queue->buffer.size - queue->buffer.length);
201 nread = sys_read(queue->fd,
202 queue->buffer.data + queue->buffer.length,
205 DEBUG(DEBUG_ERR, ("read error nread=%d\n", (int)nread));
208 queue->buffer.length += nread;
211 queue_process(queue);
215 queue->callback(NULL, 0, queue->private_data);
219 /* used when an event triggers a dead queue */
220 static void queue_dead(struct tevent_context *ev, struct tevent_immediate *im,
223 struct ctdb_queue *queue = talloc_get_type(private_data, struct ctdb_queue);
224 queue->callback(NULL, 0, queue->private_data);
229 called when an incoming connection is writeable
231 static void queue_io_write(struct ctdb_queue *queue)
233 while (queue->out_queue) {
234 struct ctdb_queue_pkt *pkt = queue->out_queue;
236 if (queue->ctdb->flags & CTDB_FLAG_TORTURE) {
237 n = write(queue->fd, pkt->data, 1);
239 n = write(queue->fd, pkt->data, pkt->length);
242 if (n == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
243 if (pkt->length != pkt->full_length) {
244 /* partial packet sent - we have to drop it */
245 DLIST_REMOVE(queue->out_queue, pkt);
246 queue->out_queue_length--;
249 talloc_free(queue->fde);
252 tevent_schedule_immediate(queue->im, queue->ctdb->ev,
258 if (n != pkt->length) {
264 DLIST_REMOVE(queue->out_queue, pkt);
265 queue->out_queue_length--;
269 TEVENT_FD_NOT_WRITEABLE(queue->fde);
273 called when an incoming connection is readable or writeable
275 static void queue_io_handler(struct tevent_context *ev, struct tevent_fd *fde,
276 uint16_t flags, void *private_data)
278 struct ctdb_queue *queue = talloc_get_type(private_data, struct ctdb_queue);
280 if (flags & TEVENT_FD_READ) {
281 queue_io_read(queue);
283 queue_io_write(queue);
289 queue a packet for sending
291 int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length)
293 struct ctdb_req_header *hdr = (struct ctdb_req_header *)data;
294 struct ctdb_queue_pkt *pkt;
295 uint32_t length2, full_length;
297 /* If the queue does not have valid fd, no point queueing a packet */
298 if (queue->fd == -1) {
302 if (queue->alignment) {
303 /* enforce the length and alignment rules from the tcp packet allocator */
304 length2 = (length+(queue->alignment-1)) & ~(queue->alignment-1);
305 *(uint32_t *)data = length2;
310 if (length2 != length) {
311 memset(data+length, 0, length2-length);
314 full_length = length2;
316 /* if the queue is empty then try an immediate write, avoiding
317 queue overhead. This relies on non-blocking sockets */
318 if (queue->out_queue == NULL && queue->fd != -1 &&
319 !(queue->ctdb->flags & CTDB_FLAG_TORTURE)) {
320 ssize_t n = write(queue->fd, data, length2);
321 if (n == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
322 talloc_free(queue->fde);
325 tevent_schedule_immediate(queue->im, queue->ctdb->ev,
327 /* yes, we report success, as the dead node is
328 handled via a separate event */
335 if (length2 == 0) return 0;
339 queue, offsetof(struct ctdb_queue_pkt, buf) + length2);
340 CTDB_NO_MEMORY(queue->ctdb, pkt);
341 talloc_set_name_const(pkt, "struct ctdb_queue_pkt");
343 pkt->data = pkt->buf;
344 memcpy(pkt->data, data, length2);
346 pkt->length = length2;
347 pkt->full_length = full_length;
349 if (queue->out_queue == NULL && queue->fd != -1) {
350 TEVENT_FD_WRITEABLE(queue->fde);
353 DLIST_ADD_END(queue->out_queue, pkt);
355 queue->out_queue_length++;
357 if (queue->ctdb->tunable.verbose_memory_names != 0) {
358 switch (hdr->operation) {
359 case CTDB_REQ_CONTROL: {
360 struct ctdb_req_control_old *c = (struct ctdb_req_control_old *)hdr;
361 talloc_set_name(pkt, "ctdb_queue_pkt: %s control opcode=%u srvid=%llu datalen=%u",
362 queue->name, (unsigned)c->opcode, (unsigned long long)c->srvid, (unsigned)c->datalen);
365 case CTDB_REQ_MESSAGE: {
366 struct ctdb_req_message_old *m = (struct ctdb_req_message_old *)hdr;
367 talloc_set_name(pkt, "ctdb_queue_pkt: %s message srvid=%llu datalen=%u",
368 queue->name, (unsigned long long)m->srvid, (unsigned)m->datalen);
372 talloc_set_name(pkt, "ctdb_queue_pkt: %s operation=%u length=%u src=%u dest=%u",
373 queue->name, (unsigned)hdr->operation, (unsigned)hdr->length,
374 (unsigned)hdr->srcnode, (unsigned)hdr->destnode);
384 setup the fd used by the queue
386 int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd)
389 talloc_free(queue->fde);
393 queue->fde = tevent_add_fd(queue->ctdb->ev, queue, fd,
395 queue_io_handler, queue);
396 if (queue->fde == NULL) {
399 tevent_fd_set_auto_close(queue->fde);
401 if (queue->out_queue) {
402 TEVENT_FD_WRITEABLE(queue->fde);
410 setup a packet queue on a socket
412 struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb,
413 TALLOC_CTX *mem_ctx, int fd, int alignment,
414 ctdb_queue_cb_fn_t callback,
415 void *private_data, const char *fmt, ...)
417 struct ctdb_queue *queue;
420 queue = talloc_zero(mem_ctx, struct ctdb_queue);
421 CTDB_NO_MEMORY_NULL(ctdb, queue);
423 queue->name = talloc_vasprintf(mem_ctx, fmt, ap);
425 CTDB_NO_MEMORY_NULL(ctdb, queue->name);
427 queue->im= tevent_create_immediate(queue);
428 CTDB_NO_MEMORY_NULL(ctdb, queue->im);
432 queue->alignment = alignment;
433 queue->private_data = private_data;
434 queue->callback = callback;
436 if (ctdb_queue_set_fd(queue, fd) != 0) {
442 queue->buffer_size = ctdb->tunable.queue_buffer_size;
443 /* In client code, ctdb->tunable is not initialized.
444 * This does not affect recovery daemon.
446 if (queue->buffer_size == 0) {
447 queue->buffer_size = 1024;