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>
34 #include "got_error.h"
35 #include "got_version.h"
38 #include "got_lib_gitproto.h"
43 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 #define GOTCTL_CMD_INFO "info"
47 #define GOTCTL_CMD_STOP "stop"
51 const struct got_error
*(*cmd_main
)(int, char *[], int);
52 void (*cmd_usage
)(void);
55 __dead
static void usage(int, int);
57 __dead
static void usage_info(void);
58 __dead
static void usage_stop(void);
60 static const struct got_error
* cmd_info(int, char *[], int);
61 static const struct got_error
* cmd_stop(int, char *[], int);
63 static const struct gotctl_cmd gotctl_commands
[] = {
64 { "info", cmd_info
, usage_info
},
65 { "stop", cmd_stop
, usage_stop
},
71 fprintf(stderr
, "usage: %s info\n", getprogname());
75 static const struct got_error
*
76 show_info(struct imsg
*imsg
)
78 struct gotd_imsg_info info
;
81 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
82 if (datalen
!= sizeof(info
))
83 return got_error(GOT_ERR_PRIVSEP_LEN
);
84 memcpy(&info
, imsg
->data
, sizeof(info
));
86 printf("gotd PID: %d\n", info
.pid
);
87 printf("verbosity: %d\n", info
.verbosity
);
88 printf("number of repositories: %d\n", info
.nrepos
);
89 printf("number of connected clients: %d\n", info
.nclients
);
93 static const struct got_error
*
94 show_repo_info(struct imsg
*imsg
)
96 struct gotd_imsg_info_repo info
;
99 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
100 if (datalen
!= sizeof(info
))
101 return got_error(GOT_ERR_PRIVSEP_LEN
);
102 memcpy(&info
, imsg
->data
, sizeof(info
));
104 printf("repository \"%s\", path %s\n", info
.repo_name
, info
.repo_path
);
108 static const struct got_error
*
109 show_client_info(struct imsg
*imsg
)
111 struct gotd_imsg_info_client info
;
114 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
115 if (datalen
!= sizeof(info
))
116 return got_error(GOT_ERR_PRIVSEP_LEN
);
117 memcpy(&info
, imsg
->data
, sizeof(info
));
119 printf("client UID %d, GID %d, ", info
.euid
, info
.egid
);
120 if (info
.session_child_pid
)
121 printf("session PID %ld, ", (long)info
.session_child_pid
);
122 if (info
.repo_child_pid
)
123 printf("repo PID %ld, ", (long)info
.repo_child_pid
);
125 printf("writing to %s\n", info
.repo_name
);
127 printf("reading from %s\n", info
.repo_name
);
132 static const struct got_error
*
133 cmd_info(int argc
, char *argv
[], int gotd_sock
)
135 const struct got_error
*err
;
139 imsg_init(&ibuf
, gotd_sock
);
141 if (imsg_compose(&ibuf
, GOTD_IMSG_INFO
, 0, 0, -1, NULL
, 0) == -1)
142 return got_error_from_errno("imsg_compose INFO");
144 err
= gotd_imsg_flush(&ibuf
);
145 while (err
== NULL
) {
146 err
= gotd_imsg_poll_recv(&imsg
, &ibuf
, 0);
148 if (err
->code
== GOT_ERR_EOF
)
153 switch (imsg
.hdr
.type
) {
154 case GOTD_IMSG_ERROR
:
155 err
= gotd_imsg_recv_error(NULL
, &imsg
);
158 err
= show_info(&imsg
);
160 case GOTD_IMSG_INFO_REPO
:
161 err
= show_repo_info(&imsg
);
163 case GOTD_IMSG_INFO_CLIENT
:
164 err
= show_client_info(&imsg
);
167 err
= got_error(GOT_ERR_PRIVSEP_MSG
);
181 fprintf(stderr
, "usage: %s stop\n", getprogname());
185 static const struct got_error
*
186 cmd_stop(int argc
, char *argv
[], int gotd_sock
)
188 const struct got_error
*err
;
192 imsg_init(&ibuf
, gotd_sock
);
194 if (imsg_compose(&ibuf
, GOTD_IMSG_STOP
, 0, 0, -1, NULL
, 0) == -1)
195 return got_error_from_errno("imsg_compose STOP");
197 err
= gotd_imsg_flush(&ibuf
);
198 while (err
== NULL
) {
199 err
= gotd_imsg_poll_recv(&imsg
, &ibuf
, 0);
201 if (err
->code
== GOT_ERR_EOF
)
206 switch (imsg
.hdr
.type
) {
207 case GOTD_IMSG_ERROR
:
208 err
= gotd_imsg_recv_error(NULL
, &imsg
);
211 err
= got_error(GOT_ERR_PRIVSEP_MSG
);
223 list_commands(FILE *fp
)
227 fprintf(fp
, "commands:");
228 for (i
= 0; i
< nitems(gotctl_commands
); i
++) {
229 const struct gotctl_cmd
*cmd
= &gotctl_commands
[i
];
230 fprintf(fp
, " %s", cmd
->cmd_name
);
236 usage(int hflag
, int status
)
238 FILE *fp
= (status
== 0) ? stdout
: stderr
;
240 fprintf(fp
, "usage: %s [-hV] [-f path] command [arg ...]\n",
247 static const struct got_error
*
248 apply_unveil(const char *unix_socket_path
)
251 if (unveil("gmon.out", "rwc") != 0)
252 return got_error_from_errno2("unveil", "gmon.out");
254 if (unveil(unix_socket_path
, "w") != 0)
255 return got_error_from_errno2("unveil", unix_socket_path
);
257 if (unveil(NULL
, NULL
) != 0)
258 return got_error_from_errno("unveil");
264 connect_gotd(const char *socket_path
)
266 const struct got_error
*error
= NULL
;
268 struct sockaddr_un sun
;
270 error
= apply_unveil(socket_path
);
272 errx(1, "%s", error
->msg
);
275 if (pledge("stdio unix", NULL
) == -1)
278 if ((gotd_sock
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1)
281 memset(&sun
, 0, sizeof(sun
));
282 sun
.sun_family
= AF_UNIX
;
283 if (strlcpy(sun
.sun_path
, socket_path
, sizeof(sun
.sun_path
)) >=
284 sizeof(sun
.sun_path
))
285 errx(1, "gotd socket path too long");
286 if (connect(gotd_sock
, (struct sockaddr
*)&sun
, sizeof(sun
)) == -1)
287 err(1, "connect: %s", socket_path
);
290 if (pledge("stdio", NULL
) == -1)
298 main(int argc
, char *argv
[])
300 const struct gotctl_cmd
*cmd
;
301 int gotd_sock
= -1, i
;
303 int hflag
= 0, Vflag
= 0;
304 static const struct option longopts
[] = {
305 { "version", no_argument
, NULL
, 'V' },
308 const char *socket_path
= GOTD_UNIX_SOCKET
;
310 setlocale(LC_CTYPE
, "");
313 if (pledge("stdio unix unveil", NULL
) == -1)
317 while ((ch
= getopt_long(argc
, argv
, "+hf:V", longopts
, NULL
)) != -1) {
323 socket_path
= optarg
;
340 got_version_print_str();
345 usage(hflag
, hflag
? 0 : 1);
347 for (i
= 0; i
< nitems(gotctl_commands
); i
++) {
348 const struct got_error
*error
;
350 cmd
= &gotctl_commands
[i
];
352 if (strncmp(cmd
->cmd_name
, argv
[0], strlen(argv
[0])) != 0)
358 gotd_sock
= connect_gotd(socket_path
);
361 error
= cmd
->cmd_main(argc
, argv
, gotd_sock
);
364 fprintf(stderr
, "%s: %s\n", getprogname(), error
->msg
);
371 fprintf(stderr
, "%s: unknown command '%s'\n", getprogname(), argv
[0]);
372 list_commands(stderr
);