简体   繁体   中英

C IPC: Revoke access to shared memory, similar to Microsoft/Apple Clipboard

As I understand it, the way you interact with the clipboard in Windows(And MacOS too, I think) is similar to:

  1. Open the clipboard (Requesting access)
  2. Clear the clipboard
  3. Allocate new Global Memory, and get a pointer to that memory
  4. Fill the memory
  5. Release the memory handle
  6. Indicate to the system that the clipboard is ready.

Those final steps are what I am concerned with- Reading up on shared memory APIs, I see no way for a provider of shared memory to enforce/verify that someone it has shared the memory with has in fact released it. Without such a guarantee, the "Copier" could manipulate the data freely even when it was supposed to be "Done", without the knowledge of the clipboard owner.

Can someone help me find how one process can create shared memory (Similar to shm_open()), share that memory, and then know when the client they shared to has completely released that memory (Or force revoke it- either works)?

Alternatively, am I either having a key misunderstanding in how these clipboards work, or are these OS taking further special OS level action that a normal program can not replicate?

I may have found the answer, please let me know this makes sense here, I won't be able to try it for a couple of days:

In the man page for fcntl it mentions the following behavior: EBUSY cmd is F_ADD_SEALS, arg includes F_SEAL_WRITE, and there exists a writable, shared mapping on the file referred to by fd

So you could create a block of shared memory, pass it to a trusted arbiter, and not trust that block until you successfully apply F_SEAL_WRITE (Thus knowing the client has released all open writable mappings, and is unable to create any more)

I was looking for this for most of this week, but found the answer almost right after posting to stackoverflow. If I got this right, sorry for the trouble but hopefully it helps people in the future!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM