justinstenning / SharedMemory

C# shared memory classes for sharing data between processes (Array, Buffer and Circular Buffer)
https://www.nuget.org/packages/SharedMemory
Other
566 stars 118 forks source link

When I run shared memory within a window service, Unable to read from applications as console application #53

Open vinaydwivedi opened 3 years ago

vinaydwivedi commented 3 years ago

private SharedMemory.SharedArray _buffer = _buffer = new SharedArray(@"Global\MARKETTICK", 200000); from within window service.

Try creating SharedMemory.SharedArray _reader = new SharedArray(@"Global\MARKETTICK");

I Get "Access to the path 'Global\MARKETTICK_evt_write' is denied"

When I create the file without Global Prefix i.e. private SharedMemory.SharedArray _buffer = _buffer = new SharedArray("MARKETTICK", 200000); from within window service.

from Console SharedMemory.SharedArray _reader = new SharedArray(@"MARKETTICK");

I get file not found error.

justinstenning commented 3 years ago

What is the window service authenticating as? I would say you need to change this.

vinaydwivedi commented 3 years ago

Window service is authenticated as Network service, tried running it under local service account as well, doesn't work.

justinstenning commented 3 years ago

I believe the service must be running using an account that has the Local Security Policy “Create global objects” allowed.

vinaydwivedi commented 3 years ago

Yes, please see below, Just to be sure, if this is not permission issue, I granted create "create global objects" to everyone.

image

vinaydwivedi commented 3 years ago

Could following be required, don't see this being set.

var security = new MemoryMappedFileSecurity(); security.AddAccessRule(new AccessRule("everyone", MemoryMappedFileRights.FullControl,AccessControlType.Allow));

justinstenning commented 3 years ago

Probably for the client to connect but that does not explain why you can’t create the initial MMF.

vinaydwivedi commented 3 years ago

Both the issue were from client side. I have switched to Managed version of MemoryMapped files, and able to work as expected with security MemoryMappedFileSecurity added.

justinstenning commented 3 years ago

Ah I thought the issue was creating not opening existing. Anyway, sounds like that is the issue, thanks.

lukiluki123 commented 3 years ago

Hey, vinaydwivedi. Can you explain your solution more detailed? I have similar issue but with CircularBuffer, between service (net47) and consol (net35)

wsloth commented 3 years ago

Hey all, bumping this issue thread again because we are also running into this issue.

SharedMemory via RpcBuffer works fine when the host & client are console applications, but no longer when the host is a Windows Service.

I tried the Global\Name approach, but this is not a feasible solution for us as we are deploying to hundreds of machines and would rather not modify permissions for all users on these machines just for sharing a mutex lock.

I also tried cloning the project, adding MutexSecurity as a parameter to RpcBuffer and using it to initialize the Mutex. However, I still ran into the same issues (probably due to a lack of knowledge about how to properly configure this).

Is there anyone in this thread who is willing to post their solution?

justinstenning commented 3 years ago

I haven’t had a chance to look at this in a lot of detail yet, but the MutexSecurity looks like the correct approach to me.

It looks like you need to use a security descriptor with null DACL (might not be possible using current managed classes) - take a look at the following (old but might provide some direction) https://www.codeproject.com/Articles/14022/A-security-neutral-mutex-class-for-the-managed-pla