| 49 | } |
| 50 | |
| 51 | int SendArgsToRunningInstance(LPCWSTR args, std::wstring& result, std::wstring& err) |
| 52 | { |
| 53 | HANDLE hPipe; |
| 54 | |
| 55 | auto args_len = wcslen(args); |
| 56 | |
| 57 | if (args_len < 1) |
| 58 | return SEND_ARGS_STATUS_SUCCESS; |
| 59 | |
| 60 | static_assert(CMD_PIPE_MAX_ARGS_LEN_USER < CMD_PIPE_MAX_ARGS_LEN, "problem with CMD_PIPE_MAX_ARGS_LEN_USER"); |
| 61 | |
| 62 | if (args_len > CMD_PIPE_MAX_ARGS_LEN_USER) { |
| 63 | err = L"command to long. max length is " + to_wstring(CMD_PIPE_MAX_ARGS_LEN_USER) + L" characters."; |
| 64 | return SEND_ARGS_STATUS_ERROR; |
| 65 | } |
| 66 | |
| 67 | LockZeroBuffer<WCHAR> buf(static_cast<DWORD>(args_len + CMD_PIPE_VERSION_LEN + 1), false); |
| 68 | |
| 69 | if (!buf.IsLocked()) { |
| 70 | err = L"cannot lock command buffer."; |
| 71 | return SEND_ARGS_STATUS_ERROR; |
| 72 | } |
| 73 | |
| 74 | memcpy(buf.m_buf, CMD_PIPE_VERSION_STR, CMD_PIPE_VERSION_LEN * sizeof(WCHAR)); |
| 75 | |
| 76 | memcpy(buf.m_buf + CMD_PIPE_VERSION_LEN, args, (args_len + 1) * sizeof(WCHAR)); |
| 77 | |
| 78 | while (true) { |
| 79 | hPipe = CreateFile( |
| 80 | GetNamedPipeName(true), // pipe name |
| 81 | GENERIC_READ | // read and write access |
| 82 | GENERIC_WRITE, |
| 83 | 0, // no sharing |
| 84 | NULL, // default security attributes |
| 85 | OPEN_EXISTING, // opens existing pipe |
| 86 | 0, // default attributes |
| 87 | NULL); // no template file |
| 88 | |
| 89 | // break if we have a pipe |
| 90 | if (hPipe != INVALID_HANDLE_VALUE) |
| 91 | break; |
| 92 | |
| 93 | // return false if an error other than ERROR_PIPE_BUSY occurs. |
| 94 | if (GetLastError() != ERROR_PIPE_BUSY) { |
| 95 | DWORD lastErr = GetLastError(); |
| 96 | err = FormatErr(L"Unable to open pipe.", lastErr); |
| 97 | if (lastErr == ERROR_FILE_NOT_FOUND) { |
| 98 | err += L" Is cppcryptfs running?"; |
| 99 | return SEND_ARGS_STATUS_CANNOT_CONNECT; // startiing cppcryptfs or retry is worthwhile |
| 100 | } else if (lastErr == ERROR_ACCESS_DENIED) { |
| 101 | err += L" Is cppcryptfs running as administrator and you are not?"; |
| 102 | return SEND_ARGS_STATUS_ERROR; // retry not worthwhile |
| 103 | } else { |
| 104 | return SEND_ARGS_STATUS_ERROR; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // All pipe instances are busy, so wait. |
no test coverage detected