SHMCTL(2)

✨ SHMCTL(2) ✨

okay bestie, sit DOWN because we're talking about shmctl β€” that's the shared memory control girlie from the System Calls Manual πŸ’… she's part of the Standard C Library (libc, -lc) and she is literally the queen of telling your shared memory segments what to do.


🌸 SYNOPSIS 🌸

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int
shmctl(int shmid, int cmd, struct shmid_ds *buf);

πŸ’– DESCRIPTION πŸ’–

So basically she performs whatever action you specify in cmd, and she does it on the shared memory segment that's identified by shmid. That's it. That's the whole vibe check. πŸ¦‹

Here's the tea on each cmd:

  • IPC_STAT β€” she fetches the segment's struct shmid_ds and stores it in the memory that buf points to. Grabby hands, but make it legal ✨
  • IPC_SET β€” she changes the shm_perm.uid, shm_perm.gid, and shm_perm.mode members of the segment's struct shmid_ds so they match the ones in the struct that buf points to. ⚠️ but like, permission check: the calling process's effective uid has to match either shm_perm.uid or shm_perm.cuid, OR it needs superuser privileges. No shortcuts bestie.
  • IPC_RMID β€” she removes the segment from the system. BUT β€” and this is the important part β€” the removal does not take effect until every single process that has attached the segment has exited. She's patient like that. 🧚 For the operation to succeed, the calling process's effective uid must match shm_perm.uid or shm_perm.cuid, or the process must have superuser privileges.
lowkey a big deal: if the kern.ipc.shm_allow_removed sysctl(3) variable is set to 0, then once the IPC_RMID operation has taken place, no further processes will be allowed to attach the segment. That's it. Doors closed. Not me being dramatic, that's just what it says 😭

The shmid_ds structure is defined as follows β€” copy it exactly, don't freestyle it:

struct shmid_ds {
    struct ipc_perm shm_perm;   /* operation permission structure */
    size_t          shm_segsz;  /* size of segment in bytes */
    pid_t           shm_lpid;   /* process ID of last shared memory op */
    pid_t           shm_cpid;   /* process ID of creator */
    int             shm_nattch; /* number of current attaches */
    time_t          shm_atime;  /* time of last shmat() */
    time_t          shm_dtime;  /* time of last shmdt() */
    time_t          shm_ctime;  /* time of last change by shmctl() */
};

πŸŽ€ RETURN VALUES πŸŽ€

The shmctl() function returns the value 0 if successful. If not? She returns the value -1 and sets the global variable errno to indicate the error. The duality of woman πŸ’€


πŸ”₯ ERRORS πŸ”₯

The shmctl() system call will fail if:

  • [EINVAL] β€” invalid operation, or no shared memory segment was found corresponding to shmid. She looked. She found nothing. πŸ™ˆ
  • [EPERM] β€” the calling process's effective uid does not match the uid of the shared memory segment's owner or creator. Not your segment, not your rules, bestie.
  • [EACCES] β€” permission denied due to a mismatch between the operation and the mode of the shared memory segment. The vibes were off, so the access was denied.

πŸ‘‘ SEE ALSO πŸ‘‘

shmat(2), shmdt(2), shmget(2), ftok(3)


and that's the whole page, no cap 🫢 this Debian-flavoured icon dropped on October 10, 2015 β€” yeah she's an elder but she ate and left no crumbs βœ¨πŸ’œ

πŸ“¦ where this page comes from