public GlobalReaderWriterLock(string nameOfThisLock)
        {
            _hatName = nameOfThisLock;

            // we need to make two native sync objects, a mutex and a semaphore
            // that are global so every process and user on the system can share them as long as they agree on the lock's name
            string mutexId = string.Format("Global\\Mut_{0}", _hatName);
            string semaphoreId = string.Format("Global\\Sem_{0}", _hatName);
            bool iDontCareWhetherItsNew;
            {
                MutexAccessRule allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
                MutexSecurity securitySettings = new MutexSecurity();
                securitySettings.AddAccessRule(allowEveryoneRule);

                _holdTheHat = new Mutex(false, mutexId, out iDontCareWhetherItsNew, securitySettings);
            }
            {
                SemaphoreAccessRule allowEveryoneRule = new SemaphoreAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), SemaphoreRights.FullControl, AccessControlType.Allow);
                SemaphoreSecurity securitySettings = new SemaphoreSecurity();
                securitySettings.AddAccessRule(allowEveryoneRule);

                _chipsInTheHat = new Semaphore(READER_CHIPS, READER_CHIPS, semaphoreId, out iDontCareWhetherItsNew, securitySettings);
            }
        }
Beispiel #2
0
 public void ResetAccessRule(SemaphoreAccessRule rule)
 {
     base.ResetAccessRule(rule);
 }
Beispiel #3
0
 public void AddAccessRule(SemaphoreAccessRule rule)
 {
     base.AddAccessRule(rule);
 }
Beispiel #4
0
 public void SetAccessRule(SemaphoreAccessRule rule)
 {
     SetAccessRule((AccessRule)rule);
 }
Beispiel #5
0
 public void RemoveAccessRuleSpecific(SemaphoreAccessRule rule)
 {
     RemoveAccessRuleSpecific((AccessRule)rule);
 }
Beispiel #6
0
 public bool RemoveAccessRule(SemaphoreAccessRule rule)
 {
     return(RemoveAccessRule((AccessRule)rule));
 }
Beispiel #7
0
 public bool RemoveAccessRule(SemaphoreAccessRule rule)
 {
     throw new NotImplementedException();
 }
 public void ResetAccessRule(SemaphoreAccessRule rule)
 {
     base.ResetAccessRule(rule);
 }
 public void AddAccessRule(SemaphoreAccessRule rule)
 {
 }
Beispiel #10
0
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        /// <returns></returns>
        public static bool Initialize()
        {
            bool bResult = false;

            if (CurrentElement == null)
            {
                // lock
                lock (typeof(CompanyWebSiteSemaphore))
                {
                    if (CurrentElement == null)
                    {
                        int maxPortalsCount = License.PortalsCount;

                        Semaphore semaphore = null;

                        if (maxPortalsCount > 0)
                        {
                            // Step 1. Try open Semaphore

                            try
                            {
                                semaphore = Semaphore.OpenExisting(Uid);
                            }
                            catch (WaitHandleCannotBeOpenedException)
                            {
                                // The named semaphore does not exist.
                                SemaphoreSecurity security = new SemaphoreSecurity();

                                // OZ 2009-03-10/Fix: System.Security.Principal.IdentityNotMappedException
                                //SemaphoreAccessRule accessRule = new SemaphoreAccessRule("Everyone", SemaphoreRights.TakeOwnership | SemaphoreRights.FullControl, AccessControlType.Allow);
                                SecurityIdentifier everyoneSid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
                                SemaphoreAccessRule accessRule = new SemaphoreAccessRule(everyoneSid, SemaphoreRights.TakeOwnership | SemaphoreRights.FullControl, AccessControlType.Allow);

                                security.AddAccessRule(accessRule);

                                bool createdNew;
                                semaphore = new Semaphore(maxPortalsCount, maxPortalsCount, Uid, out createdNew, security);
                            }

                            // Wait one with timeout
                            if (!semaphore.WaitOne(3000, false))
                            {
                                throw new LicenseRestrictionException("License.PortalsCount");
                            }

                            CurrentElement = new CompanyWebSiteSemaphore(semaphore);
                        }
                        else if (maxPortalsCount==-1)
                        {
                            // Unlimited
                            CurrentElement = new CompanyWebSiteSemaphore(null);
                        }
                        else
                            throw new LicenseRestrictionException("Wrong License.PortalsCount = " + maxPortalsCount.ToString());

                        bResult = true;
                    }
                }
            }

            return bResult;
        }
		public void RemoveAccessRuleSpecific (SemaphoreAccessRule rule)
		{
			throw new NotImplementedException ();
		}
		public bool RemoveAccessRule (SemaphoreAccessRule rule)
		{
			throw new NotImplementedException ();
		}
		public void AddAccessRule (SemaphoreAccessRule rule)
		{
			throw new NotImplementedException ();
		}
