Skip to content

Commit 481e403

Browse files
author
manojampalam
committed
ssh-agent launched interactive will now automatically start ssh-agent service.
This is to support Linux scenarios.
1 parent f5e6d88 commit 481e403

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

contrib/win32/win32compat/ssh-agent/agent-main.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,42 @@ int main(int argc, char **argv) {
9696
load_config();
9797
if (!StartServiceCtrlDispatcherW(dispatch_table)) {
9898
if (GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
99-
if (argc == 1) {
100-
/* console app - start in debug mode*/
99+
/*todo - support debug mode*/
100+
/*
101+
if (debugmode) {
101102
SetConsoleCtrlHandler(ctrl_c_handler, TRUE);
102103
log_init("ssh-agent", 7, 1, 1);
103104
agent_start(TRUE, FALSE, 0, 0);
104105
return 0;
105106
}
106-
else {
107+
*/
108+
if (argc == 2) {
109+
/*agent process is likely a spawned child*/
107110
char* h = 0;
108111
h += atoi(*(argv + 1));
109-
log_init("ssh-agent", config_log_level(), 1, 0);
110-
agent_start(FALSE, TRUE, h, atoi(*(argv+2)));
112+
if (h != 0) {
113+
log_init("ssh-agent", config_log_level(), 1, 0);
114+
agent_start(FALSE, TRUE, h, atoi(*(argv + 2)));
115+
return 0;
116+
}
117+
}
118+
/* to support linux compat scenarios where ssh-agent.exe is typically launched per session*/
119+
/* - just start ssh-agent service if needed */
120+
{
121+
SC_HANDLE sc_handle, svc_handle;
122+
DWORD err;
123+
124+
if ((sc_handle = OpenSCManagerW(NULL, NULL, SERVICE_START)) == NULL ||
125+
(svc_handle = OpenServiceW(sc_handle, L"ssh-agent", SERVICE_START)) == NULL){
126+
fatal("unable to open service handle");
127+
return -1;
128+
}
129+
130+
if (StartService(svc_handle, 0, NULL) == FALSE && GetLastError() != ERROR_SERVICE_ALREADY_RUNNING) {
131+
fatal("unable to start ssh-agent service, error :%d", GetLastError());
132+
return -1;
133+
}
134+
111135
return 0;
112136
}
113137
}

0 commit comments

Comments
 (0)