Esempio n. 1
0
//MailSlot of flush DNS cache Monitor
bool __fastcall FlushDNSMailSlotMonitor(
	void)
{
//System security setting
	std::shared_ptr<SECURITY_ATTRIBUTES> SecurityAttributes(new SECURITY_ATTRIBUTES());
	std::shared_ptr<SECURITY_DESCRIPTOR> SecurityDescriptor(new SECURITY_DESCRIPTOR());
	std::shared_ptr<char> ACL_Buffer(new char[PACKET_MAXSIZE]());
	memset(ACL_Buffer.get(), 0, PACKET_MAXSIZE);
	PSID SID_Value = nullptr;

	InitializeSecurityDescriptor(SecurityDescriptor.get(), SECURITY_DESCRIPTOR_REVISION);
	InitializeAcl((PACL)ACL_Buffer.get(), PACKET_MAXSIZE, ACL_REVISION);
	ConvertStringSidToSidW(SID_ADMINISTRATORS_GROUP, &SID_Value);
	AddAccessAllowedAce((PACL)ACL_Buffer.get(), ACL_REVISION, GENERIC_ALL, SID_Value);
	SetSecurityDescriptorDacl(SecurityDescriptor.get(), true, (PACL)ACL_Buffer.get(), false);
	SecurityAttributes->lpSecurityDescriptor = SecurityDescriptor.get();
	SecurityAttributes->bInheritHandle = true;

//Create mailslot.
	HANDLE hSlot = CreateMailslotW(MAILSLOT_NAME, PACKET_MAXSIZE - 1U, MAILSLOT_WAIT_FOREVER, SecurityAttributes.get());
	if (hSlot == INVALID_HANDLE_VALUE)
	{
		LocalFree(SID_Value);

		PrintError(LOG_ERROR_SYSTEM, L"Create mailslot error", GetLastError(), nullptr, 0);
		return false;
	}

	ACL_Buffer.reset();
	LocalFree(SID_Value);

//Initialization
	BOOL Result = FALSE;
	bool FlushDNS = false;
	DWORD cbMessage = 0, cMessage = 0, cbRead = 0;
	std::shared_ptr<wchar_t> lpszBuffer(new wchar_t[PACKET_MAXSIZE]());
	wmemset(lpszBuffer.get(), 0, PACKET_MAXSIZE);

//MailSlot Monitor
	for (;;)
	{
		cbMessage = 0;
		cMessage = 0;

	//Get mailslot messages.
		Result = GetMailslotInfo(hSlot, nullptr, &cbMessage, &cMessage, nullptr);
		if (Result == FALSE)
		{
			PrintError(LOG_ERROR_SYSTEM, L"Mailslot Monitor initialization error", GetLastError(), nullptr, 0);
			
			CloseHandle(hSlot);
			return false;
		}

	//Wait for messages.
		if (cbMessage == MAILSLOT_NO_MESSAGE)
		{
			Sleep(LOOP_INTERVAL_TIME_MONITOR);
			continue;
		}

	//Got messages.
		FlushDNS = false;
		while (cMessage > 0)
		{
			Result = ReadFile(hSlot, lpszBuffer.get(), cbMessage, &cbRead, nullptr);
			if (Result == FALSE)
			{
				PrintError(LOG_ERROR_SYSTEM, L"MailSlot read messages error", GetLastError(), nullptr, 0);
				
				CloseHandle(hSlot);
				return false;
			}

			if (!FlushDNS && memcmp(lpszBuffer.get(), MAILSLOT_MESSAGE_FLUSH_DNS, wcslen(MAILSLOT_MESSAGE_FLUSH_DNS)) == EXIT_SUCCESS)
			{
				FlushDNS = true;
				FlushAllDNSCache();
			}
			memset(lpszBuffer.get(), 0, PACKET_MAXSIZE);

		//Get other mailslot messages.
			Result = GetMailslotInfo(hSlot, nullptr, &cbMessage, &cMessage, nullptr);
			if (Result == FALSE)
			{
				PrintError(LOG_ERROR_SYSTEM, L"Mailslot Monitor initialization error", GetLastError(), nullptr, 0);
				
				CloseHandle(hSlot);
				return false;
			}
		}

		Sleep(LOOP_INTERVAL_TIME_MONITOR);
	}

//Monitor terminated
	CloseHandle(hSlot);
	PrintError(LOG_ERROR_SYSTEM, L"MailSlot module Monitor terminated", 0, nullptr, 0);
	return false;
}
Esempio n. 2
0
/* RETURNS: -1 = error; >=0 = the shmid of the segment	*/
int
shmCreateSegment(
        dsmContext_t *pcontext,
        SHMDSC       *pshmdsc,/* control blk for a series of shm segs */
        int           idchar)  /*id for multiple segments for same db  */
{

    LPVOID  lpmem;
    HANDLE  hmap;
    TEXT    *pname = pshmdsc->pname;
    char    szShareMem[MAXUTFLEN];
    char    szFile[MAXUTFLEN];

    pshmdsc->segsize = MIN(pshmdsc->segsize, MAXSEGSIZE);
    utapath(szFile, MAXUTFLEN, pname, "");
    utmkosnm("sharemem.", szFile, szShareMem, MAXUTFLEN, idchar);

    hmap = CreateFileMapping((HANDLE)0xFFFFFFFF,  /* Use the swap file    */
			     (SECURITY_ATTRIBUTES*)SecurityAttributes(0), 
                                                  /* Not inherited        */
			     PAGE_READWRITE,      /* Memory is Read/Write */
			     0L,                  /* Size Upper 32 Bits   */
			     (DWORD)pshmdsc->segsize,/* Size Lower 32 bits*/
			     szShareMem);

    if (!hmap)
    {
    	DWORD dwRet = GetLastError();
    
        switch (dwRet) 
	{
	  case ERROR_ALREADY_EXISTS:      /* Right now this case will never */
	    /* be TRUE. hmap is not NULL   */
	    /* if the mapping already existed */
	  default:
	    /* Unable to create shared memory %s, error %d */
	    MSGN_CALLBACK(pcontext, utMSG062, szShareMem, dwRet);
	    return -1;
	}
    }

    lpmem = MapViewOfFile(hmap,
			  FILE_MAP_WRITE | FILE_MAP_READ,
			  0, 0, (DWORD)pshmdsc->segsize);

#if 0 /* Your supposed to be able to do this */
	CloseHandle(hmap);
#endif
    if (lpmem == 0)
    {
    	/* Unable to create shared memory %s, error %d */
    	MSGN_CALLBACK(pcontext, utMSG062, szShareMem, 0);
    	return -1;
    }

    if (utshmAddNamedSegment(szShareMem, (int)lpmem, (ULONG)hmap))
    {
    	/* Failed, out of memory */
    	UnmapViewOfFile(lpmem);
    	return -1;
    }
    
    return (int)lpmem;
}