]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/ipcs.c
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¶kiewicz <misiek@pld.ORG.PL>
17 - added Native Language Support
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 ulong shm_tot
; /* total allocated shm */
59 ulong shm_rss
; /* total resident shm */
60 ulong shm_swp
; /* total swapped shm */
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 int *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
109 void do_shm (char format
);
110 void do_sem (char format
);
111 void do_msg (char format
);
112 void print_shm (int id
);
113 void print_msg (int id
);
114 void print_sem (int id
);
116 static char *progname
;
120 printf (_("usage : %s -asmq -tclup \n"), progname
);
121 printf (_("\t%s [-s -m -q] -i id\n"), progname
);
122 printf (_("\t%s -h for help.\n"), progname
);
128 printf (_("%s provides information on ipc facilities for"
129 " which you have read access.\n"), progname
);
130 printf (_("Resource Specification:\n\t-m : shared_mem\n\t-q : messages\n"));
131 printf (_("\t-s : semaphores\n\t-a : all (default)\n"));
132 printf (_("Output Format:\n\t-t : time\n\t-p : pid\n\t-c : creator\n"));
133 printf (_("\t-l : limits\n\t-u : summary\n"));
134 printf (_("-i id [-s -q -m] : details on resource identified by id\n"));
139 main (int argc
, char **argv
) {
140 int opt
, msg
= 0, sem
= 0, shm
= 0, id
=0, print
=0;
142 char options
[] = "atcluphsmqi:";
144 setlocale(LC_ALL
, "");
145 bindtextdomain(PACKAGE
, LOCALEDIR
);
149 while ((opt
= getopt (argc
, argv
, options
)) != -1) {
197 usage (EXIT_FAILURE
);
199 if ( !shm
&& !msg
&& !sem
)
221 print_perms (int id
, struct ipc_perm
*ipcp
) {
225 printf ("%-10d %-10o", id
, ipcp
->mode
& 0777);
227 if ((pw
= getpwuid(ipcp
->cuid
)))
228 printf(" %-10s", pw
->pw_name
);
230 printf(" %-10d", ipcp
->cuid
);
231 if ((gr
= getgrgid(ipcp
->cgid
)))
232 printf(" %-10s", gr
->gr_name
);
234 printf(" %-10d", ipcp
->cgid
);
236 if ((pw
= getpwuid(ipcp
->uid
)))
237 printf(" %-10s", pw
->pw_name
);
239 printf(" %-10d", ipcp
->uid
);
240 if ((gr
= getgrgid(ipcp
->gid
)))
241 printf(" %-10s\n", gr
->gr_name
);
243 printf(" %-10d\n", ipcp
->gid
);
247 void do_shm (char format
)
249 int maxid
, shmid
, id
;
250 struct shmid_ds shmseg
;
251 struct shm_info shm_info
;
252 struct shminfo shminfo
;
253 struct ipc_perm
*ipcp
= &shmseg
.shm_perm
;
256 maxid
= shmctl (0, SHM_INFO
, (struct shmid_ds
*) (void *) &shm_info
);
258 printf (_("kernel not configured for shared memory\n"));
264 printf (_("------ Shared Memory Limits --------\n"));
265 if ((shmctl (0, IPC_INFO
, (struct shmid_ds
*) (void *) &shminfo
)) < 0 )
267 /* glibc 2.1.3 and all earlier libc's have ints as fields
268 of struct shminfo; glibc 2.1.91 has unsigned long; ach */
269 printf (_("max number of segments = %lu\n"),
270 (unsigned long) shminfo
.shmmni
);
271 printf (_("max seg size (kbytes) = %lu\n"),
272 (unsigned long) (shminfo
.shmmax
>> 10));
273 printf (_("max total shared memory (kbytes) = %llu\n"),
274 getpagesize() / 1024 * (unsigned long long) shminfo
.shmall
);
275 printf (_("min seg size (bytes) = %lu\n"),
276 (unsigned long) shminfo
.shmmin
);
280 printf (_("------ Shared Memory Status --------\n"));
281 printf (_("segments allocated %d\n"), shm_info
.used_ids
);
282 printf (_("pages allocated %ld\n"), shm_info
.shm_tot
);
283 printf (_("pages resident %ld\n"), shm_info
.shm_rss
);
284 printf (_("pages swapped %ld\n"), shm_info
.shm_swp
);
285 printf (_("Swap performance: %ld attempts\t %ld successes\n"),
286 shm_info
.swap_attempts
, shm_info
.swap_successes
);
290 printf (_("------ Shared Memory Segment Creators/Owners --------\n"));
291 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
292 _("shmid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
296 printf (_("------ Shared Memory Attach/Detach/Change Times --------\n"));
297 printf ("%-10s %-10s %-20s %-20s %-20s\n",
298 _("shmid"),_("owner"),_("attached"),_("detached"),
303 printf (_("------ Shared Memory Creator/Last-op --------\n"));
304 printf ("%-10s %-10s %-10s %-10s\n",
305 _("shmid"),_("owner"),_("cpid"),_("lpid"));
309 printf (_("------ Shared Memory Segments --------\n"));
310 printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
311 _("key"),_("shmid"),_("owner"),_("perms"),_("bytes"),
312 _("nattch"),_("status"));
316 for (id
= 0; id
<= maxid
; id
++) {
317 shmid
= shmctl (id
, SHM_STAT
, &shmseg
);
320 if (format
== CREATOR
) {
321 print_perms (shmid
, ipcp
);
324 pw
= getpwuid(ipcp
->uid
);
328 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
330 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
331 /* ctime uses static buffer: use separate calls */
332 printf(" %-20.16s", shmseg
.shm_atime
333 ? ctime(&shmseg
.shm_atime
) + 4 : _("Not set"));
334 printf(" %-20.16s", shmseg
.shm_dtime
335 ? ctime(&shmseg
.shm_dtime
) + 4 : _("Not set"));
336 printf(" %-20.16s\n", shmseg
.shm_ctime
337 ? ctime(&shmseg
.shm_ctime
) + 4 : _("Not set"));
341 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
343 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
344 printf (" %-10d %-10d\n",
345 shmseg
.shm_cpid
, shmseg
.shm_lpid
);
349 printf("0x%08x ",ipcp
->KEY
);
351 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
353 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
354 printf (" %-10o %-10lu %-10ld %-6s %-6s\n",
357 * earlier: int, Austin has size_t
359 (unsigned long) shmseg
.shm_segsz
,
361 * glibc-2.1.3 and earlier has unsigned short;
362 * Austin has shmatt_t
364 (long) shmseg
.shm_nattch
,
365 ipcp
->mode
& SHM_DEST
? _("dest") : " ",
366 ipcp
->mode
& SHM_LOCKED
? _("locked") : " ");
374 void do_sem (char format
)
376 int maxid
, semid
, id
;
377 struct semid_ds semary
;
378 struct seminfo seminfo
;
379 struct ipc_perm
*ipcp
= &semary
.sem_perm
;
383 arg
.array
= (ushort
*) (void *) &seminfo
;
384 maxid
= semctl (0, 0, SEM_INFO
, arg
);
386 printf (_("kernel not configured for semaphores\n"));
392 printf (_("------ Semaphore Limits --------\n"));
393 arg
.array
= (ushort
*) (void *) &seminfo
; /* damn union */
394 if ((semctl (0, 0, IPC_INFO
, arg
)) < 0 )
396 printf (_("max number of arrays = %d\n"), seminfo
.semmni
);
397 printf (_("max semaphores per array = %d\n"), seminfo
.semmsl
);
398 printf (_("max semaphores system wide = %d\n"), seminfo
.semmns
);
399 printf (_("max ops per semop call = %d\n"), seminfo
.semopm
);
400 printf (_("semaphore max value = %d\n"), seminfo
.semvmx
);
404 printf (_("------ Semaphore Status --------\n"));
405 printf (_("used arrays = %d\n"), seminfo
.semusz
);
406 printf (_("allocated semaphores = %d\n"), seminfo
.semaem
);
410 printf (_("------ Semaphore Arrays Creators/Owners --------\n"));
411 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
412 _("semid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
416 printf (_("------ Semaphore Operation/Change Times --------\n"));
417 printf ("%-8s %-10s %-26.24s %-26.24s\n",
418 _("semid"),_("owner"),_("last-op"),_("last-changed"));
425 printf (_("------ Semaphore Arrays --------\n"));
426 printf ("%-10s %-10s %-10s %-10s %-10s\n",
427 _("key"),_("semid"),_("owner"),_("perms"),_("nsems"));
431 for (id
= 0; id
<= maxid
; id
++) {
432 arg
.buf
= (struct semid_ds
*) &semary
;
433 semid
= semctl (id
, 0, SEM_STAT
, arg
);
436 if (format
== CREATOR
) {
437 print_perms (semid
, ipcp
);
440 pw
= getpwuid(ipcp
->uid
);
444 printf ("%-8d %-10.10s", semid
, pw
->pw_name
);
446 printf ("%-8d %-10d", semid
, ipcp
->uid
);
447 printf (" %-26.24s", semary
.sem_otime
448 ? ctime(&semary
.sem_otime
) : _("Not set"));
449 printf (" %-26.24s\n", semary
.sem_ctime
450 ? ctime(&semary
.sem_ctime
) : _("Not set"));
456 printf("0x%08x ", ipcp
->KEY
);
458 printf ("%-10d %-10.10s", semid
, pw
->pw_name
);
460 printf ("%-10d %-10d", semid
, ipcp
->uid
);
461 printf (" %-10o %-10ld\n",
464 * glibc-2.1.3 and earlier has unsigned short;
465 * glibc-2.1.91 has variation between
466 * unsigned short and unsigned long
467 * Austin prescribes unsigned short.
469 (long) semary
.sem_nsems
);
476 void do_msg (char format
)
478 int maxid
, msqid
, id
;
479 struct msqid_ds msgque
;
480 struct msginfo msginfo
;
481 struct ipc_perm
*ipcp
= &msgque
.msg_perm
;
484 maxid
= msgctl (0, MSG_INFO
, (struct msqid_ds
*) (void *) &msginfo
);
486 printf (_("kernel not configured for message queues\n"));
492 if ((msgctl (0, IPC_INFO
, (struct msqid_ds
*) (void *) &msginfo
)) < 0 )
494 printf (_("------ Messages: Limits --------\n"));
495 printf (_("max queues system wide = %d\n"), msginfo
.msgmni
);
496 printf (_("max size of message (bytes) = %d\n"), msginfo
.msgmax
);
497 printf (_("default max size of queue (bytes) = %d\n"), msginfo
.msgmnb
);
501 printf (_("------ Messages: Status --------\n"));
502 printf (_("allocated queues = %d\n"), msginfo
.msgpool
);
503 printf (_("used headers = %d\n"), msginfo
.msgmap
);
504 printf (_("used space = %d bytes\n"), msginfo
.msgtql
);
508 printf (_("------ Message Queues: Creators/Owners --------\n"));
509 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
510 _("msqid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
514 printf (_("------ Message Queues Send/Recv/Change Times --------\n"));
515 printf ("%-8s %-10s %-20s %-20s %-20s\n",
516 _("msqid"),_("owner"),_("send"),_("recv"),_("change"));
520 printf (_("------ Message Queues PIDs --------\n"));
521 printf ("%-10s %-10s %-10s %-10s\n",
522 _("msqid"),_("owner"),_("lspid"),_("lrpid"));
526 printf (_("------ Message Queues --------\n"));
527 printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
528 _("key"), _("msqid"), _("owner"), _("perms"),
529 _("used-bytes"), _("messages"));
533 for (id
= 0; id
<= maxid
; id
++) {
534 msqid
= msgctl (id
, MSG_STAT
, &msgque
);
537 if (format
== CREATOR
) {
538 print_perms (msqid
, ipcp
);
541 pw
= getpwuid(ipcp
->uid
);
545 printf ("%-8d %-10.10s", msqid
, pw
->pw_name
);
547 printf ("%-8d %-10d", msqid
, ipcp
->uid
);
548 printf (" %-20.16s", msgque
.msg_stime
549 ? ctime(&msgque
.msg_stime
) + 4 : _("Not set"));
550 printf (" %-20.16s", msgque
.msg_rtime
551 ? ctime(&msgque
.msg_rtime
) + 4 : _("Not set"));
552 printf (" %-20.16s\n", msgque
.msg_ctime
553 ? ctime(&msgque
.msg_ctime
) + 4 : _("Not set"));
557 printf ("%-8d %-10.10s", msqid
, pw
->pw_name
);
559 printf ("%-8d %-10d", msqid
, ipcp
->uid
);
560 printf (" %5d %5d\n",
561 msgque
.msg_lspid
, msgque
.msg_lrpid
);
565 printf( "0x%08x ",ipcp
->KEY
);
567 printf ("%-10d %-10.10s", msqid
, pw
->pw_name
);
569 printf ("%-10d %-10d", msqid
, ipcp
->uid
);
570 printf (" %-10o %-12ld %-12ld\n",
573 * glibc-2.1.3 and earlier has unsigned short;
574 * glibc-2.1.91 has variation between
575 * unsigned short, unsigned long
576 * Austin has msgqnum_t
578 (long) msgque
.msg_cbytes
,
579 (long) msgque
.msg_qnum
);
587 void print_shm (int shmid
)
589 struct shmid_ds shmds
;
590 struct ipc_perm
*ipcp
= &shmds
.shm_perm
;
592 if (shmctl (shmid
, IPC_STAT
, &shmds
) == -1)
593 err(EXIT_FAILURE
, _("shmctl failed"));
595 printf (_("\nShared memory Segment shmid=%d\n"), shmid
);
596 printf (_("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"),
597 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
);
598 printf (_("mode=%#o\taccess_perms=%#o\n"),
599 ipcp
->mode
, ipcp
->mode
& 0777);
600 printf (_("bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n"),
601 (long) shmds
.shm_segsz
, shmds
.shm_lpid
, shmds
.shm_cpid
,
602 (long) shmds
.shm_nattch
);
603 printf (_("att_time=%-26.24s\n"),
604 shmds
.shm_atime
? ctime (&shmds
.shm_atime
) : _("Not set"));
605 printf (_("det_time=%-26.24s\n"),
606 shmds
.shm_dtime
? ctime (&shmds
.shm_dtime
) : _("Not set"));
607 printf (_("change_time=%-26.24s\n"), ctime (&shmds
.shm_ctime
));
613 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"));
643 void print_sem (int semid
)
645 struct semid_ds semds
;
646 struct ipc_perm
*ipcp
= &semds
.sem_perm
;
651 if (semctl (semid
, 0, IPC_STAT
, arg
) < 0)
652 err(EXIT_FAILURE
, _("semctl failed"));
654 printf (_("\nSemaphore Array semid=%d\n"), semid
);
655 printf (_("uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"),
656 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
);
657 printf (_("mode=%#o, access_perms=%#o\n"),
658 ipcp
->mode
, ipcp
->mode
& 0777);
659 printf (_("nsems = %ld\n"), (long) semds
.sem_nsems
);
660 printf (_("otime = %-26.24s\n"),
661 semds
.sem_otime
? ctime (&semds
.sem_otime
) : _("Not set"));
662 printf (_("ctime = %-26.24s\n"), ctime (&semds
.sem_ctime
));
664 printf ("%-10s %-10s %-10s %-10s %-10s\n",
665 _("semnum"),_("value"),_("ncount"),_("zcount"),_("pid"));
667 for (i
=0; i
< semds
.sem_nsems
; i
++) {
668 int val
, ncnt
, zcnt
, pid
;
669 val
= semctl (semid
, i
, GETVAL
, arg
);
670 ncnt
= semctl (semid
, i
, GETNCNT
, arg
);
671 zcnt
= semctl (semid
, i
, GETZCNT
, arg
);
672 pid
= semctl (semid
, i
, GETPID
, arg
);
673 if (val
< 0 || ncnt
< 0 || zcnt
< 0 || pid
< 0)
674 err(EXIT_FAILURE
, _("semctl failed"));
676 printf ("%-10d %-10d %-10d %-10d %-10d\n",
677 i
, val
, ncnt
, zcnt
, pid
);