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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * --- Pre-busybox history from util-linux-2.12a ------------------------
23 * 1999-02-22 Arkadiusz Miÿkiewicz <misiek@pld.ORG.PL>
24 * - added Native Language Support
26 * Patched to display the key field -- hy@picksys.com 12/18/96
28 * Patch from arnolds@ifns.de (Heinz-Ado Arnolds) applied Mon Jul 1
29 * 19:30:41 1996 by janl@math.uio.no to add code missing in case PID:
32 * Patches from Mike Jagdis (jaggy@purplet.demon.co.uk) applied
33 * Wed Feb 8 12:12:21 1995 by faith@cs.unc.edu to print numeric uids
34 * if no passwd file entry.
36 * Modified Sat Oct 9 10:55:28 1993 for 0.99.13
37 * Original author unknown, may be "krishna balasub@cis.ohio-state.edu"
49 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
50 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
51 /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
52 #include <sys/types.h>
60 /*-------------------------------------------------------------------*/
61 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
62 but inside #ifdef __KERNEL__ ... #endif */
64 /* shm_mode upper byte flags */
65 #define SHM_DEST 01000 /* segment will be destroyed on last detach */
66 #define SHM_LOCKED 02000 /* segment will not be swapped */
69 /* For older kernels the same holds for the defines below */
80 ulong shm_tot; /* total allocated shm */
81 ulong shm_rss; /* total resident shm */
82 ulong shm_swp; /* total swapped shm */
93 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
97 /*-------------------------------------------------------------------*/
99 /* The last arg of semctl is a union semun, but where is it defined?
100 X/OPEN tells us to define it ourselves, but until recently
101 Linux include files would also define it. */
102 #if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
103 /* union semun is defined by including <sys/sem.h> */
105 /* according to X/OPEN we have to define it ourselves */
108 struct semid_ds *buf;
109 unsigned short int *array;
110 struct seminfo *__buf;
114 /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
115 libc 4/5 does not mention struct ipc_term at all, but includes
116 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
117 glibc-1.09 has no support for sysv ipc.
118 glibc 2 uses __key, __seq */
119 #if defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
131 static void do_shm (char format);
132 static void do_sem (char format);
133 static void do_msg (char format);
134 static void print_shm (int id);
135 static void print_msg (int id);
136 static void print_sem (int id);
138 int ipcs_main (int argc, char **argv) {
139 int opt, msg = 0, sem = 0, shm = 0, id=0, print=0;
141 char options[] = "atclupsmqi:ih?";
143 while ((opt = getopt (argc, argv, options)) != -1) {
179 bb_fflush_stdout_and_exit (0);
186 bb_fflush_stdout_and_exit (0);
190 bb_fflush_stdout_and_exit (0);
194 bb_fflush_stdout_and_exit (0);
197 bb_fflush_stdout_and_exit (0);
200 if ( !shm && !msg && !sem)
221 print_perms (int id, struct ipc_perm *ipcp) {
225 bb_printf ("%-10d %-10o", id, ipcp->mode & 0777);
227 if ((pw = getpwuid(ipcp->cuid)))
228 bb_printf(" %-10s", pw->pw_name);
230 bb_printf(" %-10d", ipcp->cuid);
231 if ((gr = getgrgid(ipcp->cgid)))
232 bb_printf(" %-10s", gr->gr_name);
234 bb_printf(" %-10d", ipcp->cgid);
236 if ((pw = getpwuid(ipcp->uid)))
237 bb_printf(" %-10s", pw->pw_name);
239 bb_printf(" %-10d", ipcp->uid);
240 if ((gr = getgrgid(ipcp->gid)))
241 bb_printf(" %-10s\n", gr->gr_name);
243 bb_printf(" %-10d\n", ipcp->gid);
247 void do_shm (char format)
249 int maxid, shmid, id;
250 struct shmid_ds shmseg;
251 struct shm_info shm_info;
252 struct shminfo shminfo;
253 struct ipc_perm *ipcp = &shmseg.shm_perm;
256 maxid = shmctl (0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
258 bb_printf ("kernel not configured for shared memory\n");
264 bb_printf ("------ Shared Memory Limits --------\n");
265 if ((shmctl (0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0 )
267 /* glibc 2.1.3 and all earlier libc's have ints as fields
268 of struct shminfo; glibc 2.1.91 has unsigned long; ach */
269 bb_printf ("max number of segments = %lu\n",
270 (unsigned long) shminfo.shmmni);
271 bb_printf ("max seg size (kbytes) = %lu\n",
272 (unsigned long) (shminfo.shmmax >> 10));
273 bb_printf ("max total shared memory (pages) = %lu\n",
274 (unsigned long) shminfo.shmall);
275 bb_printf ("min seg size (bytes) = %lu\n",
276 (unsigned long) shminfo.shmmin);
280 bb_printf ("------ Shared Memory Status --------\n");
281 bb_printf ("segments allocated %d\n", shm_info.used_ids);
282 bb_printf ("pages allocated %ld\n", shm_info.shm_tot);
283 bb_printf ("pages resident %ld\n", shm_info.shm_rss);
284 bb_printf ("pages swapped %ld\n", shm_info.shm_swp);
285 bb_printf ("Swap performance: %ld attempts\t %ld successes\n",
286 shm_info.swap_attempts, shm_info.swap_successes);
290 bb_printf ("------ Shared Memory Segment Creators/Owners --------\n");
291 bb_printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
292 "shmid","perms","cuid","cgid","uid","gid");
296 bb_printf ("------ Shared Memory Attach/Detach/Change Times --------\n");
297 bb_printf ("%-10s %-10s %-20s %-20s %-20s\n",
298 "shmid","owner","attached","detached","changed");
302 bb_printf ("------ Shared Memory Creator/Last-op --------\n");
303 bb_printf ("%-10s %-10s %-10s %-10s\n",
304 "shmid","owner","cpid","lpid");
308 bb_printf ("------ Shared Memory Segments --------\n");
309 bb_printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
310 "key","shmid","owner","perms","bytes","nattch","status");
314 for (id = 0; id <= maxid; id++) {
315 shmid = shmctl (id, SHM_STAT, &shmseg);
318 if (format == CREATOR) {
319 print_perms (shmid, ipcp);
322 pw = getpwuid(ipcp->uid);
326 bb_printf ("%-10d %-10.10s", shmid, pw->pw_name);
328 bb_printf ("%-10d %-10d", shmid, ipcp->uid);
329 /* ctime uses static buffer: use separate calls */
330 bb_printf(" %-20.16s", shmseg.shm_atime
331 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
332 bb_printf(" %-20.16s", shmseg.shm_dtime
333 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
334 bb_printf(" %-20.16s\n", shmseg.shm_ctime
335 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
339 bb_printf ("%-10d %-10.10s", shmid, pw->pw_name);
341 bb_printf ("%-10d %-10d", shmid, ipcp->uid);
342 bb_printf (" %-10d %-10d\n",
343 shmseg.shm_cpid, shmseg.shm_lpid);
347 bb_printf("0x%08x ",ipcp->KEY );
349 bb_printf ("%-10d %-10.10s", shmid, pw->pw_name);
351 bb_printf ("%-10d %-10d", shmid, ipcp->uid);
352 bb_printf ("%-10o %-10lu %-10ld %-6s %-6s\n",
355 * earlier: int, Austin has size_t
357 (unsigned long) shmseg.shm_segsz,
359 * glibc-2.1.3 and earlier has unsigned short;
360 * Austin has shmatt_t
362 (long) shmseg.shm_nattch,
363 ipcp->mode & SHM_DEST ? "dest" : " ",
364 ipcp->mode & SHM_LOCKED ? "locked" : " ");
372 void do_sem (char format)
374 int maxid, semid, id;
375 struct semid_ds semary;
376 struct seminfo seminfo;
377 struct ipc_perm *ipcp = &semary.sem_perm;
381 arg.array = (ushort *) (void *) &seminfo;
382 maxid = semctl (0, 0, SEM_INFO, arg);
384 bb_printf ("kernel not configured for semaphores\n");
390 bb_printf ("------ Semaphore Limits --------\n");
391 arg.array = (ushort *) (void *) &seminfo; /* damn union */
392 if ((semctl (0, 0, IPC_INFO, arg)) < 0 )
394 bb_printf ("max number of arrays = %d\n", seminfo.semmni);
395 bb_printf ("max semaphores per array = %d\n", seminfo.semmsl);
396 bb_printf ("max semaphores system wide = %d\n", seminfo.semmns);
397 bb_printf ("max ops per semop call = %d\n", seminfo.semopm);
398 bb_printf ("semaphore max value = %d\n", seminfo.semvmx);
402 bb_printf ("------ Semaphore Status --------\n");
403 bb_printf ("used arrays = %d\n", seminfo.semusz);
404 bb_printf ("allocated semaphores = %d\n", seminfo.semaem);
408 bb_printf ("------ Semaphore Arrays Creators/Owners --------\n");
409 bb_printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
410 "semid","perms","cuid","cgid","uid","gid");
414 bb_printf ("------ Shared Memory Operation/Change Times --------\n");
415 bb_printf ("%-8s %-10s %-26.24s %-26.24s\n",
416 "shmid","owner","last-op","last-changed");
423 bb_printf ("------ Semaphore Arrays --------\n");
424 bb_printf ("%-10s %-10s %-10s %-10s %-10s\n",
425 "key","semid","owner","perms","nsems");
429 for (id = 0; id <= maxid; id++) {
430 arg.buf = (struct semid_ds *) &semary;
431 semid = semctl (id, 0, SEM_STAT, arg);
434 if (format == CREATOR) {
435 print_perms (semid, ipcp);
438 pw = getpwuid(ipcp->uid);
442 bb_printf ("%-8d %-10.10s", semid, pw->pw_name);
444 bb_printf ("%-8d %-10d", semid, ipcp->uid);
445 bb_printf (" %-26.24s", semary.sem_otime
446 ? ctime(&semary.sem_otime) : "Not set");
447 bb_printf (" %-26.24s\n", semary.sem_ctime
448 ? ctime(&semary.sem_ctime) : "Not set");
454 bb_printf("0x%08x ", ipcp->KEY);
456 bb_printf ("%-10d %-10.9s", semid, pw->pw_name);
458 bb_printf ("%-10d %-9d", semid, ipcp->uid);
459 bb_printf ("%-10o %-10ld\n",
462 * glibc-2.1.3 and earlier has unsigned short;
463 * glibc-2.1.91 has variation between
464 * unsigned short and unsigned long
465 * Austin prescribes unsigned short.
467 (long) semary.sem_nsems);
474 void do_msg (char format)
476 int maxid, msqid, id;
477 struct msqid_ds msgque;
478 struct msginfo msginfo;
479 struct ipc_perm *ipcp = &msgque.msg_perm;
482 maxid = msgctl (0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
484 bb_printf ("kernel not configured for message queues\n");
490 if ((msgctl (0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0 )
492 bb_printf ("------ Messages: Limits --------\n");
493 bb_printf ("max queues system wide = %d\n", msginfo.msgmni);
494 bb_printf ("max size of message (bytes) = %d\n", msginfo.msgmax);
495 bb_printf ("default max size of queue (bytes) = %d\n", msginfo.msgmnb);
499 bb_printf ("------ Messages: Status --------\n");
500 bb_printf ("allocated queues = %d\n", msginfo.msgpool);
501 bb_printf ("used headers = %d\n", msginfo.msgmap);
502 bb_printf ("used space = %d bytes\n", msginfo.msgtql);
506 bb_printf ("------ Message Queues: Creators/Owners --------\n");
507 bb_printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
508 "msqid","perms","cuid","cgid","uid","gid");
512 bb_printf ("------ Message Queues Send/Recv/Change Times --------\n");
513 bb_printf ("%-8s %-10s %-20s %-20s %-20s\n",
514 "msqid","owner","send","recv","change");
518 bb_printf ("------ Message Queues PIDs --------\n");
519 bb_printf ("%-10s %-10s %-10s %-10s\n",
520 "msqid","owner","lspid","lrpid");
524 bb_printf ("------ Message Queues --------\n");
525 bb_printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
526 "key", "msqid", "owner", "perms","used-bytes", "messages");
530 for (id = 0; id <= maxid; id++) {
531 msqid = msgctl (id, MSG_STAT, &msgque);
534 if (format == CREATOR) {
535 print_perms (msqid, ipcp);
538 pw = getpwuid(ipcp->uid);
542 bb_printf ("%-8d %-10.10s", msqid, pw->pw_name);
544 bb_printf ("%-8d %-10d", msqid, ipcp->uid);
545 bb_printf (" %-20.16s", msgque.msg_stime
546 ? ctime(&msgque.msg_stime) + 4 : "Not set");
547 bb_printf (" %-20.16s", msgque.msg_rtime
548 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
549 bb_printf (" %-20.16s\n", msgque.msg_ctime
550 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
554 bb_printf ("%-8d %-10.10s", msqid, pw->pw_name);
556 bb_printf ("%-8d %-10d", msqid, ipcp->uid);
557 bb_printf (" %5d %5d\n",
558 msgque.msg_lspid, msgque.msg_lrpid);
562 bb_printf( "0x%08x ",ipcp->KEY );
564 bb_printf ("%-10d %-10.10s", msqid, pw->pw_name);
566 bb_printf ("%-10d %-10d", msqid, ipcp->uid);
567 bb_printf (" %-10o %-12ld %-12ld\n",
570 * glibc-2.1.3 and earlier has unsigned short;
571 * glibc-2.1.91 has variation between
572 * unsigned short, unsigned long
573 * Austin has msgqnum_t
575 (long) msgque.msg_cbytes,
576 (long) msgque.msg_qnum);
584 void print_shm (int shmid)
586 struct shmid_ds shmds;
587 struct ipc_perm *ipcp = &shmds.shm_perm;
589 if (shmctl (shmid, IPC_STAT, &shmds) == -1) {
594 bb_printf ("\nShared memory Segment shmid=%d\n", shmid);
595 bb_printf ("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n",
596 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid);
597 bb_printf ("mode=%#o\taccess_perms=%#o\n",
598 ipcp->mode, ipcp->mode & 0777);
599 bb_printf ("bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
600 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
601 (long) shmds.shm_nattch);
602 bb_printf ("att_time=%-26.24s\n",
603 shmds.shm_atime ? ctime (&shmds.shm_atime) : "Not set");
604 bb_printf ("det_time=%-26.24s\n",
605 shmds.shm_dtime ? ctime (&shmds.shm_dtime) : "Not set");
606 bb_printf ("change_time=%-26.24s\n", ctime (&shmds.shm_ctime));
612 void print_msg (int msqid)
615 struct ipc_perm *ipcp = &buf.msg_perm;
617 if (msgctl (msqid, IPC_STAT, &buf) == -1) {
622 bb_printf ("\nMessage Queue msqid=%d\n", msqid);
623 bb_printf ("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n",
624 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode);
625 bb_printf ("cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
627 * glibc-2.1.3 and earlier has unsigned short;
628 * glibc-2.1.91 has variation between
629 * unsigned short, unsigned long
630 * Austin has msgqnum_t (for msg_qbytes)
632 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
633 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
634 bb_printf ("send_time=%-26.24s\n",
635 buf.msg_stime ? ctime (&buf.msg_stime) : "Not set");
636 bb_printf ("rcv_time=%-26.24s\n",
637 buf.msg_rtime ? ctime (&buf.msg_rtime) : "Not set");
638 bb_printf ("change_time=%-26.24s\n",
639 buf.msg_ctime ? ctime (&buf.msg_ctime) : "Not set");
644 void print_sem (int semid)
646 struct semid_ds semds;
647 struct ipc_perm *ipcp = &semds.sem_perm;
652 if (semctl (semid, 0, IPC_STAT, arg) < 0) {
657 bb_printf ("\nSemaphore Array semid=%d\n", semid);
658 bb_printf ("uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n",
659 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid);
660 bb_printf ("mode=%#o, access_perms=%#o\n",
661 ipcp->mode, ipcp->mode & 0777);
662 bb_printf ("nsems = %ld\n", (long) semds.sem_nsems);
663 bb_printf ("otime = %-26.24s\n",
664 semds.sem_otime ? ctime (&semds.sem_otime) : "Not set");
665 bb_printf ("ctime = %-26.24s\n", ctime (&semds.sem_ctime));
667 bb_printf ("%-10s %-10s %-10s %-10s %-10s\n",
668 "semnum","value","ncount","zcount","pid");
671 for (i=0; i< semds.sem_nsems; i++) {
672 int val, ncnt, zcnt, pid;
673 val = semctl (semid, i, GETVAL, arg);
674 ncnt = semctl (semid, i, GETNCNT, arg);
675 zcnt = semctl (semid, i, GETZCNT, arg);
676 pid = semctl (semid, i, GETPID, arg);
677 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
679 bb_fflush_stdout_and_exit (1);
681 bb_printf ("%-10d %-10d %-10d %-10d %-10d\n",
682 i, val, ncnt, zcnt, pid);