"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Omar Polo <op@omarpolo.com>
Subject:
gotd: start to use the newer imsg APIs
To:
gameoftrees@openbsd.org
Date:
Tue, 16 Apr 2024 14:07:34 +0200

Download raw body.

Thread
  • Omar Polo:

    gotd: start to use the newer imsg APIs

There are a few newer imsg APIs that makes the handling more clear and
straightforward.

Instead of trying to do one big diff to convert everything, let's do
some it in some smaller steps.

This changes the places in gotd where we're reading a struct off a imsg
to use imsg_get_data().  Underneath, it does the same length check and
memcpy(), but we can save a few lines of code with it.

I'm also starting to sprinkle around the usage of the other getters,
with the goal to treat the imsg struct as it were opaque.

Will follow-up with the changes for the other part of the components and
with diff for the other more complicate cases, where we have more than a
struct inside a imsg.

ok?

diffstat /home/op/w/got
 M  gotd/auth.c           |   3+   7-
 M  gotd/gotd.c           |  20+  26-
 M  gotd/imsg.c           |   1+   4-
 M  gotd/listen.c         |   3+   6-
 M  gotd/notify.c         |   6+  12-
 M  gotd/repo_read.c      |  10+  25-
 M  gotd/repo_write.c     |   5+  16-
 M  gotd/session_read.c   |  11+  29-
 M  gotd/session_write.c  |  17+  37-

9 files changed, 76 insertions(+), 162 deletions(-)

