1MSGCTL(2)                  Linux Programmer's Manual                 MSGCTL(2)
2
3
4

NAME

6       msgctl - System V message control operations
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/ipc.h>
11       #include <sys/msg.h>
12
13       int msgctl(int msqid, int cmd, struct msqid_ds *buf);
14

DESCRIPTION

16       msgctl()  performs  the  control operation specified by cmd on the Sys‐
17       tem V message queue with identifier msqid.
18
19       The msqid_ds data structure is defined in <sys/msg.h> as follows:
20
21           struct msqid_ds {
22               struct ipc_perm msg_perm;     /* Ownership and permissions */
23               time_t          msg_stime;    /* Time of last msgsnd(2) */
24               time_t          msg_rtime;    /* Time of last msgrcv(2) */
25               time_t          msg_ctime;    /* Creation time/time of last
26                                                modification via msgctl() */
27               unsigned long   __msg_cbytes; /* Current number of bytes in
28                                                queue (nonstandard) */
29               msgqnum_t       msg_qnum;     /* Current number of messages
30                                                in queue */
31               msglen_t        msg_qbytes;   /* Maximum number of bytes
32                                                allowed in queue */
33               pid_t           msg_lspid;    /* PID of last msgsnd(2) */
34               pid_t           msg_lrpid;    /* PID of last msgrcv(2) */
35           };
36
37       The fields of the msgid_ds structure are as follows:
38
39       msg_perm   This is an ipc_perm structure (see below) that specifies the
40                  access permissions on the message queue.
41
42       msg_qnum   Number of messages currently on the message queue.
43
44       msg_qbytes Maximum  number of bytes of message text allowed on the mes‐
45                  sage queue.
46
47       msg_lspid  ID of the process that performed the last  msgsnd(2)  system
48                  call.
49
50       msg_lrpid  ID  of  the process that performed the last msgrcv(2) system
51                  call.
52
53       msg_stime  Time of the last msgsnd(2) system call.
54
55       msg_rtime  Time of the last msgrcv(2) system call.
56
57       msg_ctime  Time of creation of queue or time of last  msgctl()  IPC_SET
58                  operation.
59
60       The  ipc_perm  structure  is defined as follows (the highlighted fields
61       are settable using IPC_SET):
62
63           struct ipc_perm {
64               key_t          __key;       /* Key supplied to msgget(2) */
65               uid_t          uid;         /* Effective UID of owner */
66               gid_t          gid;         /* Effective GID of owner */
67               uid_t          cuid;        /* Effective UID of creator */
68               gid_t          cgid;        /* Effective GID of creator */
69               unsigned short mode;        /* Permissions */
70               unsigned short __seq;       /* Sequence number */
71           };
72
73       The least significant 9 bits of the mode field of the  ipc_perm  struc‐
74       ture  define the access permissions for the message queue.  The permis‐
75       sion bits are as follows:
76
77       0400   Read by user
78       0200   Write by user
79       0040   Read by group
80       0020   Write by group
81       0004   Read by others
82       0002   Write by others
83
84       Bits 0100, 0010, and 0001 (the execute bits) are unused by the system.
85
86       Valid values for cmd are:
87
88       IPC_STAT
89              Copy information from the kernel data structure associated  with
90              msqid into the msqid_ds structure pointed to by buf.  The caller
91              must have read permission on the message queue.
92
93       IPC_SET
94              Write the values of  some  members  of  the  msqid_ds  structure
95              pointed  to  by buf to the kernel data structure associated with
96              this message queue, updating also  its  msg_ctime  member.   The
97              following  members  of  the  structure  are updated: msg_qbytes,
98              msg_perm.uid, msg_perm.gid, and (the least  significant  9  bits
99              of)  msg_perm.mode.   The  effective  UID of the calling process
100              must match the owner (msg_perm.uid) or  creator  (msg_perm.cuid)
101              of  the message queue, or the caller must be privileged.  Appro‐
102              priate privilege (Linux:  the  CAP_SYS_RESOURCE  capability)  is
103              required to raise the msg_qbytes value beyond the system parame‐
104              ter MSGMNB.
105
106       IPC_RMID
107              Immediately remove the  message  queue,  awakening  all  waiting
108              reader  and writer processes (with an error return and errno set
109              to EIDRM).  The calling process must have appropriate privileges
110              or  its  effective user ID must be either that of the creator or
111              owner of the message queue.  The third argument to  msgctl()  is
112              ignored in this case.
113
114       IPC_INFO (Linux-specific)
115              Return  information  about  system-wide message queue limits and
116              parameters in the structure pointed to by buf.   This  structure
117              is  of  type  msginfo  (thus,  a  cast  is required), defined in
118              <sys/msg.h> if the _GNU_SOURCE feature test macro is defined:
119
120                  struct msginfo {
121                      int msgpool; /* Size in kibibytes of buffer pool
122                                      used to hold message data;
123                                      unused within kernel */
124                      int msgmap;  /* Maximum number of entries in message
125                                      map; unused within kernel */
126                      int msgmax;  /* Maximum number of bytes that can be
127                                      written in a single message */
128                      int msgmnb;  /* Maximum number of bytes that can be
129                                      written to queue; used to initialize
130                                      msg_qbytes during queue creation
131                                      (msgget(2)) */
132                      int msgmni;  /* Maximum number of message queues */
133                      int msgssz;  /* Message segment size;
134                                      unused within kernel */
135                      int msgtql;  /* Maximum number of messages on all queues
136                                      in system; unused within kernel */
137                      unsigned short int msgseg;
138                                   /* Maximum number of segments;
139                                      unused within kernel */
140                  };
141
142              The msgmni, msgmax, and msgmnb settings can be changed via /proc
143              files of the same name; see proc(5) for details.
144
145       MSG_INFO (Linux-specific)
146              Return  a  msginfo  structure containing the same information as
147              for IPC_INFO, except that the following fields are returned with
148              information  about  system resources consumed by message queues:
149              the msgpool field returns the number of message queues that cur‐
150              rently  exist  on the system; the msgmap field returns the total
151              number of messages in all queues on the system; and  the  msgtql
152              field  returns  the total number of bytes in all messages in all
153              queues on the system.
154
155       MSG_STAT (Linux-specific)
156              Return a msqid_ds structure as for IPC_STAT.  However, the msqid
157              argument  is  not  a queue identifier, but instead an index into
158              the kernel's internal array that maintains information about all
159              message queues on the system.
160
161       MSG_STAT_ANY (Linux-specific, since Linux 4.17)
162              Return   a   msqid_ds   structure  as  for  MSG_STAT.   However,
163              msg_perm.mode is not checked for read access for  msqid  meaning
164              that  any  user  can employ this operation (just as any user may
165              read /proc/sysvipc/msg to obtain the same information).
166

