private static bool OnlyOneCopy() { bool isnew; s_setup_mutex = new Mutex(true, SETUP_MUTEX_NAME, out isnew); RestoreEvent = null; try { #if RELEASE RestoreEvent = AutoResetEvent.OpenExisting(EVENT_NAME); RestoreEvent.Set(); return(false); } catch (WaitHandleCannotBeOpenedException) { #endif string user = Environment.UserDomainName + "\\" + Environment.UserName; EventWaitHandleSecurity evh_sec = new EventWaitHandleSecurity(); EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Allow); evh_sec.AddAccessRule(rule); bool was_created; RestoreEvent = new EventWaitHandle(false, EventResetMode.AutoReset, EVENT_NAME, out was_created, evh_sec); } catch (Exception) { } return(true); }
public InterProcessLockedMemoryMappedFileStream(string mapName, long capacity) { _mapName = mapName; const int streamHeaderSize = 1024; _mmf = MemoryMappedFile.CreateOrOpen(mapName, capacity + streamHeaderSize, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, null, HandleInheritability.Inheritable); _header = _mmf.CreateViewAccessor(0, streamHeaderSize); _mmfstr = _mmf.CreateViewStream(streamHeaderSize, capacity); var id = string.Format("Global\\PLMMFS-ObLock-{0}", mapName); var mutexAccessRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow); var mutexSecurity = new MutexSecurity(); mutexSecurity.AddAccessRule(mutexAccessRule); _objectMutex = new Mutex(false, id); _objectMutex.SetAccessControl(mutexSecurity); _writeSignal = new EventWaitHandle(false, EventResetMode.ManualReset, string.Format("Global\\PLMMFS-Written-{0}", mapName)); var eventSecurity = new EventWaitHandleSecurity(); var eventAccessRule = new EventWaitHandleAccessRule( new SecurityIdentifier(WellKnownSidType.WorldSid, null), EventWaitHandleRights.FullControl, AccessControlType.Allow); eventSecurity.AddAccessRule(eventAccessRule); _writeSignal.SetAccessControl(eventSecurity); _capacity = capacity; }
/// <summary> /// Gets the cross process event wait handle. /// </summary> /// <param name="name">The name.</param> /// <returns></returns> private EventWaitHandle GetCrossProcessEventWaitHandle(string name) { if (!name.StartsWith("Global\\")) { name = "Global\\" + name; } EventWaitHandle waitHandle; try { waitHandle = EventWaitHandle.OpenExisting(name, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify); } catch (WaitHandleCannotBeOpenedException) { // Create a rule that allows any authenticated user to synchronise with us var users = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null); var rule = new EventWaitHandleAccessRule(users, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Allow); var security = new EventWaitHandleSecurity(); security.AddAccessRule(rule); bool created; waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, name, out created, security); } return(waitHandle); }
private EventWaitHandleSecurity CreateSecurity(EventWaitHandleRights eventRights) { EventWaitHandleSecurity security = null; // When "running as user", we have to grant the target user permissions to the objects (events and file mapping) explicitly if (!string.IsNullOrEmpty(_session.ExecutableProcessUserName)) { security = new EventWaitHandleSecurity(); IdentityReference si; try { si = new NTAccount(_session.ExecutableProcessUserName); } catch (Exception e) { throw _logger.WriteException(new SessionLocalException(_session, string.Format(CultureInfo.CurrentCulture, "Error resolving account {0}", _session.ExecutableProcessUserName), e)); } EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule( si, eventRights, AccessControlType.Allow); security.AddAccessRule(rule); } return(security); }
// Note: in order to be consistent with other messagings (e.g. TcpMessaging) value 0 for timeouts mean infinite time. public SharedMemoryReceiver(string memoryMappedFileName, bool useExistingMemoryMappedFile, TimeSpan openTimeout, int maxMessageSize, MemoryMappedFileSecurity memmoryMappedFileSecurity) { using (EneterTrace.Entering()) { myOpenTimeout = (openTimeout == TimeSpan.Zero) ? TimeSpan.FromMilliseconds(-1) : openTimeout; myMemoryMappedFileName = memoryMappedFileName; myMaxMessageSize = maxMessageSize; // If true then the listener will not create the new memory shared file but will open existing one. // This is useful for security restrictions if the functionality is used in the windows service. myUseExistingMemoryMappedFile = useExistingMemoryMappedFile; myMemmoryMappedFileSecurity = memmoryMappedFileSecurity; // If the security shall be applied. if (myMemmoryMappedFileSecurity != null) { // Create the security for the WaitHandle sync logic. myEventWaitHandleSecurity = new EventWaitHandleSecurity(); myEventWaitHandleSecurity.SetSecurityDescriptorSddlForm("S:(ML;;NW;;;LW)"); SecurityIdentifier aSid = new SecurityIdentifier(WellKnownSidType.WorldSid, null); EventWaitHandleAccessRule anAccessRule = new EventWaitHandleAccessRule(aSid, EventWaitHandleRights.Modify | EventWaitHandleRights.Synchronize, AccessControlType.Allow); myEventWaitHandleSecurity.AddAccessRule(anAccessRule); } } }
static public EventWaitHandle CreateWaitHandle(string id, bool initState, EventResetMode resetMode, bool allowEveryone) { //EventResetMode.AutoReset: Set()後,不管有無進入WaitOne(),馬上被自動reset EventWaitHandle eventWaitHandle; bool createNew; if (allowEveryone) { //避免跨Process因權限不足產生WinIOError (Access to the path 'id' is denied.) // create a rule that allows anybody in the "Users" group to synchronise with us //var users = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null); EventWaitHandleSecurity evhSec = new EventWaitHandleSecurity(); EventWaitHandleRights rights = EventWaitHandleRights.FullControl;// EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify; EventWaitHandleAccessRule evhAccessRule = new EventWaitHandleAccessRule("Everyone", rights, AccessControlType.Allow); evhSec.AddAccessRule(evhAccessRule); eventWaitHandle = new EventWaitHandle(initState, resetMode, id, out createNew); //eventWaitHandle.SetAccessControl(evhSec); } else { eventWaitHandle = new EventWaitHandle(initState, resetMode, id, out createNew); } return(eventWaitHandle); }
private EventWaitHandle CreateSignal(string name) { EventWaitHandle result = null; // if (EventWaitHandle.TryOpenExisting(name, out result)) // { // return result; // } // else // { // code from https://stackoverflow.com/questions/2590334/creating-a-cross-process-eventwaithandle // user https://stackoverflow.com/users/241462/dean-harding var users = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null); var rule = new EventWaitHandleAccessRule(users, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Allow); var security = new EventWaitHandleSecurity(); security.AddAccessRule(rule); bool created; var wh = new EventWaitHandle(false, EventResetMode.ManualReset, name, out created, security); return(wh); // } }
public static void StartEvent() { // The event does not exist, so create it. // Create an access control list (ACL) that denies the // current user the right to wait on or signal the // event, but allows the right to read and change // security information for the event. // bool wasCreated; string user = GetUser(); EventWaitHandleSecurity EventHandleSec = new EventWaitHandleSecurity(); EventWaitHandleAccessRule rule = SetRule(user, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify); EventHandleSec.AddAccessRule(rule); rule = SetRule(user, EventWaitHandleRights.ReadPermissions | EventWaitHandleRights.ChangePermissions); EventHandleSec.AddAccessRule(rule); EventHandle = new EventWaitHandle(true, EventResetMode.AutoReset, EventName, out wasCreated, EventHandleSec); if (wasCreated) { Console.Write("Created the named event."); } else { WriteError("Unable to create the event."); return; } }
/// <summary>Initializes the synchronization objects needed to deal with multiple instances of the same application.</summary> /// <param name="baseName">The base name of the synchronization objects.</param> /// <param name="identity">The identity to be associated to the synchronization objects.</param> void InitializeSynchronizationObjects(string baseName, IdentityReference identity) { string firstInstanceMutexName = baseName + "_FirstInstance"; string serviceInitializationMutexName = baseName + "_ServiceInitialization"; string serviceReadySemaphoreName = baseName + "_ServiceReady"; string signaledToFirstInstanceSemaphoreName = baseName + "_SignaledToFirstInstance"; bool isNew; var eventRule = new EventWaitHandleAccessRule( identity, EventWaitHandleRights.FullControl, AccessControlType.Allow); var eventSecurity = new EventWaitHandleSecurity(); eventSecurity.AddAccessRule(eventRule); var mutexRule = new MutexAccessRule(identity, MutexRights.FullControl, AccessControlType.Allow); var mutexSecurity = new MutexSecurity(); mutexSecurity.AddAccessRule(mutexRule); firstInstanceMutex = new Mutex(false, firstInstanceMutexName, out isNew, mutexSecurity); serviceInitializationMutex = new Mutex(false, serviceInitializationMutexName, out isNew, mutexSecurity); serviceReadySemaphore = new EventWaitHandle( false, EventResetMode.ManualReset, serviceReadySemaphoreName, out isNew, eventSecurity); signaledToFirstInstanceSemaphore = new EventWaitHandle( false, EventResetMode.AutoReset, signaledToFirstInstanceSemaphoreName, out isNew, eventSecurity); }
protected override void ProcessRecord() { base.ProcessRecord(); var secAttrs = new EventWaitHandleSecurity(); SecurityIdentifier sid = WindowsIdentity.GetCurrent().User; var rights = EventWaitHandleRights.Synchronize; var aceType = AccessControlType.Allow; var ace = new EventWaitHandleAccessRule(sid, rights, aceType); secAttrs.AddAccessRule(ace); bool createdNew; var eventWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset, null, out createdNew, secAttrs); bool itWorked = NativeMethods.SetHandleInformation(eventWaitHandle.SafeWaitHandle, HandleFlag.Inherit, HandleFlag.Inherit); if (!itWorked) { throw new Win32Exception(); // uses last win32 error } SafeWriteObject(eventWaitHandle); } // end ProcessRecord()
/// <summary> /// Listen for a stop request. /// </summary> private void Listen() { String eventName = String.Format("STOP_COPS_MSGSERVER_{0}", Server.Name); sLogger.Info("Listening for event {0}.", eventName); var users = new SecurityIdentifier(WellKnownSidType.WorldSid, null); var rule = new EventWaitHandleAccessRule(users, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Allow); var security = new EventWaitHandleSecurity(); security.AddAccessRule(rule); bool createdNew = false; EventWaitHandle handle = new EventWaitHandle( false, EventResetMode.AutoReset, eventName, out createdNew, security); handle.WaitOne(); sLogger.Info("Received stop request !"); Server.Stop(); }
private void ConfigureDropListener() { var sid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null); var accessRule = new EventWaitHandleAccessRule(sid, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Allow); var security = new EventWaitHandleSecurity(); security.AddAccessRule(accessRule); bool newlyCreated = false; var waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, "ExtensionsForOneDrive_DataAvailableEvent", out newlyCreated, security); if (!newlyCreated) { throw new InvalidOperationException("Configuration Changed event already exists."); } _changeListenerTask = Task.Factory.StartNew(() => { while (waitHandle.WaitOne()) { GetPublicLinkAndStore(); } }); }
private bool AreAccessRulesEqual(EventWaitHandleAccessRule expectedRule, EventWaitHandleAccessRule actualRule) { return (expectedRule.AccessControlType == actualRule.AccessControlType && expectedRule.EventWaitHandleRights == actualRule.EventWaitHandleRights && expectedRule.InheritanceFlags == actualRule.InheritanceFlags && expectedRule.PropagationFlags == actualRule.PropagationFlags); }
/// <summary> /// Initializes the security for future events creation. /// </summary> private static void InitializeEventSecurity() { AuthorizedUsers = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null); AccessRule = new EventWaitHandleAccessRule(AuthorizedUsers, EventWaitHandleRights.FullControl, AccessControlType.Allow); Security = new EventWaitHandleSecurity(); Security.AddAccessRule(AccessRule); }
private EventWaitHandleSecurity GetEventWaitHandleSecurity(WellKnownSidType sid, EventWaitHandleRights rights, AccessControlType accessControl) { EventWaitHandleSecurity security = new EventWaitHandleSecurity(); SecurityIdentifier identity = new SecurityIdentifier(sid, null); EventWaitHandleAccessRule accessRule = new EventWaitHandleAccessRule(identity, rights, accessControl); security.AddAccessRule(accessRule); return(security); }
private EventWaitHandle ServiceEventFactory(string eventName) { var securityIdentifier = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null); var accessRule = new EventWaitHandleAccessRule(securityIdentifier, EventWaitHandleRights.Synchronize, AccessControlType.Allow); var security = new EventWaitHandleSecurity(); security.AddAccessRule(accessRule); return(new EventWaitHandle(false, EventResetMode.AutoReset, eventName, out _, security)); }
private bool InitEvent(string instanceName) { this.eventWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset, instanceName, out bool createdNew); var allowEveryoneRule = new EventWaitHandleAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), EventWaitHandleRights.FullControl, AccessControlType.Allow); var securitySettings = new EventWaitHandleSecurity(); securitySettings.AddAccessRule(allowEveryoneRule); this.eventWaitHandle.SetAccessControl(securitySettings); return(createdNew); }
public static EventWaitHandleSecurity EventSecurity() { SecurityIdentifier user = GetEveryoneSID(); EventWaitHandleSecurity result = new EventWaitHandleSecurity(); EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.FullControl, AccessControlType.Allow); result.AddAccessRule(rule); return(result); }
public static void CreateSyncEvent() { EventWaitHandleSecurity eventSecurity = new EventWaitHandleSecurity(); EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule("SYSTEM", EventWaitHandleRights.FullControl, AccessControlType.Allow); eventSecurity.AddAccessRule(rule); rule = new EventWaitHandleAccessRule("NETWORK SERVICE", EventWaitHandleRights.FullControl, AccessControlType.Allow); eventSecurity.AddAccessRule(rule); bool createdNew; EventWaitHandle ew = new EventWaitHandle(true, EventResetMode.AutoReset, "Global\\ShopAide_MemSync_Event", out createdNew, eventSecurity); ew.Set(); }
private EventWaitHandle CreateGlobalEventWaitHandle(string name) { var users = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null); var rule = new EventWaitHandleAccessRule(users, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Allow); var security = new EventWaitHandleSecurity(); security.AddAccessRule(rule); bool createdNew; return(new EventWaitHandle(false, EventResetMode.AutoReset, name, out createdNew, security)); }
public static void Main() { // Create a string representing the current user. string user = Environment.UserDomainName + "\\" + Environment.UserName; // Create a security object that grants no access. EventWaitHandleSecurity mSec = new EventWaitHandleSecurity(); // Add a rule that grants the current user the // right to wait on or signal the event. EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Allow); mSec.AddAccessRule(rule); // Add a rule that denies the current user the // right to change permissions on the event. rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.ChangePermissions, AccessControlType.Deny); mSec.AddAccessRule(rule); // Display the rules in the security object. ShowSecurity(mSec); // Add a rule that allows the current user the // right to read permissions on the event. This rule // is merged with the existing Allow rule. rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.ReadPermissions, AccessControlType.Allow); mSec.AddAccessRule(rule); ShowSecurity(mSec); // Create a rule that allows the current user to // take ownership of the event, and use that rule // to remove the existing Allow rule from the // EventWaitHandleSecurity object, showing that the user // and access type must match, while the rights are // ignored. Console.WriteLine("Use RemoveAccessRuleAll to remove the Allow rule."); rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.TakeOwnership, AccessControlType.Allow); mSec.RemoveAccessRuleAll(rule); ShowSecurity(mSec); }
public bool TPcastStartChecking() { this.StopThread = false; bool flag = false; try { this.ewh_stopthread = new EventWaitHandle(false, EventResetMode.AutoReset); this.ewh_devstatus = new EventWaitHandle(false, EventResetMode.ManualReset, "Global\\TPcastDeviceEvent", out this.ewhwasCreated); } catch (UnauthorizedAccessException ex) { Console.WriteLine("UnauthorizedAccessException: {0}", ex.Message); flag = true; } catch (Exception ex2) { Console.WriteLine("Exception: {0}", ex2.Message); bool result = false; return(result); } if (flag) { try { this.ewh_devstatus = EventWaitHandle.OpenExisting("Global\\TPcastDeviceEvent", EventWaitHandleRights.ReadPermissions | EventWaitHandleRights.ChangePermissions); EventWaitHandleSecurity accessControl = this.ewh_devstatus.GetAccessControl(); string expr_9B = Environment.UserDomainName + "\\" + Environment.UserName; EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule(expr_9B, EventWaitHandleRights.Modify | EventWaitHandleRights.Synchronize, AccessControlType.Deny); accessControl.RemoveAccessRule(rule); rule = new EventWaitHandleAccessRule(expr_9B, EventWaitHandleRights.Modify | EventWaitHandleRights.Synchronize, AccessControlType.Allow); accessControl.AddAccessRule(rule); this.ewh_devstatus.SetAccessControl(accessControl); Console.WriteLine("Updated event security."); this.ewh_devstatus = EventWaitHandle.OpenExisting("Global\\TPcastDeviceEvent"); } catch (UnauthorizedAccessException ex3) { Console.WriteLine("Unable to change permissions: {0}", ex3.Message); bool result = false; return(result); } } if (!TPcastRemoteDevices.ThreadIsRunning) { TPcastRemoteDevices.ThreadIsRunning = true; this.workerThread = new Thread(new ParameterizedThreadStart(this.ProcessChecking)); this.workerThread.Start(this); } return(true); }
public static bool Create(string name, EventResetMode mode, out EventWaitHandle signal) { var users = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null); var rule = new EventWaitHandleAccessRule(users, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Allow); var security = new EventWaitHandleSecurity(); security.AddAccessRule(rule); bool created; signal = new EventWaitHandle(false, mode, @"Global\" + name, out created, security); return(created); }
// Methods public ASMExecutor(MemoryManager memory, IntPtr endscene) { bool flag; this.privateLock = new object(); this.byte_0 = new byte[0x1000]; this.publicLock = new object(); if (!((memory != null) && memory.IsProcessOpen)) { throw new ArgumentNullException("memory", "Memory object passed to Executor constructor was invalid"); } if (endscene == IntPtr.Zero) { throw new ArgumentNullException("endscene", "Endscene pointer passed to Executor constructor was invalid"); } this.Memory = memory; this.Memory.Asm.SetMemorySize(0x10000); this.pntEndScene = endscene; byte[] data = new byte[4]; new RNGCryptoServiceProvider().GetNonZeroBytes(data); this.random_0 = new Random(BitConverter.ToInt32(data, 0)); string identity = Environment.UserDomainName + @"\" + Environment.UserName; EventWaitHandleSecurity eventSecurity = new EventWaitHandleSecurity(); EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule(identity, EventWaitHandleRights.FullControl, AccessControlType.Allow); eventSecurity.AddAccessRule(rule); this.eventWaitHandle_0_Name = @"Global\" + this.RandomString(0x10); this.eventWaitHandle_0 = new EventWaitHandle(false, EventResetMode.AutoReset, this.eventWaitHandle_0_Name, out flag, eventSecurity); if (!flag) { throw new Exception("You should never see this message but the event was opened instead of created! That's bad!"); } this.eventWaitHandle_1_Name = @"Global\" + this.RandomString(0x10); this.eventWaitHandle_1 = new EventWaitHandle(false, EventResetMode.AutoReset, this.eventWaitHandle_1_Name, out flag, eventSecurity); if (!flag) { throw new Exception("You should never see this message but the event was opened instead of created! That's bad!"); } this.eventWaitHandle_2_Name = @"Global\" + this.RandomString(0x10); this.eventWaitHandle_2 = new EventWaitHandle(false, EventResetMode.AutoReset, this.eventWaitHandle_2_Name, out flag, eventSecurity); if (!flag) { throw new Exception("You should never see this message but the event was opened instead of created! That's bad!"); } this.Initialize(); this.Clear(); this.IsInitialized = true; }
/// <summary> /// Initializes a new instance of the <see cref="GlobalLockData" /> class /// </summary> /// <param name="id">Storage id</param> /// <param name="global">Indicating whether to use global or terminal session specific names</param> public GlobalLockData(Guid id, bool global) { _id = id; var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow); var securitySettings = new MutexSecurity(); securitySettings.AddAccessRule(allowEveryoneRule); _mutex = new Mutex(false, $"{(global ? "Global\\" : "")}BlobStorage-{id}-Lock", out _, securitySettings); var eventSecurity = new EventWaitHandleSecurity(); var eventRule = new EventWaitHandleAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), EventWaitHandleRights.FullControl, AccessControlType.Allow); eventSecurity.AddAccessRule(eventRule); ReadEvent = new EventWaitHandle(true, EventResetMode.ManualReset, $"{(global ? "Global\\" : "")}BlobStorage-{id}-ReadSignal", out _, eventSecurity); }
public static EventWaitHandle GetOrCreateEvent(string name) { EventWaitHandle eventWaitHandle = null; if (!EventWaitHandle.TryOpenExisting(@"Global\" + name, out eventWaitHandle)) { SecurityIdentifier users = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null); EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule(users, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Allow); EventWaitHandleSecurity security = new EventWaitHandleSecurity(); security.AddAccessRule(rule); bool created; eventWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset, @"Global\" + name, out created, security); } return(eventWaitHandle); }
public TvServiceThread() { // set working dir from application.exe string applicationPath = Application.ExecutablePath; applicationPath = System.IO.Path.GetFullPath(applicationPath); applicationPath = System.IO.Path.GetDirectoryName(applicationPath); System.IO.Directory.SetCurrentDirectory(applicationPath); _powerEventHandlers = new List <PowerEventHandler>(); GlobalServiceProvider.Instance.Add <IPowerEventHandler>(this); AddPowerEventHandler(OnPowerEventHandler); try { Log.Debug("Setting up EventWaitHandle with name: {0}", RemoteControl.InitializedEventName); EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), EventWaitHandleRights.FullControl, AccessControlType.Allow); EventWaitHandleSecurity sec = new EventWaitHandleSecurity(); sec.AddAccessRule(rule); bool eventCreated; _InitializedEvent = new EventWaitHandle(false, EventResetMode.ManualReset, RemoteControl.InitializedEventName, out eventCreated, sec); if (!eventCreated) { Log.Info("{0} was not created", RemoteControl.InitializedEventName); } } catch (Exception ex) { Log.Write(ex); } // setup the remoting channels try { string remotingFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; // process the remoting configuration file RemotingConfiguration.Configure(remotingFile, false); } catch (Exception ex) { Log.Write(ex); } }
public static EventWaitHandle AttachToEventWaitHandle(string name, EventResetMode mode = EventResetMode.ManualReset) { bool createdNew; // Create a string representing the current user. string user = Environment.UserDomainName + "\\" + Environment.UserName; // Create a security object that grants no access. EventWaitHandleSecurity mSec = new EventWaitHandleSecurity(); // Add a rule that grants the current user the // right to wait on or signal the event. EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.FullControl, AccessControlType.Allow); mSec.AddAccessRule(rule); return(new EventWaitHandle(false, mode, name, out createdNew, mSec)); }
public static void Main() { // Create a string representing the current user. string user = Environment.UserDomainName + "\\" + Environment.UserName; // Create a security object that grants no access. EventWaitHandleSecurity mSec = new EventWaitHandleSecurity(); // Add a rule that grants the current user the // right to wait on or signal the event and read the // permissions on the event. EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify | EventWaitHandleRights.ReadPermissions, AccessControlType.Allow); mSec.AddAccessRule(rule); // Add a rule that denies the current user the // right to change permissions on the event. rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.ChangePermissions, AccessControlType.Deny); mSec.AddAccessRule(rule); // Display the rules in the security object. ShowSecurity(mSec); // Create a rule that grants the current user // the right to read permissions on the event, and // take ownership of the event. Use this rule to // remove the right to read permissions from the // Allow rule for the current user. The inclusion // of the right to take ownership has no effect. rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.TakeOwnership | EventWaitHandleRights.ReadPermissions, AccessControlType.Allow); mSec.RemoveAccessRule(rule); ShowSecurity(mSec); }
/// <summary> /// Monitors a global event that can be used to signal this process to exit, for example /// during install /// </summary> private void InitExitEventWaiter() { // create a rule that allows anybody in the "Users" group to synchronise with us var users = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null); var rule = new EventWaitHandleAccessRule(users, EventWaitHandleRights.FullControl, AccessControlType.Allow); var security = new EventWaitHandleSecurity(); security.AddAccessRule(rule); EventWaitHandle eventWaitHandle; try { eventWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset, @"Global\nachmore.ChimeHelper.IsRunning", out bool createdNew); eventWaitHandle.SetAccessControl(security); } catch (Exception e) { Debug.WriteLine("InitExitEventWaiter: " + e); // Windows will sometimes get into a weird state and throw AccessDenied, in these cases ignore the exception and continue to launch // since this can often be when a different ChimeHelper is stuck return; } _exitRequestWaiter = new Thread(() => { try { eventWaitHandle.WaitOne(); Application.Current.Dispatcher.BeginInvoke(new Action(() => { Application.Current.Shutdown(); })); } catch (ThreadInterruptedException) { } // expected while exiting }); _exitRequestWaiter.Start(); }
public void SetAccessRule(EventWaitHandleAccessRule rule);
public void RemoveAccessRuleSpecific(EventWaitHandleAccessRule rule);
public void RemoveAccessRuleAll(EventWaitHandleAccessRule rule);
public bool RemoveAccessRule(EventWaitHandleAccessRule rule);
public void AddAccessRule(EventWaitHandleAccessRule rule);
public void ResetAccessRule(EventWaitHandleAccessRule rule) {}