1 // Copyright (c) 2015 Ryan Prichard
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "../shared/WindowsSecurity.h"
27 #include "../shared/WinptyException.h"
29 const wchar_t *kPipeName = L"\\\\.\\pipe\\DebugServer";
31 // A message may not be larger than this size.
32 const int MSG_SIZE = 4096;
34 static void usage(const char *program, int code) {
35 printf("Usage: %s [--everyone]\n"
37 "Creates the named pipe %ls and reads messages. Prints each\n"
38 "message to stdout. By default, only the current user can send messages.\n"
39 "Pass --everyone to let anyone send a message.\n"
41 "Use the WINPTY_DEBUG environment variable to enable winpty trace output.\n"
42 "(e.g. WINPTY_DEBUG=trace for the default trace output.) Set WINPTYDBG=1\n"
43 "to enable trace with older winpty versions.\n",
48 int main(int argc, char *argv[]) {
49 bool everyone = false;
50 for (int i = 1; i < argc; ++i) {
51 std::string arg = argv[i];
52 if (arg == "--everyone") {
54 } else if (arg == "-h" || arg == "--help") {
61 SecurityDescriptor sd;
62 PSECURITY_ATTRIBUTES psa = nullptr;
63 SECURITY_ATTRIBUTES sa = {};
66 sd = createPipeSecurityDescriptorOwnerFullControlEveryoneWrite();
67 } catch (const WinptyException &e) {
69 "error creating security descriptor: %ls\n", e.what());
72 sa.nLength = sizeof(sa);
73 sa.lpSecurityDescriptor = sd.get();
77 HANDLE serverPipe = CreateNamedPipeW(
79 /*dwOpenMode=*/PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
80 /*dwPipeMode=*/PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE |
81 rejectRemoteClientsPipeFlag(),
83 /*nOutBufferSize=*/MSG_SIZE,
84 /*nInBufferSize=*/MSG_SIZE,
85 /*nDefaultTimeOut=*/10 * 1000,
88 if (serverPipe == INVALID_HANDLE_VALUE) {
89 fprintf(stderr, "error: could not create %ls pipe: error %u\n",
90 kPipeName, static_cast<unsigned>(GetLastError()));
94 char msgBuffer[MSG_SIZE + 1];
97 if (!ConnectNamedPipe(serverPipe, nullptr)) {
98 fprintf(stderr, "error: ConnectNamedPipe failed\n");
103 if (!ReadFile(serverPipe, msgBuffer, MSG_SIZE, &bytesRead, nullptr)) {
104 fprintf(stderr, "error: ReadFile on pipe failed\n");
106 DisconnectNamedPipe(serverPipe);
109 msgBuffer[bytesRead] = '\n';
110 fwrite(msgBuffer, 1, bytesRead + 1, stdout);
113 DWORD bytesWritten = 0;
114 WriteFile(serverPipe, "OK", 2, &bytesWritten, nullptr);
115 DisconnectNamedPipe(serverPipe);