簡體   English   中英

如何刪除另一個用戶創建的共享內存文件?

[英]How to delete a shared memory file created by another user?

我剛剛遇到了/ dev / shm權限的問題:我有一套軟件,多個用戶應該可以使用。 共享資源的權限是通過使用同一組給予的。 但是在共享內存的情況下,我遇到了這個問題:

  • 由用戶“ a”運行的c程序應該能夠在/ dev / shm中刪除由用戶“ b”創建的共享內存。

  • 由於/ dev / shm的粘性位,因此禁止刪除另一個用戶的共享內存-即使兩個用戶和共享內存都屬於同一組

  • 在具有適當權限的/ dev / shm中創建子目錄不起作用,因為shm_open()使用的文件名中不允許內部'/'

好吧,我不想刪除/ dev / shm的粘性位,因為這是Linux標准。

例如,如果有可能將我的應用程序套件的共享內存文件放置在/ dev / shm以外的另一個目錄中(最好是/ dev / shm的子目錄),那將是很好的。 但是我沒有發現任何提示。

有誰知道如何允許用戶“ a”刪除用戶“ b”創建的共享內存?

如果可以將我的應用程序套件的共享內存文件放置在/ dev / shm以外的其他目錄中,那將非常好

shm_open()使用的文件沒有什么特別之處,除了它們必須位於/dev/shm 您可以使用完全相同的方法mmap()任何具有共享訪問權限的文件。

不要使用shm_open()打開用作共享內存的文件。 shm_open()唯一真正要做的就是為/dev/shm的文件提供文件描述符。 這是glibc的shm_open()實現的全部內容

/* Open shared memory object.  */
int
shm_open (const char *name, int oflag, mode_t mode)
{
  SHM_GET_NAME (EINVAL, -1, "");

  oflag |= O_NOFOLLOW | O_CLOEXEC;

  /* Disable asynchronous cancellation.  */
  int state;
  pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);

  int fd = open (shm_name, oflag, mode);
  if (fd == -1 && __glibc_unlikely (errno == EISDIR))
    /* It might be better to fold this error with EINVAL since
       directory names are just another example for unsuitable shared
       object names and the standard does not mention EISDIR.  */
    __set_errno (EINVAL);

  pthread_setcancelstate (state, NULL);

  return fd;
}

所有要做的就是確保該文件名用於/dev/shm的文件。

只需將shm_open()替換為普通的open() ,然后將共享內存文件放在所需的位置即可。

代替

int fd = shm_open( "shared_memory_file", oflag, mode );

只需使用

int fd = open( "/path/to/my/shared_memory_file", oflag, mode );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM