Formatted all code using indent

This commit is contained in:
Patrick J Cherry
2018-02-20 10:05:35 +00:00
parent 19a1127bde
commit f47f56d4c4
59 changed files with 7631 additions and 7762 deletions

3
CONTRIBUTING.md Normal file
View File

@@ -0,0 +1,3 @@
# Contribution guide
The code is formatted using the K&R style of "indent".

View File

@@ -45,8 +45,7 @@ int build_allocation_map(struct bitset * allocation_map, int fd)
if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
debug("Couldn't get fiemap, returning no allocation_map");
return 0; /* it's up to the caller to free the map */
}
else {
} else {
for (unsigned int i = 0; i < fiemap->fm_mapped_extents; i++) {
bitset_set_range(allocation_map,
fiemap->fm_extents[i].fe_logical,
@@ -58,12 +57,10 @@ int build_allocation_map(struct bitset * allocation_map, int fd)
* if we've actually hit max_offsets.
*/
if (fiemap->fm_mapped_extents > 0) {
struct fiemap_extent *last = &fiemap->fm_extents[
fiemap->fm_mapped_extents-1
];
struct fiemap_extent *last =
&fiemap->fm_extents[fiemap->fm_mapped_extents - 1];
offset = last->fe_logical + last->fe_length;
}
else {
} else {
offset += fiemap->fm_length;
}
}
@@ -74,7 +71,8 @@ int build_allocation_map(struct bitset * allocation_map, int fd)
}
int open_and_mmap(const char* filename, int* out_fd, uint64_t *out_size, void **out_map)
int open_and_mmap(const char *filename, int *out_fd, uint64_t * out_size,
void **out_map)
{
/*
* size and out_size are intentionally of different types.
@@ -117,9 +115,9 @@ int open_and_mmap(const char* filename, int* out_fd, uint64_t *out_size, void **
warn("mmap64() failed");
return -1;
}
debug("opened %s size %ld on fd %d @ %p", filename, size, *out_fd, *out_map);
}
else {
debug("opened %s size %ld on fd %d @ %p", filename, size, *out_fd,
*out_map);
} else {
debug("opened %s size %ld on fd %d", filename, size, *out_fd);
}
@@ -150,7 +148,8 @@ int readloop(int filedes, void *buffer, size_t size)
ssize_t result = read(filedes, buffer + readden, size - readden);
if (result == 0 /* EOF */ ) {
warn( "end-of-file detected while reading after %i bytes", readden );
warn("end-of-file detected while reading after %i bytes",
readden);
return -1;
}
@@ -170,7 +169,9 @@ int sendfileloop(int out_fd, int in_fd, off64_t *offset, size_t count)
size_t sent = 0;
while (sent < count) {
ssize_t result = sendfile64(out_fd, in_fd, offset, count - sent);
debug("sendfile64(out_fd=%d, in_fd=%d, offset=%p, count-sent=%ld) = %ld", out_fd, in_fd, offset, count-sent, result);
debug
("sendfile64(out_fd=%d, in_fd=%d, offset=%p, count-sent=%ld) = %ld",
out_fd, in_fd, offset, count - sent, result);
if (result == -1) {
debug("%s (%i) calling sendfile64()", strerror(errno), errno);
@@ -184,7 +185,8 @@ int sendfileloop(int out_fd, int in_fd, off64_t *offset, size_t count)
}
#include <errno.h>
ssize_t spliceloop(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags2)
ssize_t spliceloop(int fd_in, loff_t * off_in, int fd_out,
loff_t * off_out, size_t len, unsigned int flags2)
{
const unsigned int flags = SPLICE_F_MORE | SPLICE_F_MOVE | flags2;
size_t spliced = 0;
@@ -192,13 +194,13 @@ ssize_t spliceloop(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_
//debug("spliceloop(%d, %ld, %d, %ld, %ld)", fd_in, off_in ? *off_in : 0, fd_out, off_out ? *off_out : 0, len);
while (spliced < len) {
ssize_t result = splice(fd_in, off_in, fd_out, off_out, len, flags);
ssize_t result =
splice(fd_in, off_in, fd_out, off_out, len, flags);
if (result < 0) {
//debug("result=%ld (%s), spliced=%ld, len=%ld", result, strerror(errno), spliced, len);
if (errno == EAGAIN && (flags & SPLICE_F_NONBLOCK)) {
return spliced;
}
else {
} else {
return -1;
}
} else {
@@ -222,13 +224,19 @@ int splice_via_pipe_loop(int fd_in, int fd_out, size_t len)
while (spliced < len) {
ssize_t run = len - spliced;
ssize_t s2, s1 = spliceloop(fd_in, NULL, pipefd[1], NULL, run, SPLICE_F_NONBLOCK);
ssize_t s2, s1 =
spliceloop(fd_in, NULL, pipefd[1], NULL, run,
SPLICE_F_NONBLOCK);
/*if (run > 65535)
run = 65535; */
if (s1 < 0) { break; }
if (s1 < 0) {
break;
}
s2 = spliceloop(pipefd[0], NULL, fd_out, NULL, s1, 0);
if (s2 < 0) { break; }
if (s2 < 0) {
break;
}
spliced += s2;
}
close(pipefd[0]);
@@ -250,7 +258,9 @@ int read_until_newline(int fd, char* buf, int bufsize)
for (cur = 0; cur < bufsize; cur++) {
int result = read(fd, buf + cur, 1);
if (result <= 0) { return -1; }
if (result <= 0) {
return -1;
}
if (buf[cur] == 10) {
buf[cur] = '\0';
break;
@@ -275,7 +285,9 @@ int read_lines_until_blankline(int fd, int max_line_length, char ***lines)
* -1 for an eof
* -1 for a read error
*/
if (readden <= 1) { return lines_count; }
if (readden <= 1) {
return lines_count;
}
*lines = xrealloc(*lines, (lines_count + 1) * sizeof(char *));
(*lines)[lines_count] = strdup(line);
if ((*lines)[lines_count][0] == 0) {
@@ -313,7 +325,8 @@ ssize_t iobuf_read(int fd, struct iobuf *iobuf, size_t default_size )
}
left = iobuf->size - iobuf->needle;
debug( "Reading %"PRIu32" of %"PRIu32" bytes from fd %i", left, iobuf->size, fd );
debug("Reading %" PRIu32 " of %" PRIu32 " bytes from fd %i", left,
iobuf->size, fd);
count = read(fd, iobuf->buf + iobuf->needle, left);
@@ -341,7 +354,8 @@ ssize_t iobuf_write( int fd, struct iobuf *iobuf )
size_t left = iobuf->size - iobuf->needle;
ssize_t count;
debug( "Writing %"PRIu32" of %"PRIu32" bytes to fd %i", left, iobuf->size, fd );
debug("Writing %" PRIu32 " of %" PRIu32 " bytes to fd %i", left,
iobuf->size, fd);
count = write(fd, iobuf->buf + iobuf->needle, left);
if (count >= 0) {

View File

@@ -38,7 +38,8 @@ int readloop(int filedes, void *buffer, size_t size);
int sendfileloop(int out_fd, int in_fd, off64_t * offset, size_t count);
/** Repeat a splice() operation until we have 'len' bytes. */
ssize_t spliceloop(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags2);
ssize_t spliceloop(int fd_in, loff_t * off_in, int fd_out,
loff_t * off_out, size_t len, unsigned int flags2);
/** Copy ''len'' bytes from ''fd_in'' to ''fd_out'' by creating a temporary
* pipe and using the Linux splice call repeatedly until it has transferred
@@ -65,7 +66,8 @@ int read_lines_until_blankline(int fd, int max_line_length, char ***lines);
* ''out_size'' and the address of the mmap in ''out_map''. If anything goes
* wrong, returns -1 setting errno, otherwise 0.
*/
int open_and_mmap( const char* filename, int* out_fd, uint64_t* out_size, void **out_map);
int open_and_mmap(const char *filename, int *out_fd, uint64_t * out_size,
void **out_map);
/** Check to see whether the given file descriptor is closed.
@@ -73,4 +75,3 @@ int open_and_mmap( const char* filename, int* out_fd, uint64_t* out_size, void *
int fd_is_closed(int fd_in);
#endif

View File

@@ -94,4 +94,3 @@ void mode(char* mode, int argc, char **argv);
char *help_help_text;
#endif

View File

@@ -59,4 +59,3 @@ void nbd_h2r_reply( struct nbd_reply * from, struct nbd_reply_raw * to )
to->error = htobe32(from->error);
to->handle.w = from->handle.w;
}

View File

@@ -112,4 +112,3 @@ void nbd_h2r_request( struct nbd_request * from, struct nbd_request_raw * to );
void nbd_h2r_reply(struct nbd_reply *from, struct nbd_reply_raw *to);
#endif

View File

@@ -22,7 +22,9 @@ int parse_ip_to_sockaddr(struct sockaddr* out, char* src)
/* allow user to start with [ and end with any other invalid char */
{
int i = 0, j = 0;
if (src[i] == '[') { i++; }
if (src[i] == '[') {
i++;
}
for (; i < 64 && IS_IP_VALID_CHAR(src[i]); i++) {
temp[j++] = src[i];
}
@@ -72,8 +74,7 @@ int parse_acl(struct ip_and_mask (**out)[], int max, char **entries)
if (max == 0) {
*out = NULL;
return 0;
}
else {
} else {
list = xmalloc(max * sizeof(struct ip_and_mask));
*out = (struct ip_and_mask(*)[]) list;
debug("acl alloc: %p", *out);
@@ -88,16 +89,14 @@ int parse_acl(struct ip_and_mask (**out)[], int max, char **entries)
return i;
}
for (j=0; entries[i][j] && entries[i][j] != '/'; j++)
; // increment j!
for (j = 0; entries[i][j] && entries[i][j] != '/'; j++); // increment j!
if (entries[i][j] == '/') {
outentry->mask = atoi(entries[i] + j + 1);
if (outentry->mask < 1 || outentry->mask > MAX_MASK_BITS) {
return i;
}
}
else {
} else {
outentry->mask = MAX_MASK_BITS;
}
# undef MAX_MASK_BITS
@@ -124,4 +123,3 @@ void parse_port( char *s_port, struct sockaddr_in *out )
}
out->sin_port = htobe16(raw_port);
}

View File

@@ -26,4 +26,3 @@ int parse_acl(struct ip_and_mask (**out)[], int max, char **entries);
void parse_port(char *s_port, struct sockaddr_in *out);
#endif

View File

@@ -10,7 +10,9 @@
int socket_connect(struct sockaddr *to, struct sockaddr *from)
{
int fd = socket(to->sa_family == AF_INET ? PF_INET : PF_INET6, SOCK_STREAM, 0);
int fd =
socket(to->sa_family == AF_INET ? PF_INET : PF_INET6, SOCK_STREAM,
0);
if (fd < 0) {
warn("Couldn't create client socket");
return -1;
@@ -41,7 +43,8 @@ int socket_connect(struct sockaddr* to, struct sockaddr* from)
return fd;
}
int nbd_check_hello( struct nbd_init_raw* init_raw, uint64_t* out_size, uint32_t* out_flags )
int nbd_check_hello(struct nbd_init_raw *init_raw, uint64_t * out_size,
uint32_t * out_flags)
{
if (strncmp(init_raw->passwd, INIT_PASSWD, 8) != 0) {
warn("wrong passwd");
@@ -66,7 +69,8 @@ fail:
}
int socket_nbd_read_hello( int fd, uint64_t* out_size, uint32_t* out_flags )
int socket_nbd_read_hello(int fd, uint64_t * out_size,
uint32_t * out_flags)
{
struct nbd_init_raw init_raw;
@@ -79,7 +83,8 @@ int socket_nbd_read_hello( int fd, uint64_t* out_size, uint32_t* out_flags )
return nbd_check_hello(&init_raw, out_size, out_flags);
}
void nbd_hello_to_buf( struct nbd_init_raw *buf, off64_t out_size, uint32_t out_flags )
void nbd_hello_to_buf(struct nbd_init_raw *buf, off64_t out_size,
uint32_t out_flags)
{
struct nbd_init init;
@@ -106,21 +111,25 @@ int socket_nbd_write_hello( int fd, off64_t out_size, uint32_t out_flags )
return 1;
}
void fill_request(struct nbd_request_raw *request_raw, uint16_t type, uint16_t flags, uint64_t from, uint32_t len)
void fill_request(struct nbd_request_raw *request_raw, uint16_t type,
uint16_t flags, uint64_t from, uint32_t len)
{
request_raw->magic = htobe32(REQUEST_MAGIC);
request_raw->type = htobe16(type);
request_raw->flags = htobe16(flags);
request_raw->handle.w = (((uint64_t)rand()) << 32) | ((uint64_t)rand());
request_raw->handle.w =
(((uint64_t) rand()) << 32) | ((uint64_t) rand());
request_raw->from = htobe64(from);
request_raw->len = htobe32(len);
}
void read_reply(int fd, uint64_t request_raw_handle, struct nbd_reply *reply)
void read_reply(int fd, uint64_t request_raw_handle,
struct nbd_reply *reply)
{
struct nbd_reply_raw reply_raw;
ERROR_IF_NEGATIVE(readloop(fd, &reply_raw, sizeof(struct nbd_reply_raw)),
ERROR_IF_NEGATIVE(readloop
(fd, &reply_raw, sizeof(struct nbd_reply_raw)),
"Couldn't read reply");
nbd_r2h_reply(&reply_raw, reply);
@@ -145,16 +154,17 @@ void wait_for_data( int fd, int timeout_secs )
FD_ZERO(&fds);
FD_SET(fd, &fds);
selected = sock_try_select(
FD_SETSIZE, &fds, NULL, NULL, timeout_secs >=0 ? &tv : NULL
);
selected =
sock_try_select(FD_SETSIZE, &fds, NULL, NULL,
timeout_secs >= 0 ? &tv : NULL);
FATAL_IF(-1 == selected, "Select failed");
ERROR_IF(0 == selected, "Timed out waiting for reply");
}
void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd, void* out_buf, int timeout_secs)
void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd,
void *out_buf, int timeout_secs)
{
struct nbd_request_raw request_raw;
struct nbd_reply reply;
@@ -168,18 +178,15 @@ void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd, void* out_
read_reply(fd, request_raw.handle.w, &reply);
if (out_buf) {
FATAL_IF_NEGATIVE(readloop(fd, out_buf, len),
"Read failed");
}
else {
FATAL_IF_NEGATIVE(
splice_via_pipe_loop(fd, out_fd, len),
"Splice failed"
);
FATAL_IF_NEGATIVE(readloop(fd, out_buf, len), "Read failed");
} else {
FATAL_IF_NEGATIVE(splice_via_pipe_loop(fd, out_fd, len),
"Splice failed");
}
}
void socket_nbd_write(int fd, uint64_t from, uint32_t len, int in_fd, void* in_buf, int timeout_secs)
void socket_nbd_write(int fd, uint64_t from, uint32_t len, int in_fd,
void *in_buf, int timeout_secs)
{
struct nbd_request_raw request_raw;
struct nbd_reply reply;
@@ -189,14 +196,10 @@ void socket_nbd_write(int fd, uint64_t from, uint32_t len, int in_fd, void* in_b
"Couldn't write request");
if (in_buf) {
ERROR_IF_NEGATIVE(writeloop(fd, in_buf, len),
"Write failed");
}
else {
ERROR_IF_NEGATIVE(
splice_via_pipe_loop(in_fd, fd, len),
"Splice failed"
);
ERROR_IF_NEGATIVE(writeloop(fd, in_buf, len), "Write failed");
} else {
ERROR_IF_NEGATIVE(splice_via_pipe_loop(in_fd, fd, len),
"Splice failed");
}
wait_for_data(fd, timeout_secs);
@@ -239,7 +242,9 @@ int socket_nbd_disconnect( int fd )
void do_read(struct mode_readwrite_params *params)
{
params->client = socket_connect(&params->connect_to.generic, &params->connect_from.generic);
params->client =
socket_connect(&params->connect_to.generic,
&params->connect_from.generic);
FATAL_IF_NEGATIVE(params->client, "Couldn't connect.");
CHECK_RANGE("read");
socket_nbd_read(params->client, params->from, params->len,
@@ -249,11 +254,12 @@ void do_read(struct mode_readwrite_params* params)
void do_write(struct mode_readwrite_params *params)
{
params->client = socket_connect(&params->connect_to.generic, &params->connect_from.generic);
params->client =
socket_connect(&params->connect_to.generic,
&params->connect_from.generic);
FATAL_IF_NEGATIVE(params->client, "Couldn't connect.");
CHECK_RANGE("write");
socket_nbd_write(params->client, params->from, params->len,
params->data_fd, NULL, 10);
close(params->client);
}

View File

@@ -9,15 +9,18 @@
int socket_connect(struct sockaddr *to, struct sockaddr *from);
int socket_nbd_read_hello(int fd, uint64_t * size, uint32_t * flags);
int socket_nbd_write_hello(int fd, uint64_t size, uint32_t flags);
void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd, void* out_buf, int timeout_secs);
void socket_nbd_write(int fd, uint64_t from, uint32_t len, int out_fd, void* out_buf, int timeout_secs);
void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd,
void *out_buf, int timeout_secs);
void socket_nbd_write(int fd, uint64_t from, uint32_t len, int out_fd,
void *out_buf, int timeout_secs);
int socket_nbd_disconnect(int fd);
/* as you can see, we're slowly accumulating code that should really be in an
* NBD library */
void nbd_hello_to_buf( struct nbd_init_raw* buf, uint64_t out_size, uint32_t out_flags );
int nbd_check_hello( struct nbd_init_raw* init_raw, uint64_t* out_size, uint32_t* out_flags );
void nbd_hello_to_buf(struct nbd_init_raw *buf, uint64_t out_size,
uint32_t out_flags);
int nbd_check_hello(struct nbd_init_raw *init_raw, uint64_t * out_size,
uint32_t * out_flags);
#endif

View File

@@ -24,7 +24,8 @@ void print_response( const char * response )
fprintf(out, "%s\n", response_text + 2);
}
void do_remote_command(char* command, char* socket_name, int argc, char** argv)
void do_remote_command(char *command, char *socket_name, int argc,
char **argv)
{
char newline = 10;
int i;
@@ -40,10 +41,10 @@ void do_remote_command(char* command, char* socket_name, int argc, char** argv)
address.sun_family = AF_UNIX;
strncpy(address.sun_path, socket_name, sizeof(address.sun_path));
FATAL_IF_NEGATIVE(
connect(remote, (struct sockaddr*) &address, sizeof(address)),
"Couldn't connect to %s", socket_name
);
FATAL_IF_NEGATIVE(connect
(remote, (struct sockaddr *) &address,
sizeof(address)), "Couldn't connect to %s",
socket_name);
write(remote, command, strlen(command));
write(remote, &newline, 1);
@@ -55,13 +56,10 @@ void do_remote_command(char* command, char* socket_name, int argc, char** argv)
}
write(remote, &newline, 1);
FATAL_IF_NEGATIVE(
read_until_newline(remote, response, max_response),
"Couldn't read response from %s", socket_name
);
FATAL_IF_NEGATIVE(read_until_newline(remote, response, max_response),
"Couldn't read response from %s", socket_name);
print_response(response);
exit(atoi(response));
}

View File

@@ -52,7 +52,9 @@ struct self_pipe * self_pipe_create(void)
struct self_pipe *sig = xmalloc(sizeof(struct self_pipe));
int fds[2];
if ( NULL == sig ) { return NULL; }
if (NULL == sig) {
return NULL;
}
if (pipe(fds)) {
free(sig);
@@ -60,7 +62,8 @@ struct self_pipe * self_pipe_create(void)
return NULL;
}
if ( fcntl( fds[0], F_SETFL, O_NONBLOCK ) || fcntl( fds[1], F_SETFL, O_NONBLOCK ) ) {
if (fcntl(fds[0], F_SETFL, O_NONBLOCK)
|| fcntl(fds[1], F_SETFL, O_NONBLOCK)) {
int fcntl_err = errno;
while (close(fds[0]) == -1 && errno == EINTR);
while (close(fds[1]) == -1 && errno == EINTR);

View File

@@ -29,7 +29,8 @@ size_t sockaddr_size( const struct sockaddr* sa )
return ret;
}
const char* sockaddr_address_string( const struct sockaddr* sa, char* dest, size_t len )
const char *sockaddr_address_string(const struct sockaddr *sa, char *dest,
size_t len)
{
NULLCHECK(sa);
NULLCHECK(dest);
@@ -65,7 +66,8 @@ const char* sockaddr_address_string( const struct sockaddr* sa, char* dest, size
int sock_set_reuseaddr(int fd, int optval)
{
return setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval) );
return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval));
}
int sock_set_keepalive_params(int fd, int time, int intvl, int probes)
@@ -81,28 +83,33 @@ int sock_set_keepalive_params( int fd, int time, int intvl, int probes)
int sock_set_keepalive(int fd, int optval)
{
return setsockopt( fd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval) );
return setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &optval,
sizeof(optval));
}
int sock_set_tcp_keepidle(int fd, int optval)
{
return setsockopt( fd, IPPROTO_TCP, TCP_KEEPIDLE, &optval, sizeof(optval) );
return setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &optval,
sizeof(optval));
}
int sock_set_tcp_keepintvl(int fd, int optval)
{
return setsockopt( fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval, sizeof(optval) );
return setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval,
sizeof(optval));
}
int sock_set_tcp_keepcnt(int fd, int optval)
{
return setsockopt( fd, IPPROTO_TCP, TCP_KEEPCNT, &optval, sizeof(optval) );
return setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &optval,
sizeof(optval));
}
/* Set the tcp_nodelay option */
int sock_set_tcp_nodelay(int fd, int optval)
{
return setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(optval) );
return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &optval,
sizeof(optval));
}
int sock_set_tcp_cork(int fd, int optval)
@@ -140,8 +147,7 @@ int sock_try_bind( int fd, const struct sockaddr* sa )
if (0 == bind_result) {
info("Bound to %s", s_address);
break;
}
else {
} else {
warn(SHOW_ERRNO("Couldn't bind to %s", s_address));
switch (errno) {
@@ -177,7 +183,8 @@ int sock_try_bind( int fd, const struct sockaddr* sa )
return bind_result;
}
int sock_try_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
int sock_try_select(int nfds, fd_set * readfds, fd_set * writefds,
fd_set * exceptfds, struct timeval *timeout)
{
int result;
@@ -192,14 +199,16 @@ int sock_try_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptf
return result;
}
int sock_try_connect( int fd, struct sockaddr* to, socklen_t addrlen, int wait )
int sock_try_connect(int fd, struct sockaddr *to, socklen_t addrlen,
int wait)
{
fd_set fds;
struct timeval tv = { wait, 0 };
int result = 0;
if (sock_set_nonblock(fd, 1) == -1) {
warn( SHOW_ERRNO( "Failed to set socket non-blocking for connect()" ) );
warn(SHOW_ERRNO
("Failed to set socket non-blocking for connect()"));
return connect(fd, to, addrlen);
}
@@ -284,4 +293,3 @@ int sock_try_close( int fd )
return result;
}

View File

@@ -12,7 +12,8 @@ size_t sockaddr_size(const struct sockaddr* sa);
/* Convert a sockaddr into an address. Like inet_ntop, it returns dest if
* successful, NULL otherwise. In the latter case, dest will contain "???"
*/
const char* sockaddr_address_string(const struct sockaddr* sa, char* dest, size_t len);
const char *sockaddr_address_string(const struct sockaddr *sa, char *dest,
size_t len);
/* Configure TCP keepalive on a socket */
int sock_set_keepalive_params(int fd, int time, int intvl, int probes);
@@ -44,13 +45,14 @@ int sock_set_nonblock(int fd, int optval);
int sock_try_bind(int fd, const struct sockaddr *sa);
/* Try to call select(), retrying EINTR */
int sock_try_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
int sock_try_select(int nfds, fd_set * readfds, fd_set * writefds,
fd_set * exceptfds, struct timeval *timeout);
/* Try to call connect(), timing out after wait seconds */
int sock_try_connect( int fd, struct sockaddr* to, socklen_t addrlen, int wait );
int sock_try_connect(int fd, struct sockaddr *to, socklen_t addrlen,
int wait);
/* Try to call close(), retrying EINTR */
int sock_try_close(int fd);
#endif

View File

@@ -26,10 +26,12 @@ void error_handler(int fatal)
if (context) {
longjmp(context->jmp, fatal ? 1 : 2);
} else {
if (fatal) {
abort();
} else {
pthread_exit((void *) 1);
}
else {
if ( fatal ) { abort(); }
else { pthread_exit((void*) 1); }
}
}
@@ -43,7 +45,9 @@ void exit_err( const char *msg )
void mylog(int line_level, const char *format, ...)
{
if (line_level < log_level) { return; }
if (line_level < log_level) {
return;
}
va_list argptr;
@@ -57,8 +61,7 @@ uint64_t monotonic_time_ms()
struct timespec ts;
uint64_t seconds_ms, nanoseconds_ms;
FATAL_IF_NEGATIVE(
clock_gettime(CLOCK_MONOTONIC, &ts),
FATAL_IF_NEGATIVE(clock_gettime(CLOCK_MONOTONIC, &ts),
SHOW_ERRNO("clock_gettime failed")
);
@@ -75,7 +78,8 @@ uint64_t monotonic_time_ms()
void *xrealloc(void *ptr, size_t size)
{
void *p = realloc(ptr, size);
FATAL_IF_NULL(p, "couldn't xrealloc %d bytes", ptr ? "realloc" : "malloc", size);
FATAL_IF_NULL(p, "couldn't xrealloc %d bytes",
ptr ? "realloc" : "malloc", size);
return p;
}
@@ -85,4 +89,3 @@ void* xmalloc(size_t size)
memset(p, 0, size);
return p;
}

View File

@@ -13,7 +13,8 @@
void *xrealloc(void *ptr, size_t size);
void *xmalloc(size_t size);
typedef void (cleanup_handler)(void* /* context */, int /* is fatal? */);
typedef void (cleanup_handler) (void * /* context */ ,
int /* is fatal? */ );
/* set from 0 - 5 depending on what level of verbosity you want */
extern int log_level;
@@ -162,4 +163,3 @@ uint64_t monotonic_time_ms(void);
#define WARN_IF_NEGATIVE( value, msg, ... ) if ( value < 0 ) { warn( msg, ##__VA_ARGS__ ); }
#endif

View File

@@ -19,4 +19,3 @@ int main(int argc, char** argv)
return 0;
}

View File

@@ -19,6 +19,7 @@ static struct option proxy_options[] = {
GETOPT_VERBOSE,
{0}
};
static char proxy_short_options[] = "hl:p:C:P:b:" SOPT_QUIET SOPT_VERBOSE;
static char proxy_help_text[] =
"Usage: flexnbd-proxy <options>\n\n"
@@ -26,24 +27,24 @@ static char proxy_help_text[] =
"We can listen on TCP or UNIX socket, but only connect to TCP servers.\n\n"
HELP_LINE
"\t--" OPT_ADDR ",-l <ADDR>\tThe address we will bind to as a proxy.\n"
"\t--" OPT_PORT ",-p <PORT>\tThe port we will bind to as a proxy, if required.\n"
"\t--" OPT_PORT
",-p <PORT>\tThe port we will bind to as a proxy, if required.\n"
"\t--" OPT_CONNECT_ADDR ",-C <ADDR>\tAddress of the proxied server.\n"
"\t--" OPT_CONNECT_PORT ",-P <PORT>\tPort of the proxied server.\n"
"\t--" OPT_BIND ",-b <ADDR>\tThe address we connect from, as a proxy.\n"
"\t--" OPT_CACHE ",-c[=<CACHE-BYTES>]\tUse a RAM read cache of the given size.\n"
QUIET_LINE
VERBOSE_LINE;
"\t--" OPT_BIND
",-b <ADDR>\tThe address we connect from, as a proxy.\n" "\t--"
OPT_CACHE
",-c[=<CACHE-BYTES>]\tUse a RAM read cache of the given size.\n"
QUIET_LINE VERBOSE_LINE;
static char proxy_default_cache_size[] = "4096";
void read_proxy_param(
int c,
void read_proxy_param(int c,
char **downstream_addr,
char **downstream_port,
char **upstream_addr,
char **upstream_port,
char **bind_addr,
char **cache_bytes)
char **bind_addr, char **cache_bytes)
{
switch (c) {
case 'h':
@@ -116,34 +117,31 @@ int main( int argc, char *argv[] )
srand(time(NULL));
while (1) {
c = getopt_long( argc, argv, proxy_short_options, proxy_options, NULL );
if ( -1 == c ) { break; }
c = getopt_long(argc, argv, proxy_short_options, proxy_options,
NULL);
if (-1 == c) {
break;
}
read_proxy_param(c,
&downstream_addr,
&downstream_port,
&upstream_addr,
&upstream_port,
&bind_addr,
&cache_bytes
);
&upstream_port, &bind_addr, &cache_bytes);
}
if (NULL == downstream_addr) {
fprintf(stderr, "--addr is required.\n");
exit_err(proxy_help_text);
} else if (NULL == upstream_addr || NULL == upstream_port) {
fprintf( stderr, "both --conn-addr and --conn-port are required.\n" );
fprintf(stderr,
"both --conn-addr and --conn-port are required.\n");
exit_err(proxy_help_text);
}
proxy = proxy_create(
downstream_addr,
proxy = proxy_create(downstream_addr,
downstream_port,
upstream_addr,
upstream_port,
bind_addr,
cache_bytes
);
upstream_port, bind_addr, cache_bytes);
/* Set these *after* proxy has been assigned to */
sigaction(SIGTERM, &exit_action, NULL);
@@ -152,15 +150,12 @@ int main( int argc, char *argv[] )
signal(SIGPIPE, SIG_IGN); /* calls to splice() unhelpfully throw this */
if (NULL != downstream_port) {
info(
"Proxying between %s %s (downstream) and %s %s (upstream)",
downstream_addr, downstream_port, upstream_addr, upstream_port
);
info("Proxying between %s %s (downstream) and %s %s (upstream)",
downstream_addr, downstream_port, upstream_addr,
upstream_port);
} else {
info(
"Proxying between %s (downstream) and %s %s (upstream)",
downstream_addr, upstream_addr, upstream_port
);
info("Proxying between %s (downstream) and %s %s (upstream)",
downstream_addr, upstream_addr, upstream_port);
}
success = do_proxy(proxy);
@@ -168,4 +163,3 @@ int main( int argc, char *argv[] )
return success ? 0 : 1;
}

View File

@@ -2,7 +2,8 @@
#include "util.h"
struct prefetch* prefetch_create( size_t size_bytes ){
struct prefetch *prefetch_create(size_t size_bytes)
{
struct prefetch *out = xmalloc(sizeof(struct prefetch));
NULLCHECK(out);
@@ -19,14 +20,16 @@ struct prefetch* prefetch_create( size_t size_bytes ){
}
void prefetch_destroy( struct prefetch *prefetch ) {
void prefetch_destroy(struct prefetch *prefetch)
{
if (prefetch) {
free(prefetch->buffer);
free(prefetch);
}
}
size_t prefetch_size( struct prefetch *prefetch){
size_t prefetch_size(struct prefetch *prefetch)
{
if (prefetch) {
return prefetch->size;
} else {
@@ -34,21 +37,25 @@ size_t prefetch_size( struct prefetch *prefetch){
}
}
void prefetch_set_is_empty( struct prefetch *prefetch ){
void prefetch_set_is_empty(struct prefetch *prefetch)
{
prefetch_set_full(prefetch, 0);
}
void prefetch_set_is_full( struct prefetch *prefetch ){
void prefetch_set_is_full(struct prefetch *prefetch)
{
prefetch_set_full(prefetch, 1);
}
void prefetch_set_full( struct prefetch *prefetch, int val ){
void prefetch_set_full(struct prefetch *prefetch, int val)
{
if (prefetch) {
prefetch->is_full = val;
}
}
int prefetch_is_full( struct prefetch *prefetch ){
int prefetch_is_full(struct prefetch *prefetch)
{
if (prefetch) {
return prefetch->is_full;
} else {
@@ -56,13 +63,16 @@ int prefetch_is_full( struct prefetch *prefetch ){
}
}
int prefetch_contains( struct prefetch *prefetch, uint64_t from, uint32_t len ){
int prefetch_contains(struct prefetch *prefetch, uint64_t from,
uint32_t len)
{
NULLCHECK(prefetch);
return from >= prefetch->from &&
from + len <= prefetch->from + prefetch->len;
}
char *prefetch_offset( struct prefetch *prefetch, uint64_t from ){
char *prefetch_offset(struct prefetch *prefetch, uint64_t from)
{
NULLCHECK(prefetch);
return prefetch->buffer + (from - prefetch->from);
}

View File

@@ -27,7 +27,8 @@ void prefetch_set_is_empty( struct prefetch *prefetch );
void prefetch_set_is_full(struct prefetch *prefetch);
void prefetch_set_full(struct prefetch *prefetch, int val);
int prefetch_is_full(struct prefetch *prefetch);
int prefetch_contains( struct prefetch *prefetch, uint64_t from, uint32_t len );
int prefetch_contains(struct prefetch *prefetch, uint64_t from,
uint32_t len);
char *prefetch_offset(struct prefetch *prefetch, uint64_t from);
#endif

View File

@@ -13,13 +13,11 @@
#include <sys/socket.h>
#include <netinet/tcp.h>
struct proxier* proxy_create(
char* s_downstream_address,
struct proxier *proxy_create(char *s_downstream_address,
char *s_downstream_port,
char *s_upstream_address,
char *s_upstream_port,
char* s_upstream_bind,
char* s_cache_bytes )
char *s_upstream_bind, char *s_cache_bytes)
{
struct proxier *out;
out = xmalloc(sizeof(struct proxier));
@@ -27,10 +25,9 @@ struct proxier* proxy_create(
FATAL_IF_NULL(s_downstream_address, "Listen address not specified");
NULLCHECK(s_downstream_address);
FATAL_UNLESS(
parse_to_sockaddr( &out->listen_on.generic, s_downstream_address ),
"Couldn't parse downstream address %s"
);
FATAL_UNLESS(parse_to_sockaddr
(&out->listen_on.generic, s_downstream_address),
"Couldn't parse downstream address %s");
if (out->listen_on.family != AF_UNIX) {
FATAL_IF_NULL(s_downstream_port, "Downstream port not specified");
@@ -41,22 +38,19 @@ struct proxier* proxy_create(
FATAL_IF_NULL(s_upstream_address, "Upstream address not specified");
NULLCHECK(s_upstream_address);
FATAL_UNLESS(
parse_ip_to_sockaddr( &out->connect_to.generic, s_upstream_address ),
FATAL_UNLESS(parse_ip_to_sockaddr
(&out->connect_to.generic, s_upstream_address),
"Couldn't parse upstream address '%s'",
s_upstream_address
);
s_upstream_address);
FATAL_IF_NULL(s_upstream_port, "Upstream port not specified");
NULLCHECK(s_upstream_port);
parse_port(s_upstream_port, &out->connect_to.v4);
if (s_upstream_bind) {
FATAL_IF_ZERO(
parse_ip_to_sockaddr( &out->connect_from.generic, s_upstream_bind ),
"Couldn't parse bind address '%s'",
s_upstream_bind
);
FATAL_IF_ZERO(parse_ip_to_sockaddr
(&out->connect_from.generic, s_upstream_bind),
"Couldn't parse bind address '%s'", s_upstream_bind);
out->bind = 1;
}
@@ -83,18 +77,21 @@ struct proxier* proxy_create(
out->req.buf = xmalloc(NBD_MAX_SIZE + NBD_REQUEST_SIZE);
out->rsp.buf = xmalloc(NBD_MAX_SIZE + NBD_REPLY_SIZE);
log_context = xmalloc( strlen(s_upstream_address) + strlen(s_upstream_port) + 2 );
log_context =
xmalloc(strlen(s_upstream_address) + strlen(s_upstream_port) + 2);
sprintf(log_context, "%s:%s", s_upstream_address, s_upstream_port);
return out;
}
int proxy_prefetches( struct proxier* proxy ) {
int proxy_prefetches(struct proxier *proxy)
{
NULLCHECK(proxy);
return proxy->prefetch != NULL;
}
int proxy_prefetch_bufsize( struct proxier* proxy ){
int proxy_prefetch_bufsize(struct proxier *proxy)
{
NULLCHECK(proxy);
return prefetch_size(proxy->prefetch);
}
@@ -110,7 +107,8 @@ void proxy_destroy( struct proxier* proxy )
}
/* Shared between our two different connect_to_upstream paths */
void proxy_finish_connect_to_upstream( struct proxier *proxy, uint64_t size, uint32_t flags );
void proxy_finish_connect_to_upstream(struct proxier *proxy, uint64_t size,
uint32_t flags);
/* Try to establish a connection to our upstream server. Return 1 on success,
* 0 on failure. this is a blocking call that returns a non-blocking socket.
@@ -131,10 +129,9 @@ int proxy_connect_to_upstream( struct proxier* proxy )
}
if (!socket_nbd_read_hello(fd, &size, &flags)) {
WARN_IF_NEGATIVE(
sock_try_close( fd ),
"Couldn't close() after failed read of NBD hello on fd %i", fd
);
WARN_IF_NEGATIVE(sock_try_close(fd),
"Couldn't close() after failed read of NBD hello on fd %i",
fd);
return 0;
}
@@ -161,7 +158,8 @@ void proxy_start_connect_to_upstream( struct proxier* proxy )
fd = socket(to->sa_family, SOCK_STREAM, 0);
if (fd < 0) {
warn( SHOW_ERRNO( "Couldn't create socket to reconnect to upstream" ) );
warn(SHOW_ERRNO
("Couldn't create socket to reconnect to upstream"));
return;
}
@@ -196,15 +194,15 @@ error:
return;
}
void proxy_finish_connect_to_upstream( struct proxier *proxy, uint64_t size, uint32_t flags ) {
void proxy_finish_connect_to_upstream(struct proxier *proxy, uint64_t size,
uint32_t flags)
{
if (proxy->upstream_size == 0) {
info("Size of upstream image is %" PRIu64 " bytes", size);
} else if (proxy->upstream_size != size) {
warn(
"Size changed from %"PRIu64" to %"PRIu64" bytes",
proxy->upstream_size, size
);
warn("Size changed from %" PRIu64 " to %" PRIu64 " bytes",
proxy->upstream_size, size);
}
proxy->upstream_size = size;
@@ -212,10 +210,8 @@ void proxy_finish_connect_to_upstream( struct proxier *proxy, uint64_t size, uin
if (proxy->upstream_flags == 0) {
info("Upstream transmission flags set to %" PRIu32 "", flags);
} else if (proxy->upstream_flags != flags) {
warn(
"Upstream transmission flags changed from %"PRIu32" to %"PRIu32"",
proxy->upstream_flags, flags
);
warn("Upstream transmission flags changed from %" PRIu32 " to %"
PRIu32 "", proxy->upstream_flags, flags);
}
proxy->upstream_flags = flags;
@@ -237,11 +233,9 @@ void proxy_disconnect_from_upstream( struct proxier* proxy )
info("Closing upstream connection on fd %i", proxy->upstream_fd);
/* TODO: An NBD disconnect would be pleasant here */
WARN_IF_NEGATIVE(
sock_try_close( proxy->upstream_fd ),
WARN_IF_NEGATIVE(sock_try_close(proxy->upstream_fd),
"Failed to close() fd %i when disconnecting from upstream",
proxy->upstream_fd
);
proxy->upstream_fd);
proxy->upstream_fd = -1;
}
}
@@ -253,31 +247,28 @@ void proxy_open_listen_socket(struct proxier* params)
NULLCHECK(params);
params->listen_fd = socket(params->listen_on.family, SOCK_STREAM, 0);
FATAL_IF_NEGATIVE(
params->listen_fd, SHOW_ERRNO( "Couldn't create listen socket" )
FATAL_IF_NEGATIVE(params->listen_fd,
SHOW_ERRNO("Couldn't create listen socket")
);
/* Allow us to restart quickly */
FATAL_IF_NEGATIVE(
sock_set_reuseaddr(params->listen_fd, 1),
FATAL_IF_NEGATIVE(sock_set_reuseaddr(params->listen_fd, 1),
SHOW_ERRNO("Couldn't set SO_REUSEADDR")
);
if (AF_UNIX != params->listen_on.family) {
FATAL_IF_NEGATIVE(
sock_set_tcp_nodelay(params->listen_fd, 1),
FATAL_IF_NEGATIVE(sock_set_tcp_nodelay(params->listen_fd, 1),
SHOW_ERRNO("Couldn't set TCP_NODELAY")
);
}
FATAL_UNLESS_ZERO(
sock_try_bind( params->listen_fd, &params->listen_on.generic ),
FATAL_UNLESS_ZERO(sock_try_bind
(params->listen_fd, &params->listen_on.generic),
SHOW_ERRNO("Failed to bind to listening socket")
);
/* We're only serving one client at a time, hence backlog of 1 */
FATAL_IF_NEGATIVE(
listen(params->listen_fd, 1),
FATAL_IF_NEGATIVE(listen(params->listen_fd, 1),
SHOW_ERRNO("Failed to listen on listening socket")
);
@@ -308,8 +299,8 @@ static char* proxy_session_state_names[] = {
static inline int proxy_state_upstream(int state)
{
return state == CONNECT_TO_UPSTREAM || state == READ_INIT_FROM_UPSTREAM ||
state == WRITE_TO_UPSTREAM || state == READ_FROM_UPSTREAM;
return state == CONNECT_TO_UPSTREAM || state == READ_INIT_FROM_UPSTREAM
|| state == WRITE_TO_UPSTREAM || state == READ_FROM_UPSTREAM;
}
int proxy_prefetch_for_request(struct proxier *proxy, int state)
@@ -318,8 +309,10 @@ int proxy_prefetch_for_request( struct proxier* proxy, int state )
struct nbd_request *req = &proxy->req_hdr;
struct nbd_reply *rsp = &proxy->rsp_hdr;
struct nbd_request_raw* req_raw = (struct nbd_request_raw*) proxy->req.buf;
struct nbd_reply_raw *rsp_raw = (struct nbd_reply_raw*) proxy->rsp.buf;
struct nbd_request_raw *req_raw =
(struct nbd_request_raw *) proxy->req.buf;
struct nbd_reply_raw *rsp_raw =
(struct nbd_reply_raw *) proxy->rsp.buf;
int is_read = req->type == REQUEST_READ;
@@ -340,11 +333,8 @@ int proxy_prefetch_for_request( struct proxier* proxy, int state )
nbd_h2r_reply(rsp, rsp_raw);
/* and the data */
memcpy(
proxy->rsp.buf + NBD_REPLY_SIZE,
prefetch_offset( proxy->prefetch, req->from ),
req->len
);
memcpy(proxy->rsp.buf + NBD_REPLY_SIZE,
prefetch_offset(proxy->prefetch, req->from), req->len);
proxy->rsp.size = NBD_REPLY_SIZE + req->len;
proxy->rsp.needle = 0;
@@ -352,14 +342,14 @@ int proxy_prefetch_for_request( struct proxier* proxy, int state )
/* return early, our work here is done */
return WRITE_TO_DOWNSTREAM;
}
}
else {
} else {
/* Safety catch. If we're sending a write request, we
* blow away the cache. This is very pessimistic, but
* it's simpler (and therefore safer) than working out
* whether we can keep it or not.
*/
debug( "Blowing away prefetch cache on type %d request.", req->type );
debug("Blowing away prefetch cache on type %d request.",
req->type);
prefetch_set_is_empty(proxy->prefetch);
}
@@ -407,13 +397,12 @@ int proxy_prefetch_for_reply( struct proxier* proxy, int state )
prefetched_bytes = proxy->req_hdr.len - proxy->prefetch_req_orig_len;
debug("Prefetched additional %d bytes", prefetched_bytes);
memcpy(
proxy->prefetch->buffer,
memcpy(proxy->prefetch->buffer,
proxy->rsp.buf + proxy->prefetch_req_orig_len + NBD_REPLY_SIZE,
prefetched_bytes
);
prefetched_bytes);
proxy->prefetch->from = proxy->req_hdr.from + proxy->prefetch_req_orig_len;
proxy->prefetch->from =
proxy->req_hdr.from + proxy->prefetch_req_orig_len;
proxy->prefetch->len = prefetched_bytes;
/* We've finished with proxy->req by now, so don't need to alter it to make
@@ -436,12 +425,14 @@ int proxy_read_from_downstream( struct proxier *proxy, int state )
{
ssize_t count;
struct nbd_request_raw* request_raw = (struct nbd_request_raw*) proxy->req.buf;
struct nbd_request_raw *request_raw =
(struct nbd_request_raw *) proxy->req.buf;
struct nbd_request *request = &(proxy->req_hdr);
// assert( state == READ_FROM_DOWNSTREAM );
count = iobuf_read( proxy->downstream_fd, &proxy->req, NBD_REQUEST_SIZE );
count =
iobuf_read(proxy->downstream_fd, &proxy->req, NBD_REQUEST_SIZE);
if (count == -1) {
warn(SHOW_ERRNO("Couldn't read request from downstream"));
@@ -462,13 +453,15 @@ int proxy_read_from_downstream( struct proxier *proxy, int state )
*/
if (request->type == REQUEST_READ) {
if (request->len > NBD_MAX_SIZE) {
warn( "NBD read request size %"PRIu32" too large", request->len );
warn("NBD read request size %" PRIu32 " too large",
request->len);
return EXIT;
}
}
if (request->type == REQUEST_WRITE) {
if (request->len > NBD_MAX_SIZE) {
warn( "NBD write request size %"PRIu32" too large", request->len );
warn("NBD write request size %" PRIu32 " too large",
request->len);
return EXIT;
}
@@ -477,10 +470,9 @@ int proxy_read_from_downstream( struct proxier *proxy, int state )
}
if (proxy->req.needle == proxy->req.size) {
debug(
"Received NBD request from downstream. type=%"PRIu16" flags=%"PRIu16" from=%"PRIu64" len=%"PRIu32,
request->type, request->flags, request->from, request->len
);
debug("Received NBD request from downstream. type=%" PRIu16
" flags=%" PRIu16 " from=%" PRIu64 " len=%" PRIu32,
request->type, request->flags, request->from, request->len);
/* Finished reading, so advance state. Leave size untouched so the next
* state knows how many bytes to write */
@@ -498,9 +490,8 @@ int proxy_continue_connecting_to_upstream( struct proxier* proxy, int state )
// assert( state == CONNECT_TO_UPSTREAM );
result = getsockopt(
proxy->upstream_fd, SOL_SOCKET, SO_ERROR, &error, &len
);
result =
getsockopt(proxy->upstream_fd, SOL_SOCKET, SO_ERROR, &error, &len);
if (result == -1) {
warn(SHOW_ERRNO("Failed to tell if connected to upstream"));
@@ -526,7 +517,9 @@ int proxy_read_init_from_upstream( struct proxier* proxy, int state )
// assert( state == READ_INIT_FROM_UPSTREAM );
count = iobuf_read( proxy->upstream_fd, &proxy->init, sizeof( struct nbd_init_raw ) );
count =
iobuf_read(proxy->upstream_fd, &proxy->init,
sizeof(struct nbd_init_raw));
if (count == -1) {
warn(SHOW_ERRNO("Failed to read init from upstream"));
@@ -536,13 +529,16 @@ int proxy_read_init_from_upstream( struct proxier* proxy, int state )
if (proxy->init.needle == proxy->init.size) {
uint64_t upstream_size;
uint32_t upstream_flags;
if ( !nbd_check_hello( (struct nbd_init_raw*) proxy->init.buf, &upstream_size, &upstream_flags ) ) {
if (!nbd_check_hello
((struct nbd_init_raw *) proxy->init.buf, &upstream_size,
&upstream_flags)) {
warn("Upstream sent invalid init");
goto disconnect;
}
/* record the flags, and log the reconnection, set TCP_NODELAY */
proxy_finish_connect_to_upstream( proxy, upstream_size, upstream_flags );
proxy_finish_connect_to_upstream(proxy, upstream_size,
upstream_flags);
/* Currently, we only get disconnected from upstream (so needing to come
* here) when we have an outstanding request. If that becomes false,
@@ -607,7 +603,8 @@ int proxy_read_from_upstream( struct proxier* proxy, int state )
ssize_t count;
struct nbd_reply *reply = &(proxy->rsp_hdr);
struct nbd_reply_raw* reply_raw = (struct nbd_reply_raw*) proxy->rsp.buf;
struct nbd_reply_raw *reply_raw =
(struct nbd_reply_raw *) proxy->rsp.buf;
/* We can't assume the NBD_REPLY_SIZE + req->len is what we'll get back */
count = iobuf_read(proxy->upstream_fd, &proxy->rsp, NBD_REPLY_SIZE);
@@ -701,7 +698,8 @@ void proxy_session( struct proxier* proxy )
/* First action: Write hello to downstream */
nbd_hello_to_buf( (struct nbd_init_raw *) proxy->rsp.buf, proxy->upstream_size, proxy->upstream_flags );
nbd_hello_to_buf((struct nbd_init_raw *) proxy->rsp.buf,
proxy->upstream_size, proxy->upstream_flags);
proxy->rsp.size = sizeof(struct nbd_init_raw);
proxy->rsp.needle = 0;
state = WRITE_TO_DOWNSTREAM;
@@ -729,13 +727,13 @@ void proxy_session( struct proxier* proxy )
if (state != old_state) {
state_started = monotonic_time_ms();
debug(
"State transition from %s to %s",
debug("State transition from %s to %s",
proxy_session_state_names[old_state],
proxy_session_state_names[state]
);
} else {
debug( "Proxy is in state %s", proxy_session_state_names[state], state );
debug("Proxy is in state %s", proxy_session_state_names[state],
state);
}
old_state = state;
@@ -780,7 +778,9 @@ void proxy_session( struct proxier* proxy )
select_timeout_ptr = &select_timeout;
}
result = sock_try_select( FD_SETSIZE, &rfds, &wfds, NULL, select_timeout_ptr );
result =
sock_try_select(FD_SETSIZE, &rfds, &wfds, NULL,
select_timeout_ptr);
if (result == -1) {
warn(SHOW_ERRNO("select() failed: "));
@@ -806,7 +806,8 @@ void proxy_session( struct proxier* proxy )
break;
case CONNECT_TO_UPSTREAM:
if (FD_ISSET(proxy->upstream_fd, &wfds)) {
state = proxy_continue_connecting_to_upstream( proxy, state );
state =
proxy_continue_connecting_to_upstream(proxy, state);
}
/* Leaving state untouched will retry connecting to upstream -
* so introduce a bit of sleep */
@@ -848,9 +849,7 @@ void proxy_session( struct proxier* proxy )
*/
if (old_state == state && proxy_state_upstream(state)) {
if ((monotonic_time_ms()) - state_started > UPSTREAM_TIMEOUT) {
warn(
"Timed out in state %s while communicating with upstream",
proxy_session_state_names[state]
warn("Timed out in state %s while communicating with upstream", proxy_session_state_names[state]
);
state = CONNECT_TO_UPSTREAM;
@@ -864,10 +863,8 @@ void proxy_session( struct proxier* proxy )
}
}
info(
"Finished proxy session on fd %i after %"PRIu64" successful request(s)",
proxy->downstream_fd, proxy->req_count
);
info("Finished proxy session on fd %i after %" PRIu64
" successful request(s)", proxy->downstream_fd, proxy->req_count);
/* Reset these two for the next session */
proxy->req_count = 0;
@@ -892,13 +889,13 @@ int proxy_accept( struct proxier* params )
FD_ZERO(&fds);
FD_SET(params->listen_fd, &fds);
FATAL_IF_NEGATIVE(
sock_try_select(FD_SETSIZE, &fds, NULL, NULL, NULL),
FATAL_IF_NEGATIVE(sock_try_select(FD_SETSIZE, &fds, NULL, NULL, NULL),
SHOW_ERRNO("select() failed")
);
if (FD_ISSET(params->listen_fd, &fds)) {
client_fd = accept( params->listen_fd, &client_address.generic, &socklen );
client_fd =
accept(params->listen_fd, &client_address.generic, &socklen);
if (client_address.family != AF_UNIX) {
if (sock_set_tcp_nodelay(client_fd, 1) == -1) {
@@ -911,11 +908,9 @@ int proxy_accept( struct proxier* params )
params->downstream_fd = client_fd;
proxy_session(params);
WARN_IF_NEGATIVE(
sock_try_close( params->downstream_fd ),
WARN_IF_NEGATIVE(sock_try_close(params->downstream_fd),
"Couldn't close() downstram fd %i after proxy session",
params->downstream_fd
);
params->downstream_fd);
params->downstream_fd = -1;
}
@@ -940,33 +935,31 @@ void proxy_cleanup( struct proxier* proxy )
if (AF_UNIX == proxy->listen_on.family) {
if (-1 == unlink(proxy->listen_on.un.sun_path)) {
warn( SHOW_ERRNO( "Failed to unlink %s", proxy->listen_on.un.sun_path ) );
warn(SHOW_ERRNO
("Failed to unlink %s",
proxy->listen_on.un.sun_path));
}
}
WARN_IF_NEGATIVE(
sock_try_close( proxy->listen_fd ),
SHOW_ERRNO( "Failed to close() listen fd %i", proxy->listen_fd )
WARN_IF_NEGATIVE(sock_try_close(proxy->listen_fd),
SHOW_ERRNO("Failed to close() listen fd %i",
proxy->listen_fd)
);
proxy->listen_fd = -1;
}
if (-1 != proxy->downstream_fd) {
WARN_IF_NEGATIVE(
sock_try_close( proxy->downstream_fd ),
SHOW_ERRNO(
"Failed to close() downstream fd %i", proxy->downstream_fd
)
WARN_IF_NEGATIVE(sock_try_close(proxy->downstream_fd),
SHOW_ERRNO("Failed to close() downstream fd %i",
proxy->downstream_fd)
);
proxy->downstream_fd = -1;
}
if (-1 != proxy->upstream_fd) {
WARN_IF_NEGATIVE(
sock_try_close( proxy->upstream_fd ),
SHOW_ERRNO(
"Failed to close() upstream fd %i", proxy->upstream_fd
)
WARN_IF_NEGATIVE(sock_try_close(proxy->upstream_fd),
SHOW_ERRNO("Failed to close() upstream fd %i",
proxy->upstream_fd)
);
proxy->upstream_fd = -1;
}
@@ -993,4 +986,3 @@ int do_proxy( struct proxier* params )
return 0;
}

View File

@@ -85,16 +85,13 @@ struct proxier {
/** */
};
struct proxier* proxy_create(
char* s_downstream_address,
struct proxier *proxy_create(char *s_downstream_address,
char *s_downstream_port,
char *s_upstream_address,
char *s_upstream_port,
char* s_upstream_bind,
char* s_cache_bytes);
char *s_upstream_bind, char *s_cache_bytes);
int do_proxy(struct proxier *proxy);
void proxy_cleanup(struct proxier *proxy);
void proxy_destroy(struct proxier *proxy);
#endif

View File

@@ -22,7 +22,9 @@ static int testmasks[9] = { 0,128,192,224,240,248,252,254,255 };
/** Test whether AF_INET or AF_INET6 sockaddr is included in the given access
* control list, returning 1 if it is, and 0 if not.
*/
static int is_included_in_acl(int list_length, struct ip_and_mask (*list)[], union mysockaddr* test)
static int is_included_in_acl(int list_length,
struct ip_and_mask (*list)[],
union mysockaddr *test)
{
NULLCHECK(test);
@@ -33,7 +35,8 @@ static int is_included_in_acl(int list_length, struct ip_and_mask (*list)[], uni
int testbits;
unsigned char *raw_address1 = NULL, *raw_address2 = NULL;
debug("checking acl entry %d (%d/%d)", i, test->generic.sa_family, entry->ip.family);
debug("checking acl entry %d (%d/%d)", i, test->generic.sa_family,
entry->ip.family);
if (test->generic.sa_family != entry->ip.family) {
continue;
@@ -43,24 +46,24 @@ static int is_included_in_acl(int list_length, struct ip_and_mask (*list)[], uni
debug("it's an AF_INET");
raw_address1 = (unsigned char *) &test->v4.sin_addr;
raw_address2 = (unsigned char *) &entry->ip.v4.sin_addr;
}
else if (test->generic.sa_family == AF_INET6) {
} else if (test->generic.sa_family == AF_INET6) {
debug("it's an AF_INET6");
raw_address1 = (unsigned char *) &test->v6.sin6_addr;
raw_address2 = (unsigned char *) &entry->ip.v6.sin6_addr;
}
else {
} else {
fatal("Can't check an ACL for this address type.");
}
debug("testbits=%d", entry->mask);
for (testbits = entry->mask; testbits > 0; testbits -= 8) {
debug("testbits=%d, c1=%02x, c2=%02x", testbits, raw_address1[0], raw_address2[0]);
debug("testbits=%d, c1=%02x, c2=%02x", testbits,
raw_address1[0], raw_address2[0]);
if (testbits >= 8) {
if (raw_address1[0] != raw_address2[0]) { goto no_match; }
if (raw_address1[0] != raw_address2[0]) {
goto no_match;
}
else {
} else {
if ((raw_address1[0] & testmasks[testbits % 8]) !=
(raw_address2[0] & testmasks[testbits % 8])) {
goto no_match;
@@ -86,8 +89,7 @@ int acl_includes( struct acl * acl, union mysockaddr * addr )
if (0 == acl->len) {
return !(acl->default_deny);
}
else {
} else {
return is_included_in_acl(acl->len, acl->entries, addr);
}
}
@@ -105,4 +107,3 @@ void acl_destroy( struct acl * acl )
acl->entries = NULL;
free(acl);
}

View File

@@ -30,30 +30,41 @@ typedef bitfield_word_t * bitfield_p;
((_bytes + (BITFIELD_WORD_SIZE-1)) / BITFIELD_WORD_SIZE)
/** Return the bit value ''idx'' in array ''b'' */
static inline int bit_get(bitfield_p b, uint64_t idx) {
static inline int bit_get(bitfield_p b, uint64_t idx)
{
return (BIT_WORD(b, idx) >> (idx & (BITS_PER_WORD - 1))) & 1;
}
/** Return 1 if the bit at ''idx'' in array ''b'' is set */
static inline int bit_is_set(bitfield_p b, uint64_t idx) {
static inline int bit_is_set(bitfield_p b, uint64_t idx)
{
return bit_get(b, idx);
}
/** Return 1 if the bit at ''idx'' in array ''b'' is clear */
static inline int bit_is_clear(bitfield_p b, uint64_t idx) {
static inline int bit_is_clear(bitfield_p b, uint64_t idx)
{
return !bit_get(b, idx);
}
/** Tests whether the bit at ''idx'' in array ''b'' has value ''value'' */
static inline int bit_has_value(bitfield_p b, uint64_t idx, int value) {
static inline int bit_has_value(bitfield_p b, uint64_t idx, int value)
{
return bit_get(b, idx) == ! !value;
}
/** Sets the bit ''idx'' in array ''b'' */
static inline void bit_set(bitfield_p b, uint64_t idx) {
static inline void bit_set(bitfield_p b, uint64_t idx)
{
BIT_WORD(b, idx) |= BIT_MASK(idx);
}
/** Clears the bit ''idx'' in array ''b'' */
static inline void bit_clear(bitfield_p b, uint64_t idx) {
static inline void bit_clear(bitfield_p b, uint64_t idx)
{
BIT_WORD(b, idx) &= ~BIT_MASK(idx);
}
/** Sets ''len'' bits in array ''b'' starting at offset ''from'' */
static inline void bit_set_range(bitfield_p b, uint64_t from, uint64_t len)
{
@@ -72,8 +83,10 @@ static inline void bit_set_range(bitfield_p b, uint64_t from, uint64_t len)
bit_set(b, from++);
}
}
/** Clears ''len'' bits in array ''b'' starting at offset ''from'' */
static inline void bit_clear_range(bitfield_p b, uint64_t from, uint64_t len)
static inline void bit_clear_range(bitfield_p b, uint64_t from,
uint64_t len)
{
for (; (from % BITS_PER_WORD) != 0 && len > 0; len--) {
bit_clear(b, from++);
@@ -96,7 +109,9 @@ static inline void bit_clear_range(bitfield_p b, uint64_t from, uint64_t len)
* bits that are the same as the first one specified. If ''run_is_set'' is
* non-NULL, the value of that bit is placed into it.
*/
static inline uint64_t bit_run_count(bitfield_p b, uint64_t from, uint64_t len, int *run_is_set) {
static inline uint64_t bit_run_count(bitfield_p b, uint64_t from,
uint64_t len, int *run_is_set)
{
uint64_t count = 0;
int first_value = bit_get(b, from);
bitfield_word_t word_match = first_value ? -1 : 0;
@@ -185,8 +200,10 @@ static inline struct bitset *bitset_alloc( uint64_t size, int resolution )
// calculate a size to allocate that is a multiple of the size of the
// bitfield word
size_t bitfield_size =
BIT_WORDS_FOR_SIZE((( size + resolution - 1 ) / resolution)) * sizeof( bitfield_word_t );
struct bitset *bitset = xmalloc(sizeof( struct bitset ) + ( bitfield_size / 8 ) );
BIT_WORDS_FOR_SIZE(((size + resolution -
1) / resolution)) * sizeof(bitfield_word_t);
struct bitset *bitset =
xmalloc(sizeof(struct bitset) + (bitfield_size / 8));
bitset->size = size;
bitset->resolution = resolution;
@@ -224,12 +241,9 @@ static inline void bitset_free( struct bitset * set )
FATAL_IF_NEGATIVE(pthread_mutex_unlock(&set->lock), "Error unlocking bitset")
static inline void bitset_stream_enqueue(
struct bitset * set,
static inline void bitset_stream_enqueue(struct bitset *set,
enum bitset_stream_events event,
uint64_t from,
uint64_t len
)
uint64_t from, uint64_t len)
{
struct bitset_stream *stream = set->stream;
@@ -254,10 +268,8 @@ static inline void bitset_stream_enqueue(
return;
}
static inline void bitset_stream_dequeue(
struct bitset * set,
struct bitset_stream_entry * out
)
static inline void bitset_stream_dequeue(struct bitset *set,
struct bitset_stream_entry *out)
{
struct bitset_stream *stream = set->stream;
struct bitset_stream_entry *dequeued;
@@ -298,10 +310,9 @@ static inline size_t bitset_stream_size( struct bitset * set )
return size;
}
static inline uint64_t bitset_stream_queued_bytes(
struct bitset * set,
enum bitset_stream_events event
)
static inline uint64_t bitset_stream_queued_bytes(struct bitset *set,
enum bitset_stream_events
event)
{
uint64_t total;
@@ -331,10 +342,8 @@ static inline void bitset_disable_stream( struct bitset * set )
/** Set the bits in a bitset which correspond to the given bytes in the larger
* file.
*/
static inline void bitset_set_range(
struct bitset * set,
uint64_t from,
uint64_t len)
static inline void bitset_set_range(struct bitset *set,
uint64_t from, uint64_t len)
{
INT_FIRST_AND_LAST;
BITSET_LOCK;
@@ -357,10 +366,8 @@ static inline void bitset_set( struct bitset * set )
/** Clear the bits in a bitset which correspond to the given bytes in the
* larger file.
*/
static inline void bitset_clear_range(
struct bitset * set,
uint64_t from,
uint64_t len)
static inline void bitset_clear_range(struct bitset *set,
uint64_t from, uint64_t len)
{
INT_FIRST_AND_LAST;
BITSET_LOCK;
@@ -383,12 +390,9 @@ static inline void bitset_clear( struct bitset * set )
/** As per bitset_run_count but also tells you whether the run it found was set
* or unset, atomically.
*/
static inline uint64_t bitset_run_count_ex(
struct bitset * set,
static inline uint64_t bitset_run_count_ex(struct bitset *set,
uint64_t from,
uint64_t len,
int* run_is_set
)
uint64_t len, int *run_is_set)
{
uint64_t run;
@@ -401,7 +405,9 @@ static inline uint64_t bitset_run_count_ex(
INT_FIRST_AND_LAST;
BITSET_LOCK;
run = bit_run_count(set->bits, first, bitlen, run_is_set) * set->resolution;
run =
bit_run_count(set->bits, first, bitlen,
run_is_set) * set->resolution;
run -= (from % set->resolution);
BITSET_UNLOCK;
@@ -411,10 +417,8 @@ static inline uint64_t bitset_run_count_ex(
/** Counts the number of contiguous bytes that are represented as a run in
* the bit field.
*/
static inline uint64_t bitset_run_count(
struct bitset * set,
uint64_t from,
uint64_t len)
static inline uint64_t bitset_run_count(struct bitset *set,
uint64_t from, uint64_t len)
{
return bitset_run_count_ex(set, from, len, NULL);
}
@@ -435,4 +439,3 @@ static inline int bitset_is_set_at( struct bitset * set, uint64_t at )
#endif

View File

@@ -18,14 +18,16 @@
// When this signal is invoked, we call shutdown() on the client fd, which
// results in the thread being wound up
void client_killswitch_hit(int signal __attribute__ ((unused)), siginfo_t *info, void *ptr __attribute__ ((unused)))
void client_killswitch_hit(int signal
__attribute__ ((unused)), siginfo_t * info,
void *ptr __attribute__ ((unused)))
{
int fd = info->si_value.sival_int;
warn("Killswitch for fd %i activated, calling shutdown on socket", fd);
FATAL_IF(
-1 == shutdown( fd, SHUT_RDWR ),
SHOW_ERRNO( "Failed to shutdown() the socket, killing the server" )
FATAL_IF(-1 == shutdown(fd, SHUT_RDWR),
SHOW_ERRNO
("Failed to shutdown() the socket, killing the server")
);
}
@@ -53,8 +55,8 @@ struct client *client_create( struct server *serve, int socket )
c->stop_signal = self_pipe_create();
FATAL_IF_NEGATIVE(
timer_create( CLOCK_MONOTONIC, &evp, &(c->killswitch) ),
FATAL_IF_NEGATIVE(timer_create
(CLOCK_MONOTONIC, &evp, &(c->killswitch)),
SHOW_ERRNO("Failed to create killswitch timer")
);
@@ -67,7 +69,8 @@ void client_signal_stop( struct client *c)
{
NULLCHECK(c);
debug("client %p: signal stop (%d, %d)", c,c->stop_signal->read_fd, c->stop_signal->write_fd );
debug("client %p: signal stop (%d, %d)", c, c->stop_signal->read_fd,
c->stop_signal->write_fd);
self_pipe_signal(c->stop_signal);
}
@@ -75,8 +78,7 @@ void client_destroy( struct client *client )
{
NULLCHECK(client);
FATAL_IF_NEGATIVE(
timer_delete( client->killswitch ),
FATAL_IF_NEGATIVE(timer_delete(client->killswitch),
SHOW_ERRNO("Couldn't delete killswitch")
);
@@ -119,7 +121,8 @@ void write_not_zeroes(struct client* client, uint64_t from, uint64_t len)
uint64_t run = bitset_run_count(map, from, len);
debug("write_not_zeroes: from=%ld, len=%d, run=%d", from, len, run);
debug("write_not_zeroes: from=%ld, len=%d, run=%d", from, len,
run);
if (run > len) {
run = len;
@@ -164,8 +167,7 @@ void write_not_zeroes(struct client* client, uint64_t from, uint64_t len)
bitset_set_range(map, from, run);
len -= run;
from += run;
}
else {
} else {
char zerobuffer[block_allocation_resolution];
/* not allocated, read in block_allocation_resoution */
while (run > 0) {
@@ -183,7 +185,8 @@ void write_not_zeroes(struct client* client, uint64_t from, uint64_t len)
*/
int all_zeros = (zerobuffer[0] == 0) &&
(0 == memcmp( zerobuffer, zerobuffer+1, blockrun-1 ));
(0 ==
memcmp(zerobuffer, zerobuffer + 1, blockrun - 1));
if (!all_zeros) {
memcpy(client->mapped + from, zerobuffer, blockrun);
@@ -217,7 +220,8 @@ int fd_read_request( int fd, struct nbd_request_raw *out_request)
/* Returns 1 if *request was filled with a valid request which we should
* try to honour. 0 otherwise. */
int client_read_request( struct client * client , struct nbd_request *out_request, int * disconnected )
int client_read_request(struct client *client,
struct nbd_request *out_request, int *disconnected)
{
NULLCHECK(client);
NULLCHECK(out_request);
@@ -231,12 +235,10 @@ int client_read_request( struct client * client , struct nbd_request *out_reques
warn("EOF while reading request");
return 0;
case ECONNRESET:
warn( "Connection reset while"
" reading request" );
warn("Connection reset while" " reading request");
return 0;
case ETIMEDOUT:
warn( "Connection timed out while"
" reading request" );
warn("Connection timed out while" " reading request");
return 0;
default:
/* FIXME: I've seen this happen, but I
@@ -246,9 +248,7 @@ int client_read_request( struct client * client , struct nbd_request *out_reques
* again. It should *probably* be an
* error() call, but I want to be sure.
* */
fatal("Error reading request: %d, %s",
errno,
strerror( errno ));
fatal("Error reading request: %d, %s", errno, strerror(errno));
}
}
@@ -293,7 +293,8 @@ int fd_write_reply( int fd, uint64_t handle, int error )
* Returns 1; we don't check for errors on the write.
* TODO: Check for errors on the write.
*/
int client_write_reply( struct client * client, struct nbd_request *request, int error )
int client_write_reply(struct client *client, struct nbd_request *request,
int error)
{
return fd_write_reply(client->socket, request->handle.w, error);
}
@@ -315,10 +316,9 @@ void client_write_init( struct client * client, uint64_t size )
nbd_h2r_init(&init, &init_raw);
ERROR_IF_NEGATIVE(
writeloop(client->socket, &init_raw, sizeof(init_raw)),
"Couldn't send hello"
);
ERROR_IF_NEGATIVE(writeloop
(client->socket, &init_raw, sizeof(init_raw)),
"Couldn't send hello");
}
@@ -338,23 +338,15 @@ void client_flush( struct client * client, size_t len )
size_t spliced = 0;
while (spliced < len) {
ssize_t received = splice(
client->socket, NULL,
ssize_t received = splice(client->socket, NULL,
pipes[1], NULL,
len - spliced, flags);
FATAL_IF_NEGATIVE( received,
"splice error: %s",
strerror(errno));
FATAL_IF_NEGATIVE(received, "splice error: %s", strerror(errno));
ssize_t junked = 0;
while (junked < received) {
ssize_t junk;
junk = splice(
pipes[0], NULL,
devnull, NULL,
received, flags );
FATAL_IF_NEGATIVE( junk,
"splice error: %s",
strerror(errno));
junk = splice(pipes[0], NULL, devnull, NULL, received, flags);
FATAL_IF_NEGATIVE(junk, "splice error: %s", strerror(errno));
junked += junk;
}
spliced += received;
@@ -386,18 +378,16 @@ int client_request_needs_reply( struct client * client,
return 0;
}
debug(
"request type=%"PRIu16", flags=%"PRIu16", from=%"PRIu64", len=%"PRIu32", handle=0x%08X",
request.type, request.flags, request.from, request.len, request.handle
);
debug("request type=%" PRIu16 ", flags=%" PRIu16 ", from=%" PRIu64
", len=%" PRIu32 ", handle=0x%08X", request.type, request.flags,
request.from, request.len, request.handle);
/* check it's not out of range. NBD protocol requires ENOSPC to be
* returned in this instance
*/
if (request.from + request.len > client->serve->size) {
warn("write request %" PRIu64 "+%" PRIu32 " out of range",
request.from, request.len
);
request.from, request.len);
if (request.type == REQUEST_WRITE) {
client_flush(client, request.len);
}
@@ -407,8 +397,7 @@ int client_request_needs_reply( struct client * client,
}
switch (request.type)
{
switch (request.type) {
case REQUEST_READ:
break;
case REQUEST_WRITE:
@@ -431,7 +420,8 @@ int client_request_needs_reply( struct client * client,
}
void client_reply_to_read( struct client* client, struct nbd_request request )
void client_reply_to_read(struct client *client,
struct nbd_request request)
{
off64_t offset;
@@ -444,72 +434,69 @@ void client_reply_to_read( struct client* client, struct nbd_request request )
/* If we get cut off partway through this sendfile, we don't
* want to kill the server. This should be an error.
*/
ERROR_IF_NEGATIVE(
sendfileloop(
client->socket,
ERROR_IF_NEGATIVE(sendfileloop(client->socket,
client->fileno,
&offset,
request.len),
"sendfile failed from=%ld, len=%d",
offset,
request.len);
offset, request.len);
sock_set_tcp_cork(client->socket, 0);
}
void client_reply_to_write( struct client* client, struct nbd_request request )
void client_reply_to_write(struct client *client,
struct nbd_request request)
{
debug("request write from=%"PRIu64", len=%"PRIu32", handle=0x%08X", request.from, request.len, request.handle);
debug("request write from=%" PRIu64 ", len=%" PRIu32 ", handle=0x%08X",
request.from, request.len, request.handle);
if (client->serve->allocation_map_built) {
write_not_zeroes(client, request.from, request.len);
}
else {
} else {
debug("No allocation map, writing directly.");
/* If we get cut off partway through reading this data:
* */
ERROR_IF_NEGATIVE(
readloop( client->socket,
ERROR_IF_NEGATIVE(readloop(client->socket,
client->mapped + request.from,
request.len),
"reading write data failed from=%ld, len=%d",
request.from,
request.len
);
request.from, request.len);
/* the allocation_map is shared between client threads, and may be
* being built. We need to reflect the write in it, as it may be in
* a position the builder has already gone over.
*/
bitset_set_range(client->serve->allocation_map, request.from, request.len);
bitset_set_range(client->serve->allocation_map, request.from,
request.len);
}
// Only flush if FUA is set
if (request.flags & CMD_FLAG_FUA)
{
if (request.flags & CMD_FLAG_FUA) {
/* multiple of page size */
uint64_t from_rounded = request.from & (~(sysconf(_SC_PAGE_SIZE)-1));
uint64_t from_rounded =
request.from & (~(sysconf(_SC_PAGE_SIZE) - 1));
uint64_t len_rounded = request.len + (request.from - from_rounded);
debug("Calling msync from=%"PRIu64", len=%"PRIu64"",from_rounded, len_rounded);
debug("Calling msync from=%" PRIu64 ", len=%" PRIu64 "",
from_rounded, len_rounded);
FATAL_IF_NEGATIVE(
msync( client->mapped + from_rounded,
FATAL_IF_NEGATIVE(msync(client->mapped + from_rounded,
len_rounded,
MS_SYNC | MS_INVALIDATE),
"msync failed %ld %ld", request.from, request.len
);
"msync failed %ld %ld", request.from,
request.len);
}
client_write_reply(client, &request, 0);
}
void client_reply_to_flush( struct client* client, struct nbd_request request )
void client_reply_to_flush(struct client *client,
struct nbd_request request)
{
debug("request flush from=%"PRIu64", len=%"PRIu32", handle=0x%08X", request.from, request.len, request.handle);
debug("request flush from=%" PRIu64 ", len=%" PRIu32 ", handle=0x%08X",
request.from, request.len, request.handle);
ERROR_IF_NEGATIVE(
msync(client->mapped, client->mapped_size, MS_SYNC | MS_INVALIDATE),
"flush failed"
);
ERROR_IF_NEGATIVE(msync
(client->mapped, client->mapped_size,
MS_SYNC | MS_INVALIDATE), "flush failed");
client_write_reply(client, &request, 0);
}
@@ -546,8 +533,7 @@ void client_arm_killswitch( struct client* client )
debug("Arming killswitch");
FATAL_IF_NEGATIVE(
timer_settime( client->killswitch, 0, &its, NULL ),
FATAL_IF_NEGATIVE(timer_settime(client->killswitch, 0, &its, NULL),
SHOW_ERRNO("Failed to arm killswitch")
);
@@ -567,8 +553,7 @@ void client_disarm_killswitch( struct client* client )
debug("Disarming killswitch");
FATAL_IF_NEGATIVE(
timer_settime( client->killswitch, 0, &its, NULL ),
FATAL_IF_NEGATIVE(timer_settime(client->killswitch, 0, &its, NULL),
SHOW_ERRNO("Failed to disarm killswitch")
);
@@ -604,8 +589,9 @@ int client_serve_request(struct client* client)
if (fd_count == 0) {
/* This "can't ever happen" */
fatal("No FDs selected, and no timeout!");
} else if (fd_count < 0) {
fatal("Select failed");
}
else if ( fd_count < 0 ) { fatal( "Select failed" ); }
if (self_pipe_fd_isset(client->stop_signal, &rfds)) {
debug("Client received stop signal.");
@@ -683,13 +669,14 @@ void client_cleanup(struct client* client,
}
if (client->fileno) {
FATAL_IF_NEGATIVE(close(client->fileno),
"Error closing file %d",
client->fileno );
"Error closing file %d", client->fileno);
debug("Closed client file fd %d", client->fileno);
client->fileno = -1;
}
if ( server_acl_locked( client->serve ) ) { server_unlock_acl( client->serve ); }
if (server_acl_locked(client->serve)) {
server_unlock_acl(client->serve);
}
}
@@ -700,19 +687,18 @@ void* client_serve(void* client_uncast)
error_set_handler((cleanup_handler *) client_cleanup, client);
info("client: mmaping file");
FATAL_IF_NEGATIVE(
open_and_mmap(
client->serve->filename,
FATAL_IF_NEGATIVE(open_and_mmap(client->serve->filename,
&client->fileno,
&client->mapped_size,
(void**) &client->mapped
),
"Couldn't open/mmap file %s: %s", client->serve->filename, strerror( errno )
(void **) &client->mapped),
"Couldn't open/mmap file %s: %s",
client->serve->filename, strerror(errno)
);
FATAL_IF_NEGATIVE(
madvise( client->mapped, client->serve->size, MADV_RANDOM ),
SHOW_ERRNO( "Failed to madvise() %s", client->serve->filename )
FATAL_IF_NEGATIVE(madvise
(client->mapped, client->serve->size, MADV_RANDOM),
SHOW_ERRNO("Failed to madvise() %s",
client->serve->filename)
);
debug("Opened client file fd %d", client->fileno);
@@ -720,8 +706,7 @@ void* client_serve(void* client_uncast)
client_send_hello(client);
debug("client: serving requests");
while (client_serve_request(client) == 0)
;
while (client_serve_request(client) == 0);
debug("client: stopped serving requests");
client->stopped = 1;
@@ -730,10 +715,10 @@ void* client_serve(void* client_uncast)
server_control_arrived(client->serve);
}
debug("Cleaning client %p up normally in thread %p", client, pthread_self());
debug("Cleaning client %p up normally in thread %p", client,
pthread_self());
client_cleanup(client, 0);
debug("Client thread done");
return NULL;
}

View File

@@ -56,4 +56,3 @@ void client_destroy( struct client * client );
void client_signal_stop(struct client *client);
#endif

View File

@@ -44,9 +44,7 @@
#include <unistd.h>
struct control * control_create(
struct flexnbd * flexnbd,
const char * csn)
struct control *control_create(struct flexnbd *flexnbd, const char *csn)
{
struct control *control = xmalloc(sizeof(struct control));
@@ -80,8 +78,7 @@ void control_destroy( struct control * control )
free(control);
}
struct control_client * control_client_create(
struct flexnbd * flexnbd,
struct control_client *control_client_create(struct flexnbd *flexnbd,
int client_fd,
struct mbox *state_mbox)
{
@@ -112,8 +109,7 @@ void control_handle_client( struct control * control, int client_fd )
NULLCHECK(control);
NULLCHECK(control->flexnbd);
struct control_client *control_client =
control_client_create(
control->flexnbd,
control_client_create(control->flexnbd,
client_fd,
control->mirror_state_mbox);
@@ -132,7 +128,8 @@ void control_accept_client( struct control * control )
union mysockaddr client_address;
socklen_t addrlen = sizeof(union mysockaddr);
client_fd = accept( control->control_fd, &client_address.generic, &addrlen );
client_fd =
accept(control->control_fd, &client_address.generic, &addrlen);
FATAL_IF(-1 == client_fd, "control accept failed");
control_handle_client(control, client_fd);
@@ -178,25 +175,23 @@ int open_control_socket( const char * socket_name )
}
control_fd = socket(AF_UNIX, SOCK_STREAM, 0);
FATAL_IF_NEGATIVE(control_fd ,
"Couldn't create control socket");
FATAL_IF_NEGATIVE(control_fd, "Couldn't create control socket");
memset(&bind_address, 0, sizeof(struct sockaddr_un));
bind_address.sun_family = AF_UNIX;
strncpy(bind_address.sun_path, socket_name, sizeof(bind_address.sun_path)-1);
strncpy(bind_address.sun_path, socket_name,
sizeof(bind_address.sun_path) - 1);
//unlink(socket_name); /* ignore failure */
FATAL_IF_NEGATIVE(
bind(control_fd , &bind_address, sizeof(bind_address)),
FATAL_IF_NEGATIVE(bind
(control_fd, &bind_address, sizeof(bind_address)),
"Couldn't bind control socket to %s: %s",
socket_name, strerror(errno)
);
FATAL_IF_NEGATIVE(
listen(control_fd , 5),
"Couldn't listen on control socket"
);
FATAL_IF_NEGATIVE(listen(control_fd, 5),
"Couldn't listen on control socket");
return control_fd;
}
@@ -229,8 +224,7 @@ void control_serve( struct control * control )
}
void control_cleanup(
struct control * control,
void control_cleanup(struct control *control,
int fatal __attribute__ ((unused)))
{
NULLCHECK(control);
@@ -256,7 +250,8 @@ void * control_runner( void * control_uncast )
#define write_socket(msg) write(client_fd, (msg "\n"), strlen((msg))+1)
void control_write_mirror_response( enum mirror_state mirror_state, int client_fd )
void control_write_mirror_response(enum mirror_state mirror_state,
int client_fd)
{
switch (mirror_state) {
case MS_INIT:
@@ -286,12 +281,12 @@ void control_write_mirror_response( enum mirror_state mirror_state, int client_f
fatal("Unhandled mirror state: %d", mirror_state);
}
}
#undef write_socket
/* Call this in the thread where you want to receive the mirror state */
enum mirror_state control_client_mirror_wait(
struct control_client* client)
enum mirror_state control_client_mirror_wait(struct control_client *client)
{
NULLCHECK(client);
NULLCHECK(client->mirror_state_mbox);
@@ -345,14 +340,11 @@ int control_mirror(struct control_client* client, int linesc, char** lines)
if (linesc > 2) {
if (strcmp("exit", lines[2]) == 0) {
action_at_finish = ACTION_EXIT;
}
else if (strcmp( "unlink", lines[2]) == 0 ) {
} else if (strcmp("unlink", lines[2]) == 0) {
action_at_finish = ACTION_UNLINK;
}
else if (strcmp("nothing", lines[2]) == 0) {
} else if (strcmp("nothing", lines[2]) == 0) {
action_at_finish = ACTION_NOTHING;
}
else {
} else {
write_socket("1: action must be 'exit' or 'nothing'");
return -1;
}
@@ -389,13 +381,13 @@ int control_mirror(struct control_client* client, int linesc, char** lines)
server_lock_start_mirror(serve);
{
if (server_mirror_can_start(serve)) {
serve->mirror_super = mirror_super_create(
serve->filename,
serve->mirror_super = mirror_super_create(serve->filename,
connect_to,
connect_from,
max_Bps,
action_at_finish,
client->mirror_state_mbox );
client->
mirror_state_mbox);
serve->mirror = serve->mirror_super->mirror;
server_prevent_mirror_start(serve);
} else {
@@ -415,18 +407,14 @@ int control_mirror(struct control_client* client, int linesc, char** lines)
* sighandler can block the serve thread
*/
if (serve->mirror_super) {
FATAL_IF( 0 != pthread_create(
&serve->mirror_super->thread,
FATAL_IF(0 != pthread_create(&serve->mirror_super->thread,
NULL,
mirror_super_runner,
serve
),
"Failed to create mirror thread"
);
serve),
"Failed to create mirror thread");
debug("Control thread mirror super waiting");
enum mirror_state state =
control_client_mirror_wait( client );
enum mirror_state state = control_client_mirror_wait(client);
debug("Control thread writing response");
control_write_mirror_response(state, client->socket);
}
@@ -436,7 +424,8 @@ int control_mirror(struct control_client* client, int linesc, char** lines)
return 0;
}
int control_mirror_max_bps( struct control_client* client, int linesc, char** lines )
int control_mirror_max_bps(struct control_client *client, int linesc,
char **lines)
{
NULLCHECK(client);
NULLCHECK(client->flexnbd);
@@ -489,8 +478,7 @@ int control_acl(struct control_client* client, int linesc, char** lines)
strlen(lines[new_acl->len]));
write(client->socket, "\n", 1);
acl_destroy(new_acl);
}
else {
} else {
flexnbd_replace_acl(flexnbd, new_acl);
info("ACL set");
write(client->socket, "0: updated\n", 11);
@@ -500,8 +488,7 @@ int control_acl(struct control_client* client, int linesc, char** lines)
}
int control_break(
struct control_client* client,
int control_break(struct control_client *client,
int linesc __attribute__ ((unused)),
char **lines __attribute__ ((unused))
)
@@ -524,13 +511,10 @@ int control_break(
if (server_is_closed(serve)) {
info("Mirror completed while canceling");
write( client->socket,
"1: mirror completed\n", 20 );
}
else {
write(client->socket, "1: mirror completed\n", 20);
} else {
info("Mirror successfully stopped.");
write( client->socket,
"0: mirror stopped\n", 18 );
write(client->socket, "0: mirror stopped\n", 18);
result = 1;
}
@@ -546,8 +530,7 @@ int control_break(
/** FIXME: add some useful statistics */
int control_status(
struct control_client* client,
int control_status(struct control_client *client,
int linesc __attribute__ ((unused)),
char **lines __attribute__ ((unused))
)
@@ -566,10 +549,14 @@ int control_status(
void control_client_cleanup(struct control_client *client,
int fatal __attribute__ ((unused)))
{
if (client->socket) { close(client->socket); }
if (client->socket) {
close(client->socket);
}
/* This is wrongness */
if ( server_acl_locked( client->flexnbd->serve ) ) { server_unlock_acl( client->flexnbd->serve ); }
if (server_acl_locked(client->flexnbd->serve)) {
server_unlock_acl(client->flexnbd->serve);
}
control_client_destroy(client);
}
@@ -584,30 +571,25 @@ void control_respond(struct control_client * client)
int i, linesc;
linesc = read_lines_until_blankline(client->socket, 256, &lines);
if (linesc < 1)
{
if (linesc < 1) {
write(client->socket, "9: missing command\n", 19);
/* ignore failure */
}
else if (strcmp(lines[0], "acl") == 0) {
} else if (strcmp(lines[0], "acl") == 0) {
info("acl command received");
if (control_acl(client, linesc - 1, lines + 1) < 0) {
debug("acl command failed");
}
}
else if (strcmp(lines[0], "mirror") == 0) {
} else if (strcmp(lines[0], "mirror") == 0) {
info("mirror command received");
if (control_mirror(client, linesc - 1, lines + 1) < 0) {
debug("mirror command failed");
}
}
else if (strcmp(lines[0], "break") == 0) {
} else if (strcmp(lines[0], "break") == 0) {
info("break command received");
if (control_break(client, linesc - 1, lines + 1) < 0) {
debug("break command failed");
}
}
else if (strcmp(lines[0], "status") == 0) {
} else if (strcmp(lines[0], "status") == 0) {
info("status command received");
if (control_status(client, linesc - 1, lines + 1) < 0) {
debug("status command failed");
@@ -617,8 +599,7 @@ void control_respond(struct control_client * client)
if (control_mirror_max_bps(client, linesc - 1, lines + 1) < 0) {
debug("mirror_max_bps command failed");
}
}
else {
} else {
write(client->socket, "10: unknown command\n", 23);
}
@@ -630,4 +611,3 @@ void control_respond(struct control_client * client)
control_client_cleanup(client, 0);
debug("control command handled");
}

View File

@@ -44,16 +44,15 @@ struct control_client{
struct mbox *mirror_state_mbox;
};
struct control * control_create(
struct flexnbd *,
struct control *control_create(struct flexnbd *,
const char *control_socket_name);
void control_signal_close(struct control *);
void control_destroy(struct control *);
void *control_runner(void *);
void accept_control_connection(struct server* params, int client_fd, union mysockaddr* client_address);
void accept_control_connection(struct server *params, int client_fd,
union mysockaddr *client_address);
void serve_open_control_socket(struct server *params);
#endif

View File

@@ -61,16 +61,13 @@ int flexnbd_build_signal_fd(void)
}
void flexnbd_create_shared(
struct flexnbd * flexnbd,
void flexnbd_create_shared(struct flexnbd *flexnbd,
const char *s_ctrl_sock)
{
NULLCHECK(flexnbd);
if (s_ctrl_sock) {
flexnbd->control =
control_create( flexnbd, s_ctrl_sock );
}
else {
flexnbd->control = control_create(flexnbd, s_ctrl_sock);
} else {
flexnbd->control = NULL;
}
@@ -78,8 +75,7 @@ void flexnbd_create_shared(
}
struct flexnbd * flexnbd_create_serving(
char* s_ip_address,
struct flexnbd *flexnbd_create_serving(char *s_ip_address,
char *s_port,
char *s_file,
char *s_ctrl_sock,
@@ -90,17 +86,14 @@ struct flexnbd * flexnbd_create_serving(
int use_killswitch)
{
struct flexnbd *flexnbd = xmalloc(sizeof(struct flexnbd));
flexnbd->serve = server_create(
flexnbd,
flexnbd->serve = server_create(flexnbd,
s_ip_address,
s_port,
s_file,
default_deny,
acl_entries,
s_acl_entries,
max_nbd_clients,
use_killswitch,
1);
max_nbd_clients, use_killswitch, 1);
flexnbd_create_shared(flexnbd, s_ctrl_sock);
// Beats installing one handler per client instance
@@ -110,17 +103,14 @@ struct flexnbd * flexnbd_create_serving(
.sa_flags = SA_RESTART | SA_SIGINFO
};
FATAL_UNLESS(
0 == sigaction( CLIENT_KILLSWITCH_SIGNAL, &act, NULL ),
"Installing client killswitch signal failed"
);
FATAL_UNLESS(0 == sigaction(CLIENT_KILLSWITCH_SIGNAL, &act, NULL),
"Installing client killswitch signal failed");
}
return flexnbd;
}
struct flexnbd * flexnbd_create_listening(
char* s_ip_address,
struct flexnbd *flexnbd_create_listening(char *s_ip_address,
char *s_port,
char *s_file,
char *s_ctrl_sock,
@@ -129,15 +119,12 @@ struct flexnbd * flexnbd_create_listening(
char **s_acl_entries)
{
struct flexnbd *flexnbd = xmalloc(sizeof(struct flexnbd));
flexnbd->serve = server_create(
flexnbd,
flexnbd->serve = server_create(flexnbd,
s_ip_address,
s_port,
s_file,
default_deny,
acl_entries,
s_acl_entries,
1, 0, 0);
acl_entries, s_acl_entries, 1, 0, 0);
flexnbd_create_shared(flexnbd, s_ctrl_sock);
// listen can't use killswitch, as mirror may pause on sending things
@@ -153,8 +140,7 @@ void flexnbd_spawn_control(struct flexnbd * flexnbd )
pthread_t *control_thread = &flexnbd->control->thread;
FATAL_UNLESS( 0 == pthread_create(
control_thread,
FATAL_UNLESS(0 == pthread_create(control_thread,
NULL,
control_runner,
flexnbd->control),
@@ -233,9 +219,7 @@ void make_writable( const char * filename )
{
NULLCHECK(filename);
FATAL_IF_NEGATIVE(chmod(filename, S_IWUSR),
"Couldn't chmod %s: %s",
filename,
strerror( errno ) );
"Couldn't chmod %s: %s", filename, strerror(errno));
}
@@ -262,4 +246,3 @@ int flexnbd_serve( struct flexnbd * flexnbd )
return success;
}

View File

@@ -29,8 +29,7 @@ struct flexnbd {
struct flexnbd *flexnbd_create(void);
struct flexnbd * flexnbd_create_serving(
char* s_ip_address,
struct flexnbd *flexnbd_create_serving(char *s_ip_address,
char *s_port,
char *s_file,
char *s_ctrl_sock,
@@ -40,8 +39,7 @@ struct flexnbd * flexnbd_create_serving(
int max_nbd_clients,
int use_killswitch);
struct flexnbd * flexnbd_create_listening(
char* s_ip_address,
struct flexnbd *flexnbd_create_listening(char *s_ip_address,
char *s_port,
char *s_file,
char *s_ctrl_sock,
@@ -63,4 +61,3 @@ struct server * flexnbd_server( struct flexnbd * flexnbd );
void flexnbd_replace_acl(struct flexnbd *flexnbd, struct acl *acl);
struct status *flexnbd_status_create(struct flexnbd *flexnbd);
#endif

View File

@@ -22,15 +22,14 @@ void flexthread_mutex_destroy( struct flexthread_mutex * ftm )
if (flexthread_mutex_held(ftm)) {
flexthread_mutex_unlock(ftm);
}
else if ( (pthread_t)NULL != ftm->holder ) {
} else if ((pthread_t) NULL != ftm->holder) {
/* This "should never happen": if we can try to destroy
* a mutex currently held by another thread, there's a
* logic bug somewhere. I know the test here is racy,
* but there's not a lot we can do about it at this
* point.
*/
fatal( "Attempted to destroy a flexthread_mutex"\
fatal("Attempted to destroy a flexthread_mutex"
" held by another thread!");
}
@@ -72,4 +71,3 @@ int flexthread_mutex_held( struct flexthread_mutex * ftm )
NULLCHECK(ftm);
return pthread_self() == ftm->holder;
}

View File

@@ -90,8 +90,7 @@ struct mirror_ctrl {
};
struct mirror * mirror_alloc(
union mysockaddr * connect_to,
struct mirror *mirror_alloc(union mysockaddr *connect_to,
union mysockaddr *connect_from,
uint64_t max_Bps,
enum mirror_finish_action action_at_finish,
@@ -144,19 +143,13 @@ void mirror_init( struct mirror * mirror, const char * filename )
NULLCHECK(mirror);
NULLCHECK(filename);
FATAL_IF_NEGATIVE(
open_and_mmap(
filename,
FATAL_IF_NEGATIVE(open_and_mmap(filename,
&map_fd,
&size,
(void**) &mirror->mapped
),
"Failed to open and mmap %s",
filename
);
(void **) &mirror->mapped),
"Failed to open and mmap %s", filename);
FATAL_IF_NEGATIVE(
madvise( mirror->mapped, size, MADV_SEQUENTIAL ),
FATAL_IF_NEGATIVE(madvise(mirror->mapped, size, MADV_SEQUENTIAL),
SHOW_ERRNO("Failed to madvise() %s", filename)
);
}
@@ -176,8 +169,7 @@ void mirror_reset( struct mirror * mirror )
}
struct mirror * mirror_create(
const char * filename,
struct mirror *mirror_create(const char *filename,
union mysockaddr *connect_to,
union mysockaddr *connect_from,
uint64_t max_Bps,
@@ -189,9 +181,7 @@ struct mirror * mirror_create(
mirror = mirror_alloc(connect_to,
connect_from,
max_Bps,
action_at_finish,
commit_signal);
max_Bps, action_at_finish, commit_signal);
mirror_init(mirror, filename);
mirror_reset(mirror);
@@ -281,7 +271,8 @@ int mirror_connect( struct mirror * mirror, uint64_t local_size )
NULLCHECK(mirror->connect_to);
mirror->client = socket_connect(&mirror->connect_to->generic, connect_from);
mirror->client =
socket_connect(&mirror->connect_to->generic, connect_from);
if (0 < mirror->client) {
fd_set fds;
struct timeval tv = { MS_HELLO_TIME_SECS, 0 };
@@ -294,30 +285,29 @@ int mirror_connect( struct mirror * mirror, uint64_t local_size )
if (FD_ISSET(mirror->client, &fds)) {
uint64_t remote_size;
uint32_t remote_flags;
if ( socket_nbd_read_hello( mirror->client, &remote_size, &remote_flags ) ) {
if (socket_nbd_read_hello
(mirror->client, &remote_size, &remote_flags)) {
if (remote_size == local_size) {
connected = 1;
mirror_set_state(mirror, MS_GO);
}
else {
} else {
warn("Remote size (%d) doesn't match local (%d)",
remote_size, local_size);
mirror_set_state(mirror, MS_FAIL_SIZE_MISMATCH);
}
}
else {
} else {
warn("Mirror attempt rejected.");
mirror_set_state(mirror, MS_FAIL_REJECTED);
}
}
else {
} else {
warn("No NBD Hello received.");
mirror_set_state(mirror, MS_FAIL_NO_HELLO);
}
if ( !connected ) { close( mirror->client ); }
if (!connected) {
close(mirror->client);
}
else {
} else {
warn("Mirror failed to connect.");
mirror_set_state(mirror, MS_FAIL_CONNECT);
}
@@ -372,12 +362,15 @@ int mirror_setup_next_xfer( struct mirror_ctrl *ctrl )
* full, and stop when it's a quarter full. This stops a busy client from
* stalling a migration forever. FIXME: made-up numbers.
*/
if ( mirror->offset < serve->size && bitset_stream_size( serve->allocation_map ) > BITSET_STREAM_SIZE / 2 ) {
if (mirror->offset < serve->size
&& bitset_stream_size(serve->allocation_map) >
BITSET_STREAM_SIZE / 2) {
ctrl->clear_events = 1;
}
while ( ( mirror->offset == serve->size || ctrl->clear_events ) && e.event != BITSET_STREAM_SET ) {
while ((mirror->offset == serve->size || ctrl->clear_events)
&& e.event != BITSET_STREAM_SET) {
uint64_t events = bitset_stream_size(serve->allocation_map);
if (events == 0) {
@@ -409,7 +402,8 @@ int mirror_setup_next_xfer( struct mirror_ctrl *ctrl )
return 0;
}
debug( "Next transfer: current=%"PRIu64", run=%"PRIu64, current, run );
debug("Next transfer: current=%" PRIu64 ", run=%" PRIu64, current,
run);
struct nbd_request req = {
.magic = REQUEST_MAGIC,
.type = REQUEST_WRITE,
@@ -461,7 +455,8 @@ static void mirror_write_cb( struct ev_loop *loop, ev_io *w, int revents )
return;
}
debug( "Mirror write callback invoked with events %d. fd: %i", revents, ctrl->mirror->client );
debug("Mirror write callback invoked with events %d. fd: %i", revents,
ctrl->mirror->client);
/* FIXME: We can end up corking multiple times in unusual circumstances; this
* is annoying, but harmless */
@@ -473,7 +468,8 @@ static void mirror_write_cb( struct ev_loop *loop, ev_io *w, int revents )
data_loc = ((char *) &xfer->hdr.req_raw) + ctrl->xfer.written;
to_write = hdr_size - xfer->written;
} else {
data_loc = ctrl->mirror->mapped + xfer->from + ( xfer->written - hdr_size );
data_loc =
ctrl->mirror->mapped + xfer->from + (xfer->written - hdr_size);
to_write = xfer->len - (ctrl->xfer.written - hdr_size);
}
@@ -486,14 +482,14 @@ static void mirror_write_cb( struct ev_loop *loop, ev_io *w, int revents )
return;
}
debug("Wrote %" PRIu64 " bytes", count);
debug( "to_write was %"PRIu64", xfer->written was %"PRIu64, to_write, xfer->written );
debug("to_write was %" PRIu64 ", xfer->written was %" PRIu64, to_write,
xfer->written);
// We wrote some bytes, so reset the timer and keep track for the next pass
if (count > 0) {
ctrl->xfer.written += count;
ev_timer_again(ctrl->ev_loop, &ctrl->timeout_watcher);
}
// All bytes written, so now we need to read the NBD reply back.
if (ctrl->xfer.written == ctrl->xfer.len + hdr_size) {
sock_set_tcp_cork(ctrl->mirror->client, 0);
@@ -524,10 +520,13 @@ static void mirror_read_cb( struct ev_loop *loop, ev_io *w, int revents )
ssize_t count;
uint64_t left = sizeof(struct nbd_reply_raw) - xfer->read;
debug( "Mirror read callback invoked with events %d. fd:%i", revents, m->client );
debug("Mirror read callback invoked with events %d. fd:%i", revents,
m->client);
/* Start / continue reading the NBD response from the mirror. */
if ( ( count = read( m->client, ((void*) &xfer->hdr.rsp_raw) + xfer->read, left ) ) < 0 ) {
if ((count =
read(m->client, ((void *) &xfer->hdr.rsp_raw) + xfer->read,
left)) < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
warn(SHOW_ERRNO("Couldn't read from listener"));
ev_break(loop, EVBREAK_ONE);
@@ -541,12 +540,12 @@ static void mirror_read_cb( struct ev_loop *loop, ev_io *w, int revents )
ev_break(loop, EVBREAK_ONE);
return;
}
// We read some bytes, so reset the timer
ev_timer_again(ctrl->ev_loop, &ctrl->timeout_watcher);
debug("Read %i bytes", count);
debug( "left was %"PRIu64", xfer->read was %"PRIu64, left, xfer->read );
debug("left was %" PRIu64 ", xfer->read was %" PRIu64, left,
xfer->read);
xfer->read += count;
if (xfer->read < sizeof(struct nbd_reply_raw)) {
@@ -603,7 +602,9 @@ static void mirror_read_cb( struct ev_loop *loop, ev_io *w, int revents )
debug("next_xfer: %d", next_xfer);
/* Regardless of time estimates, if there's no waiting transfer, we can start closing clients down. */
if ( !ctrl->clients_closed && ( !next_xfer || server_mirror_eta( ctrl->serve ) < MS_CONVERGE_TIME_SECS ) ) {
if (!ctrl->clients_closed
&& (!next_xfer
|| server_mirror_eta(ctrl->serve) < MS_CONVERGE_TIME_SECS)) {
info("Closing clients to allow mirroring to converge");
server_forbid_new_clients(ctrl->serve);
server_close_clients(ctrl->serve);
@@ -644,7 +645,8 @@ static void mirror_read_cb( struct ev_loop *loop, ev_io *w, int revents )
return;
}
static void mirror_timeout_cb( struct ev_loop *loop, ev_timer *w __attribute__((unused)), int revents )
static void mirror_timeout_cb(struct ev_loop *loop, ev_timer * w
__attribute__ ((unused)), int revents)
{
if (!(revents & EV_TIMER)) {
warn("Mirror timeout called but no timer event signalled");
@@ -674,7 +676,8 @@ static void mirror_abandon_cb( struct ev_loop *loop, ev_io *w, int revents )
}
static void mirror_limit_cb( struct ev_loop *loop, ev_timer *w, int revents )
static void mirror_limit_cb(struct ev_loop *loop, ev_timer * w,
int revents)
{
struct mirror_ctrl *ctrl = (struct mirror_ctrl *) w->data;
NULLCHECK(ctrl);
@@ -685,7 +688,8 @@ static void mirror_limit_cb( struct ev_loop *loop, ev_timer *w, int revents )
}
if (mirror_should_wait(ctrl)) {
debug( "max_bps exceeded, waiting", ctrl->mirror->max_bytes_per_second );
debug("max_bps exceeded, waiting",
ctrl->mirror->max_bytes_per_second);
ev_timer_again(loop, w);
} else {
/* We're below the limit, so do the next request */
@@ -702,7 +706,8 @@ static void mirror_limit_cb( struct ev_loop *loop, ev_timer *w, int revents )
* if it has, start migrating. If it's not finished, then enabling the bitset
* stream does not go well for us.
*/
static void mirror_begin_cb( struct ev_loop *loop, ev_timer *w, int revents )
static void mirror_begin_cb(struct ev_loop *loop, ev_timer * w,
int revents)
{
struct mirror_ctrl *ctrl = (struct mirror_ctrl *) w->data;
NULLCHECK(ctrl);
@@ -712,7 +717,8 @@ static void mirror_begin_cb( struct ev_loop *loop, ev_timer *w, int revents )
return;
}
if ( ctrl->serve->allocation_map_built || ctrl->serve->allocation_map_not_built ) {
if (ctrl->serve->allocation_map_built
|| ctrl->serve->allocation_map_not_built) {
info("allocation map builder is finished, beginning migration");
ev_timer_stop(loop, w);
/* Start by writing xfer 0 to the listener */
@@ -797,10 +803,8 @@ void mirror_run( struct server *serve )
ctrl.abandon_watcher.data = (void *) &ctrl;
ev_io_start(ctrl.ev_loop, &ctrl.abandon_watcher);
ERROR_UNLESS(
mirror_setup_next_xfer( &ctrl ),
"Couldn't find first transfer for mirror!"
);
ERROR_UNLESS(mirror_setup_next_xfer(&ctrl),
"Couldn't find first transfer for mirror!");
if (serve->allocation_map_built) {
@@ -831,7 +835,8 @@ void mirror_run( struct server *serve )
* it to something sane - they just terminate the event loop with state !=
* MS_DONE. We re-allow new clients here if necessary.
*/
if ( m->action_at_finish == ACTION_NOTHING || m->commit_state != MS_DONE ) {
if (m->action_at_finish == ACTION_NOTHING
|| m->commit_state != MS_DONE) {
server_allow_new_clients(serve);
}
@@ -897,7 +902,9 @@ void* mirror_runner(void* serve_params_uncast)
time_t start_time = time(NULL);
int connected = mirror_connect(mirror, serve->size);
mirror_signal_commit(mirror);
if ( !connected ) { goto abandon_mirror; }
if (!connected) {
goto abandon_mirror;
}
/* After this point, if we see a failure we need to disconnect
* and retry everything from mirror_set_state(_, MS_INIT), but
@@ -933,38 +940,33 @@ abandon_mirror:
}
struct mirror_super * mirror_super_create(
const char * filename,
struct mirror_super *mirror_super_create(const char *filename,
union mysockaddr *connect_to,
union mysockaddr *connect_from,
uint64_t max_Bps,
enum mirror_finish_action action_at_finish,
enum mirror_finish_action
action_at_finish,
struct mbox *state_mbox)
{
struct mirror_super *super = xmalloc(sizeof(struct mirror_super));
super->mirror = mirror_create(
filename,
super->mirror = mirror_create(filename,
connect_to,
connect_from,
max_Bps,
action_at_finish,
mbox_create() ) ;
action_at_finish, mbox_create());
super->state_mbox = state_mbox;
return super;
}
/* Post the current state of the mirror into super->state_mbox.*/
void mirror_super_signal_committed(
struct mirror_super * super ,
void mirror_super_signal_committed(struct mirror_super *super,
enum mirror_state commit_state)
{
NULLCHECK(super);
NULLCHECK(super->state_mbox);
mbox_post_mirror_state(
super->state_mbox,
commit_state );
mbox_post_mirror_state(super->state_mbox, commit_state);
}
@@ -998,8 +1000,7 @@ void * mirror_super_runner( void * serve_uncast )
struct mirror_super *super = serve->mirror_super;
do {
FATAL_IF( 0 != pthread_create(
&mirror->thread,
FATAL_IF(0 != pthread_create(&mirror->thread,
NULL,
mirror_runner,
serve),
@@ -1019,9 +1020,7 @@ void * mirror_super_runner( void * serve_uncast )
should_retry = *commit_state == MS_GO;
/* Only send this signal the first time */
mirror_super_signal_committed(
super,
*commit_state);
mirror_super_signal_committed(super, *commit_state);
debug("Mirror supervisor committed");
}
/* We only care about the value of the commit signal on

View File

@@ -127,15 +127,13 @@ struct mirror_super {
struct server;
struct flexnbd;
struct mirror_super * mirror_super_create(
const char * filename,
struct mirror_super *mirror_super_create(const char *filename,
union mysockaddr *connect_to,
union mysockaddr *connect_from,
uint64_t max_Bps,
enum mirror_finish_action action_at_finish,
struct mbox * state_mbox
);
enum mirror_finish_action
action_at_finish,
struct mbox *state_mbox);
void *mirror_super_runner(void *serve_uncast);
#endif

View File

@@ -19,6 +19,7 @@ static struct option serve_options[] = {
GETOPT_VERBOSE,
{0}
};
static char serve_short_options[] = "hl:p:f:s:dk" SOPT_QUIET SOPT_VERBOSE;
static char serve_help_text[] =
"Usage: flexnbd " CMD_SERVE " <options> [<acl address>*]\n\n"
@@ -28,10 +29,9 @@ static char serve_help_text[] =
"\t--" OPT_PORT ",-p <PORT>\tThe port to serve on.\n"
"\t--" OPT_FILE ",-f <FILE>\tThe file to serve.\n"
"\t--" OPT_DENY ",-d\tDeny connections by default unless in ACL.\n"
"\t--" OPT_KILLSWITCH",-k \tKill the server if a request takes 120 seconds.\n"
SOCK_LINE
VERBOSE_LINE
QUIET_LINE;
"\t--" OPT_KILLSWITCH
",-k \tKill the server if a request takes 120 seconds.\n" SOCK_LINE
VERBOSE_LINE QUIET_LINE;
static struct option listen_options[] = {
@@ -45,6 +45,7 @@ static struct option listen_options[] = {
GETOPT_VERBOSE,
{0}
};
static char listen_short_options[] = "hl:p:f:s:d" SOPT_QUIET SOPT_VERBOSE;
static char listen_help_text[] =
"Usage: flexnbd " CMD_LISTEN " <options> [<acl_address>*]\n\n"
@@ -54,9 +55,7 @@ static char listen_help_text[] =
"\t--" OPT_PORT ",-p <PORT>\tThe port to listen on.\n"
"\t--" OPT_FILE ",-f <FILE>\tThe file to serve.\n"
"\t--" OPT_DENY ",-d\tDeny connections by default unless in ACL.\n"
SOCK_LINE
VERBOSE_LINE
QUIET_LINE;
SOCK_LINE VERBOSE_LINE QUIET_LINE;
static struct option read_options[] = {
GETOPT_HELP,
@@ -69,6 +68,7 @@ static struct option read_options[] = {
GETOPT_VERBOSE,
{0}
};
static char read_short_options[] = "hl:p:F:S:b:" SOPT_QUIET SOPT_VERBOSE;
static char read_help_text[] =
"Usage: flexnbd " CMD_READ " <options>\n\n"
@@ -78,9 +78,7 @@ static char read_help_text[] =
"\t--" OPT_PORT ",-p <PORT>\tThe port to read from.\n"
"\t--" OPT_FROM ",-F <OFFSET>\tByte offset to read from.\n"
"\t--" OPT_SIZE ",-S <SIZE>\tBytes to read.\n"
BIND_LINE
VERBOSE_LINE
QUIET_LINE;
BIND_LINE VERBOSE_LINE QUIET_LINE;
static struct option *write_options = read_options;
@@ -93,9 +91,7 @@ static char write_help_text[] =
"\t--" OPT_PORT ",-p <PORT>\tThe port to write to.\n"
"\t--" OPT_FROM ",-F <OFFSET>\tByte offset to write from.\n"
"\t--" OPT_SIZE ",-S <SIZE>\tBytes to write.\n"
BIND_LINE
VERBOSE_LINE
QUIET_LINE;
BIND_LINE VERBOSE_LINE QUIET_LINE;
static struct option acl_options[] = {
GETOPT_HELP,
@@ -104,14 +100,12 @@ static struct option acl_options[] = {
GETOPT_VERBOSE,
{0}
};
static char acl_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE;
static char acl_help_text[] =
"Usage: flexnbd " CMD_ACL " <options> [<acl address>+]\n\n"
"Set the access control list for a server with control socket SOCK.\n\n"
HELP_LINE
SOCK_LINE
VERBOSE_LINE
QUIET_LINE;
HELP_LINE SOCK_LINE VERBOSE_LINE QUIET_LINE;
static struct option mirror_speed_options[] = {
GETOPT_HELP,
@@ -121,15 +115,12 @@ static struct option mirror_speed_options[] = {
GETOPT_VERBOSE,
{0}
};
static char mirror_speed_short_options[] = "hs:m:" SOPT_QUIET SOPT_VERBOSE;
static char mirror_speed_help_text[] =
"Usage: flexnbd " CMD_MIRROR_SPEED " <options>\n\n"
"Set the maximum speed of a migration from a mirring server listening on SOCK.\n\n"
HELP_LINE
SOCK_LINE
MAX_SPEED_LINE
VERBOSE_LINE
QUIET_LINE;
HELP_LINE SOCK_LINE MAX_SPEED_LINE VERBOSE_LINE QUIET_LINE;
static struct option mirror_options[] = {
GETOPT_HELP,
@@ -142,6 +133,7 @@ static struct option mirror_options[] = {
GETOPT_VERBOSE,
{0}
};
static char mirror_short_options[] = "hs:l:p:ub:" SOPT_QUIET SOPT_VERBOSE;
static char mirror_help_text[] =
"Usage: flexnbd " CMD_MIRROR " <options>\n\n"
@@ -151,9 +143,7 @@ static char mirror_help_text[] =
"\t--" OPT_PORT ",-p <PORT>\tThe port to mirror to.\n"
SOCK_LINE
"\t--" OPT_UNLINK ",-u\tUnlink the local file when done.\n"
BIND_LINE
VERBOSE_LINE
QUIET_LINE;
BIND_LINE VERBOSE_LINE QUIET_LINE;
static struct option break_options[] = {
GETOPT_HELP,
@@ -162,14 +152,12 @@ static struct option break_options[] = {
GETOPT_VERBOSE,
{0}
};
static char break_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE;
static char break_help_text[] =
"Usage: flexnbd " CMD_BREAK " <options>\n\n"
"Stop mirroring from the server with control socket SOCK.\n\n"
HELP_LINE
SOCK_LINE
VERBOSE_LINE
QUIET_LINE;
HELP_LINE SOCK_LINE VERBOSE_LINE QUIET_LINE;
static struct option status_options[] = {
@@ -179,14 +167,12 @@ static struct option status_options[] = {
GETOPT_VERBOSE,
{0}
};
static char status_short_options[] = "hs:" SOPT_QUIET SOPT_VERBOSE;
static char status_help_text[] =
"Usage: flexnbd " CMD_STATUS " <options>\n\n"
"Get the status for a server with control socket SOCK.\n\n"
HELP_LINE
SOCK_LINE
VERBOSE_LINE
QUIET_LINE;
HELP_LINE SOCK_LINE VERBOSE_LINE QUIET_LINE;
char help_help_text_arr[] =
"Usage: flexnbd <cmd> [cmd options]\n\n"
@@ -200,8 +186,7 @@ char help_help_text_arr[] =
"\tflexnbd mirror-speed\n"
"\tflexnbd break\n"
"\tflexnbd status\n"
"\tflexnbd help\n\n"
"See flexnbd help <cmd> for further info\n";
"\tflexnbd help\n\n" "See flexnbd help <cmd> for further info\n";
/* Slightly odd array/pointer pair to stop the compiler from complaining
* about symbol sizes
*/
@@ -214,7 +199,8 @@ void do_write(struct mode_readwrite_params* params);
void do_remote_command(char *command, char *mode, int argc, char **argv);
void read_serve_param( int c, char **ip_addr, char **ip_port, char **file, char **sock, int *default_deny, int *use_killswitch )
void read_serve_param(int c, char **ip_addr, char **ip_port, char **file,
char **sock, int *default_deny, int *use_killswitch)
{
switch (c) {
case 'h':
@@ -254,9 +240,7 @@ void read_serve_param( int c, char **ip_addr, char **ip_port, char **file, char
void read_listen_param(int c,
char **ip_addr,
char **ip_port,
char **file,
char **sock,
int *default_deny )
char **file, char **sock, int *default_deny)
{
switch (c) {
case 'h':
@@ -289,7 +273,9 @@ void read_listen_param( int c,
}
}
void read_readwrite_param( int c, char **ip_addr, char **ip_port, char **bind_addr, char **from, char **size, char *err_text )
void read_readwrite_param(int c, char **ip_addr, char **ip_port,
char **bind_addr, char **from, char **size,
char *err_text)
{
switch (c) {
case 'h':
@@ -348,11 +334,7 @@ void read_acl_param( int c, char **sock )
read_sock_param(c, sock, acl_help_text);
}
void read_mirror_speed_param(
int c,
char **sock,
char **max_speed
)
void read_mirror_speed_param(int c, char **sock, char **max_speed)
{
switch (c) {
case 'h':
@@ -377,13 +359,10 @@ void read_mirror_speed_param(
}
}
void read_mirror_param(
int c,
void read_mirror_param(int c,
char **sock,
char **ip_addr,
char **ip_port,
int *unlink,
char **bind_addr )
char **ip_port, int *unlink, char **bind_addr)
{
switch (c) {
case 'h':
@@ -459,10 +438,14 @@ int mode_serve( int argc, char *argv[] )
struct flexnbd *flexnbd;
while (1) {
c = getopt_long(argc, argv, serve_short_options, serve_options, NULL);
if ( c == -1 ) { break; }
c = getopt_long(argc, argv, serve_short_options, serve_options,
NULL);
if (c == -1) {
break;
}
read_serve_param( c, &ip_addr, &ip_port, &file, &sock, &default_deny, &use_killswitch );
read_serve_param(c, &ip_addr, &ip_port, &file, &sock,
&default_deny, &use_killswitch);
}
if (NULL == ip_addr || NULL == ip_port) {
@@ -473,9 +456,14 @@ int mode_serve( int argc, char *argv[] )
err = 1;
fprintf(stderr, "--file is required\n");
}
if ( err ) { exit_err( serve_help_text ); }
if (err) {
exit_err(serve_help_text);
}
flexnbd = flexnbd_create_serving( ip_addr, ip_port, file, sock, default_deny, argc - optind, argv + optind, MAX_NBD_CLIENTS, use_killswitch );
flexnbd =
flexnbd_create_serving(ip_addr, ip_port, file, sock, default_deny,
argc - optind, argv + optind,
MAX_NBD_CLIENTS, use_killswitch);
info("Serving file %s", file);
success = flexnbd_serve(flexnbd);
flexnbd_destroy(flexnbd);
@@ -499,8 +487,11 @@ int mode_listen( int argc, char *argv[] )
struct flexnbd *flexnbd;
while (1) {
c = getopt_long(argc, argv, listen_short_options, listen_options, NULL);
if ( c == -1 ) { break; }
c = getopt_long(argc, argv, listen_short_options, listen_options,
NULL);
if (c == -1) {
break;
}
read_listen_param(c, &ip_addr, &ip_port,
&file, &sock, &default_deny);
@@ -514,16 +505,16 @@ int mode_listen( int argc, char *argv[] )
err = 1;
fprintf(stderr, "--file is required\n");
}
if ( err ) { exit_err( listen_help_text ); }
if (err) {
exit_err(listen_help_text);
}
flexnbd = flexnbd_create_listening(
ip_addr,
flexnbd = flexnbd_create_listening(ip_addr,
ip_port,
file,
sock,
default_deny,
argc - optind,
argv + optind);
argc - optind, argv + optind);
success = flexnbd_serve(flexnbd);
flexnbd_destroy(flexnbd);
@@ -545,29 +536,25 @@ int mode_listen( int argc, char *argv[] )
* char *s_length,
* char *s_filename )
*/
void params_readwrite(
int write_not_read,
void params_readwrite(int write_not_read,
struct mode_readwrite_params *out,
char *s_ip_address,
char *s_port,
char *s_bind_address,
char* s_from,
char* s_length_or_filename
)
char *s_from, char *s_length_or_filename)
{
FATAL_IF_NULL(s_ip_address, "No IP address supplied");
FATAL_IF_NULL(s_port, "No port number supplied");
FATAL_IF_NULL(s_from, "No from supplied");
FATAL_IF_NULL(s_length_or_filename, "No length supplied");
FATAL_IF_ZERO(
parse_ip_to_sockaddr(&out->connect_to.generic, s_ip_address),
"Couldn't parse connection address '%s'",
s_ip_address
);
FATAL_IF_ZERO(parse_ip_to_sockaddr
(&out->connect_to.generic, s_ip_address),
"Couldn't parse connection address '%s'", s_ip_address);
if (s_bind_address != NULL &&
parse_ip_to_sockaddr(&out->connect_from.generic, s_bind_address) == 0) {
parse_ip_to_sockaddr(&out->connect_from.generic,
s_bind_address) == 0) {
fatal("Couldn't parse bind address '%s'", s_bind_address);
}
@@ -575,30 +562,27 @@ void params_readwrite(
long signed_from = atol(s_from);
FATAL_IF_NEGATIVE(signed_from,
"Can't read from a negative offset %d.", signed_from);
"Can't read from a negative offset %d.",
signed_from);
out->from = signed_from;
if (write_not_read) {
if (s_length_or_filename[0] - 48 < 10) {
out->len = atol(s_length_or_filename);
out->data_fd = 0;
}
else {
out->data_fd = open(
s_length_or_filename, O_RDONLY);
} else {
out->data_fd = open(s_length_or_filename, O_RDONLY);
FATAL_IF_NEGATIVE(out->data_fd,
"Couldn't open %s", s_length_or_filename);
off64_t signed_len = lseek64(out->data_fd, 0, SEEK_END);
FATAL_IF_NEGATIVE(signed_len,
"Couldn't find length of %s", s_length_or_filename);
"Couldn't find length of %s",
s_length_or_filename);
out->len = signed_len;
FATAL_IF_NEGATIVE(
lseek64(out->data_fd, 0, SEEK_SET),
"Couldn't rewind %s", s_length_or_filename
);
FATAL_IF_NEGATIVE(lseek64(out->data_fd, 0, SEEK_SET),
"Couldn't rewind %s", s_length_or_filename);
}
}
else {
} else {
out->len = atol(s_length_or_filename);
out->data_fd = 1;
}
@@ -618,11 +602,15 @@ int mode_read( int argc, char *argv[] )
struct mode_readwrite_params readwrite;
while (1) {
c = getopt_long(argc, argv, read_short_options, read_options, NULL);
c = getopt_long(argc, argv, read_short_options, read_options,
NULL);
if ( c == -1 ) { break; }
if (c == -1) {
break;
}
read_readwrite_param( c, &ip_addr, &ip_port, &bind_addr, &from, &size, read_help_text );
read_readwrite_param(c, &ip_addr, &ip_port, &bind_addr, &from,
&size, read_help_text);
}
if (NULL == ip_addr || NULL == ip_port) {
@@ -633,10 +621,13 @@ int mode_read( int argc, char *argv[] )
err = 1;
fprintf(stderr, "both --from and --size are required.\n");
}
if ( err ) { exit_err( read_help_text ); }
if (err) {
exit_err(read_help_text);
}
memset(&readwrite, 0, sizeof(readwrite));
params_readwrite( 0, &readwrite, ip_addr, ip_port, bind_addr, from, size );
params_readwrite(0, &readwrite, ip_addr, ip_port, bind_addr, from,
size);
do_read(&readwrite);
return 0;
}
@@ -654,10 +645,14 @@ int mode_write( int argc, char *argv[] )
struct mode_readwrite_params readwrite;
while (1) {
c = getopt_long(argc, argv, write_short_options, write_options, NULL);
if ( c == -1 ) { break; }
c = getopt_long(argc, argv, write_short_options, write_options,
NULL);
if (c == -1) {
break;
}
read_readwrite_param( c, &ip_addr, &ip_port, &bind_addr, &from, &size, write_help_text );
read_readwrite_param(c, &ip_addr, &ip_port, &bind_addr, &from,
&size, write_help_text);
}
if (NULL == ip_addr || NULL == ip_port) {
@@ -668,10 +663,13 @@ int mode_write( int argc, char *argv[] )
err = 1;
fprintf(stderr, "both --from and --size are required.\n");
}
if ( err ) { exit_err( write_help_text ); }
if (err) {
exit_err(write_help_text);
}
memset(&readwrite, 0, sizeof(readwrite));
params_readwrite( 1, &readwrite, ip_addr, ip_port, bind_addr, from, size );
params_readwrite(1, &readwrite, ip_addr, ip_port, bind_addr, from,
size);
do_write(&readwrite);
return 0;
}
@@ -683,7 +681,9 @@ int mode_acl( int argc, char *argv[] )
while (1) {
c = getopt_long(argc, argv, acl_short_options, acl_options, NULL);
if ( c == -1 ) { break; }
if (c == -1) {
break;
}
read_acl_param(c, &sock);
}
@@ -708,8 +708,11 @@ int mode_mirror_speed( int argc, char *argv[] )
char *speed = NULL;
while (1) {
c = getopt_long( argc, argv, mirror_speed_short_options, mirror_speed_options, NULL );
if ( -1 == c ) { break; }
c = getopt_long(argc, argv, mirror_speed_short_options,
mirror_speed_options, NULL);
if (-1 == c) {
break;
}
read_mirror_speed_param(c, &sock, &speed);
}
@@ -739,14 +742,15 @@ int mode_mirror( int argc, char *argv[] )
remote_argv[2] = "exit";
while (1) {
c = getopt_long( argc, argv, mirror_short_options, mirror_options, NULL);
if ( -1 == c ) { break; }
c = getopt_long(argc, argv, mirror_short_options, mirror_options,
NULL);
if (-1 == c) {
break;
}
read_mirror_param(c,
&sock,
&remote_argv[0],
&remote_argv[1],
&unlink,
&remote_argv[3] );
&remote_argv[1], &unlink, &remote_argv[3]);
}
if (NULL == sock) {
@@ -757,13 +761,16 @@ int mode_mirror( int argc, char *argv[] )
fprintf(stderr, "both --addr and --port are required.\n");
err = 1;
}
if ( err ) { exit_err( mirror_help_text ); }
if ( unlink ) { remote_argv[2] = "unlink"; }
if (err) {
exit_err(mirror_help_text);
}
if (unlink) {
remote_argv[2] = "unlink";
}
if (remote_argv[3] == NULL) {
do_remote_command("mirror", sock, 3, remote_argv);
}
else {
} else {
do_remote_command("mirror", sock, 4, remote_argv);
}
@@ -777,8 +784,11 @@ int mode_break( int argc, char *argv[] )
char *sock = NULL;
while (1) {
c = getopt_long( argc, argv, break_short_options, break_options, NULL );
if ( -1 == c ) { break; }
c = getopt_long(argc, argv, break_short_options, break_options,
NULL);
if (-1 == c) {
break;
}
read_break_param(c, &sock);
}
@@ -798,8 +808,11 @@ int mode_status( int argc, char *argv[] )
char *sock = NULL;
while (1) {
c = getopt_long( argc, argv, status_short_options, status_options, NULL );
if ( -1 == c ) { break; }
c = getopt_long(argc, argv, status_short_options, status_options,
NULL);
if (-1 == c) {
break;
}
read_status_param(c, &sock);
}
@@ -836,7 +849,9 @@ int mode_help( int argc, char *argv[] )
help_text = mirror_help_text;
} else if (IS_CMD(CMD_STATUS, cmd)) {
help_text = status_help_text;
} else { exit_err( help_help_text ); }
} else {
exit_err(help_help_text);
}
}
fprintf(stdout, "%s\n", help_text);
@@ -848,37 +863,27 @@ void mode(char* mode, int argc, char **argv)
{
if (IS_CMD(CMD_SERVE, mode)) {
exit(mode_serve(argc, argv));
}
else if ( IS_CMD( CMD_LISTEN, mode ) ) {
} else if (IS_CMD(CMD_LISTEN, mode)) {
exit(mode_listen(argc, argv));
}
else if ( IS_CMD( CMD_READ, mode ) ) {
} else if (IS_CMD(CMD_READ, mode)) {
mode_read(argc, argv);
}
else if ( IS_CMD( CMD_WRITE, mode ) ) {
} else if (IS_CMD(CMD_WRITE, mode)) {
mode_write(argc, argv);
}
else if ( IS_CMD( CMD_ACL, mode ) ) {
} else if (IS_CMD(CMD_ACL, mode)) {
mode_acl(argc, argv);
} else if (IS_CMD(CMD_MIRROR_SPEED, mode)) {
mode_mirror_speed(argc, argv);
}
else if ( IS_CMD( CMD_MIRROR, mode ) ) {
} else if (IS_CMD(CMD_MIRROR, mode)) {
mode_mirror(argc, argv);
}
else if ( IS_CMD( CMD_BREAK, mode ) ) {
} else if (IS_CMD(CMD_BREAK, mode)) {
mode_break(argc, argv);
}
else if ( IS_CMD( CMD_STATUS, mode ) ) {
} else if (IS_CMD(CMD_STATUS, mode)) {
mode_status(argc, argv);
}
else if ( IS_CMD( CMD_HELP, mode ) ) {
} else if (IS_CMD(CMD_HELP, mode)) {
mode_help(argc - 1, argv + 1);
}
else {
} else {
mode_help(argc - 1, argv + 1);
exit(1);
}
exit(0);
}

View File

@@ -21,8 +21,7 @@
#include <sys/socket.h>
#include <netinet/tcp.h>
struct server * server_create (
struct flexnbd * flexnbd,
struct server *server_create(struct flexnbd *flexnbd,
char *s_ip_address,
char *s_port,
char *s_file,
@@ -30,8 +29,7 @@ struct server * server_create (
int acl_entries,
char **s_acl_entries,
int max_nbd_clients,
int use_killswitch,
int success)
int use_killswitch, int success)
{
NULLCHECK(flexnbd);
struct server *out;
@@ -43,19 +41,18 @@ struct server * server_create (
server_allow_new_clients(out);
out->nbd_client = xmalloc( max_nbd_clients * sizeof( struct client_tbl_entry ) );
out->nbd_client =
xmalloc(max_nbd_clients * sizeof(struct client_tbl_entry));
out->tcp_backlog = 10; /* does this need to be settable? */
FATAL_IF_NULL(s_ip_address, "No IP address supplied");
FATAL_IF_NULL(s_port, "No port number supplied");
FATAL_IF_NULL(s_file, "No filename supplied");
NULLCHECK(s_ip_address);
FATAL_IF_ZERO(
parse_ip_to_sockaddr(&out->bind_to.generic, s_ip_address),
FATAL_IF_ZERO(parse_ip_to_sockaddr
(&out->bind_to.generic, s_ip_address),
"Couldn't parse server address '%s' (use 0 if "
"you want to bind to all IPs)",
s_ip_address
);
"you want to bind to all IPs)", s_ip_address);
out->acl = acl_create(acl_entries, s_acl_entries, default_deny);
@@ -110,8 +107,7 @@ void server_unlink( struct server * serve )
FATAL_IF_NEGATIVE(unlink(serve->filename),
"Failed to unlink %s: %s",
serve->filename,
strerror( errno ) );
serve->filename, strerror(errno));
}
@@ -155,7 +151,8 @@ void server_unlock_start_mirror( struct server *serve )
{
debug("Mirror start unlocking");
SERVER_UNLOCK( serve, l_start_mirror, "Problem with start mirror unlock" );
SERVER_UNLOCK(serve, l_start_mirror,
"Problem with start mirror unlock");
}
int server_start_mirror_locked(struct server *serve)
@@ -186,8 +183,9 @@ void serve_open_server_socket(struct server* params)
{
NULLCHECK(params);
params->server_fd = socket(params->bind_to.generic.sa_family == AF_INET ?
PF_INET : PF_INET6, SOCK_STREAM, 0);
params->server_fd =
socket(params->bind_to.generic.sa_family ==
AF_INET ? PF_INET : PF_INET6, SOCK_STREAM, 0);
FATAL_IF_NEGATIVE(params->server_fd, "Couldn't create server socket");
@@ -199,35 +197,32 @@ void serve_open_server_socket(struct server* params)
* problem. It's also indicative of something odd going on, so
* we barf.
*/
FATAL_IF_NEGATIVE(
sock_set_reuseaddr( params->server_fd, 1 ), "Couldn't set SO_REUSEADDR"
);
FATAL_IF_NEGATIVE(sock_set_reuseaddr(params->server_fd, 1),
"Couldn't set SO_REUSEADDR");
/* TCP_NODELAY makes everything not be slow. If we can't set
* this, again, there's something odd going on which we don't
* understand.
*/
FATAL_IF_NEGATIVE(
sock_set_tcp_nodelay( params->server_fd, 1 ), "Couldn't set TCP_NODELAY"
);
FATAL_IF_NEGATIVE(sock_set_tcp_nodelay(params->server_fd, 1),
"Couldn't set TCP_NODELAY");
/* If we can't bind, presumably that's because someone else is
* squatting on our ip/port combo, or the ip isn't yet
* configured. Ideally we want to retry this. */
FATAL_UNLESS_ZERO(
sock_try_bind( params->server_fd, &params->bind_to.generic ),
FATAL_UNLESS_ZERO(sock_try_bind
(params->server_fd, &params->bind_to.generic),
SHOW_ERRNO("Failed to bind() socket")
);
FATAL_IF_NEGATIVE(
listen(params->server_fd, params->tcp_backlog),
"Couldn't listen on server socket"
);
FATAL_IF_NEGATIVE(listen(params->server_fd, params->tcp_backlog),
"Couldn't listen on server socket");
}
int tryjoin_client_thread( struct client_tbl_entry *entry, int (*joinfunc)(pthread_t, void **) )
int tryjoin_client_thread(struct client_tbl_entry *entry,
int (*joinfunc) (pthread_t, void **))
{
NULLCHECK(entry);
@@ -239,25 +234,25 @@ int tryjoin_client_thread( struct client_tbl_entry *entry, int (*joinfunc)(pthre
if (entry->thread != 0) {
char s_client_address[128];
sockaddr_address_string( &entry->address.generic, &s_client_address[0], 128 );
sockaddr_address_string(&entry->address.generic,
&s_client_address[0], 128);
debug( "%s(%p,...)", joinfunc == pthread_join ? "joining" : "tryjoining", entry->thread );
debug("%s(%p,...)",
joinfunc == pthread_join ? "joining" : "tryjoining",
entry->thread);
int join_errno = joinfunc(entry->thread, &status);
/* join_errno can legitimately be ESRCH if the thread is
* already dead, but the client still needs tidying up. */
if (join_errno != 0 && !entry->client->stopped) {
debug( "join_errno was %s, stopped was %d", strerror( join_errno ), entry->client->stopped );
debug("join_errno was %s, stopped was %d",
strerror(join_errno), entry->client->stopped);
FATAL_UNLESS(join_errno == EBUSY,
"Problem with joining thread %p: %s",
entry->thread,
strerror(join_errno) );
}
else if ( join_errno == 0 ) {
entry->thread, strerror(join_errno));
} else if (join_errno == 0) {
debug("nbd thread %016x exited (%s) with status %ld",
entry->thread,
s_client_address,
(uintptr_t)status);
entry->thread, s_client_address, (uintptr_t) status);
client_destroy(entry->client);
entry->client = NULL;
entry->thread = 0;
@@ -287,7 +282,8 @@ int cleanup_client_thread( struct client_tbl_entry * entry )
return tryjoin_client_thread(entry, pthread_tryjoin_np);
}
void cleanup_client_threads( struct client_tbl_entry * entries, size_t entries_len )
void cleanup_client_threads(struct client_tbl_entry *entries,
size_t entries_len)
{
size_t i;
for (i = 0; i < entries_len; i++) {
@@ -349,7 +345,8 @@ int server_count_clients( struct server *params )
* to the current acl. If params->acl is NULL, the result will be 1,
* otherwise it will be the result of acl_includes().
*/
int server_acl_accepts( struct server *params, union mysockaddr * client_address )
int server_acl_accepts(struct server *params,
union mysockaddr *client_address)
{
NULLCHECK(params);
NULLCHECK(client_address);
@@ -368,8 +365,7 @@ int server_acl_accepts( struct server *params, union mysockaddr * client_address
}
int server_should_accept_client(
struct server * params,
int server_should_accept_client(struct server *params,
union mysockaddr *client_address,
char *s_client_address,
size_t s_client_address_len)
@@ -378,9 +374,9 @@ int server_should_accept_client(
NULLCHECK(client_address);
NULLCHECK(s_client_address);
const char* result = sockaddr_address_string(
&client_address->generic, s_client_address, s_client_address_len
);
const char *result =
sockaddr_address_string(&client_address->generic, s_client_address,
s_client_address_len);
if (NULL == result) {
warn("Rejecting client %s: Bad client_address", s_client_address);
@@ -388,7 +384,8 @@ int server_should_accept_client(
}
if (!server_acl_accepts(params, client_address)) {
warn( "Rejecting client %s: Access control error", s_client_address );
warn("Rejecting client %s: Access control error",
s_client_address);
debug("We %s have an acl, and default_deny is %s",
(params->acl ? "do" : "do not"),
(params->acl->default_deny ? "true" : "false"));
@@ -400,11 +397,11 @@ int server_should_accept_client(
int spawn_client_thread(
struct client * client_params,
int spawn_client_thread(struct client *client_params,
pthread_t * out_thread)
{
int result = pthread_create(out_thread, NULL, client_serve, client_params);
int result =
pthread_create(out_thread, NULL, client_serve, client_params);
return result;
}
@@ -414,10 +411,8 @@ int spawn_client_thread(
* to handle it. Rejects the connection if there is an ACL, and the far end's
* address doesn't match, or if there are too many clients already connected.
*/
void accept_nbd_client(
struct server* params,
int client_fd,
union mysockaddr* client_address)
void accept_nbd_client(struct server *params,
int client_fd, union mysockaddr *client_address)
{
NULLCHECK(params);
NULLCHECK(client_address);
@@ -426,11 +421,15 @@ void accept_nbd_client(
int slot;
char s_client_address[64] = { 0 };
FATAL_IF_NEGATIVE( sock_set_keepalive_params( client_fd, CLIENT_KEEPALIVE_TIME, CLIENT_KEEPALIVE_INTVL, CLIENT_KEEPALIVE_PROBES),
"Error setting keepalive parameters on client socket fd %d", client_fd );
FATAL_IF_NEGATIVE(sock_set_keepalive_params
(client_fd, CLIENT_KEEPALIVE_TIME,
CLIENT_KEEPALIVE_INTVL, CLIENT_KEEPALIVE_PROBES),
"Error setting keepalive parameters on client socket fd %d",
client_fd);
if ( !server_should_accept_client( params, client_address, s_client_address, 64 ) ) {
if (!server_should_accept_client
(params, client_address, s_client_address, 64)) {
FATAL_IF_NEGATIVE(close(client_fd),
"Error closing client socket fd %d", client_fd);
debug("Closed client socket fd %d", client_fd);
@@ -464,7 +463,8 @@ void accept_nbd_client(
return;
}
debug("nbd thread %p started (%s)", params->nbd_client[slot].thread, s_client_address);
debug("nbd thread %p started (%s)", params->nbd_client[slot].thread,
s_client_address);
}
@@ -485,8 +485,12 @@ void server_audit_clients( struct server * serve)
*/
for (i = 0; i < serve->max_nbd_clients; i++) {
entry = &serve->nbd_client[i];
if ( 0 == entry->thread ) { continue; }
if ( server_acl_accepts( serve, &entry->address ) ) { continue; }
if (0 == entry->thread) {
continue;
}
if (server_acl_accepts(serve, &entry->address)) {
continue;
}
client_signal_stop(entry->client);
}
}
@@ -546,7 +550,9 @@ void server_replace_acl( struct server *serve, struct acl * new_acl )
struct acl *old_acl = serve->acl;
serve->acl = new_acl;
/* We should always have an old_acl, but just in case... */
if ( old_acl ) { acl_destroy( old_acl ); }
if (old_acl) {
acl_destroy(old_acl);
}
}
server_unlock_acl(serve);
@@ -614,12 +620,13 @@ int server_accept( struct server * params )
FD_ZERO(&fds);
FD_SET(params->server_fd, &fds);
if( 0 < signal_fd ) { FD_SET(signal_fd, &fds); }
if (0 < signal_fd) {
FD_SET(signal_fd, &fds);
}
self_pipe_fd_set(params->close_signal, &fds);
self_pipe_fd_set(params->acl_updated_signal, &fds);
FATAL_IF_NEGATIVE(
sock_try_select(FD_SETSIZE, &fds, NULL, NULL, NULL),
FATAL_IF_NEGATIVE(sock_try_select(FD_SETSIZE, &fds, NULL, NULL, NULL),
SHOW_ERRNO("select() failed")
);
@@ -632,7 +639,8 @@ int server_accept( struct server * params )
if (0 < signal_fd && FD_ISSET(signal_fd, &fds)) {
debug("Stop signal received.");
server_close_clients(params);
params->success = params->success && serve_shutdown_is_graceful( params );
params->success = params->success
&& serve_shutdown_is_graceful(params);
should_continue = 0;
}
@@ -643,7 +651,8 @@ int server_accept( struct server * params )
}
if (FD_ISSET(params->server_fd, &fds)) {
int client_fd = accept( params->server_fd, &client_address.generic, &socklen );
int client_fd =
accept(params->server_fd, &client_address.generic, &socklen);
if (params->allow_new_clients) {
debug("Accepted nbd client socket fd %d", client_fd);
@@ -678,8 +687,7 @@ void* build_allocation_map_thread(void* serve_uncast)
if (build_allocation_map(serve->allocation_map, fd)) {
serve->allocation_map_built = 1;
}
else {
} else {
/* We can operate without it, but we can't free it without a race.
* All that happens if we leave it is that it gradually builds up an
* *incomplete* record of writes. Nobody will use it, as
@@ -722,8 +730,7 @@ void serve_init_allocation_map(struct server* params)
}
params->size = size;
FATAL_IF_NEGATIVE( size, "Couldn't find size of %s",
params->filename );
FATAL_IF_NEGATIVE(size, "Couldn't find size of %s", params->filename);
params->allocation_map =
bitset_alloc(params->size, block_allocation_resolution);
@@ -749,7 +756,8 @@ void server_allow_new_clients( struct server * serve )
return;
}
void server_join_clients( struct server * serve ) {
void server_join_clients(struct server *serve)
{
int i;
void *status;
@@ -762,7 +770,8 @@ void server_join_clients( struct server * serve ) {
if (0 == err) {
serve->nbd_client[i].thread = 0;
} else {
warn( "Error %s (%i) joining thread %p", strerror( err ), err, thread_id );
warn("Error %s (%i) joining thread %p", strerror(err), err,
thread_id);
}
}
}
@@ -814,7 +823,9 @@ void serve_cleanup(struct server* params,
info("cleaning up");
if (params->server_fd){ close(params->server_fd); }
if (params->server_fd) {
close(params->server_fd);
}
/* need to stop background build if we're killed very early on */
pthread_cancel(params->allocation_map_builder_thread);
@@ -823,14 +834,18 @@ void serve_cleanup(struct server* params,
int need_mirror_lock;
need_mirror_lock = !server_start_mirror_locked(params);
if ( need_mirror_lock ) { server_lock_start_mirror( params ); }
if (need_mirror_lock) {
server_lock_start_mirror(params);
}
{
if (server_is_mirroring(params)) {
server_abandon_mirror(params);
}
server_prevent_mirror_start(params);
}
if ( need_mirror_lock ) { server_unlock_start_mirror( params ); }
if (need_mirror_lock) {
server_unlock_start_mirror(params);
}
server_join_clients(params);
@@ -875,8 +890,11 @@ uint64_t server_mirror_bytes_remaining( struct server * serve )
{
if (server_is_mirroring(serve)) {
uint64_t bytes_to_xfer =
bitset_stream_queued_bytes( serve->allocation_map, BITSET_STREAM_SET ) +
( serve->size - serve->mirror->offset );
bitset_stream_queued_bytes(serve->allocation_map,
BITSET_STREAM_SET) + (serve->size -
serve->
mirror->
offset);
return bytes_to_xfer;
}
@@ -922,10 +940,8 @@ void server_abandon_mirror( struct server * serve )
* We can set abandon_signal after mirror_super has checked it, but
* before the reset. However, mirror_reset doesn't clear abandon_signal
* so it'll just terminate early on the next pass. */
ERROR_UNLESS(
self_pipe_signal( serve->mirror->abandon_signal ),
"Failed to signal abandon to mirror"
);
ERROR_UNLESS(self_pipe_signal(serve->mirror->abandon_signal),
"Failed to signal abandon to mirror");
pthread_t tid = serve->mirror_super->thread;
pthread_join(tid, NULL);
@@ -959,7 +975,9 @@ int do_serve( struct server* params, struct self_pipe * open_signal )
/* Only signal that we are open for business once the server
socket is open */
if ( NULL != open_signal ) { self_pipe_signal( open_signal ); }
if (NULL != open_signal) {
self_pipe_signal(open_signal);
}
serve_init_allocation_map(params);
serve_accept_loop(params);

View File

@@ -103,8 +103,7 @@ struct server {
int success;
};
struct server * server_create(
struct flexnbd * flexnbd,
struct server *server_create(struct flexnbd *flexnbd,
char *s_ip_address,
char *s_port,
char *s_file,
@@ -112,8 +111,7 @@ struct server * server_create(
int acl_entries,
char **s_acl_entries,
int max_nbd_clients,
int use_killswitch,
int success );
int use_killswitch, int success);
void server_destroy(struct server *);
int server_is_closed(struct server *serve);
void serve_signal_close(struct server *serve);
@@ -167,4 +165,3 @@ struct mode_readwrite_params {
#endif

View File

@@ -21,17 +21,20 @@ struct status * status_create( struct server * serve )
if (status->is_mirroring) {
status->migration_duration = monotonic_time_ms();
if ( ( serve->mirror->migration_started ) < status->migration_duration ) {
if ((serve->mirror->migration_started) <
status->migration_duration) {
status->migration_duration -= serve->mirror->migration_started;
} else {
status->migration_duration = 0;
}
status->migration_duration /= 1000;
status->migration_speed = server_mirror_bps(serve);
status->migration_speed_limit = serve->mirror->max_bytes_per_second;
status->migration_speed_limit =
serve->mirror->max_bytes_per_second;
status->migration_seconds_left = server_mirror_eta(serve);
status->migration_bytes_left = server_mirror_bytes_remaining( serve );
status->migration_bytes_left =
server_mirror_bytes_remaining(serve);
}
server_unlock_start_mirror(serve);
@@ -77,4 +80,3 @@ void status_destroy( struct status * status )
NULLCHECK(status);
free(status);
}

View File

@@ -101,4 +101,3 @@ void status_destroy( struct status * );
#endif

View File

@@ -11,10 +11,8 @@ START_TEST( test_null_acl )
fail_if(NULL == acl, "No acl alloced.");
fail_unless(0 == acl->len, "Incorrect length");
}
END_TEST
START_TEST( test_parses_single_line )
END_TEST START_TEST(test_parses_single_line)
{
char *lines[] = { "127.0.0.1" };
struct acl *acl = acl_create(1, lines, 0);
@@ -22,9 +20,8 @@ START_TEST( test_parses_single_line )
fail_unless(1 == acl->len, "Incorrect length.");
fail_if(NULL == acl->entries, "No entries present.");
}
END_TEST
START_TEST( test_parses_multiple_lines )
END_TEST START_TEST(test_parses_multiple_lines)
{
char *lines[] = { "127.0.0.1", "::1" };
struct acl *acl = acl_create(2, lines, 0);
@@ -37,23 +34,22 @@ START_TEST( test_parses_multiple_lines )
struct ip_and_mask *entry;
entry = &(*acl->entries)[0];
fail_unless(entry->ip.family == e0.family, "entry 0 has wrong family!");
fail_unless(entry->ip.family == e0.family,
"entry 0 has wrong family!");
entry = &(*acl->entries)[1];
fail_unless(entry->ip.family == e1.family, "entry 1 has wrong family!");
fail_unless(entry->ip.family == e1.family,
"entry 1 has wrong family!");
}
END_TEST
START_TEST( test_destroy_doesnt_crash )
END_TEST START_TEST(test_destroy_doesnt_crash)
{
char *lines[] = { "127.0.0.1" };
struct acl *acl = acl_create(1, lines, 0);
acl_destroy(acl);
}
END_TEST
START_TEST( test_includes_single_address )
END_TEST START_TEST(test_includes_single_address)
{
char *lines[] = { "127.0.0.1" };
struct acl *acl = acl_create(1, lines, 0);
@@ -63,8 +59,8 @@ START_TEST( test_includes_single_address )
fail_unless(acl_includes(acl, &x), "Included address wasn't covered");
}
END_TEST
END_TEST
START_TEST(test_includes_single_address_when_netmask_specified_ipv4)
{
char *lines[] = { "127.0.0.1/24" };
@@ -80,8 +76,8 @@ START_TEST( test_includes_single_address_when_netmask_specified_ipv4 )
parse_ip_to_sockaddr(&x.generic, "127.0.0.255");
fail_unless(acl_includes(acl, &x), "Included address wasn't covered");
}
END_TEST
END_TEST
START_TEST(test_includes_single_address_when_netmask_specified_ipv6)
{
char *lines[] = { "fe80::/10" };
@@ -97,8 +93,8 @@ START_TEST( test_includes_single_address_when_netmask_specified_ipv6 )
parse_ip_to_sockaddr(&x.generic, "fe80:ffff:ffff::ffff");
fail_unless(acl_includes(acl, &x), "Included address wasn't covered");
}
END_TEST
END_TEST
START_TEST(test_includes_single_address_when_multiple_entries_exist)
{
char *lines[] = { "127.0.0.1", "::1" };
@@ -109,13 +105,13 @@ START_TEST( test_includes_single_address_when_multiple_entries_exist )
parse_ip_to_sockaddr(&e0.generic, "127.0.0.1");
parse_ip_to_sockaddr(&e1.generic, "::1");
fail_unless( acl_includes( acl, &e0 ), "Included address 0 wasn't covered" );
fail_unless( acl_includes( acl, &e1 ), "Included address 1 wasn't covered" );
fail_unless(acl_includes(acl, &e0),
"Included address 0 wasn't covered");
fail_unless(acl_includes(acl, &e1),
"Included address 1 wasn't covered");
}
END_TEST
START_TEST( test_doesnt_include_other_address )
END_TEST START_TEST(test_doesnt_include_other_address)
{
char *lines[] = { "127.0.0.1" };
struct acl *acl = acl_create(1, lines, 0);
@@ -124,8 +120,8 @@ START_TEST( test_doesnt_include_other_address )
parse_ip_to_sockaddr(&x.generic, "127.0.0.2");
fail_if(acl_includes(acl, &x), "Excluded address was covered.");
}
END_TEST
END_TEST
START_TEST(test_doesnt_include_other_address_when_netmask_specified)
{
char *lines[] = { "127.0.0.1/32" };
@@ -135,8 +131,8 @@ START_TEST( test_doesnt_include_other_address_when_netmask_specified )
parse_ip_to_sockaddr(&x.generic, "127.0.0.2");
fail_if(acl_includes(acl, &x), "Excluded address was covered.");
}
END_TEST
END_TEST
START_TEST(test_doesnt_include_other_address_when_multiple_entries_exist)
{
char *lines[] = { "127.0.0.1", "::1" };
@@ -150,9 +146,8 @@ START_TEST( test_doesnt_include_other_address_when_multiple_entries_exist )
fail_if(acl_includes(acl, &e0), "Excluded address 0 was covered.");
fail_if(acl_includes(acl, &e1), "Excluded address 1 was covered.");
}
END_TEST
START_TEST( test_default_deny_rejects )
END_TEST START_TEST(test_default_deny_rejects)
{
struct acl *acl = acl_create(0, NULL, 1);
union mysockaddr x;
@@ -161,10 +156,8 @@ START_TEST( test_default_deny_rejects )
fail_if(acl_includes(acl, &x), "Default deny accepted.");
}
END_TEST
START_TEST( test_default_accept_rejects )
END_TEST START_TEST(test_default_accept_rejects)
{
struct acl *acl = acl_create(0, NULL, 0);
union mysockaddr x;
@@ -173,10 +166,8 @@ START_TEST( test_default_accept_rejects )
fail_unless(acl_includes(acl, &x), "Default accept rejected.");
}
END_TEST
Suite* acl_suite(void)
END_TEST Suite * acl_suite(void)
{
Suite *s = suite_create("acl");
TCase *tc_create = tcase_create("create");
@@ -189,14 +180,19 @@ Suite* acl_suite(void)
tcase_add_test(tc_includes, test_parses_multiple_lines);
tcase_add_test(tc_includes, test_includes_single_address);
tcase_add_test(tc_includes, test_includes_single_address_when_netmask_specified_ipv4);
tcase_add_test(tc_includes, test_includes_single_address_when_netmask_specified_ipv6);
tcase_add_test(tc_includes,
test_includes_single_address_when_netmask_specified_ipv4);
tcase_add_test(tc_includes,
test_includes_single_address_when_netmask_specified_ipv6);
tcase_add_test(tc_includes, test_includes_single_address_when_multiple_entries_exist);
tcase_add_test(tc_includes,
test_includes_single_address_when_multiple_entries_exist);
tcase_add_test(tc_includes, test_doesnt_include_other_address);
tcase_add_test(tc_includes, test_doesnt_include_other_address_when_netmask_specified);
tcase_add_test(tc_includes, test_doesnt_include_other_address_when_multiple_entries_exist);
tcase_add_test(tc_includes,
test_doesnt_include_other_address_when_netmask_specified);
tcase_add_test(tc_includes,
test_doesnt_include_other_address_when_multiple_entries_exist);
tcase_add_test(tc_includes, test_default_deny_rejects);
tcase_add_test(tc_includes, test_default_accept_rejects);
@@ -226,4 +222,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -22,9 +22,8 @@ START_TEST(test_bit_set)
TEST_BIT_SET(7, 0x87);
TEST_BIT_SET(63, 0x8000000000000087);
}
END_TEST
START_TEST(test_bit_clear)
END_TEST START_TEST(test_bit_clear)
{
uint64_t num = 0xffffffffffffffff;
bitfield_p bits = (bitfield_p) & num;
@@ -39,9 +38,8 @@ START_TEST(test_bit_clear)
TEST_BIT_CLEAR(7, 0xffffffffffffff78);
TEST_BIT_CLEAR(63, 0x7fffffffffffff78);
}
END_TEST
START_TEST(test_bit_tests)
END_TEST START_TEST(test_bit_tests)
{
uint64_t num = 0x5555555555555555;
bitfield_p bits = (bitfield_p) & num;
@@ -54,9 +52,8 @@ START_TEST(test_bit_tests)
fail_unless(bit_is_set(bits, 62), "bit_is_set malfunction");
fail_unless(bit_is_clear(bits, 63), "bit_is_clear malfunction");
}
END_TEST
START_TEST(test_bit_ranges)
END_TEST START_TEST(test_bit_ranges)
{
bitfield_word_t buffer[BIT_WORDS_FOR_SIZE(4160)];
uint64_t *longs = (uint64_t *) buffer;
@@ -66,27 +63,27 @@ START_TEST(test_bit_ranges)
for (i = 0; i < 64; i++) {
bit_set_range(buffer, i * 64, i);
fail_unless(
longs[i] == (1ULL<<i)-1,
fail_unless(longs[i] == (1ULL << i) - 1,
"longs[%ld] = %lx SHOULD BE %lx",
i, longs[i], (1ULL<<i)-1
);
i, longs[i], (1ULL << i) - 1);
fail_unless(longs[i+1] == 0, "bit_set_range overshot at i=%d", i);
fail_unless(longs[i + 1] == 0, "bit_set_range overshot at i=%d",
i);
}
for (i = 0; i < 64; i++) {
bit_clear_range(buffer, i * 64, i);
fail_unless(longs[i] == 0, "bit_clear_range didn't work at i=%d", i);
fail_unless(longs[i] == 0, "bit_clear_range didn't work at i=%d",
i);
}
}
END_TEST
START_TEST(test_bit_runs)
END_TEST START_TEST(test_bit_runs)
{
bitfield_word_t buffer[BIT_WORDS_FOR_SIZE(256)];
int i, ptr = 0, runs[] = {
56,97,22,12,83,1,45,80,85,51,64,40,63,67,75,64,94,81,79,62
56, 97, 22, 12, 83, 1, 45, 80, 85, 51, 64, 40, 63, 67, 75, 64, 94,
81, 79, 62
};
memset(buffer, 0, 256);
@@ -101,17 +98,13 @@ START_TEST(test_bit_runs)
for (i = 0; i < 20; i += 1) {
int run = bit_run_count(buffer, ptr, 2048 - ptr, NULL);
fail_unless(
run == runs[i],
"run %d should have been %d, was %d",
i, runs[i], run
);
fail_unless(run == runs[i],
"run %d should have been %d, was %d", i, runs[i], run);
ptr += runs[i];
}
}
END_TEST
START_TEST(test_bitset)
END_TEST START_TEST(test_bitset)
{
struct bitset *map;
uint64_t *num;
@@ -143,10 +136,8 @@ START_TEST(test_bitset)
bitset_clear_range(map, 3200, 400);
ck_assert_int_eq(0xfffffff0ffffffff, *num);
}
END_TEST
START_TEST( test_bitset_set )
END_TEST START_TEST(test_bitset_set)
{
struct bitset *map;
uint64_t run;
@@ -174,10 +165,8 @@ START_TEST( test_bitset_set )
}
END_TEST
START_TEST( test_bitset_clear )
END_TEST START_TEST(test_bitset_clear)
{
struct bitset *map;
uint64_t *num;
@@ -201,9 +190,8 @@ START_TEST( test_bitset_clear )
ck_assert_int_eq(run, 53687091200);
bitset_free(map);
}
END_TEST
START_TEST( test_bitset_set_range )
END_TEST START_TEST(test_bitset_set_range)
{
struct bitset *map = bitset_alloc(64, 1);
assert_bitset_is(map, 0x0000000000000000);
@@ -218,9 +206,8 @@ START_TEST( test_bitset_set_range )
bitset_free(map);
}
END_TEST
START_TEST( test_bitset_clear_range )
END_TEST START_TEST(test_bitset_clear_range)
{
struct bitset *map = bitset_alloc(64, 1);
bitset_set(map);
@@ -236,9 +223,8 @@ START_TEST( test_bitset_clear_range )
bitset_free(map);
}
END_TEST
START_TEST( test_bitset_run_count )
END_TEST START_TEST(test_bitset_run_count)
{
struct bitset *map = bitset_alloc(64, 1);
uint64_t run;
@@ -304,27 +290,24 @@ START_TEST( test_bitset_run_count )
bitset_free(map);
}
END_TEST
START_TEST( test_bitset_set_range_doesnt_push_to_stream )
END_TEST START_TEST(test_bitset_set_range_doesnt_push_to_stream)
{
struct bitset *map = bitset_alloc(64, 1);
bitset_set_range(map, 0, 64);
ck_assert_int_eq(0, bitset_stream_size(map));
bitset_free(map);
}
END_TEST
START_TEST( test_bitset_clear_range_doesnt_push_to_stream )
END_TEST START_TEST(test_bitset_clear_range_doesnt_push_to_stream)
{
struct bitset *map = bitset_alloc(64, 1);
bitset_clear_range(map, 0, 64);
ck_assert_int_eq(0, bitset_stream_size(map));
bitset_free(map);
}
END_TEST
START_TEST(test_bitset_enable_stream)
END_TEST START_TEST(test_bitset_enable_stream)
{
struct bitset *map = bitset_alloc(64, 1);
struct bitset_stream_entry result;
@@ -342,9 +325,8 @@ START_TEST(test_bitset_enable_stream)
bitset_free(map);
}
END_TEST
START_TEST(test_bitset_disable_stream)
END_TEST START_TEST(test_bitset_disable_stream)
{
struct bitset *map = bitset_alloc(64, 1);
struct bitset_stream_entry result;
@@ -365,9 +347,8 @@ START_TEST(test_bitset_disable_stream)
bitset_free(map);
}
END_TEST
START_TEST(test_bitset_stream_with_set_range)
END_TEST START_TEST(test_bitset_stream_with_set_range)
{
struct bitset *map = bitset_alloc(64, 1);
struct bitset_stream_entry result;
@@ -387,9 +368,8 @@ START_TEST(test_bitset_stream_with_set_range)
bitset_free(map);
}
END_TEST
START_TEST(test_bitset_stream_with_clear_range)
END_TEST START_TEST(test_bitset_stream_with_clear_range)
{
struct bitset *map = bitset_alloc(64, 1);
struct bitset_stream_entry result;
@@ -408,9 +388,8 @@ START_TEST(test_bitset_stream_with_clear_range)
bitset_free(map);
}
END_TEST
START_TEST(test_bitset_stream_size)
END_TEST START_TEST(test_bitset_stream_size)
{
struct bitset *map = bitset_alloc(64, 1);
bitset_enable_stream(map);
@@ -427,9 +406,8 @@ START_TEST(test_bitset_stream_size)
bitset_free(map);
}
END_TEST
START_TEST(test_bitset_stream_queued_bytes)
END_TEST START_TEST(test_bitset_stream_queued_bytes)
{
struct bitset *map = bitset_alloc(64, 1);
bitset_enable_stream(map);
@@ -443,15 +421,18 @@ START_TEST(test_bitset_stream_queued_bytes)
bitset_clear_range(map, 0, 2);
bitset_disable_stream(map);
ck_assert_int_eq( 64, bitset_stream_queued_bytes( map, BITSET_STREAM_ON ) );
ck_assert_int_eq( 80, bitset_stream_queued_bytes( map, BITSET_STREAM_SET ) );
ck_assert_int_eq( 82, bitset_stream_queued_bytes( map, BITSET_STREAM_UNSET ) );
ck_assert_int_eq( 64, bitset_stream_queued_bytes( map, BITSET_STREAM_OFF ) );
ck_assert_int_eq(64,
bitset_stream_queued_bytes(map, BITSET_STREAM_ON));
ck_assert_int_eq(80,
bitset_stream_queued_bytes(map, BITSET_STREAM_SET));
ck_assert_int_eq(82,
bitset_stream_queued_bytes(map, BITSET_STREAM_UNSET));
ck_assert_int_eq(64,
bitset_stream_queued_bytes(map, BITSET_STREAM_OFF));
bitset_free(map);
}
END_TEST
Suite* bitset_suite(void)
END_TEST Suite * bitset_suite(void)
{
Suite *s = suite_create("bitset");
@@ -471,7 +452,8 @@ Suite* bitset_suite(void)
tcase_add_test(tc_bitset, test_bitset_set_range);
tcase_add_test(tc_bitset, test_bitset_clear_range);
tcase_add_test(tc_bitset, test_bitset_set_range_doesnt_push_to_stream);
tcase_add_test(tc_bitset, test_bitset_clear_range_doesnt_push_to_stream);
tcase_add_test(tc_bitset,
test_bitset_clear_range_doesnt_push_to_stream);
suite_add_tcase(s, tc_bitset);
@@ -497,4 +479,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -10,6 +10,7 @@
#include <unistd.h>
struct server fake_server = { 0 };
#define FAKE_SERVER &fake_server
#define FAKE_SOCKET (42)
@@ -21,10 +22,8 @@ START_TEST( test_assigns_socket )
fail_unless(42 == c->socket, "Socket wasn't assigned.");
}
END_TEST
START_TEST( test_assigns_server )
END_TEST START_TEST(test_assigns_server)
{
struct client *c;
/* can't predict the storage size so we can't allocate one on
@@ -35,10 +34,8 @@ START_TEST( test_assigns_server )
fail_unless(FAKE_SERVER == c->serve, "Serve wasn't assigned.");
}
END_TEST
START_TEST( test_opens_stop_signal )
END_TEST START_TEST(test_opens_stop_signal)
{
struct client *c = client_create(FAKE_SERVER, FAKE_SOCKET);
@@ -48,10 +45,8 @@ START_TEST( test_opens_stop_signal )
"No signal was sent.");
}
END_TEST
int fd_is_closed(int);
END_TEST int fd_is_closed(int);
START_TEST(test_closes_stop_signal)
{
@@ -64,10 +59,8 @@ START_TEST( test_closes_stop_signal )
fail_unless(fd_is_closed(read_fd), "Stop signal wasn't destroyed.");
fail_unless(fd_is_closed(write_fd), "Stop signal wasn't destroyed.");
}
END_TEST
START_TEST( test_read_request_quits_on_stop_signal )
END_TEST START_TEST(test_read_request_quits_on_stop_signal)
{
int fds[2];
struct nbd_request nbdr;
@@ -82,10 +75,8 @@ START_TEST( test_read_request_quits_on_stop_signal )
close(fds[0]);
close(fds[1]);
}
END_TEST
Suite *client_suite(void)
END_TEST Suite * client_suite(void)
{
Suite *s = suite_create("client");
@@ -119,4 +110,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -13,10 +13,8 @@ START_TEST( test_assigns_sock_name )
fail_unless(csn == control->socket_name, "Socket name not assigned");
}
END_TEST
Suite *control_suite(void)
END_TEST Suite * control_suite(void)
{
Suite *s = suite_create("control");
@@ -39,4 +37,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -5,8 +5,7 @@
START_TEST(test_listening_assigns_sock)
{
struct flexnbd * flexnbd = flexnbd_create_listening(
"127.0.0.1",
struct flexnbd *flexnbd = flexnbd_create_listening("127.0.0.1",
"4777",
"fakefile",
"fakesock",
@@ -15,10 +14,8 @@ START_TEST( test_listening_assigns_sock )
NULL);
fail_if(NULL == flexnbd->control->socket_name, "No socket was copied");
}
END_TEST
Suite *flexnbd_suite(void)
END_TEST Suite * flexnbd_suite(void)
{
Suite *s = suite_create("flexnbd");
@@ -41,4 +38,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -10,25 +10,24 @@ START_TEST( test_mutex_create )
NULLCHECK(ftm);
flexthread_mutex_destroy(ftm);
}
END_TEST
START_TEST( test_mutex_lock )
END_TEST START_TEST(test_mutex_lock)
{
struct flexthread_mutex *ftm = flexthread_mutex_create();
fail_if( flexthread_mutex_held( ftm ), "Flexthread_mutex is held before lock" );
fail_if(flexthread_mutex_held(ftm),
"Flexthread_mutex is held before lock");
flexthread_mutex_lock(ftm);
fail_unless( flexthread_mutex_held( ftm ), "Flexthread_mutex is not held inside lock" );
fail_unless(flexthread_mutex_held(ftm),
"Flexthread_mutex is not held inside lock");
flexthread_mutex_unlock(ftm);
fail_if( flexthread_mutex_held( ftm ), "Flexthread_mutex is held after unlock" );
fail_if(flexthread_mutex_held(ftm),
"Flexthread_mutex is held after unlock");
flexthread_mutex_destroy(ftm);
}
END_TEST
Suite* flexthread_suite(void)
END_TEST Suite * flexthread_suite(void)
{
Suite *s = suite_create("flexthread");
TCase *tc_create = tcase_create("create");
@@ -59,4 +58,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -15,10 +15,8 @@ START_TEST( test_read_until_newline_returns_line_length_plus_null )
ck_assert_int_eq(5, nread);
}
END_TEST
START_TEST( test_read_until_newline_inserts_null )
END_TEST START_TEST(test_read_until_newline_inserts_null)
{
int fds[2];
int nread;
@@ -32,10 +30,8 @@ START_TEST( test_read_until_newline_inserts_null )
ck_assert_int_eq('\0', buf[4]);
}
END_TEST
START_TEST( test_read_empty_line_inserts_null )
END_TEST START_TEST(test_read_empty_line_inserts_null)
{
int fds[2];
int nread;
@@ -48,10 +44,8 @@ START_TEST( test_read_empty_line_inserts_null )
ck_assert_int_eq('\0', buf[0]);
ck_assert_int_eq(1, nread);
}
END_TEST
START_TEST( test_read_eof_returns_err )
END_TEST START_TEST(test_read_eof_returns_err)
{
int fds[2];
int nread;
@@ -63,10 +57,8 @@ START_TEST( test_read_eof_returns_err )
ck_assert_int_eq(-1, nread);
}
END_TEST
START_TEST( test_read_eof_fills_line )
END_TEST START_TEST(test_read_eof_fills_line)
{
int fds[2];
int nread;
@@ -80,10 +72,8 @@ START_TEST( test_read_eof_fills_line )
ck_assert_int_eq(-1, nread);
ck_assert_int_eq('4', buf[3]);
}
END_TEST
START_TEST( test_read_lines_until_blankline )
END_TEST START_TEST(test_read_lines_until_blankline)
{
char **lines = NULL;
int fds[2];
@@ -96,23 +86,26 @@ START_TEST( test_read_lines_until_blankline )
ck_assert_int_eq(3, nlines);
}
END_TEST
Suite *ioutil_suite(void)
END_TEST Suite * ioutil_suite(void)
{
Suite *s = suite_create("ioutil");
TCase *tc_read_until_newline = tcase_create("read_until_newline");
TCase *tc_read_lines_until_blankline = tcase_create("read_lines_until_blankline");
TCase *tc_read_lines_until_blankline =
tcase_create("read_lines_until_blankline");
tcase_add_test(tc_read_until_newline, test_read_until_newline_returns_line_length_plus_null);
tcase_add_test(tc_read_until_newline, test_read_until_newline_inserts_null);
tcase_add_test(tc_read_until_newline, test_read_empty_line_inserts_null);
tcase_add_test(tc_read_until_newline,
test_read_until_newline_returns_line_length_plus_null);
tcase_add_test(tc_read_until_newline,
test_read_until_newline_inserts_null);
tcase_add_test(tc_read_until_newline,
test_read_empty_line_inserts_null);
tcase_add_test(tc_read_until_newline, test_read_eof_returns_err);
tcase_add_test(tc_read_until_newline, test_read_eof_fills_line);
tcase_add_test(tc_read_lines_until_blankline, test_read_lines_until_blankline );
tcase_add_test(tc_read_lines_until_blankline,
test_read_lines_until_blankline);
suite_add_tcase(s, tc_read_until_newline);
suite_add_tcase(s, tc_read_lines_until_blankline);
@@ -131,4 +124,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -14,13 +14,11 @@ START_TEST( test_allocs_cvar )
memset(&cond_zero, 'X', sizeof(cond_zero));
fail_if(memcmp(&cond_zero, &mbox->filled_cond, sizeof(cond_zero)) == 0,
"Condition variable not allocated");
fail_if( memcmp( &cond_zero, &mbox->emptied_cond, sizeof( cond_zero ) ) == 0 ,
"Condition variable not allocated" );
fail_if(memcmp(&cond_zero, &mbox->emptied_cond, sizeof(cond_zero)) ==
0, "Condition variable not allocated");
}
END_TEST
START_TEST( test_post_stores_value )
END_TEST START_TEST(test_post_stores_value)
{
struct mbox *mbox = mbox_create();
@@ -30,10 +28,8 @@ START_TEST( test_post_stores_value )
fail_unless(deadbeef == mbox_contents(mbox),
"Contents were not posted");
}
END_TEST
void * mbox_receive_runner( void * mbox_uncast )
END_TEST void *mbox_receive_runner(void *mbox_uncast)
{
struct mbox *mbox = (struct mbox *) mbox_uncast;
void *contents = NULL;
@@ -58,15 +54,12 @@ START_TEST( test_receive_blocks_until_post )
mbox_post(mbox, deadbeef);
fail_unless(0 == pthread_join(receiver, &retval),
"Failed to join the receiver thread");
fail_unless( retval == deadbeef,
"Return value was wrong" );
fail_unless(retval == deadbeef, "Return value was wrong");
}
END_TEST
Suite* mbox_suite(void)
END_TEST Suite * mbox_suite(void)
{
Suite *s = suite_create("mbox");
TCase *tc_create = tcase_create("create");
@@ -101,4 +94,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -13,13 +13,13 @@ START_TEST(test_init_passwd)
memset(init_raw.passwd, 0, 8);
nbd_h2r_init(&init, &init_raw);
fail_unless( memcmp( init.passwd, INIT_PASSWD, 8 ) == 0, "The password was not copied." );
fail_unless( memcmp( init_raw.passwd, INIT_PASSWD, 8 ) == 0, "The password was not copied back." );
fail_unless(memcmp(init.passwd, INIT_PASSWD, 8) == 0,
"The password was not copied.");
fail_unless(memcmp(init_raw.passwd, INIT_PASSWD, 8) == 0,
"The password was not copied back.");
}
END_TEST
START_TEST(test_init_magic)
END_TEST START_TEST(test_init_magic)
{
struct nbd_init_raw init_raw;
struct nbd_init init;
@@ -30,12 +30,11 @@ START_TEST(test_init_magic)
init.magic = 67890;
nbd_h2r_init(&init, &init_raw);
fail_unless( htobe64( 67890 ) == init_raw.magic, "Magic was not converted back." );
fail_unless(htobe64(67890) == init_raw.magic,
"Magic was not converted back.");
}
END_TEST
START_TEST(test_init_size)
END_TEST START_TEST(test_init_size)
{
struct nbd_init_raw init_raw;
struct nbd_init init;
@@ -46,27 +45,27 @@ START_TEST(test_init_size)
init.size = 67890;
nbd_h2r_init(&init, &init_raw);
fail_unless( htobe64( 67890 ) == init_raw.size, "Size was not converted back." );
fail_unless(htobe64(67890) == init_raw.size,
"Size was not converted back.");
}
END_TEST
START_TEST(test_request_magic )
END_TEST START_TEST(test_request_magic)
{
struct nbd_request_raw request_raw;
struct nbd_request request;
request_raw.magic = 12345;
nbd_r2h_request(&request_raw, &request);
fail_unless( be32toh( 12345 ) == request.magic, "Magic was not converted." );
fail_unless(be32toh(12345) == request.magic,
"Magic was not converted.");
request.magic = 67890;
nbd_h2r_request(&request, &request_raw);
fail_unless( htobe32( 67890 ) == request_raw.magic, "Magic was not converted back." );
fail_unless(htobe32(67890) == request_raw.magic,
"Magic was not converted back.");
}
END_TEST
START_TEST(test_request_type)
END_TEST START_TEST(test_request_type)
{
struct nbd_request_raw request_raw;
struct nbd_request request;
@@ -77,30 +76,27 @@ START_TEST(test_request_type)
request.type = 234;
nbd_h2r_request(&request, &request_raw);
fail_unless( htobe16( 234 ) == request_raw.type, "Type was not converted back." );
fail_unless(htobe16(234) == request_raw.type,
"Type was not converted back.");
}
END_TEST
START_TEST(test_request_flags)
END_TEST START_TEST(test_request_flags)
{
struct nbd_request_raw request_raw;
struct nbd_request request;
request_raw.flags = 123;
nbd_r2h_request(&request_raw, &request);
fail_unless( be16toh( 123 ) == request.flags, "Flags were not converted." );
fail_unless(be16toh(123) == request.flags,
"Flags were not converted.");
request.flags = 234;
nbd_h2r_request(&request, &request_raw);
fail_unless( htobe16( 234 ) == request_raw.flags, "Flags were not converted back." );
fail_unless(htobe16(234) == request_raw.flags,
"Flags were not converted back.");
}
END_TEST
START_TEST(test_request_handle)
END_TEST START_TEST(test_request_handle)
{
struct nbd_request_raw request_raw;
struct nbd_request request;
@@ -111,14 +107,13 @@ START_TEST(test_request_handle)
request_raw.handle.w = 0;
nbd_h2r_request(&request, &request_raw);
fail_unless( memcmp( request.handle.b, "MYHANDLE", 8 ) == 0, "The handle was not copied." );
fail_unless( memcmp( request_raw.handle.b, "MYHANDLE", 8 ) == 0, "The handle was not copied back." );
fail_unless(memcmp(request.handle.b, "MYHANDLE", 8) == 0,
"The handle was not copied.");
fail_unless(memcmp(request_raw.handle.b, "MYHANDLE", 8) == 0,
"The handle was not copied back.");
}
END_TEST
START_TEST(test_request_from )
END_TEST START_TEST(test_request_from)
{
struct nbd_request_raw request_raw;
struct nbd_request request;
@@ -129,13 +124,11 @@ START_TEST(test_request_from )
request.from = 67890;
nbd_h2r_request(&request, &request_raw);
fail_unless( htobe64( 67890 ) == request_raw.from, "From was not converted back." );
fail_unless(htobe64(67890) == request_raw.from,
"From was not converted back.");
}
END_TEST
START_TEST(test_request_len )
END_TEST START_TEST(test_request_len)
{
struct nbd_request_raw request_raw;
struct nbd_request request;
@@ -146,12 +139,11 @@ START_TEST(test_request_len )
request.len = 67890;
nbd_h2r_request(&request, &request_raw);
fail_unless( htobe32( 67890 ) == request_raw.len, "Type was not converted back." );
fail_unless(htobe32(67890) == request_raw.len,
"Type was not converted back.");
}
END_TEST
START_TEST(test_reply_magic )
END_TEST START_TEST(test_reply_magic)
{
struct nbd_reply_raw reply_raw;
struct nbd_reply reply;
@@ -162,12 +154,11 @@ START_TEST(test_reply_magic )
reply.magic = 67890;
nbd_h2r_reply(&reply, &reply_raw);
fail_unless( htobe32( 67890 ) == reply_raw.magic, "Magic was not converted back." );
fail_unless(htobe32(67890) == reply_raw.magic,
"Magic was not converted back.");
}
END_TEST
START_TEST(test_reply_error )
END_TEST START_TEST(test_reply_error)
{
struct nbd_reply_raw reply_raw;
struct nbd_reply reply;
@@ -178,11 +169,11 @@ START_TEST(test_reply_error )
reply.error = 67890;
nbd_h2r_reply(&reply, &reply_raw);
fail_unless( htobe32( 67890 ) == reply_raw.error, "Error was not converted back." );
fail_unless(htobe32(67890) == reply_raw.error,
"Error was not converted back.");
}
END_TEST
START_TEST(test_reply_handle)
END_TEST START_TEST(test_reply_handle)
{
struct nbd_reply_raw reply_raw;
struct nbd_reply reply;
@@ -193,13 +184,13 @@ START_TEST(test_reply_handle)
reply_raw.handle.w = 0;
nbd_h2r_reply(&reply, &reply_raw);
fail_unless( memcmp( reply.handle.b, "MYHANDLE", 8 ) == 0, "The handle was not copied." );
fail_unless( memcmp( reply_raw.handle.b, "MYHANDLE", 8 ) == 0, "The handle was not copied back." );
fail_unless(memcmp(reply.handle.b, "MYHANDLE", 8) == 0,
"The handle was not copied.");
fail_unless(memcmp(reply_raw.handle.b, "MYHANDLE", 8) == 0,
"The handle was not copied back.");
}
END_TEST
START_TEST( test_convert_from )
END_TEST START_TEST(test_convert_from)
{
/* Check that we can correctly pull numbers out of an
* nbd_request_raw */
@@ -216,9 +207,8 @@ START_TEST( test_convert_from )
fail_unless(target == request.from, "from was wrong");
}
END_TEST
Suite *nbdtypes_suite(void)
END_TEST Suite * nbdtypes_suite(void)
{
Suite *s = suite_create("nbdtypes");
TCase *tc_init = tcase_create("nbd_init");
@@ -257,4 +247,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -11,11 +11,8 @@ START_TEST( test_can_parse_ip_address_twice )
parse_ip_to_sockaddr(&saddr, ip_address);
parse_ip_to_sockaddr(&saddr, ip_address);
}
END_TEST
Suite* parse_suite(void)
END_TEST Suite * parse_suite(void)
{
Suite *s = suite_create("parse");
TCase *tc_create = tcase_create("ip_to_sockaddr");
@@ -44,4 +41,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -54,8 +54,7 @@ void * responder( void *respond_uncast )
nbd_r2h_request(&request_raw, &resp->received);
if (resp->do_fail) {
fd_write_reply(sock_fd, wrong_handle, 0);
}
else {
} else {
fd_write_reply(sock_fd, resp->received.handle.w, 0);
}
write(sock_fd, "12345678", 8);
@@ -66,17 +65,20 @@ void * responder( void *respond_uncast )
struct respond *respond_create(int do_fail)
{
struct respond * respond = (struct respond *)calloc( 1, sizeof( struct respond ) );
struct respond *respond =
(struct respond *) calloc(1, sizeof(struct respond));
socketpair(PF_UNIX, SOCK_STREAM, 0, respond->sock_fds);
respond->do_fail = do_fail;
pthread_attr_init(&respond->thread_attr);
pthread_create( &respond->thread_id, &respond->thread_attr, responder, respond );
pthread_create(&respond->thread_id, &respond->thread_attr, responder,
respond);
return respond;
}
void respond_destroy( struct respond * respond ){
void respond_destroy(struct respond *respond)
{
NULLCHECK(respond);
pthread_join(respond->thread_id, NULL);
@@ -119,10 +121,8 @@ START_TEST( test_rejects_mismatched_handle )
fail_unless(marker == 1, "Error handler wasn't called");
}
END_TEST
START_TEST( test_accepts_matched_handle )
END_TEST START_TEST(test_accepts_matched_handle)
{
struct respond *respond = respond_create(0);
@@ -133,10 +133,8 @@ START_TEST( test_accepts_matched_handle )
respond_destroy(respond);
}
END_TEST
START_TEST( test_disconnect_doesnt_read_reply )
END_TEST START_TEST(test_disconnect_doesnt_read_reply)
{
struct respond *respond = respond_create(1);
@@ -144,10 +142,8 @@ START_TEST( test_disconnect_doesnt_read_reply )
respond_destroy(respond);
}
END_TEST
Suite* readwrite_suite(void)
END_TEST Suite * readwrite_suite(void)
{
Suite *s = suite_create("readwrite");
TCase *tc_transfer = tcase_create("entrust");
@@ -162,7 +158,8 @@ Suite* readwrite_suite(void)
* because we want to know that the sender won't even try to
* read the response.
*/
tcase_add_exit_test( tc_disconnect, test_disconnect_doesnt_read_reply,0 );
tcase_add_exit_test(tc_disconnect, test_disconnect_doesnt_read_reply,
0);
suite_add_tcase(s, tc_transfer);
suite_add_tcase(s, tc_disconnect);
@@ -190,4 +187,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -22,10 +22,8 @@ START_TEST( test_opens_pipe )
fail_unless(buf[0] == '1', "Pipe does not seem to be open;");
self_pipe_destroy(sig);
}
END_TEST
void * signal_thread( void * thing )
END_TEST void *signal_thread(void *thing)
{
struct self_pipe *sig = (struct self_pipe *) thing;
usleep(100000);
@@ -63,24 +61,21 @@ START_TEST( test_signals )
}
self_pipe_signal_clear(sig);
fail_unless( self_pipe_fd_isset( sig, &fds ), "Signalled pipe was not FD_ISSET." );
fail_unless(self_pipe_fd_isset(sig, &fds),
"Signalled pipe was not FD_ISSET.");
pthread_join(signal_thread_id, NULL);
self_pipe_destroy(sig);
}
END_TEST
START_TEST( test_clear_returns_immediately )
END_TEST START_TEST(test_clear_returns_immediately)
{
struct self_pipe *sig;
sig = self_pipe_create();
fail_unless(0 == self_pipe_signal_clear(sig), "Wrong clear result.");
}
END_TEST
START_TEST( test_destroy_closes_read_pipe )
END_TEST START_TEST(test_destroy_closes_read_pipe)
{
struct self_pipe *sig;
ssize_t read_len;
@@ -114,10 +109,8 @@ START_TEST( test_destroy_closes_read_pipe )
break;
}
}
END_TEST
START_TEST( test_destroy_closes_write_pipe )
END_TEST START_TEST(test_destroy_closes_write_pipe)
{
struct self_pipe *sig;
ssize_t write_len;
@@ -127,7 +120,8 @@ START_TEST( test_destroy_closes_write_pipe )
orig_write_fd = sig->write_fd;
self_pipe_destroy(sig);
while ( ( write_len = write( orig_write_fd, "", 0 ) ) == -1 && errno == EINTR );
while ((write_len = write(orig_write_fd, "", 0)) == -1
&& errno == EINTR);
switch (write_len) {
case 0:
@@ -156,11 +150,8 @@ START_TEST( test_destroy_closes_write_pipe )
}
}
END_TEST
Suite *self_pipe_suite(void)
END_TEST Suite * self_pipe_suite(void)
{
Suite *s = suite_create("self_pipe");
@@ -195,4 +186,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -54,7 +54,9 @@ void setup( void )
void teardown(void)
{
if( dummy_file ){ unlink( dummy_file ); }
if (dummy_file) {
unlink(dummy_file);
}
free(dummy_file);
dummy_file = NULL;
}
@@ -64,7 +66,9 @@ START_TEST( test_replaces_acl )
{
struct flexnbd flexnbd;
flexnbd.signal_fd = -1;
struct server * s = server_create( &flexnbd, "127.0.0.1", "0", dummy_file, 0, 0, NULL, 1, 0, 1 );
struct server *s =
server_create(&flexnbd, "127.0.0.1", "0", dummy_file, 0, 0, NULL,
1, 0, 1);
struct acl *new_acl = acl_create(0, NULL, 0);
server_replace_acl(s, new_acl);
@@ -72,14 +76,14 @@ START_TEST( test_replaces_acl )
myfail_unless(s->acl == new_acl, "ACL wasn't replaced.");
server_destroy(s);
}
END_TEST
START_TEST( test_signals_acl_updated )
END_TEST START_TEST(test_signals_acl_updated)
{
struct flexnbd flexnbd;
flexnbd.signal_fd = -1;
struct server * s = server_create( &flexnbd, "127.0.0.1", "0", dummy_file, 0, 0, NULL, 1, 0, 1 );
struct server *s =
server_create(&flexnbd, "127.0.0.1", "0", dummy_file, 0, 0, NULL,
1, 0, 1);
struct acl *new_acl = acl_create(0, NULL, 0);
server_replace_acl(s, new_acl);
@@ -88,10 +92,8 @@ START_TEST( test_signals_acl_updated )
"No signal sent.");
server_destroy(s);
}
END_TEST
int connect_client( char *addr, int actual_port, char *source_addr )
END_TEST int connect_client(char *addr, int actual_port, char *source_addr)
{
int client_fd = -1;
@@ -104,12 +106,15 @@ int connect_client( char *addr, int actual_port, char *source_addr )
memset(&hint, '\0', sizeof(struct addrinfo));
hint.ai_socktype = SOCK_STREAM;
myfail_if( getaddrinfo( addr, NULL, &hint, &ailist ) != 0, "getaddrinfo failed." );
myfail_if(getaddrinfo(addr, NULL, &hint, &ailist) != 0,
"getaddrinfo failed.");
int connected = 0;
for (aip = ailist; aip; aip = aip->ai_next) {
((struct sockaddr_in *)aip->ai_addr)->sin_port = htons( actual_port );
client_fd = socket( aip->ai_family, aip->ai_socktype, aip->ai_protocol );
((struct sockaddr_in *) aip->ai_addr)->sin_port =
htons(actual_port);
client_fd =
socket(aip->ai_family, aip->ai_socktype, aip->ai_protocol);
if (source_addr) {
struct sockaddr src;
@@ -120,7 +125,9 @@ int connect_client( char *addr, int actual_port, char *source_addr )
bind(client_fd, &src, sizeof(struct sockaddr_in6));
}
if( client_fd == -1) { continue; }
if (client_fd == -1) {
continue;
}
if (connect(client_fd, aip->ai_addr, aip->ai_addrlen) == 0) {
connected = 1;
break;
@@ -148,7 +155,9 @@ START_TEST( test_acl_update_closes_bad_client )
*/
struct flexnbd flexnbd;
flexnbd.signal_fd = -1;
struct server * s = server_create( &flexnbd, "127.0.0.7", "0", dummy_file, 0, 0, NULL, 1, 0, 1 );
struct server *s =
server_create(&flexnbd, "127.0.0.7", "0", dummy_file, 0, 0, NULL,
1, 0, 1);
struct acl *new_acl = acl_create(0, NULL, 1);
struct client *c;
struct client_tbl_entry *entry;
@@ -185,15 +194,15 @@ START_TEST( test_acl_update_closes_bad_client )
server_close_clients(s);
server_destroy(s);
}
END_TEST
START_TEST( test_acl_update_leaves_good_client )
END_TEST START_TEST(test_acl_update_leaves_good_client)
{
struct flexnbd flexnbd;
flexnbd.signal_fd = -1;
struct server * s = server_create( &flexnbd, "127.0.0.7", "0", dummy_file, 0, 0, NULL, 1, 0, 1 );
struct server *s =
server_create(&flexnbd, "127.0.0.7", "0", dummy_file, 0, 0, NULL,
1, 0, 1);
char *lines[] = { "127.0.0.1" };
struct acl *new_acl = acl_create(1, lines, 1);
@@ -228,10 +237,8 @@ START_TEST( test_acl_update_leaves_good_client )
server_close_clients(s);
server_destroy(s);
}
END_TEST
Suite* serve_suite(void)
END_TEST Suite * serve_suite(void)
{
Suite *s = suite_create("serve");
TCase *tc_acl_update = tcase_create("acl_update");
@@ -241,8 +248,10 @@ Suite* serve_suite(void)
tcase_add_test(tc_acl_update, test_replaces_acl);
tcase_add_test(tc_acl_update, test_signals_acl_updated);
tcase_add_exit_test(tc_acl_update, test_acl_update_closes_bad_client, 0);
tcase_add_exit_test(tc_acl_update, test_acl_update_leaves_good_client, 0);
tcase_add_exit_test(tc_acl_update, test_acl_update_closes_bad_client,
0);
tcase_add_exit_test(tc_acl_update, test_acl_update_leaves_good_client,
0);
suite_add_tcase(s, tc_acl_update);
@@ -261,4 +270,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -22,9 +22,8 @@ START_TEST( test_sockaddr_address_string_af_inet_converts_to_string )
ck_assert_str_eq("192.168.0.1 port 4777", testbuf);
}
END_TEST
START_TEST(test_sockaddr_address_string_af_inet6_converts_to_string)
{
struct sockaddr_in6 v6_raw;
@@ -43,8 +42,8 @@ START_TEST( test_sockaddr_address_string_af_inet6_converts_to_string )
ck_assert_str_eq("fe80::1 port 4777", testbuf);
}
END_TEST
END_TEST
/* We don't know what it is, so we just call it "???" and return NULL */
START_TEST(test_sockaddr_address_string_af_unspec_is_failure)
{
@@ -62,8 +61,8 @@ START_TEST( test_sockaddr_address_string_af_unspec_is_failure )
ck_assert_str_eq("???", testbuf);
}
END_TEST
END_TEST
/* This is a complete failure to parse, rather than a partial failure */
START_TEST(test_sockaddr_address_string_doesnt_overflow_short_buffer)
{
@@ -84,18 +83,22 @@ START_TEST( test_sockaddr_address_string_doesnt_overflow_short_buffer )
ck_assert_str_eq("??", testbuf);
}
END_TEST
Suite *sockutil_suite(void)
END_TEST Suite * sockutil_suite(void)
{
Suite *s = suite_create("sockutil");
TCase *tc_sockaddr_address_string = tcase_create("sockaddr_address_string");
TCase *tc_sockaddr_address_string =
tcase_create("sockaddr_address_string");
tcase_add_test(tc_sockaddr_address_string, test_sockaddr_address_string_af_inet_converts_to_string);
tcase_add_test(tc_sockaddr_address_string, test_sockaddr_address_string_af_inet6_converts_to_string);
tcase_add_test(tc_sockaddr_address_string, test_sockaddr_address_string_af_unspec_is_failure);
tcase_add_test(tc_sockaddr_address_string, test_sockaddr_address_string_doesnt_overflow_short_buffer);
tcase_add_test(tc_sockaddr_address_string,
test_sockaddr_address_string_af_inet_converts_to_string);
tcase_add_test(tc_sockaddr_address_string,
test_sockaddr_address_string_af_inet6_converts_to_string);
tcase_add_test(tc_sockaddr_address_string,
test_sockaddr_address_string_af_unspec_is_failure);
tcase_add_test(tc_sockaddr_address_string,
test_sockaddr_address_string_doesnt_overflow_short_buffer);
suite_add_tcase(s, tc_sockaddr_address_string);
return s;
@@ -112,4 +115,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -53,9 +53,8 @@ START_TEST( test_status_create )
status_destroy(status);
destroy_mock_server(server);
}
END_TEST
START_TEST( test_gets_has_control )
END_TEST START_TEST(test_gets_has_control)
{
struct server *server = mock_server();
server->success = 1;
@@ -66,10 +65,8 @@ START_TEST( test_gets_has_control )
status_destroy(status);
destroy_mock_server(server);
}
END_TEST
START_TEST( test_gets_is_mirroring )
END_TEST START_TEST(test_gets_is_mirroring)
{
struct server *server = mock_server();
struct status *status = status_create(server);
@@ -85,9 +82,8 @@ START_TEST( test_gets_is_mirroring )
status_destroy(status);
destroy_mock_server(server);
}
END_TEST
START_TEST( test_gets_clients_allowed )
END_TEST START_TEST(test_gets_clients_allowed)
{
struct server *server = mock_server();
struct status *status = status_create(server);
@@ -103,9 +99,8 @@ START_TEST( test_gets_clients_allowed )
destroy_mock_server(server);
}
END_TEST
START_TEST( test_gets_pid )
END_TEST START_TEST(test_gets_pid)
{
struct server *server = mock_server();
struct status *status = status_create(server);
@@ -115,9 +110,8 @@ START_TEST( test_gets_pid )
status_destroy(status);
destroy_mock_server(server);
}
END_TEST
START_TEST( test_gets_size )
END_TEST START_TEST(test_gets_size)
{
struct server *server = mock_server();
server->size = 1024;
@@ -129,9 +123,8 @@ START_TEST( test_gets_size )
status_destroy(status);
destroy_mock_server(server);
}
END_TEST
START_TEST( test_gets_migration_statistics )
END_TEST START_TEST(test_gets_migration_statistics)
{
struct server *server = mock_mirroring_server();
server->mirror->all_dirty = 16384;
@@ -143,34 +136,31 @@ START_TEST( test_gets_migration_statistics )
struct status *status = status_create(server);
fail_unless (
0 == status->migration_duration ||
fail_unless(0 == status->migration_duration ||
1 == status->migration_duration ||
2 == status->migration_duration,
"migration_duration is unreasonable!"
);
"migration_duration is unreasonable!");
fail_unless(
16384 / ( status->migration_duration + 1 ) == status->migration_speed,
"migration_speed not calculated correctly"
);
fail_unless(16384 / (status->migration_duration + 1) ==
status->migration_speed,
"migration_speed not calculated correctly");
fail_unless( 32768 == status->migration_speed_limit, "migration_speed_limit not read" );
fail_unless(32768 == status->migration_speed_limit,
"migration_speed_limit not read");
// ( size / current_bps ) + 1 happens to be 3 for this test
fail_unless( 3 == status->migration_seconds_left, "migration_seconds_left not gathered" );
fail_unless(3 == status->migration_seconds_left,
"migration_seconds_left not gathered");
status_destroy(status);
destroy_mock_server(server);
}
END_TEST
#define RENDER_TEST_SETUP \
struct status status; \
int fds[2]; \
pipe( fds );
void fail_unless_rendered(int fd, char *fragment)
{
char buf[1024] = { 0 };
@@ -203,9 +193,7 @@ void fail_if_rendered( int fd, char *fragment )
START_TEST(test_renders_has_control)
{
RENDER_TEST_SETUP
status.has_control = 1;
RENDER_TEST_SETUP status.has_control = 1;
status_write(&status, fds[1]);
fail_unless_rendered(fds[0], "has_control=true");
@@ -213,14 +201,10 @@ START_TEST( test_renders_has_control )
status_write(&status, fds[1]);
fail_unless_rendered(fds[0], "has_control=false");
}
END_TEST
START_TEST( test_renders_is_mirroring )
END_TEST START_TEST(test_renders_is_mirroring)
{
RENDER_TEST_SETUP
status.is_mirroring = 1;
RENDER_TEST_SETUP status.is_mirroring = 1;
status_write(&status, fds[1]);
fail_unless_rendered(fds[0], "is_mirroring=true");
@@ -228,13 +212,10 @@ START_TEST( test_renders_is_mirroring )
status_write(&status, fds[1]);
fail_unless_rendered(fds[0], "is_mirroring=false");
}
END_TEST
START_TEST( test_renders_clients_allowed )
END_TEST START_TEST(test_renders_clients_allowed)
{
RENDER_TEST_SETUP
status.clients_allowed = 1;
RENDER_TEST_SETUP status.clients_allowed = 1;
status_write(&status, fds[1]);
fail_unless_rendered(fds[0], "clients_allowed=true");
@@ -242,13 +223,10 @@ START_TEST( test_renders_clients_allowed )
status_write(&status, fds[1]);
fail_unless_rendered(fds[0], "clients_allowed=false");
}
END_TEST
START_TEST( test_renders_num_clients )
END_TEST START_TEST(test_renders_num_clients)
{
RENDER_TEST_SETUP
status.num_clients = 0;
RENDER_TEST_SETUP status.num_clients = 0;
status_write(&status, fds[1]);
fail_unless_rendered(fds[0], "num_clients=0");
@@ -257,34 +235,24 @@ START_TEST( test_renders_num_clients )
fail_unless_rendered(fds[0], "num_clients=4000");
}
END_TEST
START_TEST( test_renders_pid )
END_TEST START_TEST(test_renders_pid)
{
RENDER_TEST_SETUP
status.pid = 42;
RENDER_TEST_SETUP status.pid = 42;
status_write(&status, fds[1]);
fail_unless_rendered(fds[0], "pid=42");
}
END_TEST
START_TEST( test_renders_size )
END_TEST START_TEST(test_renders_size)
{
RENDER_TEST_SETUP
status.size = ( (uint64_t)1 << 33 );
RENDER_TEST_SETUP status.size = ((uint64_t) 1 << 33);
status_write(&status, fds[1]);
fail_unless_rendered(fds[0], "size=8589934592");
}
END_TEST
START_TEST( test_renders_migration_statistics )
END_TEST START_TEST(test_renders_migration_statistics)
{
RENDER_TEST_SETUP
status.is_mirroring = 0;
RENDER_TEST_SETUP status.is_mirroring = 0;
status.migration_duration = 8;
status.migration_speed = 40000000;
status.migration_speed_limit = 40000001;
@@ -325,10 +293,8 @@ START_TEST( test_renders_migration_statistics )
status_write(&status, fds[1]);
fail_if_rendered(fds[0], "migration_speed_limit");
}
END_TEST
Suite *status_suite(void)
END_TEST Suite * status_suite(void)
{
Suite *s = suite_create("status");
TCase *tc_create = tcase_create("create");
@@ -368,4 +334,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}

View File

@@ -14,7 +14,8 @@ struct cleanup_bucket {
struct cleanup_bucket bkt;
void bucket_init(void){
void bucket_init(void)
{
if (bkt.called_signal) {
self_pipe_destroy(bkt.called_signal);
}
@@ -31,7 +32,8 @@ int handler_called(void)
return self_pipe_signal_clear(bkt.called_signal);
}
void dummy_cleanup( struct cleanup_bucket * foo, int fatal __attribute__((unused)) )
void dummy_cleanup(struct cleanup_bucket *foo, int fatal
__attribute__ ((unused)))
{
if (NULL != foo) {
self_pipe_signal(foo->called_signal);
@@ -69,8 +71,7 @@ START_TEST( test_fatal_kills_process )
/* If we get here, just block so the test timeout fails
* us */
sleep(10);
}
else {
} else {
int kidret, kidstatus, result;
result = waitpid(pid, &kidret, 0);
fail_if(result < 0, "Wait failed.");
@@ -80,10 +81,8 @@ START_TEST( test_fatal_kills_process )
}
}
END_TEST
void * error_thread( void *nothing __attribute__((unused)) )
END_TEST void *error_thread(void *nothing __attribute__ ((unused)))
{
trigger_error();
return NULL;
@@ -101,10 +100,8 @@ START_TEST( test_error_doesnt_kill_process )
pthread_create(&tid, &attr, error_thread, NULL);
pthread_join(tid, NULL);
}
END_TEST
START_TEST( test_error_calls_handler )
END_TEST START_TEST(test_error_calls_handler)
{
bucket_init();
@@ -117,10 +114,8 @@ START_TEST( test_error_calls_handler )
pthread_join(tid, NULL);
fail_unless(handler_called(), "Handler wasn't called.");
}
END_TEST
START_TEST( test_fatal_doesnt_call_handler )
END_TEST START_TEST(test_fatal_doesnt_call_handler)
{
bucket_init();
@@ -129,8 +124,7 @@ START_TEST( test_fatal_doesnt_call_handler )
kidpid = fork();
if (kidpid == 0) {
trigger_fatal();
}
else {
} else {
int kidstatus;
int result = waitpid(kidpid, &kidstatus, 0);
fail_if(result < 0, "Wait failed");
@@ -138,10 +132,8 @@ START_TEST( test_fatal_doesnt_call_handler )
}
}
END_TEST
Suite* util_suite(void)
END_TEST Suite * util_suite(void)
{
Suite *s = suite_create("util");
TCase *tc_process = tcase_create("process");
@@ -170,4 +162,3 @@ int main(void)
srunner_free(sr);
return (number_failed == 0) ? 0 : 1;
}