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 ||
69 /* the descriptor has been closed */
74 queue->partial.data = talloc_realloc_size(queue, queue->partial.data,
75 num_ready + queue->partial.length);
77 if (queue->partial.data == NULL) {
81 nread = read(queue->fd, queue->partial.data + queue->partial.length, num_ready);
87 data = queue->partial.data;
88 nread += queue->partial.length;
90 queue->partial.data = NULL;
91 queue->partial.length = 0;
93 if (nread >= 4 && *(uint32_t *)data == nread) {
94 /* it is the responsibility of the incoming packet
95 function to free 'data' */
96 queue->callback(data, nread, queue->private_data);
102 while (nread >= 4 && *(uint32_t *)data <= nread) {
103 /* we have at least one packet */
106 len = *(uint32_t *)data;
107 d2 = talloc_memdup(queue, data, len);
112 queue->callback(d2, len, queue->private_data);
118 /* we have only part of a packet */
119 if (data_base == data) {
120 queue->partial.data = data;
121 queue->partial.length = nread;
123 queue->partial.data = talloc_memdup(queue, data, nread);
124 if (queue->partial.data == NULL) {
127 queue->partial.length = nread;
128 talloc_free(data_base);
133 talloc_free(data_base);
137 queue->callback(NULL, 0, queue->private_data);
141 /* used when an event triggers a dead queue */
142 static void queue_dead(struct event_context *ev, struct timed_event *te,
143 struct timeval t, void *private_data)
145 struct ctdb_queue *queue = talloc_get_type(private_data, struct ctdb_queue);
146 queue->callback(NULL, 0, queue->private_data);
151 called when an incoming connection is writeable
153 static void queue_io_write(struct ctdb_queue *queue)
155 while (queue->out_queue) {
156 struct ctdb_queue_pkt *pkt = queue->out_queue;
159 n = write(queue->fd, pkt->data, pkt->length);
161 if (n == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
162 event_add_timed(queue->ctdb->ev, queue, timeval_zero(),
164 EVENT_FD_NOT_WRITEABLE(queue->fde);
169 if (n != pkt->length) {
175 DLIST_REMOVE(queue->out_queue, pkt);
179 EVENT_FD_NOT_WRITEABLE(queue->fde);
183 called when an incoming connection is readable or writeable
185 static void queue_io_handler(struct event_context *ev, struct fd_event *fde,
186 uint16_t flags, void *private_data)
188 struct ctdb_queue *queue = talloc_get_type(private_data, struct ctdb_queue);
190 if (flags & EVENT_FD_READ) {
191 queue_io_read(queue);
193 queue_io_write(queue);
199 queue a packet for sending
201 int ctdb_queue_send(struct ctdb_queue *queue, uint8_t *data, uint32_t length)
203 struct ctdb_queue_pkt *pkt;
206 /* enforce the length and alignment rules from the tcp packet allocator */
207 length2 = (length+(queue->alignment-1)) & ~(queue->alignment-1);
208 *(uint32_t *)data = length2;
210 if (length2 != length) {
211 memset(data+length, 0, length2-length);
214 /* if the queue is empty then try an immediate write, avoiding
215 queue overhead. This relies on non-blocking sockets */
216 if (queue->out_queue == NULL && queue->fd != -1) {
217 ssize_t n = write(queue->fd, data, length2);
218 if (n == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
219 event_add_timed(queue->ctdb->ev, queue, timeval_zero(),
221 /* yes, we report success, as the dead node is
222 handled via a separate event */
229 if (length2 == 0) return 0;
232 pkt = talloc(queue, struct ctdb_queue_pkt);
233 CTDB_NO_MEMORY(queue->ctdb, pkt);
235 pkt->data = talloc_memdup(pkt, data, length2);
236 CTDB_NO_MEMORY(queue->ctdb, pkt->data);
238 pkt->length = length2;
240 if (queue->out_queue == NULL && queue->fd != -1) {
241 EVENT_FD_WRITEABLE(queue->fde);
244 DLIST_ADD_END(queue->out_queue, pkt, struct ctdb_queue_pkt *);
251 setup the fd used by the queue
253 int ctdb_queue_set_fd(struct ctdb_queue *queue, int fd)
256 talloc_free(queue->fde);
260 queue->fde = event_add_fd(queue->ctdb->ev, queue, fd, EVENT_FD_READ,
261 queue_io_handler, queue);
262 if (queue->fde == NULL) {
266 if (queue->out_queue) {
267 EVENT_FD_WRITEABLE(queue->fde);
277 setup a packet queue on a socket
279 struct ctdb_queue *ctdb_queue_setup(struct ctdb_context *ctdb,
280 TALLOC_CTX *mem_ctx, int fd, int alignment,
282 ctdb_queue_cb_fn_t callback,
285 struct ctdb_queue *queue;
287 queue = talloc_zero(mem_ctx, struct ctdb_queue);
288 CTDB_NO_MEMORY_NULL(ctdb, queue);
292 queue->alignment = alignment;
293 queue->private_data = private_data;
294 queue->callback = callback;
296 if (ctdb_queue_set_fd(queue, fd) != 0) {