Beispiel #14
0
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                if (args[0].ToLower().Equals("start"))
                {

                    ProcessStartInfo startInfo = new ProcessStartInfo("startup\\startuptask.exe");
                    startInfo.CreateNoWindow = true;
                    try
                    {
                        Process exeProcess = Process.Start(startInfo);
                    }
                    catch 
                    {
                       
                    }
                    return;
                }
            }
            else
            {
                const string semaphoreName = "SemaphoreShutDown";
                Semaphore sem = null;
                bool doesNotExist = false;
                // Attempt to open the named semaphore.
                try
                {
                    sem = Semaphore.OpenExisting(semaphoreName);
                }
                catch (WaitHandleCannotBeOpenedException)
                {
                    doesNotExist = true;
                }

                if (doesNotExist)
                {
                    // The semaphore does not exist, so create it.
                    //
                    // The value of this variable is set by the semaphore
                    // constructor. It is true if the named system semaphore was
                    // created, and false if the named semaphore already existed.
                    //
                    bool semaphoreWasCreated;


                    string user = "******";
                    SemaphoreSecurity semSec = new SemaphoreSecurity();

                    SemaphoreAccessRule rule = new SemaphoreAccessRule(user,
                        SemaphoreRights.Synchronize | SemaphoreRights.Modify,
                        AccessControlType.Allow);
                    semSec.AddAccessRule(rule);

                    // Create a Semaphore object 
                    sem = new Semaphore(1, 1, semaphoreName,
                        out semaphoreWasCreated, semSec);
                    if (!semaphoreWasCreated)
                        return;
                }

                // Enter the semaphore, and hold it until the program
                // exits.
                //
                try
                {
                    sem.WaitOne();
                    sem.WaitOne();
                    ProcessStartInfo startInfo = new ProcessStartInfo("NET");
                    startInfo.Arguments = "STOP W3SVC";
                    startInfo.CreateNoWindow = false;
                    try
                    {
                        // Start the process with the info we specified.
                        // Call WaitForExit and then the using statement will close.
                        using (Process exeProcess = Process.Start(startInfo))
                        {
                            exeProcess.WaitForExit();
                            int exitCode = exeProcess.ExitCode;
                        }
                    }
                    catch 
                    {
                    }
                    sem.Release();
                }
                catch 
                {
                }
            }
        }
Beispiel #15
0
 public void RemoveAccessRuleSpecific(SemaphoreAccessRule rule)
 {
     throw new NotImplementedException();
 }
Beispiel #16
0
 public void RemoveAccessRuleAll(SemaphoreAccessRule rule)
 {
     base.RemoveAccessRuleAll(rule);
 }
 public void AddAccessRule(SemaphoreAccessRule rule)
 {
     base.AddAccessRule(rule);
 }
 public bool RemoveAccessRule(SemaphoreAccessRule rule)
 {
     return(default(bool));
 }
 public void RemoveAccessRuleAll(SemaphoreAccessRule rule)
 {
     base.RemoveAccessRuleAll(rule);
 }
 public void SetAccessRule(SemaphoreAccessRule rule)
 {
 }
Beispiel #21
0
 public void AddAccessRule(SemaphoreAccessRule rule)
 {
     AddAccessRule((AccessRule)rule);
 }
 public void AddAccessRule(SemaphoreAccessRule rule)
 {
 }
Beispiel #23
0
 public void RemoveAccessRuleAll(SemaphoreAccessRule rule)
 {
     RemoveAccessRuleAll((AccessRule)rule);
 }
 public bool RemoveAccessRule(SemaphoreAccessRule rule)
 {
   return default(bool);
 }
Beispiel #25
0
 public void ResetAccessRule(SemaphoreAccessRule rule)
 {
     ResetAccessRule((AccessRule)rule);
 }
 public void SetAccessRule(SemaphoreAccessRule rule)
 {
 }
