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 printf(USAGE_HELP_OPTIONS(16));
63 fputs(USAGE_SEPARATOR
, out
);
64 fputs(_("Resource options:\n"), out
);
65 fputs(_(" -m, --shmems shared memory segments\n"), out
);
66 fputs(_(" -q, --queues message queues\n"), out
);
67 fputs(_(" -s, --semaphores semaphores\n"), out
);
68 fputs(_(" -a, --all all (default)\n"), out
);
70 fputs(USAGE_SEPARATOR
, out
);
71 fputs(_("Output options:\n"), out
);
72 fputs(_(" -t, --time show attach, detach and change times\n"), out
);
73 fputs(_(" -p, --pid show PIDs of creator and last operator\n"), out
);
74 fputs(_(" -c, --creator show creator and owner\n"), out
);
75 fputs(_(" -l, --limits show resource limits\n"), out
);
76 fputs(_(" -u, --summary show status summary\n"), out
);
77 fputs(_(" --human show sizes in human-readable format\n"), out
);
78 fputs(_(" -b, --bytes show sizes in bytes\n"), out
);
79 printf(USAGE_MAN_TAIL("ipcs(1)"));
84 int main (int argc
, char **argv
)
86 int opt
, msg
= 0, shm
= 0, sem
= 0, id
= 0, specific
= 0;
87 char format
= NOTSPECIFIED
;
88 int unit
= IPC_UNIT_DEFAULT
;
89 static const struct option longopts
[] = {
90 {"id", required_argument
, NULL
, 'i'},
91 {"queues", no_argument
, NULL
, 'q'},
92 {"shmems", no_argument
, NULL
, 'm'},
93 {"semaphores", no_argument
, NULL
, 's'},
94 {"all", no_argument
, NULL
, 'a'},
95 {"time", no_argument
, NULL
, 't'},
96 {"pid", no_argument
, NULL
, 'p'},
97 {"creator", no_argument
, NULL
, 'c'},
98 {"limits", no_argument
, NULL
, 'l'},
99 {"summary", no_argument
, NULL
, 'u'},
100 {"human", no_argument
, NULL
, OPT_HUMAN
},
101 {"bytes", no_argument
, NULL
, 'b'},
102 {"version", no_argument
, NULL
, 'V'},
103 {"help", no_argument
, NULL
, 'h'},
106 char options
[] = "i:qmsatpclubVh";
108 setlocale(LC_ALL
, "");
109 bindtextdomain(PACKAGE
, LOCALEDIR
);
111 close_stdout_atexit();
113 while ((opt
= getopt_long(argc
, argv
, options
, longopts
, NULL
)) != -1) {
147 unit
= IPC_UNIT_HUMAN
;
150 unit
= IPC_UNIT_BYTES
;
156 print_version(EXIT_SUCCESS
);
158 errtryhelp(EXIT_FAILURE
);
162 if (specific
&& (msg
+ shm
+ sem
!= 1))
164 _("when using an ID, a single resource must be specified"));
167 print_msg (id
, unit
);
169 print_shm (id
, unit
);
173 if (!msg
&& !shm
&& !sem
)
177 do_msg (format
, unit
);
181 do_shm (format
, unit
);
192 static void do_shm (char format
, int unit
)
195 struct shm_data
*shmds
, *shmdsp
;
200 struct ipc_limits lim
;
201 uint64_t tmp
, pgsz
= getpagesize();
203 if (ipc_shm_get_limits(&lim
)) {
204 printf (_("unable to fetch shared memory limits\n"));
207 printf (_("------ Shared Memory Limits --------\n"));
208 printf (_("max number of segments = %ju\n"), lim
.shmmni
);
209 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_KB
: unit
,
210 _("max seg size"), lim
.shmmax
, "\n", 0);
212 tmp
= (uint64_t) lim
.shmall
* pgsz
;
213 /* overflow handling, at least we don't print ridiculous small values */
214 if (lim
.shmall
!= 0 && tmp
/ lim
.shmall
!= pgsz
) {
215 tmp
= UINT64_MAX
- (UINT64_MAX
% pgsz
);
217 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_KB
: unit
,
218 _("max total shared memory"), tmp
, "\n", 0);
219 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
220 _("min seg size"), lim
.shmmin
, "\n", 0);
226 struct shmid_ds shmbuf
;
227 struct shm_info
*shm_info
;
229 maxid
= shmctl (0, SHM_INFO
, &shmbuf
);
230 shm_info
= (struct shm_info
*) &shmbuf
;
232 printf (_("kernel not configured for shared memory\n"));
236 printf (_("------ Shared Memory Status --------\n"));
238 * TRANSLATORS: This output format is maintained for backward
239 * compatibility as ipcs is used in scripts. For consistency
240 * with the rest, the translated form can follow this model:
242 * "segments allocated = %d\n"
243 * "pages allocated = %ld\n"
244 * "pages resident = %ld\n"
245 * "pages swapped = %ld\n"
246 * "swap performance = %ld attempts, %ld successes\n"
248 printf (_("segments allocated %d\n"
249 "pages allocated %ld\n"
250 "pages resident %ld\n"
251 "pages swapped %ld\n"
252 "Swap performance: %ld attempts\t %ld successes\n"),
257 shm_info
->swap_attempts
, shm_info
->swap_successes
);
265 printf (_("------ Shared Memory Segment Creators/Owners --------\n"));
266 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
267 _("shmid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
271 printf (_("------ Shared Memory Attach/Detach/Change Times --------\n"));
272 printf ("%-10s %-10s %-20s %-20s %-20s\n",
273 _("shmid"),_("owner"),_("attached"),_("detached"),
278 printf (_("------ Shared Memory Creator/Last-op PIDs --------\n"));
279 printf ("%-10s %-10s %-10s %-10s\n",
280 _("shmid"),_("owner"),_("cpid"),_("lpid"));
284 printf (_("------ Shared Memory Segments --------\n"));
285 printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
286 _("key"),_("shmid"),_("owner"),_("perms"),
287 unit
== IPC_UNIT_HUMAN
? _("size") : _("bytes"),
288 _("nattch"),_("status"));
295 if (ipc_shm_get_info(-1, &shmds
) < 1)
298 for (shmdsp
= shmds
; shmdsp
->next
!= NULL
; shmdsp
= shmdsp
->next
) {
299 if (format
== CREATOR
) {
300 ipc_print_perms(stdout
, &shmdsp
->shm_perm
);
303 pw
= getpwuid(shmdsp
->shm_perm
.uid
);
307 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
309 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
310 /* ctime uses static buffer: use separate calls */
311 printf(" %-20.16s", shmdsp
->shm_atim
312 ? xctime(&shmdsp
->shm_atim
) + 4 : _("Not set"));
313 printf(" %-20.16s", shmdsp
->shm_dtim
314 ? xctime(&shmdsp
->shm_dtim
) + 4 : _("Not set"));
315 printf(" %-20.16s\n", shmdsp
->shm_ctim
316 ? xctime(&shmdsp
->shm_ctim
) + 4 : _("Not set"));
320 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
322 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
323 printf (" %-10u %-10u\n",
324 shmdsp
->shm_cprid
, shmdsp
->shm_lprid
);
328 printf("0x%08x ", shmdsp
->shm_perm
.key
);
330 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
332 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
333 printf (" %-10o ", shmdsp
->shm_perm
.mode
& 0777);
335 if (unit
== IPC_UNIT_HUMAN
)
336 ipc_print_size(unit
, NULL
, shmdsp
->shm_segsz
, " ", 6);
338 ipc_print_size(unit
, NULL
, shmdsp
->shm_segsz
, NULL
, -10);
340 printf (" %-10ju %-6s %-6s\n",
342 shmdsp
->shm_perm
.mode
& SHM_DEST
? _("dest") : " ",
343 shmdsp
->shm_perm
.mode
& SHM_LOCKED
? _("locked") : " ");
348 ipc_shm_free_info(shmds
);
352 static void do_sem (char format
)
355 struct sem_data
*semds
, *semdsp
;
360 struct ipc_limits lim
;
362 if (ipc_sem_get_limits(&lim
)) {
363 printf (_("unable to fetch semaphore limits\n"));
366 printf (_("------ Semaphore Limits --------\n"));
367 printf (_("max number of arrays = %d\n"), lim
.semmni
);
368 printf (_("max semaphores per array = %d\n"), lim
.semmsl
);
369 printf (_("max semaphores system wide = %d\n"), lim
.semmns
);
370 printf (_("max ops per semop call = %d\n"), lim
.semopm
);
371 printf (_("semaphore max value = %u\n"), lim
.semvmx
);
376 struct seminfo seminfo
;
378 arg
.array
= (ushort
*) (void *) &seminfo
;
379 if (semctl (0, 0, SEM_INFO
, arg
) < 0) {
380 printf (_("kernel not configured for semaphores\n"));
383 printf (_("------ Semaphore Status --------\n"));
384 printf (_("used arrays = %d\n"), seminfo
.semusz
);
385 printf (_("allocated semaphores = %d\n"), seminfo
.semaem
);
390 printf (_("------ Semaphore Arrays Creators/Owners --------\n"));
391 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
392 _("semid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
396 printf (_("------ Semaphore Operation/Change Times --------\n"));
397 printf ("%-8s %-10s %-26.24s %-26.24s\n",
398 _("semid"),_("owner"),_("last-op"),_("last-changed"));
405 printf (_("------ Semaphore Arrays --------\n"));
406 printf ("%-10s %-10s %-10s %-10s %-10s\n",
407 _("key"),_("semid"),_("owner"),_("perms"),_("nsems"));
414 if (ipc_sem_get_info(-1, &semds
) < 1)
417 for (semdsp
= semds
; semdsp
->next
!= NULL
; semdsp
= semdsp
->next
) {
418 if (format
== CREATOR
) {
419 ipc_print_perms(stdout
, &semdsp
->sem_perm
);
422 pw
= getpwuid(semdsp
->sem_perm
.uid
);
426 printf ("%-8d %-10.10s", semdsp
->sem_perm
.id
, pw
->pw_name
);
428 printf ("%-8d %-10u", semdsp
->sem_perm
.id
, semdsp
->sem_perm
.uid
);
429 printf (" %-26.24s", semdsp
->sem_otime
430 ? xctime(&semdsp
->sem_otime
) : _("Not set"));
431 printf (" %-26.24s\n", semdsp
->sem_ctime
432 ? xctime( &semdsp
->sem_ctime
) : _("Not set"));
438 printf("0x%08x ", semdsp
->sem_perm
.key
);
440 printf ("%-10d %-10.10s", semdsp
->sem_perm
.id
, pw
->pw_name
);
442 printf ("%-10d %-10u", semdsp
->sem_perm
.id
, semdsp
->sem_perm
.uid
);
443 printf (" %-10o %-10ju\n",
444 semdsp
->sem_perm
.mode
& 0777,
450 ipc_sem_free_info(semds
);
454 static void do_msg (char format
, int unit
)
457 struct msg_data
*msgds
, *msgdsp
;
462 struct ipc_limits lim
;
464 if (ipc_msg_get_limits(&lim
)) {
465 printf (_("unable to fetch message limits\n"));
468 printf (_("------ Messages Limits --------\n"));
469 printf (_("max queues system wide = %d\n"), lim
.msgmni
);
470 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
471 _("max size of message"), lim
.msgmax
, "\n", 0);
472 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
473 _("default max size of queue"), lim
.msgmnb
, "\n", 0);
478 struct msginfo msginfo
;
479 if (msgctl (0, MSG_INFO
, (struct msqid_ds
*) (void *) &msginfo
) < 0) {
480 printf (_("kernel not configured for message queues\n"));
483 printf (_("------ Messages Status --------\n"));
484 #ifndef __FreeBSD_kernel__
485 printf (_("allocated queues = %d\n"), msginfo
.msgpool
);
486 printf (_("used headers = %d\n"), msginfo
.msgmap
);
488 ipc_print_size(unit
, _("used space"), msginfo
.msgtql
,
489 unit
== IPC_UNIT_DEFAULT
? _(" bytes\n") : "\n", 0);
493 printf (_("------ Message Queues Creators/Owners --------\n"));
494 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
495 _("msqid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
499 printf (_("------ Message Queues Send/Recv/Change Times --------\n"));
500 printf ("%-8s %-10s %-20s %-20s %-20s\n",
501 _("msqid"),_("owner"),_("send"),_("recv"),_("change"));
505 printf (_("------ Message Queues PIDs --------\n"));
506 printf ("%-10s %-10s %-10s %-10s\n",
507 _("msqid"),_("owner"),_("lspid"),_("lrpid"));
511 printf (_("------ Message Queues --------\n"));
512 printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
513 _("key"), _("msqid"), _("owner"), _("perms"),
514 unit
== IPC_UNIT_HUMAN
? _("size") : _("used-bytes"),
522 if (ipc_msg_get_info(-1, &msgds
) < 1)
525 for (msgdsp
= msgds
; msgdsp
->next
!= NULL
; msgdsp
= msgdsp
->next
) {
526 if (format
== CREATOR
) {
527 ipc_print_perms(stdout
, &msgdsp
->msg_perm
);
530 pw
= getpwuid(msgdsp
->msg_perm
.uid
);
534 printf ("%-8d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
536 printf ("%-8d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
537 printf (" %-20.16s", msgdsp
->q_stime
538 ? xctime(&msgdsp
->q_stime
) + 4 : _("Not set"));
539 printf (" %-20.16s", msgdsp
->q_rtime
540 ? xctime(&msgdsp
->q_rtime
) + 4 : _("Not set"));
541 printf (" %-20.16s\n", msgdsp
->q_ctime
542 ? xctime(&msgdsp
->q_ctime
) + 4 : _("Not set"));
546 printf ("%-8d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
548 printf ("%-8d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
549 printf (" %5d %5d\n",
550 msgdsp
->q_lspid
, msgdsp
->q_lrpid
);
554 printf( "0x%08x ",msgdsp
->msg_perm
.key
);
556 printf ("%-10d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
558 printf ("%-10d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
559 printf (" %-10o ", msgdsp
->msg_perm
.mode
& 0777);
561 if (unit
== IPC_UNIT_HUMAN
)
562 ipc_print_size(unit
, NULL
, msgdsp
->q_cbytes
, " ", 6);
564 ipc_print_size(unit
, NULL
, msgdsp
->q_cbytes
, NULL
, -12);
566 printf (" %-12ju\n", msgdsp
->q_qnum
);
571 ipc_msg_free_info(msgds
);
575 static void print_shm(int shmid
, int unit
)
577 struct shm_data
*shmdata
;
579 if (ipc_shm_get_info(shmid
, &shmdata
) < 1) {
580 warnx(_("id %d not found"), shmid
);
584 printf(_("\nShared memory Segment shmid=%d\n"), shmid
);
585 printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\n"),
586 shmdata
->shm_perm
.uid
, shmdata
->shm_perm
.gid
,
587 shmdata
->shm_perm
.cuid
, shmdata
->shm_perm
.cgid
);
588 printf(_("mode=%#o\taccess_perms=%#o\n"), shmdata
->shm_perm
.mode
,
589 shmdata
->shm_perm
.mode
& 0777);
590 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("size=") : _("bytes="),
591 shmdata
->shm_segsz
, "\t", 0);
592 printf(_("lpid=%u\tcpid=%u\tnattch=%jd\n"),
593 shmdata
->shm_lprid
, shmdata
->shm_cprid
,
594 shmdata
->shm_nattch
);
595 printf(_("att_time=%-26.24s\n"),
596 shmdata
->shm_atim
? xctime(&(shmdata
->shm_atim
)) : _("Not set"));
597 printf(_("det_time=%-26.24s\n"),
598 shmdata
->shm_dtim
? xctime(&shmdata
->shm_dtim
) : _("Not set"));
599 printf(_("change_time=%-26.24s\n"), xctime(&shmdata
->shm_ctim
));
602 ipc_shm_free_info(shmdata
);
605 static void print_msg(int msgid
, int unit
)
607 struct msg_data
*msgdata
;
609 if (ipc_msg_get_info(msgid
, &msgdata
) < 1) {
610 warnx(_("id %d not found"), msgid
);
614 printf(_("\nMessage Queue msqid=%d\n"), msgid
);
615 printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\tmode=%#o\n"),
616 msgdata
->msg_perm
.uid
, msgdata
->msg_perm
.gid
,
617 msgdata
->msg_perm
.cuid
, msgdata
->msg_perm
.cgid
,
618 msgdata
->msg_perm
.mode
);
619 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("csize=") : _("cbytes="),
620 msgdata
->q_cbytes
, "\t", 0);
621 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("qsize=") : _("qbytes="),
622 msgdata
->q_qbytes
, "\t", 0);
623 printf("qnum=%jd\tlspid=%d\tlrpid=%d\n",
625 msgdata
->q_lspid
, msgdata
->q_lrpid
);
626 printf(_("send_time=%-26.24s\n"),
627 msgdata
->q_stime
? xctime(&msgdata
->q_stime
) : _("Not set"));
628 printf(_("rcv_time=%-26.24s\n"),
629 msgdata
->q_rtime
? xctime(&msgdata
->q_rtime
) : _("Not set"));
630 printf(_("change_time=%-26.24s\n"),
631 msgdata
->q_ctime
? xctime(&msgdata
->q_ctime
) : _("Not set"));
634 ipc_msg_free_info(msgdata
);
637 static void print_sem(int semid
)
639 struct sem_data
*semdata
;
642 if (ipc_sem_get_info(semid
, &semdata
) < 1) {
643 warnx(_("id %d not found"), semid
);
647 printf(_("\nSemaphore Array semid=%d\n"), semid
);
648 printf(_("uid=%u\t gid=%u\t cuid=%u\t cgid=%u\n"),
649 semdata
->sem_perm
.uid
, semdata
->sem_perm
.gid
,
650 semdata
->sem_perm
.cuid
, semdata
->sem_perm
.cgid
);
651 printf(_("mode=%#o, access_perms=%#o\n"),
652 semdata
->sem_perm
.mode
, semdata
->sem_perm
.mode
& 0777);
653 printf(_("nsems = %ju\n"), semdata
->sem_nsems
);
654 printf(_("otime = %-26.24s\n"),
655 semdata
->sem_otime
? xctime(&semdata
->sem_otime
) : _("Not set"));
656 printf(_("ctime = %-26.24s\n"), xctime(&semdata
->sem_ctime
));
658 printf("%-10s %-10s %-10s %-10s %-10s\n",
659 _("semnum"), _("value"), _("ncount"), _("zcount"), _("pid"));
661 for (i
= 0; i
< semdata
->sem_nsems
; i
++) {
662 struct sem_elem
*e
= &semdata
->elements
[i
];
663 printf("%-10zu %-10d %-10d %-10d %-10d\n",
664 i
, e
->semval
, e
->ncount
, e
->zcount
, e
->pid
);
667 ipc_sem_free_info(semdata
);