diff /home/op/w/got
commit - 53c2bfa8e1d5c3e128dcfcfd90c1a55099168eba
path + /home/op/w/got
blob - 46bcfeef428d4ba1125f8851a58966b8bf62c96d
file + gotd/auth.c
--- gotd/auth.c
+++ gotd/auth.c
@@ -181,7 +181,6 @@ recv_authreq(struct imsg *imsg, struct gotd_imsgev *ie
 	const struct got_error *err;
 	struct imsgbuf *ibuf = &iev->ibuf;
 	struct gotd_imsg_auth iauth;
-	size_t datalen;
 	uid_t euid;
 	gid_t egid;
 	char *username = NULL;
@@ -191,12 +190,9 @@ recv_authreq(struct imsg *imsg, struct gotd_imsgev *ie
 
 	log_debug("authentication request received");
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(iauth))
+	if (imsg_get_data(imsg, &iauth, sizeof(iauth)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
-	memcpy(&iauth, imsg->data, datalen);
-
 	fd = imsg_get_fd(imsg);
 	if (fd == -1)
 		return got_error(GOT_ERR_PRIVSEP_NO_FD);
@@ -261,14 +257,14 @@ auth_dispatch(int fd, short event, void *arg)
 		if (n == 0)	/* No more messages. */
 			break;
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_AUTHENTICATE:
 			err = recv_authreq(&imsg, iev);
 			if (err)
 				log_warnx("%s", err->msg);
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 
blob - 56aa6c3d8a0cc75fe5a0804df81b9f06486921a1
file + gotd/gotd.c
--- gotd/gotd.c
+++ gotd/gotd.c
@@ -519,7 +519,6 @@ start_client_authentication(struct gotd_client *client
 	const struct got_error *err;
 	struct gotd_imsg_list_refs ireq;
 	struct gotd_repo *repo = NULL;
-	size_t datalen;
 
 	log_debug("list-refs request from uid %d", client->euid);
 
@@ -527,12 +526,9 @@ start_client_authentication(struct gotd_client *client
 		return got_error_msg(GOT_ERR_BAD_REQUEST,
 		    "unexpected list-refs request received");
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(ireq))
+	if (imsg_get_data(imsg, &ireq, sizeof(ireq)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
-	memcpy(&ireq, imsg->data, datalen);
-
 	if (ireq.client_is_reading) {
 		err = ensure_client_is_not_writing(client);
 		if (err)
@@ -622,7 +618,7 @@ gotd_request(int fd, short events, void *arg)
 
 		evtimer_del(&client->tmo);
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_INFO:
 			err = send_info(client);
 			break;
@@ -633,7 +629,7 @@ gotd_request(int fd, short events, void *arg)
 			err = start_client_authentication(client, &imsg);
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			err = got_error(GOT_ERR_PRIVSEP_MSG);
 			break;
 		}
@@ -663,16 +659,13 @@ recv_connect(uint32_t *client_id, struct imsg *imsg)
 {
 	const struct got_error *err = NULL;
 	struct gotd_imsg_connect iconnect;
-	size_t datalen;
 	int s = -1;
 	struct gotd_client *client = NULL;
 
 	*client_id = 0;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(iconnect))
+	if (imsg_get_data(imsg, &iconnect, sizeof(iconnect)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	memcpy(&iconnect, imsg->data, sizeof(iconnect));
 
 	s = imsg_get_fd(imsg);
 	if (s == -1) {
@@ -930,7 +923,7 @@ verify_imsg_src(struct gotd_client *client, struct got
 		}
 	}
 
-	switch (imsg->hdr.type) {
+	switch (imsg_get_type(imsg)) {
 	case GOTD_IMSG_ERROR:
 		ret = 1;
 		break;
@@ -987,7 +980,8 @@ verify_imsg_src(struct gotd_client *client, struct got
 			ret = 1;
 		break;
 	default:
-		log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
+		log_debug("%s: unexpected imsg %d", __func__,
+		    imsg_get_type(imsg));
 		break;
 	}
 
@@ -1079,7 +1073,7 @@ gotd_dispatch_listener(int fd, short event, void *arg)
 		if (n == 0)	/* No more messages. */
 			break;
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_ERROR:
 			do_disconnect = 1;
 			err = gotd_imsg_recv_error(&client_id, &imsg);
@@ -1088,7 +1082,7 @@ gotd_dispatch_listener(int fd, short event, void *arg)
 			err = recv_connect(&client_id, &imsg);
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 
@@ -1161,9 +1155,9 @@ gotd_dispatch_notifier(int fd, short event, void *arg)
 		if (n == 0)	/* No more messages. */
 			break;
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 
@@ -1246,7 +1240,7 @@ gotd_dispatch_auth_child(int fd, short event, void *ar
 
 	datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
 
-	switch (imsg.hdr.type) {
+	switch (imsg_get_type(&imsg)) {
 	case GOTD_IMSG_ERROR:
 		do_disconnect = 1;
 		err = gotd_imsg_recv_error(&client_id, &imsg);
@@ -1259,14 +1253,14 @@ gotd_dispatch_auth_child(int fd, short event, void *ar
 		break;
 	default:
 		do_disconnect = 1;
-		log_debug("unexpected imsg %d", imsg.hdr.type);
+		log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 		break;
 	}
 
 	if (!verify_imsg_src(client, client->auth, &imsg)) {
 		do_disconnect = 1;
 		log_debug("dropping imsg type %d from PID %d",
-		    imsg.hdr.type, client->auth->pid);
+		    imsg_get_type(&imsg), client->auth->pid);
 	}
 
 	if (do_disconnect) {
@@ -1422,7 +1416,7 @@ gotd_dispatch_client_session(int fd, short event, void
 		if (n == 0)	/* No more messages. */
 			break;
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_ERROR:
 			do_disconnect = 1;
 			err = gotd_imsg_recv_error(&client_id, &imsg);
@@ -1438,13 +1432,13 @@ gotd_dispatch_client_session(int fd, short event, void
 			do_disconnect = 1;
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 
 		if (!verify_imsg_src(client, proc, &imsg)) {
 			log_debug("dropping imsg type %d from PID %d",
-			    imsg.hdr.type, proc->pid);
+			    imsg_get_type(&imsg), proc->pid);
 			imsg_free(&imsg);
 			continue;
 		}
@@ -1583,7 +1577,7 @@ gotd_dispatch_repo_child(int fd, short event, void *ar
 		if (n == 0)	/* No more messages. */
 			break;
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_ERROR:
 			do_disconnect = 1;
 			err = gotd_imsg_recv_error(&client_id, &imsg);
@@ -1598,13 +1592,13 @@ gotd_dispatch_repo_child(int fd, short event, void *ar
 			err = connect_repo_child(client, proc);
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 
 		if (!verify_imsg_src(client, proc, &imsg)) {
 			log_debug("dropping imsg type %d from PID %d",
-			    imsg.hdr.type, proc->pid);
+			    imsg_get_type(&imsg), proc->pid);
 			imsg_free(&imsg);
 			continue;
 		}
blob - 86b16615f22802ae9f3a4f249265458eb1392651
file + gotd/imsg.c
--- gotd/imsg.c
+++ gotd/imsg.c
@@ -40,12 +40,9 @@ const struct got_error *
 gotd_imsg_recv_error(uint32_t *client_id, struct imsg *imsg)
 {
 	struct gotd_imsg_error ierr;
-	size_t datalen;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(ierr))
+	if (imsg_get_data(imsg, &ierr, sizeof(ierr)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	memcpy(&ierr, imsg->data, sizeof(ierr));
 
 	if (client_id)
 		*client_id = ierr.client_id;
blob - 23dd87f8191b27375b82a3f72a796826d19990e2
file + gotd/listen.c
--- gotd/listen.c
+++ gotd/listen.c
@@ -365,13 +365,10 @@ static const struct got_error *
 recv_disconnect(struct imsg *imsg)
 {
 	struct gotd_imsg_disconnect idisconnect;
-	size_t datalen;
 	struct gotd_listen_client *client = NULL;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(idisconnect))
+	if (imsg_get_data(imsg, &idisconnect, sizeof(idisconnect)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
 
 	log_debug("client disconnecting");
 
@@ -413,14 +410,14 @@ listen_dispatch(int fd, short event, void *arg)
 		if (n == 0)	/* No more messages. */
 			break;
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_DISCONNECT:
 			err = recv_disconnect(&imsg);
 			if (err)
 				log_warnx("disconnect: %s", err->msg);
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 
blob - abc10999b714c055c44b01f7d380e97962a222f8
file + gotd/notify.c
--- gotd/notify.c
+++ gotd/notify.c
@@ -289,17 +289,13 @@ send_notification(struct imsg *imsg, struct gotd_imsge
 {
 	const struct got_error *err = NULL;
 	struct gotd_imsg_notify inotify;
-	size_t datalen;
 	struct gotd_repo *repo;
 	struct gotd_notification_target *target;
 	int fd;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(inotify))
+	if (imsg_get_data(imsg, &inotify, sizeof(inotify)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
-	memcpy(&inotify, imsg->data, datalen);
-
 	repo = gotd_find_repo_by_name(inotify.repo_name, gotd_notify.repos);
 	if (repo == NULL)
 		return got_error(GOT_ERR_PRIVSEP_MSG);
@@ -372,12 +368,12 @@ notify_dispatch_session(int fd, short event, void *arg
 		if (n == 0)	/* No more messages. */
 			break;
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_NOTIFY:
 			err = send_notification(&imsg, iev);
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 		imsg_free(&imsg);
@@ -405,11 +401,9 @@ static const struct got_error *
 recv_session(struct imsg *imsg)
 {
 	struct gotd_notify_session *session;
-	size_t datalen;
 	int fd;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != 0)
+	if (imsg_get_len(imsg) != 0)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
 	fd = imsg_get_fd(imsg);
@@ -471,12 +465,12 @@ notify_dispatch(int fd, short event, void *arg)
 		if (n == 0)	/* No more messages. */
 			break;
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_CONNECT_SESSION:
 			err = recv_session(&imsg);
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 		imsg_free(&imsg);
blob - 7e04767d68c4c1e7eff6a3520785b24aac07efcf
file + gotd/repo_read.c
--- gotd/repo_read.c
+++ gotd/repo_read.c
@@ -261,7 +261,6 @@ list_refs(struct imsg *imsg)
 	struct repo_read_client *client = &repo_read_client;
 	struct got_reflist_head refs;
 	struct got_reflist_entry *re;
-	size_t datalen;
 	struct gotd_imsg_reflist irefs;
 	struct imsgbuf ibuf;
 	int client_fd;
@@ -273,8 +272,7 @@ list_refs(struct imsg *imsg)
 	if (client_fd == -1)
 		return got_error(GOT_ERR_PRIVSEP_NO_FD);
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != 0)
+	if (imsg_get_len(imsg) != 0)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
 	if (repo_read.refs_listed) {
@@ -403,16 +401,13 @@ recv_want(struct imsg *imsg)
 	const struct got_error *err;
 	struct repo_read_client *client = &repo_read_client;
 	struct gotd_imsg_want iwant;
-	size_t datalen;
 	char hex[SHA1_DIGEST_STRING_LENGTH];
 	struct got_object_id id;
 	int obj_type;
 	struct imsgbuf ibuf;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(iwant))
+	if (imsg_get_data(imsg, &iwant, sizeof(iwant)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	memcpy(&iwant, imsg->data, sizeof(iwant));
 
 	memset(&id, 0, sizeof(id));
 	memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
@@ -448,16 +443,13 @@ recv_have(struct imsg *imsg)
 	const struct got_error *err;
 	struct repo_read_client *client = &repo_read_client;
 	struct gotd_imsg_have ihave;
-	size_t datalen;
 	char hex[SHA1_DIGEST_STRING_LENGTH];
 	struct got_object_id id;
 	int obj_type;
 	struct imsgbuf ibuf;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(ihave))
+	if (imsg_get_data(imsg, &ihave, sizeof(ihave)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	memcpy(&ihave, imsg->data, sizeof(ihave));
 
 	memset(&id, 0, sizeof(id));
 	memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
@@ -554,14 +546,11 @@ receive_delta_cache_fd(struct imsg *imsg,
 {
 	struct repo_read_client *client = &repo_read_client;
 	struct gotd_imsg_send_packfile ireq;
-	size_t datalen;
 
 	log_debug("receiving delta cache file");
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(ireq))
+	if (imsg_get_data(imsg, &ireq, sizeof(ireq)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	memcpy(&ireq, imsg->data, sizeof(ireq));
 
 	if (client->delta_cache_fd != -1)
 		return got_error(GOT_ERR_PRIVSEP_MSG);
@@ -578,12 +567,10 @@ static const struct got_error *
 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
 {
 	struct repo_read_client *client = &repo_read_client;
-	size_t datalen;
 
 	log_debug("receiving pack pipe descriptor");
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != 0)
+	if (imsg_get_len(imsg) != 0)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
 	if (client->pack_pipe != -1)
@@ -701,13 +688,13 @@ repo_read_dispatch_session(int fd, short event, void *
 		if (n == 0)	/* No more messages. */
 			break;
 
-		if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
+		if (imsg_get_type(&imsg) != GOTD_IMSG_LIST_REFS_INTERNAL &&
 		    !repo_read.refs_listed) {
 			err = got_error(GOT_ERR_PRIVSEP_MSG);
 			break;
 		}
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_LIST_REFS_INTERNAL:
 			err = list_refs(&imsg);
 			if (err)
@@ -740,7 +727,7 @@ repo_read_dispatch_session(int fd, short event, void *
 				log_warnx("sending packfile: %s", err->msg);
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 
@@ -766,10 +753,8 @@ static const struct got_error *
 recv_connect(struct imsg *imsg)
 {
 	struct gotd_imsgev *iev = &repo_read.session_iev;
-	size_t datalen;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != 0)
+	if (imsg_get_len(imsg) != 0)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
 	if (repo_read.session_fd != -1)
@@ -827,7 +812,7 @@ repo_read_dispatch(int fd, short event, void *arg)
 			err = recv_connect(&imsg);
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 
blob - 787504ad4f6f0817c648d3f24b5d75d65da66010
file + gotd/repo_write.c
--- gotd/repo_write.c
+++ gotd/repo_write.c
@@ -249,7 +249,6 @@ list_refs(struct imsg *imsg)
 	struct repo_write_client *client = &repo_write_client;
 	struct got_reflist_head refs;
 	struct got_reflist_entry *re;
-	size_t datalen;
 	struct gotd_imsg_reflist irefs;
 	struct imsgbuf ibuf;
 	int client_fd;
@@ -260,8 +259,7 @@ list_refs(struct imsg *imsg)
 	if (client_fd == -1)
 		return got_error(GOT_ERR_PRIVSEP_NO_FD);
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != 0)
+	if (imsg_get_len(imsg) != 0)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
 	if (repo_write.refs_listed) {
@@ -1212,7 +1210,6 @@ recv_packfile(int *have_packfile, struct imsg *imsg)
 		int idx;
 	} repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
 	int i;
-	size_t datalen;
 	struct imsgbuf ibuf;
 	struct got_ratelimit rl;
 	struct got_pack *pack = NULL;
@@ -1224,10 +1221,8 @@ recv_packfile(int *have_packfile, struct imsg *imsg)
 	*have_packfile = 0;
 	got_ratelimit_init(&rl, 2, 0);
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(ireq))
+	if (imsg_get_data(imsg, &ireq, sizeof(ireq)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	memcpy(&ireq, imsg->data, sizeof(ireq));
 
 	if (client->pack_pipe == -1 || client->packidx_fd == -1)
 		return got_error(GOT_ERR_PRIVSEP_NO_FD);
@@ -1589,12 +1584,10 @@ static const struct got_error *
 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
 {
 	struct repo_write_client *client = &repo_write_client;
-	size_t datalen;
 
 	log_debug("receiving pack pipe descriptor");
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != 0)
+	if (imsg_get_len(imsg) != 0)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
 	if (client->pack_pipe != -1)
@@ -1611,12 +1604,10 @@ static const struct got_error *
 receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
 {
 	struct repo_write_client *client = &repo_write_client;
-	size_t datalen;
 
 	log_debug("receiving pack index output file");
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != 0)
+	if (imsg_get_len(imsg) != 0)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
 	if (client->packidx_fd != -1)
@@ -2342,10 +2333,8 @@ static const struct got_error *
 recv_connect(struct imsg *imsg)
 {
 	struct gotd_imsgev *iev = &repo_write.session_iev;
-	size_t datalen;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != 0)
+	if (imsg_get_len(imsg) != 0)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
 	if (repo_write.session_fd != -1)
blob - f009715ddfce49aeb0846413c598b3ee30297df7
file + gotd/session_read.c
--- gotd/session_read.c
+++ gotd/session_read.c
@@ -180,12 +180,9 @@ session_read_sighdlr(int sig, short event, void *arg)
 static const struct got_error *
 recv_packfile_done(struct imsg *imsg)
 {
-	size_t datalen;
-
 	log_debug("packfile-done received");
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != 0)
+	if (imsg_get_len(imsg) != 0)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
 	return NULL;
@@ -232,7 +229,7 @@ session_dispatch_repo_child(int fd, short event, void 
 		if (n == 0)	/* No more messages. */
 			break;
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_ERROR:
 			do_disconnect = 1;
 			err = gotd_imsg_recv_error(&client_id, &imsg);
@@ -242,7 +239,7 @@ session_dispatch_repo_child(int fd, short event, void 
 			err = recv_packfile_done(&imsg);
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 
@@ -271,12 +268,9 @@ static const struct got_error *
 recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
 {
 	struct gotd_imsg_capabilities icapas;
-	size_t datalen;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(icapas))
+	if (imsg_get_data(imsg, &icapas, sizeof(icapas)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	memcpy(&icapas, imsg->data, sizeof(icapas));
 
 	client->ncapa_alloc = icapas.ncapabilities;
 	client->capabilities = calloc(client->ncapa_alloc,
@@ -344,14 +338,10 @@ forward_want(struct gotd_session_client *client, struc
 {
 	struct gotd_imsg_want ireq;
 	struct gotd_imsg_want iwant;
-	size_t datalen;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(ireq))
+	if (imsg_get_data(imsg, &ireq, sizeof(ireq)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
-	memcpy(&ireq, imsg->data, datalen);
-
 	memset(&iwant, 0, sizeof(iwant));
 	memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
 
@@ -368,14 +358,10 @@ forward_have(struct gotd_session_client *client, struc
 {
 	struct gotd_imsg_have ireq;
 	struct gotd_imsg_have ihave;
-	size_t datalen;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(ireq))
+	if (imsg_get_data(imsg, &ireq, sizeof(ireq)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
-	memcpy(&ireq, imsg->data, datalen);
-
 	memset(&ihave, 0, sizeof(ihave));
 	memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
 
@@ -516,7 +502,7 @@ session_dispatch_client(int fd, short events, void *ar
 
 		evtimer_del(&client->tmo);
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_CAPABILITIES:
 			if (gotd_session.state !=
 			    GOTD_STATE_EXPECT_CAPABILITIES) {
@@ -617,7 +603,7 @@ session_dispatch_client(int fd, short events, void *ar
 			err = send_packfile(client);
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			err = got_error(GOT_ERR_PRIVSEP_MSG);
 			break;
 		}
@@ -707,7 +693,6 @@ recv_repo_child(struct imsg *imsg)
 {
 	struct gotd_imsg_connect_repo_child ichild;
 	struct gotd_session_client *client = &gotd_session_client;
-	size_t datalen;
 	int fd;
 
 	if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
@@ -717,12 +702,9 @@ recv_repo_child(struct imsg *imsg)
 	if (client->fd == -1)
 		return got_error(GOT_ERR_PRIVSEP_MSG);
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(ichild))
+	if (imsg_get_data(imsg, &ichild, sizeof(ichild)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
-	memcpy(&ichild, imsg->data, sizeof(ichild));
-
 	if (ichild.proc_id != PROC_REPO_READ)
 		return got_error_msg(GOT_ERR_PRIVSEP_MSG,
 		    "bad child process type");
@@ -788,7 +770,7 @@ session_dispatch(int fd, short event, void *arg)
 		if (n == 0)	/* No more messages. */
 			break;
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_ERROR:
 			do_disconnect = 1;
 			err = gotd_imsg_recv_error(&client_id, &imsg);
@@ -806,7 +788,7 @@ session_dispatch(int fd, short event, void *arg)
 			do_list_refs = 1;
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 		imsg_free(&imsg);
blob - 1d073e97037a9cb64129ca446a58b0f3fe2a2a62 (staged)
file + gotd/session_write.c
--- gotd/session_write.c
+++ gotd/session_write.c
@@ -191,14 +191,11 @@ static const struct got_error *
 recv_packfile_install(struct imsg *imsg)
 {
 	struct gotd_imsg_packfile_install inst;
-	size_t datalen;
 
 	log_debug("packfile-install received");
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(inst))
+	if (imsg_get_data(imsg, &inst, sizeof(inst)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	memcpy(&inst, imsg->data, sizeof(inst));
 
 	return NULL;
 }
@@ -207,14 +204,11 @@ static const struct got_error *
 recv_ref_updates_start(struct imsg *imsg)
 {
 	struct gotd_imsg_ref_updates_start istart;
-	size_t datalen;
 
 	log_debug("ref-updates-start received");
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(istart))
+	if (imsg_get_data(imsg, &istart, sizeof(istart)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	memcpy(&istart, imsg->data, sizeof(istart));
 
 	return NULL;
 }
@@ -317,13 +311,10 @@ install_pack(struct gotd_session_client *client, const
 	const struct got_error *err = NULL;
 	struct gotd_imsg_packfile_install inst;
 	char hex[SHA1_DIGEST_STRING_LENGTH];
-	size_t datalen;
 	char *packfile_path = NULL, *packidx_path = NULL;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(inst))
+	if (imsg_get_data(imsg, &inst, sizeof(inst)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	memcpy(&inst, imsg->data, sizeof(inst));
 
 	if (client->packfile_path == NULL)
 		return got_error_msg(GOT_ERR_BAD_REQUEST,
@@ -379,15 +370,12 @@ static const struct got_error *
 begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
 {
 	struct gotd_imsg_ref_updates_start istart;
-	size_t datalen;
 
 	if (client->nref_updates != -1)
 		return got_error(GOT_ERR_PRIVSEP_MSG);
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(istart))
+	if (imsg_get_data(imsg, &istart, sizeof(istart)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	memcpy(&istart, imsg->data, sizeof(istart));
 
 	if (istart.nref_updates <= 0)
 		return got_error(GOT_ERR_PRIVSEP_MSG);
@@ -871,7 +859,7 @@ session_dispatch_repo_child(int fd, short event, void 
 		if (n == 0)	/* No more messages. */
 			break;
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_ERROR:
 			do_disconnect = 1;
 			err = gotd_imsg_recv_error(&client_id, &imsg);
@@ -897,7 +885,7 @@ session_dispatch_repo_child(int fd, short event, void 
 				do_notify = 1;
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 
@@ -949,12 +937,9 @@ static const struct got_error *
 recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
 {
 	struct gotd_imsg_capabilities icapas;
-	size_t datalen;
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(icapas))
+	if (imsg_get_data(imsg, &icapas, sizeof(icapas)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
-	memcpy(&icapas, imsg->data, sizeof(icapas));
 
 	client->ncapa_alloc = icapas.ncapabilities;
 	client->capabilities = calloc(client->ncapa_alloc,
@@ -1237,7 +1222,7 @@ session_dispatch_client(int fd, short events, void *ar
 
 		evtimer_del(&client->tmo);
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_CAPABILITIES:
 			if (gotd_session.state !=
 			    GOTD_STATE_EXPECT_CAPABILITIES) {
@@ -1315,7 +1300,7 @@ session_dispatch_client(int fd, short events, void *ar
 			}
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			err = got_error(GOT_ERR_PRIVSEP_MSG);
 			break;
 		}
@@ -1440,10 +1425,11 @@ session_dispatch_notifier(int fd, short event, void *a
 		if (n == 0)	/* No more messages. */
 			break;
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_NOTIFICATION_SENT:
 			if (gotd_session.state != GOTD_STATE_NOTIFY) {
-				log_warn("unexpected imsg %d", imsg.hdr.type);
+				log_warn("unexpected imsg %d",
+				    imsg_get_type(&imsg));
 				break;
 			}
 			notif = STAILQ_FIRST(&notifications);
@@ -1460,7 +1446,7 @@ session_dispatch_notifier(int fd, short event, void *a
 			}
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 
@@ -1482,7 +1468,6 @@ recv_notifier(struct imsg *imsg)
 {
 	struct gotd_imsgev *iev = &gotd_session.notifier_iev;
 	struct gotd_session_client *client = &gotd_session_client;
-	size_t datalen;
 	int fd;
 
 	if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
@@ -1492,8 +1477,7 @@ recv_notifier(struct imsg *imsg)
 	if (client->fd == -1)
 		return got_error(GOT_ERR_PRIVSEP_MSG);
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != 0)
+	if (imsg_get_len(imsg) != 0)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
 	fd = imsg_get_fd(imsg);
@@ -1516,7 +1500,6 @@ recv_repo_child(struct imsg *imsg)
 {
 	struct gotd_imsg_connect_repo_child ichild;
 	struct gotd_session_client *client = &gotd_session_client;
-	size_t datalen;
 	int fd;
 
 	if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
@@ -1526,12 +1509,9 @@ recv_repo_child(struct imsg *imsg)
 	if (client->fd == -1)
 		return got_error(GOT_ERR_PRIVSEP_MSG);
 
-	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
-	if (datalen != sizeof(ichild))
+	if (imsg_get_data(imsg, &ichild, sizeof(ichild)) == -1)
 		return got_error(GOT_ERR_PRIVSEP_LEN);
 
-	memcpy(&ichild, imsg->data, sizeof(ichild));
-
 	if (ichild.proc_id != PROC_REPO_WRITE)
 		return got_error_msg(GOT_ERR_PRIVSEP_MSG,
 		    "bad child process type");
@@ -1597,7 +1577,7 @@ session_dispatch(int fd, short event, void *arg)
 		if (n == 0)	/* No more messages. */
 			break;
 
-		switch (imsg.hdr.type) {
+		switch (imsg_get_type(&imsg)) {
 		case GOTD_IMSG_ERROR:
 			do_disconnect = 1;
 			err = gotd_imsg_recv_error(&client_id, &imsg);
@@ -1618,7 +1598,7 @@ session_dispatch(int fd, short event, void *arg)
 			do_list_refs = 1;
 			break;
 		default:
-			log_debug("unexpected imsg %d", imsg.hdr.type);
+			log_debug("unexpected imsg %d", imsg_get_type(&imsg));
 			break;
 		}
 		imsg_free(&imsg);