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