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 source tree.
11 //config: bool "ipcs (11 kb)"
13 //config: select PLATFORM_LINUX
15 //config: The ipcs utility is used to provide information on the currently
16 //config: allocated System V interprocess (IPC) objects in the system.
18 //applet:IF_IPCS(APPLET_NOEXEC(ipcs, ipcs, BB_DIR_USR_BIN, BB_SUID_DROP, ipcs))
20 //kbuild:lib-$(CONFIG_IPCS) += ipcs.o
22 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
23 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
24 /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
25 #include <sys/types.h>
33 /*-------------------------------------------------------------------*/
34 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
35 but inside #ifdef __KERNEL__ ... #endif */
37 /* shm_mode upper byte flags */
38 #define SHM_DEST 01000 /* segment will be destroyed on last detach */
39 #define SHM_LOCKED 02000 /* segment will not be swapped */
42 /* For older kernels the same holds for the defines below */
53 unsigned long shm_tot; /* total allocated shm */
54 unsigned long shm_rss; /* total resident shm */
55 unsigned long shm_swp; /* total swapped shm */
56 unsigned long swap_attempts;
57 unsigned long swap_successes;
66 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
70 /*-------------------------------------------------------------------*/
72 /* The last arg of semctl is a union semun, but where is it defined?
73 X/OPEN tells us to define it ourselves, but until recently
74 Linux include files would also define it. */
75 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
76 /* union semun is defined by including <sys/sem.h> */
78 /* according to X/OPEN we have to define it ourselves */
82 unsigned short *array;
83 struct seminfo *__buf;
87 /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
88 libc 4/5 does not mention struct ipc_term at all, but includes
89 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
90 glibc-1.09 has no support for sysv ipc.
91 glibc 2 uses __key, __seq */
92 #if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
106 static void print_perms(int id, struct ipc_perm *ipcp)
111 printf("%-10d %-10o", id, ipcp->mode & 0777);
113 pw = getpwuid(ipcp->cuid);
114 if (pw) printf(" %-10s", pw->pw_name);
115 else printf(" %-10d", ipcp->cuid);
116 gr = getgrgid(ipcp->cgid);
117 if (gr) printf(" %-10s", gr->gr_name);
118 else printf(" %-10d", ipcp->cgid);
120 pw = getpwuid(ipcp->uid);
121 if (pw) printf(" %-10s", pw->pw_name);
122 else printf(" %-10d", ipcp->uid);
123 gr = getgrgid(ipcp->gid);
124 if (gr) printf(" %-10s\n", gr->gr_name);
125 else printf(" %-10d\n", ipcp->gid);
129 static NOINLINE void do_shm(void)
131 int maxid, shmid, id;
132 struct shmid_ds shmseg;
133 struct shm_info shm_info;
134 struct shminfo shminfo;
135 struct ipc_perm *ipcp = &shmseg.shm_perm;
138 maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
140 printf("kernel not configured for %s\n", "shared memory");
146 printf("------ Shared Memory %s --------\n", "Limits");
147 if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
149 /* glibc 2.1.3 and all earlier libc's have ints as fields
150 * of struct shminfo; glibc 2.1.91 has unsigned long; ach */
151 printf("max number of segments = %lu\n"
152 "max seg size (kbytes) = %lu\n"
153 "max total shared memory (pages) = %lu\n"
154 "min seg size (bytes) = %lu\n",
155 (unsigned long) shminfo.shmmni,
156 (unsigned long) (shminfo.shmmax >> 10),
157 (unsigned long) shminfo.shmall,
158 (unsigned long) shminfo.shmmin);
162 printf("------ Shared Memory %s --------\n", "Status");
163 printf("segments allocated %d\n"
164 "pages allocated %lu\n"
165 "pages resident %lu\n"
166 "pages swapped %lu\n"
167 "Swap performance: %lu attempts\t%lu successes\n",
172 shm_info.swap_attempts, shm_info.swap_successes);
176 printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
177 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
178 "shmid", "perms", "cuid", "cgid", "uid", "gid");
182 printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
183 printf("%-10s %-10s %-20s %-20s %-20s\n",
184 "shmid", "owner", "attached", "detached", "changed");
188 printf("------ Shared Memory %s --------\n", "Creator/Last-op");
189 printf("%-10s %-10s %-10s %-10s\n",
190 "shmid", "owner", "cpid", "lpid");
194 printf("------ Shared Memory %s --------\n", "Segments");
195 printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
196 "key", "shmid", "owner", "perms", "bytes", "nattch",
201 for (id = 0; id <= maxid; id++) {
202 shmid = shmctl(id, SHM_STAT, &shmseg);
205 if (format == CREATOR) {
206 print_perms(shmid, ipcp);
209 pw = getpwuid(ipcp->uid);
213 printf("%-10d %-10.10s", shmid, pw->pw_name);
215 printf("%-10d %-10d", shmid, ipcp->uid);
216 /* ctime uses static buffer: use separate calls */
217 printf(" %-20.16s", shmseg.shm_atime
218 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
219 printf(" %-20.16s", shmseg.shm_dtime
220 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
221 printf(" %-20.16s\n", shmseg.shm_ctime
222 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
226 printf("%-10d %-10.10s", shmid, pw->pw_name);
228 printf("%-10d %-10d", shmid, ipcp->uid);
229 printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
233 printf("0x%08x ", ipcp->KEY);
235 printf("%-10d %-10.10s", shmid, pw->pw_name);
237 printf("%-10d %-10d", shmid, ipcp->uid);
238 printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
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" : " ");
256 static NOINLINE void do_sem(void)
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 = (unsigned short *) (void *) &seminfo;
266 maxid = semctl(0, 0, SEM_INFO, arg);
268 printf("kernel not configured for %s\n", "semaphores");
274 printf("------ Semaphore %s --------\n", "Limits");
275 arg.array = (unsigned short *) (void *) &seminfo; /* damn union */
276 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
278 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",
285 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
289 printf("------ Semaphore %s --------\n", "Status");
290 printf("used arrays = %d\n"
291 "allocated semaphores = %d\n",
292 seminfo.semusz, seminfo.semaem);
296 printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
297 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
298 "semid", "perms", "cuid", "cgid", "uid", "gid");
302 printf("------ Shared Memory %s --------\n", "Operation/Change Times");
303 printf("%-8s %-10s %-26.24s %-26.24s\n",
304 "shmid", "owner", "last-op", "last-changed");
311 printf("------ Semaphore %s --------\n", "Arrays");
312 printf("%-10s %-10s %-10s %-10s %-10s\n",
313 "key", "semid", "owner", "perms", "nsems");
317 for (id = 0; id <= maxid; id++) {
318 arg.buf = (struct semid_ds *) &semary;
319 semid = semctl(id, 0, SEM_STAT, arg);
322 if (format == CREATOR) {
323 print_perms(semid, ipcp);
326 pw = getpwuid(ipcp->uid);
330 printf("%-8d %-10.10s", semid, pw->pw_name);
332 printf("%-8d %-10d", semid, ipcp->uid);
333 /* ctime uses static buffer: use separate calls */
334 printf(" %-26.24s", semary.sem_otime
335 ? ctime(&semary.sem_otime) : "Not set");
336 printf(" %-26.24s\n", semary.sem_ctime
337 ? ctime(&semary.sem_ctime) : "Not set");
343 printf("0x%08x ", ipcp->KEY);
345 printf("%-10d %-10.9s", semid, pw->pw_name);
347 printf("%-10d %-9d", semid, ipcp->uid);
348 printf(" %-10o %-10ld\n", ipcp->mode & 0777,
350 * glibc-2.1.3 and earlier has unsigned short;
351 * glibc-2.1.91 has variation between
352 * unsigned short and unsigned long
353 * Austin prescribes unsigned short.
355 (long) semary.sem_nsems);
362 static NOINLINE void do_msg(void)
364 int maxid, msqid, id;
365 struct msqid_ds msgque;
366 struct msginfo msginfo;
367 struct ipc_perm *ipcp = &msgque.msg_perm;
370 maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
372 printf("kernel not configured for %s\n", "message queues");
378 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
380 printf("------ Message%s --------\n", "s: Limits");
381 printf("max queues system wide = %d\n"
382 "max size of message (bytes) = %d\n"
383 "default max size of queue (bytes) = %d\n",
384 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
388 printf("------ Message%s --------\n", "s: Status");
389 printf("allocated queues = %d\n"
390 "used headers = %d\n"
391 "used space = %d bytes\n",
392 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
396 printf("------ Message%s --------\n", " Queues: Creators/Owners");
397 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
398 "msqid", "perms", "cuid", "cgid", "uid", "gid");
402 printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
403 printf("%-8s %-10s %-20s %-20s %-20s\n",
404 "msqid", "owner", "send", "recv", "change");
408 printf("------ Message%s --------\n", " Queues PIDs");
409 printf("%-10s %-10s %-10s %-10s\n",
410 "msqid", "owner", "lspid", "lrpid");
414 printf("------ Message%s --------\n", " Queues");
415 printf("%-10s %-10s %-10s %-10s %-12s %-12s\n",
416 "key", "msqid", "owner", "perms", "used-bytes", "messages");
420 for (id = 0; id <= maxid; id++) {
421 msqid = msgctl(id, MSG_STAT, &msgque);
424 if (format == CREATOR) {
425 print_perms(msqid, ipcp);
428 pw = getpwuid(ipcp->uid);
432 printf("%-8d %-10.10s", msqid, pw->pw_name);
434 printf("%-8d %-10d", msqid, ipcp->uid);
435 printf(" %-20.16s", msgque.msg_stime
436 ? ctime(&msgque.msg_stime) + 4 : "Not set");
437 printf(" %-20.16s", msgque.msg_rtime
438 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
439 printf(" %-20.16s\n", msgque.msg_ctime
440 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
444 printf("%-8d %-10.10s", msqid, pw->pw_name);
446 printf("%-8d %-10d", msqid, ipcp->uid);
447 printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
451 printf("0x%08x ", ipcp->KEY);
453 printf("%-10d %-10.10s", msqid, pw->pw_name);
455 printf("%-10d %-10d", msqid, ipcp->uid);
456 printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
458 * glibc-2.1.3 and earlier has unsigned short;
459 * glibc-2.1.91 has variation between
460 * unsigned short, unsigned long
461 * Austin has msgqnum_t
463 (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
470 static void print_shm(int shmid)
472 struct shmid_ds shmds;
473 struct ipc_perm *ipcp = &shmds.shm_perm;
475 if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
476 bb_perror_msg("shmctl");
480 printf("\nShared memory Segment shmid=%d\n"
481 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
482 "mode=%#o\taccess_perms=%#o\n"
483 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
485 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
486 ipcp->mode, ipcp->mode & 0777,
487 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
488 (long) shmds.shm_nattch);
489 printf("att_time=%-26.24s\n",
490 shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
491 printf("det_time=%-26.24s\n",
492 shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
493 printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
497 static void print_msg(int msqid)
500 struct ipc_perm *ipcp = &buf.msg_perm;
502 if (msgctl(msqid, IPC_STAT, &buf) == -1) {
503 bb_perror_msg("msgctl");
507 printf("\nMessage Queue msqid=%d\n"
508 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
509 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
510 msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
512 * glibc-2.1.3 and earlier has unsigned short;
513 * glibc-2.1.91 has variation between
514 * unsigned short, unsigned long
515 * Austin has msgqnum_t (for msg_qbytes)
517 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
518 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
520 printf("send_time=%-26.24s\n",
521 buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
522 printf("rcv_time=%-26.24s\n",
523 buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
524 printf("change_time=%-26.24s\n\n",
525 buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
528 static void print_sem(int semid)
530 struct semid_ds semds;
531 struct ipc_perm *ipcp = &semds.sem_perm;
536 if (semctl(semid, 0, IPC_STAT, arg)) {
537 bb_perror_msg("semctl");
541 printf("\nSemaphore Array semid=%d\n"
542 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
543 "mode=%#o, access_perms=%#o\n"
545 "otime = %-26.24s\n",
547 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
548 ipcp->mode, ipcp->mode & 0777,
549 (long) semds.sem_nsems,
550 semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
551 printf("ctime = %-26.24s\n"
552 "%-10s %-10s %-10s %-10s %-10s\n",
553 ctime(&semds.sem_ctime),
554 "semnum", "value", "ncount", "zcount", "pid");
557 for (i = 0; i < semds.sem_nsems; i++) {
558 int val, ncnt, zcnt, pid;
560 val = semctl(semid, i, GETVAL, arg);
561 ncnt = semctl(semid, i, GETNCNT, arg);
562 zcnt = semctl(semid, i, GETZCNT, arg);
563 pid = semctl(semid, i, GETPID, arg);
564 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
565 bb_perror_msg_and_die("semctl");
567 printf("%-10u %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
572 //usage:#define ipcs_trivial_usage
573 //usage: "[[-smq] -i shmid] | [[-asmq] [-tcplu]]"
574 //usage:#define ipcs_full_usage "\n\n"
575 //usage: " -i Show specific resource"
576 //usage: "\nResource specification:"
577 //usage: "\n -m Shared memory segments"
578 //usage: "\n -q Message queues"
579 //usage: "\n -s Semaphore arrays"
580 //usage: "\n -a All (default)"
581 //usage: "\nOutput format:"
582 //usage: "\n -t Time"
583 //usage: "\n -c Creator"
585 //usage: "\n -l Limits"
586 //usage: "\n -u Summary"
588 int ipcs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
589 int ipcs_main(int argc UNUSED_PARAM, char **argv)
595 #define flag_print (1<<0)
596 #define flag_msg (1<<1)
597 #define flag_sem (1<<2)
598 #define flag_shm (1<<3)
600 opt = getopt32(argv, "i:aqsmtcplu", &opt_i);
601 if (opt & 0x1) { // -i
605 if (opt & 0x2) flags |= flag_msg | flag_sem | flag_shm; // -a
606 if (opt & 0x4) flags |= flag_msg; // -q
607 if (opt & 0x8) flags |= flag_sem; // -s
608 if (opt & 0x10) flags |= flag_shm; // -m
609 if (opt & 0x20) format = TIME; // -t
610 if (opt & 0x40) format = CREATOR; // -c
611 if (opt & 0x80) format = PID; // -p
612 if (opt & 0x100) format = LIMITS; // -l
613 if (opt & 0x200) format = STATUS; // -u
615 if (flags & flag_print) {
616 if (flags & flag_shm) {
618 fflush_stdout_and_exit(EXIT_SUCCESS);
620 if (flags & flag_sem) {
622 fflush_stdout_and_exit(EXIT_SUCCESS);
624 if (flags & flag_msg) {
626 fflush_stdout_and_exit(EXIT_SUCCESS);
631 if (!(flags & (flag_shm | flag_msg | flag_sem)))
632 flags |= flag_msg | flag_shm | flag_sem;
635 if (flags & flag_shm) {
639 if (flags & flag_sem) {
643 if (flags & flag_msg) {
647 fflush_stdout_and_exit(EXIT_SUCCESS);