installed pty
[VSoRC/.git] / node_modules / node-pty / deps / winpty / src / shared / BackgroundDesktop.cc
1 // Copyright (c) 2011-2016 Ryan Prichard
2 //
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:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
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
19 // IN THE SOFTWARE.
20
21 #include "BackgroundDesktop.h"
22
23 #include <memory>
24
25 #include "DebugClient.h"
26 #include "StringUtil.h"
27 #include "WinptyException.h"
28
29 namespace {
30
31 static std::wstring getObjectName(HANDLE object) {
32     BOOL success;
33     DWORD lengthNeeded = 0;
34     GetUserObjectInformationW(object, UOI_NAME,
35                               nullptr, 0,
36                               &lengthNeeded);
37     ASSERT(lengthNeeded % sizeof(wchar_t) == 0);
38     std::unique_ptr<wchar_t[]> tmp(
39         new wchar_t[lengthNeeded / sizeof(wchar_t)]);
40     success = GetUserObjectInformationW(object, UOI_NAME,
41                                         tmp.get(), lengthNeeded,
42                                         nullptr);
43     if (!success) {
44         throwWindowsError(L"GetUserObjectInformationW failed");
45     }
46     return std::wstring(tmp.get());
47 }
48
49 static std::wstring getDesktopName(HWINSTA winsta, HDESK desk) {
50     return getObjectName(winsta) + L"\\" + getObjectName(desk);
51 }
52
53 } // anonymous namespace
54
55 // Get a non-interactive window station for the agent.
56 // TODO: review security w.r.t. windowstation and desktop.
57 BackgroundDesktop::BackgroundDesktop() {
58     try {
59         m_originalStation = GetProcessWindowStation();
60         if (m_originalStation == nullptr) {
61             throwWindowsError(
62                 L"BackgroundDesktop ctor: "
63                 L"GetProcessWindowStation returned NULL");
64         }
65         m_newStation =
66             CreateWindowStationW(nullptr, 0, WINSTA_ALL_ACCESS, nullptr);
67         if (m_newStation == nullptr) {
68             throwWindowsError(
69                 L"BackgroundDesktop ctor: CreateWindowStationW returned NULL");
70         }
71         if (!SetProcessWindowStation(m_newStation)) {
72             throwWindowsError(
73                 L"BackgroundDesktop ctor: SetProcessWindowStation failed");
74         }
75         m_newDesktop = CreateDesktopW(
76             L"Default", nullptr, nullptr, 0, GENERIC_ALL, nullptr);
77         if (m_newDesktop == nullptr) {
78             throwWindowsError(
79                 L"BackgroundDesktop ctor: CreateDesktopW failed");
80         }
81         m_newDesktopName = getDesktopName(m_newStation, m_newDesktop);
82         TRACE("Created background desktop: %s",
83             utf8FromWide(m_newDesktopName).c_str());
84     } catch (...) {
85         dispose();
86         throw;
87     }
88 }
89
90 void BackgroundDesktop::dispose() WINPTY_NOEXCEPT {
91     if (m_originalStation != nullptr) {
92         SetProcessWindowStation(m_originalStation);
93         m_originalStation = nullptr;
94     }
95     if (m_newDesktop != nullptr) {
96         CloseDesktop(m_newDesktop);
97         m_newDesktop = nullptr;
98     }
99     if (m_newStation != nullptr) {
100         CloseWindowStation(m_newStation);
101         m_newStation = nullptr;
102     }
103 }
104
105 std::wstring getCurrentDesktopName() {
106     // MSDN says that the handles returned by GetProcessWindowStation and
107     // GetThreadDesktop do not need to be passed to CloseWindowStation and
108     // CloseDesktop, respectively.
109     const HWINSTA winsta = GetProcessWindowStation();
110     if (winsta == nullptr) {
111         throwWindowsError(
112             L"getCurrentDesktopName: "
113             L"GetProcessWindowStation returned NULL");
114     }
115     const HDESK desk = GetThreadDesktop(GetCurrentThreadId());
116     if (desk == nullptr) {
117         throwWindowsError(
118             L"getCurrentDesktopName: "
119             L"GetThreadDesktop returned NULL");
120     }
121     return getDesktopName(winsta, desk);
122 }