2 * Copyright (c) 2020 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.
17 #include <sys/types.h>
19 #include <sys/socket.h>
29 #include "got_compat.h"
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_repository.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_object.h"
37 #include "got_lib_privsep.h"
38 #include "got_lib_gotconfig.h"
40 const struct got_error *
41 got_gotconfig_read(struct got_gotconfig **conf, const char *gotconfig_path)
43 const struct got_error *err = NULL, *child_err = NULL;
45 int imsg_fds[2] = { -1, -1 };
49 *conf = calloc(1, sizeof(**conf));
51 return got_error_from_errno("calloc");
53 fd = open(gotconfig_path, O_RDONLY | O_CLOEXEC);
57 return got_error_from_errno2("open", gotconfig_path);
60 ibuf = calloc(1, sizeof(*ibuf));
62 err = got_error_from_errno("calloc");
66 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
67 err = got_error_from_errno("socketpair");
73 err = got_error_from_errno("fork");
75 } else if (pid == 0) {
76 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
81 if (close(imsg_fds[1]) == -1) {
82 err = got_error_from_errno("close");
86 imsg_init(ibuf, imsg_fds[0]);
88 err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
93 err = got_privsep_send_gotconfig_author_req(ibuf);
97 err = got_privsep_recv_gotconfig_str(&(*conf)->author, ibuf);
101 err = got_privsep_send_gotconfig_remotes_req(ibuf);
105 err = got_privsep_recv_gotconfig_remotes(&(*conf)->remotes,
106 &(*conf)->nremotes, ibuf);
110 err = got_privsep_send_stop(imsg_fds[0]);
111 child_err = got_privsep_wait_for_child(pid);
112 if (child_err && err == NULL)
115 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
116 err = got_error_from_errno("close");
117 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
118 err = got_error_from_errno("close");
119 if (fd != -1 && close(fd) == -1 && err == NULL)
120 err = got_error_from_errno2("close", gotconfig_path);
122 got_gotconfig_free(*conf);
130 got_gotconfig_free(struct got_gotconfig *conf)
139 for (i = 0; i < conf->nremotes; i++)
140 got_repo_free_remote_repo_data(&conf->remotes[i]);
146 got_gotconfig_get_author(const struct got_gotconfig *conf)
152 got_gotconfig_get_remotes(int *nremotes, const struct got_remote_repo **remotes,
153 const struct got_gotconfig *conf)
155 *nremotes = conf->nremotes;
156 *remotes = conf->remotes;