aboutsummaryrefslogtreecommitdiff
path: root/git_hooks_client/git_hooks_client.c
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-02-17 06:43:39 +0800
committerRunxi Yu <me@runxiyu.org>2025-02-17 06:43:39 +0800
commitda9bfc27b1fbaf1b3557d213ea46bd8172491c90 (patch)
treea9a0105d3b7e1353407d89c7428feeae3ee14dec /git_hooks_client/git_hooks_client.c
parent*: Restructure build system (diff)
downloadforge-da9bfc27b1fbaf1b3557d213ea46bd8172491c90.tar.gz
forge-da9bfc27b1fbaf1b3557d213ea46bd8172491c90.tar.zst
forge-da9bfc27b1fbaf1b3557d213ea46bd8172491c90.zip
git_hooks_client: Ensure stdin is a pipe
Diffstat (limited to 'git_hooks_client/git_hooks_client.c')
-rw-r--r--git_hooks_client/git_hooks_client.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/git_hooks_client/git_hooks_client.c b/git_hooks_client/git_hooks_client.c
index ef6f3c8..f402c26 100644
--- a/git_hooks_client/git_hooks_client.c
+++ b/git_hooks_client/git_hooks_client.c
@@ -3,6 +3,7 @@
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
+#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
@@ -31,8 +32,28 @@ int main(void) {
return EXIT_FAILURE;
}
+ struct stat stdin_stat;
+ if (fstat(STDIN_FILENO, &stdin_stat) == -1) {
+ perror("fstat");
+ close(sock);
+ return EXIT_FAILURE;
+ }
+
+ if (!S_ISFIFO(stdin_stat.st_mode)) {
+ dprintf(STDERR_FILENO, "fatal: stdin must be a pipe\n");
+ close(sock);
+ return EXIT_FAILURE;
+ }
+
+ int pipe_size = fcntl(STDIN_FILENO, F_GETPIPE_SZ);
+ if (pipe_size == -1) {
+ perror("fcntl");
+ close(sock);
+ return EXIT_FAILURE;
+ }
+
ssize_t bytes_spliced;
- while ((bytes_spliced = splice(STDIN_FILENO, NULL, sock, NULL, 1, SPLICE_F_MORE)) > 0) {
+ while ((bytes_spliced = splice(STDIN_FILENO, NULL, sock, NULL, pipe_size, SPLICE_F_MORE)) > 0) {
}
if (bytes_spliced == -1) {