portable: release 0.105
[got-portable.git] / gotsh / gotsh.c
bloba663ee9a015fdd3d41974ce6ea958626d40bb8c4
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 <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_serve.h"
35 #include "got_path.h"
36 #include "got_compat.h"
38 #include "got_lib_dial.h"
40 #include "gotd.h"
42 static int chattygot;
44 __dead static void
45 usage(void)
47 fprintf(stderr, "usage: %s -c '%s|%s repository-path'\n",
48 getprogname(), GOT_DIAL_CMD_SEND, GOT_DIAL_CMD_FETCH);
49 exit(1);
52 static const struct got_error *
53 apply_unveil(const char *unix_socket_path)
55 #ifdef PROFILE
56 if (unveil("gmon.out", "rwc") != 0)
57 return got_error_from_errno2("unveil", "gmon.out");
58 #endif
59 if (unveil(unix_socket_path, "w") != 0)
60 return got_error_from_errno2("unveil", unix_socket_path);
62 if (unveil(NULL, NULL) != 0)
63 return got_error_from_errno("unveil");
65 return NULL;
68 int
69 main(int argc, char *argv[])
71 const struct got_error *error;
72 const char *unix_socket_path;
73 int gotd_sock = -1;
74 struct sockaddr_un sun;
75 char *gitcmd = NULL, *command = NULL, *repo_path = NULL;
77 #ifndef PROFILE
78 if (pledge("stdio recvfd unix unveil", NULL) == -1)
79 err(1, "pledge");
80 #endif
82 unix_socket_path = getenv("GOTD_UNIX_SOCKET");
83 if (unix_socket_path == NULL)
84 unix_socket_path = GOTD_UNIX_SOCKET;
86 error = apply_unveil(unix_socket_path);
87 if (error)
88 goto done;
90 if (strcmp(argv[0], GOT_DIAL_CMD_SEND) == 0 ||
91 strcmp(argv[0], GOT_DIAL_CMD_FETCH) == 0) {
92 if (argc != 2)
93 usage();
94 if (asprintf(&gitcmd, "%s %s", argv[0], argv[1]) == -1)
95 err(1, "asprintf");
96 error = got_dial_parse_command(&command, &repo_path, gitcmd);
97 } else {
98 if (argc != 3 || strcmp(argv[1], "-c") != 0)
99 usage();
100 error = got_dial_parse_command(&command, &repo_path, argv[2]);
102 if (error && error->code == GOT_ERR_BAD_PACKET)
103 usage();
104 if (error)
105 goto done;
107 if ((gotd_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
108 err(1, "socket");
110 memset(&sun, 0, sizeof(sun));
111 sun.sun_family = AF_UNIX;
112 if (strlcpy(sun.sun_path, unix_socket_path,
113 sizeof(sun.sun_path)) >= sizeof(sun.sun_path))
114 errx(1, "gotd socket path too long");
115 if (connect(gotd_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
116 err(1, "connect: %s", unix_socket_path);
118 #ifndef PROFILE
119 if (pledge("stdio recvfd", NULL) == -1)
120 err(1, "pledge");
121 #endif
122 error = got_serve(STDIN_FILENO, STDOUT_FILENO, command, repo_path,
123 gotd_sock, chattygot);
124 done:
125 free(gitcmd);
126 free(command);
127 free(repo_path);
128 if (gotd_sock != -1)
129 close(gotd_sock);
130 if (error) {
131 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
132 return 1;
135 return 0;