1 /* vi: set sw=4 ts=4: */
3 * ipcs.c -- provides information on allocated ipc resources.
5 * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
6 * Adapted for busybox from util-linux-2.12a.
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
19 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
20 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
21 /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
22 #include <sys/types.h>
30 /*-------------------------------------------------------------------*/
31 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
32 but inside #ifdef __KERNEL__ ... #endif */
34 /* shm_mode upper byte flags */
35 #define SHM_DEST 01000 /* segment will be destroyed on last detach */
36 #define SHM_LOCKED 02000 /* segment will not be swapped */
39 /* For older kernels the same holds for the defines below */
50 ulong shm_tot; /* total allocated shm */
51 ulong shm_rss; /* total resident shm */
52 ulong shm_swp; /* total swapped shm */
63 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
67 /*-------------------------------------------------------------------*/
69 /* The last arg of semctl is a union semun, but where is it defined?
70 X/OPEN tells us to define it ourselves, but until recently
71 Linux include files would also define it. */
72 #if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
73 /* union semun is defined by including <sys/sem.h> */
75 /* according to X/OPEN we have to define it ourselves */
79 unsigned short int *array;
80 struct seminfo *__buf;
84 /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
85 libc 4/5 does not mention struct ipc_term at all, but includes
86 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
87 glibc-1.09 has no support for sysv ipc.
88 glibc 2 uses __key, __seq */
89 #if defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
102 static void print_perms (int id, struct ipc_perm *ipcp) {
106 bb_printf ("%-10d %-10o", id, ipcp->mode & 0777);
108 if ((pw = getpwuid(ipcp->cuid)))
109 bb_printf(" %-10s", pw->pw_name);
111 bb_printf(" %-10d", ipcp->cuid);
112 if ((gr = getgrgid(ipcp->cgid)))
113 bb_printf(" %-10s", gr->gr_name);
115 bb_printf(" %-10d", ipcp->cgid);
117 if ((pw = getpwuid(ipcp->uid)))
118 bb_printf(" %-10s", pw->pw_name);
120 bb_printf(" %-10d", ipcp->uid);
121 if ((gr = getgrgid(ipcp->gid)))
122 bb_printf(" %-10s\n", gr->gr_name);
124 bb_printf(" %-10d\n", ipcp->gid);
128 static void do_shm (char format)
130 int maxid, shmid, id;
131 struct shmid_ds shmseg;
132 struct shm_info shm_info;
133 struct shminfo shminfo;
134 struct ipc_perm *ipcp = &shmseg.shm_perm;
137 maxid = shmctl (0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
139 bb_printf ("kernel not configured for shared memory\n");
145 bb_printf ("------ Shared Memory Limits --------\n");
146 if ((shmctl (0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0 )
148 /* glibc 2.1.3 and all earlier libc's have ints as fields
149 of struct shminfo; glibc 2.1.91 has unsigned long; ach */
150 bb_printf ("max number of segments = %lu\n"
151 "max seg size (kbytes) = %lu\n"
152 "max total shared memory (pages) = %lu\n"
153 "min seg size (bytes) = %lu\n",
154 (unsigned long) shminfo.shmmni,
155 (unsigned long) (shminfo.shmmax >> 10),
156 (unsigned long) shminfo.shmall,
157 (unsigned long) shminfo.shmmin);
161 bb_printf ("------ Shared Memory Status --------\n"
162 "segments allocated %d\n"
163 "pages allocated %ld\n"
164 "pages resident %ld\n"
165 "pages swapped %ld\n"
166 "Swap performance: %ld attempts\t %ld successes\n",
171 shm_info.swap_attempts, shm_info.swap_successes);
175 bb_printf ("------ Shared Memory Segment Creators/Owners --------\n"
176 "%-10s %-10s %-10s %-10s %-10s %-10s\n",
177 "shmid","perms","cuid","cgid","uid","gid");
181 bb_printf ("------ Shared Memory Attach/Detach/Change Times --------\n"
182 "%-10s %-10s %-20s %-20s %-20s\n",
183 "shmid","owner","attached","detached","changed");
187 bb_printf ("------ Shared Memory Creator/Last-op --------\n"
188 "%-10s %-10s %-10s %-10s\n",
189 "shmid","owner","cpid","lpid");
193 bb_printf ("------ Shared Memory Segments --------\n"
194 "%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
195 "key","shmid","owner","perms","bytes","nattch","status");
199 for (id = 0; id <= maxid; id++) {
200 shmid = shmctl (id, SHM_STAT, &shmseg);
203 if (format == CREATOR) {
204 print_perms (shmid, ipcp);
207 pw = getpwuid(ipcp->uid);
211 bb_printf ("%-10d %-10.10s", shmid, pw->pw_name);
213 bb_printf ("%-10d %-10d", shmid, ipcp->uid);
214 /* ctime uses static buffer: use separate calls */
215 bb_printf(" %-20.16s", shmseg.shm_atime
216 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
217 bb_printf(" %-20.16s", shmseg.shm_dtime
218 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
219 bb_printf(" %-20.16s\n", shmseg.shm_ctime
220 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
224 bb_printf ("%-10d %-10.10s", shmid, pw->pw_name);
226 bb_printf ("%-10d %-10d", shmid, ipcp->uid);
227 bb_printf (" %-10d %-10d\n",
228 shmseg.shm_cpid, shmseg.shm_lpid);
232 bb_printf("0x%08x ",ipcp->KEY );
234 bb_printf ("%-10d %-10.10s", shmid, pw->pw_name);
236 bb_printf ("%-10d %-10d", shmid, ipcp->uid);
237 bb_printf ("%-10o %-10lu %-10ld %-6s %-6s\n",
240 * earlier: int, Austin has size_t
242 (unsigned long) shmseg.shm_segsz,
244 * glibc-2.1.3 and earlier has unsigned short;
245 * Austin has shmatt_t
247 (long) shmseg.shm_nattch,
248 ipcp->mode & SHM_DEST ? "dest" : " ",
249 ipcp->mode & SHM_LOCKED ? "locked" : " ");
257 static void do_sem (char format)
259 int maxid, semid, id;
260 struct semid_ds semary;
261 struct seminfo seminfo;
262 struct ipc_perm *ipcp = &semary.sem_perm;
266 arg.array = (ushort *) (void *) &seminfo;
267 maxid = semctl (0, 0, SEM_INFO, arg);
269 bb_printf ("kernel not configured for semaphores\n");
275 bb_printf ("------ Semaphore Limits --------\n");
276 arg.array = (ushort *) (void *) &seminfo; /* damn union */
277 if ((semctl (0, 0, IPC_INFO, arg)) < 0 )
279 bb_printf ("max number of arrays = %d\n"
280 "max semaphores per array = %d\n"
281 "max semaphores system wide = %d\n"
282 "max ops per semop call = %d\n"
283 "semaphore max value = %d\n",
292 bb_printf ("------ Semaphore Status --------\n"
294 "allocated semaphores = %d\n",
300 bb_printf ("------ Semaphore Arrays Creators/Owners --------\n"
301 "%-10s %-10s %-10s %-10s %-10s %-10s\n",
302 "semid","perms","cuid","cgid","uid","gid");
306 bb_printf ("------ Shared Memory Operation/Change Times --------\n"
307 "%-8s %-10s %-26.24s %-26.24s\n",
308 "shmid","owner","last-op","last-changed");
315 bb_printf ("------ Semaphore Arrays --------\n"
316 "%-10s %-10s %-10s %-10s %-10s\n",
317 "key","semid","owner","perms","nsems");
321 for (id = 0; id <= maxid; id++) {
322 arg.buf = (struct semid_ds *) &semary;
323 semid = semctl (id, 0, SEM_STAT, arg);
326 if (format == CREATOR) {
327 print_perms (semid, ipcp);
330 pw = getpwuid(ipcp->uid);
334 bb_printf ("%-8d %-10.10s", semid, pw->pw_name);
336 bb_printf ("%-8d %-10d", semid, ipcp->uid);
337 bb_printf (" %-26.24s", semary.sem_otime
338 ? ctime(&semary.sem_otime) : "Not set");
339 bb_printf (" %-26.24s\n", semary.sem_ctime
340 ? ctime(&semary.sem_ctime) : "Not set");
346 bb_printf("0x%08x ", ipcp->KEY);
348 bb_printf ("%-10d %-10.9s", semid, pw->pw_name);
350 bb_printf ("%-10d %-9d", semid, ipcp->uid);
351 bb_printf ("%-10o %-10ld\n",
354 * glibc-2.1.3 and earlier has unsigned short;
355 * glibc-2.1.91 has variation between
356 * unsigned short and unsigned long
357 * Austin prescribes unsigned short.
359 (long) semary.sem_nsems);
366 static void do_msg (char format)
368 int maxid, msqid, id;
369 struct msqid_ds msgque;
370 struct msginfo msginfo;
371 struct ipc_perm *ipcp = &msgque.msg_perm;
374 maxid = msgctl (0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
376 bb_printf ("kernel not configured for message queues\n");
382 if ((msgctl (0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0 )
384 bb_printf ("------ Messages: Limits --------\n"
385 "max queues system wide = %d\n"
386 "max size of message (bytes) = %d\n"
387 "default max size of queue (bytes) = %d\n",
394 bb_printf ("------ Messages: Status --------\n"
395 "allocated queues = %d\n"
396 "used headers = %d\n"
397 "used space = %d bytes\n",
404 bb_printf ("------ Message Queues: Creators/Owners --------\n"
405 "%-10s %-10s %-10s %-10s %-10s %-10s\n",
406 "msqid","perms","cuid","cgid","uid","gid");
410 bb_printf ("------ Message Queues Send/Recv/Change Times --------\n"
411 "%-8s %-10s %-20s %-20s %-20s\n",
412 "msqid","owner","send","recv","change");
416 bb_printf ("------ Message Queues PIDs --------\n"
417 "%-10s %-10s %-10s %-10s\n",
418 "msqid","owner","lspid","lrpid");
422 bb_printf ("------ Message Queues --------\n"
423 "%-10s %-10s %-10s %-10s %-12s %-12s\n",
424 "key","msqid","owner","perms","used-bytes","messages");
428 for (id = 0; id <= maxid; id++) {
429 msqid = msgctl (id, MSG_STAT, &msgque);
432 if (format == CREATOR) {
433 print_perms (msqid, ipcp);
436 pw = getpwuid(ipcp->uid);
440 bb_printf ("%-8d %-10.10s", msqid, pw->pw_name);
442 bb_printf ("%-8d %-10d", msqid, ipcp->uid);
443 bb_printf (" %-20.16s", msgque.msg_stime
444 ? ctime(&msgque.msg_stime) + 4 : "Not set");
445 bb_printf (" %-20.16s", msgque.msg_rtime
446 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
447 bb_printf (" %-20.16s\n", msgque.msg_ctime
448 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
452 bb_printf ("%-8d %-10.10s", msqid, pw->pw_name);
454 bb_printf ("%-8d %-10d", msqid, ipcp->uid);
455 bb_printf (" %5d %5d\n",
456 msgque.msg_lspid, msgque.msg_lrpid);
460 bb_printf( "0x%08x ",ipcp->KEY );
462 bb_printf ("%-10d %-10.10s", msqid, pw->pw_name);
464 bb_printf ("%-10d %-10d", msqid, ipcp->uid);
465 bb_printf (" %-10o %-12ld %-12ld\n",
468 * glibc-2.1.3 and earlier has unsigned short;
469 * glibc-2.1.91 has variation between
470 * unsigned short, unsigned long
471 * Austin has msgqnum_t
473 (long) msgque.msg_cbytes,
474 (long) msgque.msg_qnum);
482 static void print_shm (int shmid)
484 struct shmid_ds shmds;
485 struct ipc_perm *ipcp = &shmds.shm_perm;
487 if (shmctl (shmid, IPC_STAT, &shmds) == -1) {
492 bb_printf ("\nShared memory Segment shmid=%d\n"
493 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
494 "mode=%#o\taccess_perms=%#o\n"
495 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n"
496 "att_time=%-26.24s\n"
497 "det_time=%-26.24s\n"
498 "change_time=%-26.24s\n"
501 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
502 ipcp->mode, ipcp->mode & 0777,
503 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
504 (long) shmds.shm_nattch,
505 shmds.shm_atime ? ctime (&shmds.shm_atime) : "Not set",
506 shmds.shm_dtime ? ctime (&shmds.shm_dtime) : "Not set",
507 ctime (&shmds.shm_ctime));
512 static void print_msg (int msqid)
515 struct ipc_perm *ipcp = &buf.msg_perm;
517 if (msgctl (msqid, IPC_STAT, &buf) == -1) {
522 bb_printf ("\nMessage Queue msqid=%d\n"
523 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
524 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n"
525 "send_time=%-26.24s\n"
526 "rcv_time=%-26.24s\n"
527 "change_time=%-26.24s\n"
530 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
532 * glibc-2.1.3 and earlier has unsigned short;
533 * glibc-2.1.91 has variation between
534 * unsigned short, unsigned long
535 * Austin has msgqnum_t (for msg_qbytes)
537 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
538 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid,
539 buf.msg_stime ? ctime (&buf.msg_stime) : "Not set",
540 buf.msg_rtime ? ctime (&buf.msg_rtime) : "Not set",
541 buf.msg_ctime ? ctime (&buf.msg_ctime) : "Not set");
545 static void print_sem (int semid)
547 struct semid_ds semds;
548 struct ipc_perm *ipcp = &semds.sem_perm;
553 if (semctl (semid, 0, IPC_STAT, arg) < 0) {
558 bb_printf ("\nSemaphore Array semid=%d\n"
559 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
560 "mode=%#o, access_perms=%#o\n"
564 "%-10s %-10s %-10s %-10s %-10s\n",
566 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
567 ipcp->mode, ipcp->mode & 0777,
568 (long) semds.sem_nsems,
569 semds.sem_otime ? ctime (&semds.sem_otime) : "Not set",
570 ctime (&semds.sem_ctime),
571 "semnum","value","ncount","zcount","pid");
574 for (i=0; i < semds.sem_nsems; i++) {
575 int val, ncnt, zcnt, pid;
576 val = semctl (semid, i, GETVAL, arg);
577 ncnt = semctl (semid, i, GETNCNT, arg);
578 zcnt = semctl (semid, i, GETZCNT, arg);
579 pid = semctl (semid, i, GETPID, arg);
580 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
582 bb_fflush_stdout_and_exit (1);
584 bb_printf ("%-10d %-10d %-10d %-10d %-10d\n",
585 i, val, ncnt, zcnt, pid);
591 int ipcs_main (int argc, char **argv) {
592 int opt, msg = 0, sem = 0, shm = 0, id=0, print=0;
594 char options[] = "atclupsmqi:ih?";
596 while ((opt = getopt (argc, argv, options)) != -1) {
632 bb_fflush_stdout_and_exit (0);
639 bb_fflush_stdout_and_exit (0);
643 bb_fflush_stdout_and_exit (0);
647 bb_fflush_stdout_and_exit (0);
650 bb_fflush_stdout_and_exit (0);
653 if ( !shm && !msg && !sem)