2 Simple queuing of input and output records for libctdb
4 Copyright (C) Rusty Russell 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <sys/types.h>
26 #include "libctdb_private.h"
29 #include <netinet/in.h>
30 #include <dlinklist.h>
31 #include <ctdb_protocol.h> // For CTDB_DS_ALIGNMENT and ctdb_req_header
34 struct io_elem *next, *prev;
39 struct io_elem *new_io_elem(size_t len)
44 len = (len + (CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
46 elem = malloc(sizeof(*elem));
49 elem->data = malloc(len);
55 /* stamp out any padding to keep valgrind happy */
57 memset(elem->data + ask, 0, len-ask);
66 void free_io_elem(struct io_elem *io)
72 bool io_elem_finished(const struct io_elem *io)
74 return io->off == io->len;
77 void io_elem_init_req_header(struct io_elem *io,
82 struct ctdb_req_header *hdr = io_elem_data(io, NULL);
84 hdr->length = io->len;
85 hdr->ctdb_magic = CTDB_MAGIC;
86 hdr->ctdb_version = CTDB_VERSION;
87 /* Generation and srcnode only used for inter-ctdbd communication. */
89 hdr->destnode = destnode;
91 hdr->operation = operation;
95 /* Access to raw data: if len is non-NULL it is filled in. */
96 void *io_elem_data(const struct io_elem *io, size_t *len)
103 /* Returns -1 if we hit an error. Errno will be set. */
104 int read_io_elem(int fd, struct io_elem *io)
108 ret = read(fd, io->data + io->off, io->len - io->off);
113 if (io_elem_finished(io)) {
114 struct ctdb_req_header *hdr = (void *)io->data;
116 /* Finished. But maybe this was just header? */
117 if (io->len == sizeof(*hdr) && hdr->length > io->len) {
120 /* Enlarge and re-read. */
121 io->len = hdr->length;
122 newdata = realloc(io->data, io->len);
126 /* Try reading again immediately. */
127 reret = read_io_elem(fd, io);
136 /* Returns -1 if we hit an error. Errno will be set. */
137 int write_io_elem(int fd, struct io_elem *io)
141 ret = write(fd, io->data + io->off, io->len - io->off);
149 void io_elem_reset(struct io_elem *io)
154 void io_elem_queue(struct ctdb_connection *ctdb, struct io_elem *io)
156 DLIST_ADD_END(ctdb->inqueue, io, struct io_elem);
159 void io_elem_dequeue(struct ctdb_connection *ctdb, struct io_elem *io)
161 DLIST_REMOVE(ctdb->inqueue, io);