1 /* Original author unknown, may be "krishna balasub@cis.ohio-state.edu" */
3 * Modified Sat Oct 9 10:55:28 1993 for 0.99.13
5 * Patches from Mike Jagdis (jaggy@purplet.demon.co.uk) applied Wed Feb 8
6 * 12:12:21 1995 by faith@cs.unc.edu to print numeric uids if no passwd file
9 * Patch from arnolds@ifns.de (Heinz-Ado Arnolds) applied Mon Jul 1 19:30:41
10 * 1996 by janl@math.uio.no to add code missing in case PID: clauses.
12 * Patched to display the key field -- hy@picksys.com 12/18/96
14 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
15 * - added Native Language Support
30 #include <sys/types.h>
36 #include "closestream.h"
39 * SHM_DEST and SHM_LOCKED are defined in kernel headers, but inside
40 * #ifdef __KERNEL__ ... #endif
43 /* shm_mode upper byte flags */
44 # define SHM_DEST 01000 /* segment will be destroyed on last detach */
45 # define SHM_LOCKED 02000 /* segment will not be swapped */
48 /* For older kernels the same holds for the defines below */
59 ulong shm_tot
; /* total allocated shm */
60 ulong shm_rss
; /* total resident shm */
61 ulong shm_swp
; /* total swapped shm */
72 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
78 * The last arg of semctl is a union semun, but where is it defined? X/OPEN
79 * tells us to define it ourselves, but until recently Linux include files
80 * would also define it.
82 #ifndef HAVE_UNION_SEMUN
83 /* according to X/OPEN we have to define it ourselves */
87 unsigned short int *array
;
88 struct seminfo
*__buf
;
93 * X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
94 * glibc-1.09 has no support for sysv ipc.
95 * glibc 2 uses __key, __seq
97 #if defined (__GLIBC__) && __GLIBC__ >= 2
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 void __attribute__ ((__noreturn__
)) usage(FILE * out
)
118 fprintf(out
, USAGE_HEADER
);
119 fprintf(out
, " %s [resource ...] [output-format]\n", program_invocation_short_name
);
120 fprintf(out
, " %s [resource] -i <id>\n", program_invocation_short_name
);
121 fprintf(out
, USAGE_OPTIONS
);
122 fputs(_(" -i, --id <id> print details on resource identified by id\n"), out
);
123 fprintf(out
, USAGE_HELP
);
124 fprintf(out
, USAGE_VERSION
);
126 fputs(_("Resource options:\n"), out
);
127 fputs(_(" -m, --shmems shared memory segments\n"), out
);
128 fputs(_(" -q, --queues message queues\n"), out
);
129 fputs(_(" -s, --semaphores semaphores\n"), out
);
130 fputs(_(" -a, --all all (default)\n"), out
);
132 fputs(_("Output format:\n"), out
);
133 fputs(_(" -t, --time show attach, detach and change times\n"), out
);
134 fputs(_(" -p, --pid show creator and last operations PIDs\n"), out
);
135 fputs(_(" -c, --creator show creator and owner\n"), out
);
136 fputs(_(" -l, --limits show resource limits\n"), out
);
137 fputs(_(" -u, --summary show status summary\n"), out
);
138 fprintf(out
, USAGE_MAN_TAIL("ipcs(1)"));
139 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
142 int main (int argc
, char **argv
)
144 int opt
, msg
= 0, sem
= 0, shm
= 0, id
=0, print
=0;
146 static const struct option longopts
[] = {
147 {"id", required_argument
, NULL
, 'i'},
148 {"shmems", no_argument
, NULL
, 'm'},
149 {"queues", no_argument
, NULL
, 'q'},
150 {"semaphores", no_argument
, NULL
, 's'},
151 {"all", no_argument
, NULL
, 'a'},
152 {"time", no_argument
, NULL
, 't'},
153 {"pid", no_argument
, NULL
, 'p'},
154 {"creator", no_argument
, NULL
, 'c'},
155 {"limits", no_argument
, NULL
, 'l'},
156 {"summary", no_argument
, NULL
, 'u'},
157 {"version", no_argument
, NULL
, 'V'},
158 {"help", no_argument
, NULL
, 'h'},
161 char options
[] = "i:mqsatpcluVh";
163 setlocale(LC_ALL
, "");
164 bindtextdomain(PACKAGE
, LOCALEDIR
);
166 atexit(close_stdout
);
168 while ((opt
= getopt_long(argc
, argv
, options
, longopts
, NULL
)) != -1) {
204 printf(UTIL_LINUX_VERSION
);
218 if (!shm
&& !sem
&& !msg
)
221 if ( !shm
&& !msg
&& !sem
)
241 static void print_perms (int id
, struct ipc_perm
*ipcp
)
246 printf ("%-10d %-10o", id
, ipcp
->mode
& 0777);
248 if ((pw
= getpwuid(ipcp
->cuid
)))
249 printf(" %-10s", pw
->pw_name
);
251 printf(" %-10u", ipcp
->cuid
);
252 if ((gr
= getgrgid(ipcp
->cgid
)))
253 printf(" %-10s", gr
->gr_name
);
255 printf(" %-10u", ipcp
->cgid
);
257 if ((pw
= getpwuid(ipcp
->uid
)))
258 printf(" %-10s", pw
->pw_name
);
260 printf(" %-10u", ipcp
->uid
);
261 if ((gr
= getgrgid(ipcp
->gid
)))
262 printf(" %-10s\n", gr
->gr_name
);
264 printf(" %-10u\n", ipcp
->gid
);
267 void do_shm (char format
)
269 int maxid
, shmid
, id
;
270 struct shmid_ds shmseg
;
271 struct shm_info shm_info
;
272 struct shminfo shminfo
;
273 struct ipc_perm
*ipcp
= &shmseg
.shm_perm
;
276 maxid
= shmctl (0, SHM_INFO
, (struct shmid_ds
*) (void *) &shm_info
);
278 printf (_("kernel not configured for shared memory\n"));
284 printf (_("------ Shared Memory Limits --------\n"));
285 if ((shmctl (0, IPC_INFO
, (struct shmid_ds
*) (void *) &shminfo
)) < 0 )
288 * glibc 2.1.3 and all earlier libc's have ints as fields of
289 * struct shminfo; glibc 2.1.91 has unsigned long; ach
291 printf (_("max number of segments = %lu\n"),
292 (unsigned long) shminfo
.shmmni
);
293 printf (_("max seg size (kbytes) = %lu\n"),
294 (unsigned long) (shminfo
.shmmax
>> 10));
295 printf (_("max total shared memory (kbytes) = %llu\n"),
296 getpagesize() / 1024 * (unsigned long long) shminfo
.shmall
);
297 printf (_("min seg size (bytes) = %lu\n"),
298 (unsigned long) shminfo
.shmmin
);
302 printf (_("------ Shared Memory Status --------\n"));
304 * TRANSLATORS: This output format is mantained for backward
305 * compatibility as ipcs is used in scripts. For consistency
306 * with the rest, the translated form can follow this model:
308 * "segments allocated = %d\n"
309 * "pages allocated = %ld\n"
310 * "pages resident = %ld\n"
311 * "pages swapped = %ld\n"
312 * "swap performance = %ld attempts, %ld successes\n"
314 printf (_("segments allocated %d\n"
315 "pages allocated %ld\n"
316 "pages resident %ld\n"
317 "pages swapped %ld\n"
318 "Swap performance: %ld attempts\t %ld successes\n"),
323 shm_info
.swap_attempts
, shm_info
.swap_successes
);
327 printf (_("------ Shared Memory Segment Creators/Owners --------\n"));
328 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
329 _("shmid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
333 printf (_("------ Shared Memory Attach/Detach/Change Times --------\n"));
334 printf ("%-10s %-10s %-20s %-20s %-20s\n",
335 _("shmid"),_("owner"),_("attached"),_("detached"),
340 printf (_("------ Shared Memory Creator/Last-op PIDs --------\n"));
341 printf ("%-10s %-10s %-10s %-10s\n",
342 _("shmid"),_("owner"),_("cpid"),_("lpid"));
346 printf (_("------ Shared Memory Segments --------\n"));
347 printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
348 _("key"),_("shmid"),_("owner"),_("perms"),_("bytes"),
349 _("nattch"),_("status"));
353 for (id
= 0; id
<= maxid
; id
++) {
354 shmid
= shmctl (id
, SHM_STAT
, &shmseg
);
357 if (format
== CREATOR
) {
358 print_perms (shmid
, ipcp
);
361 pw
= getpwuid(ipcp
->uid
);
365 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
367 printf ("%-10d %-10u", shmid
, ipcp
->uid
);
368 /* ctime uses static buffer: use separate calls */
369 printf(" %-20.16s", shmseg
.shm_atime
370 ? ctime(&shmseg
.shm_atime
) + 4 : _("Not set"));
371 printf(" %-20.16s", shmseg
.shm_dtime
372 ? ctime(&shmseg
.shm_dtime
) + 4 : _("Not set"));
373 printf(" %-20.16s\n", shmseg
.shm_ctime
374 ? ctime(&shmseg
.shm_ctime
) + 4 : _("Not set"));
378 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
380 printf ("%-10d %-10u", shmid
, ipcp
->uid
);
381 printf (" %-10d %-10d\n",
382 shmseg
.shm_cpid
, shmseg
.shm_lpid
);
386 printf("0x%08x ",ipcp
->KEY
);
388 printf ("%-10d %-10.10s", shmid
, pw
->pw_name
);
390 printf ("%-10d %-10u", shmid
, ipcp
->uid
);
391 printf (" %-10o %-10lu %-10ld %-6s %-6s\n",
394 * earlier: int, Austin has size_t
396 (unsigned long) shmseg
.shm_segsz
,
398 * glibc-2.1.3 and earlier has unsigned short;
399 * Austin has shmatt_t
401 (long) shmseg
.shm_nattch
,
402 ipcp
->mode
& SHM_DEST
? _("dest") : " ",
403 ipcp
->mode
& SHM_LOCKED
? _("locked") : " ");
410 void do_sem (char format
)
412 int maxid
, semid
, id
;
413 struct semid_ds semary
;
414 struct seminfo seminfo
;
415 struct ipc_perm
*ipcp
= &semary
.sem_perm
;
419 arg
.array
= (ushort
*) (void *) &seminfo
;
420 maxid
= semctl (0, 0, SEM_INFO
, arg
);
422 printf (_("kernel not configured for semaphores\n"));
428 printf (_("------ Semaphore Limits --------\n"));
429 arg
.array
= (ushort
*) (void *) &seminfo
; /* damn union */
430 if ((semctl (0, 0, IPC_INFO
, arg
)) < 0 )
432 printf (_("max number of arrays = %d\n"), seminfo
.semmni
);
433 printf (_("max semaphores per array = %d\n"), seminfo
.semmsl
);
434 printf (_("max semaphores system wide = %d\n"), seminfo
.semmns
);
435 printf (_("max ops per semop call = %d\n"), seminfo
.semopm
);
436 printf (_("semaphore max value = %d\n"), seminfo
.semvmx
);
440 printf (_("------ Semaphore Status --------\n"));
441 printf (_("used arrays = %d\n"), seminfo
.semusz
);
442 printf (_("allocated semaphores = %d\n"), seminfo
.semaem
);
446 printf (_("------ Semaphore Arrays Creators/Owners --------\n"));
447 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
448 _("semid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
452 printf (_("------ Semaphore Operation/Change Times --------\n"));
453 printf ("%-8s %-10s %-26.24s %-26.24s\n",
454 _("semid"),_("owner"),_("last-op"),_("last-changed"));
461 printf (_("------ Semaphore Arrays --------\n"));
462 printf ("%-10s %-10s %-10s %-10s %-10s\n",
463 _("key"),_("semid"),_("owner"),_("perms"),_("nsems"));
467 for (id
= 0; id
<= maxid
; id
++) {
468 arg
.buf
= (struct semid_ds
*) &semary
;
469 semid
= semctl (id
, 0, SEM_STAT
, arg
);
472 if (format
== CREATOR
) {
473 print_perms (semid
, ipcp
);
476 pw
= getpwuid(ipcp
->uid
);
480 printf ("%-8d %-10.10s", semid
, pw
->pw_name
);
482 printf ("%-8d %-10u", semid
, ipcp
->uid
);
483 printf (" %-26.24s", semary
.sem_otime
484 ? ctime(&semary
.sem_otime
) : _("Not set"));
485 printf (" %-26.24s\n", semary
.sem_ctime
486 ? ctime(&semary
.sem_ctime
) : _("Not set"));
492 printf("0x%08x ", ipcp
->KEY
);
494 printf ("%-10d %-10.10s", semid
, pw
->pw_name
);
496 printf ("%-10d %-10u", semid
, ipcp
->uid
);
497 printf (" %-10o %-10ld\n",
500 * glibc-2.1.3 and earlier has unsigned
501 * short. glibc-2.1.91 has variation between
502 * unsigned short and unsigned long. Austin
503 * prescribes unsigned short.
505 (long) semary
.sem_nsems
);
511 void do_msg (char format
)
513 int maxid
, msqid
, id
;
514 struct msqid_ds msgque
;
515 struct msginfo msginfo
;
516 struct ipc_perm
*ipcp
= &msgque
.msg_perm
;
519 maxid
= msgctl (0, MSG_INFO
, (struct msqid_ds
*) (void *) &msginfo
);
521 printf (_("kernel not configured for message queues\n"));
527 if ((msgctl (0, IPC_INFO
, (struct msqid_ds
*) (void *) &msginfo
)) < 0 )
529 printf (_("------ Messages Limits --------\n"));
530 printf (_("max queues system wide = %d\n"), msginfo
.msgmni
);
531 printf (_("max size of message (bytes) = %d\n"), msginfo
.msgmax
);
532 printf (_("default max size of queue (bytes) = %d\n"), msginfo
.msgmnb
);
536 printf (_("------ Messages Status --------\n"));
537 printf (_("allocated queues = %d\n"), msginfo
.msgpool
);
538 printf (_("used headers = %d\n"), msginfo
.msgmap
);
539 printf (_("used space = %d bytes\n"), msginfo
.msgtql
);
543 printf (_("------ Message Queues Creators/Owners --------\n"));
544 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
545 _("msqid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
549 printf (_("------ Message Queues Send/Recv/Change Times --------\n"));
550 printf ("%-8s %-10s %-20s %-20s %-20s\n",
551 _("msqid"),_("owner"),_("send"),_("recv"),_("change"));
555 printf (_("------ Message Queues PIDs --------\n"));
556 printf ("%-10s %-10s %-10s %-10s\n",
557 _("msqid"),_("owner"),_("lspid"),_("lrpid"));
561 printf (_("------ Message Queues --------\n"));
562 printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
563 _("key"), _("msqid"), _("owner"), _("perms"),
564 _("used-bytes"), _("messages"));
568 for (id
= 0; id
<= maxid
; id
++) {
569 msqid
= msgctl (id
, MSG_STAT
, &msgque
);
572 if (format
== CREATOR
) {
573 print_perms (msqid
, ipcp
);
576 pw
= getpwuid(ipcp
->uid
);
580 printf ("%-8d %-10.10s", msqid
, pw
->pw_name
);
582 printf ("%-8d %-10u", msqid
, ipcp
->uid
);
583 printf (" %-20.16s", msgque
.msg_stime
584 ? ctime(&msgque
.msg_stime
) + 4 : _("Not set"));
585 printf (" %-20.16s", msgque
.msg_rtime
586 ? ctime(&msgque
.msg_rtime
) + 4 : _("Not set"));
587 printf (" %-20.16s\n", msgque
.msg_ctime
588 ? ctime(&msgque
.msg_ctime
) + 4 : _("Not set"));
592 printf ("%-8d %-10.10s", msqid
, pw
->pw_name
);
594 printf ("%-8d %-10u", msqid
, ipcp
->uid
);
595 printf (" %5d %5d\n",
596 msgque
.msg_lspid
, msgque
.msg_lrpid
);
600 printf( "0x%08x ",ipcp
->KEY
);
602 printf ("%-10d %-10.10s", msqid
, pw
->pw_name
);
604 printf ("%-10d %-10u", msqid
, ipcp
->uid
);
605 printf (" %-10o %-12ld %-12ld\n",
608 * glibc-2.1.3 and earlier has unsigned
609 * short. glibc-2.1.91 has variation between
610 * unsigned short, unsigned long. Austin has
613 (long) msgque
.msg_cbytes
,
614 (long) msgque
.msg_qnum
);
621 void print_shm (int shmid
)
623 struct shmid_ds shmds
;
624 struct ipc_perm
*ipcp
= &shmds
.shm_perm
;
626 if (shmctl (shmid
, IPC_STAT
, &shmds
) == -1)
627 err(EXIT_FAILURE
, _("shmctl failed"));
629 printf (_("\nShared memory Segment shmid=%d\n"), shmid
);
630 printf (_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\n"),
631 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
);
632 printf (_("mode=%#o\taccess_perms=%#o\n"),
633 ipcp
->mode
, ipcp
->mode
& 0777);
634 printf (_("bytes=%lu\tlpid=%d\tcpid=%d\tnattch=%ld\n"),
635 (unsigned long) shmds
.shm_segsz
, shmds
.shm_lpid
, shmds
.shm_cpid
,
636 (long) shmds
.shm_nattch
);
637 printf (_("att_time=%-26.24s\n"),
638 shmds
.shm_atime
? ctime (&shmds
.shm_atime
) : _("Not set"));
639 printf (_("det_time=%-26.24s\n"),
640 shmds
.shm_dtime
? ctime (&shmds
.shm_dtime
) : _("Not set"));
641 printf (_("change_time=%-26.24s\n"), ctime (&shmds
.shm_ctime
));
647 void print_msg (int msqid
)
650 struct ipc_perm
*ipcp
= &buf
.msg_perm
;
652 if (msgctl (msqid
, IPC_STAT
, &buf
) == -1)
653 err(EXIT_FAILURE
, _("msgctl failed"));
655 printf (_("\nMessage Queue msqid=%d\n"), msqid
);
656 printf (_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\tmode=%#o\n"),
657 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
, ipcp
->mode
);
658 printf (_("cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n"),
660 * glibc-2.1.3 and earlier has unsigned short. glibc-2.1.91
661 * has variation between unsigned short, unsigned long.
662 * Austin has msgqnum_t (for msg_qbytes)
664 (long) buf
.msg_cbytes
, (long) buf
.msg_qbytes
,
665 (long) buf
.msg_qnum
, buf
.msg_lspid
, buf
.msg_lrpid
);
666 printf (_("send_time=%-26.24s\n"),
667 buf
.msg_stime
? ctime (&buf
.msg_stime
) : _("Not set"));
668 printf (_("rcv_time=%-26.24s\n"),
669 buf
.msg_rtime
? ctime (&buf
.msg_rtime
) : _("Not set"));
670 printf (_("change_time=%-26.24s\n"),
671 buf
.msg_ctime
? ctime (&buf
.msg_ctime
) : _("Not set"));
676 void print_sem (int semid
)
678 struct semid_ds semds
;
679 struct ipc_perm
*ipcp
= &semds
.sem_perm
;
684 if (semctl (semid
, 0, IPC_STAT
, arg
) < 0)
685 err(EXIT_FAILURE
, _("semctl failed"));
687 printf (_("\nSemaphore Array semid=%d\n"), semid
);
688 printf (_("uid=%u\t gid=%u\t cuid=%u\t cgid=%u\n"),
689 ipcp
->uid
, ipcp
->gid
, ipcp
->cuid
, ipcp
->cgid
);
690 printf (_("mode=%#o, access_perms=%#o\n"),
691 ipcp
->mode
, ipcp
->mode
& 0777);
692 printf (_("nsems = %ld\n"), (long) semds
.sem_nsems
);
693 printf (_("otime = %-26.24s\n"),
694 semds
.sem_otime
? ctime (&semds
.sem_otime
) : _("Not set"));
695 printf (_("ctime = %-26.24s\n"), ctime (&semds
.sem_ctime
));
697 printf ("%-10s %-10s %-10s %-10s %-10s\n",
698 _("semnum"),_("value"),_("ncount"),_("zcount"),_("pid"));
700 for (i
=0; i
< semds
.sem_nsems
; i
++) {
701 int val
, ncnt
, zcnt
, pid
;
702 val
= semctl (semid
, i
, GETVAL
, arg
);
703 ncnt
= semctl (semid
, i
, GETNCNT
, arg
);
704 zcnt
= semctl (semid
, i
, GETZCNT
, arg
);
705 pid
= semctl (semid
, i
, GETPID
, arg
);
706 if (val
< 0 || ncnt
< 0 || zcnt
< 0 || pid
< 0)
707 err(EXIT_FAILURE
, _("semctl failed"));
709 printf ("%-10zd %-10d %-10d %-10d %-10d\n",
710 i
, val
, ncnt
, zcnt
, pid
);