- Use pjdlog for assertions and aborts as this will log assert/abort message

to syslog if we run in background.
- Asserts in proto.c that method we want to call is implemented and remove
  dummy methods from protocols implementation that are only there to abort
  the program with nice message.

MFC after:	1 week
This commit is contained in:
pjd 2011-01-31 18:32:17 +00:00
parent 34c6c0a739
commit d916d2edb5
9 changed files with 197 additions and 241 deletions

View File

@ -37,7 +37,6 @@ __FBSDID("$FreeBSD$");
#include <sys/stat.h>
#include <sys/wait.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <libutil.h>
@ -328,7 +327,7 @@ resource_needs_restart(const struct hast_resource *res0,
const struct hast_resource *res1)
{
assert(strcmp(res0->hr_name, res1->hr_name) == 0);
PJDLOG_ASSERT(strcmp(res0->hr_name, res1->hr_name) == 0);
if (strcmp(res0->hr_provname, res1->hr_provname) != 0)
return (true);
@ -353,9 +352,9 @@ resource_needs_reload(const struct hast_resource *res0,
const struct hast_resource *res1)
{
assert(strcmp(res0->hr_name, res1->hr_name) == 0);
assert(strcmp(res0->hr_provname, res1->hr_provname) == 0);
assert(strcmp(res0->hr_localpath, res1->hr_localpath) == 0);
PJDLOG_ASSERT(strcmp(res0->hr_name, res1->hr_name) == 0);
PJDLOG_ASSERT(strcmp(res0->hr_provname, res1->hr_provname) == 0);
PJDLOG_ASSERT(strcmp(res0->hr_localpath, res1->hr_localpath) == 0);
if (res0->hr_role != HAST_ROLE_PRIMARY)
return (false);
@ -377,7 +376,7 @@ resource_reload(const struct hast_resource *res)
struct nv *nvin, *nvout;
int error;
assert(res->hr_role == HAST_ROLE_PRIMARY);
PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
nvout = nv_alloc();
nv_add_uint8(nvout, HASTCTL_RELOAD, "cmd");
@ -524,7 +523,7 @@ hastd_reload(void)
if (strcmp(cres->hr_name, nres->hr_name) == 0)
break;
}
assert(cres != NULL);
PJDLOG_ASSERT(cres != NULL);
if (resource_needs_restart(cres, nres)) {
pjdlog_info("Resource %s configuration was modified, restarting it.",
cres->hr_name);
@ -700,10 +699,10 @@ listen_accept(void)
* we have to cancel those and accept the new connection.
*/
if (token == NULL) {
assert(res->hr_remoteout == NULL);
PJDLOG_ASSERT(res->hr_remoteout == NULL);
pjdlog_debug(1, "Initial connection from %s.", raddr);
if (res->hr_workerpid != 0) {
assert(res->hr_remotein == NULL);
PJDLOG_ASSERT(res->hr_remotein == NULL);
pjdlog_debug(1,
"Worker process exists (pid=%u), stopping it.",
(unsigned int)res->hr_workerpid);
@ -843,29 +842,29 @@ main_loop(void)
hastd_reload();
break;
default:
assert(!"invalid condition");
PJDLOG_ABORT("Unexpected signal (%d).", signo);
}
}
/* Setup descriptors for select(2). */
FD_ZERO(&rfds);
maxfd = fd = proto_descriptor(cfg->hc_controlconn);
assert(fd >= 0);
PJDLOG_ASSERT(fd >= 0);
FD_SET(fd, &rfds);
fd = proto_descriptor(cfg->hc_listenconn);
assert(fd >= 0);
PJDLOG_ASSERT(fd >= 0);
FD_SET(fd, &rfds);
maxfd = fd > maxfd ? fd : maxfd;
TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
if (res->hr_event == NULL)
continue;
fd = proto_descriptor(res->hr_event);
assert(fd >= 0);
PJDLOG_ASSERT(fd >= 0);
FD_SET(fd, &rfds);
maxfd = fd > maxfd ? fd : maxfd;
}
assert(maxfd + 1 <= (int)FD_SETSIZE);
PJDLOG_ASSERT(maxfd + 1 <= (int)FD_SETSIZE);
ret = select(maxfd + 1, &rfds, NULL, NULL, &seltimeout);
if (ret == 0)
hook_check();
@ -957,7 +956,7 @@ main(int argc, char *argv[])
}
cfg = yy_config_parse(cfgpath, true);
assert(cfg != NULL);
PJDLOG_ASSERT(cfg != NULL);
/*
* Restore default actions for interesting signals in case parent

View File

@ -40,7 +40,6 @@ __FBSDID("$FreeBSD$");
#include <geom/gate/g_gate.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
@ -266,7 +265,7 @@ primary_exit(int exitcode, const char *fmt, ...)
{
va_list ap;
assert(exitcode != EX_OK);
PJDLOG_ASSERT(exitcode != EX_OK);
va_start(ap, fmt);
pjdlogv_errno(LOG_ERR, fmt, ap);
va_end(ap);
@ -293,8 +292,8 @@ hast_activemap_flush(struct hast_resource *res)
size_t size;
buf = activemap_bitmap(res->hr_amp, &size);
assert(buf != NULL);
assert((size % res->hr_local_sectorsize) == 0);
PJDLOG_ASSERT(buf != NULL);
PJDLOG_ASSERT((size % res->hr_local_sectorsize) == 0);
if (pwrite(res->hr_localfd, buf, size, METADATA_SIZE) !=
(ssize_t)size) {
KEEP_ERRNO(pjdlog_errno(LOG_ERR,
@ -503,8 +502,8 @@ init_remote(struct hast_resource *res, struct proto_conn **inp,
uint32_t mapsize;
size_t size;
assert((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
assert(real_remote(res));
PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
PJDLOG_ASSERT(real_remote(res));
in = out = NULL;
errmsg = NULL;
@ -857,7 +856,7 @@ hastd_primary(struct hast_resource *res)
* very begining.
*/
error = pthread_create(&td, NULL, guard_thread, res);
assert(error == 0);
PJDLOG_ASSERT(error == 0);
/*
* Create the control thread before sending any event to the parent,
* as we can deadlock when parent sends control request to worker,
@ -867,19 +866,19 @@ hastd_primary(struct hast_resource *res)
* request response.
*/
error = pthread_create(&td, NULL, ctrl_thread, res);
assert(error == 0);
PJDLOG_ASSERT(error == 0);
if (real_remote(res) && init_remote(res, NULL, NULL))
sync_start();
error = pthread_create(&td, NULL, ggate_recv_thread, res);
assert(error == 0);
PJDLOG_ASSERT(error == 0);
error = pthread_create(&td, NULL, local_send_thread, res);
assert(error == 0);
PJDLOG_ASSERT(error == 0);
error = pthread_create(&td, NULL, remote_send_thread, res);
assert(error == 0);
PJDLOG_ASSERT(error == 0);
error = pthread_create(&td, NULL, remote_recv_thread, res);
assert(error == 0);
PJDLOG_ASSERT(error == 0);
error = pthread_create(&td, NULL, ggate_send_thread, res);
assert(error == 0);
PJDLOG_ASSERT(error == 0);
(void)sync_thread(res);
}
@ -932,14 +931,14 @@ remote_close(struct hast_resource *res, int ncomp)
* another thread can close connection in-between.
*/
if (!ISCONNECTED(res, ncomp)) {
assert(res->hr_remotein == NULL);
assert(res->hr_remoteout == NULL);
PJDLOG_ASSERT(res->hr_remotein == NULL);
PJDLOG_ASSERT(res->hr_remoteout == NULL);
rw_unlock(&hio_remote_lock[ncomp]);
return;
}
assert(res->hr_remotein != NULL);
assert(res->hr_remoteout != NULL);
PJDLOG_ASSERT(res->hr_remotein != NULL);
PJDLOG_ASSERT(res->hr_remoteout != NULL);
pjdlog_debug(2, "Closing incoming connection to %s.",
res->hr_remoteaddr);
@ -1051,7 +1050,7 @@ ggate_recv_thread(void *arg)
ncomp = 0;
} else /* if (res->hr_syncsrc ==
HAST_SYNCSRC_SECONDARY) */ {
assert(res->hr_syncsrc ==
PJDLOG_ASSERT(res->hr_syncsrc ==
HAST_SYNCSRC_SECONDARY);
/*
* This range is out-of-date on local component,
@ -1233,8 +1232,8 @@ keepalive_send(struct hast_resource *res, unsigned int ncomp)
if (!ISCONNECTED(res, ncomp))
return;
assert(res->hr_remotein != NULL);
assert(res->hr_remoteout != NULL);
PJDLOG_ASSERT(res->hr_remotein != NULL);
PJDLOG_ASSERT(res->hr_remoteout != NULL);
nv = nv_alloc();
nv_add_uint8(nv, HIO_KEEPALIVE, "cmd");
@ -1317,7 +1316,7 @@ remote_send_thread(void *arg)
length = 0;
break;
default:
assert(!"invalid condition");
PJDLOG_ASSERT(!"invalid condition");
abort();
}
nv = nv_alloc();
@ -1447,7 +1446,7 @@ remote_recv_thread(void *arg)
*/
mtx_lock(&hio_recv_list_lock[ncomp]);
hio = TAILQ_FIRST(&hio_recv_list[ncomp]);
assert(hio != NULL);
PJDLOG_ASSERT(hio != NULL);
TAILQ_REMOVE(&hio_recv_list[ncomp], hio,
hio_next[ncomp]);
mtx_unlock(&hio_recv_list_lock[ncomp]);
@ -1517,7 +1516,7 @@ remote_recv_thread(void *arg)
case BIO_FLUSH:
break;
default:
assert(!"invalid condition");
PJDLOG_ASSERT(!"invalid condition");
abort();
}
hio->hio_errors[ncomp] = 0;
@ -1769,7 +1768,7 @@ sync_thread(void *arg __unused)
/* Local component is 0 for now. */
ncomp = 0;
} else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
assert(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
/*
* This range is out-of-date on local component,
* so send request to the remote node.
@ -1816,7 +1815,7 @@ sync_thread(void *arg __unused)
/* Remote component is 1 for now. */
ncomp = 1;
} else /* if (res->hr_syncsrc == HAST_SYNCSRC_SECONDARY) */ {
assert(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
PJDLOG_ASSERT(res->hr_syncsrc == HAST_SYNCSRC_SECONDARY);
/*
* This range is out-of-date on local component,
* so we update it.
@ -1869,8 +1868,8 @@ primary_config_reload(struct hast_resource *res, struct nv *nv)
pjdlog_info("Reloading configuration...");
assert(res->hr_role == HAST_ROLE_PRIMARY);
assert(gres == res);
PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
PJDLOG_ASSERT(gres == res);
nv_assert(nv, "remoteaddr");
nv_assert(nv, "replication");
nv_assert(nv, "timeout");
@ -1971,16 +1970,16 @@ guard_one(struct hast_resource *res, unsigned int ncomp)
}
if (ISCONNECTED(res, ncomp)) {
assert(res->hr_remotein != NULL);
assert(res->hr_remoteout != NULL);
PJDLOG_ASSERT(res->hr_remotein != NULL);
PJDLOG_ASSERT(res->hr_remoteout != NULL);
rw_unlock(&hio_remote_lock[ncomp]);
pjdlog_debug(2, "remote_guard: Connection to %s is ok.",
res->hr_remoteaddr);
return;
}
assert(res->hr_remotein == NULL);
assert(res->hr_remoteout == NULL);
PJDLOG_ASSERT(res->hr_remotein == NULL);
PJDLOG_ASSERT(res->hr_remoteout == NULL);
/*
* Upgrade the lock. It doesn't have to be atomic as no other thread
* can change connection status from disconnected to connected.
@ -1991,9 +1990,9 @@ guard_one(struct hast_resource *res, unsigned int ncomp)
in = out = NULL;
if (init_remote(res, &in, &out)) {
rw_wlock(&hio_remote_lock[ncomp]);
assert(res->hr_remotein == NULL);
assert(res->hr_remoteout == NULL);
assert(in != NULL && out != NULL);
PJDLOG_ASSERT(res->hr_remotein == NULL);
PJDLOG_ASSERT(res->hr_remoteout == NULL);
PJDLOG_ASSERT(in != NULL && out != NULL);
res->hr_remotein = in;
res->hr_remoteout = out;
rw_unlock(&hio_remote_lock[ncomp]);
@ -2002,9 +2001,9 @@ guard_one(struct hast_resource *res, unsigned int ncomp)
sync_start();
} else {
/* Both connections should be NULL. */
assert(res->hr_remotein == NULL);
assert(res->hr_remoteout == NULL);
assert(in == NULL && out == NULL);
PJDLOG_ASSERT(res->hr_remotein == NULL);
PJDLOG_ASSERT(res->hr_remoteout == NULL);
PJDLOG_ASSERT(in == NULL && out == NULL);
pjdlog_debug(2, "remote_guard: Reconnect to %s failed.",
res->hr_remoteaddr);
}

View File

@ -34,10 +34,10 @@ __FBSDID("$FreeBSD$");
#include <sys/queue.h>
#include <sys/socket.h>
#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include "pjdlog.h"
#include "proto.h"
#include "proto_impl.h"
@ -62,7 +62,7 @@ proto_register(struct hast_proto *proto, bool isdefault)
if (!isdefault)
TAILQ_INSERT_HEAD(&protos, proto, hp_next);
else {
assert(!seen_default);
PJDLOG_ASSERT(!seen_default);
seen_default = true;
TAILQ_INSERT_TAIL(&protos, proto, hp_next);
}
@ -76,7 +76,7 @@ proto_common_setup(const char *addr, struct proto_conn **connp, int side)
void *ctx;
int ret;
assert(side == PROTO_SIDE_CLIENT || side == PROTO_SIDE_SERVER_LISTEN);
PJDLOG_ASSERT(side == PROTO_SIDE_CLIENT || side == PROTO_SIDE_SERVER_LISTEN);
conn = malloc(sizeof(*conn));
if (conn == NULL)
@ -127,10 +127,11 @@ proto_connect(struct proto_conn *conn)
{
int ret;
assert(conn != NULL);
assert(conn->pc_magic == PROTO_CONN_MAGIC);
assert(conn->pc_side == PROTO_SIDE_CLIENT);
assert(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_side == PROTO_SIDE_CLIENT);
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_connect != NULL);
ret = conn->pc_proto->hp_connect(conn->pc_ctx);
if (ret != 0) {
@ -154,10 +155,11 @@ proto_accept(struct proto_conn *conn, struct proto_conn **newconnp)
struct proto_conn *newconn;
int ret;
assert(conn != NULL);
assert(conn->pc_magic == PROTO_CONN_MAGIC);
assert(conn->pc_side == PROTO_SIDE_SERVER_LISTEN);
assert(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_side == PROTO_SIDE_SERVER_LISTEN);
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_accept != NULL);
newconn = malloc(sizeof(*newconn));
if (newconn == NULL)
@ -183,9 +185,10 @@ proto_send(const struct proto_conn *conn, const void *data, size_t size)
{
int ret;
assert(conn != NULL);
assert(conn->pc_magic == PROTO_CONN_MAGIC);
assert(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_send != NULL);
ret = conn->pc_proto->hp_send(conn->pc_ctx, data, size);
if (ret != 0) {
@ -200,9 +203,10 @@ proto_recv(const struct proto_conn *conn, void *data, size_t size)
{
int ret;
assert(conn != NULL);
assert(conn->pc_magic == PROTO_CONN_MAGIC);
assert(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_recv != NULL);
ret = conn->pc_proto->hp_recv(conn->pc_ctx, data, size);
if (ret != 0) {
@ -216,9 +220,10 @@ int
proto_descriptor(const struct proto_conn *conn)
{
assert(conn != NULL);
assert(conn->pc_magic == PROTO_CONN_MAGIC);
assert(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_descriptor != NULL);
return (conn->pc_proto->hp_descriptor(conn->pc_ctx));
}
@ -227,9 +232,10 @@ bool
proto_address_match(const struct proto_conn *conn, const char *addr)
{
assert(conn != NULL);
assert(conn->pc_magic == PROTO_CONN_MAGIC);
assert(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_address_match != NULL);
return (conn->pc_proto->hp_address_match(conn->pc_ctx, addr));
}
@ -238,9 +244,10 @@ void
proto_local_address(const struct proto_conn *conn, char *addr, size_t size)
{
assert(conn != NULL);
assert(conn->pc_magic == PROTO_CONN_MAGIC);
assert(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_local_address != NULL);
conn->pc_proto->hp_local_address(conn->pc_ctx, addr, size);
}
@ -249,9 +256,10 @@ void
proto_remote_address(const struct proto_conn *conn, char *addr, size_t size)
{
assert(conn != NULL);
assert(conn->pc_magic == PROTO_CONN_MAGIC);
assert(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_remote_address != NULL);
conn->pc_proto->hp_remote_address(conn->pc_ctx, addr, size);
}
@ -262,9 +270,9 @@ proto_timeout(const struct proto_conn *conn, int timeout)
struct timeval tv;
int fd;
assert(conn != NULL);
assert(conn->pc_magic == PROTO_CONN_MAGIC);
assert(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_proto != NULL);
fd = proto_descriptor(conn);
if (fd < 0)
@ -284,9 +292,10 @@ void
proto_close(struct proto_conn *conn)
{
assert(conn != NULL);
assert(conn->pc_magic == PROTO_CONN_MAGIC);
assert(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->hp_close != NULL);
conn->pc_proto->hp_close(conn->pc_ctx);
conn->pc_magic = 0;

View File

@ -33,11 +33,11 @@ __FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/socket.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include "pjdlog.h"
#include "proto_impl.h"
/* Maximum size of packet we want to use when sending data. */
@ -51,6 +51,10 @@ proto_common_send(int fd, const unsigned char *data, size_t size)
ssize_t done;
size_t sendsize;
PJDLOG_ASSERT(fd >= 0);
PJDLOG_ASSERT(data != NULL);
PJDLOG_ASSERT(size > 0);
do {
sendsize = size < MAX_SEND_SIZE ? size : MAX_SEND_SIZE;
done = send(fd, data, sendsize, MSG_NOSIGNAL);
@ -73,6 +77,10 @@ proto_common_recv(int fd, unsigned char *data, size_t size)
{
ssize_t done;
PJDLOG_ASSERT(fd >= 0);
PJDLOG_ASSERT(data != NULL);
PJDLOG_ASSERT(size > 0);
do {
done = recv(fd, data, size, MSG_WAITALL);
} while (done == -1 && errno == EINTR);

View File

@ -33,7 +33,6 @@ __FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/socket.h>
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
@ -42,6 +41,7 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
#include "hast.h"
#include "pjdlog.h"
#include "proto_impl.h"
#define SP_CTX_MAGIC 0x50c3741
@ -82,41 +82,14 @@ sp_client(const char *addr, void **ctxp)
return (0);
}
static int
sp_connect(void *ctx __unused)
{
assert(!"proto_connect() not supported on socketpairs");
abort();
}
static int
sp_server(const char *addr, void **ctxp __unused)
{
if (strcmp(addr, "socketpair://") != 0)
return (-1);
assert(!"proto_server() not supported on socketpairs");
abort();
}
static int
sp_accept(void *ctx __unused, void **newctxp __unused)
{
assert(!"proto_server() not supported on socketpairs");
abort();
}
static int
sp_send(void *ctx, const unsigned char *data, size_t size)
{
struct sp_ctx *spctx = ctx;
int fd;
assert(spctx != NULL);
assert(spctx->sp_magic == SP_CTX_MAGIC);
PJDLOG_ASSERT(spctx != NULL);
PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC);
switch (spctx->sp_side) {
case SP_SIDE_UNDEF:
@ -128,16 +101,17 @@ sp_send(void *ctx, const unsigned char *data, size_t size)
spctx->sp_side = SP_SIDE_CLIENT;
/* Close other end. */
close(spctx->sp_fd[1]);
spctx->sp_fd[1] = -1;
case SP_SIDE_CLIENT:
assert(spctx->sp_fd[0] >= 0);
PJDLOG_ASSERT(spctx->sp_fd[0] >= 0);
fd = spctx->sp_fd[0];
break;
case SP_SIDE_SERVER:
assert(spctx->sp_fd[1] >= 0);
PJDLOG_ASSERT(spctx->sp_fd[1] >= 0);
fd = spctx->sp_fd[1];
break;
default:
abort();
PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side);
}
/* Someone is just trying to decide about side. */
@ -153,8 +127,8 @@ sp_recv(void *ctx, unsigned char *data, size_t size)
struct sp_ctx *spctx = ctx;
int fd;
assert(spctx != NULL);
assert(spctx->sp_magic == SP_CTX_MAGIC);
PJDLOG_ASSERT(spctx != NULL);
PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC);
switch (spctx->sp_side) {
case SP_SIDE_UNDEF:
@ -166,16 +140,17 @@ sp_recv(void *ctx, unsigned char *data, size_t size)
spctx->sp_side = SP_SIDE_SERVER;
/* Close other end. */
close(spctx->sp_fd[0]);
spctx->sp_fd[0] = -1;
case SP_SIDE_SERVER:
assert(spctx->sp_fd[1] >= 0);
PJDLOG_ASSERT(spctx->sp_fd[1] >= 0);
fd = spctx->sp_fd[1];
break;
case SP_SIDE_CLIENT:
assert(spctx->sp_fd[0] >= 0);
PJDLOG_ASSERT(spctx->sp_fd[0] >= 0);
fd = spctx->sp_fd[0];
break;
default:
abort();
PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side);
}
/* Someone is just trying to decide about side. */
@ -190,47 +165,21 @@ sp_descriptor(const void *ctx)
{
const struct sp_ctx *spctx = ctx;
assert(spctx != NULL);
assert(spctx->sp_magic == SP_CTX_MAGIC);
assert(spctx->sp_side == SP_SIDE_CLIENT ||
PJDLOG_ASSERT(spctx != NULL);
PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC);
PJDLOG_ASSERT(spctx->sp_side == SP_SIDE_CLIENT ||
spctx->sp_side == SP_SIDE_SERVER);
switch (spctx->sp_side) {
case SP_SIDE_CLIENT:
assert(spctx->sp_fd[0] >= 0);
PJDLOG_ASSERT(spctx->sp_fd[0] >= 0);
return (spctx->sp_fd[0]);
case SP_SIDE_SERVER:
assert(spctx->sp_fd[1] >= 0);
PJDLOG_ASSERT(spctx->sp_fd[1] >= 0);
return (spctx->sp_fd[1]);
}
abort();
}
static bool
sp_address_match(const void *ctx __unused, const char *addr __unused)
{
assert(!"proto_address_match() not supported on socketpairs");
abort();
}
static void
sp_local_address(const void *ctx __unused, char *addr __unused,
size_t size __unused)
{
assert(!"proto_local_address() not supported on socketpairs");
abort();
}
static void
sp_remote_address(const void *ctx __unused, char *addr __unused,
size_t size __unused)
{
assert(!"proto_remote_address() not supported on socketpairs");
abort();
PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side);
}
static void
@ -238,22 +187,32 @@ sp_close(void *ctx)
{
struct sp_ctx *spctx = ctx;
assert(spctx != NULL);
assert(spctx->sp_magic == SP_CTX_MAGIC);
PJDLOG_ASSERT(spctx != NULL);
PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC);
switch (spctx->sp_side) {
case SP_SIDE_UNDEF:
PJDLOG_ASSERT(spctx->sp_fd[0] >= 0);
close(spctx->sp_fd[0]);
spctx->sp_fd[0] = -1;
PJDLOG_ASSERT(spctx->sp_fd[1] >= 0);
close(spctx->sp_fd[1]);
spctx->sp_fd[1] = -1;
break;
case SP_SIDE_CLIENT:
PJDLOG_ASSERT(spctx->sp_fd[0] >= 0);
close(spctx->sp_fd[0]);
spctx->sp_fd[0] = -1;
PJDLOG_ASSERT(spctx->sp_fd[1] == -1);
break;
case SP_SIDE_SERVER:
PJDLOG_ASSERT(spctx->sp_fd[1] >= 0);
close(spctx->sp_fd[1]);
spctx->sp_fd[1] = -1;
PJDLOG_ASSERT(spctx->sp_fd[0] == -1);
break;
default:
abort();
PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side);
}
spctx->sp_magic = 0;
@ -263,15 +222,9 @@ sp_close(void *ctx)
static struct hast_proto sp_proto = {
.hp_name = "socketpair",
.hp_client = sp_client,
.hp_connect = sp_connect,
.hp_server = sp_server,
.hp_accept = sp_accept,
.hp_send = sp_send,
.hp_recv = sp_recv,
.hp_descriptor = sp_descriptor,
.hp_address_match = sp_address_match,
.hp_local_address = sp_local_address,
.hp_remote_address = sp_remote_address,
.hp_close = sp_close
};

View File

@ -35,7 +35,6 @@ __FBSDID("$FreeBSD$");
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
@ -230,10 +229,10 @@ tcp4_connect(void *ctx)
socklen_t esize;
int error, flags, ret;
assert(tctx != NULL);
assert(tctx->tc_magic == TCP4_CTX_MAGIC);
assert(tctx->tc_side == TCP4_SIDE_CLIENT);
assert(tctx->tc_fd >= 0);
PJDLOG_ASSERT(tctx != NULL);
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
PJDLOG_ASSERT(tctx->tc_side == TCP4_SIDE_CLIENT);
PJDLOG_ASSERT(tctx->tc_fd >= 0);
flags = fcntl(tctx->tc_fd, F_GETFL);
if (flags == -1) {
@ -282,8 +281,8 @@ tcp4_connect(void *ctx)
pjdlog_common(LOG_DEBUG, 1, errno, "select() failed");
goto done;
}
assert(ret > 0);
assert(FD_ISSET(tctx->tc_fd, &fdset));
PJDLOG_ASSERT(ret > 0);
PJDLOG_ASSERT(FD_ISSET(tctx->tc_fd, &fdset));
esize = sizeof(error);
if (getsockopt(tctx->tc_fd, SOL_SOCKET, SO_ERROR, &error,
&esize) == -1) {
@ -349,10 +348,10 @@ tcp4_accept(void *ctx, void **newctxp)
socklen_t fromlen;
int ret;
assert(tctx != NULL);
assert(tctx->tc_magic == TCP4_CTX_MAGIC);
assert(tctx->tc_side == TCP4_SIDE_SERVER_LISTEN);
assert(tctx->tc_fd >= 0);
PJDLOG_ASSERT(tctx != NULL);
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
PJDLOG_ASSERT(tctx->tc_side == TCP4_SIDE_SERVER_LISTEN);
PJDLOG_ASSERT(tctx->tc_fd >= 0);
newtctx = malloc(sizeof(*newtctx));
if (newtctx == NULL)
@ -379,9 +378,9 @@ tcp4_send(void *ctx, const unsigned char *data, size_t size)
{
struct tcp4_ctx *tctx = ctx;
assert(tctx != NULL);
assert(tctx->tc_magic == TCP4_CTX_MAGIC);
assert(tctx->tc_fd >= 0);
PJDLOG_ASSERT(tctx != NULL);
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
PJDLOG_ASSERT(tctx->tc_fd >= 0);
return (proto_common_send(tctx->tc_fd, data, size));
}
@ -391,9 +390,9 @@ tcp4_recv(void *ctx, unsigned char *data, size_t size)
{
struct tcp4_ctx *tctx = ctx;
assert(tctx != NULL);
assert(tctx->tc_magic == TCP4_CTX_MAGIC);
assert(tctx->tc_fd >= 0);
PJDLOG_ASSERT(tctx != NULL);
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
PJDLOG_ASSERT(tctx->tc_fd >= 0);
return (proto_common_recv(tctx->tc_fd, data, size));
}
@ -403,8 +402,8 @@ tcp4_descriptor(const void *ctx)
{
const struct tcp4_ctx *tctx = ctx;
assert(tctx != NULL);
assert(tctx->tc_magic == TCP4_CTX_MAGIC);
PJDLOG_ASSERT(tctx != NULL);
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
return (tctx->tc_fd);
}
@ -415,8 +414,8 @@ sin2str(struct sockaddr_in *sinp, char *addr, size_t size)
in_addr_t ip;
unsigned int port;
assert(addr != NULL);
assert(sinp->sin_family == AF_INET);
PJDLOG_ASSERT(addr != NULL);
PJDLOG_ASSERT(sinp->sin_family == AF_INET);
ip = ntohl(sinp->sin_addr.s_addr);
port = ntohs(sinp->sin_port);
@ -433,8 +432,8 @@ tcp4_address_match(const void *ctx, const char *addr)
socklen_t sinlen;
in_addr_t ip1, ip2;
assert(tctx != NULL);
assert(tctx->tc_magic == TCP4_CTX_MAGIC);
PJDLOG_ASSERT(tctx != NULL);
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
if (tcp4_addr(addr, &sin) != 0)
return (false);
@ -455,8 +454,8 @@ tcp4_local_address(const void *ctx, char *addr, size_t size)
struct sockaddr_in sin;
socklen_t sinlen;
assert(tctx != NULL);
assert(tctx->tc_magic == TCP4_CTX_MAGIC);
PJDLOG_ASSERT(tctx != NULL);
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
sinlen = sizeof(sin);
if (getsockname(tctx->tc_fd, (struct sockaddr *)&sin, &sinlen) < 0) {
@ -473,8 +472,8 @@ tcp4_remote_address(const void *ctx, char *addr, size_t size)
struct sockaddr_in sin;
socklen_t sinlen;
assert(tctx != NULL);
assert(tctx->tc_magic == TCP4_CTX_MAGIC);
PJDLOG_ASSERT(tctx != NULL);
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
sinlen = sizeof(sin);
if (getpeername(tctx->tc_fd, (struct sockaddr *)&sin, &sinlen) < 0) {
@ -489,8 +488,8 @@ tcp4_close(void *ctx)
{
struct tcp4_ctx *tctx = ctx;
assert(tctx != NULL);
assert(tctx->tc_magic == TCP4_CTX_MAGIC);
PJDLOG_ASSERT(tctx != NULL);
PJDLOG_ASSERT(tctx->tc_magic == TCP4_CTX_MAGIC);
if (tctx->tc_fd >= 0)
close(tctx->tc_fd);

View File

@ -34,7 +34,6 @@ __FBSDID("$FreeBSD$");
#include <sys/un.h>
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
@ -128,10 +127,10 @@ uds_connect(void *ctx)
{
struct uds_ctx *uctx = ctx;
assert(uctx != NULL);
assert(uctx->uc_magic == UDS_CTX_MAGIC);
assert(uctx->uc_side == UDS_SIDE_CLIENT);
assert(uctx->uc_fd >= 0);
PJDLOG_ASSERT(uctx != NULL);
PJDLOG_ASSERT(uctx->uc_magic == UDS_CTX_MAGIC);
PJDLOG_ASSERT(uctx->uc_side == UDS_SIDE_CLIENT);
PJDLOG_ASSERT(uctx->uc_fd >= 0);
if (connect(uctx->uc_fd, (struct sockaddr *)&uctx->uc_sun,
sizeof(uctx->uc_sun)) < 0) {
@ -177,10 +176,10 @@ uds_accept(void *ctx, void **newctxp)
socklen_t fromlen;
int ret;
assert(uctx != NULL);
assert(uctx->uc_magic == UDS_CTX_MAGIC);
assert(uctx->uc_side == UDS_SIDE_SERVER_LISTEN);
assert(uctx->uc_fd >= 0);
PJDLOG_ASSERT(uctx != NULL);
PJDLOG_ASSERT(uctx->uc_magic == UDS_CTX_MAGIC);
PJDLOG_ASSERT(uctx->uc_side == UDS_SIDE_SERVER_LISTEN);
PJDLOG_ASSERT(uctx->uc_fd >= 0);
newuctx = malloc(sizeof(*newuctx));
if (newuctx == NULL)
@ -207,9 +206,9 @@ uds_send(void *ctx, const unsigned char *data, size_t size)
{
struct uds_ctx *uctx = ctx;
assert(uctx != NULL);
assert(uctx->uc_magic == UDS_CTX_MAGIC);
assert(uctx->uc_fd >= 0);
PJDLOG_ASSERT(uctx != NULL);
PJDLOG_ASSERT(uctx->uc_magic == UDS_CTX_MAGIC);
PJDLOG_ASSERT(uctx->uc_fd >= 0);
return (proto_common_send(uctx->uc_fd, data, size));
}
@ -219,9 +218,9 @@ uds_recv(void *ctx, unsigned char *data, size_t size)
{
struct uds_ctx *uctx = ctx;
assert(uctx != NULL);
assert(uctx->uc_magic == UDS_CTX_MAGIC);
assert(uctx->uc_fd >= 0);
PJDLOG_ASSERT(uctx != NULL);
PJDLOG_ASSERT(uctx->uc_magic == UDS_CTX_MAGIC);
PJDLOG_ASSERT(uctx->uc_fd >= 0);
return (proto_common_recv(uctx->uc_fd, data, size));
}
@ -231,20 +230,12 @@ uds_descriptor(const void *ctx)
{
const struct uds_ctx *uctx = ctx;
assert(uctx != NULL);
assert(uctx->uc_magic == UDS_CTX_MAGIC);
PJDLOG_ASSERT(uctx != NULL);
PJDLOG_ASSERT(uctx->uc_magic == UDS_CTX_MAGIC);
return (uctx->uc_fd);
}
static bool
uds_address_match(const void *ctx __unused, const char *addr __unused)
{
assert(!"proto_address_match() not supported on UNIX domain sockets");
abort();
}
static void
uds_local_address(const void *ctx, char *addr, size_t size)
{
@ -252,16 +243,16 @@ uds_local_address(const void *ctx, char *addr, size_t size)
struct sockaddr_un sun;
socklen_t sunlen;
assert(uctx != NULL);
assert(uctx->uc_magic == UDS_CTX_MAGIC);
assert(addr != NULL);
PJDLOG_ASSERT(uctx != NULL);
PJDLOG_ASSERT(uctx->uc_magic == UDS_CTX_MAGIC);
PJDLOG_ASSERT(addr != NULL);
sunlen = sizeof(sun);
if (getsockname(uctx->uc_fd, (struct sockaddr *)&sun, &sunlen) < 0) {
PJDLOG_VERIFY(strlcpy(addr, "N/A", size) < size);
return;
}
assert(sun.sun_family == AF_UNIX);
PJDLOG_ASSERT(sun.sun_family == AF_UNIX);
if (sun.sun_path[0] == '\0') {
PJDLOG_VERIFY(strlcpy(addr, "N/A", size) < size);
return;
@ -276,16 +267,16 @@ uds_remote_address(const void *ctx, char *addr, size_t size)
struct sockaddr_un sun;
socklen_t sunlen;
assert(uctx != NULL);
assert(uctx->uc_magic == UDS_CTX_MAGIC);
assert(addr != NULL);
PJDLOG_ASSERT(uctx != NULL);
PJDLOG_ASSERT(uctx->uc_magic == UDS_CTX_MAGIC);
PJDLOG_ASSERT(addr != NULL);
sunlen = sizeof(sun);
if (getpeername(uctx->uc_fd, (struct sockaddr *)&sun, &sunlen) < 0) {
PJDLOG_VERIFY(strlcpy(addr, "N/A", size) < size);
return;
}
assert(sun.sun_family == AF_UNIX);
PJDLOG_ASSERT(sun.sun_family == AF_UNIX);
if (sun.sun_path[0] == '\0') {
PJDLOG_VERIFY(strlcpy(addr, "N/A", size) < size);
return;
@ -298,8 +289,8 @@ uds_close(void *ctx)
{
struct uds_ctx *uctx = ctx;
assert(uctx != NULL);
assert(uctx->uc_magic == UDS_CTX_MAGIC);
PJDLOG_ASSERT(uctx != NULL);
PJDLOG_ASSERT(uctx->uc_magic == UDS_CTX_MAGIC);
if (uctx->uc_fd >= 0)
close(uctx->uc_fd);
@ -317,7 +308,6 @@ static struct hast_proto uds_proto = {
.hp_send = uds_send,
.hp_recv = uds_recv,
.hp_descriptor = uds_descriptor,
.hp_address_match = uds_address_match,
.hp_local_address = uds_local_address,
.hp_remote_address = uds_remote_address,
.hp_close = uds_close

View File

@ -37,7 +37,6 @@ __FBSDID("$FreeBSD$");
#include <sys/disk.h>
#include <sys/stat.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
@ -248,7 +247,7 @@ init_remote(struct hast_resource *res, struct nv *nvin)
* there is no need to synchronize anything. If primary node
* done any writes already we have to synchronize everything.
*/
assert(res->hr_secondary_localcnt == 0);
PJDLOG_ASSERT(res->hr_secondary_localcnt == 0);
res->hr_resuid = resuid;
if (metadata_write(res) < 0)
exit(EX_NOINPUT);
@ -307,7 +306,7 @@ init_remote(struct hast_resource *res, struct nv *nvin)
* This should never happen in practise, but we will perform
* full synchronization.
*/
assert(res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
PJDLOG_ASSERT(res->hr_secondary_localcnt < res->hr_primary_remotecnt ||
res->hr_primary_localcnt < res->hr_secondary_remotecnt);
mapsize = activemap_calc_ondisk_size(res->hr_local_mediasize -
METADATA_SIZE, res->hr_extentsize,
@ -425,15 +424,15 @@ hastd_secondary(struct hast_resource *res, struct nv *nvin)
* request response.
*/
error = pthread_create(&td, NULL, ctrl_thread, res);
assert(error == 0);
PJDLOG_ASSERT(error == 0);
init_remote(res, nvin);
event_send(res, EVENT_CONNECT);
error = pthread_create(&td, NULL, recv_thread, res);
assert(error == 0);
PJDLOG_ASSERT(error == 0);
error = pthread_create(&td, NULL, disk_thread, res);
assert(error == 0);
PJDLOG_ASSERT(error == 0);
(void)send_thread(res);
}
@ -555,7 +554,7 @@ secondary_exit(int exitcode, const char *fmt, ...)
{
va_list ap;
assert(exitcode != EX_OK);
PJDLOG_ASSERT(exitcode != EX_OK);
va_start(ap, fmt);
pjdlogv_errno(LOG_ERR, fmt, ap);
va_end(ap);

View File

@ -35,7 +35,6 @@ __FBSDID("$FreeBSD$");
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
@ -51,7 +50,8 @@ provinfo(struct hast_resource *res, bool dowrite)
{
struct stat sb;
assert(res->hr_localpath != NULL && res->hr_localpath[0] != '\0');
PJDLOG_ASSERT(res->hr_localpath != NULL &&
res->hr_localpath[0] != '\0');
if (res->hr_localfd == -1) {
res->hr_localfd = open(res->hr_localpath,