04-22-04 08:36 AM
On Wed, 21 Apr 2004 07:30:57 -0400, Dragan Cvetkovic <me@privacy.net>
wrote:
>amujoo@yahoo.com (Ash) writes:
>
>
>
>Strange. msgids(2) is available on Solaris 8 systems I have access to. What
>revision of it you have? I have no ideas about Linux though.
(It's also available on the Solaris 8 systems that I can access.)
The OP seems intent on not listening to advice or answering
questions ;-) : parsing the output of ipcs is *the* most portable way
of solving this problem.
It appears from the above post that the OP is interested primarily in
Solaris and Linux. Dragan has posted the Solaris-specific alternative
to parsing ipcs output.
On Linux, there are two different ways of solving this problem:
-- read the contents of /proc/sysvipc/msg (Linux 2.4 and later)
-- make use of the IPC_INFO and MSG_STAT msgctl() operations
Both of the above methods are *not* portable. The second of these
techniques is shown below.
Cheers,
Michael
/* List all System V message queues */
#define _GNU_SOURCE
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define errMsg(msg) { perror(msg); }
#define errExit(msg) { perror(msg); exit(EXIT_FAILURE); }
int
main(int argc, char *argv[])
{
int maxind, ind, msqid;
struct msqid_ds ds;
struct msginfo msginfo;
/* Obtain size of kernel 'entries' array */
maxind = msgctl(0, IPC_INFO, (struct msqid_ds *) &msginfo);
if (maxind == -1) errExit("msgctl");
printf("maxind: %d\n\n", maxind);
printf("index id key messages\n");
for (ind = 0; ind <= maxind; ind++) {
msqid = msgctl(ind, MSG_STAT, &ds);
if (msqid == -1) {
if (errno != EINVAL && errno != EACCES)
errMsg("msgctl-MSG_STAT"); /* Unexpected error */
continue; /* Ignore this item */
}
printf("%4d %8d 0x%08x %7ld\n", ind, msqid,
ds.msg_perm.__key, (long) ds.msg_qnum);
}
exit(EXIT_SUCCESS);
} /* main */
[ Post a follow-up to this message ]
|