]>
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
31 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
32 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
33 /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
34 #include <sys/types.h>
40 /*-------------------------------------------------------------------*/
41 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
42 but inside #ifdef __KERNEL__ ... #endif */
44 /* shm_mode upper byte flags */
45 #define SHM_DEST 01000 /* segment will be destroyed on last detach */
46 #define SHM_LOCKED 02000 /* segment will not be swapped */
49 /* For older kernels the same holds for the defines below */
60 ulong shm_tot
; /* total allocated shm */
61 ulong shm_rss
; /* total resident shm */
62 ulong shm_swp
; /* total swapped shm */
73 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
77 /*-------------------------------------------------------------------*/
79 /* The last arg of semctl is a union semun, but where is it defined?
80 X/OPEN tells us to define it ourselves, but until recently
81 Linux include files would also define it. */
82 #if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
83 /* union semun is defined by including <sys/sem.h> */
85 /* according to X/OPEN we have to define it ourselves */
89 unsigned short int *array
;
90 struct seminfo
*__buf
;
94 /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
95 libc 4/5 does not mention struct ipc_term at all, but includes
96 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
97 glibc-1.09 has no support for sysv ipc.
98 glibc 2 uses __key, __seq */
99 #if defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
111 void do_shm (char format
);
112 void do_sem (char format
);
113 void do_msg (char format
);
114 void print_shm (int id
);
115 void print_msg (int id
);
116 void print_sem (int id
);
118 static char *progname
;
122 printf (_("usage : %s -asmq -tclup \n"), progname
);
123 printf (_("\t%s [-s -m -q] -i id\n"), progname
);
124 printf (_("\t%s -h for help.\n"), progname
);
130 printf (_("%s provides information on ipc facilities for"
131 " which you have read access.\n"), progname
);
132 printf (_("Resource Specification:\n\t-m : shared_mem\n\t-q : messages\n"));
133 printf (_("\t-s : semaphores\n\t-a : all (default)\n"));
134 printf (_("Output Format:\n\t-t : time\n\t-p : pid\n\t-c : creator\n"));
135 printf (_("\t-l : limits\n\t-u : summary\n"));
136 printf (_("-i id [-s -q -m] : details on resource identified by id\n"));
142 main (int argc
, char **argv
) {
143 int opt
, msg
= 0, sem
= 0, shm
= 0, id
=0, print
=0;
145 char options
[] = "atcluphsmqi:";
147 setlocale(LC_ALL
, "");
148 bindtextdomain(PACKAGE
, LOCALEDIR
);
152 while ((opt
= getopt (argc
, argv
, options
)) != -1) {
210 if ( !shm
&& !msg
&& !sem
)
231 print_perms (int id
, struct ipc_perm
*ipcp
) {
235 printf ("%-10d %-10o", id
, ipcp
->mode
& 0777);
237 if ((pw
= getpwuid(ipcp
->cuid
)))
238 printf(" %-10s", pw
->pw_name
);
240 printf(" %-10d", ipcp
->cuid
);
241 if ((gr
= getgrgid(ipcp
->cgid
)))
242 printf(" %-10s", gr
->gr_name
);
244 printf(" %-10d", ipcp
->cgid
);
246 if ((pw
= getpwuid(ipcp
->uid
)))
247 printf(" %-10s", pw
->pw_name
);
249 printf(" %-10d", ipcp
->uid
);
250 if ((gr
= getgrgid(ipcp
->gid
)))
251 printf(" %-10s\n", gr
->gr_name
);
253 printf(" %-10d\n", ipcp
->gid
);
257 void do_shm (char format
)
259 int maxid
, shmid
, id
;
260 struct shmid_ds shmseg
;
261 struct shm_info shm_info
;
262 struct shminfo shminfo
;
263 struct ipc_perm
*ipcp
= &shmseg
.shm_perm
;
266 maxid
= shmctl (0, SHM_INFO
, (struct shmid_ds
*) (void *) &shm_info
);
268 printf (_("kernel not configured for shared memory\n"));
274 printf (_("------ Shared Memory Limits --------\n"));
275 if ((shmctl (0, IPC_INFO
, (struct shmid_ds
*) (void *) &shminfo
)) < 0 )
277 /* glibc 2.1.3 and all earlier libc's have ints as fields
278 of struct shminfo; glibc 2.1.91 has unsigned long; ach */
279 printf (_("max number of segments = %lu\n"),
280 (unsigned long) shminfo
.shmmni
);
281 printf (_("max seg size (kbytes) = %lu\n"),
282 (unsigned long) (shminfo
.shmmax
>> 10));
283 printf (_("max total shared memory (kbytes) = %llu\n"),
284 getpagesize() / 1024 * (unsigned long long) shminfo
.shmall
);
285 printf (_("min seg size (bytes) = %lu\n"),
286 (unsigned long) shminfo
.shmmin
);
290 printf (_("------ Shared Memory Status --------\n"));
291 printf (_("segments allocated %d\n"), shm_info
.used_ids
);
292 printf (_("pages allocated %ld\n"), shm_info
.shm_tot
);
293 printf (_("pages resident %ld\n"), shm_info
.shm_rss
);
294 printf (_("pages swapped %ld\n"), shm_info
.shm_swp
);
295 printf (_("Swap performance: %ld attempts\t %ld successes\n"),
296 shm_info
.swap_attempts
, shm_info
.swap_successes
);
300 printf (_("------ Shared Memory Segment Creators/Owners --------\n"));
301 printf (_("%-10s %-10s %-10s %-10s %-10s %-10s\n"),
302 _("shmid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
306 printf (_("------ Shared Memory Attach/Detach/Change Times --------\n"));
307 printf (_("%-10s %-10s %-20s %-20s %-20s\n"),
308 _("shmid"),_("owner"),_("attached"),_("detached"),
313 printf (_("------ Shared Memory Creator/Last-op --------\n"));
314 printf (_("%-10s %-10s %-10s %-10s\n"),
315 _("shmid"),_("owner"),_("cpid"),_("lpid"));
319 printf (_("------ Shared Memory Segments --------\n"));
320 printf (_("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n"),
321 _("key"),_("shmid"),_("owner"),_("perms"),_("bytes"),
322 _("nattch"),_("status"));
326 for (id
= 0; id
<= maxid
; id
++) {
327 shmid
= shmctl (id
, SHM_STAT
, &shmseg
);
330 if (format
== CREATOR
) {
331 print_perms (shmid
, ipcp
);
334 pw
= getpwuid(ipcp
->uid
);
338 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
340 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
341 /* ctime uses static buffer: use separate calls */
342 printf(" %-20.16s", shmseg
.shm_atime
343 ? ctime(&shmseg
.shm_atime
) + 4 : _("Not set"));
344 printf(" %-20.16s", shmseg
.shm_dtime
345 ? ctime(&shmseg
.shm_dtime
) + 4 : _("Not set"));
346 printf(" %-20.16s\n", shmseg
.shm_ctime
347 ? ctime(&shmseg
.shm_ctime
) + 4 : _("Not set"));
351 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
353 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
354 printf (" %-10d %-10d\n",
355 shmseg
.shm_cpid
, shmseg
.shm_lpid
);
359 printf("0x%08x ",ipcp
->KEY
);
361 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
363 printf ("%-10d %-10d", shmid
, ipcp
->uid
);
364 printf ("%-10o %-10lu %-10ld %-6s %-6s\n",
367 * earlier: int, Austin has size_t
369 (unsigned long) shmseg
.shm_segsz
,
371 * glibc-2.1.3 and earlier has unsigned short;
372 * Austin has shmatt_t
374 (long) shmseg
.shm_nattch
,
375 ipcp
->mode
& SHM_DEST
? _("dest") : " ",
376 ipcp
->mode
& SHM_LOCKED
? _("locked") : " ");
384 void do_sem (char format
)
386 int maxid
, semid
, id
;
387 struct semid_ds semary
;
388 struct seminfo seminfo
;
389 struct ipc_perm
*ipcp
= &semary
.sem_perm
;
393 arg
.array
= (ushort
*) (void *) &seminfo
;
394 maxid
= semctl (0, 0, SEM_INFO
, arg
);
396 printf (_("kernel not configured for semaphores\n"));
402 printf (_("------ Semaphore Limits --------\n"));
403 arg
.array
= (ushort
*) (void *) &seminfo
; /* damn union */
404 if ((semctl (0, 0, IPC_INFO
, arg
)) < 0 )
406 printf (_("max number of arrays = %d\n"), seminfo
.semmni
);
407 printf (_("max semaphores per array = %d\n"), seminfo
.semmsl
);
408 printf (_("max semaphores system wide = %d\n"), seminfo
.semmns
);
409 printf (_("max ops per semop call = %d\n"), seminfo
.semopm
);
410 printf (_("semaphore max value = %d\n"), seminfo
.semvmx
);
414 printf (_("------ Semaphore Status --------\n"));
415 printf (_("used arrays = %d\n"), seminfo
.semusz
);
416 printf (_("allocated semaphores = %d\n"), seminfo
.semaem
);
420 printf (_("------ Semaphore Arrays Creators/Owners --------\n"));
421 printf (_("%-10s %-10s %-10s %-10s %-10s %-10s\n"),
422 _("semid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
426 printf (_("------ Semaphore Operation/Change Times --------\n"));
427 printf (_("%-8s %-10s %-26.24s %-26.24s\n"),
428 _("semid"),_("owner"),_("last-op"),_("last-changed"));
435 printf (_("------ Semaphore Arrays --------\n"));
436 printf (_("%-10s %-10s %-10s %-10s %-10s\n"),
437 _("key"),_("semid"),_("owner"),_("perms"),_("nsems"));
441 for (id
= 0; id
<= maxid
; id
++) {
442 arg
.buf
= (struct semid_ds
*) &semary
;
443 semid
= semctl (id
, 0, SEM_STAT
, arg
);
446 if (format
== CREATOR
) {
447 print_perms (semid
, ipcp
);
450 pw
= getpwuid(ipcp
->uid
);
454 printf ("%-8d %-10.10s", semid
, pw
->pw_name
);
456 printf ("%-8d %-10d", semid
, ipcp
->uid
);
457 printf (" %-26.24s", semary
.sem_otime
458 ? ctime(&semary
.sem_otime
) : _("Not set"));
459 printf (" %-26.24s\n", semary
.sem_ctime
460 ? ctime(&semary
.sem_ctime
) : _("Not set"));
466 printf("0x%08x ", ipcp
->KEY
);
468 printf ("%-10d %-10.9s", semid
, pw
->pw_name
);
470 printf ("%-10d %-9d", semid
, ipcp
->uid
);
471 printf ("%-10o %-10ld\n",
474 * glibc-2.1.3 and earlier has unsigned short;
475 * glibc-2.1.91 has variation between
476 * unsigned short and unsigned long
477 * Austin prescribes unsigned short.
479 (long) semary
.sem_nsems
);
486 void do_msg (char format
)
488 int maxid
, msqid
, id
;
489 struct msqid_ds msgque
;
490 struct msginfo msginfo
;
491 struct ipc_perm
*ipcp
= &msgque
.msg_perm
;
494 maxid
= msgctl (0, MSG_INFO
, (struct msqid_ds
*) (void *) &msginfo
);
496 printf (_("kernel not configured for message queues\n"));
502 if ((msgctl (0, IPC_INFO
, (struct msqid_ds
*) (void *) &msginfo
)) < 0 )
504 printf (_("------ Messages: Limits --------\n"));
505 printf (_("max queues system wide = %d\n"), msginfo
.msgmni
);
506 printf (_("max size of message (bytes) = %d\n"), msginfo
.msgmax
);
507 printf (_("default max size of queue (bytes) = %d\n"), msginfo
.msgmnb
);
511 printf (_("------ Messages: Status --------\n"));
512 printf (_("allocated queues = %d\n"), msginfo
.msgpool
);
513 printf (_("used headers = %d\n"), msginfo
.msgmap
);
514 printf (_("used space = %d bytes\n"), msginfo
.msgtql
);
518 printf (_("------ Message Queues: Creators/Owners --------\n"));
519 printf (_("%-10s %-10s %-10s %-10s %-10s %-10s\n"),
520 _("msqid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
524 printf (_("------ Message Queues Send/Recv/Change Times --------\n"));
525 printf (_("%-8s %-10s %-20s %-20s %-20s\n"),
526 _("msqid"),_("owner"),_("send"),_("recv"),_("change"));
530 printf (_("------ Message Queues PIDs --------\n"));
531 printf (_("%-10s %-10s %-10s %-10s\n"),
532 _("msqid"),_("owner"),_("lspid"),_("lrpid"));
536 printf (_("------ Message Queues --------\n"));
537 printf (_("%-10s %-10s %-10s %-10s %-12s %-12s\n"),
538 _("key"), _("msqid"), _("owner"), _("perms"),
539 _("used-bytes"), _("messages"));
543 for (id
= 0; id
<= maxid
; id
++) {
544 msqid
= msgctl (id
, MSG_STAT
, &msgque
);
547 if (format
== CREATOR
) {
548 print_perms (msqid
, ipcp
);
551 pw
= getpwuid(ipcp
->uid
);
555 printf ("%-8d %-10.10s", msqid
, pw
->pw_name
);
557 printf ("%-8d %-10d", msqid
, ipcp
->uid
);
558 printf (" %-20.16s", msgque
.msg_stime
559 ? ctime(&msgque
.msg_stime
) + 4 : _("Not set"));
560 printf (" %-20.16s", msgque
.msg_rtime
561 ? ctime(&msgque
.msg_rtime
) + 4 : _("Not set"));
562 printf (" %-20.16s\n", msgque
.msg_ctime
563 ? ctime(&msgque
.msg_ctime
) + 4 : _("Not set"));
567 printf ("%-8d %-10.10s", msqid
, pw
->pw_name
);
569 printf ("%-8d %-10d", msqid
, ipcp
->uid
);
570 printf (" %5d %5d\n",
571 msgque
.msg_lspid
, msgque
.msg_lrpid
);
575 printf( "0x%08x ",ipcp
->KEY
);
577 printf ("%-10d %-10.10s", msqid
, pw
->pw_name
);
579 printf ("%-10d %-10d", msqid
, ipcp
->uid
);
580 printf (" %-10o %-12ld %-12ld\n",
583 * glibc-2.1.3 and earlier has unsigned short;
584 * glibc-2.1.91 has variation between
585 * unsigned short, unsigned long
586 * Austin has msgqnum_t
588 (long) msgque
.msg_cbytes
,
589 (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) {
607 printf (_("\nShared memory Segment shmid=%d\n"), shmid
);
608 printf (_("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"),
609 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
);
610 printf (_("mode=%#o\taccess_perms=%#o\n"),
611 ipcp
->mode
, ipcp
->mode
& 0777);
612 printf (_("bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n"),
613 (long) shmds
.shm_segsz
, shmds
.shm_lpid
, shmds
.shm_cpid
,
614 (long) shmds
.shm_nattch
);
615 printf (_("att_time=%-26.24s\n"),
616 shmds
.shm_atime
? ctime (&shmds
.shm_atime
) : _("Not set"));
617 printf (_("det_time=%-26.24s\n"),
618 shmds
.shm_dtime
? ctime (&shmds
.shm_dtime
) : _("Not set"));
619 printf (_("change_time=%-26.24s\n"), ctime (&shmds
.shm_ctime
));
625 void print_msg (int msqid
)
628 struct ipc_perm
*ipcp
= &buf
.msg_perm
;
630 if (msgctl (msqid
, IPC_STAT
, &buf
) == -1) {
634 printf (_("\nMessage Queue msqid=%d\n"), msqid
);
635 printf (_("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"),
636 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
, ipcp
->mode
);
637 printf (_("cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n"),
639 * glibc-2.1.3 and earlier has unsigned short;
640 * glibc-2.1.91 has variation between
641 * unsigned short, unsigned long
642 * Austin has msgqnum_t (for msg_qbytes)
644 (long) buf
.msg_cbytes
, (long) buf
.msg_qbytes
,
645 (long) buf
.msg_qnum
, buf
.msg_lspid
, buf
.msg_lrpid
);
646 printf (_("send_time=%-26.24s\n"),
647 buf
.msg_stime
? ctime (&buf
.msg_stime
) : _("Not set"));
648 printf (_("rcv_time=%-26.24s\n"),
649 buf
.msg_rtime
? ctime (&buf
.msg_rtime
) : _("Not set"));
650 printf (_("change_time=%-26.24s\n"),
651 buf
.msg_ctime
? ctime (&buf
.msg_ctime
) : _("Not set"));
656 void print_sem (int semid
)
658 struct semid_ds semds
;
659 struct ipc_perm
*ipcp
= &semds
.sem_perm
;
664 if (semctl (semid
, 0, IPC_STAT
, arg
) < 0) {
668 printf (_("\nSemaphore Array semid=%d\n"), semid
);
669 printf (_("uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"),
670 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
);
671 printf (_("mode=%#o, access_perms=%#o\n"),
672 ipcp
->mode
, ipcp
->mode
& 0777);
673 printf (_("nsems = %ld\n"), (long) semds
.sem_nsems
);
674 printf (_("otime = %-26.24s\n"),
675 semds
.sem_otime
? ctime (&semds
.sem_otime
) : _("Not set"));
676 printf (_("ctime = %-26.24s\n"), ctime (&semds
.sem_ctime
));
678 printf (_("%-10s %-10s %-10s %-10s %-10s\n"),
679 _("semnum"),_("value"),_("ncount"),_("zcount"),_("pid"));
681 for (i
=0; i
< semds
.sem_nsems
; i
++) {
682 int val
, ncnt
, zcnt
, pid
;
683 val
= semctl (semid
, i
, GETVAL
, arg
);
684 ncnt
= semctl (semid
, i
, GETNCNT
, arg
);
685 zcnt
= semctl (semid
, i
, GETZCNT
, arg
);
686 pid
= semctl (semid
, i
, GETPID
, arg
);
687 if (val
< 0 || ncnt
< 0 || zcnt
< 0 || pid
< 0) {
691 printf ("%-10d %-10d %-10d %-10d %-10d\n",
692 i
, val
, ncnt
, zcnt
, pid
);