简体   繁体   English

如何删除另一个用户创建的共享内存文件?

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

I am just running into a problem with the permissions of /dev/shm: I have an suite of software, which multiple users should be able to use. 我刚刚遇到了/ dev / shm权限的问题:我有一套软件,多个用户应该可以使用。 Permissions of shared resources are given by using the same group. 共享资源的权限是通过使用同一组给予的。 But in case of shared memory I am running into this problem: 但是在共享内存的情况下,我遇到了这个问题:

  • a c-program run by user "a" should be able to remove a shared memory in /dev/shm, which was created by user "b" 由用户“ a”运行的c程序应该能够在/ dev / shm中删除由用户“ b”创建的共享内存。

  • due to the sticky bit of /dev/shm it is forbidden to delete a shared memory of another user - even if both users and the shared memory are belonging to the same group 由于/ dev / shm的粘性位,因此禁止删除另一个用户的共享内存-即使两个用户和共享内存都属于同一组

  • creating a subdirectory in /dev/shm with appropriate permissions does not work since inner '/' are not allowed in the filename used by shm_open() 在具有适当权限的/ dev / shm中创建子目录不起作用,因为shm_open()使用的文件名中不允许内部'/'

Well, I do not want to remove the sticky bit of /dev/shm since this is linux standard. 好吧,我不想删除/ dev / shm的粘性位,因为这是Linux标准。

Eg it would be very fine if there was a possibility to place the shared memory files of my application suite in another directory than /dev/shm - ideally a subdirectory of /dev/shm. 例如,如果有可能将我的应用程序套件的共享内存文件放置在/ dev / shm以外的另一个目录中(最好是/ dev / shm的子目录),那将是很好的。 But I did not find any hint how to do this. 但是我没有发现任何提示。

Does anyone has an idea, how to allow user "a" to delete a shared memory created by user "b"? 有谁知道如何允许用户“ a”删除用户“ b”创建的共享内存?

it would be very fine if there was a possibility to place the shared memory files of my application suite in another directory than /dev/shm 如果可以将我的应用程序套件的共享内存文件放置在/ dev / shm以外的其他目录中,那将非常好

There's nothing special about files used by shm_open() other than they have to be in /dev/shm . shm_open()使用的文件没有什么特别之处,除了它们必须位于/dev/shm You can mmap() any file with shared access exactly the same way. 您可以使用完全相同的方法mmap()任何具有共享访问权限的文件。

Don't use shm_open() to open a file for use as shared memory. 不要使用shm_open()打开用作共享内存的文件。 The only thing shm_open() really does is give you a file descriptor to a file located in /dev/shm . shm_open()唯一真正要做的就是为/dev/shm的文件提供文件描述符。 This is the entirety of glibc's shm_open() implementation : 这是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;
}

All that does is make sure the file name is for a file in /dev/shm . 所有要做的就是确保该文件名用于/dev/shm的文件。

Just replace shm_open() with a plain open() and put your shared memory file where you want. 只需将shm_open()替换为普通的open() ,然后将共享内存文件放在所需的位置即可。

Instead of 代替

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

simply use 只需使用

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