1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/*
Simple queuing of input and output records for libctdb
Copyright (C) Rusty Russell 2010
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include "io_elem.h"
#include <tdb.h>
#include <ctdb_protocol.h> // For CTDB_DS_ALIGNMENT and ctdb_req_header
struct io_elem {
size_t len, off;
char *data;
};
struct io_elem *new_io_elem(size_t len)
{
struct io_elem *elem;
size_t ask = len;
len = (len + (CTDB_DS_ALIGNMENT-1)) & ~(CTDB_DS_ALIGNMENT-1);
elem = malloc(sizeof(*elem));
if (!elem)
return NULL;
elem->data = malloc(len);
if (!elem->data) {
free(elem);
return NULL;
}
/* stamp out any padding to keep valgrind happy */
if (ask != len) {
memset(elem->data + ask, 0, len-ask);
}
elem->len = len;
elem->off = 0;
return elem;
}
void free_io_elem(struct io_elem *io)
{
free(io->data);
free(io);
}
bool io_elem_finished(const struct io_elem *io)
{
return io->off == io->len;
}
void io_elem_init_req_header(struct io_elem *io,
uint32_t operation,
uint32_t destnode,
uint32_t reqid)
{
struct ctdb_req_header *hdr = io_elem_data(io, NULL);
hdr->length = io->len;
hdr->ctdb_magic = CTDB_MAGIC;
hdr->ctdb_version = CTDB_VERSION;
/* Generation and srcnode only used for inter-ctdbd communication. */
hdr->generation = 0;
hdr->destnode = destnode;
hdr->srcnode = 0;
hdr->operation = operation;
hdr->reqid = reqid;
}
/* Access to raw data: if len is non-NULL it is filled in. */
void *io_elem_data(const struct io_elem *io, size_t *len)
{
if (len)
*len = io->len;
return io->data;
}
/* Returns -1 if we hit an error. Errno will be set. */
int read_io_elem(int fd, struct io_elem *io)
{
ssize_t ret;
ret = read(fd, io->data + io->off, io->len - io->off);
if (ret < 0)
return ret;
io->off += ret;
if (io_elem_finished(io)) {
struct ctdb_req_header *hdr = (void *)io->data;
/* Finished. But maybe this was just header? */
if (io->len == sizeof(*hdr) && hdr->length > io->len) {
int reret;
void *newdata;
/* Enlarge and re-read. */
io->len = hdr->length;
newdata = realloc(io->data, io->len);
if (!newdata)
return -1;
io->data = newdata;
/* Try reading again immediately. */
reret = read_io_elem(fd, io);
if (reret >= 0)
reret += ret;
return reret;
}
}
return ret;
}
/* Returns -1 if we hit an error. Errno will be set. */
int write_io_elem(int fd, struct io_elem *io)
{
ssize_t ret;
ret = write(fd, io->data + io->off, io->len - io->off);
if (ret < 0)
return ret;
io->off += ret;
return ret;
}
void io_elem_reset(struct io_elem *io)
{
io->off = 0;
}
|