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 <sys/queue.h>
18 #include <sys/socket.h>
32 #include "got_error.h"
33 #include "got_version.h"
35 #include "got_lib_gitproto.h"
40 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 #define GOTCTL_CMD_INFO "info"
44 #define GOTCTL_CMD_STOP "stop"
48 const struct got_error
*(*cmd_main
)(int, char *[], int);
49 void (*cmd_usage
)(void);
52 __dead
static void usage(int, int);
54 __dead
static void usage_info(void);
55 __dead
static void usage_stop(void);
57 static const struct got_error
* cmd_info(int, char *[], int);
58 static const struct got_error
* cmd_stop(int, char *[], int);
60 static const struct gotctl_cmd gotctl_commands
[] = {
61 { "info", cmd_info
, usage_info
},
62 { "stop", cmd_stop
, usage_stop
},
68 fprintf(stderr
, "usage: %s info\n", getprogname());
72 static const struct got_error
*
73 show_info(struct imsg
*imsg
)
75 struct gotd_imsg_info info
;
78 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
79 if (datalen
!= sizeof(info
))
80 return got_error(GOT_ERR_PRIVSEP_LEN
);
81 memcpy(&info
, imsg
->data
, sizeof(info
));
83 printf("gotd PID: %d\n", info
.pid
);
84 printf("verbosity: %d\n", info
.verbosity
);
85 printf("number of repositories: %d\n", info
.nrepos
);
86 printf("number of connected clients: %d\n", info
.nclients
);
90 static const struct got_error
*
91 show_repo_info(struct imsg
*imsg
)
93 struct gotd_imsg_info_repo info
;
96 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
97 if (datalen
!= sizeof(info
))
98 return got_error(GOT_ERR_PRIVSEP_LEN
);
99 memcpy(&info
, imsg
->data
, sizeof(info
));
101 printf("repository \"%s\", path %s\n", info
.repo_name
, info
.repo_path
);
105 static const struct got_error
*
106 show_client_info(struct imsg
*imsg
)
108 struct gotd_imsg_info_client info
;
111 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
112 if (datalen
!= sizeof(info
))
113 return got_error(GOT_ERR_PRIVSEP_LEN
);
114 memcpy(&info
, imsg
->data
, sizeof(info
));
116 printf("client UID %d, GID %d, ", info
.euid
, info
.egid
);
117 if (info
.session_child_pid
)
118 printf("session PID %ld, ", (long)info
.session_child_pid
);
119 if (info
.repo_child_pid
)
120 printf("repo PID %ld, ", (long)info
.repo_child_pid
);
122 printf("writing to %s\n", info
.repo_name
);
124 printf("reading from %s\n", info
.repo_name
);
129 static const struct got_error
*
130 cmd_info(int argc
, char *argv
[], int gotd_sock
)
132 const struct got_error
*err
;
136 imsg_init(&ibuf
, gotd_sock
);
138 if (imsg_compose(&ibuf
, GOTD_IMSG_INFO
, 0, 0, -1, NULL
, 0) == -1)
139 return got_error_from_errno("imsg_compose INFO");
141 err
= gotd_imsg_flush(&ibuf
);
142 while (err
== NULL
) {
143 err
= gotd_imsg_poll_recv(&imsg
, &ibuf
, 0);
145 if (err
->code
== GOT_ERR_EOF
)
150 switch (imsg
.hdr
.type
) {
151 case GOTD_IMSG_ERROR
:
152 err
= gotd_imsg_recv_error(NULL
, &imsg
);
155 err
= show_info(&imsg
);
157 case GOTD_IMSG_INFO_REPO
:
158 err
= show_repo_info(&imsg
);
160 case GOTD_IMSG_INFO_CLIENT
:
161 err
= show_client_info(&imsg
);
164 err
= got_error(GOT_ERR_PRIVSEP_MSG
);
178 fprintf(stderr
, "usage: %s stop\n", getprogname());
182 static const struct got_error
*
183 cmd_stop(int argc
, char *argv
[], int gotd_sock
)
185 const struct got_error
*err
;
189 imsg_init(&ibuf
, gotd_sock
);
191 if (imsg_compose(&ibuf
, GOTD_IMSG_STOP
, 0, 0, -1, NULL
, 0) == -1)
192 return got_error_from_errno("imsg_compose STOP");
194 err
= gotd_imsg_flush(&ibuf
);
195 while (err
== NULL
) {
196 err
= gotd_imsg_poll_recv(&imsg
, &ibuf
, 0);
198 if (err
->code
== GOT_ERR_EOF
)
203 switch (imsg
.hdr
.type
) {
204 case GOTD_IMSG_ERROR
:
205 err
= gotd_imsg_recv_error(NULL
, &imsg
);
208 err
= got_error(GOT_ERR_PRIVSEP_MSG
);
220 list_commands(FILE *fp
)
224 fprintf(fp
, "commands:");
225 for (i
= 0; i
< nitems(gotctl_commands
); i
++) {
226 const struct gotctl_cmd
*cmd
= &gotctl_commands
[i
];
227 fprintf(fp
, " %s", cmd
->cmd_name
);
233 usage(int hflag
, int status
)
235 FILE *fp
= (status
== 0) ? stdout
: stderr
;
237 fprintf(fp
, "usage: %s [-hV] [-f path] command [arg ...]\n",
244 static const struct got_error
*
245 apply_unveil(const char *unix_socket_path
)
248 if (unveil("gmon.out", "rwc") != 0)
249 return got_error_from_errno2("unveil", "gmon.out");
251 if (unveil(unix_socket_path
, "w") != 0)
252 return got_error_from_errno2("unveil", unix_socket_path
);
254 if (unveil(NULL
, NULL
) != 0)
255 return got_error_from_errno("unveil");
261 connect_gotd(const char *socket_path
)
263 const struct got_error
*error
= NULL
;
265 struct sockaddr_un sun
;
267 error
= apply_unveil(socket_path
);
269 errx(1, "%s", error
->msg
);
272 if (pledge("stdio unix", NULL
) == -1)
275 if ((gotd_sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1)
278 memset(&sun
, 0, sizeof(sun
));
279 sun
.sun_family
= AF_UNIX
;
280 if (strlcpy(sun
.sun_path
, socket_path
, sizeof(sun
.sun_path
)) >=
281 sizeof(sun
.sun_path
))
282 errx(1, "gotd socket path too long");
283 if (connect(gotd_sock
, (struct sockaddr
*)&sun
, sizeof(sun
)) == -1)
284 err(1, "connect: %s", socket_path
);
287 if (pledge("stdio", NULL
) == -1)
295 main(int argc
, char *argv
[])
297 const struct gotctl_cmd
*cmd
;
298 int gotd_sock
= -1, i
;
300 int hflag
= 0, Vflag
= 0;
301 static const struct option longopts
[] = {
302 { "version", no_argument
, NULL
, 'V' },
305 const char *socket_path
= GOTD_UNIX_SOCKET
;
307 setlocale(LC_CTYPE
, "");
310 if (pledge("stdio unix unveil", NULL
) == -1)
314 while ((ch
= getopt_long(argc
, argv
, "+hf:V", longopts
, NULL
)) != -1) {
320 socket_path
= optarg
;
337 got_version_print_str();
342 usage(hflag
, hflag
? 0 : 1);
344 for (i
= 0; i
< nitems(gotctl_commands
); i
++) {
345 const struct got_error
*error
;
347 cmd
= &gotctl_commands
[i
];
349 if (strncmp(cmd
->cmd_name
, argv
[0], strlen(argv
[0])) != 0)
355 gotd_sock
= connect_gotd(socket_path
);
358 error
= cmd
->cmd_main(argc
, argv
, gotd_sock
);
361 fprintf(stderr
, "%s: %s\n", getprogname(), error
->msg
);
368 fprintf(stderr
, "%s: unknown command '%s'\n", getprogname(), argv
[0]);
369 list_commands(stderr
);