RETURN VALUE

168       On success, IPC_STAT, IPC_SET, and IPC_RMID  return  0.   A  successful
169       IPC_INFO  or  MSG_INFO  operation returns the index of the highest used
170       entry in the kernel's internal array recording  information  about  all
171       message  queues.   (This information can be used with repeated MSG_STAT
172       or MSG_STAT_ANY operations to obtain information about  all  queues  on
173       the  system.)   A successful MSG_STAT or MSG_STAT_ANY operation returns
174       the identifier of the queue whose index was given in msqid.
175
176       On error, -1 is returned with errno indicating the error.
177

ERRORS

179       On failure, errno is set to one of the following:
180
181       EACCES The argument cmd is equal to IPC_STAT or MSG_STAT, but the call‐
182              ing  process  does not have read permission on the message queue
183              msqid, and does not have the  CAP_IPC_OWNER  capability  in  the
184              user namespace that governs its IPC namespace.
185
186       EFAULT The  argument  cmd  has  the  value IPC_SET or IPC_STAT, but the
187              address pointed to by buf isn't accessible.
188
189       EIDRM  The message queue was removed.
190
191       EINVAL Invalid value for cmd or msqid.  Or: for a  MSG_STAT  operation,
192              the  index  value  specified  in msqid referred to an array slot
193              that is currently unused.
194
195       EPERM  The argument cmd has the value  IPC_SET  or  IPC_RMID,  but  the
196              effective  user ID of the calling process is not the creator (as
197              found in msg_perm.cuid) or the owner (as found in  msg_perm.uid)
198              of  the  message queue, and the caller is not privileged (Linux:
199              does not have the CAP_SYS_ADMIN capability).
200
201       EPERM  An attempt (IPC_SET) was made to increase msg_qbytes beyond  the
202              system  parameter  MSGMNB,  but  the  caller  is  not privileged
203              (Linux: does not have the CAP_SYS_RESOURCE capability).
204

CONFORMING TO

206       POSIX.1-2001, POSIX.1-2008, SVr4.
207

NOTES

209       The inclusion of <sys/types.h> and <sys/ipc.h> isn't required on  Linux
210       or by any version of POSIX.  However, some old implementations required
211       the inclusion of these header files, and the SVID also documented their
212       inclusion.   Applications  intended  to be portable to such old systems
213       may need to include these header files.
214
215       The IPC_INFO, MSG_STAT and MSG_INFO operations are used by the  ipcs(1)
216       program  to  provide information on allocated resources.  In the future
217       these may modified or moved to a /proc filesystem interface.
218
219       Various fields in the struct msqid_ds were typed as short  under  Linux
220       2.2 and have become long under Linux 2.4.  To take advantage of this, a
221       recompilation under glibc-2.1.91 or later should suffice.  (The  kernel
222       distinguishes old and new calls by an IPC_64 flag in cmd.)
223

SEE ALSO

225       msgget(2),   msgrcv(2),   msgsnd(2),  capabilities(7),  mq_overview(7),
226       sysvipc(7)
227

COLOPHON

229       This page is part of release 5.07 of the Linux  man-pages  project.   A
230       description  of  the project, information about reporting bugs, and the
231       latest    version    of    this    page,    can     be     found     at
232       https://www.kernel.org/doc/man-pages/.
233
234
235
236Linux                             2020-04-11                         MSGCTL(2)
Impressum