Operating System - HP-UX
1833465 Members
3083 Online
110052 Solutions
New Discussion

changing permission of shmat address without detach

 
Srinivasan S_1
Advisor

changing permission of shmat address without detach

Hi,
Is there is any way to change the address attached as "Read only" (SHM_RDONLY) to write enabled without "detaching" in same process.

For example,
Process P1()
{
addr1 = shmat(id1,0,SHM_RDONLY);
//addr1 is being used by another process
function(addr1);
shmdt(addr1)
addr2=shmat(id1,0,0);
//Modifies data of shared memory id1,
addr2. data = XXX
}

function(addr1)
{
//addr1 is used down the line
}

Can above be done without detaching the address "addr1" , so that I can get another address("addr2") within same process which is write enabled.

Thanks in Advance.

Regards,
Srinivasan S
8 REPLIES 8
Don Morris_1
Honored Contributor

Re: changing permission of shmat address without detach

Not as a general rule. There's only one way to do this I can think of -- you'd have to be running 11.23 or later and the application would have to be using the Mostly Private Address Space layout (MPAS). MPAS processes are allowed to have multiple private copies of the same object - which is what you're describing.

For pre-11.23 or the traditional address space models (Mostly Global), this just isn't possible. From shmop(2): "If the calling process is already attached to the shared memory segment, shmat() fails and returns SHM_FAILED regardless of what value is passed in shmaddr."
Srinivasan S_1
Advisor

Re: changing permission of shmat address without detach

Thanks!
Let me simplify the question,

If I have attached to a Shared Memory segment in "Read Only" mode can I change it to "Write Enabled" without detaching to segment ?

Can I achieve this in SYS V Calls and is there is any other way to achieve above in SYS V Implementation ?

Don Morris_1
Honored Contributor

Re: changing permission of shmat address without detach

Yes, that you can do with mprotect(). [Note that you have to initially shmat() the segment writable, then mprotect() it to read -- if it's mapped read-only, you'll fail a mprotect() call to make it writeable]. I'm not positive what this gets you, though -- if you're doing this to try to limit data corruption, you're just narrowing the window since any thread in the process is still going to be able to write during the PROT_WRITE time... and I'd assume you're already using synchronization of some kind on the writes so it is unexpected writes you'd be looking for.
Srinivasan S_1
Advisor

Re: changing permission of shmat address without detach

Thanks Again Morris !
But mprotect will require address to be multiple of page size.

My system is configured for 4096 bytes. If I require protection of memory in particular region which is not multiple of 4096 then how I can achieve such a protection ?

For example,

If allocate shared memory of 1024 bytes( which by itself is less than page size of 4096 bytes).

Lets say address addr1, I want to protect the memory (prevent accidental write) region between addr1+40, addr1+80 bytes, can I do this in SYS V Call ?

Thanks again for your response.

Note: Of Course lock consistency are taken care.
Don Morris_1
Honored Contributor

Re: changing permission of shmat address without detach

Any new mapping requests to the system which are less than a page are really going to get a page. VM doesn't do micro-pages internally -- and the protection model is all page-based as well. So you can ask for the segment to be 128 bytes -- but you still use a full page for it anyway.

mprotect() can handle less than a full page arguments - so just be consistent and it shouldn't matter (though if you set up a 128 bytes segment and read between 128 to the page boundary, you just get zero'es instead of a SIGSEGV).

Srinivasan S_1
Advisor

Re: changing permission of shmat address without detach

Thanks !

I conclude that at user level If I have to protect memory of certain region (using SYS V Call) i.e., memory of lesser than page size
it is not possible.

I can do only page protection.

But I still feel that at programming level ideally I should not be bothered about pages. I feel as a programmer I should have provision to control even small memory area.

Regards.




Srinivasan S_1
Advisor

Re: changing permission of shmat address without detach

Thanks !

I conclude that at user level If I have to protect memory of certain region (using SYS V Call) i.e., memory of lesser than page size
it is not possible.

I can do only page protection.

But I still feel that at programming level ideally I should not be bothered about pages. I feel as a programmer I should have provision to control even small memory area.

With kind Regards.




Srinivasan S_1
Advisor

Re: changing permission of shmat address without detach

Thanks !

I conclude that at user level If I have to protect memory of certain region (using SYS V Call) i.e., memory of lesser than page size
it is not possible.

I can do only page protection.

But I still feel that at programming level ideally I should not be bothered about pages. I feel as a programmer I should have provision to control even small memory area.

With kind Regards,
Srinivasan S