From 54125fb6438e492e2bc1cf4b1c49f4ac94138ed6 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Mon, 17 Feb 2025 00:40:15 +0800 Subject: [PATCH] git_hooks{.go,_client}: Add stub for git hook clients --- git_hooks.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ git_hooks_client/.gitignore | 1 + git_hooks_client/Makefile | 1 + git_hooks_client/git_hooks_client.c | 43 +++++++++++++++++++++++++++++++++++++++++++ diff --git a/git_hooks.go b/git_hooks.go new file mode 100644 index 0000000000000000000000000000000000000000..86300cb2e35754a540c794d8330a496222659a1b --- /dev/null +++ b/git_hooks.go @@ -0,0 +1,45 @@ +package main + +import ( + "errors" + "fmt" + "net" + "os" + "syscall" +) + +var err_not_unixconn = errors.New("Not a unix connection") + +func hooks_handle_connection(conn net.Conn) (err error) { + defer conn.Close() + + unix_conn, ok := conn.(*net.UnixConn) + if !ok { + return err_not_unixconn + } + + fd, err := unix_conn.File() + if err != nil { + return err + } + defer fd.Close() + + ucred, err := get_ucred(fd) + if err != nil { + return err + } + + pid := ucred.Pid + + _ = pid + + return nil +} + +func get_ucred(fd *os.File) (*syscall.Ucred, error) { + ucred, err := syscall.GetsockoptUcred(int(fd.Fd()), syscall.SOL_SOCKET, syscall.SO_PEERCRED) + if err != nil { + return nil, fmt.Errorf("failed to get credentials: %v", err) + } + return ucred, nil +} diff --git a/git_hooks_client/.gitignore b/git_hooks_client/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..129c0b4e8c659fe8c91dc7863b6d9eec4688bbe6 --- /dev/null +++ b/git_hooks_client/.gitignore @@ -0,0 +1 @@ +/git_hooks_client diff --git a/git_hooks_client/Makefile b/git_hooks_client/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..0dd283e80f87d1aa98bd8dd7c0646f52f2d8e028 --- /dev/null +++ b/git_hooks_client/Makefile @@ -0,0 +1 @@ +git_hooks_client: diff --git a/git_hooks_client/git_hooks_client.c b/git_hooks_client/git_hooks_client.c new file mode 100644 index 0000000000000000000000000000000000000000..b274e0340d5baaa01d70f093578a385ad374e22f --- /dev/null +++ b/git_hooks_client/git_hooks_client.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include + +int main() { + int sock; + struct sockaddr_un addr; + const char *message = "hi"; + const char *socket_path = getenv("LINDENII_FORGE_HOOKS_SOCKET_PATH"); + + if (socket_path == NULL) { + exit(EXIT_FAILURE); + } + + sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock == -1) { + perror("socket"); + exit(EXIT_FAILURE); + } + + memset(&addr, 0, sizeof(struct sockaddr_un)); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); + + if (connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) { + perror("connect"); + close(sock); + exit(EXIT_FAILURE); + } + + if (send(sock, message, strlen(message), 0) == -1) { + perror("send"); + close(sock); + exit(EXIT_FAILURE); + } + + close(sock); + + return 0; +} -- 2.48.1