2 * ipcs.c -- provides information on allocated ipc resources.
4 * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
5 * Adapted for busybox from util-linux-2.12a.
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
18 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
19 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
20 /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
21 #include <sys/types.h>
29 /*-------------------------------------------------------------------*/
30 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
31 but inside #ifdef __KERNEL__ ... #endif */
33 /* shm_mode upper byte flags */
34 #define SHM_DEST 01000 /* segment will be destroyed on last detach */
35 #define SHM_LOCKED 02000 /* segment will not be swapped */
38 /* For older kernels the same holds for the defines below */
49 ulong shm_tot; /* total allocated shm */
50 ulong shm_rss; /* total resident shm */
51 ulong shm_swp; /* total swapped shm */
62 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
66 /*-------------------------------------------------------------------*/
68 /* The last arg of semctl is a union semun, but where is it defined?
69 X/OPEN tells us to define it ourselves, but until recently
70 Linux include files would also define it. */
71 #if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
72 /* union semun is defined by including <sys/sem.h> */
74 /* according to X/OPEN we have to define it ourselves */
78 unsigned short int *array;
79 struct seminfo *__buf;
83 /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
84 libc 4/5 does not mention struct ipc_term at all, but includes
85 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
86 glibc-1.09 has no support for sysv ipc.
87 glibc 2 uses __key, __seq */
88 #if defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
101 static void print_perms (int id, struct ipc_perm *ipcp) {
105 bb_printf ("%-10d %-10o", id, ipcp->mode & 0777);
107 if ((pw = getpwuid(ipcp->cuid)))
108 bb_printf(" %-10s", pw->pw_name);
110 bb_printf(" %-10d", ipcp->cuid);
111 if ((gr = getgrgid(ipcp->cgid)))
112 bb_printf(" %-10s", gr->gr_name);
114 bb_printf(" %-10d", ipcp->cgid);
116 if ((pw = getpwuid(ipcp->uid)))
117 bb_printf(" %-10s", pw->pw_name);
119 bb_printf(" %-10d", ipcp->uid);
120 if ((gr = getgrgid(ipcp->gid)))
121 bb_printf(" %-10s\n", gr->gr_name);
123 bb_printf(" %-10d\n", ipcp->gid);
127 static void do_shm (char format)
129 int maxid, shmid, id;
130 struct shmid_ds shmseg;
131 struct shm_info shm_info;
132 struct shminfo shminfo;
133 struct ipc_perm *ipcp = &shmseg.shm_perm;
136 maxid = shmctl (0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
138 bb_printf ("kernel not configured for shared memory\n");
144 bb_printf ("------ Shared Memory Limits --------\n");
145 if ((shmctl (0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0 )
147 /* glibc 2.1.3 and all earlier libc's have ints as fields
148 of struct shminfo; glibc 2.1.91 has unsigned long; ach */
149 bb_printf ("max number of segments = %lu\n"
150 "max seg size (kbytes) = %lu\n"
151 "max total shared memory (pages) = %lu\n"
152 "min seg size (bytes) = %lu\n",
153 (unsigned long) shminfo.shmmni,
154 (unsigned long) (shminfo.shmmax >> 10),
155 (unsigned long) shminfo.shmall,
156 (unsigned long) shminfo.shmmin);
160 bb_printf ("------ Shared Memory Status --------\n"
161 "segments allocated %d\n"
162 "pages allocated %ld\n"
163 "pages resident %ld\n"
164 "pages swapped %ld\n"
165 "Swap performance: %ld attempts\t %ld successes\n",
170 shm_info.swap_attempts, shm_info.swap_successes);
174 bb_printf ("------ Shared Memory Segment Creators/Owners --------\n"
175 "%-10s %-10s %-10s %-10s %-10s %-10s\n",
176 "shmid","perms","cuid","cgid","uid","gid");
180 bb_printf ("------ Shared Memory Attach/Detach/Change Times --------\n"
181 "%-10s %-10s %-20s %-20s %-20s\n",
182 "shmid","owner","attached","detached","changed");
186 bb_printf ("------ Shared Memory Creator/Last-op --------\n"
187 "%-10s %-10s %-10s %-10s\n",
188 "shmid","owner","cpid","lpid");
192 bb_printf ("------ Shared Memory Segments --------\n"
193 "%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
194 "key","shmid","owner","perms","bytes","nattch","status");
198 for (id = 0; id <= maxid; id++) {
199 shmid = shmctl (id, SHM_STAT, &shmseg);
202 if (format == CREATOR) {
203 print_perms (shmid, ipcp);
206 pw = getpwuid(ipcp->uid);
210 bb_printf ("%-10d %-10.10s", shmid, pw->pw_name);
212 bb_printf ("%-10d %-10d", shmid, ipcp->uid);
213 /* ctime uses static buffer: use separate calls */
214 bb_printf(" %-20.16s", shmseg.shm_atime
215 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
216 bb_printf(" %-20.16s", shmseg.shm_dtime
217 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
218 bb_printf(" %-20.16s\n", shmseg.shm_ctime
219 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
223 bb_printf ("%-10d %-10.10s", shmid, pw->pw_name);
225 bb_printf ("%-10d %-10d", shmid, ipcp->uid);
226 bb_printf (" %-10d %-10d\n",
227 shmseg.shm_cpid, shmseg.shm_lpid);
231 bb_printf("0x%08x ",ipcp->KEY );
233 bb_printf ("%-10d %-10.10s", shmid, pw->pw_name);
235 bb_printf ("%-10d %-10d", shmid, ipcp->uid);
236 bb_printf ("%-10o %-10lu %-10ld %-6s %-6s\n",
239 * earlier: int, Austin has size_t
241 (unsigned long) shmseg.shm_segsz,
243 * glibc-2.1.3 and earlier has unsigned short;
244 * Austin has shmatt_t
246 (long) shmseg.shm_nattch,
247 ipcp->mode & SHM_DEST ? "dest" : " ",
248 ipcp->mode & SHM_LOCKED ? "locked" : " ");
256 static void do_sem (char format)
258 int maxid, semid, id;
259 struct semid_ds semary;
260 struct seminfo seminfo;
261 struct ipc_perm *ipcp = &semary.sem_perm;
265 arg.array = (ushort *) (void *) &seminfo;
266 maxid = semctl (0, 0, SEM_INFO, arg);
268 bb_printf ("kernel not configured for semaphores\n");
274 bb_printf ("------ Semaphore Limits --------\n");
275 arg.array = (ushort *) (void *) &seminfo; /* damn union */
276 if ((semctl (0, 0, IPC_INFO, arg)) < 0 )
278 bb_printf ("max number of arrays = %d\n"
279 "max semaphores per array = %d\n"
280 "max semaphores system wide = %d\n"
281 "max ops per semop call = %d\n"
282 "semaphore max value = %d\n",
291 bb_printf ("------ Semaphore Status --------\n"
293 "allocated semaphores = %d\n",
299 bb_printf ("------ Semaphore Arrays Creators/Owners --------\n"
300 "%-10s %-10s %-10s %-10s %-10s %-10s\n",
301 "semid","perms","cuid","cgid","uid","gid");
305 bb_printf ("------ Shared Memory Operation/Change Times --------\n"
306 "%-8s %-10s %-26.24s %-26.24s\n",
307 "shmid","owner","last-op","last-changed");
314 bb_printf ("------ Semaphore Arrays --------\n"
315 "%-10s %-10s %-10s %-10s %-10s\n",
316 "key","semid","owner","perms","nsems");
320 for (id = 0; id <= maxid; id++) {
321 arg.buf = (struct semid_ds *) &semary;
322 semid = semctl (id, 0, SEM_STAT, arg);
325 if (format == CREATOR) {
326 print_perms (semid, ipcp);
329 pw = getpwuid(ipcp->uid);
333 bb_printf ("%-8d %-10.10s", semid, pw->pw_name);
335 bb_printf ("%-8d %-10d", semid, ipcp->uid);
336 bb_printf (" %-26.24s", semary.sem_otime
337 ? ctime(&semary.sem_otime) : "Not set");
338 bb_printf (" %-26.24s\n", semary.sem_ctime
339 ? ctime(&semary.sem_ctime) : "Not set");
345 bb_printf("0x%08x ", ipcp->KEY);
347 bb_printf ("%-10d %-10.9s", semid, pw->pw_name);
349 bb_printf ("%-10d %-9d", semid, ipcp->uid);
350 bb_printf ("%-10o %-10ld\n",
353 * glibc-2.1.3 and earlier has unsigned short;
354 * glibc-2.1.91 has variation between
355 * unsigned short and unsigned long
356 * Austin prescribes unsigned short.
358 (long) semary.sem_nsems);
365 static void do_msg (char format)
367 int maxid, msqid, id;
368 struct msqid_ds msgque;
369 struct msginfo msginfo;
370 struct ipc_perm *ipcp = &msgque.msg_perm;
373 maxid = msgctl (0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
375 bb_printf ("kernel not configured for message queues\n");
381 if ((msgctl (0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0 )
383 bb_printf ("------ Messages: Limits --------\n"
384 "max queues system wide = %d\n"
385 "max size of message (bytes) = %d\n"
386 "default max size of queue (bytes) = %d\n",
393 bb_printf ("------ Messages: Status --------\n"
394 "allocated queues = %d\n"
395 "used headers = %d\n"
396 "used space = %d bytes\n",
403 bb_printf ("------ Message Queues: Creators/Owners --------\n"
404 "%-10s %-10s %-10s %-10s %-10s %-10s\n",
405 "msqid","perms","cuid","cgid","uid","gid");
409 bb_printf ("------ Message Queues Send/Recv/Change Times --------\n"
410 "%-8s %-10s %-20s %-20s %-20s\n",
411 "msqid","owner","send","recv","change");
415 bb_printf ("------ Message Queues PIDs --------\n"
416 "%-10s %-10s %-10s %-10s\n",
417 "msqid","owner","lspid","lrpid");
421 bb_printf ("------ Message Queues --------\n"
422 "%-10s %-10s %-10s %-10s %-12s %-12s\n",
423 "key","msqid","owner","perms","used-bytes","messages");
427 for (id = 0; id <= maxid; id++) {
428 msqid = msgctl (id, MSG_STAT, &msgque);
431 if (format == CREATOR) {
432 print_perms (msqid, ipcp);
435 pw = getpwuid(ipcp->uid);
439 bb_printf ("%-8d %-10.10s", msqid, pw->pw_name);
441 bb_printf ("%-8d %-10d", msqid, ipcp->uid);
442 bb_printf (" %-20.16s", msgque.msg_stime
443 ? ctime(&msgque.msg_stime) + 4 : "Not set");
444 bb_printf (" %-20.16s", msgque.msg_rtime
445 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
446 bb_printf (" %-20.16s\n", msgque.msg_ctime
447 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
451 bb_printf ("%-8d %-10.10s", msqid, pw->pw_name);
453 bb_printf ("%-8d %-10d", msqid, ipcp->uid);
454 bb_printf (" %5d %5d\n",
455 msgque.msg_lspid, msgque.msg_lrpid);
459 bb_printf( "0x%08x ",ipcp->KEY );
461 bb_printf ("%-10d %-10.10s", msqid, pw->pw_name);
463 bb_printf ("%-10d %-10d", msqid, ipcp->uid);
464 bb_printf (" %-10o %-12ld %-12ld\n",
467 * glibc-2.1.3 and earlier has unsigned short;
468 * glibc-2.1.91 has variation between
469 * unsigned short, unsigned long
470 * Austin has msgqnum_t
472 (long) msgque.msg_cbytes,
473 (long) msgque.msg_qnum);
481 static void print_shm (int shmid)
483 struct shmid_ds shmds;
484 struct ipc_perm *ipcp = &shmds.shm_perm;
486 if (shmctl (shmid, IPC_STAT, &shmds) == -1) {
491 bb_printf ("\nShared memory Segment shmid=%d\n"
492 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
493 "mode=%#o\taccess_perms=%#o\n"
494 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n"
495 "att_time=%-26.24s\n"
496 "det_time=%-26.24s\n"
497 "change_time=%-26.24s\n"
500 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
501 ipcp->mode, ipcp->mode & 0777,
502 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
503 (long) shmds.shm_nattch,
504 shmds.shm_atime ? ctime (&shmds.shm_atime) : "Not set",
505 shmds.shm_dtime ? ctime (&shmds.shm_dtime) : "Not set",
506 ctime (&shmds.shm_ctime));
511 static void print_msg (int msqid)
514 struct ipc_perm *ipcp = &buf.msg_perm;
516 if (msgctl (msqid, IPC_STAT, &buf) == -1) {
521 bb_printf ("\nMessage Queue msqid=%d\n"
522 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
523 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n"
524 "send_time=%-26.24s\n"
525 "rcv_time=%-26.24s\n"
526 "change_time=%-26.24s\n"
529 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
531 * glibc-2.1.3 and earlier has unsigned short;
532 * glibc-2.1.91 has variation between
533 * unsigned short, unsigned long
534 * Austin has msgqnum_t (for msg_qbytes)
536 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
537 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid,
538 buf.msg_stime ? ctime (&buf.msg_stime) : "Not set",
539 buf.msg_rtime ? ctime (&buf.msg_rtime) : "Not set",
540 buf.msg_ctime ? ctime (&buf.msg_ctime) : "Not set");
544 static void print_sem (int semid)
546 struct semid_ds semds;
547 struct ipc_perm *ipcp = &semds.sem_perm;
552 if (semctl (semid, 0, IPC_STAT, arg) < 0) {
557 bb_printf ("\nSemaphore Array semid=%d\n"
558 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
559 "mode=%#o, access_perms=%#o\n"
563 "%-10s %-10s %-10s %-10s %-10s\n",
565 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
566 ipcp->mode, ipcp->mode & 0777,
567 (long) semds.sem_nsems,
568 semds.sem_otime ? ctime (&semds.sem_otime) : "Not set",
569 ctime (&semds.sem_ctime),
570 "semnum","value","ncount","zcount","pid");
573 for (i=0; i < semds.sem_nsems; i++) {
574 int val, ncnt, zcnt, pid;
575 val = semctl (semid, i, GETVAL, arg);
576 ncnt = semctl (semid, i, GETNCNT, arg);
577 zcnt = semctl (semid, i, GETZCNT, arg);
578 pid = semctl (semid, i, GETPID, arg);
579 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
581 bb_fflush_stdout_and_exit (1);
583 bb_printf ("%-10d %-10d %-10d %-10d %-10d\n",
584 i, val, ncnt, zcnt, pid);
590 int ipcs_main (int argc, char **argv) {
591 int opt, msg = 0, sem = 0, shm = 0, id=0, print=0;
593 char options[] = "atclupsmqi:ih?";
595 while ((opt = getopt (argc, argv, options)) != -1) {
631 bb_fflush_stdout_and_exit (0);
638 bb_fflush_stdout_and_exit (0);
642 bb_fflush_stdout_and_exit (0);
646 bb_fflush_stdout_and_exit (0);
649 bb_fflush_stdout_and_exit (0);
652 if ( !shm && !msg && !sem)