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
104 static void print_perms(int id, struct ipc_perm *ipcp)
109 printf("%-10d %-10o", id, ipcp->mode & 0777);
111 pw = getpwuid(ipcp->cuid);
112 if (pw) printf(" %-10s", pw->pw_name);
113 else printf(" %-10d", ipcp->cuid);
114 gr = getgrgid(ipcp->cgid);
115 if (gr) printf(" %-10s", gr->gr_name);
116 else printf(" %-10d", ipcp->cgid);
118 pw = getpwuid(ipcp->uid);
119 if (pw) printf(" %-10s", pw->pw_name);
120 else printf(" %-10d", ipcp->uid);
121 gr = getgrgid(ipcp->gid);
122 if (gr) printf(" %-10s\n", gr->gr_name);
123 else printf(" %-10d\n", ipcp->gid);
126 static NOINLINE void do_shm(int format)
128 int maxid, shmid, id;
129 struct shmid_ds shmseg;
130 struct shm_info shm_info;
131 struct shminfo shminfo;
132 struct ipc_perm *ipcp = &shmseg.shm_perm;
135 maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
137 printf("kernel not configured for %s\n", "shared memory");
143 printf("------ Shared Memory %s --------\n", "Limits");
144 if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
146 /* glibc 2.1.3 and all earlier libc's have ints as fields
147 * of struct shminfo; glibc 2.1.91 has unsigned long; ach */
148 printf("max number of segments = %lu\n"
149 "max seg size (kbytes) = %lu\n"
150 "max total shared memory (pages) = %lu\n"
151 "min seg size (bytes) = %lu\n",
152 (unsigned long) shminfo.shmmni,
153 (unsigned long) (shminfo.shmmax >> 10),
154 (unsigned long) shminfo.shmall,
155 (unsigned long) shminfo.shmmin);
159 printf("------ Shared Memory %s --------\n", "Status");
160 printf("segments allocated %d\n"
161 "pages allocated %lu\n"
162 "pages resident %lu\n"
163 "pages swapped %lu\n"
164 "Swap performance: %lu attempts\t%lu successes\n",
169 shm_info.swap_attempts, shm_info.swap_successes);
173 printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
174 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
175 "shmid", "perms", "cuid", "cgid", "uid", "gid");
179 printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
180 printf("%-10s %-10s %-20s %-20s %-20s\n",
181 "shmid", "owner", "attached", "detached", "changed");
185 printf("------ Shared Memory %s --------\n", "Creator/Last-op");
186 printf("%-10s %-10s %-10s %-10s\n",
187 "shmid", "owner", "cpid", "lpid");
191 printf("------ Shared Memory %s --------\n", "Segments");
192 printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
193 "key", "shmid", "owner", "perms", "bytes", "nattch",
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 printf("%-10d %-10.10s", shmid, pw->pw_name);
212 printf("%-10d %-10d", shmid, ipcp->uid);
213 /* ctime uses static buffer: use separate calls */
214 printf(" %-20.16s", shmseg.shm_atime
215 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
216 printf(" %-20.16s", shmseg.shm_dtime
217 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
218 printf(" %-20.16s\n", shmseg.shm_ctime
219 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
223 printf("%-10d %-10.10s", shmid, pw->pw_name);
225 printf("%-10d %-10d", shmid, ipcp->uid);
226 printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
230 printf("0x%08x ", ipcp->KEY);
232 printf("%-10d %-10.10s", shmid, pw->pw_name);
234 printf("%-10d %-10d", shmid, ipcp->uid);
235 printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
237 * earlier: int, Austin has size_t
239 (unsigned long) shmseg.shm_segsz,
241 * glibc-2.1.3 and earlier has unsigned short;
242 * Austin has shmatt_t
244 (long) shmseg.shm_nattch,
245 ipcp->mode & SHM_DEST ? "dest" : " ",
246 ipcp->mode & SHM_LOCKED ? "locked" : " ");
252 static NOINLINE void do_sem(int format)
254 int maxid, semid, id;
255 struct semid_ds semary;
256 struct seminfo seminfo;
257 struct ipc_perm *ipcp = &semary.sem_perm;
261 arg.array = (unsigned short *) (void *) &seminfo;
262 maxid = semctl(0, 0, SEM_INFO, arg);
264 printf("kernel not configured for %s\n", "semaphores");
270 printf("------ Semaphore %s --------\n", "Limits");
271 arg.array = (unsigned short *) (void *) &seminfo; /* damn union */
272 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
274 printf("max number of arrays = %d\n"
275 "max semaphores per array = %d\n"
276 "max semaphores system wide = %d\n"
277 "max ops per semop call = %d\n"
278 "semaphore max value = %d\n",
281 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
285 printf("------ Semaphore %s --------\n", "Status");
286 printf("used arrays = %d\n"
287 "allocated semaphores = %d\n",
288 seminfo.semusz, seminfo.semaem);
292 printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
293 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
294 "semid", "perms", "cuid", "cgid", "uid", "gid");
298 printf("------ Shared Memory %s --------\n", "Operation/Change Times");
299 printf("%-8s %-10s %-26.24s %-26.24s\n",
300 "shmid", "owner", "last-op", "last-changed");
307 printf("------ Semaphore %s --------\n", "Arrays");
308 printf("%-10s %-10s %-10s %-10s %-10s\n",
309 "key", "semid", "owner", "perms", "nsems");
313 for (id = 0; id <= maxid; id++) {
314 arg.buf = (struct semid_ds *) &semary;
315 semid = semctl(id, 0, SEM_STAT, arg);
318 if (format == CREATOR) {
319 print_perms(semid, ipcp);
322 pw = getpwuid(ipcp->uid);
326 printf("%-8d %-10.10s", semid, pw->pw_name);
328 printf("%-8d %-10d", semid, ipcp->uid);
329 /* ctime uses static buffer: use separate calls */
330 printf(" %-26.24s", semary.sem_otime
331 ? ctime(&semary.sem_otime) : "Not set");
332 printf(" %-26.24s\n", semary.sem_ctime
333 ? ctime(&semary.sem_ctime) : "Not set");
339 printf("0x%08x ", ipcp->KEY);
341 printf("%-10d %-10.9s", semid, pw->pw_name);
343 printf("%-10d %-9d", semid, ipcp->uid);
344 printf(" %-10o %-10ld\n", ipcp->mode & 0777,
346 * glibc-2.1.3 and earlier has unsigned short;
347 * glibc-2.1.91 has variation between
348 * unsigned short and unsigned long
349 * Austin prescribes unsigned short.
351 (long) semary.sem_nsems);
357 static NOINLINE void do_msg(int format)
359 int maxid, msqid, id;
360 struct msqid_ds msgque;
361 struct msginfo msginfo;
362 struct ipc_perm *ipcp = &msgque.msg_perm;
365 maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
367 printf("kernel not configured for %s\n", "message queues");
373 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
375 printf("------ Message%s --------\n", "s: Limits");
376 printf("max queues system wide = %d\n"
377 "max size of message (bytes) = %d\n"
378 "default max size of queue (bytes) = %d\n",
379 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
383 printf("------ Message%s --------\n", "s: Status");
384 printf("allocated queues = %d\n"
385 "used headers = %d\n"
386 "used space = %d bytes\n",
387 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
391 printf("------ Message%s --------\n", " Queues: Creators/Owners");
392 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
393 "msqid", "perms", "cuid", "cgid", "uid", "gid");
397 printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
398 printf("%-8s %-10s %-20s %-20s %-20s\n",
399 "msqid", "owner", "send", "recv", "change");
403 printf("------ Message%s --------\n", " Queues PIDs");
404 printf("%-10s %-10s %-10s %-10s\n",
405 "msqid", "owner", "lspid", "lrpid");
409 printf("------ Message%s --------\n", " Queues");
410 printf("%-10s %-10s %-10s %-10s %-12s %-12s\n",
411 "key", "msqid", "owner", "perms", "used-bytes", "messages");
415 for (id = 0; id <= maxid; id++) {
416 msqid = msgctl(id, MSG_STAT, &msgque);
419 if (format == CREATOR) {
420 print_perms(msqid, ipcp);
423 pw = getpwuid(ipcp->uid);
427 printf("%-8d %-10.10s", msqid, pw->pw_name);
429 printf("%-8d %-10d", msqid, ipcp->uid);
430 printf(" %-20.16s", msgque.msg_stime
431 ? ctime(&msgque.msg_stime) + 4 : "Not set");
432 printf(" %-20.16s", msgque.msg_rtime
433 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
434 printf(" %-20.16s\n", msgque.msg_ctime
435 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
439 printf("%-8d %-10.10s", msqid, pw->pw_name);
441 printf("%-8d %-10d", msqid, ipcp->uid);
442 printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
446 printf("0x%08x ", ipcp->KEY);
448 printf("%-10d %-10.10s", msqid, pw->pw_name);
450 printf("%-10d %-10d", msqid, ipcp->uid);
451 printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
453 * glibc-2.1.3 and earlier has unsigned short;
454 * glibc-2.1.91 has variation between
455 * unsigned short, unsigned long
456 * Austin has msgqnum_t
458 (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
464 static void print_shm(int shmid)
466 struct shmid_ds shmds;
467 struct ipc_perm *ipcp = &shmds.shm_perm;
469 if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
470 bb_simple_perror_msg("shmctl");
474 printf("\nShared memory Segment shmid=%d\n"
475 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
476 "mode=%#o\taccess_perms=%#o\n"
477 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
479 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
480 ipcp->mode, ipcp->mode & 0777,
481 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
482 (long) shmds.shm_nattch);
483 printf("att_time=%-26.24s\n",
484 shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
485 printf("det_time=%-26.24s\n",
486 shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
487 printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
490 static void print_msg(int msqid)
493 struct ipc_perm *ipcp = &buf.msg_perm;
495 if (msgctl(msqid, IPC_STAT, &buf) == -1) {
496 bb_simple_perror_msg("msgctl");
500 printf("\nMessage Queue msqid=%d\n"
501 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
502 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
503 msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
505 * glibc-2.1.3 and earlier has unsigned short;
506 * glibc-2.1.91 has variation between
507 * unsigned short, unsigned long
508 * Austin has msgqnum_t (for msg_qbytes)
510 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
511 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
513 printf("send_time=%-26.24s\n",
514 buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
515 printf("rcv_time=%-26.24s\n",
516 buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
517 printf("change_time=%-26.24s\n\n",
518 buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
521 static void print_sem(int semid)
523 struct semid_ds semds;
524 struct ipc_perm *ipcp = &semds.sem_perm;
529 if (semctl(semid, 0, IPC_STAT, arg)) {
530 bb_simple_perror_msg("semctl");
534 printf("\nSemaphore Array semid=%d\n"
535 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
536 "mode=%#o, access_perms=%#o\n"
538 "otime = %-26.24s\n",
540 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
541 ipcp->mode, ipcp->mode & 0777,
542 (long) semds.sem_nsems,
543 semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
544 printf("ctime = %-26.24s\n"
545 "%-10s %-10s %-10s %-10s %-10s\n",
546 ctime(&semds.sem_ctime),
547 "semnum", "value", "ncount", "zcount", "pid");
550 for (i = 0; i < semds.sem_nsems; i++) {
551 int val, ncnt, zcnt, pid;
553 val = semctl(semid, i, GETVAL, arg);
554 ncnt = semctl(semid, i, GETNCNT, arg);
555 zcnt = semctl(semid, i, GETZCNT, arg);
556 pid = semctl(semid, i, GETPID, arg);
557 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
558 bb_simple_perror_msg_and_die("semctl");
560 printf("%-10u %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
565 //usage:#define ipcs_trivial_usage
566 //usage: "[[-smq] -i SHMID] | [[-asmq] [-tcplu]]"
567 //usage:#define ipcs_full_usage "\n\n"
568 //usage: " -i ID Show specific resource"
569 //usage: "\nResource specification:"
570 //usage: "\n -m Shared memory segments"
571 //usage: "\n -q Message queues"
572 //usage: "\n -s Semaphore arrays"
573 //usage: "\n -a All (default)"
574 //usage: "\nOutput format:"
575 //usage: "\n -t Time"
576 //usage: "\n -c Creator"
578 //usage: "\n -l Limits"
579 //usage: "\n -u Summary"
581 int ipcs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
582 int ipcs_main(int argc UNUSED_PARAM, char **argv)
588 opt = getopt32(argv, "i:aqsmtcplu", &opt_i);
589 #define flag_msg (1<<2)
590 #define flag_sem (1<<3)
591 #define flag_shm (1<<4)
592 if (opt & (1<<5)) format = TIME; // -t
593 if (opt & (1<<6)) format = CREATOR; // -c
594 if (opt & (1<<7)) format = PID; // -p
595 if (opt & (1<<8)) format = LIMITS; // -l
596 if (opt & (1<<9)) format = STATUS; // -u
598 if (opt & (1<<0)) { // -i
602 if (opt & flag_shm) {
604 fflush_stdout_and_exit(EXIT_SUCCESS);
606 if (opt & flag_sem) {
608 fflush_stdout_and_exit(EXIT_SUCCESS);
610 if (opt & flag_msg) {
612 fflush_stdout_and_exit(EXIT_SUCCESS);
617 if ((opt & (1<<1)) // -a
618 || !(opt & (flag_msg | flag_sem | flag_shm)) // none of -q,-s,-m == all
620 opt |= flag_msg | flag_sem | flag_shm;
625 if (opt & flag_msg) {
629 if (opt & flag_shm) {
633 if (opt & flag_sem) {
637 fflush_stdout_and_exit(EXIT_SUCCESS);