2 * Copyright (c) 2019 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>
18 #include <sys/queue.h>
21 #include <sys/syslimits.h>
33 #include "got_error.h"
34 #include "got_object.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_object.h"
38 #include "got_lib_privsep.h"
39 #include "got_lib_gitconfig.h"
41 static volatile sig_atomic_t sigint_received;
44 catch_sigint(int signo)
49 static const struct got_error *
50 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
51 char *section, char *tag, int def)
55 if (gitconfig == NULL)
56 return got_error(GOT_ERR_PRIVSEP_MSG);
58 value = got_gitconfig_get_num(gitconfig, section, tag, def);
59 return got_privsep_send_gitconfig_int(ibuf, value);
62 static const struct got_error *
63 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
64 char *section, char *tag)
68 if (gitconfig == NULL)
69 return got_error(GOT_ERR_PRIVSEP_MSG);
71 value = got_gitconfig_get_str(gitconfig, section, tag);
72 return got_privsep_send_gitconfig_str(ibuf, value);
76 main(int argc, char *argv[])
78 const struct got_error *err = NULL;
81 struct got_gitconfig *gitconfig = NULL;
88 signal(SIGINT, catch_sigint);
90 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
93 /* revoke access to most system calls */
94 if (pledge("stdio recvfd", NULL) == -1) {
95 err = got_error_from_errno("pledge");
96 got_privsep_send_error(&ibuf, err);
104 memset(&imsg, 0, sizeof(imsg));
107 if (sigint_received) {
108 err = got_error(GOT_ERR_CANCELLED);
112 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
114 if (err->code == GOT_ERR_PRIVSEP_PIPE)
119 if (imsg.hdr.type == GOT_IMSG_STOP)
122 switch (imsg.hdr.type) {
123 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
124 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
126 err = got_error(GOT_ERR_PRIVSEP_LEN);
130 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
135 got_gitconfig_close(gitconfig);
136 err = got_gitconfig_open(&gitconfig, imsg.fd);
138 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
139 err = gitconfig_num_request(&ibuf, gitconfig, "core",
140 "repositoryformatversion", 0);
142 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
143 err = gitconfig_str_request(&ibuf, gitconfig, "user",
146 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
147 err = gitconfig_str_request(&ibuf, gitconfig, "user",
151 err = got_error(GOT_ERR_PRIVSEP_MSG);
156 if (close(imsg.fd) == -1 && err == NULL)
157 err = got_error_from_errno("close");
167 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
168 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
169 got_privsep_send_error(&ibuf, err);
172 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
173 err = got_error_from_errno("close");