MSGGET(2)

๐Ÿ’ฌ MSGGET(2) โ€” get message queue ๐Ÿ’ฌ

okay bestie, welcome to the msggot... no wait, msgget page ๐Ÿ’– it's a System Calls Manual entry, and it is exactly the kind of thing you need in your life if you're out here doing inter-process communication like a boss. let's get into it ๐Ÿซถ


โœจ NAME โœจ

msgget โ€” get a message queue, babes ๐Ÿ’…


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C Library (libc, -lc) ๐ŸŽ€


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <sys/msg.h>

int
msgget(key_t key, int msgflg);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so, the msgget() function returns the message queue identifier that's associated with key. and what even is a message queue identifier? it's a unique integer, and it's greater than zero, periodt ๐Ÿ’ฏ

here's the tea โ˜• โ€” a message queue gets created if EITHER:

  • key is equal to IPC_PRIVATE, OR
  • key doesn't have a message queue identifier associated with it and the IPC_CREAT bit is set in msgflg
not me pointing out that you literally need the IPC_CREAT bit if you want a fresh queue and the key isn't IPC_PRIVATE. that's just how it is, no cap.

if a brand new message queue gets created, the data structure that's associated with it โ€” that's the msqid_ds structure, see msgctl(2) โ€” gets initialized like this ๐Ÿ‘‡

  • msg_perm.cuid and msg_perm.uid are set to the effective uid of the calling process ๐Ÿ‘‘
  • msg_perm.gid and msg_perm.cgid are set to the effective gid of the calling process ๐Ÿ‘‘
  • msg_perm.mode is set to the lower 9 bits of msgflg, which are set by ORing these constants together:
    • 0400 โ€” Read access for user ๐ŸŒˆ
    • 0200 โ€” Write access for user โœ๏ธ
    • 0040 โ€” Read access for group ๐ŸŒˆ
    • 0020 โ€” Write access for group โœ๏ธ
    • 0004 โ€” Read access for other ๐ŸŒˆ
    • 0002 โ€” Write access for other โœ๏ธ
  • msg_cbytes, msg_qnum, msg_lspid, msg_lrpid, msg_rtime, and msg_stime are all set to 0 ๐Ÿซง (yes, all of them, they start from scratch, we love a clean slate)
  • msg_qbytes is set to the system wide maximum value for the number of bytes in a queue โ€” that's MSGMNB ๐Ÿงฎ
  • msg_ctime is set to the current time โฐ

๐ŸŽ€ RETURN VALUES ๐ŸŽ€

if everything goes slay, a positive message queue identifier comes back at you ๐Ÿ’–

otherwise? you get -1 back, and the global variable errno gets set to indicate the error ๐Ÿ˜ญ (we hate to see it, but at least it tells you why)


๐Ÿ’€ ERRORS ๐Ÿ’€

  • [EACCES] โ€” a message queue is already associated with key and the caller has no permission to access it ๐Ÿ™ˆ
  • [EEXIST] โ€” both IPC_CREAT and IPC_EXCL are set in msgflg, and a message queue is already associated with key ๐Ÿ˜ค
  • [ENOSPC] โ€” a new message queue could not be created because the system limit for the number of message queues has been reached ๐Ÿ’ฅ
  • [ENOENT] โ€” IPC_CREAT was not set in msgflg and no message queue associated with key was found ๐Ÿ˜”

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

msgctl(2), msgrcv(2), msgsnd(2) โ€” go read those next, bestie, they're the whole friend group ๐Ÿซถ


๐Ÿ“œ HISTORY ๐Ÿ“œ

Message queues appeared in the first release of AT&T System V UNIX โญ like... iconic behavior honestly, they've been around forever and they still ate ๐Ÿ‘‘


and that's the whole page, my love ๐Ÿ’– this one's from the Debian fam, dated March 4, 2018 ๐ŸŽ€ go make some queues, and don't forget to clean up after yourself, iykyk ๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from