sync got.1 authors list with people appearing in commit log and copyright lines
[got-portable.git] / gotctl / gotctl.c
blob01004be305ffcd67c3e65eee2369956e16f23b5c
1 /*
2 * Copyright (c) 2022 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 "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
23 #include <err.h>
24 #include <event.h>
25 #include <imsg.h>
26 #include <limits.h>
27 #include <locale.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <getopt.h>
32 #include <unistd.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_version.h"
37 #include "got_path.h"
39 #include "got_lib_gitproto.h"
41 #include "gotd.h"
43 #ifndef nitems
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 #endif
47 #define GOTCTL_CMD_INFO "info"
48 #define GOTCTL_CMD_STOP "stop"
50 struct gotctl_cmd {
51 const char *cmd_name;
52 const struct got_error *(*cmd_main)(int, char *[], int);
53 void (*cmd_usage)(void);
56 __dead static void usage(int, int);
58 __dead static void usage_info(void);
59 __dead static void usage_stop(void);
61 static const struct got_error* cmd_info(int, char *[], int);
62 static const struct got_error* cmd_stop(int, char *[], int);
64 static const struct gotctl_cmd gotctl_commands[] = {
65 { "info", cmd_info, usage_info },
66 { "stop", cmd_stop, usage_stop },
69 __dead static void
70 usage_info(void)
72 fprintf(stderr, "usage: %s info\n", getprogname());
73 exit(1);
76 static const struct got_error *
77 show_info(struct imsg *imsg)
79 struct gotd_imsg_info info;
80 size_t datalen;
82 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
83 if (datalen != sizeof(info))
84 return got_error(GOT_ERR_PRIVSEP_LEN);
85 memcpy(&info, imsg->data, sizeof(info));
87 printf("gotd PID: %d\n", info.pid);
88 printf("verbosity: %d\n", info.verbosity);
89 printf("number of repositories: %d\n", info.nrepos);
90 printf("number of connected clients: %d\n", info.nclients);
91 return NULL;
94 static const struct got_error *
95 show_repo_info(struct imsg *imsg)
97 struct gotd_imsg_info_repo info;
98 size_t datalen;
100 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
101 if (datalen != sizeof(info))
102 return got_error(GOT_ERR_PRIVSEP_LEN);
103 memcpy(&info, imsg->data, sizeof(info));
105 printf("repository \"%s\", path %s\n", info.repo_name, info.repo_path);
106 return NULL;
109 static const struct got_error *
110 show_client_info(struct imsg *imsg)
112 struct gotd_imsg_info_client info;
113 size_t datalen;
115 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
116 if (datalen != sizeof(info))
117 return got_error(GOT_ERR_PRIVSEP_LEN);
118 memcpy(&info, imsg->data, sizeof(info));
120 printf("client UID %d, GID %d, ", info.euid, info.egid);
121 if (info.session_child_pid)
122 printf("session PID %ld, ", (long)info.session_child_pid);
123 if (info.repo_child_pid)
124 printf("repo PID %ld, ", (long)info.repo_child_pid);
125 if (info.is_writing)
126 printf("writing to %s\n", info.repo_name);
127 else
128 printf("reading from %s\n", info.repo_name);
130 return NULL;
133 static const struct got_error *
134 cmd_info(int argc, char *argv[], int gotd_sock)
136 const struct got_error *err;
137 struct imsgbuf ibuf;
138 struct imsg imsg;
140 imsg_init(&ibuf, gotd_sock);
142 if (imsg_compose(&ibuf, GOTD_IMSG_INFO, 0, 0, -1, NULL, 0) == -1)
143 return got_error_from_errno("imsg_compose INFO");
145 err = gotd_imsg_flush(&ibuf);
146 while (err == NULL) {
147 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
148 if (err) {
149 if (err->code == GOT_ERR_EOF)
150 err = NULL;
151 break;
154 switch (imsg.hdr.type) {
155 case GOTD_IMSG_ERROR:
156 err = gotd_imsg_recv_error(NULL, &imsg);
157 break;
158 case GOTD_IMSG_INFO:
159 err = show_info(&imsg);
160 break;
161 case GOTD_IMSG_INFO_REPO:
162 err = show_repo_info(&imsg);
163 break;
164 case GOTD_IMSG_INFO_CLIENT:
165 err = show_client_info(&imsg);
166 break;
167 default:
168 err = got_error(GOT_ERR_PRIVSEP_MSG);
169 break;
172 imsg_free(&imsg);
175 imsg_clear(&ibuf);
176 return err;
179 __dead static void
180 usage_stop(void)
182 fprintf(stderr, "usage: %s stop\n", getprogname());
183 exit(1);
186 static const struct got_error *
187 cmd_stop(int argc, char *argv[], int gotd_sock)
189 const struct got_error *err;
190 struct imsgbuf ibuf;
191 struct imsg imsg;
193 imsg_init(&ibuf, gotd_sock);
195 if (imsg_compose(&ibuf, GOTD_IMSG_STOP, 0, 0, -1, NULL, 0) == -1)
196 return got_error_from_errno("imsg_compose STOP");
198 err = gotd_imsg_flush(&ibuf);
199 while (err == NULL) {
200 err = gotd_imsg_poll_recv(&imsg, &ibuf, 0);
201 if (err) {
202 if (err->code == GOT_ERR_EOF)
203 err = NULL;
204 break;
207 switch (imsg.hdr.type) {
208 case GOTD_IMSG_ERROR:
209 err = gotd_imsg_recv_error(NULL, &imsg);
210 break;
211 default:
212 err = got_error(GOT_ERR_PRIVSEP_MSG);
213 break;
216 imsg_free(&imsg);
219 imsg_clear(&ibuf);
220 return err;
223 static void
224 list_commands(FILE *fp)
226 size_t i;
228 fprintf(fp, "commands:");
229 for (i = 0; i < nitems(gotctl_commands); i++) {
230 const struct gotctl_cmd *cmd = &gotctl_commands[i];
231 fprintf(fp, " %s", cmd->cmd_name);
233 fputc('\n', fp);
236 __dead static void
237 usage(int hflag, int status)
239 FILE *fp = (status == 0) ? stdout : stderr;
241 fprintf(fp, "usage: %s [-hV] [-f path] command [arg ...]\n",
242 getprogname());
243 if (hflag)
244 list_commands(fp);
245 exit(status);
248 static const struct got_error *
249 apply_unveil(const char *unix_socket_path)
251 #ifdef PROFILE
252 if (unveil("gmon.out", "rwc") != 0)
253 return got_error_from_errno2("unveil", "gmon.out");
254 #endif
255 if (unveil(unix_socket_path, "w") != 0)
256 return got_error_from_errno2("unveil", unix_socket_path);
258 if (unveil(NULL, NULL) != 0)
259 return got_error_from_errno("unveil");
261 return NULL;
264 static int
265 connect_gotd(const char *socket_path)
267 const struct got_error *error = NULL;
268 int gotd_sock = -1;
269 struct sockaddr_un sun;
271 error = apply_unveil(socket_path);
272 if (error)
273 errx(1, "%s", error->msg);
275 #ifndef PROFILE
276 if (pledge("stdio unix", NULL) == -1)
277 err(1, "pledge");
278 #endif
279 if ((gotd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
280 err(1, "socket");
282 memset(&sun, 0, sizeof(sun));
283 sun.sun_family = AF_UNIX;
284 if (strlcpy(sun.sun_path, socket_path, sizeof(sun.sun_path)) >=
285 sizeof(sun.sun_path))
286 errx(1, "gotd socket path too long");
287 if (connect(gotd_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
288 err(1, "connect: %s", socket_path);
290 #ifndef PROFILE
291 if (pledge("stdio", NULL) == -1)
292 err(1, "pledge");
293 #endif
295 return gotd_sock;
299 main(int argc, char *argv[])
301 const struct gotctl_cmd *cmd;
302 int gotd_sock = -1, i;
303 int ch;
304 int hflag = 0, Vflag = 0;
305 static const struct option longopts[] = {
306 { "version", no_argument, NULL, 'V' },
307 { NULL, 0, NULL, 0 }
309 const char *socket_path = GOTD_UNIX_SOCKET;
311 setlocale(LC_CTYPE, "");
313 #ifndef PROFILE
314 if (pledge("stdio unix unveil", NULL) == -1)
315 err(1, "pledge");
316 #endif
318 while ((ch = getopt_long(argc, argv, "+hf:V", longopts, NULL)) != -1) {
319 switch (ch) {
320 case 'h':
321 hflag = 1;
322 break;
323 case 'f':
324 socket_path = optarg;
325 break;
326 case 'V':
327 Vflag = 1;
328 break;
329 default:
330 usage(hflag, 1);
331 /* NOTREACHED */
335 argc -= optind;
336 argv += optind;
337 optind = 1;
338 optreset = 1;
340 if (Vflag) {
341 got_version_print_str();
342 return 0;
345 if (argc <= 0)
346 usage(hflag, hflag ? 0 : 1);
348 for (i = 0; i < nitems(gotctl_commands); i++) {
349 const struct got_error *error;
351 cmd = &gotctl_commands[i];
353 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])) != 0)
354 continue;
356 if (hflag)
357 cmd->cmd_usage();
359 gotd_sock = connect_gotd(socket_path);
360 if (gotd_sock == -1)
361 return 1;
362 error = cmd->cmd_main(argc, argv, gotd_sock);
363 close(gotd_sock);
364 if (error) {
365 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
366 return 1;
369 return 0;
372 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
373 list_commands(stderr);
374 return 1;