add read_object_header_privsep_child() helper
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
diff --git a/lib/object.c b/lib/object.c
index a8913e5..c2a0f0c 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -216,6 +216,51 @@ done:
return err;
}
+static void
+read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
+{
+ const struct got_error *err = NULL;
+ struct got_object *child_obj = NULL;
+ struct imsgbuf child_ibuf;
+ FILE *f = NULL;
+ int status = 0;
+
+ setproctitle("got: read object header");
+ close(imsg_fds[0]);
+ imsg_init(&child_ibuf, imsg_fds[1]);
+
+ /* revoke access to most system calls */
+ if (pledge("stdio", NULL) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ f = fdopen(obj_fd, "rb");
+ if (f == NULL) {
+ err = got_error_from_errno();
+ close(obj_fd);
+ goto done;
+ }
+
+ err = read_object_header(&child_obj, f);
+ if (err)
+ goto done;
+
+ err = got_privsep_send_obj(&child_ibuf, child_obj, 0);
+done:
+ if (child_obj)
+ got_object_close(child_obj);
+ if (err) {
+ got_privsep_send_error(&child_ibuf, err);
+ status = 1;
+ }
+ if (f)
+ fclose(f);
+ imsg_clear(&child_ibuf);
+ close(imsg_fds[1]);
+ _exit(status);
+}
+
static const struct got_error *
read_object_header_privsep(struct got_object **obj, int fd)
{
@@ -232,47 +277,8 @@ read_object_header_privsep(struct got_object **obj, int fd)
if (pid == -1)
return got_error_from_errno();
else if (pid == 0) {
- struct got_object *child_obj = NULL;
- struct imsgbuf child_ibuf;
- FILE *f = NULL;
- int status = 0;
-
- setproctitle("got: read object header");
- close(imsg_fds[0]);
- imsg_init(&child_ibuf, imsg_fds[1]);
- if (err)
- goto done;
-
- /* revoke access to most system calls */
- if (pledge("stdio", NULL) == -1) {
- err = got_error_from_errno();
- goto done;
- }
-
- f = fdopen(fd, "rb");
- if (f == NULL) {
- err = got_error_from_errno();
- close(fd);
- goto done;
- }
-
- err = read_object_header(&child_obj, f);
- if (err)
- goto done;
-
- err = got_privsep_send_obj(&child_ibuf, child_obj, 0);
-done:
- if (child_obj)
- got_object_close(child_obj);
- if (err) {
- got_privsep_send_error(&child_ibuf, err);
- status = 1;
- }
- if (f)
- fclose(f);
- imsg_clear(&child_ibuf);
- close(imsg_fds[1]);
- _exit(status);
+ read_object_header_privsep_child(fd, imsg_fds);
+ /* not reached */
}
close(imsg_fds[1]);