2 * SPDX-License-Identifier: GPL-2.0-or-later
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * Original author unknown, may be "krishna balasub@cis.ohio-state.edu"
11 * Copyright (C) 1995 ike Jagdis <jaggy@purplet.demon.co.uk>
12 * 1996 janl@math.uio.no
13 * Copyright (C) 2006-2023 Karel Zak <kzak@redhat.com>
16 * Patches from Mike Jagdis (jaggy@purplet.demon.co.uk) applied Wed Feb 8
17 * 12:12:21 1995 by faith@cs.unc.edu to print numeric uids if no passwd file
20 * Patch from arnolds@ifns.de (Heinz-Ado Arnolds) applied Mon Jul 1 19:30:41
21 * 1996 by janl@math.uio.no to add code missing in case PID: clauses.
23 * Patched to display the key field -- hy@picksys.com 12/18/96
25 * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL>
26 * - added Native Language Support
33 #include "closestream.h"
34 #include "timeutils.h"
48 OPT_HUMAN
= CHAR_MAX
+ 1
51 static void do_shm (char format
, int unit
);
52 static void print_shm (int id
, int unit
);
53 static void do_sem (char format
);
54 static void print_sem (int id
);
55 static void do_msg (char format
, int unit
);
56 static void print_msg (int id
, int unit
);
58 static inline char *ctime64(int64_t *t
)
60 static char buf
[CTIME_BUFSIZ
];
62 /* we read time as int64_t from /proc, so cast... */
63 ctime_r((time_t *)t
, buf
);
67 static void __attribute__((__noreturn__
)) usage(void)
70 fputs(USAGE_HEADER
, out
);
71 fprintf(out
, _(" %1$s [resource-option...] [output-option]\n"
72 " %1$s -m|-q|-s -i <id>\n"), program_invocation_short_name
);
74 fputs(USAGE_SEPARATOR
, out
);
75 fputs(_("Show information on IPC facilities.\n"), out
);
77 fputs(USAGE_OPTIONS
, out
);
78 fputs(_(" -i, --id <id> print details on resource identified by <id>\n"), out
);
79 fprintf(out
, USAGE_HELP_OPTIONS(16));
81 fputs(USAGE_SEPARATOR
, out
);
82 fputs(_("Resource options:\n"), out
);
83 fputs(_(" -m, --shmems shared memory segments\n"), out
);
84 fputs(_(" -q, --queues message queues\n"), out
);
85 fputs(_(" -s, --semaphores semaphores\n"), out
);
86 fputs(_(" -a, --all all (default)\n"), out
);
88 fputs(USAGE_SEPARATOR
, out
);
89 fputs(_("Output options:\n"), out
);
90 fputs(_(" -t, --time show attach, detach and change times\n"), out
);
91 fputs(_(" -p, --pid show PIDs of creator and last operator\n"), out
);
92 fputs(_(" -c, --creator show creator and owner\n"), out
);
93 fputs(_(" -l, --limits show resource limits\n"), out
);
94 fputs(_(" -u, --summary show status summary\n"), out
);
95 fputs(_(" --human show sizes in human-readable format\n"), out
);
96 fputs(_(" -b, --bytes show sizes in bytes\n"), out
);
97 fprintf(out
, USAGE_MAN_TAIL("ipcs(1)"));
102 int main (int argc
, char **argv
)
104 int opt
, msg
= 0, shm
= 0, sem
= 0, id
= 0, specific
= 0;
105 char format
= NOTSPECIFIED
;
106 int unit
= IPC_UNIT_DEFAULT
;
107 static const struct option longopts
[] = {
108 {"id", required_argument
, NULL
, 'i'},
109 {"queues", no_argument
, NULL
, 'q'},
110 {"shmems", no_argument
, NULL
, 'm'},
111 {"semaphores", no_argument
, NULL
, 's'},
112 {"all", no_argument
, NULL
, 'a'},
113 {"time", no_argument
, NULL
, 't'},
114 {"pid", no_argument
, NULL
, 'p'},
115 {"creator", no_argument
, NULL
, 'c'},
116 {"limits", no_argument
, NULL
, 'l'},
117 {"summary", no_argument
, NULL
, 'u'},
118 {"human", no_argument
, NULL
, OPT_HUMAN
},
119 {"bytes", no_argument
, NULL
, 'b'},
120 {"version", no_argument
, NULL
, 'V'},
121 {"help", no_argument
, NULL
, 'h'},
124 char options
[] = "i:qmsatpclubVh";
126 setlocale(LC_ALL
, "");
127 bindtextdomain(PACKAGE
, LOCALEDIR
);
129 close_stdout_atexit();
131 while ((opt
= getopt_long(argc
, argv
, options
, longopts
, NULL
)) != -1) {
134 id
= strtos32_or_err(optarg
, _("failed to parse id argument"));
165 unit
= IPC_UNIT_HUMAN
;
168 unit
= IPC_UNIT_BYTES
;
174 print_version(EXIT_SUCCESS
);
176 errtryhelp(EXIT_FAILURE
);
180 if (specific
&& (msg
+ shm
+ sem
!= 1))
182 _("when using an ID, a single resource must be specified"));
185 print_msg (id
, unit
);
187 print_shm (id
, unit
);
191 if (!msg
&& !shm
&& !sem
)
195 do_msg (format
, unit
);
199 do_shm (format
, unit
);
210 static void do_shm (char format
, int unit
)
213 struct shm_data
*shmds
, *shmdsp
;
218 struct ipc_limits lim
;
219 uint64_t tmp
, pgsz
= getpagesize();
221 if (ipc_shm_get_limits(&lim
)) {
222 printf (_("unable to fetch shared memory limits\n"));
225 printf (_("------ Shared Memory Limits --------\n"));
226 printf (_("max number of segments = %ju\n"), lim
.shmmni
);
227 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_KB
: unit
,
228 _("max seg size"), lim
.shmmax
, "\n", 0);
230 if (unit
== IPC_UNIT_KB
|| unit
== IPC_UNIT_DEFAULT
) {
231 tmp
= (uint64_t) lim
.shmall
* (pgsz
/ 1024);
232 if (lim
.shmall
!= 0 && tmp
/ lim
.shmall
!= pgsz
/ 1024)
233 tmp
= UINT64_MAX
- (UINT64_MAX
% (pgsz
/ 1024));
235 ipc_print_size(IPC_UNIT_DEFAULT
, _("max total shared memory (kbytes)"), tmp
, "\n", 0);
238 tmp
= (uint64_t) lim
.shmall
* pgsz
;
239 /* overflow handling, at least we don't print ridiculous small values */
240 if (lim
.shmall
!= 0 && tmp
/ lim
.shmall
!= pgsz
)
241 tmp
= UINT64_MAX
- (UINT64_MAX
% pgsz
);
243 ipc_print_size(unit
, _("max total shared memory"), tmp
, "\n", 0);
245 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
246 _("min seg size"), lim
.shmmin
, "\n", 0);
252 struct shmid_ds shmbuf
;
253 struct shm_info
*shm_info
;
255 maxid
= shmctl (0, SHM_INFO
, &shmbuf
);
256 shm_info
= (struct shm_info
*) &shmbuf
;
258 printf (_("kernel not configured for shared memory\n"));
262 printf (_("------ Shared Memory Status --------\n"));
264 * TRANSLATORS: This output format is maintained for backward
265 * compatibility as ipcs is used in scripts. For consistency
266 * with the rest, the translated form can follow this model:
268 * "segments allocated = %d\n"
269 * "pages allocated = %ld\n"
270 * "pages resident = %ld\n"
271 * "pages swapped = %ld\n"
272 * "swap performance = %ld attempts, %ld successes\n"
274 printf (_("segments allocated %d\n"
275 "pages allocated %ld\n"
276 "pages resident %ld\n"
277 "pages swapped %ld\n"
278 "Swap performance: %ld attempts\t %ld successes\n"),
283 shm_info
->swap_attempts
, shm_info
->swap_successes
);
291 printf (_("------ Shared Memory Segment Creators/Owners --------\n"));
292 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
293 _("shmid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
297 printf (_("------ Shared Memory Attach/Detach/Change Times --------\n"));
298 printf ("%-10s %-10s %-20s %-20s %-20s\n",
299 _("shmid"),_("owner"),_("attached"),_("detached"),
304 printf (_("------ Shared Memory Creator/Last-op PIDs --------\n"));
305 printf ("%-10s %-10s %-10s %-10s\n",
306 _("shmid"),_("owner"),_("cpid"),_("lpid"));
310 printf (_("------ Shared Memory Segments --------\n"));
311 printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
312 _("key"),_("shmid"),_("owner"),_("perms"),
313 unit
== IPC_UNIT_HUMAN
? _("size") : _("bytes"),
314 _("nattch"),_("status"));
321 if (ipc_shm_get_info(-1, &shmds
) < 1)
324 for (shmdsp
= shmds
; shmdsp
->next
!= NULL
; shmdsp
= shmdsp
->next
) {
325 if (format
== CREATOR
) {
326 ipc_print_perms(stdout
, &shmdsp
->shm_perm
);
329 pw
= getpwuid(shmdsp
->shm_perm
.uid
);
333 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
335 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
336 /* ctime uses static buffer: use separate calls */
337 printf(" %-20.16s", shmdsp
->shm_atim
338 ? ctime64(&shmdsp
->shm_atim
) + 4 : _("Not set"));
339 printf(" %-20.16s", shmdsp
->shm_dtim
340 ? ctime64(&shmdsp
->shm_dtim
) + 4 : _("Not set"));
341 printf(" %-20.16s\n", shmdsp
->shm_ctim
342 ? ctime64(&shmdsp
->shm_ctim
) + 4 : _("Not set"));
346 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
348 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
349 printf (" %-10u %-10u\n",
350 shmdsp
->shm_cprid
, shmdsp
->shm_lprid
);
354 printf("0x%08x ", shmdsp
->shm_perm
.key
);
356 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
358 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
359 printf (" %-10o ", shmdsp
->shm_perm
.mode
& 0777);
361 if (unit
== IPC_UNIT_HUMAN
)
362 ipc_print_size(unit
, NULL
, shmdsp
->shm_segsz
, " ", 6);
364 ipc_print_size(unit
, NULL
, shmdsp
->shm_segsz
, NULL
, -10);
366 printf (" %-10ju %-6s %-6s\n",
368 shmdsp
->shm_perm
.mode
& SHM_DEST
? _("dest") : " ",
369 shmdsp
->shm_perm
.mode
& SHM_LOCKED
? _("locked") : " ");
374 ipc_shm_free_info(shmds
);
377 static void do_sem (char format
)
380 struct sem_data
*semds
, *semdsp
;
385 struct ipc_limits lim
;
387 if (ipc_sem_get_limits(&lim
)) {
388 printf (_("unable to fetch semaphore limits\n"));
391 printf (_("------ Semaphore Limits --------\n"));
392 printf (_("max number of arrays = %d\n"), lim
.semmni
);
393 printf (_("max semaphores per array = %d\n"), lim
.semmsl
);
394 printf (_("max semaphores system wide = %d\n"), lim
.semmns
);
395 printf (_("max ops per semop call = %d\n"), lim
.semopm
);
396 printf (_("semaphore max value = %u\n"), lim
.semvmx
);
401 struct seminfo seminfo
;
403 arg
.array
= (ushort
*) (void *) &seminfo
;
404 if (semctl (0, 0, SEM_INFO
, arg
) < 0) {
405 printf (_("kernel not configured for semaphores\n"));
408 printf (_("------ Semaphore Status --------\n"));
409 printf (_("used arrays = %d\n"), seminfo
.semusz
);
410 printf (_("allocated semaphores = %d\n"), seminfo
.semaem
);
415 printf (_("------ Semaphore Arrays Creators/Owners --------\n"));
416 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
417 _("semid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
421 printf (_("------ Semaphore Operation/Change Times --------\n"));
422 printf ("%-8s %-10s %-26.24s %-26.24s\n",
423 _("semid"),_("owner"),_("last-op"),_("last-changed"));
430 printf (_("------ Semaphore Arrays --------\n"));
431 printf ("%-10s %-10s %-10s %-10s %-10s\n",
432 _("key"),_("semid"),_("owner"),_("perms"),_("nsems"));
439 if (ipc_sem_get_info(-1, &semds
) < 1)
442 for (semdsp
= semds
; semdsp
->next
!= NULL
; semdsp
= semdsp
->next
) {
443 if (format
== CREATOR
) {
444 ipc_print_perms(stdout
, &semdsp
->sem_perm
);
447 pw
= getpwuid(semdsp
->sem_perm
.uid
);
451 printf ("%-8d %-10.10s", semdsp
->sem_perm
.id
, pw
->pw_name
);
453 printf ("%-8d %-10u", semdsp
->sem_perm
.id
, semdsp
->sem_perm
.uid
);
454 printf (" %-26.24s", semdsp
->sem_otime
455 ? ctime64(&semdsp
->sem_otime
) : _("Not set"));
456 printf (" %-26.24s\n", semdsp
->sem_ctime
457 ? ctime64( &semdsp
->sem_ctime
) : _("Not set"));
463 printf("0x%08x ", semdsp
->sem_perm
.key
);
465 printf ("%-10d %-10.10s", semdsp
->sem_perm
.id
, pw
->pw_name
);
467 printf ("%-10d %-10u", semdsp
->sem_perm
.id
, semdsp
->sem_perm
.uid
);
468 printf (" %-10o %-10ju\n",
469 semdsp
->sem_perm
.mode
& 0777,
475 ipc_sem_free_info(semds
);
478 static void do_msg (char format
, int unit
)
481 struct msg_data
*msgds
, *msgdsp
;
486 struct ipc_limits lim
;
488 if (ipc_msg_get_limits(&lim
)) {
489 printf (_("unable to fetch message limits\n"));
492 printf (_("------ Messages Limits --------\n"));
493 printf (_("max queues system wide = %d\n"), lim
.msgmni
);
494 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
495 _("max size of message"), lim
.msgmax
, "\n", 0);
496 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
497 _("default max size of queue"), lim
.msgmnb
, "\n", 0);
502 struct msginfo msginfo
;
503 if (msgctl (0, MSG_INFO
, (struct msqid_ds
*) (void *) &msginfo
) < 0) {
504 printf (_("kernel not configured for message queues\n"));
507 printf (_("------ Messages Status --------\n"));
508 #ifndef __FreeBSD_kernel__
509 printf (_("allocated queues = %d\n"), msginfo
.msgpool
);
510 printf (_("used headers = %d\n"), msginfo
.msgmap
);
512 ipc_print_size(unit
, _("used space"), msginfo
.msgtql
,
513 unit
== IPC_UNIT_DEFAULT
? _(" bytes\n") : "\n", 0);
517 printf (_("------ Message Queues Creators/Owners --------\n"));
518 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
519 _("msqid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
523 printf (_("------ Message Queues Send/Recv/Change Times --------\n"));
524 printf ("%-8s %-10s %-20s %-20s %-20s\n",
525 _("msqid"),_("owner"),_("send"),_("recv"),_("change"));
529 printf (_("------ Message Queues PIDs --------\n"));
530 printf ("%-10s %-10s %-10s %-10s\n",
531 _("msqid"),_("owner"),_("lspid"),_("lrpid"));
535 printf (_("------ Message Queues --------\n"));
536 printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
537 _("key"), _("msqid"), _("owner"), _("perms"),
538 unit
== IPC_UNIT_HUMAN
? _("size") : _("used-bytes"),
546 if (ipc_msg_get_info(-1, &msgds
) < 1)
549 for (msgdsp
= msgds
; msgdsp
->next
!= NULL
; msgdsp
= msgdsp
->next
) {
550 if (format
== CREATOR
) {
551 ipc_print_perms(stdout
, &msgdsp
->msg_perm
);
554 pw
= getpwuid(msgdsp
->msg_perm
.uid
);
558 printf ("%-8d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
560 printf ("%-8d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
561 printf (" %-20.16s", msgdsp
->q_stime
562 ? ctime64(&msgdsp
->q_stime
) + 4 : _("Not set"));
563 printf (" %-20.16s", msgdsp
->q_rtime
564 ? ctime64(&msgdsp
->q_rtime
) + 4 : _("Not set"));
565 printf (" %-20.16s\n", msgdsp
->q_ctime
566 ? ctime64(&msgdsp
->q_ctime
) + 4 : _("Not set"));
570 printf ("%-8d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
572 printf ("%-8d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
573 printf (" %5d %5d\n",
574 msgdsp
->q_lspid
, msgdsp
->q_lrpid
);
578 printf( "0x%08x ",msgdsp
->msg_perm
.key
);
580 printf ("%-10d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
582 printf ("%-10d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
583 printf (" %-10o ", msgdsp
->msg_perm
.mode
& 0777);
585 if (unit
== IPC_UNIT_HUMAN
)
586 ipc_print_size(unit
, NULL
, msgdsp
->q_cbytes
, " ", 6);
588 ipc_print_size(unit
, NULL
, msgdsp
->q_cbytes
, NULL
, -12);
590 printf (" %-12ju\n", msgdsp
->q_qnum
);
595 ipc_msg_free_info(msgds
);
598 static void print_shm(int shmid
, int unit
)
600 struct shm_data
*shmdata
;
602 if (ipc_shm_get_info(shmid
, &shmdata
) < 1) {
603 warnx(_("id %d not found"), shmid
);
607 printf(_("\nShared memory Segment shmid=%d\n"), shmid
);
608 printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\n"),
609 shmdata
->shm_perm
.uid
, shmdata
->shm_perm
.gid
,
610 shmdata
->shm_perm
.cuid
, shmdata
->shm_perm
.cgid
);
611 printf(_("mode=%#o\taccess_perms=%#o\n"), shmdata
->shm_perm
.mode
,
612 shmdata
->shm_perm
.mode
& 0777);
613 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("size=") : _("bytes="),
614 shmdata
->shm_segsz
, "\t", 0);
615 printf(_("lpid=%u\tcpid=%u\tnattch=%jd\n"),
616 shmdata
->shm_lprid
, shmdata
->shm_cprid
,
617 shmdata
->shm_nattch
);
618 printf(_("att_time=%-26.24s\n"),
619 shmdata
->shm_atim
? ctime64(&(shmdata
->shm_atim
)) : _("Not set"));
620 printf(_("det_time=%-26.24s\n"),
621 shmdata
->shm_dtim
? ctime64(&shmdata
->shm_dtim
) : _("Not set"));
622 printf(_("change_time=%-26.24s\n"), ctime64(&shmdata
->shm_ctim
));
625 ipc_shm_free_info(shmdata
);
628 static void print_msg(int msgid
, int unit
)
630 struct msg_data
*msgdata
;
632 if (ipc_msg_get_info(msgid
, &msgdata
) < 1) {
633 warnx(_("id %d not found"), msgid
);
637 printf(_("\nMessage Queue msqid=%d\n"), msgid
);
638 printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\tmode=%#o\n"),
639 msgdata
->msg_perm
.uid
, msgdata
->msg_perm
.gid
,
640 msgdata
->msg_perm
.cuid
, msgdata
->msg_perm
.cgid
,
641 msgdata
->msg_perm
.mode
);
642 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("csize=") : _("cbytes="),
643 msgdata
->q_cbytes
, "\t", 0);
644 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("qsize=") : _("qbytes="),
645 msgdata
->q_qbytes
, "\t", 0);
646 printf("qnum=%jd\tlspid=%d\tlrpid=%d\n",
648 msgdata
->q_lspid
, msgdata
->q_lrpid
);
649 printf(_("send_time=%-26.24s\n"),
650 msgdata
->q_stime
? ctime64(&msgdata
->q_stime
) : _("Not set"));
651 printf(_("rcv_time=%-26.24s\n"),
652 msgdata
->q_rtime
? ctime64(&msgdata
->q_rtime
) : _("Not set"));
653 printf(_("change_time=%-26.24s\n"),
654 msgdata
->q_ctime
? ctime64(&msgdata
->q_ctime
) : _("Not set"));
657 ipc_msg_free_info(msgdata
);
660 static void print_sem(int semid
)
662 struct sem_data
*semdata
;
665 if (ipc_sem_get_info(semid
, &semdata
) < 1) {
666 warnx(_("id %d not found"), semid
);
670 printf(_("\nSemaphore Array semid=%d\n"), semid
);
671 printf(_("uid=%u\t gid=%u\t cuid=%u\t cgid=%u\n"),
672 semdata
->sem_perm
.uid
, semdata
->sem_perm
.gid
,
673 semdata
->sem_perm
.cuid
, semdata
->sem_perm
.cgid
);
674 printf(_("mode=%#o, access_perms=%#o\n"),
675 semdata
->sem_perm
.mode
, semdata
->sem_perm
.mode
& 0777);
676 printf(_("nsems = %ju\n"), semdata
->sem_nsems
);
677 printf(_("otime = %-26.24s\n"),
678 semdata
->sem_otime
? ctime64(&semdata
->sem_otime
) : _("Not set"));
679 printf(_("ctime = %-26.24s\n"), ctime64(&semdata
->sem_ctime
));
681 printf("%-10s %-10s %-10s %-10s %-10s\n",
682 _("semnum"), _("value"), _("ncount"), _("zcount"), _("pid"));
684 for (i
= 0; i
< semdata
->sem_nsems
; i
++) {
685 struct sem_elem
*e
= &semdata
->elements
[i
];
686 printf("%-10zu %-10d %-10d %-10d %-10d\n",
687 i
, e
->semval
, e
->ncount
, e
->zcount
, e
->pid
);
690 ipc_sem_free_info(semdata
);