Added comment to while loop
This commit is contained in:
parent
dfb888d3ef
commit
ae55708d00
@ -53,12 +53,12 @@ namespace sscs.common
|
||||
}
|
||||
~SessionManager()
|
||||
{
|
||||
if (tJanitor != null)
|
||||
{
|
||||
tJanitor.Abort();
|
||||
tJanitor.Join();
|
||||
}
|
||||
mutex.Close();
|
||||
if (tJanitor != null)
|
||||
{
|
||||
tJanitor.Abort();
|
||||
tJanitor.Join();
|
||||
}
|
||||
mutex.Close();
|
||||
}
|
||||
internal static SessionManager GetSessionManager
|
||||
{
|
||||
@ -77,52 +77,52 @@ namespace sscs.common
|
||||
{
|
||||
ss = GetUserSecretStore(userId);
|
||||
ss.IncrRefCount();
|
||||
ss.CreateTime = DateTime.Now;
|
||||
ss.CreateTime = DateTime.Now;
|
||||
return ss;
|
||||
}
|
||||
catch(UserNotInSessionException e)
|
||||
{
|
||||
// Would create either windows/unix user
|
||||
// depending on the platform.
|
||||
// depending on the platform.
|
||||
User user = User.CreateUser(userId);
|
||||
mutex.WaitOne();
|
||||
sessionTable.Add(userId,user);
|
||||
mutex.ReleaseMutex();
|
||||
ss = user.GetSecretStore();
|
||||
ss.IncrRefCount();
|
||||
ss.CreateTime = DateTime.Now;
|
||||
ss.CreateTime = DateTime.Now;
|
||||
return ss;
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool RemoveUserSession(UserIdentifier userId, bool destroySession)
|
||||
{
|
||||
CSSSLogger.ExecutionTrace(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
||||
try
|
||||
{
|
||||
mutex.WaitOne();
|
||||
SecretStore ss = GetUserSecretStore(userId);
|
||||
ss.DecrRefCount();
|
||||
|
||||
// We must keep the cache alive, and destroy it on
|
||||
// a logout event
|
||||
|
||||
//if( 0 == ss.refCount )
|
||||
if (destroySession)
|
||||
CSSSLogger.ExecutionTrace(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
||||
try
|
||||
{
|
||||
CSSSLogger.DbgLog("Removing the user session of " + userId.GetUID());
|
||||
ss.CommitStore();
|
||||
sessionTable.Remove(userId);
|
||||
}
|
||||
mutex.WaitOne();
|
||||
SecretStore ss = GetUserSecretStore(userId);
|
||||
ss.DecrRefCount();
|
||||
|
||||
mutex.ReleaseMutex();
|
||||
return true;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
}
|
||||
return false;
|
||||
// We must keep the cache alive, and destroy it on
|
||||
// a logout event
|
||||
|
||||
//if( 0 == ss.refCount )
|
||||
if (destroySession)
|
||||
{
|
||||
CSSSLogger.DbgLog("Removing the user session of " + userId.GetUID());
|
||||
ss.CommitStore();
|
||||
sessionTable.Remove(userId);
|
||||
}
|
||||
|
||||
mutex.ReleaseMutex();
|
||||
return true;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
internal static bool CheckIfUserSessionExists(UserIdentifier userId)
|
||||
@ -172,23 +172,23 @@ namespace sscs.common
|
||||
}
|
||||
}
|
||||
|
||||
internal static DateTime GetSessionCreateTime(UserIdentifier userId)
|
||||
{
|
||||
CSSSLogger.ExecutionTrace(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
||||
mutex.WaitOne();
|
||||
if( sessionTable.ContainsKey(userId) )
|
||||
{
|
||||
User user = (User)sessionTable[userId];
|
||||
SecretStore ss = user.GetSecretStore();
|
||||
mutex.ReleaseMutex();
|
||||
return ss.CreateTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
mutex.ReleaseMutex();
|
||||
throw new UserNotInSessionException(userId);
|
||||
}
|
||||
}
|
||||
internal static DateTime GetSessionCreateTime(UserIdentifier userId)
|
||||
{
|
||||
CSSSLogger.ExecutionTrace(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
||||
mutex.WaitOne();
|
||||
if( sessionTable.ContainsKey(userId) )
|
||||
{
|
||||
User user = (User)sessionTable[userId];
|
||||
SecretStore ss = user.GetSecretStore();
|
||||
mutex.ReleaseMutex();
|
||||
return ss.CreateTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
mutex.ReleaseMutex();
|
||||
throw new UserNotInSessionException(userId);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void ListActiveUserSessions()
|
||||
{
|
||||
@ -200,175 +200,178 @@ namespace sscs.common
|
||||
while(etor.MoveNext())
|
||||
{
|
||||
i++;
|
||||
/*
|
||||
CSSSLogger.DbgLog("Listing Active User Sessions");
|
||||
Console.WriteLine(etor.Key);
|
||||
Console.WriteLine((((SecretStore)(etor.Value)).secretStoreName + ":" + ((SecretStore)(etor.Value)).refCount);
|
||||
*/
|
||||
/*
|
||||
CSSSLogger.DbgLog("Listing Active User Sessions");
|
||||
Console.WriteLine(etor.Key);
|
||||
Console.WriteLine((((SecretStore)(etor.Value)).secretStoreName + ":" + ((SecretStore)(etor.Value)).refCount);
|
||||
*/
|
||||
}
|
||||
mutex.ReleaseMutex();
|
||||
}
|
||||
mutex.ReleaseMutex();
|
||||
}
|
||||
|
||||
|
||||
private static void CleanUpSessionsThread()
|
||||
{
|
||||
CSSSLogger.ExecutionTrace(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// enumerate users in the session
|
||||
IEnumerator etor;
|
||||
ICollection keys = sessionTable.Keys;
|
||||
if (keys != null)
|
||||
{
|
||||
etor = keys.GetEnumerator();
|
||||
while(etor.MoveNext())
|
||||
{
|
||||
UserIdentifier userIdentifier = (UserIdentifier)etor.Current;
|
||||
CSSSLogger.ExecutionTrace(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// enumerate users in the session
|
||||
IEnumerator etor;
|
||||
ICollection keys = sessionTable.Keys;
|
||||
if (keys != null)
|
||||
{
|
||||
etor = keys.GetEnumerator();
|
||||
while(etor.MoveNext())
|
||||
{
|
||||
UserIdentifier userIdentifier = (UserIdentifier)etor.Current;
|
||||
|
||||
// check if this user still has
|
||||
// processes running
|
||||
if(CheckAndDestroySession(userIdentifier,false))
|
||||
{
|
||||
/* If at least 1 session was removed,
|
||||
* the etor must be
|
||||
* re-initiated, else
|
||||
* Invalidoperationexception will be
|
||||
* thrown.
|
||||
*/
|
||||
keys = sessionTable.Keys;
|
||||
if( null == keys )
|
||||
break;
|
||||
else
|
||||
{
|
||||
etor = keys.GetEnumerator();
|
||||
}
|
||||
}
|
||||
}//while etor.MoveNext ends here.
|
||||
}
|
||||
Thread.Sleep(JANITOR_SLEEP_TIME);
|
||||
} //while true ends here.
|
||||
}
|
||||
catch(ThreadAbortException e)
|
||||
{
|
||||
CSSSLogger.DbgLog("Janitor thread is going down.");
|
||||
}
|
||||
// check if this user still has
|
||||
// processes running
|
||||
if(CheckAndDestroySession(userIdentifier,false))
|
||||
{
|
||||
/* If at least 1 session was removed,
|
||||
* the etor must be
|
||||
* re-initiated, else
|
||||
* Invalidoperationexception will be
|
||||
* thrown.
|
||||
*/
|
||||
keys = sessionTable.Keys;
|
||||
if( null == keys )
|
||||
break;
|
||||
else
|
||||
{
|
||||
etor = keys.GetEnumerator();
|
||||
}
|
||||
}
|
||||
}//while etor.MoveNext ends here.
|
||||
}
|
||||
Thread.Sleep(JANITOR_SLEEP_TIME);
|
||||
} //while true ends here.
|
||||
}
|
||||
catch(ThreadAbortException e)
|
||||
{
|
||||
CSSSLogger.DbgLog("Janitor thread is going down.");
|
||||
}
|
||||
|
||||
}//Method ends here.
|
||||
}//Method ends here.
|
||||
|
||||
/* As the pam module does a seteuid(), when is ps is
|
||||
* execed it would appear as if the user owns the process.
|
||||
* Hence, if this method is called from CloseSecretStore
|
||||
* verb ( that would have been initiated from the pam
|
||||
* module with ssFlags = 1), then if number of processes
|
||||
* is one, then delete the session.
|
||||
*/
|
||||
/* As the pam module does a seteuid(), when is ps is
|
||||
* execed it would appear as if the user owns the process.
|
||||
* Hence, if this method is called from CloseSecretStore
|
||||
* verb ( that would have been initiated from the pam
|
||||
* module with ssFlags = 1), then if number of processes
|
||||
* is one, then delete the session.
|
||||
*/
|
||||
|
||||
internal static bool CheckAndDestroySession(UserIdentifier userID, bool calledFromClose)
|
||||
{
|
||||
CSSSLogger.ExecutionTrace(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
||||
int iUID = userID.GetUID();
|
||||
bool retVal = false;
|
||||
Process myProcess = null;
|
||||
StreamReader myStreamReader = null;
|
||||
if (iUID != -1)
|
||||
{
|
||||
// make the 'ps h U UID' call
|
||||
try
|
||||
CSSSLogger.ExecutionTrace(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
||||
int iUID = userID.GetUID();
|
||||
bool retVal = false;
|
||||
Process myProcess = null;
|
||||
StreamReader myStreamReader = null;
|
||||
if (iUID != -1)
|
||||
{
|
||||
myProcess = new Process();
|
||||
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("ps" );
|
||||
// make the 'ps h U UID' call
|
||||
try
|
||||
{
|
||||
myProcess = new Process();
|
||||
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("ps" );
|
||||
|
||||
myProcessStartInfo.Arguments = "h U " + iUID.ToString();
|
||||
myProcessStartInfo.UseShellExecute = false;
|
||||
myProcessStartInfo.RedirectStandardOutput = true;
|
||||
myProcess.StartInfo = myProcessStartInfo;
|
||||
myProcess.Start();
|
||||
myProcess.WaitForExit();
|
||||
myProcessStartInfo.Arguments = "h U " + iUID.ToString();
|
||||
myProcessStartInfo.UseShellExecute = false;
|
||||
myProcessStartInfo.RedirectStandardOutput = true;
|
||||
myProcess.StartInfo = myProcessStartInfo;
|
||||
myProcess.Start();
|
||||
myProcess.WaitForExit();
|
||||
|
||||
myStreamReader = myProcess.StandardOutput;
|
||||
myStreamReader = myProcess.StandardOutput;
|
||||
|
||||
// Read the standard output of the spawned process.
|
||||
string myString = myStreamReader.ReadLine();
|
||||
int numProcs = 0;
|
||||
while( myString != null)
|
||||
{
|
||||
if(numProcs > 1)
|
||||
break;
|
||||
numProcs++;
|
||||
// Read the standard output of the spawned process.
|
||||
string myString = myStreamReader.ReadLine();
|
||||
int numProcs = 0;
|
||||
|
||||
myString = myStreamReader.ReadLine();
|
||||
}
|
||||
// determine if user has more than 1 process still running
|
||||
while( myString != null)
|
||||
{
|
||||
if(numProcs > 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
numProcs++;
|
||||
myString = myStreamReader.ReadLine();
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
/* If this has been called from
|
||||
* CloseSecretStore verb,
|
||||
* verb, the session must be deleted.
|
||||
*/
|
||||
if( calledFromClose )
|
||||
{
|
||||
RemoveUserSession(userID, true);
|
||||
retVal = true;
|
||||
break;
|
||||
}
|
||||
/* If the session was created during login,
|
||||
* and the janitor thread starts processing
|
||||
* before user login is completed, we need
|
||||
* maintain the user session (say for 5 mts).
|
||||
*/
|
||||
if( (numProcs == 0) && (CheckIfLoginTimeSession(userID)) )
|
||||
{
|
||||
retVal = false;
|
||||
break;
|
||||
}
|
||||
do
|
||||
{
|
||||
/* If this has been called from
|
||||
* CloseSecretStore verb,
|
||||
* verb, the session must be deleted.
|
||||
*/
|
||||
if( calledFromClose )
|
||||
{
|
||||
RemoveUserSession(userID, true);
|
||||
retVal = true;
|
||||
break;
|
||||
}
|
||||
/* If the session was created during login,
|
||||
* and the janitor thread starts processing
|
||||
* before user login is completed, we need
|
||||
* maintain the user session (say for 5 mts).
|
||||
*/
|
||||
if( (numProcs == 0) && (CheckIfLoginTimeSession(userID)) )
|
||||
{
|
||||
retVal = false;
|
||||
break;
|
||||
}
|
||||
|
||||
/* If the user does not own any processes and
|
||||
* if this method has not been called from
|
||||
* CloseSecretStore verb, it implies that a user
|
||||
* background process, which existed during user
|
||||
* logout has died now.
|
||||
* So, clean the user session.
|
||||
*/
|
||||
/* If the user does not own any processes and
|
||||
* if this method has not been called from
|
||||
* CloseSecretStore verb, it implies that a user
|
||||
* background process, which existed during user
|
||||
* logout has died now.
|
||||
* So, clean the user session.
|
||||
*/
|
||||
|
||||
if ( (numProcs == 0) && (!calledFromClose) )
|
||||
{
|
||||
RemoveUserSession(userID, true);
|
||||
retVal = true;
|
||||
break;
|
||||
}
|
||||
}while(false);
|
||||
/*
|
||||
myProcess.Close();
|
||||
myStreamReader.Close();
|
||||
*/
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
CSSSLogger.DbgLog(e.ToString());
|
||||
}
|
||||
finally
|
||||
{
|
||||
if( myProcess != null )
|
||||
myProcess.Close();
|
||||
if( myStreamReader != null )
|
||||
myStreamReader.Close();
|
||||
if ( (numProcs == 0) && (!calledFromClose) )
|
||||
{
|
||||
RemoveUserSession(userID, true);
|
||||
retVal = true;
|
||||
break;
|
||||
}
|
||||
}while(false);
|
||||
/*
|
||||
myProcess.Close();
|
||||
myStreamReader.Close();
|
||||
*/
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
CSSSLogger.DbgLog(e.ToString());
|
||||
}
|
||||
finally
|
||||
{
|
||||
if( myProcess != null )
|
||||
myProcess.Close();
|
||||
if( myStreamReader != null )
|
||||
myStreamReader.Close();
|
||||
|
||||
}
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
internal static bool CheckIfLoginTimeSession(UserIdentifier userId)
|
||||
{
|
||||
if( ((TimeSpan)(DateTime.Now - GetSessionCreateTime(userId))).TotalMinutes < 3 )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
internal static bool CheckIfLoginTimeSession(UserIdentifier userId)
|
||||
{
|
||||
if( ((TimeSpan)(DateTime.Now - GetSessionCreateTime(userId))).TotalMinutes < 3 )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user