feat(ssh_manager): expose ssh_manager_get_auth_sock_path helper
- SHA
b9c10a4b01be5dccb0107f025caaabd5cbd45af0- Parents
-
f814f4a - Tree
d4aa9ce
b9c10a4
b9c10a4b01be5dccb0107f025caaabd5cbd45af0f814f4a
d4aa9ce| Status | File | + | - |
|---|---|---|---|
| M |
src/ssh_manager.c
|
25 | 0 |
| M |
src/ssh_manager.h
|
9 | 0 |
src/ssh_manager.cmodified@@ -697,6 +697,31 @@ static int setup_ssh_environment(ssh_config_t *ssh_config) { | ||
| 697 | 697 | return 0; |
| 698 | 698 | } |
| 699 | 699 | |
| 700 | +/* Public: compute the stable SSH_AUTH_SOCK symlink path. | |
| 701 | + * Mirrors the selection done by create_isolated_agent_socket_dir() so shell | |
| 702 | + * integration emitted by `gitswitch init` points at the same socket the | |
| 703 | + * runtime maintains. */ | |
| 704 | +int ssh_manager_get_auth_sock_path(char *buf, size_t buf_size) { | |
| 705 | + if (!buf || buf_size == 0) { | |
| 706 | + set_error(ERR_INVALID_ARGS, "NULL/empty buffer to ssh_manager_get_auth_sock_path"); | |
| 707 | + return -1; | |
| 708 | + } | |
| 709 | + | |
| 710 | + const char *runtime_dir = getenv("XDG_RUNTIME_DIR"); | |
| 711 | + int written; | |
| 712 | + if (runtime_dir && *runtime_dir && path_exists(runtime_dir)) { | |
| 713 | + written = snprintf(buf, buf_size, "%s/gitswitch-ssh/current.sock", runtime_dir); | |
| 714 | + } else { | |
| 715 | + written = snprintf(buf, buf_size, "/tmp/gitswitch-ssh-%d/current.sock", getuid()); | |
| 716 | + } | |
| 717 | + | |
| 718 | + if (written < 0 || (size_t)written >= buf_size) { | |
| 719 | + set_error(ERR_INVALID_PATH, "SSH auth sock path too long"); | |
| 720 | + return -1; | |
| 721 | + } | |
| 722 | + return 0; | |
| 723 | +} | |
| 724 | + | |
| 700 | 725 | /* Create isolated agent socket directory */ |
| 701 | 726 | static int create_isolated_agent_socket_dir(char *socket_dir, size_t socket_dir_size) { |
| 702 | 727 | const char *runtime_dir = getenv("XDG_RUNTIME_DIR"); |
src/ssh_manager.hmodified@@ -85,4 +85,13 @@ int ssh_configure_host_alias(const account_t *account); | ||
| 85 | 85 | */ |
| 86 | 86 | int ssh_test_connection(const account_t *account, const char *host); |
| 87 | 87 | |
| 88 | +/** | |
| 89 | + * Write the stable SSH_AUTH_SOCK symlink path to buf. | |
| 90 | + * Uses $XDG_RUNTIME_DIR/gitswitch-ssh/current.sock when XDG_RUNTIME_DIR is set, | |
| 91 | + * otherwise /tmp/gitswitch-ssh-<uid>/current.sock. Shared by runtime switch | |
| 92 | + * logic and the `init` shell-integration command so both agree on the path. | |
| 93 | + * Returns 0 on success, -1 if the computed path would overflow buf. | |
| 94 | + */ | |
| 95 | +int ssh_manager_get_auth_sock_path(char *buf, size_t buf_size); | |
| 96 | + | |
| 88 | 97 | #endif /* SSH_MANAGER_H */ |