MSGSND(2)

πŸ’Œ MSGSND(2) β€” bestie edition

okay so this is msgsnd, a system call from the System Calls Manual bestie, and it lives in the Standard C Library (libc, -lc). it sends messages to message queues and honestly? iconic behavior.


✨ NAME ✨

msgsnd β€” send a message to a message queue πŸ’¬

πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)

πŸŽ€ SYNOPSIS πŸŽ€

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

int
msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

πŸ’– DESCRIPTION πŸ’–

so the way msgsnd() works is it sends a message to the message queue you specified in msqid. and msgp is a pointer to a structure holding the actual message. that structure should have these members, no cap:

long mtype;    /* message type */
char mtext[1]; /* body of message */

mtype is an integer greater than 0 and it's what you use to pick out messages later (peep msgrcv(2) for that one 🫢). and mtext is an array of msgsz bytes. the msgsz argument can go from 0 all the way up to a system-imposed maximum, MSGMAX. lowkey a glow-up range.

now here's the tea: if the number of bytes already chillin' on the message queue plus msgsz is bigger than the maximum number of bytes allowed on the queue (msg_qbytes, see msgctl(2)), or the number of messages on all queues system-wide is already equal to the system limit, then msgflg is the one deciding what msgsnd() does next:

  • if msgflg has the IPC_NOWAIT mask set in it, the call just returns immediately. no waiting, main character energy.
  • if msgflg does not have IPC_NOWAIT set in it, the call will block until:
    • the condition that made it block no longer exists. the message will be sent ✨
    • the message queue is removed β€” in which case -1 gets returned and errno is set to EINVAL 😭
    • the caller catches a signal. the call returns with errno set to EINTR.

after a successful call, the data structure associated with the message queue gets updated like this:

  • msg_cbytes is incremented by the size of the message.
  • msg_qnum is incremented by 1.
  • msg_lspid is set to the pid of the calling process.
  • msg_stime is set to the current time.

🌈 RETURN VALUES 🌈

msgsnd() returns the value 0 if it slayed (successful πŸŽ‰). otherwise the value -1 is returned and the global variable errno is set to indicate the error. sad girl hours fr.

πŸ’€ ERRORS πŸ’€

msgsnd() will fail if:

  • [EINVAL] β€” the msqid argument is not a valid message queue identifier.

the message queue was removed while msgsnd() was waiting for a resource to become available in order to deliver the message.

the msgsz argument is greater than msg_qbytes.

the mtype argument is not greater than 0.

  • [EACCES] β€” the calling process does not have write access to the message queue. πŸ™ˆ
  • [EAGAIN] β€” there was no space for this message either on the queue, or in the whole system, and IPC_NOWAIT was set in msgflg.
  • [EFAULT] β€” the msgp argument points to an invalid address.
  • [EINTR] β€” the system call was interrupted by the delivery of a signal.

πŸ¦‹ HISTORY πŸ¦‹

message queues appeared in the first release of AT&T Unix System V. old money behavior honestly πŸ‘‘

πŸ› BUGS πŸ›

NetBSD and FreeBSD do not define the EIDRM error value, which should be used in the case of a removed message queue. not them being inconsistent πŸ’€


and that's the whole page bestie πŸ’œ this one shipped with Debian, dated July 9, 2009, and the man page is literally just called MSGSND(2). go send some messages, you got this 🫢✨

πŸ“¦ where this page comes from