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 */
38 typedef unsigned long ulong
;
39 void err(int eval
, const char *fmt
, ...)
43 vfprintf(stderr
, fmt
, ap
);
48 /*-------------------------------------------------------------------*/
49 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
50 but inside #ifdef __KERNEL__ ... #endif */
52 /* shm_mode upper byte flags */
53 #define SHM_DEST 01000 /* segment will be destroyed on last detach */
54 #define SHM_LOCKED 02000 /* segment will not be swapped */
57 /* For older kernels the same holds for the defines below */
68 ulong shm_tot
; /* total allocated shm */
69 ulong shm_rss
; /* total resident shm */
70 ulong shm_swp
; /* total swapped shm */
81 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
85 /*-------------------------------------------------------------------*/
87 /* The last arg of semctl is a union semun, but where is it defined?
88 X/OPEN tells us to define it ourselves, but until recently
89 Linux include files would also define it. */
90 #if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
91 /* union semun is defined by including <sys/sem.h> */
93 /* according to X/OPEN we have to define it ourselves */
97 unsigned short int *array
;
98 struct seminfo
*__buf
;
102 /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
103 libc 4/5 does not mention struct ipc_term at all, but includes
104 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
105 glibc-1.09 has no support for sysv ipc.
106 glibc 2 uses __key, __seq */
107 #if defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
119 void do_shm (char format
);
120 void do_sem (char format
);
121 void do_msg (char format
);
122 void print_shm (int id
);
123 void print_msg (int id
);
124 void print_sem (int id
);
126 static char *progname
;
130 printf (_("usage : %s -asmq -tclup \n"), progname
);
131 printf (_("\t%s [-s -m -q] -i id\n"), progname
);
132 printf (_("\t%s -h for help.\n"), progname
);
138 printf (_("%s provides information on ipc facilities for"
139 " which you have read access.\n"), progname
);
140 printf (_("Resource Specification:\n\t-m : shared_mem\n\t-q : messages\n"));
141 printf (_("\t-s : semaphores\n\t-a : all (default)\n"));
142 printf (_("Output Format:\n\t-t : time\n\t-p : pid\n\t-c : creator\n"));
143 printf (_("\t-l : limits\n\t-u : summary\n"));
144 printf (_("-i id [-s -q -m] : details on resource identified by id\n"));
149 main (int argc
, char **argv
) {
150 int opt
, msg
= 0, sem
= 0, shm
= 0, id
=0, print
=0;
152 char options
[] = "atcluphsmqi:";
155 while ((opt
= getopt (argc
, argv
, options
)) != -1) {
203 usage (EXIT_FAILURE
);
205 if ( !shm
&& !msg
&& !sem
)
227 print_perms (int id
, struct ipc_perm
*ipcp
) {
231 printf ("%-10d %-10o", id
, ipcp
->mode
& 0777);
233 if ((pw
= getpwuid(ipcp
->cuid
)))
234 printf(" %-10s", pw
->pw_name
);
236 printf(" %-10d", ipcp
->cuid
);
237 if ((gr
= getgrgid(ipcp
->cgid
)))
238 printf(" %-10s", gr
->gr_name
);
240 printf(" %-10d", ipcp
->cgid
);
242 if ((pw
= getpwuid(ipcp
->uid
)))
243 printf(" %-10s", pw
->pw_name
);
245 printf(" %-10d", ipcp
->uid
);
246 if ((gr
= getgrgid(ipcp
->gid
)))
247 printf(" %-10s\n", gr
->gr_name
);
249 printf(" %-10d\n", ipcp
->gid
);
253 void do_shm (char format
)
255 int maxid
, shmid
, id
;
256 struct shmid_ds shmseg
;
257 struct shm_info shm_info
;
258 struct shminfo shminfo
;
259 struct ipc_perm
*ipcp
= &shmseg
.shm_perm
;
262 maxid
= shmctl (0, SHM_INFO
, (struct shmid_ds
*) (void *) &shm_info
);
263 if (maxid
< 0 && errno
== ENOSYS
) {
264 printf (_("kernel not configured for shared memory\n"));
270 printf (_("------ Shared Memory Limits --------\n"));
271 if ((shmctl (0, IPC_INFO
, (struct shmid_ds
*) (void *) &shminfo
)) < 0 )
273 /* glibc 2.1.3 and all earlier libc's have ints as fields
274 of struct shminfo; glibc 2.1.91 has unsigned long; ach */
275 printf (_("max number of segments = %lu\n"),
276 (unsigned long) shminfo
.shmmni
);
277 printf (_("max seg size (kbytes) = %lu\n"),
278 (unsigned long) (shminfo
.shmmax
>> 10));
279 printf (_("max total shared memory (kbytes) = %lu\n"),
280 getpagesize() / 1024 * (unsigned long) shminfo
.shmall
);
281 printf (_("min seg size (bytes) = %lu\n"),
282 (unsigned long) shminfo
.shmmin
);
286 printf (_("------ Shared Memory Status --------\n"));
287 printf (_("segments allocated %d\n"), shm_info
.used_ids
);
288 printf (_("pages allocated %ld\n"), shm_info
.shm_tot
);
289 printf (_("pages resident %ld\n"), shm_info
.shm_rss
);
290 printf (_("pages swapped %ld\n"), shm_info
.shm_swp
);
291 printf (_("Swap performance: %ld attempts\t %ld successes\n"),
292 shm_info
.swap_attempts
, shm_info
.swap_successes
);
296 printf (_("------ Shared Memory Segment Creators/Owners --------\n"));
297 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
298 _("shmid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
302 printf (_("------ Shared Memory Attach/Detach/Change Times --------\n"));
303 printf ("%-10s %-10s %-20s %-20s %-20s\n",
304 _("shmid"),_("owner"),_("attached"),_("detached"),
309 printf (_("------ Shared Memory Creator/Last-op --------\n"));
310 printf ("%-10s %-10s %-10s %-10s\n",
311 _("shmid"),_("owner"),_("cpid"),_("lpid"));
315 printf (_("------ Shared Memory Segments --------\n"));
316 printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
317 _("key"),_("shmid"),_("owner"),_("perms"),_("bytes"),
318 _("nattch"),_("status"));
322 for (id
= 0; id
<= maxid
; id
++) {
323 shmid
= shmctl (id
, SHM_STAT
, &shmseg
);
326 if (format
== CREATOR
) {
327 print_perms (shmid
, ipcp
);
330 pw
= getpwuid(ipcp
->uid
);
334 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
336 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
337 /* ctime uses static buffer: use separate calls */
338 printf(" %-20.16s", shmseg
.shm_atime
339 ? ctime(&shmseg
.shm_atime
) + 4 : _("Not set"));
340 printf(" %-20.16s", shmseg
.shm_dtime
341 ? ctime(&shmseg
.shm_dtime
) + 4 : _("Not set"));
342 printf(" %-20.16s\n", shmseg
.shm_ctime
343 ? ctime(&shmseg
.shm_ctime
) + 4 : _("Not set"));
347 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
349 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
350 printf (" %-10d %-10d\n",
351 shmseg
.shm_cpid
, shmseg
.shm_lpid
);
355 printf("0x%08x ",ipcp
->KEY
);
357 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
359 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
360 printf (" %-10o %-10lu %-10ld %-6s %-6s\n",
363 * earlier: int, Austin has size_t
365 (unsigned long) shmseg
.shm_segsz
,
367 * glibc-2.1.3 and earlier has unsigned short;
368 * Austin has shmatt_t
370 (long) shmseg
.shm_nattch
,
371 ipcp
->mode
& SHM_DEST
? _("dest") : " ",
372 ipcp
->mode
& SHM_LOCKED
? _("locked") : " ");
380 void do_sem (char format
)
382 int maxid
, semid
, id
;
383 struct semid_ds semary
;
384 struct seminfo seminfo
;
385 struct ipc_perm
*ipcp
= &semary
.sem_perm
;
389 arg
.array
= (unsigned short *) (void *) &seminfo
;
390 maxid
= semctl (0, 0, SEM_INFO
, arg
);
392 printf (_("kernel not configured for semaphores\n"));
398 printf (_("------ Semaphore Limits --------\n"));
399 arg
.array
= (unsigned short *) (void *) &seminfo
; /* damn union */
400 if ((semctl (0, 0, IPC_INFO
, arg
)) < 0 )
402 printf (_("max number of arrays = %d\n"), seminfo
.semmni
);
403 printf (_("max semaphores per array = %d\n"), seminfo
.semmsl
);
404 printf (_("max semaphores system wide = %d\n"), seminfo
.semmns
);
405 printf (_("max ops per semop call = %d\n"), seminfo
.semopm
);
406 printf (_("semaphore max value = %d\n"), seminfo
.semvmx
);
410 printf (_("------ Semaphore Status --------\n"));
411 printf (_("used arrays = %d\n"), seminfo
.semusz
);
412 printf (_("allocated semaphores = %d\n"), seminfo
.semaem
);
416 printf (_("------ Semaphore Arrays Creators/Owners --------\n"));
417 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
418 _("semid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
422 printf (_("------ Semaphore Operation/Change Times --------\n"));
423 printf ("%-8s %-10s %-26.24s %-26.24s\n",
424 _("semid"),_("owner"),_("last-op"),_("last-changed"));
431 printf (_("------ Semaphore Arrays --------\n"));
432 printf ("%-10s %-10s %-10s %-10s %-10s\n",
433 _("key"),_("semid"),_("owner"),_("perms"),_("nsems"));
437 for (id
= 0; id
<= maxid
; id
++) {
438 arg
.buf
= (struct semid_ds
*) &semary
;
439 semid
= semctl (id
, 0, SEM_STAT
, arg
);
442 if (format
== CREATOR
) {
443 print_perms (semid
, ipcp
);
446 pw
= getpwuid(ipcp
->uid
);
450 printf ("%-8d %-10.10s", semid
, pw
->pw_name
);
452 printf ("%-8d %-10d", semid
, ipcp
->uid
);
453 printf (" %-26.24s", semary
.sem_otime
454 ? ctime(&semary
.sem_otime
) : _("Not set"));
455 printf (" %-26.24s\n", semary
.sem_ctime
456 ? ctime(&semary
.sem_ctime
) : _("Not set"));
462 printf("0x%08x ", ipcp
->KEY
);
464 printf ("%-10d %-10.10s", semid
, pw
->pw_name
);
466 printf ("%-10d %-10d", semid
, ipcp
->uid
);
467 printf (" %-10o %-10ld\n",
470 * glibc-2.1.3 and earlier has unsigned short;
471 * glibc-2.1.91 has variation between
472 * unsigned short and unsigned long
473 * Austin prescribes unsigned short.
475 (long) semary
.sem_nsems
);
482 void do_msg (char format
)
485 int maxid
, msqid
, id
;
486 struct msqid_ds msgque
;
487 struct msginfo msginfo
;
488 struct ipc_perm
*ipcp
= &msgque
.msg_perm
;
491 maxid
= msgctl (0, MSG_INFO
, (struct msqid_ds
*) (void *) &msginfo
);
493 printf (_("kernel not configured for message queues\n"));
499 if ((msgctl (0, IPC_INFO
, (struct msqid_ds
*) (void *) &msginfo
)) < 0 )
501 printf (_("------ Messages: Limits --------\n"));
502 printf (_("max queues system wide = %d\n"), msginfo
.msgmni
);
503 printf (_("max size of message (bytes) = %d\n"), msginfo
.msgmax
);
504 printf (_("default max size of queue (bytes) = %d\n"), msginfo
.msgmnb
);
508 printf (_("------ Messages: Status --------\n"));
509 printf (_("allocated queues = %d\n"), msginfo
.msgpool
);
510 printf (_("used headers = %d\n"), msginfo
.msgmap
);
511 printf (_("used space = %d bytes\n"), msginfo
.msgtql
);
515 printf (_("------ Message Queues: Creators/Owners --------\n"));
516 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
517 _("msqid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
521 printf (_("------ Message Queues Send/Recv/Change Times --------\n"));
522 printf ("%-8s %-10s %-20s %-20s %-20s\n",
523 _("msqid"),_("owner"),_("send"),_("recv"),_("change"));
527 printf (_("------ Message Queues PIDs --------\n"));
528 printf ("%-10s %-10s %-10s %-10s\n",
529 _("msqid"),_("owner"),_("lspid"),_("lrpid"));
533 printf (_("------ Message Queues --------\n"));
534 printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
535 _("key"), _("msqid"), _("owner"), _("perms"),
536 _("used-bytes"), _("messages"));
540 for (id
= 0; id
<= maxid
; id
++) {
541 msqid
= msgctl (id
, MSG_STAT
, &msgque
);
544 if (format
== CREATOR
) {
545 print_perms (msqid
, ipcp
);
548 pw
= getpwuid(ipcp
->uid
);
552 printf ("%-8d %-10.10s", msqid
, pw
->pw_name
);
554 printf ("%-8d %-10d", msqid
, ipcp
->uid
);
555 printf (" %-20.16s", msgque
.msg_stime
556 ? ctime(&msgque
.msg_stime
) + 4 : _("Not set"));
557 printf (" %-20.16s", msgque
.msg_rtime
558 ? ctime(&msgque
.msg_rtime
) + 4 : _("Not set"));
559 printf (" %-20.16s\n", msgque
.msg_ctime
560 ? ctime(&msgque
.msg_ctime
) + 4 : _("Not set"));
564 printf ("%-8d %-10.10s", msqid
, pw
->pw_name
);
566 printf ("%-8d %-10d", msqid
, ipcp
->uid
);
567 printf (" %5d %5d\n",
568 msgque
.msg_lspid
, msgque
.msg_lrpid
);
572 printf( "0x%08x ",ipcp
->KEY
);
574 printf ("%-10d %-10.10s", msqid
, pw
->pw_name
);
576 printf ("%-10d %-10d", msqid
, ipcp
->uid
);
577 printf (" %-10o %-12ld %-12ld\n",
580 * glibc-2.1.3 and earlier has unsigned short;
581 * glibc-2.1.91 has variation between
582 * unsigned short, unsigned long
583 * Austin has msgqnum_t
585 (long) msgque
.msg_cbytes
,
586 (long) msgque
.msg_qnum
);
595 void print_shm (int shmid
)
597 struct shmid_ds shmds
;
598 struct ipc_perm
*ipcp
= &shmds
.shm_perm
;
600 if (shmctl (shmid
, IPC_STAT
, &shmds
) == -1)
601 err(EXIT_FAILURE
, _("shmctl failed"));
603 printf (_("\nShared memory Segment shmid=%d\n"), shmid
);
604 printf (_("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"),
605 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
);
606 printf (_("mode=%#o\taccess_perms=%#o\n"),
607 ipcp
->mode
, ipcp
->mode
& 0777);
608 printf (_("bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n"),
609 (long) shmds
.shm_segsz
, shmds
.shm_lpid
, shmds
.shm_cpid
,
610 (long) shmds
.shm_nattch
);
611 printf (_("att_time=%-26.24s\n"),
612 shmds
.shm_atime
? ctime (&shmds
.shm_atime
) : _("Not set"));
613 printf (_("det_time=%-26.24s\n"),
614 shmds
.shm_dtime
? ctime (&shmds
.shm_dtime
) : _("Not set"));
615 printf (_("change_time=%-26.24s\n"), ctime (&shmds
.shm_ctime
));
621 void print_msg (int msqid
)
625 struct ipc_perm
*ipcp
= &buf
.msg_perm
;
627 if (msgctl (msqid
, IPC_STAT
, &buf
) == -1)
628 err(EXIT_FAILURE
, _("msgctl failed"));
630 printf (_("\nMessage Queue msqid=%d\n"), msqid
);
631 printf (_("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"),
632 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
, ipcp
->mode
);
633 printf (_("cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n"),
635 * glibc-2.1.3 and earlier has unsigned short;
636 * glibc-2.1.91 has variation between
637 * unsigned short, unsigned long
638 * Austin has msgqnum_t (for msg_qbytes)
640 (long) buf
.msg_cbytes
, (long) buf
.msg_qbytes
,
641 (long) buf
.msg_qnum
, buf
.msg_lspid
, buf
.msg_lrpid
);
642 printf (_("send_time=%-26.24s\n"),
643 buf
.msg_stime
? ctime (&buf
.msg_stime
) : _("Not set"));
644 printf (_("rcv_time=%-26.24s\n"),
645 buf
.msg_rtime
? ctime (&buf
.msg_rtime
) : _("Not set"));
646 printf (_("change_time=%-26.24s\n"),
647 buf
.msg_ctime
? ctime (&buf
.msg_ctime
) : _("Not set"));
653 void print_sem (int semid
)
655 struct semid_ds semds
;
656 struct ipc_perm
*ipcp
= &semds
.sem_perm
;
661 if (semctl (semid
, 0, IPC_STAT
, arg
) < 0)
662 err(EXIT_FAILURE
, _("semctl failed"));
664 printf (_("\nSemaphore Array semid=%d\n"), semid
);
665 printf (_("uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"),
666 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
);
667 printf (_("mode=%#o, access_perms=%#o\n"),
668 ipcp
->mode
, ipcp
->mode
& 0777);
669 printf (_("nsems = %ld\n"), (long) semds
.sem_nsems
);
670 printf (_("otime = %-26.24s\n"),
671 semds
.sem_otime
? ctime (&semds
.sem_otime
) : _("Not set"));
672 printf (_("ctime = %-26.24s\n"), ctime (&semds
.sem_ctime
));
674 printf ("%-10s %-10s %-10s %-10s %-10s\n",
675 _("semnum"),_("value"),_("ncount"),_("zcount"),_("pid"));
677 for (i
=0; i
< semds
.sem_nsems
; i
++) {
678 int val
, ncnt
, zcnt
, pid
;
679 val
= semctl (semid
, i
, GETVAL
, arg
);
680 ncnt
= semctl (semid
, i
, GETNCNT
, arg
);
681 zcnt
= semctl (semid
, i
, GETZCNT
, arg
);
682 pid
= semctl (semid
, i
, GETPID
, arg
);
683 if (val
< 0 || ncnt
< 0 || zcnt
< 0 || pid
< 0)
684 err(EXIT_FAILURE
, _("semctl failed"));
686 printf ("%-10d %-10d %-10d %-10d %-10d\n",
687 i
, val
, ncnt
, zcnt
, pid
);