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>
27 #include <ctdb_protocol.h> // For CTDB_DS_ALIGNMENT and ctdb_req_header
34 struct io_elem *new_io_elem(size_t len)
37 len = (len + (CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
39 elem = malloc(sizeof(*elem));
42 elem->data = malloc(len);
53 void free_io_elem(struct io_elem *io)
59 bool io_elem_finished(const struct io_elem *io)
61 return io->off == io->len;
64 void io_elem_init_req_header(struct io_elem *io,
69 struct ctdb_req_header *hdr = io_elem_data(io, NULL);
71 hdr->length = io->len;
72 hdr->ctdb_magic = CTDB_MAGIC;
73 hdr->ctdb_version = CTDB_VERSION;
74 /* Generation and srcnode only used for inter-ctdbd communication. */
76 hdr->destnode = destnode;
78 hdr->operation = operation;
82 /* Access to raw data: if len is non-NULL it is filled in. */
83 void *io_elem_data(const struct io_elem *io, size_t *len)
90 /* Returns -1 if we hit an error. Errno will be set. */
91 int read_io_elem(int fd, struct io_elem *io)
95 ret = read(fd, io->data + io->off, io->len - io->off);
100 if (io_elem_finished(io)) {
101 struct ctdb_req_header *hdr = (void *)io->data;
103 /* Finished. But maybe this was just header? */
104 if (io->len == sizeof(*hdr) && hdr->length > io->len) {
106 /* Enlarge and re-read. */
107 io->len = hdr->length;
108 io->data = realloc(io->data, io->len);
111 /* Try reading again immediately. */
112 reret = read_io_elem(fd, io);
121 /* Returns -1 if we hit an error. Errno will be set. */
122 int write_io_elem(int fd, struct io_elem *io)
126 ret = write(fd, io->data + io->off, io->len - io->off);
134 void io_elem_reset(struct io_elem *io)