1 /* Original author unknown, may be "krishna balasub@cis.ohio-state.edu" */
4 Modified Sat Oct 9 10:55:28 1993 for 0.99.13
6 Patches from Mike Jagdis (jaggy@purplet.demon.co.uk) applied Wed Feb
7 8 12:12:21 1995 by faith@cs.unc.edu to print numeric uids if no
10 Patch from arnolds@ifns.de (Heinz-Ado Arnolds) applied Mon Jul 1
11 19:30:41 1996 by janl@math.uio.no to add code missing in case PID:
14 Patched to display the key field -- hy@picksys.com 12/18/96
16 1999-02-22 Arkadiusz Miç§iewicz <misiek@pld.ORG.PL>
17 - added Native Language Support
31 #include <sys/types.h>
36 /* remove _() stuff */
39 /*-------------------------------------------------------------------*/
40 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
41 but inside #ifdef __KERNEL__ ... #endif */
43 /* shm_mode upper byte flags */
44 #define SHM_DEST 01000 /* segment will be destroyed on last detach */
45 #define SHM_LOCKED 02000 /* segment will not be swapped */
48 /* For older kernels the same holds for the defines below */
59 ulong shm_tot
; /* total allocated shm */
60 ulong shm_rss
; /* total resident shm */
61 ulong shm_swp
; /* total swapped shm */
72 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
76 /*-------------------------------------------------------------------*/
78 /* The last arg of semctl is a union semun, but where is it defined?
79 X/OPEN tells us to define it ourselves, but until recently
80 Linux include files would also define it. */
81 #if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
82 /* union semun is defined by including <sys/sem.h> */
84 /* according to X/OPEN we have to define it ourselves */
88 unsigned short int *array
;
89 struct seminfo
*__buf
;
93 /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
94 libc 4/5 does not mention struct ipc_term at all, but includes
95 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
96 glibc-1.09 has no support for sysv ipc.
97 glibc 2 uses __key, __seq */
98 #if defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
110 void do_shm (char format
);
111 void do_sem (char format
);
112 void do_msg (char format
);
113 void print_shm (int id
);
114 void print_msg (int id
);
115 void print_sem (int id
);
117 static char *progname
;
121 printf (_("usage : %s -asmq -tclup \n"), progname
);
122 printf (_("\t%s [-s -m -q] -i id\n"), progname
);
123 printf (_("\t%s -h for help.\n"), progname
);
129 printf (_("%s provides information on ipc facilities for"
130 " which you have read access.\n"), progname
);
131 printf (_("Resource Specification:\n\t-m : shared_mem\n\t-q : messages\n"));
132 printf (_("\t-s : semaphores\n\t-a : all (default)\n"));
133 printf (_("Output Format:\n\t-t : time\n\t-p : pid\n\t-c : creator\n"));
134 printf (_("\t-l : limits\n\t-u : summary\n"));
135 printf (_("-i id [-s -q -m] : details on resource identified by id\n"));
140 main (int argc
, char **argv
) {
141 int opt
, msg
= 0, sem
= 0, shm
= 0, id
=0, print
=0;
143 char options
[] = "atcluphsmqi:";
146 while ((opt
= getopt (argc
, argv
, options
)) != -1) {
194 usage (EXIT_FAILURE
);
196 if ( !shm
&& !msg
&& !sem
)
218 print_perms (int id
, struct ipc_perm
*ipcp
) {
222 printf ("%-10d %-10o", id
, ipcp
->mode
& 0777);
224 if ((pw
= getpwuid(ipcp
->cuid
)))
225 printf(" %-10s", pw
->pw_name
);
227 printf(" %-10d", ipcp
->cuid
);
228 if ((gr
= getgrgid(ipcp
->cgid
)))
229 printf(" %-10s", gr
->gr_name
);
231 printf(" %-10d", ipcp
->cgid
);
233 if ((pw
= getpwuid(ipcp
->uid
)))
234 printf(" %-10s", pw
->pw_name
);
236 printf(" %-10d", ipcp
->uid
);
237 if ((gr
= getgrgid(ipcp
->gid
)))
238 printf(" %-10s\n", gr
->gr_name
);
240 printf(" %-10d\n", ipcp
->gid
);
244 void do_shm (char format
)
246 int maxid
, shmid
, id
;
247 struct shmid_ds shmseg
;
248 struct shm_info shm_info
;
249 struct shminfo shminfo
;
250 struct ipc_perm
*ipcp
= &shmseg
.shm_perm
;
253 maxid
= shmctl (0, SHM_INFO
, (struct shmid_ds
*) (void *) &shm_info
);
254 if (maxid
< 0 && errno
== ENOSYS
) {
255 printf (_("kernel not configured for shared memory\n"));
261 printf (_("------ Shared Memory Limits --------\n"));
262 if ((shmctl (0, IPC_INFO
, (struct shmid_ds
*) (void *) &shminfo
)) < 0 )
264 /* glibc 2.1.3 and all earlier libc's have ints as fields
265 of struct shminfo; glibc 2.1.91 has unsigned long; ach */
266 printf (_("max number of segments = %lu\n"),
267 (unsigned long) shminfo
.shmmni
);
268 printf (_("max seg size (kbytes) = %lu\n"),
269 (unsigned long) (shminfo
.shmmax
>> 10));
270 printf (_("max total shared memory (kbytes) = %lu\n"),
271 getpagesize() / 1024 * (unsigned long) shminfo
.shmall
);
272 printf (_("min seg size (bytes) = %lu\n"),
273 (unsigned long) shminfo
.shmmin
);
277 printf (_("------ Shared Memory Status --------\n"));
278 printf (_("segments allocated %d\n"), shm_info
.used_ids
);
279 printf (_("pages allocated %ld\n"), shm_info
.shm_tot
);
280 printf (_("pages resident %ld\n"), shm_info
.shm_rss
);
281 printf (_("pages swapped %ld\n"), shm_info
.shm_swp
);
282 printf (_("Swap performance: %ld attempts\t %ld successes\n"),
283 shm_info
.swap_attempts
, shm_info
.swap_successes
);
287 printf (_("------ Shared Memory Segment Creators/Owners --------\n"));
288 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
289 _("shmid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
293 printf (_("------ Shared Memory Attach/Detach/Change Times --------\n"));
294 printf ("%-10s %-10s %-20s %-20s %-20s\n",
295 _("shmid"),_("owner"),_("attached"),_("detached"),
300 printf (_("------ Shared Memory Creator/Last-op --------\n"));
301 printf ("%-10s %-10s %-10s %-10s\n",
302 _("shmid"),_("owner"),_("cpid"),_("lpid"));
306 printf (_("------ Shared Memory Segments --------\n"));
307 printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
308 _("key"),_("shmid"),_("owner"),_("perms"),_("bytes"),
309 _("nattch"),_("status"));
313 for (id
= 0; id
<= maxid
; id
++) {
314 shmid
= shmctl (id
, SHM_STAT
, &shmseg
);
317 if (format
== CREATOR
) {
318 print_perms (shmid
, ipcp
);
321 pw
= getpwuid(ipcp
->uid
);
325 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
327 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
328 /* ctime uses static buffer: use separate calls */
329 printf(" %-20.16s", shmseg
.shm_atime
330 ? ctime(&shmseg
.shm_atime
) + 4 : _("Not set"));
331 printf(" %-20.16s", shmseg
.shm_dtime
332 ? ctime(&shmseg
.shm_dtime
) + 4 : _("Not set"));
333 printf(" %-20.16s\n", shmseg
.shm_ctime
334 ? ctime(&shmseg
.shm_ctime
) + 4 : _("Not set"));
338 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
340 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
341 printf (" %-10d %-10d\n",
342 shmseg
.shm_cpid
, shmseg
.shm_lpid
);
346 printf("0x%08x ",ipcp
->KEY
);
348 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
350 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
351 printf (" %-10o %-10lu %-10ld %-6s %-6s\n",
354 * earlier: int, Austin has size_t
356 (unsigned long) shmseg
.shm_segsz
,
358 * glibc-2.1.3 and earlier has unsigned short;
359 * Austin has shmatt_t
361 (long) shmseg
.shm_nattch
,
362 ipcp
->mode
& SHM_DEST
? _("dest") : " ",
363 ipcp
->mode
& SHM_LOCKED
? _("locked") : " ");
371 void do_sem (char format
)
373 int maxid
, semid
, id
;
374 struct semid_ds semary
;
375 struct seminfo seminfo
;
376 struct ipc_perm
*ipcp
= &semary
.sem_perm
;
380 arg
.array
= (unsigned short *) (void *) &seminfo
;
381 maxid
= semctl (0, 0, SEM_INFO
, arg
);
383 printf (_("kernel not configured for semaphores\n"));
389 printf (_("------ Semaphore Limits --------\n"));
390 arg
.array
= (unsigned short *) (void *) &seminfo
; /* damn union */
391 if ((semctl (0, 0, IPC_INFO
, arg
)) < 0 )
393 printf (_("max number of arrays = %d\n"), seminfo
.semmni
);
394 printf (_("max semaphores per array = %d\n"), seminfo
.semmsl
);
395 printf (_("max semaphores system wide = %d\n"), seminfo
.semmns
);
396 printf (_("max ops per semop call = %d\n"), seminfo
.semopm
);
397 printf (_("semaphore max value = %d\n"), seminfo
.semvmx
);
401 printf (_("------ Semaphore Status --------\n"));
402 printf (_("used arrays = %d\n"), seminfo
.semusz
);
403 printf (_("allocated semaphores = %d\n"), seminfo
.semaem
);
407 printf (_("------ Semaphore Arrays Creators/Owners --------\n"));
408 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
409 _("semid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
413 printf (_("------ Semaphore Operation/Change Times --------\n"));
414 printf ("%-8s %-10s %-26.24s %-26.24s\n",
415 _("semid"),_("owner"),_("last-op"),_("last-changed"));
422 printf (_("------ Semaphore Arrays --------\n"));
423 printf ("%-10s %-10s %-10s %-10s %-10s\n",
424 _("key"),_("semid"),_("owner"),_("perms"),_("nsems"));
428 for (id
= 0; id
<= maxid
; id
++) {
429 arg
.buf
= (struct semid_ds
*) &semary
;
430 semid
= semctl (id
, 0, SEM_STAT
, arg
);
433 if (format
== CREATOR
) {
434 print_perms (semid
, ipcp
);
437 pw
= getpwuid(ipcp
->uid
);
441 printf ("%-8d %-10.10s", semid
, pw
->pw_name
);
443 printf ("%-8d %-10d", semid
, ipcp
->uid
);
444 printf (" %-26.24s", semary
.sem_otime
445 ? ctime(&semary
.sem_otime
) : _("Not set"));
446 printf (" %-26.24s\n", semary
.sem_ctime
447 ? ctime(&semary
.sem_ctime
) : _("Not set"));
453 printf("0x%08x ", ipcp
->KEY
);
455 printf ("%-10d %-10.10s", semid
, pw
->pw_name
);
457 printf ("%-10d %-10d", semid
, ipcp
->uid
);
458 printf (" %-10o %-10ld\n",
461 * glibc-2.1.3 and earlier has unsigned short;
462 * glibc-2.1.91 has variation between
463 * unsigned short and unsigned long
464 * Austin prescribes unsigned short.
466 (long) semary
.sem_nsems
);
473 void do_msg (char format
)
476 int maxid
, msqid
, id
;
477 struct msqid_ds msgque
;
478 struct msginfo msginfo
;
479 struct ipc_perm
*ipcp
= &msgque
.msg_perm
;
482 maxid
= msgctl (0, MSG_INFO
, (struct msqid_ds
*) (void *) &msginfo
);
484 printf (_("kernel not configured for message queues\n"));
490 if ((msgctl (0, IPC_INFO
, (struct msqid_ds
*) (void *) &msginfo
)) < 0 )
492 printf (_("------ Messages: Limits --------\n"));
493 printf (_("max queues system wide = %d\n"), msginfo
.msgmni
);
494 printf (_("max size of message (bytes) = %d\n"), msginfo
.msgmax
);
495 printf (_("default max size of queue (bytes) = %d\n"), msginfo
.msgmnb
);
499 printf (_("------ Messages: Status --------\n"));
500 printf (_("allocated queues = %d\n"), msginfo
.msgpool
);
501 printf (_("used headers = %d\n"), msginfo
.msgmap
);
502 printf (_("used space = %d bytes\n"), msginfo
.msgtql
);
506 printf (_("------ Message Queues: Creators/Owners --------\n"));
507 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
508 _("msqid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
512 printf (_("------ Message Queues Send/Recv/Change Times --------\n"));
513 printf ("%-8s %-10s %-20s %-20s %-20s\n",
514 _("msqid"),_("owner"),_("send"),_("recv"),_("change"));
518 printf (_("------ Message Queues PIDs --------\n"));
519 printf ("%-10s %-10s %-10s %-10s\n",
520 _("msqid"),_("owner"),_("lspid"),_("lrpid"));
524 printf (_("------ Message Queues --------\n"));
525 printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
526 _("key"), _("msqid"), _("owner"), _("perms"),
527 _("used-bytes"), _("messages"));
531 for (id
= 0; id
<= maxid
; id
++) {
532 msqid
= msgctl (id
, MSG_STAT
, &msgque
);
535 if (format
== CREATOR
) {
536 print_perms (msqid
, ipcp
);
539 pw
= getpwuid(ipcp
->uid
);
543 printf ("%-8d %-10.10s", msqid
, pw
->pw_name
);
545 printf ("%-8d %-10d", msqid
, ipcp
->uid
);
546 printf (" %-20.16s", msgque
.msg_stime
547 ? ctime(&msgque
.msg_stime
) + 4 : _("Not set"));
548 printf (" %-20.16s", msgque
.msg_rtime
549 ? ctime(&msgque
.msg_rtime
) + 4 : _("Not set"));
550 printf (" %-20.16s\n", msgque
.msg_ctime
551 ? ctime(&msgque
.msg_ctime
) + 4 : _("Not set"));
555 printf ("%-8d %-10.10s", msqid
, pw
->pw_name
);
557 printf ("%-8d %-10d", msqid
, ipcp
->uid
);
558 printf (" %5d %5d\n",
559 msgque
.msg_lspid
, msgque
.msg_lrpid
);
563 printf( "0x%08x ",ipcp
->KEY
);
565 printf ("%-10d %-10.10s", msqid
, pw
->pw_name
);
567 printf ("%-10d %-10d", msqid
, ipcp
->uid
);
568 printf (" %-10o %-12ld %-12ld\n",
571 * glibc-2.1.3 and earlier has unsigned short;
572 * glibc-2.1.91 has variation between
573 * unsigned short, unsigned long
574 * Austin has msgqnum_t
576 (long) msgque
.msg_cbytes
,
577 (long) msgque
.msg_qnum
);
586 void print_shm (int shmid
)
588 struct shmid_ds shmds
;
589 struct ipc_perm
*ipcp
= &shmds
.shm_perm
;
591 if (shmctl (shmid
, IPC_STAT
, &shmds
) == -1)
592 err(EXIT_FAILURE
, _("shmctl failed"));
594 printf (_("\nShared memory Segment shmid=%d\n"), shmid
);
595 printf (_("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"),
596 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
);
597 printf (_("mode=%#o\taccess_perms=%#o\n"),
598 ipcp
->mode
, ipcp
->mode
& 0777);
599 printf (_("bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n"),
600 (long) shmds
.shm_segsz
, shmds
.shm_lpid
, shmds
.shm_cpid
,
601 (long) shmds
.shm_nattch
);
602 printf (_("att_time=%-26.24s\n"),
603 shmds
.shm_atime
? ctime (&shmds
.shm_atime
) : _("Not set"));
604 printf (_("det_time=%-26.24s\n"),
605 shmds
.shm_dtime
? ctime (&shmds
.shm_dtime
) : _("Not set"));
606 printf (_("change_time=%-26.24s\n"), ctime (&shmds
.shm_ctime
));
612 void print_msg (int msqid
)
616 struct ipc_perm
*ipcp
= &buf
.msg_perm
;
618 if (msgctl (msqid
, IPC_STAT
, &buf
) == -1)
619 err(EXIT_FAILURE
, _("msgctl failed"));
621 printf (_("\nMessage Queue msqid=%d\n"), msqid
);
622 printf (_("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"),
623 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
, ipcp
->mode
);
624 printf (_("cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n"),
626 * glibc-2.1.3 and earlier has unsigned short;
627 * glibc-2.1.91 has variation between
628 * unsigned short, unsigned long
629 * Austin has msgqnum_t (for msg_qbytes)
631 (long) buf
.msg_cbytes
, (long) buf
.msg_qbytes
,
632 (long) buf
.msg_qnum
, buf
.msg_lspid
, buf
.msg_lrpid
);
633 printf (_("send_time=%-26.24s\n"),
634 buf
.msg_stime
? ctime (&buf
.msg_stime
) : _("Not set"));
635 printf (_("rcv_time=%-26.24s\n"),
636 buf
.msg_rtime
? ctime (&buf
.msg_rtime
) : _("Not set"));
637 printf (_("change_time=%-26.24s\n"),
638 buf
.msg_ctime
? ctime (&buf
.msg_ctime
) : _("Not set"));
644 void print_sem (int semid
)
646 struct semid_ds semds
;
647 struct ipc_perm
*ipcp
= &semds
.sem_perm
;
652 if (semctl (semid
, 0, IPC_STAT
, arg
) < 0)
653 err(EXIT_FAILURE
, _("semctl failed"));
655 printf (_("\nSemaphore Array semid=%d\n"), semid
);
656 printf (_("uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"),
657 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
);
658 printf (_("mode=%#o, access_perms=%#o\n"),
659 ipcp
->mode
, ipcp
->mode
& 0777);
660 printf (_("nsems = %ld\n"), (long) semds
.sem_nsems
);
661 printf (_("otime = %-26.24s\n"),
662 semds
.sem_otime
? ctime (&semds
.sem_otime
) : _("Not set"));
663 printf (_("ctime = %-26.24s\n"), ctime (&semds
.sem_ctime
));
665 printf ("%-10s %-10s %-10s %-10s %-10s\n",
666 _("semnum"),_("value"),_("ncount"),_("zcount"),_("pid"));
668 for (i
=0; i
< semds
.sem_nsems
; i
++) {
669 int val
, ncnt
, zcnt
, pid
;
670 val
= semctl (semid
, i
, GETVAL
, arg
);
671 ncnt
= semctl (semid
, i
, GETNCNT
, arg
);
672 zcnt
= semctl (semid
, i
, GETZCNT
, arg
);
673 pid
= semctl (semid
, i
, GETPID
, arg
);
674 if (val
< 0 || ncnt
< 0 || zcnt
< 0 || pid
< 0)
675 err(EXIT_FAILURE
, _("semctl failed"));
677 printf ("%-10d %-10d %-10d %-10d %-10d\n",
678 i
, val
, ncnt
, zcnt
, pid
);