get size of all shared memory segments

社会主义新天地 提交于 2019-12-13 16:33:00

问题


On Solaris it is possible to get with shmctl the size of a shared memory segment that I did not created and for which I don't have the permission ?

For example I want to get the size of a segment created by another user with read-only mode for him only (600).

I tried with the code below to get total size of all shared memory segments but I get a permission denied

Cannot access shared memory information for segment 0 because Permission denied

when I don't have read rights on a segment.

int *ids = NULL;
uint_t nids = 0;
uint_t n;

unsigned long memoryUsed = 0;

if (shmids(ids, nids, &n) != 0)
{
   cout <<  "Failed to get shared memory info: " << strerror(errno);
   return 0;
}

nids =n;
ids = (int *) realloc(ids, nids * sizeof (int));

if (shmids(ids, nids, &n) != 0)
{
    cout <<  "Failed to get shared memory info: " << strerror(errno);
    return 0;
}
else
{    

    for (int i = 0; i < n; i++)
    {
        struct shmid_ds buf;
        int iError = shmctl(ids[i], IPC_STAT, &buf);
        if( iError < 0 )
        {
           cout << "Cannot access shared memory information for segment " << ids[i] << " because " << strerror(errno) << endl;
        } 
        else
        {
            memoryUsed += buf.shm_segsz;
        }
    }    
}

return memoryUsed;

I ask the question because ipcs -ma command is able to get the size of all existing segments therefore I should be able to do the same.

IPC status from <running system> as of Wed Mar  9 19:44:34 MET 2016
T         ID      KEY        MODE        OWNER    GROUP  CREATOR   CGROUP NATTCH      SEGSZ  CPID  LPID   ATIME    DTIME    CTIME
Shared Memory:
m        215   0          --rw-------  olivier    staff   olivier  staff      0       4096 10869 10869 19:10:21 19:10:21 19:10:21
m          0   0x8d0c6ee7 --rw-------  oracle     dba     oracle   dba        4 40956239872   785  2275  7:17:38  7:17:38  7:16:47

来源:https://stackoverflow.com/questions/35900144/get-size-of-all-shared-memory-segments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!