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 shm_info shm_info
;
214 maxid
= shmctl (0, SHM_INFO
, (struct shmid_ds
*) &shm_info
);
216 printf (_("kernel not configured for shared memory\n"));
220 printf (_("------ Shared Memory Status --------\n"));
222 * TRANSLATORS: This output format is maintained for backward
223 * compatibility as ipcs is used in scripts. For consistency
224 * with the rest, the translated form can follow this model:
226 * "segments allocated = %d\n"
227 * "pages allocated = %ld\n"
228 * "pages resident = %ld\n"
229 * "pages swapped = %ld\n"
230 * "swap performance = %ld attempts, %ld successes\n"
232 printf (_("segments allocated %d\n"
233 "pages allocated %ld\n"
234 "pages resident %ld\n"
235 "pages swapped %ld\n"
236 "Swap performance: %ld attempts\t %ld successes\n"),
241 shm_info
.swap_attempts
, shm_info
.swap_successes
);
249 printf (_("------ Shared Memory Segment Creators/Owners --------\n"));
250 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
251 _("shmid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
255 printf (_("------ Shared Memory Attach/Detach/Change Times --------\n"));
256 printf ("%-10s %-10s %-20s %-20s %-20s\n",
257 _("shmid"),_("owner"),_("attached"),_("detached"),
262 printf (_("------ Shared Memory Creator/Last-op PIDs --------\n"));
263 printf ("%-10s %-10s %-10s %-10s\n",
264 _("shmid"),_("owner"),_("cpid"),_("lpid"));
268 printf (_("------ Shared Memory Segments --------\n"));
269 printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
270 _("key"),_("shmid"),_("owner"),_("perms"),
271 unit
== IPC_UNIT_HUMAN
? _("size") : _("bytes"),
272 _("nattch"),_("status"));
279 if (ipc_shm_get_info(-1, &shmds
) < 1)
282 for (shmdsp
= shmds
; shmdsp
->next
!= NULL
; shmdsp
= shmdsp
->next
) {
283 if (format
== CREATOR
) {
284 ipc_print_perms(stdout
, &shmdsp
->shm_perm
);
287 pw
= getpwuid(shmdsp
->shm_perm
.uid
);
291 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
293 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
294 /* ctime uses static buffer: use separate calls */
295 printf(" %-20.16s", shmdsp
->shm_atim
296 ? xctime(&shmdsp
->shm_atim
) + 4 : _("Not set"));
297 printf(" %-20.16s", shmdsp
->shm_dtim
298 ? xctime(&shmdsp
->shm_dtim
) + 4 : _("Not set"));
299 printf(" %-20.16s\n", shmdsp
->shm_ctim
300 ? xctime(&shmdsp
->shm_ctim
) + 4 : _("Not set"));
304 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
306 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
307 printf (" %-10u %-10u\n",
308 shmdsp
->shm_cprid
, shmdsp
->shm_lprid
);
312 printf("0x%08x ", shmdsp
->shm_perm
.key
);
314 printf ("%-10d %-10.10s", shmdsp
->shm_perm
.id
, pw
->pw_name
);
316 printf ("%-10d %-10u", shmdsp
->shm_perm
.id
, shmdsp
->shm_perm
.uid
);
317 printf (" %-10o ", shmdsp
->shm_perm
.mode
& 0777);
319 if (unit
== IPC_UNIT_HUMAN
)
320 ipc_print_size(unit
, NULL
, shmdsp
->shm_segsz
, " ", 6);
322 ipc_print_size(unit
, NULL
, shmdsp
->shm_segsz
, NULL
, -10);
324 printf (" %-10ju %-6s %-6s\n",
326 shmdsp
->shm_perm
.mode
& SHM_DEST
? _("dest") : " ",
327 shmdsp
->shm_perm
.mode
& SHM_LOCKED
? _("locked") : " ");
332 ipc_shm_free_info(shmds
);
336 static void do_sem (char format
)
339 struct sem_data
*semds
, *semdsp
;
344 struct ipc_limits lim
;
346 printf (_("------ Semaphore Limits --------\n"));
347 if (ipc_sem_get_limits(&lim
))
349 printf (_("max number of arrays = %d\n"), lim
.semmni
);
350 printf (_("max semaphores per array = %d\n"), lim
.semmsl
);
351 printf (_("max semaphores system wide = %d\n"), lim
.semmns
);
352 printf (_("max ops per semop call = %d\n"), lim
.semopm
);
353 printf (_("semaphore max value = %d\n"), lim
.semvmx
);
358 struct seminfo seminfo
;
360 arg
.array
= (ushort
*) (void *) &seminfo
;
361 if (semctl (0, 0, SEM_INFO
, arg
) < 0) {
362 printf (_("kernel not configured for semaphores\n"));
365 printf (_("------ Semaphore Status --------\n"));
366 printf (_("used arrays = %d\n"), seminfo
.semusz
);
367 printf (_("allocated semaphores = %d\n"), seminfo
.semaem
);
372 printf (_("------ Semaphore Arrays Creators/Owners --------\n"));
373 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
374 _("semid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
378 printf (_("------ Semaphore Operation/Change Times --------\n"));
379 printf ("%-8s %-10s %-26.24s %-26.24s\n",
380 _("semid"),_("owner"),_("last-op"),_("last-changed"));
387 printf (_("------ Semaphore Arrays --------\n"));
388 printf ("%-10s %-10s %-10s %-10s %-10s\n",
389 _("key"),_("semid"),_("owner"),_("perms"),_("nsems"));
396 if (ipc_sem_get_info(-1, &semds
) < 1)
399 for (semdsp
= semds
; semdsp
->next
!= NULL
; semdsp
= semdsp
->next
) {
400 if (format
== CREATOR
) {
401 ipc_print_perms(stdout
, &semdsp
->sem_perm
);
404 pw
= getpwuid(semdsp
->sem_perm
.uid
);
408 printf ("%-8d %-10.10s", semdsp
->sem_perm
.id
, pw
->pw_name
);
410 printf ("%-8d %-10u", semdsp
->sem_perm
.id
, semdsp
->sem_perm
.uid
);
411 printf (" %-26.24s", semdsp
->sem_otime
412 ? xctime(&semdsp
->sem_otime
) : _("Not set"));
413 printf (" %-26.24s\n", semdsp
->sem_ctime
414 ? xctime( &semdsp
->sem_ctime
) : _("Not set"));
420 printf("0x%08x ", semdsp
->sem_perm
.key
);
422 printf ("%-10d %-10.10s", semdsp
->sem_perm
.id
, pw
->pw_name
);
424 printf ("%-10d %-10u", semdsp
->sem_perm
.id
, semdsp
->sem_perm
.uid
);
425 printf (" %-10o %-10ju\n",
426 semdsp
->sem_perm
.mode
& 0777,
432 ipc_sem_free_info(semds
);
436 static void do_msg (char format
, int unit
)
439 struct msg_data
*msgds
, *msgdsp
;
444 struct ipc_limits lim
;
446 if (ipc_msg_get_limits(&lim
))
448 printf (_("------ Messages Limits --------\n"));
449 printf (_("max queues system wide = %d\n"), lim
.msgmni
);
450 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
451 _("max size of message"), lim
.msgmax
, "\n", 0);
452 ipc_print_size(unit
== IPC_UNIT_DEFAULT
? IPC_UNIT_BYTES
: unit
,
453 _("default max size of queue"), lim
.msgmnb
, "\n", 0);
458 struct msginfo msginfo
;
459 if (msgctl (0, MSG_INFO
, (struct msqid_ds
*) (void *) &msginfo
) < 0) {
460 printf (_("kernel not configured for message queues\n"));
463 printf (_("------ Messages Status --------\n"));
464 printf (_("allocated queues = %d\n"), msginfo
.msgpool
);
465 printf (_("used headers = %d\n"), msginfo
.msgmap
);
466 ipc_print_size(unit
, _("used space"), msginfo
.msgtql
,
467 unit
== IPC_UNIT_DEFAULT
? _(" bytes\n") : "\n", 0);
471 printf (_("------ Message Queues Creators/Owners --------\n"));
472 printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
473 _("msqid"),_("perms"),_("cuid"),_("cgid"),_("uid"),_("gid"));
477 printf (_("------ Message Queues Send/Recv/Change Times --------\n"));
478 printf ("%-8s %-10s %-20s %-20s %-20s\n",
479 _("msqid"),_("owner"),_("send"),_("recv"),_("change"));
483 printf (_("------ Message Queues PIDs --------\n"));
484 printf ("%-10s %-10s %-10s %-10s\n",
485 _("msqid"),_("owner"),_("lspid"),_("lrpid"));
489 printf (_("------ Message Queues --------\n"));
490 printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n",
491 _("key"), _("msqid"), _("owner"), _("perms"),
492 unit
== IPC_UNIT_HUMAN
? _("size") : _("used-bytes"),
500 if (ipc_msg_get_info(-1, &msgds
) < 1)
503 for (msgdsp
= msgds
; msgdsp
->next
!= NULL
; msgdsp
= msgdsp
->next
) {
504 if (format
== CREATOR
) {
505 ipc_print_perms(stdout
, &msgdsp
->msg_perm
);
508 pw
= getpwuid(msgdsp
->msg_perm
.uid
);
512 printf ("%-8d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
514 printf ("%-8d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
515 printf (" %-20.16s", msgdsp
->q_stime
516 ? xctime(&msgdsp
->q_stime
) + 4 : _("Not set"));
517 printf (" %-20.16s", msgdsp
->q_rtime
518 ? xctime(&msgdsp
->q_rtime
) + 4 : _("Not set"));
519 printf (" %-20.16s\n", msgdsp
->q_ctime
520 ? xctime(&msgdsp
->q_ctime
) + 4 : _("Not set"));
524 printf ("%-8d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
526 printf ("%-8d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
527 printf (" %5d %5d\n",
528 msgdsp
->q_lspid
, msgdsp
->q_lrpid
);
532 printf( "0x%08x ",msgdsp
->msg_perm
.key
);
534 printf ("%-10d %-10.10s", msgdsp
->msg_perm
.id
, pw
->pw_name
);
536 printf ("%-10d %-10u", msgdsp
->msg_perm
.id
, msgdsp
->msg_perm
.uid
);
537 printf (" %-10o ", msgdsp
->msg_perm
.mode
& 0777);
539 if (unit
== IPC_UNIT_HUMAN
)
540 ipc_print_size(unit
, NULL
, msgdsp
->q_cbytes
, " ", 6);
542 ipc_print_size(unit
, NULL
, msgdsp
->q_cbytes
, NULL
, -12);
544 printf (" %-12ju\n", msgdsp
->q_qnum
);
549 ipc_msg_free_info(msgds
);
553 static void print_shm(int shmid
, int unit
)
555 struct shm_data
*shmdata
;
557 if (ipc_shm_get_info(shmid
, &shmdata
) < 1) {
558 warnx(_("id %d not found"), shmid
);
562 printf(_("\nShared memory Segment shmid=%d\n"), shmid
);
563 printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\n"),
564 shmdata
->shm_perm
.uid
, shmdata
->shm_perm
.uid
,
565 shmdata
->shm_perm
.cuid
, shmdata
->shm_perm
.cgid
);
566 printf(_("mode=%#o\taccess_perms=%#o\n"), shmdata
->shm_perm
.mode
,
567 shmdata
->shm_perm
.mode
& 0777);
568 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("size=") : _("bytes="),
569 shmdata
->shm_segsz
, "\t", 0);
570 printf(_("lpid=%u\tcpid=%u\tnattch=%jd\n"),
571 shmdata
->shm_lprid
, shmdata
->shm_cprid
,
572 shmdata
->shm_nattch
);
573 printf(_("att_time=%-26.24s\n"),
574 shmdata
->shm_atim
? xctime(&(shmdata
->shm_atim
)) : _("Not set"));
575 printf(_("det_time=%-26.24s\n"),
576 shmdata
->shm_dtim
? xctime(&shmdata
->shm_dtim
) : _("Not set"));
577 printf(_("change_time=%-26.24s\n"), xctime(&shmdata
->shm_ctim
));
580 ipc_shm_free_info(shmdata
);
583 void print_msg(int msgid
, int unit
)
585 struct msg_data
*msgdata
;
587 if (ipc_msg_get_info(msgid
, &msgdata
) < 1) {
588 warnx(_("id %d not found"), msgid
);
592 printf(_("\nMessage Queue msqid=%d\n"), msgid
);
593 printf(_("uid=%u\tgid=%u\tcuid=%u\tcgid=%u\tmode=%#o\n"),
594 msgdata
->msg_perm
.uid
, msgdata
->msg_perm
.uid
,
595 msgdata
->msg_perm
.cuid
, msgdata
->msg_perm
.cgid
,
596 msgdata
->msg_perm
.mode
);
597 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("csize=") : _("cbytes="),
598 msgdata
->q_cbytes
, "\t", 0);
599 ipc_print_size(unit
, unit
== IPC_UNIT_HUMAN
? _("qsize=") : _("qbytes="),
600 msgdata
->q_qbytes
, "\t", 0);
601 printf("qnum=%jd\tlspid=%d\tlrpid=%d\n",
603 msgdata
->q_lspid
, msgdata
->q_lrpid
);
604 printf(_("send_time=%-26.24s\n"),
605 msgdata
->q_stime
? xctime(&msgdata
->q_stime
) : _("Not set"));
606 printf(_("rcv_time=%-26.24s\n"),
607 msgdata
->q_rtime
? xctime(&msgdata
->q_rtime
) : _("Not set"));
608 printf(_("change_time=%-26.24s\n"),
609 msgdata
->q_ctime
? xctime(&msgdata
->q_ctime
) : _("Not set"));
612 ipc_msg_free_info(msgdata
);
615 static void print_sem(int semid
)
617 struct sem_data
*semdata
;
620 if (ipc_sem_get_info(semid
, &semdata
) < 1) {
621 warnx(_("id %d not found"), semid
);
625 printf(_("\nSemaphore Array semid=%d\n"), semid
);
626 printf(_("uid=%u\t gid=%u\t cuid=%u\t cgid=%u\n"),
627 semdata
->sem_perm
.uid
, semdata
->sem_perm
.uid
,
628 semdata
->sem_perm
.cuid
, semdata
->sem_perm
.cgid
);
629 printf(_("mode=%#o, access_perms=%#o\n"),
630 semdata
->sem_perm
.mode
, semdata
->sem_perm
.mode
& 0777);
631 printf(_("nsems = %ju\n"), semdata
->sem_nsems
);
632 printf(_("otime = %-26.24s\n"),
633 semdata
->sem_otime
? xctime(&semdata
->sem_otime
) : _("Not set"));
634 printf(_("ctime = %-26.24s\n"), xctime(&semdata
->sem_ctime
));
636 printf("%-10s %-10s %-10s %-10s %-10s\n",
637 _("semnum"), _("value"), _("ncount"), _("zcount"), _("pid"));
639 for (i
= 0; i
< semdata
->sem_nsems
; i
++) {
640 struct sem_elem
*e
= &semdata
->elements
[i
];
641 printf("%-10zd %-10d %-10d %-10d %-10d\n",
642 i
, e
->semval
, e
->ncount
, e
->zcount
, e
->pid
);
645 ipc_sem_free_info(semdata
);