Beispiel #27
0
        public static Semaphore GetNamedSemaphore(string name, int maximumCount = 10, int initialCount = -1)
        {
            if (string.IsNullOrWhiteSpace(name))
                throw new ArgumentNullException("name", "Argument cannot be empty, null or white space.");

            Semaphore namedSemaphore = null;
            bool doesNotExist = false;
            bool unauthorized = false;

            if (initialCount < 0)
                initialCount = maximumCount;

            // Create a semaphore name that is specific to an object (e.g., a path and file name).
            string semaphoreName = Cipher.GetPasswordHash(name.ToLower(), SemaphoreHash).Replace('\\', '-');

            // Attempt to open the named semaphore
            try
            {
                namedSemaphore = Semaphore.OpenExisting(semaphoreName);
            }
            catch (WaitHandleCannotBeOpenedException)
            {
                doesNotExist = true;
            }
            catch (UnauthorizedAccessException)
            {
                unauthorized = true;
            }

            // Mono Semaphore implementations do not include ability to change access rules
#if MONO
            // If semaphore does not exist we create it
            if (doesNotExist || unauthorized)
            {
                try
                {
                    bool semaphoreWasCreated;

                    namedSemaphore = new Semaphore(initialCount, maximumCount, semaphoreName, out semaphoreWasCreated);

                    if (!semaphoreWasCreated)
                        throw new InvalidOperationException("Failed to create semaphore.");
                }
                catch (UnauthorizedAccessException ex)
                {
                    throw new InvalidOperationException("Failed to create semaphore: " + ex.Message);
                }
            }
#else
            // If semaphore does not exist we create it
            if (doesNotExist)
            {
                try
                {
                    SemaphoreSecurity security = new SemaphoreSecurity();
                    bool semaphoreWasCreated;

                    security.AddAccessRule(new SemaphoreAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), SemaphoreRights.FullControl, AccessControlType.Allow));
                    namedSemaphore = new Semaphore(initialCount, maximumCount, semaphoreName, out semaphoreWasCreated, security);

                    if (!semaphoreWasCreated)
                        throw new InvalidOperationException("Failed to create semaphore.");
                }
                catch (UnauthorizedAccessException)
                {
                    SemaphoreSecurity security = new SemaphoreSecurity();
                    bool semaphoreWasCreated;

                    security.AddAccessRule(new SemaphoreAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), SemaphoreRights.Synchronize | SemaphoreRights.Modify, AccessControlType.Allow));
                    namedSemaphore = new Semaphore(initialCount, maximumCount, semaphoreName, out semaphoreWasCreated, security);

                    if (!semaphoreWasCreated)
                        throw new InvalidOperationException("Failed to create semaphore.");
                }
            }
            else if (unauthorized)
            {
                namedSemaphore = Semaphore.OpenExisting(semaphoreName, SemaphoreRights.ReadPermissions | SemaphoreRights.ChangePermissions);

                // Get the current ACL. This requires MutexRights.ReadPermission
                SemaphoreSecurity security = new SemaphoreSecurity();
                SemaphoreAccessRule rule = new SemaphoreAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), SemaphoreRights.FullControl, AccessControlType.Allow);
                security.RemoveAccessRule(rule);

                // Now grant specific user rights for less than full access
                rule = new SemaphoreAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), SemaphoreRights.Synchronize | SemaphoreRights.Modify, AccessControlType.Allow);
                security.AddAccessRule(rule);

                // Update the ACL. This requires MutexRighs.ChangePermission.
                namedSemaphore.SetAccessControl(security);
                namedSemaphore = Semaphore.OpenExisting(semaphoreName);
            }
#endif

            return namedSemaphore;
        }
Beispiel #28
0
		public void AddAccessRule (SemaphoreAccessRule rule)
		{
			AddAccessRule ((AccessRule)rule);
		}
Beispiel #29
0
 public void SetAccessRule(SemaphoreAccessRule rule)
 {
     base.SetAccessRule(rule);
 }
Beispiel #30
0
		public bool RemoveAccessRule (SemaphoreAccessRule rule)
		{
			return RemoveAccessRule ((AccessRule)rule);
		}
Beispiel #31
0
 public bool RemoveAccessRule(SemaphoreAccessRule rule)
 {
     return base.RemoveAccessRule(rule);
 }
Beispiel #32
0
		public void RemoveAccessRuleAll (SemaphoreAccessRule rule)
		{
			RemoveAccessRuleAll ((AccessRule)rule);
		}
Beispiel #33
0
 public void RemoveAccessRuleSpecific(SemaphoreAccessRule rule)
 {
     base.RemoveAccessRuleSpecific(rule);
 }
Beispiel #34
0
		public void RemoveAccessRuleSpecific (SemaphoreAccessRule rule)
		{
			RemoveAccessRuleSpecific ((AccessRule)rule);
		}
 public void SetAccessRule(SemaphoreAccessRule rule)
 {
     base.SetAccessRule(rule);
 }
Beispiel #36
0
		public void ResetAccessRule (SemaphoreAccessRule rule)
		{
			ResetAccessRule ((AccessRule)rule);
		}
 public bool RemoveAccessRule(SemaphoreAccessRule rule)
 {
     return(base.RemoveAccessRule(rule));
 }
Beispiel #38
0
		public void SetAccessRule (SemaphoreAccessRule rule)
		{
			SetAccessRule ((AccessRule)rule);
		}
 public void RemoveAccessRuleSpecific(SemaphoreAccessRule rule)
 {
     base.RemoveAccessRuleSpecific(rule);
 }
Beispiel #40
0
 public void AddAccessRule(SemaphoreAccessRule rule)
 {
     throw new NotImplementedException();
 }