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.
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(ipcs, BB_DIR_USR_BIN, BB_SUID_DROP))
20 //kbuild:lib-$(CONFIG_IPCS) += ipcs.o
22 //usage:#define ipcs_trivial_usage
23 //usage: "[[-smq] -i shmid] | [[-asmq] [-tcplu]]"
24 //usage:#define ipcs_full_usage "\n\n"
25 //usage: " -i Show specific resource"
26 //usage: "\nResource specification:"
27 //usage: "\n -m Shared memory segments"
28 //usage: "\n -q Message queues"
29 //usage: "\n -s Semaphore arrays"
30 //usage: "\n -a All (default)"
31 //usage: "\nOutput format:"
33 //usage: "\n -c Creator"
35 //usage: "\n -l Limits"
36 //usage: "\n -u Summary"
38 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
39 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
40 /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
41 #include <sys/types.h>
49 /*-------------------------------------------------------------------*/
50 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
51 but inside #ifdef __KERNEL__ ... #endif */
53 /* shm_mode upper byte flags */
54 #define SHM_DEST 01000 /* segment will be destroyed on last detach */
55 #define SHM_LOCKED 02000 /* segment will not be swapped */
58 /* For older kernels the same holds for the defines below */
69 unsigned long shm_tot; /* total allocated shm */
70 unsigned long shm_rss; /* total resident shm */
71 unsigned long shm_swp; /* total swapped shm */
72 unsigned long swap_attempts;
73 unsigned long swap_successes;
82 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
86 /*-------------------------------------------------------------------*/
88 /* The last arg of semctl is a union semun, but where is it defined?
89 X/OPEN tells us to define it ourselves, but until recently
90 Linux include files would also define it. */
91 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
92 /* union semun is defined by including <sys/sem.h> */
94 /* according to X/OPEN we have to define it ourselves */
98 unsigned short *array;
99 struct seminfo *__buf;
103 /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
104 libc 4/5 does not mention struct ipc_term at all, but includes
105 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
106 glibc-1.09 has no support for sysv ipc.
107 glibc 2 uses __key, __seq */
108 #if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
122 static void print_perms(int id, struct ipc_perm *ipcp)
127 printf("%-10d %-10o", id, ipcp->mode & 0777);
129 pw = getpwuid(ipcp->cuid);
130 if (pw) printf(" %-10s", pw->pw_name);
131 else printf(" %-10d", ipcp->cuid);
132 gr = getgrgid(ipcp->cgid);
133 if (gr) printf(" %-10s", gr->gr_name);
134 else printf(" %-10d", ipcp->cgid);
136 pw = getpwuid(ipcp->uid);
137 if (pw) printf(" %-10s", pw->pw_name);
138 else printf(" %-10d", ipcp->uid);
139 gr = getgrgid(ipcp->gid);
140 if (gr) printf(" %-10s\n", gr->gr_name);
141 else printf(" %-10d\n", ipcp->gid);
145 static NOINLINE void do_shm(void)
147 int maxid, shmid, id;
148 struct shmid_ds shmseg;
149 struct shm_info shm_info;
150 struct shminfo shminfo;
151 struct ipc_perm *ipcp = &shmseg.shm_perm;
154 maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
156 printf("kernel not configured for %s\n", "shared memory");
162 printf("------ Shared Memory %s --------\n", "Limits");
163 if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
165 /* glibc 2.1.3 and all earlier libc's have ints as fields
166 * of struct shminfo; glibc 2.1.91 has unsigned long; ach */
167 printf("max number of segments = %lu\n"
168 "max seg size (kbytes) = %lu\n"
169 "max total shared memory (pages) = %lu\n"
170 "min seg size (bytes) = %lu\n",
171 (unsigned long) shminfo.shmmni,
172 (unsigned long) (shminfo.shmmax >> 10),
173 (unsigned long) shminfo.shmall,
174 (unsigned long) shminfo.shmmin);
178 printf("------ Shared Memory %s --------\n", "Status");
179 printf("segments allocated %d\n"
180 "pages allocated %lu\n"
181 "pages resident %lu\n"
182 "pages swapped %lu\n"
183 "Swap performance: %lu attempts\t%lu successes\n",
188 shm_info.swap_attempts, shm_info.swap_successes);
192 printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
193 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
194 "shmid", "perms", "cuid", "cgid", "uid", "gid");
198 printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
199 printf("%-10s %-10s %-20s %-20s %-20s\n",
200 "shmid", "owner", "attached", "detached", "changed");
204 printf("------ Shared Memory %s --------\n", "Creator/Last-op");
205 printf("%-10s %-10s %-10s %-10s\n",
206 "shmid", "owner", "cpid", "lpid");
210 printf("------ Shared Memory %s --------\n", "Segments");
211 printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
212 "key", "shmid", "owner", "perms", "bytes", "nattch",
217 for (id = 0; id <= maxid; id++) {
218 shmid = shmctl(id, SHM_STAT, &shmseg);
221 if (format == CREATOR) {
222 print_perms(shmid, ipcp);
225 pw = getpwuid(ipcp->uid);
229 printf("%-10d %-10.10s", shmid, pw->pw_name);
231 printf("%-10d %-10d", shmid, ipcp->uid);
232 /* ctime uses static buffer: use separate calls */
233 printf(" %-20.16s", shmseg.shm_atime
234 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
235 printf(" %-20.16s", shmseg.shm_dtime
236 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
237 printf(" %-20.16s\n", shmseg.shm_ctime
238 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
242 printf("%-10d %-10.10s", shmid, pw->pw_name);
244 printf("%-10d %-10d", shmid, ipcp->uid);
245 printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
249 printf("0x%08x ", ipcp->KEY);
251 printf("%-10d %-10.10s", shmid, pw->pw_name);
253 printf("%-10d %-10d", shmid, ipcp->uid);
254 printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
256 * earlier: int, Austin has size_t
258 (unsigned long) shmseg.shm_segsz,
260 * glibc-2.1.3 and earlier has unsigned short;
261 * Austin has shmatt_t
263 (long) shmseg.shm_nattch,
264 ipcp->mode & SHM_DEST ? "dest" : " ",
265 ipcp->mode & SHM_LOCKED ? "locked" : " ");
272 static NOINLINE void do_sem(void)
274 int maxid, semid, id;
275 struct semid_ds semary;
276 struct seminfo seminfo;
277 struct ipc_perm *ipcp = &semary.sem_perm;
281 arg.array = (unsigned short *) (void *) &seminfo;
282 maxid = semctl(0, 0, SEM_INFO, arg);
284 printf("kernel not configured for %s\n", "semaphores");
290 printf("------ Semaphore %s --------\n", "Limits");
291 arg.array = (unsigned short *) (void *) &seminfo; /* damn union */
292 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
294 printf("max number of arrays = %d\n"
295 "max semaphores per array = %d\n"
296 "max semaphores system wide = %d\n"
297 "max ops per semop call = %d\n"
298 "semaphore max value = %d\n",
301 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
305 printf("------ Semaphore %s --------\n", "Status");
306 printf("used arrays = %d\n"
307 "allocated semaphores = %d\n",
308 seminfo.semusz, seminfo.semaem);
312 printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
313 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
314 "semid", "perms", "cuid", "cgid", "uid", "gid");
318 printf("------ Shared Memory %s --------\n", "Operation/Change Times");
319 printf("%-8s %-10s %-26.24s %-26.24s\n",
320 "shmid", "owner", "last-op", "last-changed");
327 printf("------ Semaphore %s --------\n", "Arrays");
328 printf("%-10s %-10s %-10s %-10s %-10s\n",
329 "key", "semid", "owner", "perms", "nsems");
333 for (id = 0; id <= maxid; id++) {
334 arg.buf = (struct semid_ds *) &semary;
335 semid = semctl(id, 0, SEM_STAT, arg);
338 if (format == CREATOR) {
339 print_perms(semid, ipcp);
342 pw = getpwuid(ipcp->uid);
346 printf("%-8d %-10.10s", semid, pw->pw_name);
348 printf("%-8d %-10d", semid, ipcp->uid);
349 /* ctime uses static buffer: use separate calls */
350 printf(" %-26.24s", semary.sem_otime
351 ? ctime(&semary.sem_otime) : "Not set");
352 printf(" %-26.24s\n", semary.sem_ctime
353 ? ctime(&semary.sem_ctime) : "Not set");
359 printf("0x%08x ", ipcp->KEY);
361 printf("%-10d %-10.9s", semid, pw->pw_name);
363 printf("%-10d %-9d", semid, ipcp->uid);
364 printf(" %-10o %-10ld\n", ipcp->mode & 0777,
366 * glibc-2.1.3 and earlier has unsigned short;
367 * glibc-2.1.91 has variation between
368 * unsigned short and unsigned long
369 * Austin prescribes unsigned short.
371 (long) semary.sem_nsems);
378 static NOINLINE void do_msg(void)
380 int maxid, msqid, id;
381 struct msqid_ds msgque;
382 struct msginfo msginfo;
383 struct ipc_perm *ipcp = &msgque.msg_perm;
386 maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
388 printf("kernel not configured for %s\n", "message queues");
394 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
396 printf("------ Message%s --------\n", "s: Limits");
397 printf("max queues system wide = %d\n"
398 "max size of message (bytes) = %d\n"
399 "default max size of queue (bytes) = %d\n",
400 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
404 printf("------ Message%s --------\n", "s: Status");
405 printf("allocated queues = %d\n"
406 "used headers = %d\n"
407 "used space = %d bytes\n",
408 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
412 printf("------ Message%s --------\n", " Queues: Creators/Owners");
413 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
414 "msqid", "perms", "cuid", "cgid", "uid", "gid");
418 printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
419 printf("%-8s %-10s %-20s %-20s %-20s\n",
420 "msqid", "owner", "send", "recv", "change");
424 printf("------ Message%s --------\n", " Queues PIDs");
425 printf("%-10s %-10s %-10s %-10s\n",
426 "msqid", "owner", "lspid", "lrpid");
430 printf("------ Message%s --------\n", " Queues");
431 printf("%-10s %-10s %-10s %-10s %-12s %-12s\n",
432 "key", "msqid", "owner", "perms", "used-bytes", "messages");
436 for (id = 0; id <= maxid; id++) {
437 msqid = msgctl(id, MSG_STAT, &msgque);
440 if (format == CREATOR) {
441 print_perms(msqid, ipcp);
444 pw = getpwuid(ipcp->uid);
448 printf("%-8d %-10.10s", msqid, pw->pw_name);
450 printf("%-8d %-10d", msqid, ipcp->uid);
451 printf(" %-20.16s", msgque.msg_stime
452 ? ctime(&msgque.msg_stime) + 4 : "Not set");
453 printf(" %-20.16s", msgque.msg_rtime
454 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
455 printf(" %-20.16s\n", msgque.msg_ctime
456 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
460 printf("%-8d %-10.10s", msqid, pw->pw_name);
462 printf("%-8d %-10d", msqid, ipcp->uid);
463 printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
467 printf("0x%08x ", ipcp->KEY);
469 printf("%-10d %-10.10s", msqid, pw->pw_name);
471 printf("%-10d %-10d", msqid, ipcp->uid);
472 printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
474 * glibc-2.1.3 and earlier has unsigned short;
475 * glibc-2.1.91 has variation between
476 * unsigned short, unsigned long
477 * Austin has msgqnum_t
479 (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
486 static void print_shm(int shmid)
488 struct shmid_ds shmds;
489 struct ipc_perm *ipcp = &shmds.shm_perm;
491 if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
492 bb_perror_msg("shmctl");
496 printf("\nShared memory Segment shmid=%d\n"
497 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
498 "mode=%#o\taccess_perms=%#o\n"
499 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\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 printf("att_time=%-26.24s\n",
506 shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
507 printf("det_time=%-26.24s\n",
508 shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
509 printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
513 static void print_msg(int msqid)
516 struct ipc_perm *ipcp = &buf.msg_perm;
518 if (msgctl(msqid, IPC_STAT, &buf) == -1) {
519 bb_perror_msg("msgctl");
523 printf("\nMessage Queue msqid=%d\n"
524 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
525 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
526 msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
528 * glibc-2.1.3 and earlier has unsigned short;
529 * glibc-2.1.91 has variation between
530 * unsigned short, unsigned long
531 * Austin has msgqnum_t (for msg_qbytes)
533 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
534 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
536 printf("send_time=%-26.24s\n",
537 buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
538 printf("rcv_time=%-26.24s\n",
539 buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
540 printf("change_time=%-26.24s\n\n",
541 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)) {
553 bb_perror_msg("semctl");
557 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"
561 "otime = %-26.24s\n",
563 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
564 ipcp->mode, ipcp->mode & 0777,
565 (long) semds.sem_nsems,
566 semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
567 printf("ctime = %-26.24s\n"
568 "%-10s %-10s %-10s %-10s %-10s\n",
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;
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) {
581 bb_perror_msg_and_die("semctl");
583 printf("%-10u %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
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);