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 //usage:#define ipcs_trivial_usage
12 //usage: "[[-smq] -i shmid] | [[-asmq] [-tcplu]]"
13 //usage:#define ipcs_full_usage "\n\n"
14 //usage: " -i Show specific resource"
15 //usage: "\nResource specification:"
16 //usage: "\n -m Shared memory segments"
17 //usage: "\n -q Message queues"
18 //usage: "\n -s Semaphore arrays"
19 //usage: "\n -a All (default)"
20 //usage: "\nOutput format:"
22 //usage: "\n -c Creator"
24 //usage: "\n -l Limits"
25 //usage: "\n -u Summary"
27 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
28 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
29 /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
30 #include <sys/types.h>
38 /*-------------------------------------------------------------------*/
39 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
40 but inside #ifdef __KERNEL__ ... #endif */
42 /* shm_mode upper byte flags */
43 #define SHM_DEST 01000 /* segment will be destroyed on last detach */
44 #define SHM_LOCKED 02000 /* segment will not be swapped */
47 /* For older kernels the same holds for the defines below */
58 unsigned long shm_tot; /* total allocated shm */
59 unsigned long shm_rss; /* total resident shm */
60 unsigned long shm_swp; /* total swapped shm */
61 unsigned long swap_attempts;
62 unsigned long swap_successes;
71 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
75 /*-------------------------------------------------------------------*/
77 /* The last arg of semctl is a union semun, but where is it defined?
78 X/OPEN tells us to define it ourselves, but until recently
79 Linux include files would also define it. */
80 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
81 /* union semun is defined by including <sys/sem.h> */
83 /* according to X/OPEN we have to define it ourselves */
87 unsigned short *array;
88 struct seminfo *__buf;
92 /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
93 libc 4/5 does not mention struct ipc_term at all, but includes
94 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
95 glibc-1.09 has no support for sysv ipc.
96 glibc 2 uses __key, __seq */
97 #if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
111 static void print_perms(int id, struct ipc_perm *ipcp)
116 printf("%-10d %-10o", id, ipcp->mode & 0777);
118 pw = getpwuid(ipcp->cuid);
119 if (pw) printf(" %-10s", pw->pw_name);
120 else printf(" %-10d", ipcp->cuid);
121 gr = getgrgid(ipcp->cgid);
122 if (gr) printf(" %-10s", gr->gr_name);
123 else printf(" %-10d", ipcp->cgid);
125 pw = getpwuid(ipcp->uid);
126 if (pw) printf(" %-10s", pw->pw_name);
127 else printf(" %-10d", ipcp->uid);
128 gr = getgrgid(ipcp->gid);
129 if (gr) printf(" %-10s\n", gr->gr_name);
130 else printf(" %-10d\n", ipcp->gid);
134 static NOINLINE void do_shm(void)
136 int maxid, shmid, id;
137 struct shmid_ds shmseg;
138 struct shm_info shm_info;
139 struct shminfo shminfo;
140 struct ipc_perm *ipcp = &shmseg.shm_perm;
143 maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
145 printf("kernel not configured for %s\n", "shared memory");
151 printf("------ Shared Memory %s --------\n", "Limits");
152 if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
154 /* glibc 2.1.3 and all earlier libc's have ints as fields
155 * of struct shminfo; glibc 2.1.91 has unsigned long; ach */
156 printf("max number of segments = %lu\n"
157 "max seg size (kbytes) = %lu\n"
158 "max total shared memory (pages) = %lu\n"
159 "min seg size (bytes) = %lu\n",
160 (unsigned long) shminfo.shmmni,
161 (unsigned long) (shminfo.shmmax >> 10),
162 (unsigned long) shminfo.shmall,
163 (unsigned long) shminfo.shmmin);
167 printf("------ Shared Memory %s --------\n", "Status");
168 printf("segments allocated %d\n"
169 "pages allocated %ld\n"
170 "pages resident %ld\n"
171 "pages swapped %ld\n"
172 "Swap performance: %ld attempts\t%ld successes\n",
177 shm_info.swap_attempts, shm_info.swap_successes);
181 printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
182 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
183 "shmid", "perms", "cuid", "cgid", "uid", "gid");
187 printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
188 printf("%-10s %-10s %-20s %-20s %-20s\n",
189 "shmid", "owner", "attached", "detached", "changed");
193 printf("------ Shared Memory %s --------\n", "Creator/Last-op");
194 printf("%-10s %-10s %-10s %-10s\n",
195 "shmid", "owner", "cpid", "lpid");
199 printf("------ Shared Memory %s --------\n", "Segments");
200 printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
201 "key", "shmid", "owner", "perms", "bytes", "nattch",
206 for (id = 0; id <= maxid; id++) {
207 shmid = shmctl(id, SHM_STAT, &shmseg);
210 if (format == CREATOR) {
211 print_perms(shmid, ipcp);
214 pw = getpwuid(ipcp->uid);
218 printf("%-10d %-10.10s", shmid, pw->pw_name);
220 printf("%-10d %-10d", shmid, ipcp->uid);
221 /* ctime uses static buffer: use separate calls */
222 printf(" %-20.16s", shmseg.shm_atime
223 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
224 printf(" %-20.16s", shmseg.shm_dtime
225 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
226 printf(" %-20.16s\n", shmseg.shm_ctime
227 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
231 printf("%-10d %-10.10s", shmid, pw->pw_name);
233 printf("%-10d %-10d", shmid, ipcp->uid);
234 printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
238 printf("0x%08x ", ipcp->KEY);
240 printf("%-10d %-10.10s", shmid, pw->pw_name);
242 printf("%-10d %-10d", shmid, ipcp->uid);
243 printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
245 * earlier: int, Austin has size_t
247 (unsigned long) shmseg.shm_segsz,
249 * glibc-2.1.3 and earlier has unsigned short;
250 * Austin has shmatt_t
252 (long) shmseg.shm_nattch,
253 ipcp->mode & SHM_DEST ? "dest" : " ",
254 ipcp->mode & SHM_LOCKED ? "locked" : " ");
261 static NOINLINE void do_sem(void)
263 int maxid, semid, id;
264 struct semid_ds semary;
265 struct seminfo seminfo;
266 struct ipc_perm *ipcp = &semary.sem_perm;
270 arg.array = (unsigned short *) (void *) &seminfo;
271 maxid = semctl(0, 0, SEM_INFO, arg);
273 printf("kernel not configured for %s\n", "semaphores");
279 printf("------ Semaphore %s --------\n", "Limits");
280 arg.array = (unsigned short *) (void *) &seminfo; /* damn union */
281 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
283 printf("max number of arrays = %d\n"
284 "max semaphores per array = %d\n"
285 "max semaphores system wide = %d\n"
286 "max ops per semop call = %d\n"
287 "semaphore max value = %d\n",
290 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
294 printf("------ Semaphore %s --------\n", "Status");
295 printf("used arrays = %d\n"
296 "allocated semaphores = %d\n",
297 seminfo.semusz, seminfo.semaem);
301 printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
302 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
303 "semid", "perms", "cuid", "cgid", "uid", "gid");
307 printf("------ Shared Memory %s --------\n", "Operation/Change Times");
308 printf("%-8s %-10s %-26.24s %-26.24s\n",
309 "shmid", "owner", "last-op", "last-changed");
316 printf("------ Semaphore %s --------\n", "Arrays");
317 printf("%-10s %-10s %-10s %-10s %-10s\n",
318 "key", "semid", "owner", "perms", "nsems");
322 for (id = 0; id <= maxid; id++) {
323 arg.buf = (struct semid_ds *) &semary;
324 semid = semctl(id, 0, SEM_STAT, arg);
327 if (format == CREATOR) {
328 print_perms(semid, ipcp);
331 pw = getpwuid(ipcp->uid);
335 printf("%-8d %-10.10s", semid, pw->pw_name);
337 printf("%-8d %-10d", semid, ipcp->uid);
338 /* ctime uses static buffer: use separate calls */
339 printf(" %-26.24s", semary.sem_otime
340 ? ctime(&semary.sem_otime) : "Not set");
341 printf(" %-26.24s\n", semary.sem_ctime
342 ? ctime(&semary.sem_ctime) : "Not set");
348 printf("0x%08x ", ipcp->KEY);
350 printf("%-10d %-10.9s", semid, pw->pw_name);
352 printf("%-10d %-9d", semid, ipcp->uid);
353 printf(" %-10o %-10ld\n", ipcp->mode & 0777,
355 * glibc-2.1.3 and earlier has unsigned short;
356 * glibc-2.1.91 has variation between
357 * unsigned short and unsigned long
358 * Austin prescribes unsigned short.
360 (long) semary.sem_nsems);
367 static NOINLINE void do_msg(void)
369 int maxid, msqid, id;
370 struct msqid_ds msgque;
371 struct msginfo msginfo;
372 struct ipc_perm *ipcp = &msgque.msg_perm;
375 maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
377 printf("kernel not configured for %s\n", "message queues");
383 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
385 printf("------ Message%s --------\n", "s: Limits");
386 printf("max queues system wide = %d\n"
387 "max size of message (bytes) = %d\n"
388 "default max size of queue (bytes) = %d\n",
389 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
393 printf("------ Message%s --------\n", "s: Status");
394 printf("allocated queues = %d\n"
395 "used headers = %d\n"
396 "used space = %d bytes\n",
397 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
401 printf("------ Message%s --------\n", " Queues: Creators/Owners");
402 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
403 "msqid", "perms", "cuid", "cgid", "uid", "gid");
407 printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
408 printf("%-8s %-10s %-20s %-20s %-20s\n",
409 "msqid", "owner", "send", "recv", "change");
413 printf("------ Message%s --------\n", " Queues PIDs");
414 printf("%-10s %-10s %-10s %-10s\n",
415 "msqid", "owner", "lspid", "lrpid");
419 printf("------ Message%s --------\n", " Queues");
420 printf("%-10s %-10s %-10s %-10s %-12s %-12s\n",
421 "key", "msqid", "owner", "perms", "used-bytes", "messages");
425 for (id = 0; id <= maxid; id++) {
426 msqid = msgctl(id, MSG_STAT, &msgque);
429 if (format == CREATOR) {
430 print_perms(msqid, ipcp);
433 pw = getpwuid(ipcp->uid);
437 printf("%-8d %-10.10s", msqid, pw->pw_name);
439 printf("%-8d %-10d", msqid, ipcp->uid);
440 printf(" %-20.16s", msgque.msg_stime
441 ? ctime(&msgque.msg_stime) + 4 : "Not set");
442 printf(" %-20.16s", msgque.msg_rtime
443 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
444 printf(" %-20.16s\n", msgque.msg_ctime
445 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
449 printf("%-8d %-10.10s", msqid, pw->pw_name);
451 printf("%-8d %-10d", msqid, ipcp->uid);
452 printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
456 printf("0x%08x ", ipcp->KEY);
458 printf("%-10d %-10.10s", msqid, pw->pw_name);
460 printf("%-10d %-10d", msqid, ipcp->uid);
461 printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
463 * glibc-2.1.3 and earlier has unsigned short;
464 * glibc-2.1.91 has variation between
465 * unsigned short, unsigned long
466 * Austin has msgqnum_t
468 (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
475 static void print_shm(int shmid)
477 struct shmid_ds shmds;
478 struct ipc_perm *ipcp = &shmds.shm_perm;
480 if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
481 bb_perror_msg("shmctl");
485 printf("\nShared memory Segment shmid=%d\n"
486 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
487 "mode=%#o\taccess_perms=%#o\n"
488 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
490 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
491 ipcp->mode, ipcp->mode & 0777,
492 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
493 (long) shmds.shm_nattch);
494 printf("att_time=%-26.24s\n",
495 shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
496 printf("det_time=%-26.24s\n",
497 shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
498 printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
502 static void print_msg(int msqid)
505 struct ipc_perm *ipcp = &buf.msg_perm;
507 if (msgctl(msqid, IPC_STAT, &buf) == -1) {
508 bb_perror_msg("msgctl");
512 printf("\nMessage Queue msqid=%d\n"
513 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
514 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
515 msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
517 * glibc-2.1.3 and earlier has unsigned short;
518 * glibc-2.1.91 has variation between
519 * unsigned short, unsigned long
520 * Austin has msgqnum_t (for msg_qbytes)
522 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
523 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
525 printf("send_time=%-26.24s\n",
526 buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
527 printf("rcv_time=%-26.24s\n",
528 buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
529 printf("change_time=%-26.24s\n\n",
530 buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
533 static void print_sem(int semid)
535 struct semid_ds semds;
536 struct ipc_perm *ipcp = &semds.sem_perm;
541 if (semctl(semid, 0, IPC_STAT, arg)) {
542 bb_perror_msg("semctl");
546 printf("\nSemaphore Array semid=%d\n"
547 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
548 "mode=%#o, access_perms=%#o\n"
550 "otime = %-26.24s\n",
552 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
553 ipcp->mode, ipcp->mode & 0777,
554 (long) semds.sem_nsems,
555 semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
556 printf("ctime = %-26.24s\n"
557 "%-10s %-10s %-10s %-10s %-10s\n",
558 ctime(&semds.sem_ctime),
559 "semnum", "value", "ncount", "zcount", "pid");
562 for (i = 0; i < semds.sem_nsems; i++) {
563 int val, ncnt, zcnt, pid;
565 val = semctl(semid, i, GETVAL, arg);
566 ncnt = semctl(semid, i, GETNCNT, arg);
567 zcnt = semctl(semid, i, GETZCNT, arg);
568 pid = semctl(semid, i, GETPID, arg);
569 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
570 bb_perror_msg_and_die("semctl");
572 printf("%-10d %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
577 int ipcs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
578 int ipcs_main(int argc UNUSED_PARAM, char **argv)
584 #define flag_print (1<<0)
585 #define flag_msg (1<<1)
586 #define flag_sem (1<<2)
587 #define flag_shm (1<<3)
589 opt = getopt32(argv, "i:aqsmtcplu", &opt_i);
590 if (opt & 0x1) { // -i
594 if (opt & 0x2) flags |= flag_msg | flag_sem | flag_shm; // -a
595 if (opt & 0x4) flags |= flag_msg; // -q
596 if (opt & 0x8) flags |= flag_sem; // -s
597 if (opt & 0x10) flags |= flag_shm; // -m
598 if (opt & 0x20) format = TIME; // -t
599 if (opt & 0x40) format = CREATOR; // -c
600 if (opt & 0x80) format = PID; // -p
601 if (opt & 0x100) format = LIMITS; // -l
602 if (opt & 0x200) format = STATUS; // -u
604 if (flags & flag_print) {
605 if (flags & flag_shm) {
607 fflush_stdout_and_exit(EXIT_SUCCESS);
609 if (flags & flag_sem) {
611 fflush_stdout_and_exit(EXIT_SUCCESS);
613 if (flags & flag_msg) {
615 fflush_stdout_and_exit(EXIT_SUCCESS);
620 if (!(flags & (flag_shm | flag_msg | flag_sem)))
621 flags |= flag_msg | flag_shm | flag_sem;
624 if (flags & flag_shm) {
628 if (flags & flag_sem) {
632 if (flags & flag_msg) {
636 fflush_stdout_and_exit(EXIT_SUCCESS);