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
24 #include "closestream.h"
37 OPT_HUMAN
= CHAR_MAX
+ 1
40 static void do_shm (char format
, int unit
);
41 static void print_shm (int id
, int unit
);
42 static void do_sem (char format
);
43 static void print_sem (int id
);
44 static void do_msg (char format
, int unit
);
45 static void print_msg (int id
, int unit
);
47 /* we read time as int64_t from /proc, so cast... */
48 #define xctime(_x) ctime((time_t *) (_x))
50 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
52 fprintf(out
, USAGE_HEADER
);
53 fprintf(out
, " %s [resource ...] [output-format]\n", program_invocation_short_name
);
54 fprintf(out
, " %s [resource] -i <id>\n", program_invocation_short_name
);
55 fprintf(out
, USAGE_OPTIONS
);
56 fputs(_(" -i, --id <id> print details on resource identified by <id>\n"), out
);
57 fprintf(out
, USAGE_HELP
);
58 fprintf(out
, USAGE_VERSION
);
59 fprintf(out
, USAGE_SEPARATOR
);
60 fputs(_("Resource options:\n"), out
);
61 fputs(_(" -m, --shmems shared memory segments\n"), out
);
62 fputs(_(" -q, --queues message queues\n"), out
);
63 fputs(_(" -s, --semaphores semaphores\n"), out
);
64 fputs(_(" -a, --all all (default)\n"), out
);
65 fprintf(out
, USAGE_SEPARATOR
);
66 fputs(_("Output format:\n"), out
);
67 fputs(_(" -t, --time show attach, detach and change times\n"), out
);
68 fputs(_(" -p, --pid show PIDs of creator and last operator\n"), out
);
69 fputs(_(" -c, --creator show creator and owner\n"), out
);
70 fputs(_(" -l, --limits show resource limits\n"), out
);
71 fputs(_(" -u, --summary show status summary\n"), out
);
72 fputs(_(" --human show sizes in human-readable format\n"), out
);
73 fputs(_(" -b, --bytes show sizes in bytes\n"), out
);
74 fprintf(out
, USAGE_MAN_TAIL("ipcs(1)"));
75 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
78 int main (int argc
, char **argv
)
80 int opt
, msg
= 0, shm
= 0, sem
= 0, id
= 0, specific
= 0;
81 char format
= NOTSPECIFIED
;
82 int unit
= IPC_UNIT_DEFAULT
;
83 static const struct option longopts
[] = {
84 {"id", required_argument
, NULL
, 'i'},
85 {"queues", no_argument
, NULL
, 'q'},
86 {"shmems", no_argument
, NULL
, 'm'},
87 {"semaphores", no_argument
, NULL
, 's'},
88 {"all", no_argument
, NULL
, 'a'},
89 {"time", no_argument
, NULL
, 't'},
90 {"pid", no_argument
, NULL
, 'p'},
91 {"creator", no_argument
, NULL
, 'c'},
92 {"limits", no_argument
, NULL
, 'l'},
93 {"summary", no_argument
, NULL
, 'u'},
94 {"human", no_argument
, NULL
, OPT_HUMAN
},
95 {"bytes", no_argument
, NULL
, 'b'},
96 {"version", no_argument
, NULL
, 'V'},
97 {"help", no_argument
, NULL
, 'h'},
100 char options
[] = "i:qmsatpclubVh";
102 setlocale(LC_ALL
, "");
103 bindtextdomain(PACKAGE
, LOCALEDIR
);
105 atexit(close_stdout
);
107 while ((opt
= getopt_long(argc
, argv
, options
, longopts
, NULL
)) != -1) {
141 unit
= IPC_UNIT_HUMAN
;
144 unit
= IPC_UNIT_BYTES
;
149 printf(UTIL_LINUX_VERSION
);
156 if (specific
&& (msg
+ shm
+ sem
!= 1))
158 _("when using an ID, a single resource must be specified"));
161 print_msg (id
, unit
);
163 print_shm (id
, unit
);
167 if (!msg
&& !shm
&& !sem
)
171 do_msg (format
, unit
);
175 do_shm (format
, unit
);
186 static void do_shm (char format
, int unit
)
189 struct shm_data
*shmds
, *shmdsp
;
194 struct ipc_limits lim
;
196 printf (_("------ Shared Memory Limits --------\n"));
197 if (ipc_shm_get_limits(&lim
))
199 printf (_("max number of segments = %ju\n"), lim
.shmmni
);
200 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_KB
: unit
,
201 _("max seg size"), lim
.shmmax
, "\n", 0);
202 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_KB
: unit
,
203 _("max total shared memory"),
204 (uint64_t) lim
.shmall
* getpagesize(), "\n", 0);
205 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
206 _("min seg size"), lim
.shmmin
, "\n", 0);
212 struct shmid_ds shmbuf
;
213 struct shm_info
*shm_info
;
215 maxid
= shmctl (0, SHM_INFO
, &shmbuf
);
216 shm_info
= (struct shm_info
*) &shmbuf
;
218 printf (_("kernel not configured for shared memory\n"));
222 printf (_("------ Shared Memory Status --------\n"));
224 * TRANSLATORS: This output format is maintained for backward
225 * compatibility as ipcs is used in scripts. For consistency
226 * with the rest, the translated form can follow this model:
228 * "segments allocated = %d\n"
229 * "pages allocated = %ld\n"
230 * "pages resident = %ld\n"
231 * "pages swapped = %ld\n"
232 * "swap performance = %ld attempts, %ld successes\n"
234 printf (_("segments allocated %d\n"
235 "pages allocated %ld\n"
236 "pages resident %ld\n"
237 "pages swapped %ld\n"
238 "Swap performance: %ld attempts\t %ld successes\n"),
243 shm_info
->swap_attempts
, shm_info
->swap_successes
);
251 printf (_("------ Shared Memory Segment Creators/Owners --------\n"));
252 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
253 _("shmid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
257 printf (_("------ Shared Memory Attach/Detach/Change Times --------\n"));
258 printf ("%-10s %-10s %-20s %-20s %-20s\n",
259 _("shmid"),_("owner"),_("attached"),_("detached"),
264 printf (_("------ Shared Memory Creator/Last-op PIDs --------\n"));
265 printf ("%-10s %-10s %-10s %-10s\n",
266 _("shmid"),_("owner"),_("cpid"),_("lpid"));
270 printf (_("------ Shared Memory Segments --------\n"));
271 printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
272 _("key"),_("shmid"),_("owner"),_("perms"),
273 unit
== IPC_UNIT_HUMAN
? _("size") : _("bytes"),
274 _("nattch"),_("status"));
281 if (ipc_shm_get_info(-1, &shmds
) < 1)
284 for (shmdsp
= shmds
; shmdsp
->next
!= NULL
; shmdsp
= shmdsp
->next
) {
285 if (format
== CREATOR
) {
286 ipc_print_perms(stdout
, &shmdsp
->shm_perm
);
289 pw
= getpwuid(shmdsp
->shm_perm
.uid
);
293 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
295 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
296 /* ctime uses static buffer: use separate calls */
297 printf(" %-20.16s", shmdsp
->shm_atim
298 ? xctime(&shmdsp
->shm_atim
) + 4 : _("Not set"));
299 printf(" %-20.16s", shmdsp
->shm_dtim
300 ? xctime(&shmdsp
->shm_dtim
) + 4 : _("Not set"));
301 printf(" %-20.16s\n", shmdsp
->shm_ctim
302 ? xctime(&shmdsp
->shm_ctim
) + 4 : _("Not set"));
306 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
308 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
309 printf (" %-10u %-10u\n",
310 shmdsp
->shm_cprid
, shmdsp
->shm_lprid
);
314 printf("0x%08x ", shmdsp
->shm_perm
.key
);
316 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
318 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
319 printf (" %-10o ", shmdsp
->shm_perm
.mode
& 0777);
321 if (unit
== IPC_UNIT_HUMAN
)
322 ipc_print_size(unit
, NULL
, shmdsp
->shm_segsz
, " ", 6);
324 ipc_print_size(unit
, NULL
, shmdsp
->shm_segsz
, NULL
, -10);
326 printf (" %-10ju %-6s %-6s\n",
328 shmdsp
->shm_perm
.mode
& SHM_DEST
? _("dest") : " ",
329 shmdsp
->shm_perm
.mode
& SHM_LOCKED
? _("locked") : " ");
334 ipc_shm_free_info(shmds
);
338 static void do_sem (char format
)
341 struct sem_data
*semds
, *semdsp
;
346 struct ipc_limits lim
;
348 printf (_("------ Semaphore Limits --------\n"));
349 if (ipc_sem_get_limits(&lim
))
351 printf (_("max number of arrays = %d\n"), lim
.semmni
);
352 printf (_("max semaphores per array = %d\n"), lim
.semmsl
);
353 printf (_("max semaphores system wide = %d\n"), lim
.semmns
);
354 printf (_("max ops per semop call = %d\n"), lim
.semopm
);
355 printf (_("semaphore max value = %d\n"), lim
.semvmx
);
360 struct seminfo seminfo
;
362 arg
.array
= (ushort
*) (void *) &seminfo
;
363 if (semctl (0, 0, SEM_INFO
, arg
) < 0) {
364 printf (_("kernel not configured for semaphores\n"));
367 printf (_("------ Semaphore Status --------\n"));
368 printf (_("used arrays = %d\n"), seminfo
.semusz
);
369 printf (_("allocated semaphores = %d\n"), seminfo
.semaem
);
374 printf (_("------ Semaphore Arrays Creators/Owners --------\n"));
375 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
376 _("semid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
380 printf (_("------ Semaphore Operation/Change Times --------\n"));
381 printf ("%-8s %-10s %-26.24s %-26.24s\n",
382 _("semid"),_("owner"),_("last-op"),_("last-changed"));
389 printf (_("------ Semaphore Arrays --------\n"));
390 printf ("%-10s %-10s %-10s %-10s %-10s\n",
391 _("key"),_("semid"),_("owner"),_("perms"),_("nsems"));
398 if (ipc_sem_get_info(-1, &semds
) < 1)
401 for (semdsp
= semds
; semdsp
->next
!= NULL
; semdsp
= semdsp
->next
) {
402 if (format
== CREATOR
) {
403 ipc_print_perms(stdout
, &semdsp
->sem_perm
);
406 pw
= getpwuid(semdsp
->sem_perm
.uid
);
410 printf ("%-8d %-10.10s", semdsp
->sem_perm
.id
, pw
->pw_name
);
412 printf ("%-8d %-10u", semdsp
->sem_perm
.id
, semdsp
->sem_perm
.uid
);
413 printf (" %-26.24s", semdsp
->sem_otime
414 ? xctime(&semdsp
->sem_otime
) : _("Not set"));
415 printf (" %-26.24s\n", semdsp
->sem_ctime
416 ? xctime( &semdsp
->sem_ctime
) : _("Not set"));
422 printf("0x%08x ", semdsp
->sem_perm
.key
);
424 printf ("%-10d %-10.10s", semdsp
->sem_perm
.id
, pw
->pw_name
);
426 printf ("%-10d %-10u", semdsp
->sem_perm
.id
, semdsp
->sem_perm
.uid
);
427 printf (" %-10o %-10ju\n",
428 semdsp
->sem_perm
.mode
& 0777,
434 ipc_sem_free_info(semds
);
438 static void do_msg (char format
, int unit
)
441 struct msg_data
*msgds
, *msgdsp
;
446 struct ipc_limits lim
;
448 if (ipc_msg_get_limits(&lim
))
450 printf (_("------ Messages Limits --------\n"));
451 printf (_("max queues system wide = %d\n"), lim
.msgmni
);
452 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
453 _("max size of message"), lim
.msgmax
, "\n", 0);
454 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
455 _("default max size of queue"), lim
.msgmnb
, "\n", 0);
460 struct msginfo msginfo
;
461 if (msgctl (0, MSG_INFO
, (struct msqid_ds
*) (void *) &msginfo
) < 0) {
462 printf (_("kernel not configured for message queues\n"));
465 printf (_("------ Messages Status --------\n"));
466 #ifndef __FreeBSD_kernel__
467 printf (_("allocated queues = %d\n"), msginfo
.msgpool
);
468 printf (_("used headers = %d\n"), msginfo
.msgmap
);
470 ipc_print_size(unit
, _("used space"), msginfo
.msgtql
,
471 unit
== IPC_UNIT_DEFAULT
? _(" bytes\n") : "\n", 0);
475 printf (_("------ Message Queues Creators/Owners --------\n"));
476 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
477 _("msqid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
481 printf (_("------ Message Queues Send/Recv/Change Times --------\n"));
482 printf ("%-8s %-10s %-20s %-20s %-20s\n",
483 _("msqid"),_("owner"),_("send"),_("recv"),_("change"));
487 printf (_("------ Message Queues PIDs --------\n"));
488 printf ("%-10s %-10s %-10s %-10s\n",
489 _("msqid"),_("owner"),_("lspid"),_("lrpid"));
493 printf (_("------ Message Queues --------\n"));
494 printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
495 _("key"), _("msqid"), _("owner"), _("perms"),
496 unit
== IPC_UNIT_HUMAN
? _("size") : _("used-bytes"),
504 if (ipc_msg_get_info(-1, &msgds
) < 1)
507 for (msgdsp
= msgds
; msgdsp
->next
!= NULL
; msgdsp
= msgdsp
->next
) {
508 if (format
== CREATOR
) {
509 ipc_print_perms(stdout
, &msgdsp
->msg_perm
);
512 pw
= getpwuid(msgdsp
->msg_perm
.uid
);
516 printf ("%-8d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
518 printf ("%-8d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
519 printf (" %-20.16s", msgdsp
->q_stime
520 ? xctime(&msgdsp
->q_stime
) + 4 : _("Not set"));
521 printf (" %-20.16s", msgdsp
->q_rtime
522 ? xctime(&msgdsp
->q_rtime
) + 4 : _("Not set"));
523 printf (" %-20.16s\n", msgdsp
->q_ctime
524 ? xctime(&msgdsp
->q_ctime
) + 4 : _("Not set"));
528 printf ("%-8d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
530 printf ("%-8d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
531 printf (" %5d %5d\n",
532 msgdsp
->q_lspid
, msgdsp
->q_lrpid
);
536 printf( "0x%08x ",msgdsp
->msg_perm
.key
);
538 printf ("%-10d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
540 printf ("%-10d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
541 printf (" %-10o ", msgdsp
->msg_perm
.mode
& 0777);
543 if (unit
== IPC_UNIT_HUMAN
)
544 ipc_print_size(unit
, NULL
, msgdsp
->q_cbytes
, " ", 6);
546 ipc_print_size(unit
, NULL
, msgdsp
->q_cbytes
, NULL
, -12);
548 printf (" %-12ju\n", msgdsp
->q_qnum
);
553 ipc_msg_free_info(msgds
);
557 static void print_shm(int shmid
, int unit
)
559 struct shm_data
*shmdata
;
561 if (ipc_shm_get_info(shmid
, &shmdata
) < 1) {
562 warnx(_("id %d not found"), shmid
);
566 printf(_("\nShared memory Segment shmid=%d\n"), shmid
);
567 printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\n"),
568 shmdata
->shm_perm
.uid
, shmdata
->shm_perm
.uid
,
569 shmdata
->shm_perm
.cuid
, shmdata
->shm_perm
.cgid
);
570 printf(_("mode=%#o\taccess_perms=%#o\n"), shmdata
->shm_perm
.mode
,
571 shmdata
->shm_perm
.mode
& 0777);
572 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("size=") : _("bytes="),
573 shmdata
->shm_segsz
, "\t", 0);
574 printf(_("lpid=%u\tcpid=%u\tnattch=%jd\n"),
575 shmdata
->shm_lprid
, shmdata
->shm_cprid
,
576 shmdata
->shm_nattch
);
577 printf(_("att_time=%-26.24s\n"),
578 shmdata
->shm_atim
? xctime(&(shmdata
->shm_atim
)) : _("Not set"));
579 printf(_("det_time=%-26.24s\n"),
580 shmdata
->shm_dtim
? xctime(&shmdata
->shm_dtim
) : _("Not set"));
581 printf(_("change_time=%-26.24s\n"), xctime(&shmdata
->shm_ctim
));
584 ipc_shm_free_info(shmdata
);
587 void print_msg(int msgid
, int unit
)
589 struct msg_data
*msgdata
;
591 if (ipc_msg_get_info(msgid
, &msgdata
) < 1) {
592 warnx(_("id %d not found"), msgid
);
596 printf(_("\nMessage Queue msqid=%d\n"), msgid
);
597 printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\tmode=%#o\n"),
598 msgdata
->msg_perm
.uid
, msgdata
->msg_perm
.uid
,
599 msgdata
->msg_perm
.cuid
, msgdata
->msg_perm
.cgid
,
600 msgdata
->msg_perm
.mode
);
601 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("csize=") : _("cbytes="),
602 msgdata
->q_cbytes
, "\t", 0);
603 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("qsize=") : _("qbytes="),
604 msgdata
->q_qbytes
, "\t", 0);
605 printf("qnum=%jd\tlspid=%d\tlrpid=%d\n",
607 msgdata
->q_lspid
, msgdata
->q_lrpid
);
608 printf(_("send_time=%-26.24s\n"),
609 msgdata
->q_stime
? xctime(&msgdata
->q_stime
) : _("Not set"));
610 printf(_("rcv_time=%-26.24s\n"),
611 msgdata
->q_rtime
? xctime(&msgdata
->q_rtime
) : _("Not set"));
612 printf(_("change_time=%-26.24s\n"),
613 msgdata
->q_ctime
? xctime(&msgdata
->q_ctime
) : _("Not set"));
616 ipc_msg_free_info(msgdata
);
619 static void print_sem(int semid
)
621 struct sem_data
*semdata
;
624 if (ipc_sem_get_info(semid
, &semdata
) < 1) {
625 warnx(_("id %d not found"), semid
);
629 printf(_("\nSemaphore Array semid=%d\n"), semid
);
630 printf(_("uid=%u\t gid=%u\t cuid=%u\t cgid=%u\n"),
631 semdata
->sem_perm
.uid
, semdata
->sem_perm
.uid
,
632 semdata
->sem_perm
.cuid
, semdata
->sem_perm
.cgid
);
633 printf(_("mode=%#o, access_perms=%#o\n"),
634 semdata
->sem_perm
.mode
, semdata
->sem_perm
.mode
& 0777);
635 printf(_("nsems = %ju\n"), semdata
->sem_nsems
);
636 printf(_("otime = %-26.24s\n"),
637 semdata
->sem_otime
? xctime(&semdata
->sem_otime
) : _("Not set"));
638 printf(_("ctime = %-26.24s\n"), xctime(&semdata
->sem_ctime
));
640 printf("%-10s %-10s %-10s %-10s %-10s\n",
641 _("semnum"), _("value"), _("ncount"), _("zcount"), _("pid"));
643 for (i
= 0; i
< semdata
->sem_nsems
; i
++) {
644 struct sem_elem
*e
= &semdata
->elements
[i
];
645 printf("%-10zd %-10d %-10d %-10d %-10d\n",
646 i
, e
->semval
, e
->ncount
, e
->zcount
, e
->pid
);
649 ipc_sem_free_info(semdata
);