2 f1752522 2022-10-29 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 f1752522 2022-10-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 f1752522 2022-10-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 f1752522 2022-10-29 stsp * copyright notice and this permission notice appear in all copies.
8 f1752522 2022-10-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 f1752522 2022-10-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 f1752522 2022-10-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 f1752522 2022-10-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 f1752522 2022-10-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 f1752522 2022-10-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 f1752522 2022-10-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 f1752522 2022-10-29 stsp #include <sys/queue.h>
18 f1752522 2022-10-29 stsp #include <sys/socket.h>
19 f1752522 2022-10-29 stsp #include <sys/un.h>
21 f1752522 2022-10-29 stsp #include <err.h>
22 f1752522 2022-10-29 stsp #include <event.h>
23 f1752522 2022-10-29 stsp #include <imsg.h>
24 f1752522 2022-10-29 stsp #include <limits.h>
25 f1752522 2022-10-29 stsp #include <locale.h>
26 f1752522 2022-10-29 stsp #include <sha1.h>
27 f1752522 2022-10-29 stsp #include <stdio.h>
28 f1752522 2022-10-29 stsp #include <stdlib.h>
29 f1752522 2022-10-29 stsp #include <string.h>
30 f1752522 2022-10-29 stsp #include <getopt.h>
31 f1752522 2022-10-29 stsp #include <unistd.h>
33 f1752522 2022-10-29 stsp #include "got_error.h"
34 f1752522 2022-10-29 stsp #include "got_version.h"
36 f1752522 2022-10-29 stsp #include "got_lib_gitproto.h"
38 f1752522 2022-10-29 stsp #include "gotd.h"
40 f1752522 2022-10-29 stsp #ifndef nitems
41 f1752522 2022-10-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
44 f1752522 2022-10-29 stsp #define GOTCTL_CMD_INFO "info"
45 f1752522 2022-10-29 stsp #define GOTCTL_CMD_STOP "stop"
47 f1752522 2022-10-29 stsp struct gotctl_cmd {
48 f1752522 2022-10-29 stsp const char *cmd_name;
49 f1752522 2022-10-29 stsp const struct got_error *(*cmd_main)(int, char *[], int);
50 f1752522 2022-10-29 stsp void (*cmd_usage)(void);
53 f1752522 2022-10-29 stsp __dead static void usage(int, int);
55 f1752522 2022-10-29 stsp __dead static void usage_info(void);
56 f1752522 2022-10-29 stsp __dead static void usage_stop(void);
58 f1752522 2022-10-29 stsp static const struct got_error* cmd_info(int, char *[], int);
59 f1752522 2022-10-29 stsp static const struct got_error* cmd_stop(int, char *[], int);
61 f1752522 2022-10-29 stsp static const struct gotctl_cmd gotctl_commands[] = {
62 f1752522 2022-10-29 stsp { "info", cmd_info, usage_info },
63 f1752522 2022-10-29 stsp { "stop", cmd_stop, usage_stop },
66 f1752522 2022-10-29 stsp __dead static void
67 f1752522 2022-10-29 stsp usage_info(void)
69 f1752522 2022-10-29 stsp fprintf(stderr, "usage: %s info\n", getprogname());
73 f1752522 2022-10-29 stsp static const struct got_error *
74 f1752522 2022-10-29 stsp show_info(struct imsg *imsg)
76 f1752522 2022-10-29 stsp struct gotd_imsg_info info;
77 f1752522 2022-10-29 stsp size_t datalen;
79 f1752522 2022-10-29 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
80 f1752522 2022-10-29 stsp if (datalen != sizeof(info))
81 f1752522 2022-10-29 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
82 f1752522 2022-10-29 stsp memcpy(&info, imsg->data, sizeof(info));
84 f1752522 2022-10-29 stsp printf("gotd PID: %d\n", info.pid);
85 f1752522 2022-10-29 stsp printf("verbosity: %d\n", info.verbosity);
86 f1752522 2022-10-29 stsp printf("number of repositories: %d\n", info.nrepos);
87 f1752522 2022-10-29 stsp printf("number of connected clients: %d\n", info.nclients);
88 f1752522 2022-10-29 stsp return NULL;
91 f1752522 2022-10-29 stsp static const struct got_error *
92 f1752522 2022-10-29 stsp show_repo_info(struct imsg *imsg)
94 f1752522 2022-10-29 stsp struct gotd_imsg_info_repo info;
95 f1752522 2022-10-29 stsp size_t datalen;
97 f1752522 2022-10-29 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
98 f1752522 2022-10-29 stsp if (datalen != sizeof(info))
99 f1752522 2022-10-29 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
100 f1752522 2022-10-29 stsp memcpy(&info, imsg->data, sizeof(info));
102 f1752522 2022-10-29 stsp printf("repository \"%s\", path %s\n", info.repo_name, info.repo_path);
103 f1752522 2022-10-29 stsp return NULL;
106 f1752522 2022-10-29 stsp static const struct got_error *
107 f1752522 2022-10-29 stsp show_client_info(struct imsg *imsg)
109 f1752522 2022-10-29 stsp struct gotd_imsg_info_client info;
110 f1752522 2022-10-29 stsp size_t datalen;
112 f1752522 2022-10-29 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
113 f1752522 2022-10-29 stsp if (datalen != sizeof(info))
114 f1752522 2022-10-29 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
115 f1752522 2022-10-29 stsp memcpy(&info, imsg->data, sizeof(info));
117 eac23c30 2023-01-10 stsp printf("client UID %d, GID %d, ", info.euid, info.egid);
118 ae7c1b78 2023-01-10 stsp if (info.session_child_pid)
119 ae7c1b78 2023-01-10 stsp printf("session PID %ld, ", (long)info.session_child_pid);
120 ae7c1b78 2023-01-10 stsp if (info.repo_child_pid)
121 ae7c1b78 2023-01-10 stsp printf("repo PID %ld, ", (long)info.repo_child_pid);
122 f1752522 2022-10-29 stsp if (info.is_writing)
123 f1752522 2022-10-29 stsp printf("writing to %s\n", info.repo_name);
125 f1752522 2022-10-29 stsp printf("reading from %s\n", info.repo_name);
127 f1752522 2022-10-29 stsp return NULL;
130 f1752522 2022-10-29 stsp static const struct got_error *
131 f1752522 2022-10-29 stsp cmd_info(int argc, char *argv[], int gotd_sock)
133 f1752522 2022-10-29 stsp const struct got_error *err;
134 f1752522 2022-10-29 stsp struct imsgbuf ibuf;
135 f1752522 2022-10-29 stsp struct imsg imsg;
137 f1752522 2022-10-29 stsp imsg_init(&ibuf, gotd_sock);
139 f1752522 2022-10-29 stsp if (imsg_compose(&ibuf, GOTD_IMSG_INFO, 0, 0, -1, NULL, 0) == -1)
140 f1752522 2022-10-29 stsp return got_error_from_errno("imsg_compose INFO");
142 f1752522 2022-10-29 stsp err = gotd_imsg_flush(&ibuf);
143 f1752522 2022-10-29 stsp while (err == NULL) {
144 f1752522 2022-10-29 stsp err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
146 f1752522 2022-10-29 stsp if (err->code == GOT_ERR_EOF)
147 f1752522 2022-10-29 stsp err = NULL;
151 f1752522 2022-10-29 stsp switch (imsg.hdr.type) {
152 f1752522 2022-10-29 stsp case GOTD_IMSG_ERROR:
153 f1752522 2022-10-29 stsp err = gotd_imsg_recv_error(NULL, &imsg);
155 f1752522 2022-10-29 stsp case GOTD_IMSG_INFO:
156 f1752522 2022-10-29 stsp err = show_info(&imsg);
158 f1752522 2022-10-29 stsp case GOTD_IMSG_INFO_REPO:
159 f1752522 2022-10-29 stsp err = show_repo_info(&imsg);
161 f1752522 2022-10-29 stsp case GOTD_IMSG_INFO_CLIENT:
162 f1752522 2022-10-29 stsp err = show_client_info(&imsg);
165 f1752522 2022-10-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
169 f1752522 2022-10-29 stsp imsg_free(&imsg);
172 f1752522 2022-10-29 stsp imsg_clear(&ibuf);
173 f1752522 2022-10-29 stsp return err;
176 f1752522 2022-10-29 stsp __dead static void
177 f1752522 2022-10-29 stsp usage_stop(void)
179 f1752522 2022-10-29 stsp fprintf(stderr, "usage: %s stop\n", getprogname());
183 f1752522 2022-10-29 stsp static const struct got_error *
184 f1752522 2022-10-29 stsp cmd_stop(int argc, char *argv[], int gotd_sock)
186 f1752522 2022-10-29 stsp const struct got_error *err;
187 f1752522 2022-10-29 stsp struct imsgbuf ibuf;
188 f1752522 2022-10-29 stsp struct imsg imsg;
190 f1752522 2022-10-29 stsp imsg_init(&ibuf, gotd_sock);
192 f1752522 2022-10-29 stsp if (imsg_compose(&ibuf, GOTD_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
193 f1752522 2022-10-29 stsp return got_error_from_errno("imsg_compose STOP");
195 f1752522 2022-10-29 stsp err = gotd_imsg_flush(&ibuf);
196 f1752522 2022-10-29 stsp while (err == NULL) {
197 f1752522 2022-10-29 stsp err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
199 f1752522 2022-10-29 stsp if (err->code == GOT_ERR_EOF)
200 f1752522 2022-10-29 stsp err = NULL;
204 f1752522 2022-10-29 stsp switch (imsg.hdr.type) {
205 f1752522 2022-10-29 stsp case GOTD_IMSG_ERROR:
206 f1752522 2022-10-29 stsp err = gotd_imsg_recv_error(NULL, &imsg);
209 f1752522 2022-10-29 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
213 f1752522 2022-10-29 stsp imsg_free(&imsg);
216 f1752522 2022-10-29 stsp imsg_clear(&ibuf);
217 f1752522 2022-10-29 stsp return err;
220 f1752522 2022-10-29 stsp static void
221 f1752522 2022-10-29 stsp list_commands(FILE *fp)
225 f1752522 2022-10-29 stsp fprintf(fp, "commands:");
226 f1752522 2022-10-29 stsp for (i = 0; i < nitems(gotctl_commands); i++) {
227 f1752522 2022-10-29 stsp const struct gotctl_cmd *cmd = &gotctl_commands[i];
228 f1752522 2022-10-29 stsp fprintf(fp, " %s", cmd->cmd_name);
230 f1752522 2022-10-29 stsp fputc('\n', fp);
233 f1752522 2022-10-29 stsp __dead static void
234 f1752522 2022-10-29 stsp usage(int hflag, int status)
236 f1752522 2022-10-29 stsp FILE *fp = (status == 0) ? stdout : stderr;
238 a5a750bd 2022-11-14 op fprintf(fp, "usage: %s [-hV] [-f path] command [arg ...]\n",
239 f1752522 2022-10-29 stsp getprogname());
241 f1752522 2022-10-29 stsp list_commands(fp);
242 f1752522 2022-10-29 stsp exit(status);
245 f1752522 2022-10-29 stsp static const struct got_error *
246 f1752522 2022-10-29 stsp apply_unveil(const char *unix_socket_path)
248 f1752522 2022-10-29 stsp #ifdef PROFILE
249 f1752522 2022-10-29 stsp if (unveil("gmon.out", "rwc") != 0)
250 f1752522 2022-10-29 stsp return got_error_from_errno2("unveil", "gmon.out");
252 f1752522 2022-10-29 stsp if (unveil(unix_socket_path, "w") != 0)
253 f1752522 2022-10-29 stsp return got_error_from_errno2("unveil", unix_socket_path);
255 f1752522 2022-10-29 stsp if (unveil(NULL, NULL) != 0)
256 f1752522 2022-10-29 stsp return got_error_from_errno("unveil");
258 f1752522 2022-10-29 stsp return NULL;
262 f1752522 2022-10-29 stsp connect_gotd(const char *socket_path)
264 f1752522 2022-10-29 stsp const struct got_error *error = NULL;
265 f1752522 2022-10-29 stsp int gotd_sock = -1;
266 f1752522 2022-10-29 stsp struct sockaddr_un sun;
268 f5d30fbb 2022-10-29 op error = apply_unveil(socket_path);
270 f1752522 2022-10-29 stsp errx(1, "%s", error->msg);
272 f1752522 2022-10-29 stsp #ifndef PROFILE
273 f1752522 2022-10-29 stsp if (pledge("stdio unix", NULL) == -1)
274 f1752522 2022-10-29 stsp err(1, "pledge");
276 f1752522 2022-10-29 stsp if ((gotd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
277 f1752522 2022-10-29 stsp err(1, "socket");
279 f1752522 2022-10-29 stsp memset(&sun, 0, sizeof(sun));
280 f1752522 2022-10-29 stsp sun.sun_family = AF_UNIX;
281 f5d30fbb 2022-10-29 op if (strlcpy(sun.sun_path, socket_path, sizeof(sun.sun_path)) >=
282 f5d30fbb 2022-10-29 op sizeof(sun.sun_path))
283 f1752522 2022-10-29 stsp errx(1, "gotd socket path too long");
284 f1752522 2022-10-29 stsp if (connect(gotd_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
285 f5d30fbb 2022-10-29 op err(1, "connect: %s", socket_path);
287 f1752522 2022-10-29 stsp #ifndef PROFILE
288 f1752522 2022-10-29 stsp if (pledge("stdio", NULL) == -1)
289 f1752522 2022-10-29 stsp err(1, "pledge");
292 f1752522 2022-10-29 stsp return gotd_sock;
296 f1752522 2022-10-29 stsp main(int argc, char *argv[])
298 f1752522 2022-10-29 stsp const struct gotctl_cmd *cmd;
299 f1752522 2022-10-29 stsp int gotd_sock = -1, i;
301 f1752522 2022-10-29 stsp int hflag = 0, Vflag = 0;
302 f1752522 2022-10-29 stsp static const struct option longopts[] = {
303 f1752522 2022-10-29 stsp { "version", no_argument, NULL, 'V' },
304 f1752522 2022-10-29 stsp { NULL, 0, NULL, 0 }
306 f5d30fbb 2022-10-29 op const char *socket_path = GOTD_UNIX_SOCKET;
308 f1752522 2022-10-29 stsp setlocale(LC_CTYPE, "");
310 f1752522 2022-10-29 stsp #ifndef PROFILE
311 f1752522 2022-10-29 stsp if (pledge("stdio unix unveil", NULL) == -1)
312 f1752522 2022-10-29 stsp err(1, "pledge");
315 f1752522 2022-10-29 stsp while ((ch = getopt_long(argc, argv, "+hf:V", longopts, NULL)) != -1) {
316 f1752522 2022-10-29 stsp switch (ch) {
321 f1752522 2022-10-29 stsp socket_path = optarg;
327 f1752522 2022-10-29 stsp usage(hflag, 1);
328 f1752522 2022-10-29 stsp /* NOTREACHED */
332 f1752522 2022-10-29 stsp argc -= optind;
333 f1752522 2022-10-29 stsp argv += optind;
334 f1752522 2022-10-29 stsp optind = 1;
335 f1752522 2022-10-29 stsp optreset = 1;
337 f1752522 2022-10-29 stsp if (Vflag) {
338 f1752522 2022-10-29 stsp got_version_print_str();
342 f1752522 2022-10-29 stsp if (argc <= 0)
343 f1752522 2022-10-29 stsp usage(hflag, hflag ? 0 : 1);
345 f1752522 2022-10-29 stsp for (i = 0; i < nitems(gotctl_commands); i++) {
346 f1752522 2022-10-29 stsp const struct got_error *error;
348 f1752522 2022-10-29 stsp cmd = &gotctl_commands[i];
350 f1752522 2022-10-29 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])) != 0)
354 f1752522 2022-10-29 stsp cmd->cmd_usage();
356 f1752522 2022-10-29 stsp gotd_sock = connect_gotd(socket_path);
357 f1752522 2022-10-29 stsp if (gotd_sock == -1)
359 f1752522 2022-10-29 stsp error = cmd->cmd_main(argc, argv, gotd_sock);
360 f1752522 2022-10-29 stsp close(gotd_sock);
361 f1752522 2022-10-29 stsp if (error) {
362 f1752522 2022-10-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
369 f1752522 2022-10-29 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
370 f1752522 2022-10-29 stsp list_commands(stderr);