connectivity/com.nokia.tcf/native/TCFNative/TCFClient/ClientManager.cpp
changeset 505 6de8d9cfdda1
parent 60 9d2210c8eed2
child 914 46f36a149510
equal deleted inserted replaced
501:c39dede7ad37 505:6de8d9cfdda1
   638 	}
   638 	}
   639 }
   639 }
   640 
   640 
   641 void CClientManager::DeleteFromLockFile(DWORD serverProcessId)
   641 void CClientManager::DeleteFromLockFile(DWORD serverProcessId)
   642 {
   642 {
   643 	DWORD callingId[10];
   643 	DWORD creatorIds[10];
   644 	DWORD serverId[10];
   644 	DWORD serverIds[10];
   645 	int numIds = 0;
   645 	int numIds = 0;
   646 
   646 
   647 	DWORD ourProcessId = ::GetCurrentProcessId();
   647 	DWORD ourProcessId = ::GetCurrentProcessId();
   648 
   648 
   649 	if (m_ServerLockFile != NULL)
   649 	if (m_ServerLockFile != NULL)
   661 			if (f)
   661 			if (f)
   662 			{
   662 			{
   663 				BOOL done = FALSE;
   663 				BOOL done = FALSE;
   664 				while (!done)
   664 				while (!done)
   665 				{
   665 				{
   666 					DWORD cId = 0xffffffff;
   666 					DWORD creatorId = 0xffffffff;
   667 					DWORD sId = 0xffffffff;
   667 					DWORD serverId = 0xffffffff;
   668 					int n = fscanf(f, "%ld %ld\n", &cId, &sId);
   668 					int n = fscanf(f, "%ld %ld\n", &creatorId, &serverId);
   669 					if (n == 2)
   669 					if (n == 2)
   670 					{
   670 					{
   671 						TCDEBUGLOGA3("CClientManager::DeleteFromLockFile numIds=%d sId=%d pId=%d\n", numIds, cId, sId);
   671 						TCDEBUGLOGA3("CClientManager::DeleteFromLockFile numIds=%d creatorId=%d serverId=%d\n", numIds, creatorId, serverId);
   672 						if (cId != ourProcessId || sId != serverProcessId)
   672 						if (creatorId != ourProcessId || serverId != serverProcessId)
   673 						{
   673 						{
   674 							callingId[numIds] = cId;
   674 							creatorIds[numIds] = creatorId;
   675 							serverId[numIds] = sId;
   675 							serverIds[numIds] = serverId;
   676 							numIds++;
   676 							numIds++;
   677 							if (numIds > 9)
   677 							if (numIds > 9)
   678 								done = TRUE;
   678 								done = TRUE;
   679 						}
   679 						}
   680 					}
   680 					}
   693 				f = fopen(m_ServerLockFile, "wt");
   693 				f = fopen(m_ServerLockFile, "wt");
   694 				if (f)
   694 				if (f)
   695 				{
   695 				{
   696 					for (int i = 0; i < numIds; i++)
   696 					for (int i = 0; i < numIds; i++)
   697 					{
   697 					{
   698 						fprintf(f, "%ld %ld\n", callingId[i], serverId[i]);
   698 						fprintf(f, "%ld %ld\n", creatorIds[i], serverIds[i]);
   699 					}
   699 					}
   700 					fclose(f);
   700 					fclose(f);
   701 				}
   701 				}
   702 			}
   702 			}
   703 		}
   703 		}
   706 
   706 
   707 // Currently assumes there is only ONE TCFServer, but multiple client processes (that use that server)
   707 // Currently assumes there is only ONE TCFServer, but multiple client processes (that use that server)
   708 // we should not have more than a few Carbide processes connecting to the same TCFServer
   708 // we should not have more than a few Carbide processes connecting to the same TCFServer
   709 void CClientManager::TerminateServerThroughLockFile(pServerProcessData pData)
   709 void CClientManager::TerminateServerThroughLockFile(pServerProcessData pData)
   710 {
   710 {
   711 	DWORD callingId[10];
   711 	DWORD creatorIds[10];
   712 	DWORD serverId[10];
   712 	DWORD serverIds[10];
   713 	BOOL liveCaller[10];
   713 	BOOL liveCaller[10];
   714 	int numIds = 0;
   714 	int numIds = 0;
   715 	if (m_ServerLockFile != NULL)
   715 	if (m_ServerLockFile != NULL)
   716 	{
   716 	{
   717 		DWORD attr = ::GetFileAttributes(m_ServerLockFile);
   717 		DWORD attr = ::GetFileAttributes(m_ServerLockFile);
   727 			if (f)
   727 			if (f)
   728 			{
   728 			{
   729 				BOOL done = FALSE;
   729 				BOOL done = FALSE;
   730 				while (!done)
   730 				while (!done)
   731 				{
   731 				{
   732 					DWORD cId = 0xffffffff;
   732 					DWORD creatorId = 0xffffffff;
   733 					DWORD sId = 0xffffffff;
   733 					DWORD serverId = 0xffffffff;
   734 					int n = fscanf(f, "%ld %ld\n", &cId, &sId);
   734 					int n = fscanf(f, "%ld %ld\n", &creatorId, &serverId);
   735 					if (n == 2)
   735 					if (n == 2)
   736 					{
   736 					{
   737 						TCDEBUGLOGA3("CClientManager::TerminateServerThroughLockFile n=%d sId=%d pId=%d\n", n, cId, sId);
   737 						TCDEBUGLOGA3("CClientManager::TerminateServerThroughLockFile n=%d creatorId=%d serverId=%d\n", n, creatorId, serverId);
   738 						callingId[numIds] = cId;
   738 						creatorIds[numIds] = creatorId;
   739 						serverId[numIds] = sId;
   739 						serverIds[numIds] = serverId;
   740 						numIds++;
   740 						numIds++;
   741 						if (numIds > 9)
   741 						if (numIds > 9)
   742 							done = TRUE;
   742 							done = TRUE;
   743 					}
   743 					}
   744 					else
   744 					else
   749 				fclose(f);
   749 				fclose(f);
   750 
   750 
   751 				int numDeadCallers = 0;
   751 				int numDeadCallers = 0;
   752 				for (int i = 0; i < numIds; i++)
   752 				for (int i = 0; i < numIds; i++)
   753 				{
   753 				{
   754 					HANDLE h = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, callingId[i]);
   754 					HANDLE h = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, creatorIds[i]);
   755 					if (h)
   755 					if (h)
   756 					{
   756 					{
   757 						// calling process is still alive
   757 						// calling process is still alive
   758 						liveCaller[i] = TRUE;
   758 						liveCaller[i] = TRUE;
       
   759 						DWORD exitCode = -5;
       
   760 						BOOL exitCall = ::GetExitCodeProcess(h, &exitCode);
       
   761 						DWORD id = ::GetCurrentProcessId();
   759 						::CloseHandle(h);
   762 						::CloseHandle(h);
   760 						TCDEBUGLOGA1("CClientManager::TerminateServerThroughLockFile %d alive\n", callingId[i]);
   763 						TCDEBUGLOGA3("CClientManager::TerminateServerThroughLockFile %d alive exitCall=%d currentId=%d\n", creatorIds[i], exitCall, id);
       
   764 						if (exitCall == TRUE && exitCode != STILL_ACTIVE)
       
   765 						{
       
   766 							liveCaller[i] = FALSE;
       
   767 							numDeadCallers++;
       
   768 						}
       
   769 						{
       
   770 							TCDEBUGLOGA2("CClientManager::TerminateServerThroughLockFile exitCode=%d still_active=%d\n", exitCode, STILL_ACTIVE);
       
   771 						}
   761 					}
   772 					}
   762 					else
   773 					else
   763 					{
   774 					{
   764 						liveCaller[i] = FALSE;
   775 						liveCaller[i] = FALSE;
   765 						numDeadCallers++;
   776 						numDeadCallers++;
   766 						DWORD err = ::GetLastError();
   777 						DWORD err = ::GetLastError();
   767 						TCDEBUGLOGA3("CClientManager::TerminateServerThroughLockFile %d dead err=%d:%s\n", callingId[i], err, GetErrorText(err));
   778 						TCDEBUGLOGA3("CClientManager::TerminateServerThroughLockFile %d dead err=%d:%s\n", creatorIds[i], err, GetErrorText(err));
   768 					}
   779 					}
   769 				}
   780 				}
   770 				if (numDeadCallers == numIds)
   781 				if (numDeadCallers == numIds)
   771 				{
   782 				{
   772 					// terminate the TCFServer, and delete lock file
   783 					// terminate the TCFServer, and delete lock file
   773 					pData->numRefs = 0;
   784 					pData->numRefs = 0;
   774 					::remove(m_ServerLockFile);
   785 					::remove(m_ServerLockFile);
   775 					HANDLE h = ::OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, serverId[0]);
   786 					HANDLE h = ::OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, serverIds[0]);
   776 					if (h)
   787 					if (h)
   777 					{
   788 					{
   778 						BOOL ret = ::TerminateProcess(h, -1);
   789 						BOOL ret = ::TerminateProcess(h, -1);
   779 						if (ret == 0)
   790 						if (ret == 0)
   780 						{
   791 						{
   793 					{
   804 					{
   794 						for (int i = 0; i < numIds; i++)
   805 						for (int i = 0; i < numIds; i++)
   795 						{
   806 						{
   796 							if (liveCaller[i])
   807 							if (liveCaller[i])
   797 							{
   808 							{
   798 								fprintf(f, "%ld %ld\n", callingId[i], serverId[i]);
   809 								fprintf(f, "%ld %ld\n", creatorIds[i], serverIds[i]);
   799 							}
   810 							}
   800 						}
   811 						}
   801 						fclose(f);
   812 						fclose(f);
   802 					}
   813 					}
   803 					pData->numRefs -= numDeadCallers;
   814 					pData->numRefs -= numDeadCallers;