commit 3d97effaa4fe16c79f725bf260f06220e62e5c6a from: Omar Polo via: Thomas Adam date: Wed Jan 31 22:01:32 2024 UTC convert to use imsg_get_fd() While here also fix a fd leak in got-read-pack. We were dup'ing imsg.fd without closing imsg.fd later; instead just use imsg_get_fd() to extract the file descriptor. Tested by falsifian and Kyle Ackerman, thanks! 'go ahead' stsp@ commit - 83985e6a8ae699605976783dc7e854050f1bd79f commit + 3d97effaa4fe16c79f725bf260f06220e62e5c6a blob - 5b26d6d8645a84a229ccc5481c0b41026ec9b0e2 blob + b8cf3ed84c2bcdb68a3853783af0e070f30dd66a --- gotd/auth.c +++ gotd/auth.c @@ -187,6 +187,7 @@ recv_authreq(struct imsg *imsg, struct gotd_imsgev *ie char *username = NULL; size_t len; const size_t maxlen = MAX_IMSGSIZE - IMSG_HEADER_SIZE; + int fd = -1; log_debug("authentication request received"); @@ -196,10 +197,11 @@ recv_authreq(struct imsg *imsg, struct gotd_imsgev *ie memcpy(&iauth, imsg->data, datalen); - if (imsg->fd == -1) + fd = imsg_get_fd(imsg); + if (fd == -1) return got_error(GOT_ERR_PRIVSEP_NO_FD); - if (getpeereid(imsg->fd, &euid, &egid) == -1) + if (getpeereid(fd, &euid, &egid) == -1) return got_error_from_errno("getpeerid"); if (iauth.euid != euid) blob - 3007bcfd7e144bb4bed06fa4aca8ebb8f5606abf blob + 06930323b2023b42abe628c2dfe3bc4a12762e70 --- gotd/gotd.c +++ gotd/gotd.c @@ -682,7 +682,7 @@ recv_connect(uint32_t *client_id, struct imsg *imsg) return got_error(GOT_ERR_PRIVSEP_LEN); memcpy(&iconnect, imsg->data, sizeof(iconnect)); - s = imsg->fd; + s = imsg_get_fd(imsg); if (s == -1) { err = got_error(GOT_ERR_PRIVSEP_NO_FD); goto done; blob - d061355ea0a84bb5d92a825b752fa8c2914a9a57 blob + 5860623235693f75c4c5c3dae27ad355ee084be1 --- gotd/repo_read.c +++ gotd/repo_read.c @@ -267,11 +267,12 @@ list_refs(struct imsg *imsg) size_t datalen; struct gotd_imsg_reflist irefs; struct imsgbuf ibuf; - int client_fd = imsg->fd; + int client_fd; struct got_object_id *head_target_id = NULL; TAILQ_INIT(&refs); + client_fd = imsg_get_fd(imsg); if (client_fd == -1) return got_error(GOT_ERR_PRIVSEP_NO_FD); @@ -561,9 +562,6 @@ receive_delta_cache_fd(struct imsg *imsg, size_t datalen; log_debug("receiving delta cache file"); - - if (imsg->fd == -1) - return got_error(GOT_ERR_PRIVSEP_NO_FD); datalen = imsg->hdr.len - IMSG_HEADER_SIZE; if (datalen != sizeof(ireq)) @@ -573,7 +571,10 @@ receive_delta_cache_fd(struct imsg *imsg, if (client->delta_cache_fd != -1) return got_error(GOT_ERR_PRIVSEP_MSG); - client->delta_cache_fd = imsg->fd; + client->delta_cache_fd = imsg_get_fd(imsg); + if (client->delta_cache_fd == -1) + return got_error(GOT_ERR_PRIVSEP_NO_FD); + client->report_progress = ireq.report_progress; return NULL; } @@ -587,9 +588,6 @@ receive_pack_pipe(struct imsg *imsg, struct gotd_imsge log_debug("receiving pack pipe descriptor"); - if (imsg->fd == -1) - return got_error(GOT_ERR_PRIVSEP_NO_FD); - datalen = imsg->hdr.len - IMSG_HEADER_SIZE; if (datalen != sizeof(ireq)) return got_error(GOT_ERR_PRIVSEP_LEN); @@ -598,7 +596,10 @@ receive_pack_pipe(struct imsg *imsg, struct gotd_imsge if (client->pack_pipe != -1) return got_error(GOT_ERR_PRIVSEP_MSG); - client->pack_pipe = imsg->fd; + client->pack_pipe = imsg_get_fd(imsg); + if (client->pack_pipe == -1) + return got_error(GOT_ERR_PRIVSEP_NO_FD); + return NULL; } @@ -776,13 +777,13 @@ recv_connect(struct imsg *imsg) datalen = imsg->hdr.len - IMSG_HEADER_SIZE; if (datalen != 0) return got_error(GOT_ERR_PRIVSEP_LEN); - if (imsg->fd == -1) - return got_error(GOT_ERR_PRIVSEP_NO_FD); if (repo_read.session_fd != -1) return got_error(GOT_ERR_PRIVSEP_MSG); - repo_read.session_fd = imsg->fd; + repo_read.session_fd = imsg_get_fd(imsg); + if (repo_read.session_fd == -1) + return got_error(GOT_ERR_PRIVSEP_NO_FD); imsg_init(&iev->ibuf, repo_read.session_fd); iev->handler = repo_read_dispatch_session; blob - fae40607e756382fd72276604fff6f4a1040c452 blob + 75d16b4a52946182333abe8cc658752b4842b7d3 --- gotd/repo_write.c +++ gotd/repo_write.c @@ -241,10 +241,11 @@ list_refs(struct imsg *imsg) size_t datalen; struct gotd_imsg_reflist irefs; struct imsgbuf ibuf; - int client_fd = imsg->fd; + int client_fd; TAILQ_INIT(&refs); + client_fd = imsg_get_fd(imsg); if (client_fd == -1) return got_error(GOT_ERR_PRIVSEP_NO_FD); @@ -1224,13 +1225,13 @@ recv_packfile(int *have_packfile, struct imsg *imsg) return got_error(GOT_ERR_PRIVSEP_NO_FD); imsg_init(&ibuf, client->fd); - - if (imsg->fd == -1) - return got_error(GOT_ERR_PRIVSEP_NO_FD); pack = &client->pack; memset(pack, 0, sizeof(*pack)); - pack->fd = imsg->fd; + pack->fd = imsg_get_fd(imsg); + if (pack->fd == -1) + return got_error(GOT_ERR_PRIVSEP_NO_FD); + err = got_delta_cache_alloc(&pack->delta_cache); if (err) return err; @@ -1591,9 +1592,6 @@ receive_pack_pipe(struct imsg *imsg, struct gotd_imsge log_debug("receiving pack pipe descriptor"); - if (imsg->fd == -1) - return got_error(GOT_ERR_PRIVSEP_NO_FD); - datalen = imsg->hdr.len - IMSG_HEADER_SIZE; if (datalen != sizeof(ireq)) return got_error(GOT_ERR_PRIVSEP_LEN); @@ -1602,7 +1600,10 @@ receive_pack_pipe(struct imsg *imsg, struct gotd_imsge if (client->pack_pipe != -1) return got_error(GOT_ERR_PRIVSEP_MSG); - client->pack_pipe = imsg->fd; + client->pack_pipe = imsg_get_fd(imsg); + if (client->pack_pipe == -1) + return got_error(GOT_ERR_PRIVSEP_NO_FD); + return NULL; } @@ -1615,9 +1616,6 @@ receive_pack_idx(struct imsg *imsg, struct gotd_imsgev log_debug("receiving pack index output file"); - if (imsg->fd == -1) - return got_error(GOT_ERR_PRIVSEP_NO_FD); - datalen = imsg->hdr.len - IMSG_HEADER_SIZE; if (datalen != sizeof(ireq)) return got_error(GOT_ERR_PRIVSEP_LEN); @@ -1626,7 +1624,10 @@ receive_pack_idx(struct imsg *imsg, struct gotd_imsgev if (client->packidx_fd != -1) return got_error(GOT_ERR_PRIVSEP_MSG); - client->packidx_fd = imsg->fd; + client->packidx_fd = imsg_get_fd(imsg); + if (client->packidx_fd == -1) + return got_error(GOT_ERR_PRIVSEP_NO_FD); + return NULL; } @@ -1754,13 +1755,13 @@ recv_connect(struct imsg *imsg) datalen = imsg->hdr.len - IMSG_HEADER_SIZE; if (datalen != 0) return got_error(GOT_ERR_PRIVSEP_LEN); - if (imsg->fd == -1) - return got_error(GOT_ERR_PRIVSEP_NO_FD); if (repo_write.session_fd != -1) return got_error(GOT_ERR_PRIVSEP_MSG); - repo_write.session_fd = imsg->fd; + repo_write.session_fd = imsg_get_fd(imsg); + if (repo_write.session_fd == -1) + return got_error(GOT_ERR_PRIVSEP_NO_FD); imsg_init(&iev->ibuf, repo_write.session_fd); iev->handler = repo_write_dispatch_session; blob - b9059883b276557469eb6e0eee0207d19e630185 blob + b4c8a49f654b65665982670d9b56b7314a10efd5 --- gotd/session.c +++ gotd/session.c @@ -1303,13 +1303,12 @@ recv_connect(struct imsg *imsg) if (datalen != sizeof(iconnect)) return got_error(GOT_ERR_PRIVSEP_LEN); memcpy(&iconnect, imsg->data, sizeof(iconnect)); - - if (imsg->fd == -1) - return got_error(GOT_ERR_PRIVSEP_NO_FD); - client->fd = imsg->fd; client->euid = iconnect.euid; client->egid = iconnect.egid; + client->fd = imsg_get_fd(imsg); + if (client->fd == -1) + return got_error(GOT_ERR_PRIVSEP_NO_FD); imsg_init(&client->iev.ibuf, client->fd); client->iev.handler = session_dispatch_client; @@ -1329,6 +1328,7 @@ 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 (client->state != GOTD_STATE_EXPECT_LIST_REFS) return got_error(GOT_ERR_PRIVSEP_MSG); @@ -1352,10 +1352,11 @@ recv_repo_child(struct imsg *imsg) return got_error_msg(GOT_ERR_PRIVSEP_MSG, "bad child process type"); - if (imsg->fd == -1) + fd = imsg_get_fd(imsg); + if (fd == -1) return got_error(GOT_ERR_PRIVSEP_NO_FD); - imsg_init(&client->repo_child_iev.ibuf, imsg->fd); + imsg_init(&client->repo_child_iev.ibuf, fd); client->repo_child_iev.handler = session_dispatch_repo_child; client->repo_child_iev.events = EV_READ; client->repo_child_iev.handler_arg = NULL; blob - 9b76c142f7896c6574ee51aef180f428c3e8480c blob + eb61f54564d20341ee600522a273544e76a332d5 --- gotwebd/config.c +++ gotwebd/config.c @@ -132,13 +132,11 @@ config_getsock(struct gotwebd *env, struct imsg *imsg) /* create a new socket */ if ((sock = calloc(1, sizeof(*sock))) == NULL) { - if (imsg->fd != -1) - close(imsg->fd); return 1; } memcpy(&sock->conf, &sock_conf, sizeof(sock->conf)); - sock->fd = imsg->fd; + sock->fd = imsg_get_fd(imsg); TAILQ_INSERT_TAIL(&env->sockets, sock, entry); @@ -195,7 +193,7 @@ config_getfd(struct gotwebd *env, struct imsg *imsg) { struct socket *sock; uint8_t *p = imsg->data; - int sock_id, match = 0, i; + int sock_id, match = 0, i, j; if (IMSG_DATA_SIZE(imsg) != sizeof(sock_id)) fatalx("%s: wrong size", __func__); @@ -206,16 +204,18 @@ config_getfd(struct gotwebd *env, struct imsg *imsg) const int nfds = (GOTWEB_PACK_NUM_TEMPFILES + PRIV_FDS__MAX); for (i = 0; i < nfds; i++) { if (i < PRIV_FDS__MAX && sock->priv_fd[i] == -1) { + sock->priv_fd[i] = imsg_get_fd(imsg); log_debug("%s: assigning socket %d priv_fd %d", - __func__, sock_id, imsg->fd); - sock->priv_fd[i] = imsg->fd; + __func__, sock_id, sock->priv_fd[i]); match = 1; break; } - if (sock->pack_fds[i - PRIV_FDS__MAX] == -1) { + + j = i - PRIV_FDS__MAX; + if (sock->pack_fds[j] == -1) { + sock->pack_fds[j] = imsg_get_fd(imsg); log_debug("%s: assigning socket %d pack_fd %d", - __func__, sock_id, imsg->fd); - sock->pack_fds[i - PRIV_FDS__MAX] = imsg->fd; + __func__, sock_id, sock->pack_fds[j]); match = 1; break; } blob - ee38a8cc66e5006ea0943dbbdd1e278ab7549fd3 blob + efc694b36944f601e4a9ce7d58e37865850e5689 --- lib/serve.c +++ lib/serve.c @@ -666,6 +666,7 @@ recv_done(int *packfd, int outfd, struct imsgbuf *ibuf { const struct got_error *err; struct imsg imsg; + int fd; *packfd = -1; @@ -686,8 +687,9 @@ recv_done(int *packfd, int outfd, struct imsgbuf *ibuf err = gotd_imsg_recv_error(NULL, &imsg); break; case GOTD_IMSG_PACKFILE_PIPE: - if (imsg.fd != -1) - *packfd = imsg.fd; + fd = imsg_get_fd(&imsg); + if (fd != -1) + *packfd = fd; else err = got_error(GOT_ERR_PRIVSEP_NO_FD); break; @@ -1072,10 +1074,10 @@ recv_packfile(struct imsg *imsg, int infd) if (datalen != 0) return got_error(GOT_ERR_PRIVSEP_MSG); - if (imsg->fd == -1) + packfd = imsg_get_fd(imsg); + if (packfd == -1) return got_error(GOT_ERR_PRIVSEP_NO_FD); - packfd = imsg->fd; while (!pack_done) { ssize_t r = 0; blob - 33229996cb6bb8c8db24c52a6631b833be22b3a5 blob + c85fb402c902f5fda8a01743ae051712444ced6e --- libexec/got-fetch-pack/got-fetch-pack.c +++ libexec/got-fetch-pack/got-fetch-pack.c @@ -904,7 +904,7 @@ main(int argc, char **argv) goto done; } memcpy(&fetch_req, imsg.data, sizeof(fetch_req)); - fetchfd = imsg.fd; + fetchfd = imsg_get_fd(&imsg); if (datalen != sizeof(fetch_req) + fetch_req.worktree_branch_len + fetch_req.remote_head_len) { @@ -1081,7 +1081,7 @@ main(int argc, char **argv) err = got_error(GOT_ERR_PRIVSEP_LEN); goto done; } - packfd = imsg.fd; + packfd = imsg_get_fd(&imsg); err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs, fetch_req.fetch_all_branches, &wanted_branches, blob - 85c7a6b22b87d00ea63811681448ccb592e87830 blob + 87bf91f479ea673c698af41e4f037937415b1567 --- libexec/got-index-pack/got-index-pack.c +++ libexec/got-index-pack/got-index-pack.c @@ -140,7 +140,7 @@ main(int argc, char **argv) goto done; } memcpy(pack_hash, imsg.data, sizeof(pack_hash)); - pack.fd = imsg.fd; + pack.fd = imsg_get_fd(&imsg); err = got_privsep_recv_imsg(&imsg, &ibuf, 0); if (err) @@ -155,7 +155,7 @@ main(int argc, char **argv) err = got_error(GOT_ERR_PRIVSEP_LEN); goto done; } - idxfd = imsg.fd; + idxfd = imsg_get_fd(&imsg); for (i = 0; i < nitems(tmpfiles); i++) { err = got_privsep_recv_imsg(&imsg, &ibuf, 0); @@ -171,7 +171,7 @@ main(int argc, char **argv) err = got_error(GOT_ERR_PRIVSEP_LEN); goto done; } - tmpfd = imsg.fd; + tmpfd = imsg_get_fd(&imsg); tmpfiles[i] = fdopen(tmpfd, "w+"); if (tmpfiles[i] == NULL) { err = got_error_from_errno("fdopen"); blob - 1b8716f656c76d4d70c14bbcc33c627b7d30b3ce blob + e9efb90c2107143711b3b4283142f00087f5eba9 --- libexec/got-read-blob/got-read-blob.c +++ libexec/got-read-blob/got-read-blob.c @@ -83,6 +83,7 @@ main(int argc, char *argv[]) for (;;) { struct imsg imsg, imsg_outfd; FILE *f = NULL; + int fd = -1, outfd = -1; size_t size; struct got_object *obj = NULL; uint8_t *buf = NULL; @@ -96,9 +97,7 @@ main(int argc, char *argv[]) csum.output_ctx = &ctx; memset(&imsg, 0, sizeof(imsg)); - imsg.fd = -1; memset(&imsg_outfd, 0, sizeof(imsg_outfd)); - imsg_outfd.fd = -1; if (sigint_received) { err = got_error(GOT_ERR_CANCELLED); @@ -127,7 +126,8 @@ main(int argc, char *argv[]) } memcpy(&expected_id, imsg.data, sizeof(expected_id)); - if (imsg.fd == -1) { + fd = imsg_get_fd(&imsg); + if (fd == -1) { err = got_error(GOT_ERR_PRIVSEP_NO_FD); goto done; } @@ -152,21 +152,22 @@ main(int argc, char *argv[]) err = got_error(GOT_ERR_PRIVSEP_LEN); goto done; } - if (imsg_outfd.fd == -1) { + outfd = imsg_get_fd(&imsg_outfd); + if (outfd == -1) { err = got_error(GOT_ERR_PRIVSEP_NO_FD); goto done; } - err = got_object_read_header(&obj, imsg.fd); + err = got_object_read_header(&obj, fd); if (err) goto done; - if (lseek(imsg.fd, SEEK_SET, 0) == -1) { + if (lseek(fd, SEEK_SET, 0) == -1) { err = got_error_from_errno("lseek"); goto done; } - f = fdopen(imsg.fd, "rb"); + f = fdopen(fd, "rb"); if (f == NULL) { err = got_error_from_errno("fdopen"); goto done; @@ -178,7 +179,7 @@ main(int argc, char *argv[]) if (err) goto done; } else { - err = got_inflate_to_fd(&size, f, &csum, imsg_outfd.fd); + err = got_inflate_to_fd(&size, f, &csum, outfd); if (err) goto done; } @@ -199,12 +200,12 @@ done: if (f) { if (fclose(f) == EOF && err == NULL) err = got_error_from_errno("fclose"); - } else if (imsg.fd != -1) { - if (close(imsg.fd) == -1 && err == NULL) + } else if (fd != -1) { + if (close(fd) == -1 && err == NULL) err = got_error_from_errno("close"); } - if (imsg_outfd.fd != -1) { - if (close(imsg_outfd.fd) == -1 && err == NULL) + if (outfd != -1) { + if (close(outfd) == -1 && err == NULL) err = got_error_from_errno("close"); } blob - cdd1a966e34a11d85d27e9e65b280de9e8c735b7 blob + 3626bf63eb05a8178334d20bc4d1e4649d1b9c17 --- libexec/got-read-commit/got-read-commit.c +++ libexec/got-read-commit/got-read-commit.c @@ -85,6 +85,7 @@ main(int argc, char *argv[]) struct imsg imsg; struct got_commit_object *commit = NULL; struct got_object_id expected_id; + int fd = -1; if (sigint_received) { err = got_error(GOT_ERR_CANCELLED); @@ -113,19 +114,20 @@ main(int argc, char *argv[]) } memcpy(&expected_id, imsg.data, sizeof(expected_id)); - if (imsg.fd == -1) { + fd = imsg_get_fd(&imsg); + if (fd == -1) { err = got_error(GOT_ERR_PRIVSEP_NO_FD); goto done; } - err = got_object_read_commit(&commit, imsg.fd, &expected_id, 0); + err = got_object_read_commit(&commit, fd, &expected_id, 0); if (err) goto done; err = got_privsep_send_commit(&ibuf, commit); got_object_commit_close(commit); done: - if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL) + if (fd != -1 && close(fd) == -1 && err == NULL) err = got_error_from_errno("close"); imsg_free(&imsg); if (err) blob - bb635b215634101143c5a58e5accde6ed94389f5 blob + 6c8b57b3facca5b9dcc832edc6f66a039c44c654 --- libexec/got-read-gitconfig/got-read-gitconfig.c +++ libexec/got-read-gitconfig/got-read-gitconfig.c @@ -372,9 +372,9 @@ main(int argc, char *argv[]) for (;;) { struct imsg imsg; + int fd = -1; memset(&imsg, 0, sizeof(imsg)); - imsg.fd = -1; if (sigint_received) { err = got_error(GOT_ERR_CANCELLED); @@ -398,14 +398,15 @@ main(int argc, char *argv[]) err = got_error(GOT_ERR_PRIVSEP_LEN); break; } - if (imsg.fd == -1){ + fd = imsg_get_fd(&imsg); + if (fd == -1) { err = got_error(GOT_ERR_PRIVSEP_NO_FD); break; } if (gitconfig) got_gitconfig_close(gitconfig); - err = got_gitconfig_open(&gitconfig, imsg.fd); + err = got_gitconfig_open(&gitconfig, fd); break; case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST: err = gitconfig_num_request(&ibuf, gitconfig, "core", @@ -433,8 +434,8 @@ main(int argc, char *argv[]) break; } - if (imsg.fd != -1) { - if (close(imsg.fd) == -1 && err == NULL) + if (fd != -1) { + if (close(fd) == -1 && err == NULL) err = got_error_from_errno("close"); } blob - 3059b04c7096a2c0e70448da106d41c4d9ea6530 blob + 56d436f414c7ef9f23bd78db1d42297ca021c18a --- libexec/got-read-gotconfig/got-read-gotconfig.c +++ libexec/got-read-gotconfig/got-read-gotconfig.c @@ -514,9 +514,9 @@ main(int argc, char *argv[]) for (;;) { struct imsg imsg; + int fd = -1; memset(&imsg, 0, sizeof(imsg)); - imsg.fd = -1; if (sigint_received) { err = got_error(GOT_ERR_CANCELLED); @@ -540,14 +540,15 @@ main(int argc, char *argv[]) err = got_error(GOT_ERR_PRIVSEP_LEN); break; } - if (imsg.fd == -1){ + fd = imsg_get_fd(&imsg); + if (fd == -1){ err = got_error(GOT_ERR_PRIVSEP_NO_FD); break; } if (gotconfig) gotconfig_free(gotconfig); - err = gotconfig_parse(&gotconfig, filename, &imsg.fd); + err = gotconfig_parse(&gotconfig, filename, &fd); if (err) break; err = validate_config(gotconfig); @@ -599,8 +600,8 @@ main(int argc, char *argv[]) break; } - if (imsg.fd != -1) { - if (close(imsg.fd) == -1 && err == NULL) + if (fd != -1) { + if (close(fd) == -1 && err == NULL) err = got_error_from_errno("close"); } blob - f32cb7bfb6961e17dda8f36484ee88cedf52059f blob + 617c9f4bf78b4fb4843546569061e3c6fc6c7c0b --- libexec/got-read-object/got-read-object.c +++ libexec/got-read-object/got-read-object.c @@ -121,6 +121,8 @@ main(int argc, char *argv[]) #endif for (;;) { + int fd = -1, outfd = -1; + if (sigint_received) { err = got_error(GOT_ERR_CANCELLED); break; @@ -149,12 +151,13 @@ main(int argc, char *argv[]) } memcpy(&expected_id, imsg.data, sizeof(expected_id)); - if (imsg.fd == -1) { + fd = imsg_get_fd(&imsg); + if (fd == -1) { err = got_error(GOT_ERR_PRIVSEP_NO_FD); goto done; } - err = got_object_read_header(&obj, imsg.fd); + err = got_object_read_header(&obj, fd); if (err) goto done; @@ -185,15 +188,16 @@ main(int argc, char *argv[]) imsg_free(&imsg_outfd); goto done; } - if (imsg_outfd.fd == -1) { + outfd = imsg_get_fd(&imsg_outfd); + if (outfd == -1) { err = got_error(GOT_ERR_PRIVSEP_NO_FD); imsg_free(&imsg_outfd); goto done; } err = send_raw_obj(&ibuf, obj, &expected_id, - imsg.fd, imsg_outfd.fd); - imsg.fd = -1; /* imsg.fd is owned by send_raw_obj() */ - if (close(imsg_outfd.fd) == -1 && err == NULL) + fd, outfd); + fd = -1; /* fd is owned by send_raw_obj() */ + if (close(outfd) == -1 && err == NULL) err = got_error_from_errno("close"); imsg_free(&imsg_outfd); if (err) @@ -201,7 +205,7 @@ main(int argc, char *argv[]) } else err = got_privsep_send_obj(&ibuf, obj); done: - if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL) + if (fd != -1 && close(fd) == -1 && err == NULL) err = got_error_from_errno("close"); imsg_free(&imsg); if (obj) blob - 896cda68aacfa5e72656423d26a4cd8b962d04f1 blob + 00bb589bc2faa0903e3a4664d5d35bf948fa0aec --- libexec/got-read-pack/got-read-pack.c +++ libexec/got-read-pack/got-read-pack.c @@ -266,6 +266,7 @@ receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t const struct got_error *err; struct imsg imsg; size_t datalen; + int fd; err = got_privsep_recv_imsg(&imsg, ibuf, 0); if (err) @@ -281,15 +282,16 @@ receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t err = got_error(GOT_ERR_PRIVSEP_LEN); goto done; } - if (imsg.fd == -1) { + fd = imsg_get_fd(&imsg); + if (fd == -1) { err = got_error(GOT_ERR_PRIVSEP_NO_FD); goto done; } - *f = fdopen(imsg.fd, "w+"); + *f = fdopen(fd, "w+"); if (*f == NULL) { err = got_error_from_errno("fdopen"); - close(imsg.fd); + close(fd); goto done; } done: @@ -301,19 +303,24 @@ static const struct got_error * receive_tempfile(FILE **f, const char *mode, struct imsg *imsg, struct imsgbuf *ibuf) { + const struct got_error *err; size_t datalen; + int fd; datalen = imsg->hdr.len - IMSG_HEADER_SIZE; if (datalen != 0) return got_error(GOT_ERR_PRIVSEP_LEN); - if (imsg->fd == -1) + fd = imsg_get_fd(imsg); + if (fd == -1) return got_error(GOT_ERR_PRIVSEP_NO_FD); - *f = fdopen(imsg->fd, mode); - if (*f == NULL) - return got_error_from_errno("fdopen"); - imsg->fd = -1; + *f = fdopen(fd, mode); + if (*f == NULL) { + err = got_error_from_errno("fdopen"); + close(fd); + return err; + } return NULL; } @@ -904,8 +911,6 @@ raw_delta_request(struct imsg *imsg, struct imsgbuf *i memcpy(&req, imsg->data, sizeof(req)); memcpy(&id, &req.id, sizeof(id)); - imsg->fd = -1; - err = got_packfile_extract_raw_delta(&delta_buf, &delta_size, &delta_compressed_size, &delta_offset, &delta_data_offset, &base_offset, &base_id, &base_size, &result_size, @@ -1133,11 +1138,6 @@ receive_packidx(struct got_packidx **packidx, struct i goto done; } - if (imsg.fd == -1) { - err = got_error(GOT_ERR_PRIVSEP_NO_FD); - goto done; - } - datalen = imsg.hdr.len - IMSG_HEADER_SIZE; if (datalen != sizeof(ipackidx)) { err = got_error(GOT_ERR_PRIVSEP_LEN); @@ -1145,10 +1145,10 @@ receive_packidx(struct got_packidx **packidx, struct i } memcpy(&ipackidx, imsg.data, sizeof(ipackidx)); + p->fd = imsg_get_fd(&imsg); p->len = ipackidx.len; - p->fd = dup(imsg.fd); if (p->fd == -1) { - err = got_error_from_errno("dup"); + err = got_error(GOT_ERR_PRIVSEP_NO_FD); goto done; } if (lseek(p->fd, 0, SEEK_SET) == -1) { @@ -1166,9 +1166,8 @@ receive_packidx(struct got_packidx **packidx, struct i err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size); done: if (err) { - if (imsg.fd != -1) - close(imsg.fd); - got_packidx_close(p); + if (p != NULL) + got_packidx_close(p); } else *packidx = p; imsg_free(&imsg); @@ -1873,11 +1872,6 @@ receive_pack(struct got_pack **packp, struct imsgbuf * goto done; } - if (imsg.fd == -1) { - err = got_error(GOT_ERR_PRIVSEP_NO_FD); - goto done; - } - datalen = imsg.hdr.len - IMSG_HEADER_SIZE; if (datalen != sizeof(ipack)) { err = got_error(GOT_ERR_PRIVSEP_LEN); @@ -1886,9 +1880,9 @@ receive_pack(struct got_pack **packp, struct imsgbuf * memcpy(&ipack, imsg.data, sizeof(ipack)); pack->filesize = ipack.filesize; - pack->fd = dup(imsg.fd); + pack->fd = imsg_get_fd(&imsg); if (pack->fd == -1) { - err = got_error_from_errno("dup"); + err = got_error(GOT_ERR_PRIVSEP_NO_FD); goto done; } if (lseek(pack->fd, 0, SEEK_SET) == -1) { @@ -1915,9 +1909,8 @@ receive_pack(struct got_pack **packp, struct imsgbuf * #endif done: if (err) { - if (imsg.fd != -1) - close(imsg.fd); - free(pack); + if (pack != NULL) + got_pack_close(pack); } else *packp = pack; imsg_free(&imsg); @@ -1986,8 +1979,6 @@ main(int argc, char *argv[]) } for (;;) { - imsg.fd = -1; - if (sigint_received) { err = got_error(GOT_ERR_CANCELLED); break; @@ -2000,8 +1991,10 @@ main(int argc, char *argv[]) break; } - if (imsg.hdr.type == GOT_IMSG_STOP) + if (imsg.hdr.type == GOT_IMSG_STOP) { + imsg_free(&imsg); break; + } switch (imsg.hdr.type) { case GOT_IMSG_TMPFD: @@ -2093,8 +2086,6 @@ main(int argc, char *argv[]) break; } - if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL) - err = got_error_from_errno("close"); imsg_free(&imsg); if (err) break; blob - dec83c1a875916235977245236b2aa9d6bcf2b03 blob + e05cc1e029ded2a60dc787775eeb1c4e9f05d7e8 --- libexec/got-read-tag/got-read-tag.c +++ libexec/got-read-tag/got-read-tag.c @@ -84,6 +84,7 @@ main(int argc, char *argv[]) struct imsg imsg; struct got_tag_object *tag = NULL; struct got_object_id expected_id; + int fd = -1; if (sigint_received) { err = got_error(GOT_ERR_CANCELLED); @@ -112,19 +113,20 @@ main(int argc, char *argv[]) } memcpy(&expected_id, imsg.data, sizeof(expected_id)); - if (imsg.fd == -1) { + fd = imsg_get_fd(&imsg); + if (fd == -1) { err = got_error(GOT_ERR_PRIVSEP_NO_FD); goto done; } /* Always assume file offset zero. */ - err = got_object_read_tag(&tag, imsg.fd, &expected_id, 0); + err = got_object_read_tag(&tag, fd, &expected_id, 0); if (err) goto done; err = got_privsep_send_tag(&ibuf, tag); done: - if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL) + if (fd != -1 && close(fd) == -1 && err == NULL) err = got_error_from_errno("close"); imsg_free(&imsg); if (err) blob - a578eadbbddbc594dac437cf55427bbc85eadb71 blob + 50b57c79841c9add0976612b0ff40ef71cb31419 --- libexec/got-read-tree/got-read-tree.c +++ libexec/got-read-tree/got-read-tree.c @@ -87,6 +87,7 @@ main(int argc, char *argv[]) struct imsg imsg; uint8_t *buf = NULL; struct got_object_id expected_id; + int fd = -1; if (sigint_received) { err = got_error(GOT_ERR_CANCELLED); @@ -115,21 +116,22 @@ main(int argc, char *argv[]) } memcpy(&expected_id, imsg.data, sizeof(expected_id)); - if (imsg.fd == -1) { + fd = imsg_get_fd(&imsg); + if (fd == -1) { err = got_error(GOT_ERR_PRIVSEP_NO_FD); goto done; } /* Always assume file offset zero. */ err = got_object_read_tree(&entries, &nentries, &nentries_alloc, - &buf, imsg.fd, &expected_id); + &buf, fd, &expected_id); if (err) goto done; err = got_privsep_send_tree(&ibuf, entries, nentries); done: free(buf); - if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL) + if (fd != -1 && close(fd) == -1 && err == NULL) err = got_error_from_errno("close"); imsg_free(&imsg); if (err) blob - 48bdedda02f983189b3ee1273078927953fa18e8 blob + 76552a0cd46c0a952c443ddca495e8d107d49fde --- libexec/got-send-pack/got-send-pack.c +++ libexec/got-send-pack/got-send-pack.c @@ -135,7 +135,7 @@ recv_packfd(int *packfd, struct imsgbuf *ibuf) goto done; } - *packfd = imsg.fd; + *packfd = imsg_get_fd(&imsg); done: imsg_free(&imsg); return err; @@ -659,7 +659,7 @@ main(int argc, char **argv) goto done; } memcpy(&send_req, imsg.data, sizeof(send_req)); - sendfd = imsg.fd; + sendfd = imsg_get_fd(&imsg); imsg_free(&imsg); if (send_req.verbosity > 0)