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/queue.h>
19 #include <sys/uio.h>
20 #include <sys/socket.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <imsg.h>
29 #include <sha1.h>
30 #include <limits.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_object.h"
38 #include "got_lib_privsep.h"
39 #include "got_lib_gotconfig.h"
41 const struct got_error *
42 got_gotconfig_read(struct got_gotconfig **conf, const char *gotconfig_path)
43 {
44 const struct got_error *err = NULL, *child_err = NULL;
45 int fd = -1;
46 int imsg_fds[2] = { -1, -1 };
47 pid_t pid;
48 struct imsgbuf *ibuf;
50 *conf = calloc(1, sizeof(**conf));
51 if (*conf == NULL)
52 return got_error_from_errno("calloc");
54 fd = open(gotconfig_path, O_RDONLY);
55 if (fd == -1) {
56 if (errno == ENOENT)
57 return NULL;
58 return got_error_from_errno2("open", gotconfig_path);
59 }
61 ibuf = calloc(1, sizeof(*ibuf));
62 if (ibuf == NULL) {
63 err = got_error_from_errno("calloc");
64 goto done;
65 }
67 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
68 err = got_error_from_errno("socketpair");
69 goto done;
70 }
72 pid = fork();
73 if (pid == -1) {
74 err = got_error_from_errno("fork");
75 goto done;
76 } else if (pid == 0) {
77 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
78 gotconfig_path);
79 /* not reached */
80 }
82 if (close(imsg_fds[1]) == -1) {
83 err = got_error_from_errno("close");
84 goto done;
85 }
86 imsg_fds[1] = -1;
87 imsg_init(ibuf, imsg_fds[0]);
89 err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
90 if (err)
91 goto done;
92 fd = -1;
94 err = got_privsep_send_gotconfig_author_req(ibuf);
95 if (err)
96 goto done;
98 err = got_privsep_recv_gotconfig_str(&(*conf)->author, ibuf);
99 if (err)
100 goto done;
102 err = got_privsep_send_gotconfig_remotes_req(ibuf);
103 if (err)
104 goto done;
106 err = got_privsep_recv_gotconfig_remotes(&(*conf)->remotes,
107 &(*conf)->nremotes, ibuf);
108 if (err)
109 goto done;
111 imsg_clear(ibuf);
112 err = got_privsep_send_stop(imsg_fds[0]);
113 child_err = got_privsep_wait_for_child(pid);
114 if (child_err && err == NULL)
115 err = child_err;
116 done:
117 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
118 err = got_error_from_errno("close");
119 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
120 err = got_error_from_errno("close");
121 if (fd != -1 && close(fd) == -1 && err == NULL)
122 err = got_error_from_errno2("close", gotconfig_path);
123 if (err) {
124 got_gotconfig_free(*conf);
125 *conf = NULL;
127 free(ibuf);
128 return err;
131 void
132 got_gotconfig_free(struct got_gotconfig *conf)
134 int i;
136 if (conf == NULL)
137 return;
139 free(conf->author);
141 for (i = 0; i < conf->nremotes; i++)
142 got_repo_free_remote_repo_data(&conf->remotes[i]);
143 free(conf->remotes);
144 free(conf);
147 const char *
148 got_gotconfig_get_author(const struct got_gotconfig *conf)
150 return conf->author;
153 void
154 got_gotconfig_get_remotes(int *nremotes, const struct got_remote_repo **remotes,
155 const struct got_gotconfig *conf)
157 *nremotes = conf->nremotes;
158 *remotes = conf->remotes;