示例#1
1
bool Channel::CreatePipe(const IPC::ChannelHandle &channel_handle) {
  assert(INVALID_HANDLE_VALUE == pipe_);
  std::wstring pipe_name;
  // If we already have a valid pipe for channel just copy it.
  if (channel_handle.pipe.handle) {
    assert(channel_handle.name.empty());
    pipe_name = L"Not Available";  // Just used for LOG
    // Check that the given pipe confirms to the specified mode.  We can
    // only check for PIPE_TYPE_MESSAGE & PIPE_SERVER_END flags since the
    // other flags (PIPE_TYPE_BYTE, and PIPE_CLIENT_END) are defined as 0.
    DWORD flags = 0;
    GetNamedPipeInfo(channel_handle.pipe.handle, &flags, NULL, NULL, NULL);
    assert(!(flags & PIPE_TYPE_MESSAGE));
    if (!DuplicateHandle(GetCurrentProcess(),
                         channel_handle.pipe.handle,
                         GetCurrentProcess(),
                         &pipe_,
                         0,
                         FALSE,
                         DUPLICATE_SAME_ACCESS)) {
      //LOG(WARNING) << "DuplicateHandle failed. Error :" << GetLastError();
      return false;
    }
  } else {
	assert(!channel_handle.pipe.handle);
	pipe_name = PipeName(channel_handle.name, &client_secret_);

	//Ïȳ¢ÊÔ´´½¨
    const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED |
                            FILE_FLAG_FIRST_PIPE_INSTANCE;
    validate_client_ = !!client_secret_;
    pipe_ = CreateNamedPipeW(pipe_name.c_str(),
                             open_mode,
                             PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
                             1,
                             kReadBufferSize,
                             kReadBufferSize,
                             5000,
                             NULL);
	if (pipe_ == INVALID_HANDLE_VALUE)
	{
		pipe_ = CreateFileW(pipe_name.c_str(),
			GENERIC_READ | GENERIC_WRITE,
			0,
			NULL,
			OPEN_EXISTING,
			SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION |
			FILE_FLAG_OVERLAPPED,
			NULL);

		waiting_connect_ = false;
	}
  } 

  if (pipe_ == INVALID_HANDLE_VALUE) {
    // If this process is being closed, the pipe may be gone already.
    //LOG(WARNING) << "Unable to create pipe \"" << pipe_name <<
    //                "\" in " << (mode & MODE_SERVER_FLAG ? "server" : "client")
    //                << " mode. Error :" << GetLastError();
    return false;
  }

  // Create the Hello message to be sent when Connect is called
  Message* m = new Message(MSG_ROUTING_NONE,
                                    HELLO_MESSAGE_TYPE,
                                    IPC::Message::PRIORITY_NORMAL);
  m->AddRef();
  // Don't send the secret to the untrusted process, and don't send a secret
  // if the value is zero (for IPC backwards compatability).
  int32 secret = validate_client_ ? 0 : client_secret_;
  if (!m->WriteInt(GetCurrentProcessId()) ||
      (secret && !m->WriteUInt32(secret))) {
    CloseHandle(pipe_);
    pipe_ = INVALID_HANDLE_VALUE;
	m->Release();
    return false;
  }

  output_queue_.push(m);
  return true;
}
示例#2
0
// static
bool Channel::IsNamedServerInitialized(
	const std::string& channel_id) {
	if (WaitNamedPipe(PipeName(channel_id, NULL).c_str(), 1))
		return true;
	// If ERROR_SEM_TIMEOUT occurred, the pipe exists but is handling another
	// connection.
	return GetLastError() == ERROR_SEM_TIMEOUT;
}
示例#3
0
UINT PollIPCMessage(LPVOID param)
{
	HANDLE hPipe = CreateNamedPipe(PipeName(PIPENAME_IPC64).GetName(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1, 512, 512, 0, NULL);
	if (hPipe == INVALID_HANDLE_VALUE) {
#ifdef DEBUG_IPC
		CUtils::Log(_T("PollIPCMessage: CreateNamedPipe failed. (%d)"), GetLastError());
#endif
		return 1;
	}
	for (; ;) {
		if (ConnectNamedPipe(hPipe, NULL) ? FALSE : (GetLastError() != ERROR_PIPE_CONNECTED)) {
#ifdef DEBUG_IPC
			CUtils::Log(_T("PollIPCMessage: ConnectNamedPipe failed. (%d)"), GetLastError());
#endif
			break;
		}
		DWORD msg;
		DWORD read;
		if (!ReadFile(hPipe, &msg, sizeof(msg), &read, NULL) || read != sizeof(msg)) {
#ifdef DEBUG_IPC
			CUtils::Log(_T("PollIPCMessage: ReadFile failed. (%d)"), GetLastError());
#endif
			break;
		}
		switch (msg)
		{
		case IPC64_EXIT:
			goto exit;
			break;
		case IPC64_RELOAD:
			if (CXkeymacsDll::LoadConfig())
				CXkeymacsDll::ResetHooks();
			break;
		case IPC64_DISABLE:
			CXkeymacsDll::SetHookStateDirect(false);
			break;
		case IPC64_ENABLE:
			CXkeymacsDll::SetHookStateDirect(true);
			break;
		}
		SendAck(hPipe);
	}
exit:
	CloseHandle(hPipe);
	CXkeymacsDll::ReleaseHooks();
	reinterpret_cast<CMainFrame *>(param)->SendMessage(WM_CLOSE);
	return 0;
}
示例#4
0
void CProfile::SetDllData()
{
	memcpy(m_Config.FuncDefs, FuncDefs::GetDefs(), sizeof(m_Config.FuncDefs));
	for (int nAppID = 0; nAppID < MAX_APP; ++nAppID) {
		AppConfig& appConfig = m_Config.AppConfig[nAppID];
		appConfig.CmdID[CONTROL]['X'] = 0; // C-x is unassigned.
		for (int nType = 0; nType < MAX_COMMAND_TYPE; ++nType)
			for (int nKey = 0; nKey < MAX_KEY; ++nKey)
				if ((nType & CONTROLX) && (appConfig.CmdID[nType][nKey] || appConfig.FuncID[nType][nKey] >= 0))
					appConfig.CmdID[CONTROL]['X'] = 1; // C-x is available.
	}
	m_Config.Is106Keyboard = Is106Keyboard();
	_tcscpy_s(m_Config.PipeNameForIPC32, PipeName(PIPENAME_IPC32).GetName());
	CXkeymacsDll::SetConfig(m_Config);
	if (!CXkeymacsApp::IsWow64())
		return;
	if (!CXkeymacsDll::SaveConfig())
		return;
	CXkeymacsApp::SendIPC64Message(IPC64_RELOAD);
}