check for errors from close(2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 3961b6d..fe7760b 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -47,7 +47,7 @@ const struct got_error *got_worktree_init(const char *, struct got_reference *,
const struct got_error *got_worktree_open(struct got_worktree **, const char *);
/* Dispose of an open work tree. */
-void got_worktree_close(struct got_worktree *);
+const struct got_error *got_worktree_close(struct got_worktree *);
/*
* Get the path to the root directory of a worktree.
diff --git a/lib/buf.c b/lib/buf.c
index 2d48ff9..6fa9823 100644
--- a/lib/buf.c
+++ b/lib/buf.c
@@ -309,7 +309,8 @@ buf_write(BUF *b, const char *path, mode_t mode)
if (fchmod(fd, mode) < 0)
err = got_error_from_errno();
- (void)close(fd);
+ if (close(fd) != 0 && err == NULL)
+ err = got_error_from_errno();
return err;
}
@@ -335,7 +336,9 @@ buf_write_stmp(BUF *b, char *template, struct wklhead *temp_files)
(void)unlink(template);
}
- (void)close(fd);
+ if (close(fd) != 0 && err == NULL)
+ err = got_error_from_errno();
+
return err;
}
diff --git a/lib/object.c b/lib/object.c
index 3e9aaf4..64f1a99 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -181,7 +181,11 @@ get_packfile_path(char **path_packfile, struct got_packidx *packidx)
static void
exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
{
- close(imsg_fds[0]);
+ if (close(imsg_fds[0]) != 0) {
+ fprintf(stderr, "%s: %s\n", getprogname(),
+ strerror(errno));
+ _exit(1);
+ }
if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
fprintf(stderr, "%s: %s\n", getprogname(),
@@ -260,7 +264,8 @@ start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
/* not reached */
}
- close(imsg_fds[1]);
+ if (close(imsg_fds[1]) != 0)
+ return got_error_from_errno();
pack->privsep_child->imsg_fd = imsg_fds[0];
pack->privsep_child->pid = pid;
imsg_init(ibuf, imsg_fds[0]);
@@ -379,7 +384,8 @@ read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
/* not reached */
}
- close(imsg_fds[1]);
+ if (close(imsg_fds[1]) != 0)
+ return got_error_from_errno();
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
imsg_fds[0];
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
@@ -434,8 +440,8 @@ got_object_open(struct got_object **obj, struct got_repository *repo,
err = got_repo_cache_object(repo, id, *obj);
done:
free(path);
- if (fd != -1)
- close(fd);
+ if (fd != -1 && close(fd) != 0 && errno != EBADF && err == NULL)
+ err = got_error_from_errno();
return err;
}
@@ -545,7 +551,8 @@ read_commit_privsep(struct got_commit_object **commit, int obj_fd,
/* not reached */
}
- close(imsg_fds[1]);
+ if (close(imsg_fds[1]) != 0)
+ return got_error_from_errno();
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
imsg_fds[0];
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
@@ -598,7 +605,8 @@ open_commit(struct got_commit_object **commit,
if (err)
return err;
err = read_commit_privsep(commit, fd, repo);
- close(fd);
+ if (close(fd) != 0 && errno != EBADF && err == NULL)
+ err = got_error_from_errno();
}
if (err == NULL) {
@@ -723,8 +731,8 @@ read_tree_privsep(struct got_tree_object **tree, int obj_fd,
/* not reached */
}
- close(imsg_fds[1]);
-
+ if (close(imsg_fds[1]) != 0)
+ return got_error_from_errno();
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
imsg_fds[0];
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
@@ -777,7 +785,8 @@ open_tree(struct got_tree_object **tree, struct got_repository *repo,
if (err)
return err;
err = read_tree_privsep(tree, fd, repo);
- close(fd);
+ if (close(fd) != 0 && errno != EBADF && err == NULL)
+ err = got_error_from_errno();
}
if (err == NULL) {
@@ -949,7 +958,8 @@ read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
/* not reached */
}
- close(imsg_fds[1]);
+ if (close(imsg_fds[1]) != 0)
+ return got_error_from_errno();
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
imsg_fds[0];
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
@@ -1011,7 +1021,8 @@ open_blob(struct got_blob_object **blob, struct got_repository *repo,
goto done;
err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
repo);
- close(infd);
+ if (close(infd) != 0 && errno != EBADF && err == NULL)
+ err = got_error_from_errno();
}
if (err)
goto done;
@@ -1022,7 +1033,8 @@ open_blob(struct got_blob_object **blob, struct got_repository *repo,
}
if (outbuf) {
- close(outfd);
+ if (close(outfd) != 0 && err == NULL)
+ err = got_error_from_errno();
outfd = -1;
(*blob)->f = fmemopen(outbuf, size, "rb");
if ((*blob)->f == NULL) {
@@ -1245,7 +1257,8 @@ read_tag_privsep(struct got_tag_object **tag, int obj_fd,
/* not reached */
}
- close(imsg_fds[1]);
+ if (close(imsg_fds[1]) != 0)
+ return got_error_from_errno();
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
imsg_fds[0];
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
@@ -1297,7 +1310,8 @@ open_tag(struct got_tag_object **tag, struct got_repository *repo,
if (err)
return err;
err = read_tag_privsep(tag, fd, repo);
- close(fd);
+ if (close(fd) != 0 && errno != EBADF && err == NULL)
+ err = got_error_from_errno();
}
if (err == NULL) {
diff --git a/lib/pack.c b/lib/pack.c
index bc1c9a7..5c4ddf2 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -427,7 +427,8 @@ got_packidx_close(struct got_packidx *packidx)
free(packidx->hdr.large_offsets);
free(packidx->hdr.trailer);
}
- close(packidx->fd);
+ if (close(packidx->fd) != 0 && err == NULL)
+ err = got_error_from_errno();
free(packidx);
return err;
@@ -503,7 +504,8 @@ got_pack_close(struct got_pack *pack)
err = got_pack_stop_privsep_child(pack);
if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
err = got_error_from_errno();
- close(pack->fd);
+ if (pack->fd != -1 && close(pack->fd) != 0 && err == NULL)
+ err = got_error_from_errno();
pack->fd = -1;
free(pack->path_packfile);
pack->path_packfile = NULL;
diff --git a/lib/repository.c b/lib/repository.c
index 6112deb..18ad2bb 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -418,7 +418,9 @@ got_repo_close(struct got_repository *repo)
repo->privsep_children[i].pid);
if (child_err && err == NULL)
err = child_err;
- close(repo->privsep_children[i].imsg_fd);
+ if (close(repo->privsep_children[i].imsg_fd) != 0 &&
+ err == NULL)
+ err = got_error_from_errno();
}
free(repo);
diff --git a/lib/worktree.c b/lib/worktree.c
index 628c668..37c8744 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -412,9 +412,10 @@ got_worktree_open(struct got_worktree **worktree, const char *path)
return got_error(GOT_ERR_NOT_WORKTREE);
}
-void
+const struct got_error *
got_worktree_close(struct got_worktree *worktree)
{
+ const struct got_error *err = NULL;
free(worktree->root_path);
free(worktree->repo_path);
free(worktree->path_prefix);
@@ -422,8 +423,10 @@ got_worktree_close(struct got_worktree *worktree)
if (worktree->head_ref)
got_ref_close(worktree->head_ref);
if (worktree->lockfd != -1)
- close(worktree->lockfd);
+ if (close(worktree->lockfd) != 0)
+ err = got_error_from_errno();
free(worktree);
+ return err;
}
const char *
@@ -674,12 +677,12 @@ merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
err = got_fileindex_entry_update(ie, ondisk_path,
blob1->id.sha1, worktree->base_commit_id->sha1, 0);
done:
- if (merged_fd != -1)
- close(merged_fd);
- if (f1)
- fclose(f1);
- if (f2)
- fclose(f2);
+ if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
+ err = got_error_from_errno();
+ if (f1 && fclose(f1) != 0 && err == NULL)
+ err = got_error_from_errno();
+ if (f2 && fclose(f2) != 0 && err == NULL)
+ err = got_error_from_errno();
if (blob2)
got_object_blob_close(blob2);
free(merged_path);
@@ -800,8 +803,8 @@ install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
err = got_fileindex_entry_add(fileindex, entry);
}
done:
- if (fd != -1)
- close(fd);
+ if (fd != -1 && close(fd) != 0 && err == NULL)
+ err = got_error_from_errno();
free(tmppath);
return err;
}
diff --git a/libexec/got-read-blob/got-read-blob.c b/libexec/got-read-blob/got-read-blob.c
index ac1918d..ce22859 100644
--- a/libexec/got-read-blob/got-read-blob.c
+++ b/libexec/got-read-blob/got-read-blob.c
@@ -164,10 +164,15 @@ done:
if (f) {
if (fclose(f) != 0 && err == NULL)
err = got_error_from_errno();
- } else if (imsg.fd != -1)
- close(imsg.fd);
- if (imsg_outfd.fd != -1)
- close(imsg_outfd.fd);
+ } else if (imsg.fd != -1) {
+ if (close(imsg.fd) != 0 && err == NULL)
+ err = got_error_from_errno();
+ }
+ if (imsg_outfd.fd != -1) {
+ if (close(imsg_outfd.fd) != 0 && err == NULL)
+ err = got_error_from_errno();
+ }
+
imsg_free(&imsg);
imsg_free(&imsg_outfd);
if (obj)
@@ -183,6 +188,7 @@ done:
got_privsep_send_error(&ibuf, err);
}
}
- close(GOT_IMSG_FD_CHILD);
+ if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+ err = got_error_from_errno();
return err ? 1 : 0;
}
diff --git a/libexec/got-read-commit/got-read-commit.c b/libexec/got-read-commit/got-read-commit.c
index 98154b8..e39345f 100644
--- a/libexec/got-read-commit/got-read-commit.c
+++ b/libexec/got-read-commit/got-read-commit.c
@@ -147,8 +147,10 @@ done:
if (f) {
if (fclose(f) != 0 && err == NULL)
err = got_error_from_errno();
- } else if (imsg.fd != -1)
- close(imsg.fd);
+ } else if (imsg.fd != -1) {
+ if (close(imsg.fd) != 0 && err == NULL)
+ err = got_error_from_errno();
+ }
imsg_free(&imsg);
if (err)
break;
@@ -161,6 +163,7 @@ done:
got_privsep_send_error(&ibuf, err);
}
}
- close(GOT_IMSG_FD_CHILD);
+ if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+ err = got_error_from_errno();
return err ? 1 : 0;
}
diff --git a/libexec/got-read-object/got-read-object.c b/libexec/got-read-object/got-read-object.c
index e633934..6692587 100644
--- a/libexec/got-read-object/got-read-object.c
+++ b/libexec/got-read-object/got-read-object.c
@@ -111,7 +111,8 @@ main(int argc, char *argv[])
err = got_privsep_send_obj(&ibuf, obj);
done:
- close(imsg.fd);
+ if (close(imsg.fd) != 0 && err == NULL)
+ err = got_error_from_errno();
imsg_free(&imsg);
if (obj)
got_object_close(obj);
@@ -126,6 +127,7 @@ done:
got_privsep_send_error(&ibuf, err);
}
}
- close(GOT_IMSG_FD_CHILD);
+ if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+ err = got_error_from_errno();
return err ? 1 : 0;
}
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index 095e031..f88d42c 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -203,8 +203,8 @@ receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
*f = fdopen(imsg.fd, "w+");
if (*f == NULL) {
- close(imsg.fd);
err = got_error_from_errno();
+ close(imsg.fd);
goto done;
}
done:
@@ -551,8 +551,8 @@ main(int argc, char *argv[])
break;
}
- if (imsg.fd != -1)
- close(imsg.fd);
+ if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
+ err = got_error_from_errno();
imsg_free(&imsg);
if (err)
break;
@@ -570,6 +570,7 @@ main(int argc, char *argv[])
got_privsep_send_error(&ibuf, err);
}
}
- close(GOT_IMSG_FD_CHILD);
+ if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+ err = got_error_from_errno();
return err ? 1 : 0;
}
diff --git a/libexec/got-read-tag/got-read-tag.c b/libexec/got-read-tag/got-read-tag.c
index a9cd2de..62f4869 100644
--- a/libexec/got-read-tag/got-read-tag.c
+++ b/libexec/got-read-tag/got-read-tag.c
@@ -142,8 +142,10 @@ done:
if (f) {
if (fclose(f) != 0 && err == NULL)
err = got_error_from_errno();
- } else if (imsg.fd != -1)
- close(imsg.fd);
+ } else if (imsg.fd != -1) {
+ if (close(imsg.fd) != 0 && err == NULL)
+ err = got_error_from_errno();
+ }
imsg_free(&imsg);
if (err)
break;
@@ -156,6 +158,7 @@ done:
got_privsep_send_error(&ibuf, err);
}
}
- close(GOT_IMSG_FD_CHILD);
+ if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+ err = got_error_from_errno();
return err ? 1 : 0;
}
diff --git a/libexec/got-read-tree/got-read-tree.c b/libexec/got-read-tree/got-read-tree.c
index df442f0..3edf27d 100644
--- a/libexec/got-read-tree/got-read-tree.c
+++ b/libexec/got-read-tree/got-read-tree.c
@@ -141,8 +141,10 @@ done:
if (f) {
if (fclose(f) != 0 && err == NULL)
err = got_error_from_errno();
- } else if (imsg.fd != -1)
- close(imsg.fd);
+ } else if (imsg.fd != -1) {
+ if (close(imsg.fd) != 0 && err == NULL)
+ err = got_error_from_errno();
+ }
imsg_free(&imsg);
if (err)
break;
@@ -155,6 +157,7 @@ done:
got_privsep_send_error(&ibuf, err);
}
}
- close(GOT_IMSG_FD_CHILD);
+ if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+ err = got_error_from_errno();
return err ? 1 : 0;
}