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>
28 #include <netinet/in.h>
29 #include <ctdb_protocol.h> // For CTDB_DS_ALIGNMENT and ctdb_req_header
36 struct io_elem *new_io_elem(size_t len)
41 len = (len + (CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
43 elem = malloc(sizeof(*elem));
46 elem->data = malloc(len);
52 /* stamp out any padding to keep valgrind happy */
54 memset(elem->data + ask, 0, len-ask);
61 void free_io_elem(struct io_elem *io)
67 bool io_elem_finished(const struct io_elem *io)
69 return io->off == io->len;
72 void io_elem_init_req_header(struct io_elem *io,
77 struct ctdb_req_header *hdr = io_elem_data(io, NULL);
79 hdr->length = io->len;
80 hdr->ctdb_magic = CTDB_MAGIC;
81 hdr->ctdb_version = CTDB_VERSION;
82 /* Generation and srcnode only used for inter-ctdbd communication. */
84 hdr->destnode = destnode;
86 hdr->operation = operation;
90 /* Access to raw data: if len is non-NULL it is filled in. */
91 void *io_elem_data(const struct io_elem *io, size_t *len)
98 /* Returns -1 if we hit an error. Errno will be set. */
99 int read_io_elem(int fd, struct io_elem *io)
103 ret = read(fd, io->data + io->off, io->len - io->off);
108 if (io_elem_finished(io)) {
109 struct ctdb_req_header *hdr = (void *)io->data;
111 /* Finished. But maybe this was just header? */
112 if (io->len == sizeof(*hdr) && hdr->length > io->len) {
115 /* Enlarge and re-read. */
116 io->len = hdr->length;
117 newdata = realloc(io->data, io->len);
121 /* Try reading again immediately. */
122 reret = read_io_elem(fd, io);
131 /* Returns -1 if we hit an error. Errno will be set. */
132 int write_io_elem(int fd, struct io_elem *io)
136 ret = write(fd, io->data + io->off, io->len - io->off);
144 void io_elem_reset(struct io_elem *io)