2 c902213d 2022-10-29 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 c902213d 2022-10-29 thomas * Permission to use, copy, modify, and distribute this software for any
5 c902213d 2022-10-29 thomas * purpose with or without fee is hereby granted, provided that the above
6 c902213d 2022-10-29 thomas * copyright notice and this permission notice appear in all copies.
8 c902213d 2022-10-29 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c902213d 2022-10-29 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c902213d 2022-10-29 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c902213d 2022-10-29 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c902213d 2022-10-29 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c902213d 2022-10-29 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c902213d 2022-10-29 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4efc8dcb 2023-08-29 thomas #include "got_compat.h"
19 c902213d 2022-10-29 thomas #include <sys/queue.h>
20 c902213d 2022-10-29 thomas #include <sys/socket.h>
21 c902213d 2022-10-29 thomas #include <sys/un.h>
23 c902213d 2022-10-29 thomas #include <err.h>
24 c902213d 2022-10-29 thomas #include <event.h>
25 c902213d 2022-10-29 thomas #include <imsg.h>
26 c902213d 2022-10-29 thomas #include <limits.h>
27 c902213d 2022-10-29 thomas #include <locale.h>
28 c902213d 2022-10-29 thomas #include <stdio.h>
29 c902213d 2022-10-29 thomas #include <stdlib.h>
30 c902213d 2022-10-29 thomas #include <string.h>
31 c902213d 2022-10-29 thomas #include <getopt.h>
32 c902213d 2022-10-29 thomas #include <unistd.h>
34 c902213d 2022-10-29 thomas #include "got_error.h"
35 c902213d 2022-10-29 thomas #include "got_version.h"
36 6d7eb4f7 2023-04-04 thomas #include "got_path.h"
38 c902213d 2022-10-29 thomas #include "got_lib_gitproto.h"
40 c902213d 2022-10-29 thomas #include "gotd.h"
42 c902213d 2022-10-29 thomas #ifndef nitems
43 c902213d 2022-10-29 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 c902213d 2022-10-29 thomas #define GOTCTL_CMD_INFO "info"
47 c902213d 2022-10-29 thomas #define GOTCTL_CMD_STOP "stop"
49 c902213d 2022-10-29 thomas struct gotctl_cmd {
50 c902213d 2022-10-29 thomas const char *cmd_name;
51 c902213d 2022-10-29 thomas const struct got_error *(*cmd_main)(int, char *[], int);
52 c902213d 2022-10-29 thomas void (*cmd_usage)(void);
55 c902213d 2022-10-29 thomas __dead static void usage(int, int);
57 c902213d 2022-10-29 thomas __dead static void usage_info(void);
58 c902213d 2022-10-29 thomas __dead static void usage_stop(void);
60 c902213d 2022-10-29 thomas static const struct got_error* cmd_info(int, char *[], int);
61 c902213d 2022-10-29 thomas static const struct got_error* cmd_stop(int, char *[], int);
63 c902213d 2022-10-29 thomas static const struct gotctl_cmd gotctl_commands[] = {
64 c902213d 2022-10-29 thomas { "info", cmd_info, usage_info },
65 c902213d 2022-10-29 thomas { "stop", cmd_stop, usage_stop },
68 c902213d 2022-10-29 thomas __dead static void
69 c902213d 2022-10-29 thomas usage_info(void)
71 c902213d 2022-10-29 thomas fprintf(stderr, "usage: %s info\n", getprogname());
75 c902213d 2022-10-29 thomas static const struct got_error *
76 c902213d 2022-10-29 thomas show_info(struct imsg *imsg)
78 c902213d 2022-10-29 thomas struct gotd_imsg_info info;
79 c902213d 2022-10-29 thomas size_t datalen;
81 c902213d 2022-10-29 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
82 c902213d 2022-10-29 thomas if (datalen != sizeof(info))
83 c902213d 2022-10-29 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
84 c902213d 2022-10-29 thomas memcpy(&info, imsg->data, sizeof(info));
86 c902213d 2022-10-29 thomas printf("gotd PID: %d\n", info.pid);
87 c902213d 2022-10-29 thomas printf("verbosity: %d\n", info.verbosity);
88 c902213d 2022-10-29 thomas printf("number of repositories: %d\n", info.nrepos);
89 c902213d 2022-10-29 thomas printf("number of connected clients: %d\n", info.nclients);
90 c902213d 2022-10-29 thomas return NULL;
93 c902213d 2022-10-29 thomas static const struct got_error *
94 c902213d 2022-10-29 thomas show_repo_info(struct imsg *imsg)
96 c902213d 2022-10-29 thomas struct gotd_imsg_info_repo info;
97 c902213d 2022-10-29 thomas size_t datalen;
99 c902213d 2022-10-29 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
100 c902213d 2022-10-29 thomas if (datalen != sizeof(info))
101 c902213d 2022-10-29 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
102 c902213d 2022-10-29 thomas memcpy(&info, imsg->data, sizeof(info));
104 c902213d 2022-10-29 thomas printf("repository \"%s\", path %s\n", info.repo_name, info.repo_path);
105 c902213d 2022-10-29 thomas return NULL;
108 c902213d 2022-10-29 thomas static const struct got_error *
109 c902213d 2022-10-29 thomas show_client_info(struct imsg *imsg)
111 c902213d 2022-10-29 thomas struct gotd_imsg_info_client info;
112 c902213d 2022-10-29 thomas size_t datalen;
114 c902213d 2022-10-29 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
115 c902213d 2022-10-29 thomas if (datalen != sizeof(info))
116 c902213d 2022-10-29 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
117 c902213d 2022-10-29 thomas memcpy(&info, imsg->data, sizeof(info));
119 7b1db75e 2023-01-14 thomas printf("client UID %d, GID %d, ", info.euid, info.egid);
120 62ee7d94 2023-01-10 thomas if (info.session_child_pid)
121 62ee7d94 2023-01-10 thomas printf("session PID %ld, ", (long)info.session_child_pid);
122 62ee7d94 2023-01-10 thomas if (info.repo_child_pid)
123 62ee7d94 2023-01-10 thomas printf("repo PID %ld, ", (long)info.repo_child_pid);
124 c902213d 2022-10-29 thomas if (info.is_writing)
125 c902213d 2022-10-29 thomas printf("writing to %s\n", info.repo_name);
127 c902213d 2022-10-29 thomas printf("reading from %s\n", info.repo_name);
129 c902213d 2022-10-29 thomas return NULL;
132 c902213d 2022-10-29 thomas static const struct got_error *
133 c902213d 2022-10-29 thomas cmd_info(int argc, char *argv[], int gotd_sock)
135 c902213d 2022-10-29 thomas const struct got_error *err;
136 c902213d 2022-10-29 thomas struct imsgbuf ibuf;
137 c902213d 2022-10-29 thomas struct imsg imsg;
139 c902213d 2022-10-29 thomas imsg_init(&ibuf, gotd_sock);
141 c902213d 2022-10-29 thomas if (imsg_compose(&ibuf, GOTD_IMSG_INFO, 0, 0, -1, NULL, 0) == -1)
142 c902213d 2022-10-29 thomas return got_error_from_errno("imsg_compose INFO");
144 c902213d 2022-10-29 thomas err = gotd_imsg_flush(&ibuf);
145 c902213d 2022-10-29 thomas while (err == NULL) {
146 c902213d 2022-10-29 thomas err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
147 c902213d 2022-10-29 thomas if (err) {
148 c902213d 2022-10-29 thomas if (err->code == GOT_ERR_EOF)
149 c902213d 2022-10-29 thomas err = NULL;
153 c902213d 2022-10-29 thomas switch (imsg.hdr.type) {
154 c902213d 2022-10-29 thomas case GOTD_IMSG_ERROR:
155 c902213d 2022-10-29 thomas err = gotd_imsg_recv_error(NULL, &imsg);
157 c902213d 2022-10-29 thomas case GOTD_IMSG_INFO:
158 c902213d 2022-10-29 thomas err = show_info(&imsg);
160 c902213d 2022-10-29 thomas case GOTD_IMSG_INFO_REPO:
161 c902213d 2022-10-29 thomas err = show_repo_info(&imsg);
163 c902213d 2022-10-29 thomas case GOTD_IMSG_INFO_CLIENT:
164 c902213d 2022-10-29 thomas err = show_client_info(&imsg);
167 c902213d 2022-10-29 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
171 c902213d 2022-10-29 thomas imsg_free(&imsg);
174 c902213d 2022-10-29 thomas imsg_clear(&ibuf);
175 c902213d 2022-10-29 thomas return err;
178 c902213d 2022-10-29 thomas __dead static void
179 c902213d 2022-10-29 thomas usage_stop(void)
181 c902213d 2022-10-29 thomas fprintf(stderr, "usage: %s stop\n", getprogname());
185 c902213d 2022-10-29 thomas static const struct got_error *
186 c902213d 2022-10-29 thomas cmd_stop(int argc, char *argv[], int gotd_sock)
188 c902213d 2022-10-29 thomas const struct got_error *err;
189 c902213d 2022-10-29 thomas struct imsgbuf ibuf;
190 c902213d 2022-10-29 thomas struct imsg imsg;
192 c902213d 2022-10-29 thomas imsg_init(&ibuf, gotd_sock);
194 c902213d 2022-10-29 thomas if (imsg_compose(&ibuf, GOTD_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
195 c902213d 2022-10-29 thomas return got_error_from_errno("imsg_compose STOP");
197 c902213d 2022-10-29 thomas err = gotd_imsg_flush(&ibuf);
198 c902213d 2022-10-29 thomas while (err == NULL) {
199 c902213d 2022-10-29 thomas err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
200 c902213d 2022-10-29 thomas if (err) {
201 c902213d 2022-10-29 thomas if (err->code == GOT_ERR_EOF)
202 c902213d 2022-10-29 thomas err = NULL;
206 c902213d 2022-10-29 thomas switch (imsg.hdr.type) {
207 c902213d 2022-10-29 thomas case GOTD_IMSG_ERROR:
208 c902213d 2022-10-29 thomas err = gotd_imsg_recv_error(NULL, &imsg);
211 c902213d 2022-10-29 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
215 c902213d 2022-10-29 thomas imsg_free(&imsg);
218 c902213d 2022-10-29 thomas imsg_clear(&ibuf);
219 c902213d 2022-10-29 thomas return err;
222 c902213d 2022-10-29 thomas static void
223 c902213d 2022-10-29 thomas list_commands(FILE *fp)
225 c902213d 2022-10-29 thomas size_t i;
227 c902213d 2022-10-29 thomas fprintf(fp, "commands:");
228 c902213d 2022-10-29 thomas for (i = 0; i < nitems(gotctl_commands); i++) {
229 c902213d 2022-10-29 thomas const struct gotctl_cmd *cmd = &gotctl_commands[i];
230 c902213d 2022-10-29 thomas fprintf(fp, " %s", cmd->cmd_name);
232 c902213d 2022-10-29 thomas fputc('\n', fp);
235 c902213d 2022-10-29 thomas __dead static void
236 c902213d 2022-10-29 thomas usage(int hflag, int status)
238 c902213d 2022-10-29 thomas FILE *fp = (status == 0) ? stdout : stderr;
240 c691a00e 2022-11-17 thomas fprintf(fp, "usage: %s [-hV] [-f path] command [arg ...]\n",
241 c902213d 2022-10-29 thomas getprogname());
242 c902213d 2022-10-29 thomas if (hflag)
243 c902213d 2022-10-29 thomas list_commands(fp);
244 c902213d 2022-10-29 thomas exit(status);
247 c902213d 2022-10-29 thomas static const struct got_error *
248 c902213d 2022-10-29 thomas apply_unveil(const char *unix_socket_path)
250 c902213d 2022-10-29 thomas #ifdef PROFILE
251 c902213d 2022-10-29 thomas if (unveil("gmon.out", "rwc") != 0)
252 c902213d 2022-10-29 thomas return got_error_from_errno2("unveil", "gmon.out");
254 c902213d 2022-10-29 thomas if (unveil(unix_socket_path, "w") != 0)
255 c902213d 2022-10-29 thomas return got_error_from_errno2("unveil", unix_socket_path);
257 c902213d 2022-10-29 thomas if (unveil(NULL, NULL) != 0)
258 c902213d 2022-10-29 thomas return got_error_from_errno("unveil");
260 c902213d 2022-10-29 thomas return NULL;
263 c902213d 2022-10-29 thomas static int
264 c902213d 2022-10-29 thomas connect_gotd(const char *socket_path)
266 c902213d 2022-10-29 thomas const struct got_error *error = NULL;
267 c902213d 2022-10-29 thomas int gotd_sock = -1;
268 c902213d 2022-10-29 thomas struct sockaddr_un sun;
270 4f218b0c 2022-10-30 thomas error = apply_unveil(socket_path);
271 c902213d 2022-10-29 thomas if (error)
272 c902213d 2022-10-29 thomas errx(1, "%s", error->msg);
274 c902213d 2022-10-29 thomas #ifndef PROFILE
275 c902213d 2022-10-29 thomas if (pledge("stdio unix", NULL) == -1)
276 c902213d 2022-10-29 thomas err(1, "pledge");
278 c902213d 2022-10-29 thomas if ((gotd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
279 c902213d 2022-10-29 thomas err(1, "socket");
281 c902213d 2022-10-29 thomas memset(&sun, 0, sizeof(sun));
282 c902213d 2022-10-29 thomas sun.sun_family = AF_UNIX;
283 4f218b0c 2022-10-30 thomas if (strlcpy(sun.sun_path, socket_path, sizeof(sun.sun_path)) >=
284 4f218b0c 2022-10-30 thomas sizeof(sun.sun_path))
285 c902213d 2022-10-29 thomas errx(1, "gotd socket path too long");
286 c902213d 2022-10-29 thomas if (connect(gotd_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
287 4f218b0c 2022-10-30 thomas err(1, "connect: %s", socket_path);
289 c902213d 2022-10-29 thomas #ifndef PROFILE
290 c902213d 2022-10-29 thomas if (pledge("stdio", NULL) == -1)
291 c902213d 2022-10-29 thomas err(1, "pledge");
294 c902213d 2022-10-29 thomas return gotd_sock;
298 c902213d 2022-10-29 thomas main(int argc, char *argv[])
300 c902213d 2022-10-29 thomas const struct gotctl_cmd *cmd;
301 c902213d 2022-10-29 thomas int gotd_sock = -1, i;
303 c902213d 2022-10-29 thomas int hflag = 0, Vflag = 0;
304 c902213d 2022-10-29 thomas static const struct option longopts[] = {
305 c902213d 2022-10-29 thomas { "version", no_argument, NULL, 'V' },
306 c902213d 2022-10-29 thomas { NULL, 0, NULL, 0 }
308 4f218b0c 2022-10-30 thomas const char *socket_path = GOTD_UNIX_SOCKET;
310 c902213d 2022-10-29 thomas setlocale(LC_CTYPE, "");
312 c902213d 2022-10-29 thomas #ifndef PROFILE
313 c902213d 2022-10-29 thomas if (pledge("stdio unix unveil", NULL) == -1)
314 c902213d 2022-10-29 thomas err(1, "pledge");
317 c902213d 2022-10-29 thomas while ((ch = getopt_long(argc, argv, "+hf:V", longopts, NULL)) != -1) {
318 c902213d 2022-10-29 thomas switch (ch) {
319 c902213d 2022-10-29 thomas case 'h':
320 c902213d 2022-10-29 thomas hflag = 1;
322 c902213d 2022-10-29 thomas case 'f':
323 c902213d 2022-10-29 thomas socket_path = optarg;
325 c902213d 2022-10-29 thomas case 'V':
326 c902213d 2022-10-29 thomas Vflag = 1;
329 c902213d 2022-10-29 thomas usage(hflag, 1);
330 c902213d 2022-10-29 thomas /* NOTREACHED */
334 c902213d 2022-10-29 thomas argc -= optind;
335 c902213d 2022-10-29 thomas argv += optind;
336 c902213d 2022-10-29 thomas optind = 1;
337 c902213d 2022-10-29 thomas optreset = 1;
339 c902213d 2022-10-29 thomas if (Vflag) {
340 c902213d 2022-10-29 thomas got_version_print_str();
341 c902213d 2022-10-29 thomas return 0;
344 c902213d 2022-10-29 thomas if (argc <= 0)
345 c902213d 2022-10-29 thomas usage(hflag, hflag ? 0 : 1);
347 c902213d 2022-10-29 thomas for (i = 0; i < nitems(gotctl_commands); i++) {
348 c902213d 2022-10-29 thomas const struct got_error *error;
350 c902213d 2022-10-29 thomas cmd = &gotctl_commands[i];
352 c902213d 2022-10-29 thomas if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])) != 0)
353 c902213d 2022-10-29 thomas continue;
355 c902213d 2022-10-29 thomas if (hflag)
356 c902213d 2022-10-29 thomas cmd->cmd_usage();
358 c902213d 2022-10-29 thomas gotd_sock = connect_gotd(socket_path);
359 c902213d 2022-10-29 thomas if (gotd_sock == -1)
360 c902213d 2022-10-29 thomas return 1;
361 c902213d 2022-10-29 thomas error = cmd->cmd_main(argc, argv, gotd_sock);
362 c902213d 2022-10-29 thomas close(gotd_sock);
363 c902213d 2022-10-29 thomas if (error) {
364 c902213d 2022-10-29 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
365 c902213d 2022-10-29 thomas return 1;
368 c902213d 2022-10-29 thomas return 0;
371 c902213d 2022-10-29 thomas fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
372 c902213d 2022-10-29 thomas list_commands(stderr);
373 c902213d 2022-10-29 thomas return 1;