952
Chapter 46
and shmctl()) operations. (The ipcs program employs these operations.) These oper-
ations are as follows:
z
MSG_INFO
,
SEM_INFO
, and
SHM_INFO
: The
MSG_INFO
operation serves two purposes. First, it
returns a structure detailing resources consumed by all message queues on the
system. Second, as the function result of the ctl call, it returns the index of the max-
imum item in the entries array pointing to data structures for the message queue
objects (see Figure 45-1, on page 932). The
SEM_INFO
and
SHM_INFO
operations
perform an analogous task for semaphore sets and
shared memory segments,
respectively. We must define the
_GNU_SOURCE
feature test macro to obtain the defini-
tions of these three constants from the corresponding System V IPC header files.
An example showing the use of
MSG_INFO
to retrieve a msginfo structure contain-
ing information about resources used by all message
queue objects is provided
in the file
svmsg/svmsg_info.c
in the source code distribution for this book.
z
MSG_STAT
,
SEM_STAT
, and
SHM_STAT
: Like the
IPC_STAT
operation, these operations
retrieve the associated data structure for an IPC object. However, they differ in
two respects. First, instead of expecting an IPC identifier as the first argument
of the ctl call, these operations expect an index into the entries array. Second, if
the operation
is successful, then, as its function result, the ctl call returns the
identifier of the IPC object corresponding to that index. We must define the
_GNU_SOURCE
feature test macro to obtain the definitions of these three constants
from the corresponding System V IPC header files.
To list all message
queues on the system, we can do the following:
1. Use a
MSG_INFO
operation to find out the maximum index (maxind) of the entries
array for message queues.
2. Perform a loop for all values from 0 up to and including maxind, employing a
MSG_STAT
operation for each value.
During this loop, we ignore the errors that
may occur if an item of the entries array is empty (
EINVAL
) or if we don’t have
permissions on the object to which it refers (
EACCES
).
Listing 46-6 provides an implementation of the above steps for message queues.
The following shell session log demonstrates the use of this program:
$
./svmsg_ls
maxind: 4
index ID key messages
2 98306 0x00000000 0
4 163844 0x000004d2 2
$
ipcs -q
Check above against output of ipcs
------
Message Queues --------
key msqid owner perms used-bytes messages
0x00000000 98306 mtk 600 0 0
0x000004d2 163844 mtk 600 12 2