]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/ipcs.c
1 /* Original author unknown, but may be "krishna balasub@cis.ohio-state.edu"
2 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@misiek.eu.org>
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"), progname
);
131 printf (_(" which you have read access.\n"));
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
)) != EOF
) {
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
*) &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
*) &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 = %ld\n"),
280 (long) shminfo
.shmmni
);
281 printf (_("max seg size (kbytes) = %ld\n"),
282 (long) (shminfo
.shmmax
>> 10));
283 printf (_("max total shared memory (kbytes) = %ld\n"),
284 (long) shminfo
.shmall
<< 2);
285 printf (_("min seg size (bytes) = %ld\n"),
286 (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"),_("changed"));
312 printf (_("------ Shared Memory Creator/Last-op --------\n"));
313 printf (_("%-10s%-10s%-10s%-10s\n"),_("shmid"),_("owner"),_("cpid"),_("lpid"));
317 printf (_("------ Shared Memory Segments --------\n"));
318 printf (_("%-10s%-10s%-10s%-10s%-10s%-10s%-12s\n"), _("key"),_("shmid"),
319 _("owner"),_("perms"),_("bytes"),_("nattch"),_("status"));
323 for (id
= 0; id
<= maxid
; id
++) {
324 shmid
= shmctl (id
, SHM_STAT
, &shmseg
);
327 if (format
== CREATOR
) {
328 print_perms (shmid
, ipcp
);
331 pw
= getpwuid(ipcp
->uid
);
335 printf ("%-10d%-10.10s", shmid
, pw
->pw_name
);
337 printf ("%-10d%-10d", shmid
, ipcp
->uid
);
338 printf(" %-20.16s%-20.16s%-20.16s\n",
339 shmseg
.shm_atime
? ctime(&shmseg
.shm_atime
) + 4 : _("Not set"),
340 shmseg
.shm_dtime
? ctime(&shmseg
.shm_dtime
) + 4 : _("Not set"),
341 shmseg
.shm_ctime
? ctime(&shmseg
.shm_ctime
) + 4 : _("Not set"));
345 printf ("%-10d%-10.10s", shmid
, pw
->pw_name
);
347 printf ("%-10d%-10d", shmid
, ipcp
->uid
);
348 printf ("%-10d%-10d\n",
349 shmseg
.shm_cpid
, shmseg
.shm_lpid
);
353 printf( "0x%08x ",ipcp
->KEY
);
355 printf ("%-10d%-10.10s", shmid
, pw
->pw_name
);
357 printf ("%-10d%-10d", shmid
, ipcp
->uid
);
358 printf ("%-10o%-10d%-10ld%-6s%-6s\n",
362 * glibc-2.1.3 and earlier has unsigned short;
363 * Austin has shmatt_t
365 (long) shmseg
.shm_nattch
,
366 ipcp
->mode
& SHM_DEST
? _("dest") : " ",
367 ipcp
->mode
& SHM_LOCKED
? _("locked") : " ");
375 void do_sem (char format
)
377 int maxid
, semid
, id
;
378 struct semid_ds semary
;
379 struct seminfo seminfo
;
380 struct ipc_perm
*ipcp
= &semary
.sem_perm
;
384 arg
.array
= (ushort
*) &seminfo
;
385 maxid
= semctl (0, 0, SEM_INFO
, arg
);
387 printf (_("kernel not configured for semaphores\n"));
393 printf (_("------ Semaphore Limits --------\n"));
394 arg
.array
= (ushort
*) &seminfo
; /* damn union */
395 if ((semctl (0, 0, IPC_INFO
, arg
)) < 0 )
397 printf (_("max number of arrays = %d\n"), seminfo
.semmni
);
398 printf (_("max semaphores per array = %d\n"), seminfo
.semmsl
);
399 printf (_("max semaphores system wide = %d\n"), seminfo
.semmns
);
400 printf (_("max ops per semop call = %d\n"), seminfo
.semopm
);
401 printf (_("semaphore max value = %d\n"), seminfo
.semvmx
);
405 printf (_("------ Semaphore Status --------\n"));
406 printf (_("used arrays = %d\n"), seminfo
.semusz
);
407 printf (_("allocated semaphores = %d\n"), seminfo
.semaem
);
411 printf (_("------ Semaphore Arrays Creators/Owners --------\n"));
412 printf (_("%-10s%-10s%-10s%-10s%-10s%-10s\n"),
413 _("semid"),_("perms"),_("cuid"),_("cgid"),_("uid"),("gid"));
417 printf (_("------ Shared Memory Operation/Change Times --------\n"));
418 printf (_("%-8s%-10s %-26.24s %-26.24s\n"),
419 _("shmid"),_("owner"),_("last-op"),_("last-changed"));
426 printf (_("------ Semaphore Arrays --------\n"));
427 printf (_("%-10s%-10s%-10s%-10s%-10s%-12s\n"),
428 _("key"),_("semid"),_("owner"),_("perms"),_("nsems"),_("status"));
432 for (id
= 0; id
<= maxid
; id
++) {
433 arg
.buf
= (struct semid_ds
*) &semary
;
434 semid
= semctl (id
, 0, SEM_STAT
, arg
);
437 if (format
== CREATOR
) {
438 print_perms (semid
, ipcp
);
441 pw
= getpwuid(ipcp
->uid
);
445 printf ("%-8d%-10.10s", semid
, pw
->pw_name
);
447 printf ("%-8d%-10d", semid
, ipcp
->uid
);
448 printf (" %-26.24s %-26.24s\n",
449 semary
.sem_otime
? ctime(&semary
.sem_otime
) : _("Not set"),
450 semary
.sem_ctime
? ctime(&semary
.sem_ctime
) : _("Not set"));
456 printf( "0x%08x ",ipcp
->KEY
);
458 printf ("%-10d%-10.9s", semid
, pw
->pw_name
);
460 printf ("%-10d%-9d", 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
);
477 void do_msg (char format
)
479 int maxid
, msqid
, id
;
480 struct msqid_ds msgque
;
481 struct msginfo msginfo
;
482 struct ipc_perm
*ipcp
= &msgque
.msg_perm
;
485 maxid
= msgctl (0, MSG_INFO
, (struct msqid_ds
*) &msginfo
);
487 printf (_("kernel not configured for shared memory\n"));
493 if ((msgctl (0, IPC_INFO
, (struct msqid_ds
*) &msginfo
)) < 0 )
495 printf (_("------ Messages: Limits --------\n"));
496 printf (_("max queues system wide = %d\n"), msginfo
.msgmni
);
497 printf (_("max size of message (bytes) = %d\n"), msginfo
.msgmax
);
498 printf (_("default max size of queue (bytes) = %d\n"), msginfo
.msgmnb
);
502 printf (_("------ Messages: Status --------\n"));
503 printf (_("allocated queues = %d\n"), msginfo
.msgpool
);
504 printf (_("used headers = %d\n"), msginfo
.msgmap
);
505 printf (_("used space = %d bytes\n"), msginfo
.msgtql
);
509 printf (_("------ Message Queues: Creators/Owners --------\n"));
510 printf (_("%-10s%-10s%-10s%-10s%-10s%-10s\n"),
511 _("msqid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
515 printf (_("------ Message Queues Send/Recv/Change Times --------\n"));
516 printf (_("%-8s%-10s %-20s%-20s%-20s\n"),
517 _("msqid"),_("owner"),_("send"),_("recv"),_("change"));
521 printf (_("------ Message Queues PIDs --------\n"));
522 printf (_("%-10s%-10s%-10s%-10s\n"),_("msqid"),_("owner"),_("lspid"),_("lrpid"));
526 printf (_("------ Message Queues --------\n"));
527 printf (_("%-10s%-10s%-10s%-10s%-12s%-12s\n"), _("key"),_("msqid"),
528 _("owner"), _("perms"), _("used-bytes"), _("messages"));
532 for (id
= 0; id
<= maxid
; id
++) {
533 msqid
= msgctl (id
, MSG_STAT
, &msgque
);
536 if (format
== CREATOR
) {
537 print_perms (msqid
, ipcp
);
540 pw
= getpwuid(ipcp
->uid
);
544 printf ("%-8d%-10.10s", msqid
, pw
->pw_name
);
546 printf ("%-8d%-10d", msqid
, ipcp
->uid
);
547 printf (" %-20.16s%-20.16s%-20.16s\n",
548 msgque
.msg_stime
? ctime(&msgque
.msg_stime
) + 4 : _("Not set"),
549 msgque
.msg_rtime
? ctime(&msgque
.msg_rtime
) + 4 : _("Not set"),
550 msgque
.msg_ctime
? ctime(&msgque
.msg_ctime
) + 4 : _("Not set"));
554 printf ("%-8d%-10.10s", msqid
, pw
->pw_name
);
556 printf ("%-8d%-10d", msqid
, ipcp
->uid
);
557 printf (" %5d %5d\n",
558 msgque
.msg_lspid
, msgque
.msg_lrpid
);
562 printf( "0x%08x ",ipcp
->KEY
);
564 printf ("%-10d%-10.10s", msqid
, pw
->pw_name
);
566 printf ("%-10d%-10d", msqid
, ipcp
->uid
);
567 printf ("%-10o%-12ld%-12ld\n",
570 * glibc-2.1.3 and earlier has unsigned short;
571 * glibc-2.1.91 has variation between
572 * unsigned short, unsigned long
573 * Austin has msgqnum_t
575 (long) msgque
.msg_cbytes
,
576 (long) msgque
.msg_qnum
);
584 void print_shm (int shmid
)
586 struct shmid_ds shmds
;
587 struct ipc_perm
*ipcp
= &shmds
.shm_perm
;
589 if (shmctl (shmid
, IPC_STAT
, &shmds
) == -1) {
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=%d\tlpid=%d\tcpid=%d\tnattch=%ld\n"),
600 shmds
.shm_segsz
, shmds
.shm_lpid
, shmds
.shm_cpid
,
601 (long) shmds
.shm_nattch
);
602 printf (_("att_time=%s"), shmds
.shm_atime
? ctime (&shmds
.shm_atime
) :
604 printf (_("det_time=%s"), shmds
.shm_dtime
? ctime (&shmds
.shm_dtime
) :
606 printf (_("change_time=%s"), 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) {
622 printf (_("\nMessage Queue msqid=%d\n"), msqid
);
623 printf (_("uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"),
624 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
, ipcp
->mode
);
625 printf (_("cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n"),
627 * glibc-2.1.3 and earlier has unsigned short;
628 * glibc-2.1.91 has variation between
629 * unsigned short, unsigned long
630 * Austin has msgqnum_t (for msg_qbytes)
632 (long) buf
.msg_cbytes
, (long) buf
.msg_qbytes
,
633 (long) buf
.msg_qnum
, buf
.msg_lspid
, buf
.msg_lrpid
);
634 printf (_("send_time=%srcv_time=%schange_time=%s"),
635 buf
.msg_rtime
? ctime (&buf
.msg_rtime
) : _("Not Set\n"),
636 buf
.msg_stime
? ctime (&buf
.msg_stime
) : _("Not Set\n"),
637 buf
.msg_ctime
? ctime (&buf
.msg_ctime
) : _("Not Set\n"));
642 void print_sem (int semid
)
644 struct semid_ds semds
;
645 struct ipc_perm
*ipcp
= &semds
.sem_perm
;
650 if (semctl (semid
, 0, IPC_STAT
, arg
) < 0) {
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 = %s"), semds
.sem_otime
? ctime (&semds
.sem_otime
) :
662 printf (_("ctime = %s"), 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) {
677 printf ("%-10d%-10d%-10d%-10d%-10d\n", i
, val
, ncnt
, zcnt
, pid
);