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 library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 This library 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 GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "lib/tdb/include/tdb.h"
26 #include "lib/events/events.h"
27 #include "lib/util/dlinklist.h"
28 #include "system/network.h"
29 #include "system/filesys.h"
30 #include "../include/ctdb_private.h"
31 #include "../include/ctdb.h"
33 /* structures for packet queueing - see common/ctdb_io.c */
39 struct ctdb_queue_pkt {
40 struct ctdb_queue_pkt *next, *prev;
46 struct ctdb_context *ctdb;
47 struct ctdb_partial partial; /* partial input packet */
48 struct ctdb_queue_pkt *out_queue;
53 ctdb_queue_cb_fn_t callback;
59 called when an incoming connection is readable
61 static void queue_io_read(struct ctdb_queue *queue)
65 uint8_t *data, *data_base;
67 if (ioctl(queue->fd, FIONREAD, &num_ready) != 0) {
71 /* the descriptor has been closed */
76 queue->partial.data = talloc_realloc_size(queue, queue->partial.data,
77 num_ready + queue->partial.length);
79 if (queue->partial.data == NULL) {
80 DEBUG(0,("read error alloc failed for %u\n",
81 num_ready + queue->partial.length));
85 nread = read(queue->fd, queue->partial.data + queue->partial.length, num_ready);
87 DEBUG(0,("read error nread=%d\n", nread));
92 data = queue->partial.data;
93 nread += queue->partial.length;
95 queue->partial.data = NULL;
96 queue->partial.length = 0;
98 if (nread >= 4 && *(uint32_t *)data == nread) {
99 /* it is the responsibility of the incoming packet
100 function to free 'data' */
101 queue->callback(data, nread, queue->private_data);
107 while (nread >= 4 && *(uint32_t *)data <= nread) {
108 /* we have at least one packet */
111 len = *(uint32_t *)data;
112 d2 = talloc_memdup(queue, data, len);
114 DEBUG(0,("read error memdup failed for %u\n", len));
118 queue->callback(d2, len, queue->private_data);
124 /* we have only part of a packet */
125 if (data_base == data) {
126 queue->partial.data = data;
127 queue->partial.length = nread;
129 queue->partial.data = talloc_memdup(queue, data, nread);
130 if (queue->partial.data == NULL) {
131 DEBUG(0,("read error memdup partial failed for %u\n",
135 queue->partial.length = nread;
136 talloc_free(data_base);
141 talloc_free(data_base);
145 queue->callback(NULL, 0, queue->private_data);
149 /* used when an event triggers a dead queue */
150 static void queue_dead(struct event_context *ev, struct timed_event *te,
151 struct timeval t, void *private_data)
153 struct ctdb_queue *queue = talloc_get_type(private_data, struct ctdb_queue);
154 queue->callback(NULL, 0, queue->private_data);
159 called when an incoming connection is writeable
161 static void queue_io_write(struct ctdb_queue *queue)
163 while (queue->out_queue) {
164 struct ctdb_queue_pkt *pkt = queue->out_queue;
166 if (queue->ctdb->flags & CTDB_FLAG_TORTURE) {
167 n = write(queue->fd, pkt->data, 1);
169 n = write(queue->fd, pkt->data, pkt->length);
172 if (n == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
173 event_add_timed(queue->ctdb->ev, queue, timeval_zero(),
175 EVENT_FD_NOT_WRITEABLE(queue->fde);
180 if (n != pkt->length) {
186 DLIST_REMOVE(queue->out_queue, pkt);
190 EVENT_FD_NOT_WRITEABLE(queue->fde);
194 called when an incoming connection is readable or writeable
196 static void queue_io_handler(struct event_context *ev, struct fd_event *fde,
197 uint16_t flags, void *private_data)
199 struct ctdb_queue *queue = talloc_get_type(private_data, struct ctdb_queue);
201 if (flags & EVENT_FD_READ) {
202 queue_io_read(queue);
204 queue_io_write(queue);
210 queue a packet for sending
212 int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length)
214 struct ctdb_queue_pkt *pkt;
217 if (queue->alignment) {
218 /* enforce the length and alignment rules from the tcp packet allocator */
219 length2 = (length+(queue->alignment-1)) & ~(queue->alignment-1);
220 *(uint32_t *)data = length2;
225 if (length2 != length) {
226 memset(data+length, 0, length2-length);
229 /* if the queue is empty then try an immediate write, avoiding
230 queue overhead. This relies on non-blocking sockets */
231 if (queue->out_queue == NULL && queue->fd != -1 &&
232 !(queue->ctdb->flags & CTDB_FLAG_TORTURE)) {
233 ssize_t n = write(queue->fd, data, length2);
234 if (n == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
235 event_add_timed(queue->ctdb->ev, queue, timeval_zero(),
237 /* yes, we report success, as the dead node is
238 handled via a separate event */
245 if (length2 == 0) return 0;
248 pkt = talloc(queue, struct ctdb_queue_pkt);
249 CTDB_NO_MEMORY(queue->ctdb, pkt);
251 pkt->data = talloc_memdup(pkt, data, length2);
252 CTDB_NO_MEMORY(queue->ctdb, pkt->data);
254 pkt->length = length2;
256 if (queue->out_queue == NULL && queue->fd != -1) {
257 EVENT_FD_WRITEABLE(queue->fde);
260 DLIST_ADD_END(queue->out_queue, pkt, struct ctdb_queue_pkt *);
267 setup the fd used by the queue
269 int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd)
272 talloc_free(queue->fde);
276 queue->fde = event_add_fd(queue->ctdb->ev, queue, fd, EVENT_FD_READ,
277 queue_io_handler, queue);
278 if (queue->fde == NULL) {
282 if (queue->out_queue) {
283 EVENT_FD_WRITEABLE(queue->fde);
293 setup a packet queue on a socket
295 struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb,
296 TALLOC_CTX *mem_ctx, int fd, int alignment,
298 ctdb_queue_cb_fn_t callback,
301 struct ctdb_queue *queue;
303 queue = talloc_zero(mem_ctx, struct ctdb_queue);
304 CTDB_NO_MEMORY_NULL(ctdb, queue);
308 queue->alignment = alignment;
309 queue->private_data = private_data;
310 queue->callback = callback;
312 if (ctdb_queue_set_fd(queue, fd) != 0) {