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.
11 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
12 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
13 /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
14 #include <sys/types.h>
22 /*-------------------------------------------------------------------*/
23 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
24 but inside #ifdef __KERNEL__ ... #endif */
26 /* shm_mode upper byte flags */
27 #define SHM_DEST 01000 /* segment will be destroyed on last detach */
28 #define SHM_LOCKED 02000 /* segment will not be swapped */
31 /* For older kernels the same holds for the defines below */
42 ulong shm_tot; /* total allocated shm */
43 ulong shm_rss; /* total resident shm */
44 ulong shm_swp; /* total swapped shm */
55 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
59 /*-------------------------------------------------------------------*/
61 /* The last arg of semctl is a union semun, but where is it defined?
62 X/OPEN tells us to define it ourselves, but until recently
63 Linux include files would also define it. */
64 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
65 /* union semun is defined by including <sys/sem.h> */
67 /* according to X/OPEN we have to define it ourselves */
71 unsigned short int *array;
72 struct seminfo *__buf;
76 /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
77 libc 4/5 does not mention struct ipc_term at all, but includes
78 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
79 glibc-1.09 has no support for sysv ipc.
80 glibc 2 uses __key, __seq */
81 #if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
95 static void print_perms(int id, struct ipc_perm *ipcp)
100 printf("%-10d %-10o", id, ipcp->mode & 0777);
102 if ((pw = getpwuid(ipcp->cuid)))
103 printf(" %-10s", pw->pw_name);
105 printf(" %-10d", ipcp->cuid);
106 if ((gr = getgrgid(ipcp->cgid)))
107 printf(" %-10s", gr->gr_name);
109 printf(" %-10d", ipcp->cgid);
111 if ((pw = getpwuid(ipcp->uid)))
112 printf(" %-10s", pw->pw_name);
114 printf(" %-10d", ipcp->uid);
115 if ((gr = getgrgid(ipcp->gid)))
116 printf(" %-10s\n", gr->gr_name);
118 printf(" %-10d\n", ipcp->gid);
122 static void do_shm(void)
124 int maxid, shmid, id;
125 struct shmid_ds shmseg;
126 struct shm_info shm_info;
127 struct shminfo shminfo;
128 struct ipc_perm *ipcp = &shmseg.shm_perm;
131 maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
133 printf("kernel not configured for %s\n", "shared memory");
139 printf("------ Shared Memory %s --------\n", "Limits");
140 if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
142 /* glibc 2.1.3 and all earlier libc's have ints as fields
143 of struct shminfo; glibc 2.1.91 has unsigned long; ach */
144 printf("max number of segments = %lu\n"
145 "max seg size (kbytes) = %lu\n"
146 "max total shared memory (pages) = %lu\n"
147 "min seg size (bytes) = %lu\n",
148 (unsigned long) shminfo.shmmni,
149 (unsigned long) (shminfo.shmmax >> 10),
150 (unsigned long) shminfo.shmall,
151 (unsigned long) shminfo.shmmin);
155 printf("------ Shared Memory %s --------\n", "Status");
156 printf( "segments allocated %d\n"
157 "pages allocated %ld\n"
158 "pages resident %ld\n"
159 "pages swapped %ld\n"
160 "Swap performance: %ld attempts\t%ld successes\n",
165 shm_info.swap_attempts, shm_info.swap_successes);
169 printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
170 printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n",
171 "shmid", "perms", "cuid", "cgid", "uid", "gid");
175 printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
176 printf( "%-10s %-10s %-20s %-20s %-20s\n",
177 "shmid", "owner", "attached", "detached", "changed");
181 printf("------ Shared Memory %s --------\n", "Creator/Last-op");
182 printf( "%-10s %-10s %-10s %-10s\n",
183 "shmid", "owner", "cpid", "lpid");
187 printf("------ Shared Memory %s --------\n", "Segments");
188 printf( "%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
189 "key", "shmid", "owner", "perms", "bytes", "nattch",
194 for (id = 0; id <= maxid; id++) {
195 shmid = shmctl(id, SHM_STAT, &shmseg);
198 if (format == CREATOR) {
199 print_perms(shmid, ipcp);
202 pw = getpwuid(ipcp->uid);
206 printf("%-10d %-10.10s", shmid, pw->pw_name);
208 printf("%-10d %-10d", shmid, ipcp->uid);
209 /* ctime uses static buffer: use separate calls */
210 printf(" %-20.16s", shmseg.shm_atime
211 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
212 printf(" %-20.16s", shmseg.shm_dtime
213 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
214 printf(" %-20.16s\n", shmseg.shm_ctime
215 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
219 printf("%-10d %-10.10s", shmid, pw->pw_name);
221 printf("%-10d %-10d", shmid, ipcp->uid);
222 printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
226 printf("0x%08x ", ipcp->KEY);
228 printf("%-10d %-10.10s", shmid, pw->pw_name);
230 printf("%-10d %-10d", shmid, ipcp->uid);
231 printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
233 * earlier: int, Austin has size_t
235 (unsigned long) shmseg.shm_segsz,
237 * glibc-2.1.3 and earlier has unsigned short;
238 * Austin has shmatt_t
240 (long) shmseg.shm_nattch,
241 ipcp->mode & SHM_DEST ? "dest" : " ",
242 ipcp->mode & SHM_LOCKED ? "locked" : " ");
249 static void do_sem(void)
251 int maxid, semid, id;
252 struct semid_ds semary;
253 struct seminfo seminfo;
254 struct ipc_perm *ipcp = &semary.sem_perm;
258 arg.array = (ushort *) (void *) &seminfo;
259 maxid = semctl(0, 0, SEM_INFO, arg);
261 printf("kernel not configured for %s\n", "semaphores");
267 printf("------ Semaphore %s --------\n", "Limits");
268 arg.array = (ushort *) (void *) &seminfo; /* damn union */
269 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
271 printf("max number of arrays = %d\n"
272 "max semaphores per array = %d\n"
273 "max semaphores system wide = %d\n"
274 "max ops per semop call = %d\n"
275 "semaphore max value = %d\n",
278 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
282 printf("------ Semaphore %s --------\n", "Status");
283 printf( "used arrays = %d\n"
284 "allocated semaphores = %d\n",
285 seminfo.semusz, seminfo.semaem);
289 printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
290 printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n",
291 "semid", "perms", "cuid", "cgid", "uid", "gid");
295 printf("------ Shared Memory %s --------\n", "Operation/Change Times");
296 printf( "%-8s %-10s %-26.24s %-26.24s\n",
297 "shmid", "owner", "last-op", "last-changed");
304 printf("------ Semaphore %s --------\n", "Arrays");
305 printf( "%-10s %-10s %-10s %-10s %-10s\n",
306 "key", "semid", "owner", "perms", "nsems");
310 for (id = 0; id <= maxid; id++) {
311 arg.buf = (struct semid_ds *) &semary;
312 semid = semctl(id, 0, SEM_STAT, arg);
315 if (format == CREATOR) {
316 print_perms(semid, ipcp);
319 pw = getpwuid(ipcp->uid);
323 printf("%-8d %-10.10s", semid, pw->pw_name);
325 printf("%-8d %-10d", semid, ipcp->uid);
326 /* ctime uses static buffer: use separate calls */
327 printf(" %-26.24s", semary.sem_otime
328 ? ctime(&semary.sem_otime) : "Not set");
329 printf(" %-26.24s\n", semary.sem_ctime
330 ? ctime(&semary.sem_ctime) : "Not set");
336 printf("0x%08x ", ipcp->KEY);
338 printf("%-10d %-10.9s", semid, pw->pw_name);
340 printf("%-10d %-9d", semid, ipcp->uid);
341 printf(" %-10o %-10ld\n", ipcp->mode & 0777,
343 * glibc-2.1.3 and earlier has unsigned short;
344 * glibc-2.1.91 has variation between
345 * unsigned short and unsigned long
346 * Austin prescribes unsigned short.
348 (long) semary.sem_nsems);
355 static void do_msg(void)
357 int maxid, msqid, id;
358 struct msqid_ds msgque;
359 struct msginfo msginfo;
360 struct ipc_perm *ipcp = &msgque.msg_perm;
363 maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
365 printf("kernel not configured for %s\n", "message queues");
371 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
373 printf("------ Message%s --------\n", "s: Limits");
374 printf( "max queues system wide = %d\n"
375 "max size of message (bytes) = %d\n"
376 "default max size of queue (bytes) = %d\n",
377 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
381 printf("------ Message%s --------\n", "s: Status");
382 printf( "allocated queues = %d\n"
383 "used headers = %d\n"
384 "used space = %d bytes\n",
385 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
389 printf("------ Message%s --------\n", " Queues: Creators/Owners");
390 printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n",
391 "msqid", "perms", "cuid", "cgid", "uid", "gid");
395 printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
396 printf( "%-8s %-10s %-20s %-20s %-20s\n",
397 "msqid", "owner", "send", "recv", "change");
401 printf("------ Message%s --------\n", " Queues PIDs");
402 printf( "%-10s %-10s %-10s %-10s\n",
403 "msqid", "owner", "lspid", "lrpid");
407 printf("------ Message%s --------\n", " Queues");
408 printf( "%-10s %-10s %-10s %-10s %-12s %-12s\n",
409 "key", "msqid", "owner", "perms", "used-bytes", "messages");
413 for (id = 0; id <= maxid; id++) {
414 msqid = msgctl(id, MSG_STAT, &msgque);
417 if (format == CREATOR) {
418 print_perms(msqid, ipcp);
421 pw = getpwuid(ipcp->uid);
425 printf("%-8d %-10.10s", msqid, pw->pw_name);
427 printf("%-8d %-10d", msqid, ipcp->uid);
428 printf(" %-20.16s", msgque.msg_stime
429 ? ctime(&msgque.msg_stime) + 4 : "Not set");
430 printf(" %-20.16s", msgque.msg_rtime
431 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
432 printf(" %-20.16s\n", msgque.msg_ctime
433 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
437 printf("%-8d %-10.10s", msqid, pw->pw_name);
439 printf("%-8d %-10d", msqid, ipcp->uid);
440 printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
444 printf("0x%08x ", ipcp->KEY);
446 printf("%-10d %-10.10s", msqid, pw->pw_name);
448 printf("%-10d %-10d", msqid, ipcp->uid);
449 printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
451 * glibc-2.1.3 and earlier has unsigned short;
452 * glibc-2.1.91 has variation between
453 * unsigned short, unsigned long
454 * Austin has msgqnum_t
456 (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
463 static void print_shm(int shmid)
465 struct shmid_ds shmds;
466 struct ipc_perm *ipcp = &shmds.shm_perm;
468 if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
469 bb_perror_msg("shmctl");
473 printf("\nShared memory Segment shmid=%d\n"
474 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
475 "mode=%#o\taccess_perms=%#o\n"
476 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
478 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
479 ipcp->mode, ipcp->mode & 0777,
480 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
481 (long) shmds.shm_nattch);
482 printf("att_time=%-26.24s\n",
483 shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
484 printf("det_time=%-26.24s\n",
485 shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
486 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_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_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_perror_msg_and_die("semctl");
560 printf("%-10d %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
565 int ipcs_main(int argc, char **argv);
566 int ipcs_main(int argc, char **argv)
572 #define flag_print (1<<0)
573 #define flag_msg (1<<1)
574 #define flag_sem (1<<2)
575 #define flag_shm (1<<3)
577 opt = getopt32(argc, argv, "i:aqsmtcplu", &opt_i);
578 if (opt & 0x1) { // -i
582 if (opt & 0x2) flags |= flag_msg | flag_sem | flag_shm; // -a
583 if (opt & 0x4) flags |= flag_msg; // -q
584 if (opt & 0x8) flags |= flag_sem; // -s
585 if (opt & 0x10) flags |= flag_shm; // -m
586 if (opt & 0x20) format = TIME; // -t
587 if (opt & 0x40) format = CREATOR; // -c
588 if (opt & 0x80) format = PID; // -p
589 if (opt & 0x100) format = LIMITS; // -l
590 if (opt & 0x200) format = STATUS; // -u
592 if (flags & flag_print) {
593 if (flags & flag_shm) {
595 fflush_stdout_and_exit(0);
597 if (flags & flag_sem) {
599 fflush_stdout_and_exit(0);
601 if (flags & flag_msg) {
603 fflush_stdout_and_exit(0);
608 if (!(flags & (flag_shm | flag_msg | flag_sem)))
609 flags |= flag_msg | flag_shm | flag_sem;
612 if (flags & flag_shm) {
616 if (flags & flag_sem) {
620 if (flags & flag_msg) {
624 fflush_stdout_and_exit(0);