2 * Copyright (c) 2019, 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/queue.h>
32 #include "got_compat.h"
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_object.h"
40 #include "got_lib_object_cache.h"
41 #include "got_lib_privsep.h"
42 #include "got_lib_pack.h"
43 #include "got_lib_repository.h"
45 const struct got_error
*
46 got_repo_read_gitconfig(int *gitconfig_repository_format_version
,
47 char **gitconfig_author_name
, char **gitconfig_author_email
,
48 struct got_remote_repo
**remotes
, int *nremotes
,
49 char **gitconfig_owner
, char ***extnames
, char ***extvals
,
50 int *nextensions
, const char *gitconfig_path
)
52 const struct got_error
*err
= NULL
, *child_err
= NULL
;
54 int imsg_fds
[2] = { -1, -1 };
58 *gitconfig_repository_format_version
= 0;
65 *gitconfig_author_name
= NULL
;
66 *gitconfig_author_email
= NULL
;
72 *gitconfig_owner
= NULL
;
74 fd
= open(gitconfig_path
, O_RDONLY
| O_CLOEXEC
);
78 return got_error_from_errno2("open", gitconfig_path
);
81 ibuf
= calloc(1, sizeof(*ibuf
));
83 err
= got_error_from_errno("calloc");
87 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, imsg_fds
) == -1) {
88 err
= got_error_from_errno("socketpair");
94 err
= got_error_from_errno("fork");
96 } else if (pid
== 0) {
97 got_privsep_exec_child(imsg_fds
, GOT_PATH_PROG_READ_GITCONFIG
,
102 if (close(imsg_fds
[1]) == -1) {
103 err
= got_error_from_errno("close");
107 imsg_init(ibuf
, imsg_fds
[0]);
109 err
= got_privsep_send_gitconfig_parse_req(ibuf
, fd
);
114 err
= got_privsep_send_gitconfig_repository_format_version_req(ibuf
);
118 err
= got_privsep_recv_gitconfig_int(
119 gitconfig_repository_format_version
, ibuf
);
123 if (extnames
&& extvals
&& nextensions
) {
124 err
= got_privsep_send_gitconfig_repository_extensions_req(
128 err
= got_privsep_recv_gitconfig_int(nextensions
, ibuf
);
131 if (*nextensions
> 0) {
133 *extnames
= calloc(*nextensions
, sizeof(char *));
134 if (*extnames
== NULL
) {
135 err
= got_error_from_errno("calloc");
138 *extvals
= calloc(*nextensions
, sizeof(char *));
139 if (*extvals
== NULL
) {
140 err
= got_error_from_errno("calloc");
143 for (i
= 0; i
< *nextensions
; i
++) {
145 err
= got_privsep_recv_gitconfig_pair(&ext
,
149 (*extnames
)[i
] = ext
;
155 err
= got_privsep_send_gitconfig_author_name_req(ibuf
);
159 err
= got_privsep_recv_gitconfig_str(gitconfig_author_name
, ibuf
);
163 err
= got_privsep_send_gitconfig_author_email_req(ibuf
);
167 err
= got_privsep_recv_gitconfig_str(gitconfig_author_email
, ibuf
);
171 if (remotes
&& nremotes
) {
172 err
= got_privsep_send_gitconfig_remotes_req(ibuf
);
176 err
= got_privsep_recv_gitconfig_remotes(remotes
,
182 if (gitconfig_owner
) {
183 err
= got_privsep_send_gitconfig_owner_req(ibuf
);
186 err
= got_privsep_recv_gitconfig_str(gitconfig_owner
, ibuf
);
191 err
= got_privsep_send_stop(imsg_fds
[0]);
192 child_err
= got_privsep_wait_for_child(pid
);
193 if (child_err
&& err
== NULL
)
196 if (imsg_fds
[0] != -1 && close(imsg_fds
[0]) == -1 && err
== NULL
)
197 err
= got_error_from_errno("close");
198 if (imsg_fds
[1] != -1 && close(imsg_fds
[1]) == -1 && err
== NULL
)
199 err
= got_error_from_errno("close");
200 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
201 err
= got_error_from_errno2("close", gitconfig_path
);