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.
17 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
18 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
19 /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
20 #include <sys/types.h>
28 /*-------------------------------------------------------------------*/
29 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
30 but inside #ifdef __KERNEL__ ... #endif */
32 /* shm_mode upper byte flags */
33 #define SHM_DEST 01000 /* segment will be destroyed on last detach */
34 #define SHM_LOCKED 02000 /* segment will not be swapped */
37 /* For older kernels the same holds for the defines below */
48 ulong shm_tot; /* total allocated shm */
49 ulong shm_rss; /* total resident shm */
50 ulong shm_swp; /* total swapped shm */
61 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
65 /*-------------------------------------------------------------------*/
67 /* The last arg of semctl is a union semun, but where is it defined?
68 X/OPEN tells us to define it ourselves, but until recently
69 Linux include files would also define it. */
70 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
71 /* union semun is defined by including <sys/sem.h> */
73 /* according to X/OPEN we have to define it ourselves */
77 unsigned short int *array;
78 struct seminfo *__buf;
82 /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
83 libc 4/5 does not mention struct ipc_term at all, but includes
84 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
85 glibc-1.09 has no support for sysv ipc.
86 glibc 2 uses __key, __seq */
87 #if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
101 static void print_perms(int id, struct ipc_perm *ipcp)
106 printf("%-10d %-10o", id, ipcp->mode & 0777);
108 if ((pw = getpwuid(ipcp->cuid)))
109 printf(" %-10s", pw->pw_name);
111 printf(" %-10d", ipcp->cuid);
112 if ((gr = getgrgid(ipcp->cgid)))
113 printf(" %-10s", gr->gr_name);
115 printf(" %-10d", ipcp->cgid);
117 if ((pw = getpwuid(ipcp->uid)))
118 printf(" %-10s", pw->pw_name);
120 printf(" %-10d", ipcp->uid);
121 if ((gr = getgrgid(ipcp->gid)))
122 printf(" %-10s\n", gr->gr_name);
124 printf(" %-10d\n", ipcp->gid);
128 static void do_shm(void)
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 printf("kernel not configured for %s\n", "shared memory");
145 printf("------ Shared Memory %s --------\n", "Limits");
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 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 printf("------ Shared Memory %s --------\n", "Status");
162 printf( "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 printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
176 printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n",
177 "shmid", "perms", "cuid", "cgid", "uid", "gid");
181 printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
182 printf( "%-10s %-10s %-20s %-20s %-20s\n",
183 "shmid", "owner", "attached", "detached", "changed");
187 printf("------ Shared Memory %s --------\n", "Creator/Last-op");
188 printf( "%-10s %-10s %-10s %-10s\n",
189 "shmid", "owner", "cpid", "lpid");
193 printf("------ Shared Memory %s --------\n", "Segments");
194 printf( "%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
195 "key", "shmid", "owner", "perms", "bytes", "nattch",
200 for (id = 0; id <= maxid; id++) {
201 shmid = shmctl(id, SHM_STAT, &shmseg);
204 if (format == CREATOR) {
205 print_perms(shmid, ipcp);
208 pw = getpwuid(ipcp->uid);
212 printf("%-10d %-10.10s", shmid, pw->pw_name);
214 printf("%-10d %-10d", shmid, ipcp->uid);
215 /* ctime uses static buffer: use separate calls */
216 printf(" %-20.16s", shmseg.shm_atime
217 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
218 printf(" %-20.16s", shmseg.shm_dtime
219 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
220 printf(" %-20.16s\n", shmseg.shm_ctime
221 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
225 printf("%-10d %-10.10s", shmid, pw->pw_name);
227 printf("%-10d %-10d", shmid, ipcp->uid);
228 printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
232 printf("0x%08x ", ipcp->KEY);
234 printf("%-10d %-10.10s", shmid, pw->pw_name);
236 printf("%-10d %-10d", shmid, ipcp->uid);
237 printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
239 * earlier: int, Austin has size_t
241 (unsigned long) shmseg.shm_segsz,
243 * glibc-2.1.3 and earlier has unsigned short;
244 * Austin has shmatt_t
246 (long) shmseg.shm_nattch,
247 ipcp->mode & SHM_DEST ? "dest" : " ",
248 ipcp->mode & SHM_LOCKED ? "locked" : " ");
255 static void do_sem(void)
257 int maxid, semid, id;
258 struct semid_ds semary;
259 struct seminfo seminfo;
260 struct ipc_perm *ipcp = &semary.sem_perm;
264 arg.array = (ushort *) (void *) &seminfo;
265 maxid = semctl(0, 0, SEM_INFO, arg);
267 printf("kernel not configured for %s\n", "semaphores");
273 printf("------ Semaphore %s --------\n", "Limits");
274 arg.array = (ushort *) (void *) &seminfo; /* damn union */
275 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
277 printf("max number of arrays = %d\n"
278 "max semaphores per array = %d\n"
279 "max semaphores system wide = %d\n"
280 "max ops per semop call = %d\n"
281 "semaphore max value = %d\n",
284 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
288 printf("------ Semaphore %s --------\n", "Status");
289 printf( "used arrays = %d\n"
290 "allocated semaphores = %d\n",
291 seminfo.semusz, seminfo.semaem);
295 printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
296 printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n",
297 "semid", "perms", "cuid", "cgid", "uid", "gid");
301 printf("------ Shared Memory %s --------\n", "Operation/Change Times");
302 printf( "%-8s %-10s %-26.24s %-26.24s\n",
303 "shmid", "owner", "last-op", "last-changed");
310 printf("------ Semaphore %s --------\n", "Arrays");
311 printf( "%-10s %-10s %-10s %-10s %-10s\n",
312 "key", "semid", "owner", "perms", "nsems");
316 for (id = 0; id <= maxid; id++) {
317 arg.buf = (struct semid_ds *) &semary;
318 semid = semctl(id, 0, SEM_STAT, arg);
321 if (format == CREATOR) {
322 print_perms(semid, ipcp);
325 pw = getpwuid(ipcp->uid);
329 printf("%-8d %-10.10s", semid, pw->pw_name);
331 printf("%-8d %-10d", semid, ipcp->uid);
332 /* ctime uses static buffer: use separate calls */
333 printf(" %-26.24s", semary.sem_otime
334 ? ctime(&semary.sem_otime) : "Not set");
335 printf(" %-26.24s\n", semary.sem_ctime
336 ? ctime(&semary.sem_ctime) : "Not set");
342 printf("0x%08x ", ipcp->KEY);
344 printf("%-10d %-10.9s", semid, pw->pw_name);
346 printf("%-10d %-9d", semid, ipcp->uid);
347 printf(" %-10o %-10ld\n", ipcp->mode & 0777,
349 * glibc-2.1.3 and earlier has unsigned short;
350 * glibc-2.1.91 has variation between
351 * unsigned short and unsigned long
352 * Austin prescribes unsigned short.
354 (long) semary.sem_nsems);
361 static void do_msg(void)
363 int maxid, msqid, id;
364 struct msqid_ds msgque;
365 struct msginfo msginfo;
366 struct ipc_perm *ipcp = &msgque.msg_perm;
369 maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
371 printf("kernel not configured for %s\n", "message queues");
377 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
379 printf("------ Message%s --------\n", "s: Limits");
380 printf( "max queues system wide = %d\n"
381 "max size of message (bytes) = %d\n"
382 "default max size of queue (bytes) = %d\n",
383 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
387 printf("------ Message%s --------\n", "s: Status");
388 printf( "allocated queues = %d\n"
389 "used headers = %d\n"
390 "used space = %d bytes\n",
391 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
395 printf("------ Message%s --------\n", " Queues: Creators/Owners");
396 printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n",
397 "msqid", "perms", "cuid", "cgid", "uid", "gid");
401 printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
402 printf( "%-8s %-10s %-20s %-20s %-20s\n",
403 "msqid", "owner", "send", "recv", "change");
407 printf("------ Message%s --------\n", " Queues PIDs");
408 printf( "%-10s %-10s %-10s %-10s\n",
409 "msqid", "owner", "lspid", "lrpid");
413 printf("------ Message%s --------\n", " Queues");
414 printf( "%-10s %-10s %-10s %-10s %-12s %-12s\n",
415 "key", "msqid", "owner", "perms", "used-bytes", "messages");
419 for (id = 0; id <= maxid; id++) {
420 msqid = msgctl(id, MSG_STAT, &msgque);
423 if (format == CREATOR) {
424 print_perms(msqid, ipcp);
427 pw = getpwuid(ipcp->uid);
431 printf("%-8d %-10.10s", msqid, pw->pw_name);
433 printf("%-8d %-10d", msqid, ipcp->uid);
434 printf(" %-20.16s", msgque.msg_stime
435 ? ctime(&msgque.msg_stime) + 4 : "Not set");
436 printf(" %-20.16s", msgque.msg_rtime
437 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
438 printf(" %-20.16s\n", msgque.msg_ctime
439 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
443 printf("%-8d %-10.10s", msqid, pw->pw_name);
445 printf("%-8d %-10d", msqid, ipcp->uid);
446 printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
450 printf("0x%08x ", ipcp->KEY);
452 printf("%-10d %-10.10s", msqid, pw->pw_name);
454 printf("%-10d %-10d", msqid, ipcp->uid);
455 printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
457 * glibc-2.1.3 and earlier has unsigned short;
458 * glibc-2.1.91 has variation between
459 * unsigned short, unsigned long
460 * Austin has msgqnum_t
462 (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
469 static void print_shm(int shmid)
471 struct shmid_ds shmds;
472 struct ipc_perm *ipcp = &shmds.shm_perm;
474 if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
475 bb_perror_msg("shmctl");
479 printf("\nShared memory Segment shmid=%d\n"
480 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
481 "mode=%#o\taccess_perms=%#o\n"
482 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
484 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
485 ipcp->mode, ipcp->mode & 0777,
486 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
487 (long) shmds.shm_nattch);
488 printf("att_time=%-26.24s\n",
489 shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
490 printf("det_time=%-26.24s\n",
491 shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
492 printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
496 static void print_msg(int msqid)
499 struct ipc_perm *ipcp = &buf.msg_perm;
501 if (msgctl(msqid, IPC_STAT, &buf) == -1) {
502 bb_perror_msg("msgctl");
506 printf("\nMessage Queue msqid=%d\n"
507 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
508 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
509 msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
511 * glibc-2.1.3 and earlier has unsigned short;
512 * glibc-2.1.91 has variation between
513 * unsigned short, unsigned long
514 * Austin has msgqnum_t (for msg_qbytes)
516 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
517 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
519 printf("send_time=%-26.24s\n",
520 buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
521 printf("rcv_time=%-26.24s\n",
522 buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
523 printf("change_time=%-26.24s\n\n",
524 buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
527 static void print_sem(int semid)
529 struct semid_ds semds;
530 struct ipc_perm *ipcp = &semds.sem_perm;
535 if (semctl(semid, 0, IPC_STAT, arg)) {
536 bb_perror_msg("semctl");
540 printf("\nSemaphore Array semid=%d\n"
541 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
542 "mode=%#o, access_perms=%#o\n"
544 "otime = %-26.24s\n",
546 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
547 ipcp->mode, ipcp->mode & 0777,
548 (long) semds.sem_nsems,
549 semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
550 printf("ctime = %-26.24s\n"
551 "%-10s %-10s %-10s %-10s %-10s\n",
552 ctime(&semds.sem_ctime),
553 "semnum", "value", "ncount", "zcount", "pid");
556 for (i = 0; i < semds.sem_nsems; i++) {
557 int val, ncnt, zcnt, pid;
559 val = semctl(semid, i, GETVAL, arg);
560 ncnt = semctl(semid, i, GETNCNT, arg);
561 zcnt = semctl(semid, i, GETZCNT, arg);
562 pid = semctl(semid, i, GETPID, arg);
563 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
564 bb_perror_msg_and_die("semctl");
566 printf("%-10d %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
571 int ipcs_main(int argc, char **argv);
572 int ipcs_main(int argc, char **argv)
578 #define flag_print (1<<0)
579 #define flag_msg (1<<1)
580 #define flag_sem (1<<2)
581 #define flag_shm (1<<3)
583 opt = getopt32(argc, argv, "i:aqsmtcplu", &opt_i);
584 if (opt & 0x1) { // -i
588 if (opt & 0x2) flags |= flag_msg | flag_sem | flag_shm; // -a
589 if (opt & 0x4) flags |= flag_msg; // -q
590 if (opt & 0x8) flags |= flag_sem; // -s
591 if (opt & 0x10) flags |= flag_shm; // -m
592 if (opt & 0x20) format = TIME; // -t
593 if (opt & 0x40) format = CREATOR; // -c
594 if (opt & 0x80) format = PID; // -p
595 if (opt & 0x100) format = LIMITS; // -l
596 if (opt & 0x200) format = STATUS; // -u
598 if (flags & flag_print) {
599 if (flags & flag_shm) {
601 fflush_stdout_and_exit(0);
603 if (flags & flag_sem) {
605 fflush_stdout_and_exit(0);
607 if (flags & flag_msg) {
609 fflush_stdout_and_exit(0);
614 if (!(flags & (flag_shm | flag_msg | flag_sem)))
615 flags |= flag_msg | flag_shm | flag_sem;
618 if (flags & flag_shm) {
622 if (flags & flag_sem) {
626 if (flags & flag_msg) {
630 fflush_stdout_and_exit(0);