X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=windows%2Fwinnpc.c;h=85a3c3ffc3b6e37b252ba0c7f1d92cd41d33fa7d;hb=21101c7397e460933635a7bfed813864fc4f88fe;hp=9b347dd3c22120bbd40bb2a64d6b59de19d2451f;hpb=1b3edafcff9ddcd915a5725df2ac33cdc2a16b96;p=PuTTY.git diff --git a/windows/winnpc.c b/windows/winnpc.c index 9b347dd3..85a3c3ff 100644 --- a/windows/winnpc.c +++ b/windows/winnpc.c @@ -14,10 +14,10 @@ #if !defined NO_SECURITY -#include +#include "winsecur.h" -Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, Plug plug, - int overlapped); +Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H, + Plug plug, int overlapped); Socket new_named_pipe_client(const char *pipename, Plug plug) { @@ -27,21 +27,39 @@ Socket new_named_pipe_client(const char *pipename, Plug plug) char *err; Socket ret; - extern int advapi_initialised; - init_advapi(); /* for get_user_sid. FIXME: do better. */ - assert(strncmp(pipename, "\\\\.\\pipe\\", 9) == 0); assert(strchr(pipename + 9, '\\') == NULL); - pipehandle = CreateFile(pipename, GENERIC_READ | GENERIC_WRITE, 0, NULL, - OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); - - if (pipehandle == INVALID_HANDLE_VALUE) { - err = dupprintf("Unable to open named pipe '%s': %s", - pipename, win_strerror(GetLastError())); - ret = new_error_socket(err, plug); - sfree(err); - return ret; + while (1) { + pipehandle = CreateFile(pipename, GENERIC_READ | GENERIC_WRITE, + 0, NULL, OPEN_EXISTING, + FILE_FLAG_OVERLAPPED, NULL); + + if (pipehandle != INVALID_HANDLE_VALUE) + break; + + if (GetLastError() != ERROR_PIPE_BUSY) { + err = dupprintf("Unable to open named pipe '%s': %s", + pipename, win_strerror(GetLastError())); + ret = new_error_socket(err, plug); + sfree(err); + return ret; + } + + /* + * If we got ERROR_PIPE_BUSY, wait for the server to + * create a new pipe instance. (Since the server is + * expected to be winnps.c, which will do that immediately + * after a previous connection is accepted, that shouldn't + * take excessively long.) + */ + if (!WaitNamedPipe(pipename, NMPWAIT_USE_DEFAULT_WAIT)) { + err = dupprintf("Error waiting for named pipe '%s': %s", + pipename, win_strerror(GetLastError())); + ret = new_error_socket(err, plug); + sfree(err); + return ret; + } } if ((usersid = get_user_sid()) == NULL) { @@ -52,10 +70,10 @@ Socket new_named_pipe_client(const char *pipename, Plug plug) return ret; } - if (GetSecurityInfo(pipehandle, SE_KERNEL_OBJECT, - OWNER_SECURITY_INFORMATION, - &pipeowner, NULL, NULL, NULL, - &psd) != ERROR_SUCCESS) { + if (p_GetSecurityInfo(pipehandle, SE_KERNEL_OBJECT, + OWNER_SECURITY_INFORMATION, + &pipeowner, NULL, NULL, NULL, + &psd) != ERROR_SUCCESS) { err = dupprintf("Unable to get named pipe security information: %s", win_strerror(GetLastError())); ret = new_error_socket(err, plug); @@ -78,7 +96,7 @@ Socket new_named_pipe_client(const char *pipename, Plug plug) LocalFree(psd); sfree(usersid); - return make_handle_socket(pipehandle, pipehandle, plug, TRUE); + return make_handle_socket(pipehandle, pipehandle, NULL, plug, TRUE); } #endif /* !defined NO_SECURITY */