Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Using IPC::SharedMem to get nattch for a specific address?

by fairlite (Novice)
on Oct 02, 2018 at 14:26 UTC ( #1223405=perlquestion: print w/replies, xml ) Need Help??

fairlite has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to attach to a specific memory address with IPC::SharedMem, but keep getting in at 0x00000000 instead. Here's the ipcs output indicating what I specifically want to monitor:

------ Shared Memory Segments -------- key shmid owner perms bytes nattch stat +us 0x00000000 98304 root 600 67108864 20 dest 0x0052e2c1 65537 postgres 600 56 5 0xf102ee77 131074 XXXXXXX 700 65535 0
I'm trying to monitor the one whose username I have redacted, at 0xf102ee77. Here's my code for trying to do so:
use strict; use IPC::SysV qw(IPC_PRIVATE S_IRUSR S_IWUSR SHM_RDONLY); use IPC::SharedMem; use Class::Struct; my $key = '0xf102ee77'; my $shm = IPC::SharedMem->new(${key},65535,SHM_RDONLY); &fl_die("Could not open shared memory segment.") unless defined(${shm} +); my $stat = ${shm}->stat; &fl_die("Could not stat shared memory segment.") unless defined(${stat +}); my $concount = ${stat}->nattch;
Here's what my actual shared memory requests are creating (even without the create flag being used):
0x00000000 393224 root 0 65535 0 0x00000000 425993 root 0 65535 0 0x00000000 458762 root 0 65535 0 0x00000000 491531 root 0 65535 0 0x00000000 524300 root 0 65535 0 0x00000000 557069 root 0 65535 0 0x00000000 589838 root 0 65535 0

While I get no errors, nattch keeps reading zero, largely because I'm getting in at 0x00000000 instead of the requested address. If I try unquoting the key value, it won't even attach to any shared memory, and comes back undefined at the constructor call.

May I please get some assistance on this? This is my first time needing to deal with shared memory. I know what I'm after (nattch), but this module is not the best documented in the world.


Replies are listed 'Best First'.
Re: Using IPC::SharedMem to get nattch for a specific address?
by fairlite (Novice) on Oct 02, 2018 at 17:57 UTC

    After doing a bunch more research, I've discovered that you can get this data from /proc/sysvipc/shm on Linux. You need to do the UID mapping yourself, but the key value in that file, presented as a signed int, is what you want to use. Don't ask me why ipcs doesn't provide this itself. I tried a hex to decimal conversion, but that gave a value far, far different than the file cited above. I've since attached to the shared memory segment in question, and via doing what I know triggers sessions to attach/detach, I've been able to determine without doubt that I'm getting the proper count for the proper segment.

    I'll leave this here in case it helps someone else. Shared memory isn't very well-documented in general. I think I went through about 30 resources before stumbling onto the right area to search.

Re: Using IPC::SharedMem to get nattch for a specific address?
by bliako (Vicar) on Oct 02, 2018 at 19:56 UTC

    there is shared memory key and shared memory identifier. these are different as you already found out.

      Yup. And you really do use the key for this module. The issue was the hex value. You need to either use the signed int found in /proc/sysvipc/shm, or convert the hex value you get from ipcs. I did work out the conversion from one to the other and back again, and they definitely check out as matching each other. I find it easier to just rely upon proc, although it's arguably less portable to do so.

      The shmid is actually something you get from the stat method in the module, after you've already used the key to connect to the segment.

      I'm all set, and it works like a champ. My only real complaint is that there's no (apparent) method akin to select() or inotify or the like, for when nattch changes. To do what I'm doing, I absolutely must do an expensive polling loop, unfortunately. I suspect this is not a common use case, which probably explains the absence of any such creature.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1223405]
Approved by Eily
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2019-11-16 21:31 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (85 votes). Check out past polls.