Page MenuHomeFreeBSD

D59465.diff
No OneTemporary

D59465.diff

diff --git a/sbin/hastd/hast_proto.c b/sbin/hastd/hast_proto.c
--- a/sbin/hastd/hast_proto.c
+++ b/sbin/hastd/hast_proto.c
@@ -34,7 +34,7 @@
#include <sys/endian.h>
#include <errno.h>
-#include <strings.h>
+#include <string.h>
#include <hast.h>
#include <ebuf.h>
@@ -83,7 +83,7 @@
bool freedata;
void *dptr, *hptr;
size_t hsize;
- int ret;
+ ssize_t ret;
dptr = (void *)(uintptr_t)data;
freedata = false;
@@ -114,10 +114,22 @@
goto end;
hptr = ebuf_data(eb, &hsize);
- if (proto_send(conn, hptr, hsize) == -1)
+ if ((ret = proto_send(conn, hptr, hsize)) < 0)
goto end;
- if (data != NULL && proto_send(conn, dptr, size) == -1)
+ if ((size_t)ret != hsize) {
+ errno = EPROTO;
+ ret = -1;
goto end;
+ }
+ if (data != NULL) {
+ if ((ret = proto_send(conn, dptr, size)) < 0)
+ goto end;
+ if ((size_t)ret != size) {
+ errno = EPROTO;
+ ret = -1;
+ goto end;
+ }
+ }
ret = 0;
end:
@@ -133,12 +145,17 @@
struct nv *nv;
struct ebuf *eb;
void *hptr;
+ ssize_t ret;
eb = NULL;
nv = NULL;
- if (proto_recv(conn, &hdr, sizeof(hdr)) == -1)
+ if ((ret = proto_recv(conn, &hdr, sizeof(hdr))) < 0)
goto fail;
+ if ((size_t)ret != sizeof(hdr)) {
+ errno = EPROTO;
+ goto fail;
+ }
if (hdr.version > HAST_PROTO_VERSION) {
errno = ERPCMISMATCH;
@@ -155,8 +172,16 @@
goto fail;
hptr = ebuf_data(eb, NULL);
PJDLOG_ASSERT(hptr != NULL);
- if (proto_recv(conn, hptr, hdr.size) == -1)
- goto fail;
+ for (size_t off = 0; off < hdr.size; off += ret) {
+ ret = proto_recv(conn, (char *)hptr + off,
+ hdr.size - off);
+ if (ret == 0) {
+ errno = EPROTO;
+ ret = -1;
+ }
+ if (ret < 0)
+ goto fail;
+ }
}
nv = nv_ntoh(eb);
if (nv == NULL)
@@ -178,7 +203,7 @@
bool freedata;
size_t dsize;
void *dptr;
- int ret;
+ ssize_t ret;
PJDLOG_ASSERT(data != NULL);
PJDLOG_ASSERT(size > 0);
@@ -194,8 +219,16 @@
} else if (dsize == 0) {
(void)nv_set_error(nv, 0);
} else {
- if (proto_recv(conn, data, dsize) == -1)
- goto end;
+ for (size_t off = 0; off < dsize; off += ret) {
+ ret = proto_recv(conn, (char *)data + off,
+ dsize - off);
+ if (ret == 0) {
+ errno = EPROTO;
+ ret = -1;
+ }
+ if (ret < 0)
+ goto end;
+ }
for (ii = sizeof(pipeline) / sizeof(pipeline[0]); ii > 0;
ii--) {
ret = pipeline[ii - 1].hps_recv(res, nv, &dptr,
@@ -209,7 +242,7 @@
goto end;
}
if (dptr != data)
- bcopy(dptr, data, dsize);
+ memcpy(data, dptr, dsize);
}
ret = 0;
diff --git a/sbin/hastd/hastd.c b/sbin/hastd/hastd.c
--- a/sbin/hastd/hastd.c
+++ b/sbin/hastd/hastd.c
@@ -985,13 +985,15 @@
connection_migrate(struct hast_resource *res)
{
struct proto_conn *conn;
+ ssize_t ret;
int16_t val = 0;
pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));
PJDLOG_ASSERT(res->hr_role == HAST_ROLE_PRIMARY);
- if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
+ ret = proto_recv(res->hr_conn, &val, sizeof(val));
+ if (ret < 0 || (size_t)ret != sizeof(val)) {
pjdlog_errno(LOG_WARNING,
"Unable to receive connection command");
return;
@@ -1013,7 +1015,8 @@
}
val = 0;
out:
- if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
+ ret = proto_send(res->hr_conn, &val, sizeof(val));
+ if (ret < 0 || (size_t)ret != sizeof(val)) {
pjdlog_errno(LOG_WARNING,
"Unable to send reply to connection request");
}
diff --git a/sbin/hastd/primary.c b/sbin/hastd/primary.c
--- a/sbin/hastd/primary.c
+++ b/sbin/hastd/primary.c
@@ -288,7 +288,7 @@
if (res->hr_ggateunit >= 0) {
struct g_gate_ctl_destroy ggiod;
- bzero(&ggiod, sizeof(ggiod));
+ memset(&ggiod, 0, sizeof(ggiod));
ggiod.gctl_version = G_GATE_VERSION;
ggiod.gctl_unit = res->hr_ggateunit;
ggiod.gctl_force = 1;
@@ -569,14 +569,17 @@
primary_connect(struct hast_resource *res, struct proto_conn **connp)
{
struct proto_conn *conn;
+ ssize_t ret;
int16_t val;
val = 1;
- if (proto_send(res->hr_conn, &val, sizeof(val)) == -1) {
+ ret = proto_send(res->hr_conn, &val, sizeof(val));
+ if (ret < 0 || (size_t)ret != sizeof(val)) {
primary_exit(EX_TEMPFAIL,
"Unable to send connection request to parent");
}
- if (proto_recv(res->hr_conn, &val, sizeof(val)) == -1) {
+ ret = proto_recv(res->hr_conn, &val, sizeof(val));
+ if (ret < 0 || (size_t)ret != sizeof(val)) {
primary_exit(EX_TEMPFAIL,
"Unable to receive reply to connection request from parent");
}
@@ -613,7 +616,7 @@
{
struct g_gate_ctl_modify ggiomodify;
- bzero(&ggiomodify, sizeof(ggiomodify));
+ memset(&ggiomodify, 0, sizeof(ggiomodify));
ggiomodify.gctl_version = G_GATE_VERSION;
ggiomodify.gctl_unit = res->hr_ggateunit;
ggiomodify.gctl_modify = GG_MODIFY_READPROV | GG_MODIFY_READOFFSET;
@@ -716,7 +719,7 @@
nv_free(nvin);
goto close;
}
- bcopy(token, res->hr_token, sizeof(res->hr_token));
+ memcpy(res->hr_token, token, sizeof(res->hr_token));
nv_free(nvin);
/*
@@ -848,9 +851,9 @@
nv_free(nvin);
#ifdef notyet
/* Setup directions. */
- if (proto_send(out, NULL, 0) == -1)
+ if (proto_send(out, NULL, 0) != 0)
pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
- if (proto_recv(in, NULL, 0) == -1)
+ if (proto_recv(in, NULL, 0) != 0)
pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
#endif
pjdlog_info("Connected to %s.", res->hr_remoteaddr);
@@ -918,7 +921,7 @@
* Create provider before trying to connect, as connection failure
* is not critical, but may take some time.
*/
- bzero(&ggiocreate, sizeof(ggiocreate));
+ memset(&ggiocreate, 0, sizeof(ggiocreate));
ggiocreate.gctl_version = G_GATE_VERSION;
ggiocreate.gctl_mediasize = res->hr_datasize;
ggiocreate.gctl_sectorsize = res->hr_local_sectorsize;
@@ -945,7 +948,7 @@
* provider died and didn't clean up. In that case we will start from
* where he left of.
*/
- bzero(&ggiocancel, sizeof(ggiocancel));
+ memset(&ggiocancel, 0, sizeof(ggiocancel));
ggiocancel.gctl_version = G_GATE_VERSION;
ggiocancel.gctl_unit = G_GATE_NAME_GIVEN;
snprintf(ggiocancel.gctl_name, sizeof(ggiocancel.gctl_name), "hast/%s",
diff --git a/sbin/hastd/proto.h b/sbin/hastd/proto.h
--- a/sbin/hastd/proto.h
+++ b/sbin/hastd/proto.h
@@ -42,8 +42,8 @@
int proto_connect_wait(struct proto_conn *conn, int timeout);
int proto_server(const char *addr, struct proto_conn **connp);
int proto_accept(struct proto_conn *conn, struct proto_conn **newconnp);
-int proto_send(const struct proto_conn *conn, const void *data, size_t size);
-int proto_recv(const struct proto_conn *conn, void *data, size_t size);
+ssize_t proto_send(const struct proto_conn *conn, const void *data, size_t size);
+ssize_t proto_recv(const struct proto_conn *conn, void *data, size_t size);
int proto_connection_send(const struct proto_conn *conn,
struct proto_conn *mconn);
int proto_connection_recv(const struct proto_conn *conn, bool client,
diff --git a/sbin/hastd/proto.c b/sbin/hastd/proto.c
--- a/sbin/hastd/proto.c
+++ b/sbin/hastd/proto.c
@@ -35,7 +35,6 @@
#include <errno.h>
#include <stdint.h>
#include <string.h>
-#include <strings.h>
#include "pjdlog.h"
#include "proto.h"
@@ -98,7 +97,7 @@
conn->pc_side == PROTO_SIDE_SERVER_WORK);
PJDLOG_ASSERT(conn->pc_proto != NULL);
- bzero(conn, sizeof(*conn));
+ memset(conn, 0, sizeof(*conn));
free(conn);
}
@@ -242,40 +241,26 @@
return (0);
}
-int
+ssize_t
proto_send(const struct proto_conn *conn, const void *data, size_t size)
{
- int ret;
-
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->prt_send != NULL);
- ret = conn->pc_proto->prt_send(conn->pc_ctx, data, size, -1);
- if (ret != 0) {
- errno = ret;
- return (-1);
- }
- return (0);
+ return (conn->pc_proto->prt_send(conn->pc_ctx, data, size, -1));
}
-int
+ssize_t
proto_recv(const struct proto_conn *conn, void *data, size_t size)
{
- int ret;
-
PJDLOG_ASSERT(conn != NULL);
PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
PJDLOG_ASSERT(conn->pc_proto != NULL);
PJDLOG_ASSERT(conn->pc_proto->prt_recv != NULL);
- ret = conn->pc_proto->prt_recv(conn->pc_ctx, data, size, NULL);
- if (ret != 0) {
- errno = ret;
- return (-1);
- }
- return (0);
+ return (conn->pc_proto->prt_recv(conn->pc_ctx, data, size, NULL));
}
int
@@ -313,6 +298,7 @@
char protoname[128];
struct proto *proto;
struct proto_conn *newconn;
+ size_t len;
int ret, fd;
PJDLOG_ASSERT(conn != NULL);
@@ -321,19 +307,19 @@
PJDLOG_ASSERT(conn->pc_proto->prt_recv != NULL);
PJDLOG_ASSERT(newconnp != NULL);
- bzero(protoname, sizeof(protoname));
+ memset(protoname, 0, sizeof(protoname));
ret = conn->pc_proto->prt_recv(conn->pc_ctx, (unsigned char *)protoname,
sizeof(protoname) - 1, &fd);
- if (ret != 0) {
- errno = ret;
+ if (ret < 0)
return (-1);
- }
PJDLOG_ASSERT(fd >= 0);
TAILQ_FOREACH(proto, &protos, prt_next) {
- if (strcmp(proto->prt_name, protoname) == 0)
+ len = strlen(proto->prt_name);
+ if ((size_t)ret == len &&
+ memcmp(proto->prt_name, protoname, len) == 0)
break;
}
if (proto == NULL) {
diff --git a/sbin/hastd/proto_common.c b/sbin/hastd/proto_common.c
--- a/sbin/hastd/proto_common.c
+++ b/sbin/hastd/proto_common.c
@@ -37,7 +37,7 @@
#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
-#include <strings.h>
+#include <string.h>
#include <unistd.h>
#include "pjdlog.h"
@@ -68,8 +68,8 @@
PJDLOG_ASSERT(sock >= 0);
PJDLOG_ASSERT(fd >= 0);
- bzero(&msg, sizeof(msg));
- bzero(&ctrl, sizeof(ctrl));
+ memset(&msg, 0, sizeof(msg));
+ memset(&ctrl, 0, sizeof(ctrl));
msg.msg_iov = NULL;
msg.msg_iovlen = 0;
@@ -80,18 +80,18 @@
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
- bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));
+ memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
if (sendmsg(sock, &msg, 0) == -1)
- return (errno);
+ return (-1);
return (0);
}
-int
+ssize_t
proto_common_send(int sock, const unsigned char *data, size_t size, int fd)
{
- ssize_t done;
+ ssize_t done, total = 0;
size_t sendsize;
int errcount = 0;
@@ -103,7 +103,7 @@
PJDLOG_ASSERT(size == 0);
if (shutdown(sock, SHUT_RD) == -1)
- return (errno);
+ return (-1);
return (0);
}
@@ -114,7 +114,8 @@
sendsize = size < MAX_SEND_SIZE ? size : MAX_SEND_SIZE;
done = send(sock, data, sendsize, MSG_NOSIGNAL);
if (done == 0) {
- return (ENOTCONN);
+ errno = ENOTCONN;
+ return (-1);
} else if (done == -1) {
if (errno == EINTR)
continue;
@@ -143,19 +144,20 @@
*/
if (errno == EAGAIN && blocking_socket(sock))
errno = ETIMEDOUT;
- return (errno);
+ return (-1);
}
data += done;
size -= done;
+ total += done;
} while (size > 0);
if (errcount > 0) {
pjdlog_info("Data sent successfully after %d ENOBUFS error%s.",
errcount, errcount == 1 ? "" : "s");
}
- if (fd == -1)
- return (0);
- return (proto_descriptor_send(sock, fd));
+ if (fd != -1 && proto_descriptor_send(sock, fd) != 0)
+ return (-1);
+ return (total);
}
static int
@@ -168,8 +170,8 @@
PJDLOG_ASSERT(sock >= 0);
PJDLOG_ASSERT(fdp != NULL);
- bzero(&msg, sizeof(msg));
- bzero(&ctrl, sizeof(ctrl));
+ memset(&msg, 0, sizeof(msg));
+ memset(&ctrl, 0, sizeof(ctrl));
msg.msg_iov = NULL;
msg.msg_iovlen = 0;
@@ -177,19 +179,20 @@
msg.msg_controllen = sizeof(ctrl);
if (recvmsg(sock, &msg, 0) == -1)
- return (errno);
+ return (-1);
cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET ||
cmsg->cmsg_type != SCM_RIGHTS) {
- return (EINVAL);
+ errno = EINVAL;
+ return (-1);
}
- bcopy(CMSG_DATA(cmsg), fdp, sizeof(*fdp));
+ memcpy(fdp, CMSG_DATA(cmsg), sizeof(*fdp));
return (0);
}
-int
+ssize_t
proto_common_recv(int sock, unsigned char *data, size_t size, int *fdp)
{
ssize_t done;
@@ -210,10 +213,11 @@
PJDLOG_ASSERT(size > 0);
do {
- done = recv(sock, data, size, MSG_WAITALL);
+ done = recv(sock, data, size, 0);
} while (done == -1 && errno == EINTR);
if (done == 0) {
- return (ENOTCONN);
+ errno = ENOTCONN;
+ return (-1);
} else if (done == -1) {
/*
* If this is blocking socket and we got EAGAIN, this
@@ -223,9 +227,9 @@
*/
if (errno == EAGAIN && blocking_socket(sock))
errno = ETIMEDOUT;
- return (errno);
+ return (-1);
}
- if (fdp == NULL)
- return (0);
- return (proto_descriptor_recv(sock, fdp));
+ if (fdp != NULL && proto_descriptor_recv(sock, fdp) != 0)
+ return (-1);
+ return (done);
}
diff --git a/sbin/hastd/proto_impl.h b/sbin/hastd/proto_impl.h
--- a/sbin/hastd/proto_impl.h
+++ b/sbin/hastd/proto_impl.h
@@ -44,8 +44,8 @@
typedef int prt_server_t(const char *, void **);
typedef int prt_accept_t(void *, void **);
typedef int prt_wrap_t(int, bool, void **);
-typedef int prt_send_t(void *, const unsigned char *, size_t, int);
-typedef int prt_recv_t(void *, unsigned char *, size_t, int *);
+typedef ssize_t prt_send_t(void *, const unsigned char *, size_t, int);
+typedef ssize_t prt_recv_t(void *, unsigned char *, size_t, int *);
typedef int prt_descriptor_t(const void *);
typedef bool prt_address_match_t(const void *, const char *);
typedef void prt_local_address_t(const void *, char *, size_t);
@@ -72,7 +72,7 @@
void proto_register(struct proto *proto, bool isdefault);
-int proto_common_send(int sock, const unsigned char *data, size_t size, int fd);
-int proto_common_recv(int sock, unsigned char *data, size_t size, int *fdp);
+ssize_t proto_common_send(int sock, const unsigned char *data, size_t size, int fd);
+ssize_t proto_common_recv(int sock, unsigned char *data, size_t size, int *fdp);
#endif /* !_PROTO_IMPL_H_ */
diff --git a/sbin/hastd/proto_socketpair.c b/sbin/hastd/proto_socketpair.c
--- a/sbin/hastd/proto_socketpair.c
+++ b/sbin/hastd/proto_socketpair.c
@@ -81,7 +81,7 @@
return (0);
}
-static int
+static ssize_t
sp_send(void *ctx, const unsigned char *data, size_t size, int fd)
{
struct sp_ctx *spctx = ctx;
@@ -120,7 +120,7 @@
return (proto_common_send(sock, data, size, fd));
}
-static int
+static ssize_t
sp_recv(void *ctx, unsigned char *data, size_t size, int *fdp)
{
struct sp_ctx *spctx = ctx;
diff --git a/sbin/hastd/proto_tcp.c b/sbin/hastd/proto_tcp.c
--- a/sbin/hastd/proto_tcp.c
+++ b/sbin/hastd/proto_tcp.c
@@ -109,7 +109,7 @@
if (addr == NULL)
return (-1);
- bzero(&hints, sizeof(hints));
+ memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
@@ -478,7 +478,7 @@
client ? TCP_SIDE_CLIENT : TCP_SIDE_SERVER_WORK, ctxp));
}
-static int
+static ssize_t
tcp_send(void *ctx, const unsigned char *data, size_t size, int fd)
{
struct tcp_ctx *tctx = ctx;
@@ -491,7 +491,7 @@
return (proto_common_send(tctx->tc_fd, data, size, -1));
}
-static int
+static ssize_t
tcp_recv(void *ctx, unsigned char *data, size_t size, int *fdp)
{
struct tcp_ctx *tctx = ctx;
diff --git a/sbin/hastd/proto_uds.c b/sbin/hastd/proto_uds.c
--- a/sbin/hastd/proto_uds.c
+++ b/sbin/hastd/proto_uds.c
@@ -226,7 +226,7 @@
return (0);
}
-static int
+static ssize_t
uds_send(void *ctx, const unsigned char *data, size_t size, int fd)
{
struct uds_ctx *uctx = ctx;
@@ -238,7 +238,7 @@
return (proto_common_send(uctx->uc_fd, data, size, fd));
}
-static int
+static ssize_t
uds_recv(void *ctx, unsigned char *data, size_t size, int *fdp)
{
struct uds_ctx *uctx = ctx;
diff --git a/sbin/hastd/secondary.c b/sbin/hastd/secondary.c
--- a/sbin/hastd/secondary.c
+++ b/sbin/hastd/secondary.c
@@ -223,7 +223,7 @@
#ifdef notyet
/* Setup direction. */
- if (proto_send(res->hr_remoteout, NULL, 0) == -1)
+ if (proto_send(res->hr_remoteout, NULL, 0) != 0)
pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
#endif
@@ -402,7 +402,7 @@
nv_free(nvout);
#ifdef notyet
/* Setup direction. */
- if (proto_recv(res->hr_remotein, NULL, 0) == -1)
+ if (proto_recv(res->hr_remotein, NULL, 0) != 0)
pjdlog_errno(LOG_WARNING, "Unable to set connection direction");
#endif
}

File Metadata

Mime Type
text/plain
Expires
Mon, Sep 7, 2:13 PM (7 h, 50 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
38470893
Default Alt Text
D59465.diff (15 KB)

Event Timeline