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 typedef unsigned long ulong
;
40 void err(int eval
, const char *fmt
, ...)
44 vfprintf(stderr
, fmt
, ap
);
50 /*-------------------------------------------------------------------*/
51 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
52 but inside #ifdef __KERNEL__ ... #endif */
54 /* shm_mode upper byte flags */
55 #define SHM_DEST 01000 /* segment will be destroyed on last detach */
56 #define SHM_LOCKED 02000 /* segment will not be swapped */
59 /* For older kernels the same holds for the defines below */
70 ulong shm_tot
; /* total allocated shm */
71 ulong shm_rss
; /* total resident shm */
72 ulong shm_swp
; /* total swapped shm */
83 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
87 /*-------------------------------------------------------------------*/
89 /* The last arg of semctl is a union semun, but where is it defined?
90 X/OPEN tells us to define it ourselves, but until recently
91 Linux include files would also define it. */
92 #if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
93 /* union semun is defined by including <sys/sem.h> */
95 /* according to X/OPEN we have to define it ourselves */
99 unsigned short int *array
;
100 struct seminfo
*__buf
;
104 /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
105 libc 4/5 does not mention struct ipc_term at all, but includes
106 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
107 glibc-1.09 has no support for sysv ipc.
108 glibc 2 uses __key, __seq */
109 #if defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
121 void do_shm (char format
);
122 void do_sem (char format
);
123 void do_msg (char format
);
124 void print_shm (int id
);
125 void print_msg (int id
);
126 void print_sem (int id
);
128 static char *progname
;
132 printf (_("usage : %s -asmq -tclup \n"), progname
);
133 printf (_("\t%s [-s -m -q] -i id\n"), progname
);
134 printf (_("\t%s -h for help.\n"), progname
);
140 printf (_("%s provides information on ipc facilities for"
141 " which you have read access.\n"), progname
);
142 printf (_("Resource Specification:\n\t-m : shared_mem\n\t-q : messages\n"));
143 printf (_("\t-s : semaphores\n\t-a : all (default)\n"));
144 printf (_("Output Format:\n\t-t : time\n\t-p : pid\n\t-c : creator\n"));
145 printf (_("\t-l : limits\n\t-u : summary\n"));
146 printf (_("-i id [-s -q -m] : details on resource identified by id\n"));
151 main (int argc
, char **argv
) {
152 int opt
, msg
= 0, sem
= 0, shm
= 0, id
=0, print
=0;
154 char options
[] = "atcluphsmqi:";
157 while ((opt
= getopt (argc
, argv
, options
)) != -1) {
205 usage (EXIT_FAILURE
);
207 if ( !shm
&& !msg
&& !sem
)
229 print_perms (int id
, struct ipc_perm
*ipcp
) {
233 printf ("%-10d %-10o", id
, ipcp
->mode
& 0777);
235 if ((pw
= getpwuid(ipcp
->cuid
)))
236 printf(" %-10s", pw
->pw_name
);
238 printf(" %-10d", ipcp
->cuid
);
239 if ((gr
= getgrgid(ipcp
->cgid
)))
240 printf(" %-10s", gr
->gr_name
);
242 printf(" %-10d", ipcp
->cgid
);
244 if ((pw
= getpwuid(ipcp
->uid
)))
245 printf(" %-10s", pw
->pw_name
);
247 printf(" %-10d", ipcp
->uid
);
248 if ((gr
= getgrgid(ipcp
->gid
)))
249 printf(" %-10s\n", gr
->gr_name
);
251 printf(" %-10d\n", ipcp
->gid
);
255 void do_shm (char format
)
257 int maxid
, shmid
, id
;
258 struct shmid_ds shmseg
;
259 struct shm_info shm_info
;
260 struct shminfo shminfo
;
261 struct ipc_perm
*ipcp
= &shmseg
.shm_perm
;
264 maxid
= shmctl (0, SHM_INFO
, (struct shmid_ds
*) (void *) &shm_info
);
265 if (maxid
< 0 && errno
== ENOSYS
) {
266 printf (_("kernel not configured for shared memory\n"));
272 printf (_("------ Shared Memory Limits --------\n"));
273 if ((shmctl (0, IPC_INFO
, (struct shmid_ds
*) (void *) &shminfo
)) < 0 )
275 /* glibc 2.1.3 and all earlier libc's have ints as fields
276 of struct shminfo; glibc 2.1.91 has unsigned long; ach */
277 printf (_("max number of segments = %lu\n"),
278 (unsigned long) shminfo
.shmmni
);
279 printf (_("max seg size (kbytes) = %lu\n"),
280 (unsigned long) (shminfo
.shmmax
>> 10));
281 printf (_("max total shared memory (kbytes) = %lu\n"),
282 getpagesize() / 1024 * (unsigned long) shminfo
.shmall
);
283 printf (_("min seg size (bytes) = %lu\n"),
284 (unsigned long) shminfo
.shmmin
);
288 printf (_("------ Shared Memory Status --------\n"));
289 printf (_("segments allocated %d\n"), shm_info
.used_ids
);
290 printf (_("pages allocated %ld\n"), shm_info
.shm_tot
);
291 printf (_("pages resident %ld\n"), shm_info
.shm_rss
);
292 printf (_("pages swapped %ld\n"), shm_info
.shm_swp
);
293 printf (_("Swap performance: %ld attempts\t %ld successes\n"),
294 shm_info
.swap_attempts
, shm_info
.swap_successes
);
298 printf (_("------ Shared Memory Segment Creators/Owners --------\n"));
299 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
300 _("shmid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
304 printf (_("------ Shared Memory Attach/Detach/Change Times --------\n"));
305 printf ("%-10s %-10s %-20s %-20s %-20s\n",
306 _("shmid"),_("owner"),_("attached"),_("detached"),
311 printf (_("------ Shared Memory Creator/Last-op --------\n"));
312 printf ("%-10s %-10s %-10s %-10s\n",
313 _("shmid"),_("owner"),_("cpid"),_("lpid"));
317 printf (_("------ Shared Memory Segments --------\n"));
318 printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
319 _("key"),_("shmid"),_("owner"),_("perms"),_("bytes"),
320 _("nattch"),_("status"));
324 for (id
= 0; id
<= maxid
; id
++) {
325 shmid
= shmctl (id
, SHM_STAT
, &shmseg
);
328 if (format
== CREATOR
) {
329 print_perms (shmid
, ipcp
);
332 pw
= getpwuid(ipcp
->uid
);
336 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
338 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
339 /* ctime uses static buffer: use separate calls */
340 printf(" %-20.16s", shmseg
.shm_atime
341 ? ctime(&shmseg
.shm_atime
) + 4 : _("Not set"));
342 printf(" %-20.16s", shmseg
.shm_dtime
343 ? ctime(&shmseg
.shm_dtime
) + 4 : _("Not set"));
344 printf(" %-20.16s\n", shmseg
.shm_ctime
345 ? ctime(&shmseg
.shm_ctime
) + 4 : _("Not set"));
349 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
351 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
352 printf (" %-10d %-10d\n",
353 shmseg
.shm_cpid
, shmseg
.shm_lpid
);
357 printf("0x%08x ",ipcp
->KEY
);
359 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
361 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
362 printf (" %-10o %-10lu %-10ld %-6s %-6s\n",
365 * earlier: int, Austin has size_t
367 (unsigned long) shmseg
.shm_segsz
,
369 * glibc-2.1.3 and earlier has unsigned short;
370 * Austin has shmatt_t
372 (long) shmseg
.shm_nattch
,
373 ipcp
->mode
& SHM_DEST
? _("dest") : " ",
374 ipcp
->mode
& SHM_LOCKED
? _("locked") : " ");
382 void do_sem (char format
)
384 int maxid
, semid
, id
;
385 struct semid_ds semary
;
386 struct seminfo seminfo
;
387 struct ipc_perm
*ipcp
= &semary
.sem_perm
;
391 arg
.array
= (unsigned short *) (void *) &seminfo
;
392 maxid
= semctl (0, 0, SEM_INFO
, arg
);
394 printf (_("kernel not configured for semaphores\n"));
400 printf (_("------ Semaphore Limits --------\n"));
401 arg
.array
= (unsigned short *) (void *) &seminfo
; /* damn union */
402 if ((semctl (0, 0, IPC_INFO
, arg
)) < 0 )
404 printf (_("max number of arrays = %d\n"), seminfo
.semmni
);
405 printf (_("max semaphores per array = %d\n"), seminfo
.semmsl
);
406 printf (_("max semaphores system wide = %d\n"), seminfo
.semmns
);
407 printf (_("max ops per semop call = %d\n"), seminfo
.semopm
);
408 printf (_("semaphore max value = %d\n"), seminfo
.semvmx
);
412 printf (_("------ Semaphore Status --------\n"));
413 printf (_("used arrays = %d\n"), seminfo
.semusz
);
414 printf (_("allocated semaphores = %d\n"), seminfo
.semaem
);
418 printf (_("------ Semaphore Arrays Creators/Owners --------\n"));
419 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
420 _("semid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
424 printf (_("------ Semaphore Operation/Change Times --------\n"));
425 printf ("%-8s %-10s %-26.24s %-26.24s\n",
426 _("semid"),_("owner"),_("last-op"),_("last-changed"));
433 printf (_("------ Semaphore Arrays --------\n"));
434 printf ("%-10s %-10s %-10s %-10s %-10s\n",
435 _("key"),_("semid"),_("owner"),_("perms"),_("nsems"));
439 for (id
= 0; id
<= maxid
; id
++) {
440 arg
.buf
= (struct semid_ds
*) &semary
;
441 semid
= semctl (id
, 0, SEM_STAT
, arg
);
444 if (format
== CREATOR
) {
445 print_perms (semid
, ipcp
);
448 pw
= getpwuid(ipcp
->uid
);
452 printf ("%-8d %-10.10s", semid
, pw
->pw_name
);
454 printf ("%-8d %-10d", semid
, ipcp
->uid
);
455 printf (" %-26.24s", semary
.sem_otime
456 ? ctime(&semary
.sem_otime
) : _("Not set"));
457 printf (" %-26.24s\n", semary
.sem_ctime
458 ? ctime(&semary
.sem_ctime
) : _("Not set"));
464 printf("0x%08x ", ipcp
->KEY
);
466 printf ("%-10d %-10.10s", semid
, pw
->pw_name
);
468 printf ("%-10d %-10d", semid
, ipcp
->uid
);
469 printf (" %-10o %-10ld\n",
472 * glibc-2.1.3 and earlier has unsigned short;
473 * glibc-2.1.91 has variation between
474 * unsigned short and unsigned long
475 * Austin prescribes unsigned short.
477 (long) semary
.sem_nsems
);
484 void do_msg (char format
)
487 int maxid
, msqid
, id
;
488 struct msqid_ds msgque
;
489 struct msginfo msginfo
;
490 struct ipc_perm
*ipcp
= &msgque
.msg_perm
;
493 maxid
= msgctl (0, MSG_INFO
, (struct msqid_ds
*) (void *) &msginfo
);
495 printf (_("kernel not configured for message queues\n"));
501 if ((msgctl (0, IPC_INFO
, (struct msqid_ds
*) (void *) &msginfo
)) < 0 )
503 printf (_("------ Messages: Limits --------\n"));
504 printf (_("max queues system wide = %d\n"), msginfo
.msgmni
);
505 printf (_("max size of message (bytes) = %d\n"), msginfo
.msgmax
);
506 printf (_("default max size of queue (bytes) = %d\n"), msginfo
.msgmnb
);
510 printf (_("------ Messages: Status --------\n"));
511 printf (_("allocated queues = %d\n"), msginfo
.msgpool
);
512 printf (_("used headers = %d\n"), msginfo
.msgmap
);
513 printf (_("used space = %d bytes\n"), msginfo
.msgtql
);
517 printf (_("------ Message Queues: Creators/Owners --------\n"));
518 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
519 _("msqid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
523 printf (_("------ Message Queues Send/Recv/Change Times --------\n"));
524 printf ("%-8s %-10s %-20s %-20s %-20s\n",
525 _("msqid"),_("owner"),_("send"),_("recv"),_("change"));
529 printf (_("------ Message Queues PIDs --------\n"));
530 printf ("%-10s %-10s %-10s %-10s\n",
531 _("msqid"),_("owner"),_("lspid"),_("lrpid"));
535 printf (_("------ Message Queues --------\n"));
536 printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
537 _("key"), _("msqid"), _("owner"), _("perms"),
538 _("used-bytes"), _("messages"));
542 for (id
= 0; id
<= maxid
; id
++) {
543 msqid
= msgctl (id
, MSG_STAT
, &msgque
);
546 if (format
== CREATOR
) {
547 print_perms (msqid
, ipcp
);
550 pw
= getpwuid(ipcp
->uid
);
554 printf ("%-8d %-10.10s", msqid
, pw
->pw_name
);
556 printf ("%-8d %-10d", msqid
, ipcp
->uid
);
557 printf (" %-20.16s", msgque
.msg_stime
558 ? ctime(&msgque
.msg_stime
) + 4 : _("Not set"));
559 printf (" %-20.16s", msgque
.msg_rtime
560 ? ctime(&msgque
.msg_rtime
) + 4 : _("Not set"));
561 printf (" %-20.16s\n", msgque
.msg_ctime
562 ? ctime(&msgque
.msg_ctime
) + 4 : _("Not set"));
566 printf ("%-8d %-10.10s", msqid
, pw
->pw_name
);
568 printf ("%-8d %-10d", msqid
, ipcp
->uid
);
569 printf (" %5d %5d\n",
570 msgque
.msg_lspid
, msgque
.msg_lrpid
);
574 printf( "0x%08x ",ipcp
->KEY
);
576 printf ("%-10d %-10.10s", msqid
, pw
->pw_name
);
578 printf ("%-10d %-10d", msqid
, ipcp
->uid
);
579 printf (" %-10o %-12ld %-12ld\n",
582 * glibc-2.1.3 and earlier has unsigned short;
583 * glibc-2.1.91 has variation between
584 * unsigned short, unsigned long
585 * Austin has msgqnum_t
587 (long) msgque
.msg_cbytes
,
588 (long) msgque
.msg_qnum
);
597 void print_shm (int shmid
)
599 struct shmid_ds shmds
;
600 struct ipc_perm
*ipcp
= &shmds
.shm_perm
;
602 if (shmctl (shmid
, IPC_STAT
, &shmds
) == -1)
603 err(EXIT_FAILURE
, _("shmctl failed"));
605 printf (_("\nShared memory Segment shmid=%d\n"), shmid
);
606 printf (_("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"),
607 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
);
608 printf (_("mode=%#o\taccess_perms=%#o\n"),
609 ipcp
->mode
, ipcp
->mode
& 0777);
610 printf (_("bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n"),
611 (long) shmds
.shm_segsz
, shmds
.shm_lpid
, shmds
.shm_cpid
,
612 (long) shmds
.shm_nattch
);
613 printf (_("att_time=%-26.24s\n"),
614 shmds
.shm_atime
? ctime (&shmds
.shm_atime
) : _("Not set"));
615 printf (_("det_time=%-26.24s\n"),
616 shmds
.shm_dtime
? ctime (&shmds
.shm_dtime
) : _("Not set"));
617 printf (_("change_time=%-26.24s\n"), ctime (&shmds
.shm_ctime
));
623 void print_msg (int msqid
)
627 struct ipc_perm
*ipcp
= &buf
.msg_perm
;
629 if (msgctl (msqid
, IPC_STAT
, &buf
) == -1)
630 err(EXIT_FAILURE
, _("msgctl failed"));
632 printf (_("\nMessage Queue msqid=%d\n"), msqid
);
633 printf (_("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"),
634 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
, ipcp
->mode
);
635 printf (_("cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n"),
637 * glibc-2.1.3 and earlier has unsigned short;
638 * glibc-2.1.91 has variation between
639 * unsigned short, unsigned long
640 * Austin has msgqnum_t (for msg_qbytes)
642 (long) buf
.msg_cbytes
, (long) buf
.msg_qbytes
,
643 (long) buf
.msg_qnum
, buf
.msg_lspid
, buf
.msg_lrpid
);
644 printf (_("send_time=%-26.24s\n"),
645 buf
.msg_stime
? ctime (&buf
.msg_stime
) : _("Not set"));
646 printf (_("rcv_time=%-26.24s\n"),
647 buf
.msg_rtime
? ctime (&buf
.msg_rtime
) : _("Not set"));
648 printf (_("change_time=%-26.24s\n"),
649 buf
.msg_ctime
? ctime (&buf
.msg_ctime
) : _("Not set"));
655 void print_sem (int semid
)
657 struct semid_ds semds
;
658 struct ipc_perm
*ipcp
= &semds
.sem_perm
;
663 if (semctl (semid
, 0, IPC_STAT
, arg
) < 0)
664 err(EXIT_FAILURE
, _("semctl failed"));
666 printf (_("\nSemaphore Array semid=%d\n"), semid
);
667 printf (_("uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"),
668 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
);
669 printf (_("mode=%#o, access_perms=%#o\n"),
670 ipcp
->mode
, ipcp
->mode
& 0777);
671 printf (_("nsems = %ld\n"), (long) semds
.sem_nsems
);
672 printf (_("otime = %-26.24s\n"),
673 semds
.sem_otime
? ctime (&semds
.sem_otime
) : _("Not set"));
674 printf (_("ctime = %-26.24s\n"), ctime (&semds
.sem_ctime
));
676 printf ("%-10s %-10s %-10s %-10s %-10s\n",
677 _("semnum"),_("value"),_("ncount"),_("zcount"),_("pid"));
679 for (i
=0; i
< semds
.sem_nsems
; i
++) {
680 int val
, ncnt
, zcnt
, pid
;
681 val
= semctl (semid
, i
, GETVAL
, arg
);
682 ncnt
= semctl (semid
, i
, GETNCNT
, arg
);
683 zcnt
= semctl (semid
, i
, GETZCNT
, arg
);
684 pid
= semctl (semid
, i
, GETPID
, arg
);
685 if (val
< 0 || ncnt
< 0 || zcnt
< 0 || pid
< 0)
686 err(EXIT_FAILURE
, _("semctl failed"));
688 printf ("%-10d %-10d %-10d %-10d %-10d\n",
689 i
, val
, ncnt
, zcnt
, pid
);