#include "remoting/host/curtain_mode.h"
#include <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h>
#include <Security/Security.h>
#include <unistd.h>
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/memory/ptr_util.h"
#include "base/task/single_thread_task_runner.h"
#include "remoting/host/client_session_control.h"
#include "remoting/protocol/errors.h"
namespace remoting {
namespace {
const char* kCGSessionPath =
"/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/"
"CGSession";
const char* kCGSessionAltPath = "/usr/local/sbin/CGSession";
const UInt32 kMaxDisplaysToQuery = 32;
const CGDirectDisplayID kVirtualDisplayID = 0x76697274;
bool IsRunningHeadless() {
CGDirectDisplayID online_displays[kMaxDisplaysToQuery];
UInt32 online_display_count = 0;
CGError return_code = CGGetOnlineDisplayList(
kMaxDisplaysToQuery, online_displays, &online_display_count);
if (return_code != kCGErrorSuccess) {
LOG(ERROR) << "CGGetOnlineDisplayList() failed: " << return_code;
return true;
}
for (UInt32 i = 0; i < online_display_count; i++) {
if (CGDisplayModelNumber(online_displays[i]) != kVirtualDisplayID) {
return false;
}
}
return true;
}
class SessionWatcher : public base::RefCountedThreadSafe<SessionWatcher> {
public:
SessionWatcher(scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
base::WeakPtr<ClientSessionControl> client_session_control);
SessionWatcher(const SessionWatcher&) = delete;
SessionWatcher& operator=(const SessionWatcher&) = delete;
void Start();
void Stop();
private:
friend class base::RefCountedThreadSafe<SessionWatcher>;
virtual ~SessionWatcher();
void ActivateCurtain();
bool InstallEventHandler();
void RemoveEventHandler();
void DisconnectSession(protocol::ErrorCode error);
static OSStatus SessionActivateHandler(EventHandlerCallRef handler,
EventRef event,
void* user_data);
scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
base::WeakPtr<ClientSessionControl> client_session_control_;
EventHandlerRef event_handler_;
};
SessionWatcher::SessionWatcher(
scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
base::WeakPtr<ClientSessionControl> client_session_control)
: caller_task_runner_(caller_task_runner),
ui_task_runner_(ui_task_runner),
client_session_control_(client_session_control),
event_handler_(nullptr) {}
void SessionWatcher::Start() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
ui_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&SessionWatcher::ActivateCurtain, this));
}
void SessionWatcher::Stop() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
client_session_control_.reset();
ui_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&SessionWatcher::RemoveEventHandler, this));
}
SessionWatcher::~SessionWatcher() {
DCHECK(!event_handler_);
}
void SessionWatcher::ActivateCurtain() {
if (!InstallEventHandler()) {
LOG(ERROR) << "Failed to install the switch-in handler.";
DisconnectSession(protocol::ErrorCode::HOST_CONFIGURATION_ERROR);
return;
}
base::ScopedCFTypeRef<CFDictionaryRef> session(
CGSessionCopyCurrentDictionary());
CHECK(session != nullptr)
<< "Error activating curtain-mode: "
<< "CGSessionCopyCurrentDictionary() returned NULL. "
<< "Logging out and back in should resolve this error.";
const void* on_console =
CFDictionaryGetValue(session, kCGSessionOnConsoleKey);
const void* logged_in = CFDictionaryGetValue(session, kCGSessionLoginDoneKey);
if (logged_in == kCFBooleanTrue && on_console == kCFBooleanTrue) {
bool is_headless = IsRunningHeadless();
const char* cgsession_path = NULL;
if (access(kCGSessionPath, X_OK) == 0) {
cgsession_path = kCGSessionPath;
} else if (access(kCGSessionAltPath, X_OK) == 0) {
cgsession_path = kCGSessionAltPath;
} else {
LOG(ERROR) << "Can't find CGSession - unable to enter curtain mode.";
DisconnectSession(protocol::ErrorCode::HOST_CONFIGURATION_ERROR);
return;
}
pid_t child = fork();
if (child == 0) {
execl(cgsession_path, cgsession_path, "-suspend", nullptr);
_exit(1);
} else if (child > 0) {
int status = 0;
waitpid(child, &status, 0);
if (status != 0) {
LOG(ERROR) << kCGSessionPath << " failed.";
DisconnectSession(protocol::ErrorCode::HOST_CONFIGURATION_ERROR);
return;
}
if (is_headless) {
LOG(ERROR) << "Machine is running in headless mode (no monitors "
<< "attached), we attempted to curtain the session but "
<< "CGSession is likely to fail in this mode.";
DisconnectSession(protocol::ErrorCode::HOST_CONFIGURATION_ERROR);
return;
}
} else {
LOG(ERROR) << "fork() failed.";
DisconnectSession(protocol::ErrorCode::HOST_CONFIGURATION_ERROR);
return;
}
}
}
bool SessionWatcher::InstallEventHandler() {
DCHECK(ui_task_runner_->BelongsToCurrentThread());
DCHECK(!event_handler_);
EventTypeSpec event;
event.eventClass = kEventClassSystem;
event.eventKind = kEventSystemUserSessionActivated;
OSStatus result = ::InstallApplicationEventHandler(
NewEventHandlerUPP(SessionActivateHandler), 1, &event, this,
&event_handler_);
if (result != noErr) {
event_handler_ = nullptr;
DisconnectSession(protocol::ErrorCode::HOST_CONFIGURATION_ERROR);
return false;
}
return true;
}
void SessionWatcher::RemoveEventHandler() {
DCHECK(ui_task_runner_->BelongsToCurrentThread());
if (event_handler_) {
::RemoveEventHandler(event_handler_);
event_handler_ = nullptr;
}
}
void SessionWatcher::DisconnectSession(protocol::ErrorCode error) {
if (!caller_task_runner_->BelongsToCurrentThread()) {
caller_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&SessionWatcher::DisconnectSession, this, error));
return;
}
if (client_session_control_) {
client_session_control_->DisconnectSession(error);
}
}
OSStatus SessionWatcher::SessionActivateHandler(EventHandlerCallRef handler,
EventRef event,
void* user_data) {
static_cast<SessionWatcher*>(user_data)->DisconnectSession(
protocol::ErrorCode::OK);
return noErr;
}
}
class CurtainModeMac : public CurtainMode {
public:
CurtainModeMac(scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
base::WeakPtr<ClientSessionControl> client_session_control);
CurtainModeMac(const CurtainModeMac&) = delete;
CurtainModeMac& operator=(const CurtainModeMac&) = delete;
~CurtainModeMac() override;
bool Activate() override;
private:
scoped_refptr<SessionWatcher> session_watcher_;
};
CurtainModeMac::CurtainModeMac(
scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
base::WeakPtr<ClientSessionControl> client_session_control)
: session_watcher_(new SessionWatcher(caller_task_runner,
ui_task_runner,
client_session_control)) {}
CurtainModeMac::~CurtainModeMac() {
session_watcher_->Stop();
}
bool CurtainModeMac::Activate() {
session_watcher_->Start();
return true;
}
std::unique_ptr<CurtainMode> CurtainMode::Create(
scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
base::WeakPtr<ClientSessionControl> client_session_control) {
return base::WrapUnique(new CurtainModeMac(caller_task_runner, ui_task_runner,
client_session_control));
}
}