tenseleyflow/gitswitch / 3559e36

Browse files

fix: remove stale ssh agent sockets before starting new agent

Previously, if gitswitch crashed or the system rebooted, stale socket
files would remain and prevent new ssh-agent instances from starting.
Now checks for and removes stale sockets before starting a new agent.
Authored by espadonne
SHA
3559e360642e7f89b6b371f4f40ec51c5a5d1646
Parents
0a84b97
Tree
d22b5fb

1 changed file

StatusFile+-
M src/ssh_manager.c 25 8
src/ssh_manager.cmodified
@@ -207,29 +207,46 @@ int ssh_start_isolated_agent(ssh_config_t *ssh_config, const account_t *account)
207207
     char command[512];
208208
     char output[1024];
209209
     char socket_dir[MAX_PATH_LEN];
210
-    
210
+    char socket_path[MAX_PATH_LEN];
211
+
211212
     if (!ssh_config || !account) {
212213
         set_error(ERR_INVALID_ARGS, "Invalid arguments to ssh_start_isolated_agent");
213214
         return -1;
214215
     }
215
-    
216
+
216217
     log_info("Starting isolated SSH agent for account: %s", account->name);
217
-    
218
+
218219
     /* Stop any existing agent we own */
219220
     if (ssh_config->agent_owned && ssh_config->agent_pid > 0) {
220221
         log_debug("Stopping existing SSH agent");
221222
         ssh_stop_agent(ssh_config);
222223
     }
223
-    
224
+
224225
     /* Create secure socket directory */
225226
     if (create_isolated_agent_socket_dir(socket_dir, sizeof(socket_dir)) != 0) {
226227
         return -1;
227228
     }
228
-    
229
+
230
+    /* Build socket path and check for stale sockets */
231
+    if ((size_t)snprintf(socket_path, sizeof(socket_path),
232
+                        "%s/ssh-agent.%s.sock",
233
+                        socket_dir, account->name) >= sizeof(socket_path)) {
234
+        set_error(ERR_INVALID_ARGS, "SSH socket path too long");
235
+        return -1;
236
+    }
237
+
238
+    /* Remove stale socket if it exists */
239
+    if (path_exists(socket_path)) {
240
+        log_debug("Removing stale SSH agent socket: %s", socket_path);
241
+        if (unlink(socket_path) != 0) {
242
+            set_system_error(ERR_FILE_IO, "Failed to remove stale SSH socket");
243
+            return -1;
244
+        }
245
+    }
246
+
229247
     /* Build ssh-agent command with socket path */
230
-    if ((size_t)snprintf(command, sizeof(command), 
231
-                        "ssh-agent -a '%s/ssh-agent.%s.sock'", 
232
-                        socket_dir, account->name) >= sizeof(command)) {
248
+    if ((size_t)snprintf(command, sizeof(command),
249
+                        "ssh-agent -a '%s'", socket_path) >= sizeof(command)) {
233250
         set_error(ERR_INVALID_ARGS, "SSH agent command too long");
234251
         return -1;
235252
     }