1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Windows: factor out mutex lock/unlock from sharing.c.

This will let me reuse them easily in Pageant setup.
This commit is contained in:
Simon Tatham 2022-01-03 11:40:22 +00:00
parent d0b609c68a
commit a2de0a8b7d
4 changed files with 58 additions and 36 deletions

View File

@ -13,6 +13,7 @@ add_sources_from_current_dir(utils
utils/getdlgitemtext_alloc.c utils/getdlgitemtext_alloc.c
utils/get_system_dir.c utils/get_system_dir.c
utils/get_username.c utils/get_username.c
utils/interprocess_mutex.c
utils/is_console_handle.c utils/is_console_handle.c
utils/load_system32_dll.c utils/load_system32_dll.c
utils/ltime.c utils/ltime.c

View File

@ -740,4 +740,7 @@ void plug_closing_winsock_error(Plug *plug, DWORD error);
SeatPromptResult make_spr_sw_abort_winerror(const char *prefix, DWORD error); SeatPromptResult make_spr_sw_abort_winerror(const char *prefix, DWORD error);
HANDLE lock_interprocess_mutex(const char *mutexname, char **error);
void unlock_interprocess_mutex(HANDLE mutex);
#endif /* PUTTY_WINDOWS_PLATFORM_H */ #endif /* PUTTY_WINDOWS_PLATFORM_H */

View File

@ -36,8 +36,6 @@ int platform_ssh_share(const char *pi_name, Conf *conf,
char *name, *mutexname, *pipename; char *name, *mutexname, *pipename;
HANDLE mutex; HANDLE mutex;
Socket *retsock; Socket *retsock;
PSECURITY_DESCRIPTOR psd;
PACL acl;
/* /*
* Transform the platform-independent version of the connection * Transform the platform-independent version of the connection
@ -57,39 +55,12 @@ int platform_ssh_share(const char *pi_name, Conf *conf,
* Make a mutex name out of the connection identifier, and lock it * Make a mutex name out of the connection identifier, and lock it
* while we decide whether to be upstream or downstream. * while we decide whether to be upstream or downstream.
*/ */
{ mutexname = make_name(CONNSHARE_MUTEX_PREFIX, name);
SECURITY_ATTRIBUTES sa; mutex = lock_interprocess_mutex(mutexname, logtext);
if (!mutex) {
mutexname = make_name(CONNSHARE_MUTEX_PREFIX, name);
if (!make_private_security_descriptor(MUTEX_ALL_ACCESS,
&psd, &acl, logtext)) {
sfree(mutexname);
sfree(name);
return SHARE_NONE;
}
memset(&sa, 0, sizeof(sa));
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = psd;
sa.bInheritHandle = false;
mutex = CreateMutex(&sa, false, mutexname);
if (!mutex) {
*logtext = dupprintf("CreateMutex(\"%s\") failed: %s",
mutexname, win_strerror(GetLastError()));
sfree(mutexname);
sfree(name);
LocalFree(psd);
LocalFree(acl);
return SHARE_NONE;
}
sfree(mutexname); sfree(mutexname);
LocalFree(psd); sfree(name);
LocalFree(acl); return SHARE_NONE;
WaitForSingleObject(mutex, INFINITE);
} }
pipename = make_name(CONNSHARE_PIPE_PREFIX, name); pipename = make_name(CONNSHARE_PIPE_PREFIX, name);
@ -103,8 +74,7 @@ int platform_ssh_share(const char *pi_name, Conf *conf,
*logtext = pipename; *logtext = pipename;
*sock = retsock; *sock = retsock;
sfree(name); sfree(name);
ReleaseMutex(mutex); unlock_interprocess_mutex(mutex);
CloseHandle(mutex);
return SHARE_DOWNSTREAM; return SHARE_DOWNSTREAM;
} }
sfree(*ds_err); sfree(*ds_err);

View File

@ -0,0 +1,48 @@
/*
* Lock and unlock a mutex with a globally visible name. Used to
* synchronise between unrelated processes, such as two
* connection-sharing PuTTYs deciding which will be the upstream.
*/
#include "putty.h"
#include "security-api.h"
HANDLE lock_interprocess_mutex(const char *mutexname, char **error)
{
PSECURITY_DESCRIPTOR psd = NULL;
PACL acl = NULL;
HANDLE mutex = NULL;
if (!make_private_security_descriptor(MUTEX_ALL_ACCESS,
&psd, &acl, error))
goto out;
SECURITY_ATTRIBUTES sa;
memset(&sa, 0, sizeof(sa));
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = psd;
sa.bInheritHandle = false;
mutex = CreateMutex(&sa, false, mutexname);
if (!mutex) {
*error = dupprintf("CreateMutex(\"%s\") failed: %s",
mutexname, win_strerror(GetLastError()));
goto out;
}
WaitForSingleObject(mutex, INFINITE);
out:
if (psd)
LocalFree(psd);
if (acl)
LocalFree(acl);
return mutex;
}
void unlock_interprocess_mutex(HANDLE mutex)
{
ReleaseMutex(mutex);
CloseHandle(mutex);
}