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
23 #include "closestream.h"
36 OPT_HUMAN
= CHAR_MAX
+ 1
39 static void do_shm (char format
, int unit
);
40 static void print_shm (int id
, int unit
);
41 static void do_sem (char format
);
42 static void print_sem (int id
);
43 static void do_msg (char format
, int unit
);
44 static void print_msg (int id
, int unit
);
46 /* we read time as int64_t from /proc, so cast... */
47 #define xctime(_x) ctime((time_t *) (_x))
49 static void __attribute__((__noreturn__
)) usage(void)
52 fputs(USAGE_HEADER
, out
);
53 fprintf(out
, _(" %1$s [resource-option...] [output-option]\n"
54 " %1$s -m|-q|-s -i <id>\n"), program_invocation_short_name
);
56 fputs(USAGE_SEPARATOR
, out
);
57 fputs(_("Show information on IPC facilities.\n"), out
);
59 fputs(USAGE_OPTIONS
, out
);
60 fputs(_(" -i, --id <id> print details on resource identified by <id>\n"), out
);
61 fputs(USAGE_HELP
, out
);
62 fputs(USAGE_VERSION
, out
);
64 fputs(USAGE_SEPARATOR
, out
);
65 fputs(_("Resource options:\n"), out
);
66 fputs(_(" -m, --shmems shared memory segments\n"), out
);
67 fputs(_(" -q, --queues message queues\n"), out
);
68 fputs(_(" -s, --semaphores semaphores\n"), out
);
69 fputs(_(" -a, --all all (default)\n"), out
);
71 fputs(USAGE_SEPARATOR
, out
);
72 fputs(_("Output options:\n"), out
);
73 fputs(_(" -t, --time show attach, detach and change times\n"), out
);
74 fputs(_(" -p, --pid show PIDs of creator and last operator\n"), out
);
75 fputs(_(" -c, --creator show creator and owner\n"), out
);
76 fputs(_(" -l, --limits show resource limits\n"), out
);
77 fputs(_(" -u, --summary show status summary\n"), out
);
78 fputs(_(" --human show sizes in human-readable format\n"), out
);
79 fputs(_(" -b, --bytes show sizes in bytes\n"), out
);
80 fprintf(out
, USAGE_MAN_TAIL("ipcs(1)"));
85 int main (int argc
, char **argv
)
87 int opt
, msg
= 0, shm
= 0, sem
= 0, id
= 0, specific
= 0;
88 char format
= NOTSPECIFIED
;
89 int unit
= IPC_UNIT_DEFAULT
;
90 static const struct option longopts
[] = {
91 {"id", required_argument
, NULL
, 'i'},
92 {"queues", no_argument
, NULL
, 'q'},
93 {"shmems", no_argument
, NULL
, 'm'},
94 {"semaphores", no_argument
, NULL
, 's'},
95 {"all", no_argument
, NULL
, 'a'},
96 {"time", no_argument
, NULL
, 't'},
97 {"pid", no_argument
, NULL
, 'p'},
98 {"creator", no_argument
, NULL
, 'c'},
99 {"limits", no_argument
, NULL
, 'l'},
100 {"summary", no_argument
, NULL
, 'u'},
101 {"human", no_argument
, NULL
, OPT_HUMAN
},
102 {"bytes", no_argument
, NULL
, 'b'},
103 {"version", no_argument
, NULL
, 'V'},
104 {"help", no_argument
, NULL
, 'h'},
107 char options
[] = "i:qmsatpclubVh";
109 setlocale(LC_ALL
, "");
110 bindtextdomain(PACKAGE
, LOCALEDIR
);
112 atexit(close_stdout
);
114 while ((opt
= getopt_long(argc
, argv
, options
, longopts
, NULL
)) != -1) {
148 unit
= IPC_UNIT_HUMAN
;
151 unit
= IPC_UNIT_BYTES
;
156 printf(UTIL_LINUX_VERSION
);
159 errtryhelp(EXIT_FAILURE
);
163 if (specific
&& (msg
+ shm
+ sem
!= 1))
165 _("when using an ID, a single resource must be specified"));
168 print_msg (id
, unit
);
170 print_shm (id
, unit
);
174 if (!msg
&& !shm
&& !sem
)
178 do_msg (format
, unit
);
182 do_shm (format
, unit
);
193 static void do_shm (char format
, int unit
)
196 struct shm_data
*shmds
, *shmdsp
;
201 struct ipc_limits lim
;
202 uint64_t tmp
, pgsz
= getpagesize();
204 if (ipc_shm_get_limits(&lim
)) {
205 printf (_("unable to fetch shared memory limits\n"));
208 printf (_("------ Shared Memory Limits --------\n"));
209 printf (_("max number of segments = %ju\n"), lim
.shmmni
);
210 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_KB
: unit
,
211 _("max seg size"), lim
.shmmax
, "\n", 0);
213 tmp
= (uint64_t) lim
.shmall
* pgsz
;
214 /* overflow handling, at least we don't print ridiculous small values */
215 if (lim
.shmall
!= 0 && tmp
/ lim
.shmall
!= pgsz
) {
216 tmp
= UINT64_MAX
- (UINT64_MAX
% pgsz
);
218 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_KB
: unit
,
219 _("max total shared memory"), tmp
, "\n", 0);
220 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
221 _("min seg size"), lim
.shmmin
, "\n", 0);
227 struct shmid_ds shmbuf
;
228 struct shm_info
*shm_info
;
230 maxid
= shmctl (0, SHM_INFO
, &shmbuf
);
231 shm_info
= (struct shm_info
*) &shmbuf
;
233 printf (_("kernel not configured for shared memory\n"));
237 printf (_("------ Shared Memory Status --------\n"));
239 * TRANSLATORS: This output format is maintained for backward
240 * compatibility as ipcs is used in scripts. For consistency
241 * with the rest, the translated form can follow this model:
243 * "segments allocated = %d\n"
244 * "pages allocated = %ld\n"
245 * "pages resident = %ld\n"
246 * "pages swapped = %ld\n"
247 * "swap performance = %ld attempts, %ld successes\n"
249 printf (_("segments allocated %d\n"
250 "pages allocated %ld\n"
251 "pages resident %ld\n"
252 "pages swapped %ld\n"
253 "Swap performance: %ld attempts\t %ld successes\n"),
258 shm_info
->swap_attempts
, shm_info
->swap_successes
);
266 printf (_("------ Shared Memory Segment Creators/Owners --------\n"));
267 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
268 _("shmid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
272 printf (_("------ Shared Memory Attach/Detach/Change Times --------\n"));
273 printf ("%-10s %-10s %-20s %-20s %-20s\n",
274 _("shmid"),_("owner"),_("attached"),_("detached"),
279 printf (_("------ Shared Memory Creator/Last-op PIDs --------\n"));
280 printf ("%-10s %-10s %-10s %-10s\n",
281 _("shmid"),_("owner"),_("cpid"),_("lpid"));
285 printf (_("------ Shared Memory Segments --------\n"));
286 printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
287 _("key"),_("shmid"),_("owner"),_("perms"),
288 unit
== IPC_UNIT_HUMAN
? _("size") : _("bytes"),
289 _("nattch"),_("status"));
296 if (ipc_shm_get_info(-1, &shmds
) < 1)
299 for (shmdsp
= shmds
; shmdsp
->next
!= NULL
; shmdsp
= shmdsp
->next
) {
300 if (format
== CREATOR
) {
301 ipc_print_perms(stdout
, &shmdsp
->shm_perm
);
304 pw
= getpwuid(shmdsp
->shm_perm
.uid
);
308 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
310 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
311 /* ctime uses static buffer: use separate calls */
312 printf(" %-20.16s", shmdsp
->shm_atim
313 ? xctime(&shmdsp
->shm_atim
) + 4 : _("Not set"));
314 printf(" %-20.16s", shmdsp
->shm_dtim
315 ? xctime(&shmdsp
->shm_dtim
) + 4 : _("Not set"));
316 printf(" %-20.16s\n", shmdsp
->shm_ctim
317 ? xctime(&shmdsp
->shm_ctim
) + 4 : _("Not set"));
321 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
323 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
324 printf (" %-10u %-10u\n",
325 shmdsp
->shm_cprid
, shmdsp
->shm_lprid
);
329 printf("0x%08x ", shmdsp
->shm_perm
.key
);
331 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
333 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
334 printf (" %-10o ", shmdsp
->shm_perm
.mode
& 0777);
336 if (unit
== IPC_UNIT_HUMAN
)
337 ipc_print_size(unit
, NULL
, shmdsp
->shm_segsz
, " ", 6);
339 ipc_print_size(unit
, NULL
, shmdsp
->shm_segsz
, NULL
, -10);
341 printf (" %-10ju %-6s %-6s\n",
343 shmdsp
->shm_perm
.mode
& SHM_DEST
? _("dest") : " ",
344 shmdsp
->shm_perm
.mode
& SHM_LOCKED
? _("locked") : " ");
349 ipc_shm_free_info(shmds
);
353 static void do_sem (char format
)
356 struct sem_data
*semds
, *semdsp
;
361 struct ipc_limits lim
;
363 if (ipc_sem_get_limits(&lim
)) {
364 printf (_("unable to fetch semaphore limits\n"));
367 printf (_("------ Semaphore Limits --------\n"));
368 printf (_("max number of arrays = %d\n"), lim
.semmni
);
369 printf (_("max semaphores per array = %d\n"), lim
.semmsl
);
370 printf (_("max semaphores system wide = %d\n"), lim
.semmns
);
371 printf (_("max ops per semop call = %d\n"), lim
.semopm
);
372 printf (_("semaphore max value = %u\n"), lim
.semvmx
);
377 struct seminfo seminfo
;
379 arg
.array
= (ushort
*) (void *) &seminfo
;
380 if (semctl (0, 0, SEM_INFO
, arg
) < 0) {
381 printf (_("kernel not configured for semaphores\n"));
384 printf (_("------ Semaphore Status --------\n"));
385 printf (_("used arrays = %d\n"), seminfo
.semusz
);
386 printf (_("allocated semaphores = %d\n"), seminfo
.semaem
);
391 printf (_("------ Semaphore Arrays Creators/Owners --------\n"));
392 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
393 _("semid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
397 printf (_("------ Semaphore Operation/Change Times --------\n"));
398 printf ("%-8s %-10s %-26.24s %-26.24s\n",
399 _("semid"),_("owner"),_("last-op"),_("last-changed"));
406 printf (_("------ Semaphore Arrays --------\n"));
407 printf ("%-10s %-10s %-10s %-10s %-10s\n",
408 _("key"),_("semid"),_("owner"),_("perms"),_("nsems"));
415 if (ipc_sem_get_info(-1, &semds
) < 1)
418 for (semdsp
= semds
; semdsp
->next
!= NULL
; semdsp
= semdsp
->next
) {
419 if (format
== CREATOR
) {
420 ipc_print_perms(stdout
, &semdsp
->sem_perm
);
423 pw
= getpwuid(semdsp
->sem_perm
.uid
);
427 printf ("%-8d %-10.10s", semdsp
->sem_perm
.id
, pw
->pw_name
);
429 printf ("%-8d %-10u", semdsp
->sem_perm
.id
, semdsp
->sem_perm
.uid
);
430 printf (" %-26.24s", semdsp
->sem_otime
431 ? xctime(&semdsp
->sem_otime
) : _("Not set"));
432 printf (" %-26.24s\n", semdsp
->sem_ctime
433 ? xctime( &semdsp
->sem_ctime
) : _("Not set"));
439 printf("0x%08x ", semdsp
->sem_perm
.key
);
441 printf ("%-10d %-10.10s", semdsp
->sem_perm
.id
, pw
->pw_name
);
443 printf ("%-10d %-10u", semdsp
->sem_perm
.id
, semdsp
->sem_perm
.uid
);
444 printf (" %-10o %-10ju\n",
445 semdsp
->sem_perm
.mode
& 0777,
451 ipc_sem_free_info(semds
);
455 static void do_msg (char format
, int unit
)
458 struct msg_data
*msgds
, *msgdsp
;
463 struct ipc_limits lim
;
465 if (ipc_msg_get_limits(&lim
)) {
466 printf (_("unable to fetch message limits\n"));
469 printf (_("------ Messages Limits --------\n"));
470 printf (_("max queues system wide = %d\n"), lim
.msgmni
);
471 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
472 _("max size of message"), lim
.msgmax
, "\n", 0);
473 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
474 _("default max size of queue"), lim
.msgmnb
, "\n", 0);
479 struct msginfo msginfo
;
480 if (msgctl (0, MSG_INFO
, (struct msqid_ds
*) (void *) &msginfo
) < 0) {
481 printf (_("kernel not configured for message queues\n"));
484 printf (_("------ Messages Status --------\n"));
485 #ifndef __FreeBSD_kernel__
486 printf (_("allocated queues = %d\n"), msginfo
.msgpool
);
487 printf (_("used headers = %d\n"), msginfo
.msgmap
);
489 ipc_print_size(unit
, _("used space"), msginfo
.msgtql
,
490 unit
== IPC_UNIT_DEFAULT
? _(" bytes\n") : "\n", 0);
494 printf (_("------ Message Queues Creators/Owners --------\n"));
495 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
496 _("msqid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
500 printf (_("------ Message Queues Send/Recv/Change Times --------\n"));
501 printf ("%-8s %-10s %-20s %-20s %-20s\n",
502 _("msqid"),_("owner"),_("send"),_("recv"),_("change"));
506 printf (_("------ Message Queues PIDs --------\n"));
507 printf ("%-10s %-10s %-10s %-10s\n",
508 _("msqid"),_("owner"),_("lspid"),_("lrpid"));
512 printf (_("------ Message Queues --------\n"));
513 printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
514 _("key"), _("msqid"), _("owner"), _("perms"),
515 unit
== IPC_UNIT_HUMAN
? _("size") : _("used-bytes"),
523 if (ipc_msg_get_info(-1, &msgds
) < 1)
526 for (msgdsp
= msgds
; msgdsp
->next
!= NULL
; msgdsp
= msgdsp
->next
) {
527 if (format
== CREATOR
) {
528 ipc_print_perms(stdout
, &msgdsp
->msg_perm
);
531 pw
= getpwuid(msgdsp
->msg_perm
.uid
);
535 printf ("%-8d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
537 printf ("%-8d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
538 printf (" %-20.16s", msgdsp
->q_stime
539 ? xctime(&msgdsp
->q_stime
) + 4 : _("Not set"));
540 printf (" %-20.16s", msgdsp
->q_rtime
541 ? xctime(&msgdsp
->q_rtime
) + 4 : _("Not set"));
542 printf (" %-20.16s\n", msgdsp
->q_ctime
543 ? xctime(&msgdsp
->q_ctime
) + 4 : _("Not set"));
547 printf ("%-8d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
549 printf ("%-8d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
550 printf (" %5d %5d\n",
551 msgdsp
->q_lspid
, msgdsp
->q_lrpid
);
555 printf( "0x%08x ",msgdsp
->msg_perm
.key
);
557 printf ("%-10d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
559 printf ("%-10d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
560 printf (" %-10o ", msgdsp
->msg_perm
.mode
& 0777);
562 if (unit
== IPC_UNIT_HUMAN
)
563 ipc_print_size(unit
, NULL
, msgdsp
->q_cbytes
, " ", 6);
565 ipc_print_size(unit
, NULL
, msgdsp
->q_cbytes
, NULL
, -12);
567 printf (" %-12ju\n", msgdsp
->q_qnum
);
572 ipc_msg_free_info(msgds
);
576 static void print_shm(int shmid
, int unit
)
578 struct shm_data
*shmdata
;
580 if (ipc_shm_get_info(shmid
, &shmdata
) < 1) {
581 warnx(_("id %d not found"), shmid
);
585 printf(_("\nShared memory Segment shmid=%d\n"), shmid
);
586 printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\n"),
587 shmdata
->shm_perm
.uid
, shmdata
->shm_perm
.gid
,
588 shmdata
->shm_perm
.cuid
, shmdata
->shm_perm
.cgid
);
589 printf(_("mode=%#o\taccess_perms=%#o\n"), shmdata
->shm_perm
.mode
,
590 shmdata
->shm_perm
.mode
& 0777);
591 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("size=") : _("bytes="),
592 shmdata
->shm_segsz
, "\t", 0);
593 printf(_("lpid=%u\tcpid=%u\tnattch=%jd\n"),
594 shmdata
->shm_lprid
, shmdata
->shm_cprid
,
595 shmdata
->shm_nattch
);
596 printf(_("att_time=%-26.24s\n"),
597 shmdata
->shm_atim
? xctime(&(shmdata
->shm_atim
)) : _("Not set"));
598 printf(_("det_time=%-26.24s\n"),
599 shmdata
->shm_dtim
? xctime(&shmdata
->shm_dtim
) : _("Not set"));
600 printf(_("change_time=%-26.24s\n"), xctime(&shmdata
->shm_ctim
));
603 ipc_shm_free_info(shmdata
);
606 static void print_msg(int msgid
, int unit
)
608 struct msg_data
*msgdata
;
610 if (ipc_msg_get_info(msgid
, &msgdata
) < 1) {
611 warnx(_("id %d not found"), msgid
);
615 printf(_("\nMessage Queue msqid=%d\n"), msgid
);
616 printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\tmode=%#o\n"),
617 msgdata
->msg_perm
.uid
, msgdata
->msg_perm
.gid
,
618 msgdata
->msg_perm
.cuid
, msgdata
->msg_perm
.cgid
,
619 msgdata
->msg_perm
.mode
);
620 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("csize=") : _("cbytes="),
621 msgdata
->q_cbytes
, "\t", 0);
622 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("qsize=") : _("qbytes="),
623 msgdata
->q_qbytes
, "\t", 0);
624 printf("qnum=%jd\tlspid=%d\tlrpid=%d\n",
626 msgdata
->q_lspid
, msgdata
->q_lrpid
);
627 printf(_("send_time=%-26.24s\n"),
628 msgdata
->q_stime
? xctime(&msgdata
->q_stime
) : _("Not set"));
629 printf(_("rcv_time=%-26.24s\n"),
630 msgdata
->q_rtime
? xctime(&msgdata
->q_rtime
) : _("Not set"));
631 printf(_("change_time=%-26.24s\n"),
632 msgdata
->q_ctime
? xctime(&msgdata
->q_ctime
) : _("Not set"));
635 ipc_msg_free_info(msgdata
);
638 static void print_sem(int semid
)
640 struct sem_data
*semdata
;
643 if (ipc_sem_get_info(semid
, &semdata
) < 1) {
644 warnx(_("id %d not found"), semid
);
648 printf(_("\nSemaphore Array semid=%d\n"), semid
);
649 printf(_("uid=%u\t gid=%u\t cuid=%u\t cgid=%u\n"),
650 semdata
->sem_perm
.uid
, semdata
->sem_perm
.gid
,
651 semdata
->sem_perm
.cuid
, semdata
->sem_perm
.cgid
);
652 printf(_("mode=%#o, access_perms=%#o\n"),
653 semdata
->sem_perm
.mode
, semdata
->sem_perm
.mode
& 0777);
654 printf(_("nsems = %ju\n"), semdata
->sem_nsems
);
655 printf(_("otime = %-26.24s\n"),
656 semdata
->sem_otime
? xctime(&semdata
->sem_otime
) : _("Not set"));
657 printf(_("ctime = %-26.24s\n"), xctime(&semdata
->sem_ctime
));
659 printf("%-10s %-10s %-10s %-10s %-10s\n",
660 _("semnum"), _("value"), _("ncount"), _("zcount"), _("pid"));
662 for (i
= 0; i
< semdata
->sem_nsems
; i
++) {
663 struct sem_elem
*e
= &semdata
->elements
[i
];
664 printf("%-10zu %-10d %-10d %-10d %-10d\n",
665 i
, e
->semval
, e
->ncount
, e
->zcount
, e
->pid
);
668 ipc_sem_free_info(semdata
);