Blob


1 /*
2 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/uio.h>
19 #include <sys/socket.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <limits.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)
42 {
43 const struct got_error *err = NULL, *child_err = NULL;
44 int fd = -1;
45 int imsg_fds[2] = { -1, -1 };
46 pid_t pid;
47 struct imsgbuf *ibuf;
49 *conf = calloc(1, sizeof(**conf));
50 if (*conf == NULL)
51 return got_error_from_errno("calloc");
53 fd = open(gotconfig_path, O_RDONLY);
54 if (fd == -1) {
55 if (errno == ENOENT)
56 return NULL;
57 return got_error_from_errno2("open", gotconfig_path);
58 }
60 ibuf = calloc(1, sizeof(*ibuf));
61 if (ibuf == NULL) {
62 err = got_error_from_errno("calloc");
63 goto done;
64 }
66 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
67 err = got_error_from_errno("socketpair");
68 goto done;
69 }
71 pid = fork();
72 if (pid == -1) {
73 err = got_error_from_errno("fork");
74 goto done;
75 } else if (pid == 0) {
76 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
77 gotconfig_path);
78 /* not reached */
79 }
81 if (close(imsg_fds[1]) == -1) {
82 err = got_error_from_errno("close");
83 goto done;
84 }
85 imsg_fds[1] = -1;
86 imsg_init(ibuf, imsg_fds[0]);
88 err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
89 if (err)
90 goto done;
91 fd = -1;
93 err = got_privsep_send_gotconfig_author_req(ibuf);
94 if (err)
95 goto done;
97 err = got_privsep_recv_gotconfig_str(&(*conf)->author, ibuf);
98 if (err)
99 goto done;
101 err = got_privsep_send_gotconfig_remotes_req(ibuf);
102 if (err)
103 goto done;
105 err = got_privsep_recv_gotconfig_remotes(&(*conf)->remotes,
106 &(*conf)->nremotes, ibuf);
107 if (err)
108 goto done;
110 imsg_clear(ibuf);
111 err = got_privsep_send_stop(imsg_fds[0]);
112 child_err = got_privsep_wait_for_child(pid);
113 if (child_err && err == NULL)
114 err = child_err;
115 done:
116 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
117 err = got_error_from_errno("close");
118 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
119 err = got_error_from_errno("close");
120 if (fd != -1 && close(fd) == -1 && err == NULL)
121 err = got_error_from_errno2("close", gotconfig_path);
122 if (err) {
123 got_gotconfig_free(*conf);
124 *conf = NULL;
126 free(ibuf);
127 return err;
130 void
131 got_gotconfig_free(struct got_gotconfig *conf)
133 int i;
135 if (conf == NULL)
136 return;
138 free(conf->author);
140 for (i = 0; i < conf->nremotes; i++)
141 got_repo_free_remote_repo_data(&conf->remotes[i]);
142 free(conf->remotes);
143 free(conf);
146 const char *
147 got_gotconfig_get_author(const struct got_gotconfig *conf)
149 return conf->author;
152 void
153 got_gotconfig_get_remotes(int *nremotes, const struct got_remote_repo **remotes,
154 const struct got_gotconfig *conf)
156 *nremotes = conf->nremotes;
157 *remotes = conf->remotes;