From 56692a6701c4847d0cd80457c5adb98e5eaa6ced Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sun, 16 Dec 2012 10:43:51 +0000 Subject: ipcs: add --human readable size conversion option Introduces new function ipc_print_size() which will call size_to_human_string(), and handles the occasional '([k]bytes)' printing if default size format is requested. Reviewed-by: Karel Zak Signed-off-by: Sami Kerola --- sys-utils/ipcs.c | 88 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 34 deletions(-) (limited to 'sys-utils/ipcs.c') diff --git a/sys-utils/ipcs.c b/sys-utils/ipcs.c index 889c86880..66c6302e3 100644 --- a/sys-utils/ipcs.c +++ b/sys-utils/ipcs.c @@ -33,14 +33,16 @@ enum output_formats { TIME, PID }; +enum { + OPT_HUMAN = CHAR_MAX + 1 +}; -static void do_shm (char format); -static void print_shm (int id); +static void do_shm (char format, int unit); +static void print_shm (int id, int unit); static void do_sem (char format); static void print_sem (int id); -static void do_msg (char format); - -void print_msg (int id); +static void do_msg (char format, int unit); +static void print_msg (int id, int unit); static void __attribute__ ((__noreturn__)) usage(FILE * out) { @@ -64,6 +66,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out) fputs(_(" -c, --creator show creator and owner\n"), out); fputs(_(" -l, --limits show resource limits\n"), out); fputs(_(" -u, --summary show status summary\n"), out); + fputs(_(" --human show sizes in human readable format\n"), out); fprintf(out, USAGE_MAN_TAIL("ipcs(1)")); exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); } @@ -72,6 +75,7 @@ int main (int argc, char **argv) { int opt, msg = 0, sem = 0, shm = 0, id=0, print=0; char format = NOTSPECIFIED; + int unit = IPC_UNIT_DEFAULT; static const struct option longopts[] = { {"id", required_argument, NULL, 'i'}, {"shmems", no_argument, NULL, 'm'}, @@ -83,6 +87,7 @@ int main (int argc, char **argv) {"creator", no_argument, NULL, 'c'}, {"limits", no_argument, NULL, 'l'}, {"summary", no_argument, NULL, 'u'}, + {"human", no_argument, NULL, OPT_HUMAN}, {"version", no_argument, NULL, 'V'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} @@ -127,6 +132,9 @@ int main (int argc, char **argv) case 'u': format = STATUS; break; + case OPT_HUMAN: + unit = IPC_UNIT_HUMAN; + break; case 'h': usage(stdout); case 'V': @@ -139,11 +147,11 @@ int main (int argc, char **argv) if (print) { if (shm) - print_shm (id); + print_shm (id, unit); if (sem) print_sem (id); if (msg) - print_msg (id); + print_msg (id, unit); if (!shm && !sem && !msg ) usage (stderr); } else { @@ -152,7 +160,7 @@ int main (int argc, char **argv) printf ("\n"); if (shm) { - do_shm (format); + do_shm (format, unit); printf ("\n"); } if (sem) { @@ -160,14 +168,14 @@ int main (int argc, char **argv) printf ("\n"); } if (msg) { - do_msg (format); + do_msg (format, unit); printf ("\n"); } } return EXIT_SUCCESS; } -static void do_shm (char format) +static void do_shm (char format, int unit) { struct passwd *pw; struct shm_data *shmds, *shmdsp; @@ -181,10 +189,13 @@ static void do_shm (char format) if (ipc_shm_get_limits(&lim)) return; printf (_("max number of segments = %ju\n"), lim.shmmni); - printf (_("max seg size (kbytes) = %ju\n"), lim.shmmax / 1024); - printf (_("max total shared memory (kbytes) = %ju\n"), - (lim.shmall / 1024) * getpagesize()); - printf (_("min seg size (bytes) = %ju\n"), lim.shmmin); + ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_KB : unit, + _("max seg size"), lim.shmmax, "\n", 0); + ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_KB : unit, + _("max total shared memory"), + lim.shmall * getpagesize(), "\n", 0); + ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_BYTES : unit, + _("min seg size"), lim.shmmin, "\n", 0); return; } case STATUS: @@ -248,7 +259,8 @@ static void do_shm (char format) default: printf (_("------ Shared Memory Segments --------\n")); printf ("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n", - _("key"),_("shmid"),_("owner"),_("perms"),_("bytes"), + _("key"),_("shmid"),_("owner"),_("perms"), + unit == IPC_UNIT_HUMAN ? _("size") : _("bytes"), _("nattch"),_("status")); break; } @@ -295,9 +307,9 @@ static void do_shm (char format) printf ("%-10d %-10.10s", shmdsp->shm_perm.id, pw->pw_name); else printf ("%-10d %-10u", shmdsp->shm_perm.id, shmdsp->shm_perm.uid); - printf (" %-10o %-10lu %-10ld %-6s %-6s\n", - shmdsp->shm_perm.mode & 0777, - shmdsp->shm_segsz, + printf (" %-10o ", shmdsp->shm_perm.mode & 0777); + ipc_print_size(unit, NULL, shmdsp->shm_segsz, NULL, -10); + printf (" %-10ld %-6s %-6s\n", shmdsp->shm_nattch, shmdsp->shm_perm.mode & SHM_DEST ? _("dest") : " ", shmdsp->shm_perm.mode & SHM_LOCKED ? _("locked") : " "); @@ -410,7 +422,7 @@ static void do_sem (char format) return; } -static void do_msg (char format) +static void do_msg (char format, int unit) { struct passwd *pw; struct msg_data *msgds, *msgdsp; @@ -424,8 +436,10 @@ static void do_msg (char format) return; printf (_("------ Messages Limits --------\n")); printf (_("max queues system wide = %d\n"), lim.msgmni); - printf (_("max size of message (bytes) = %zu\n"), lim.msgmax); - printf (_("default max size of queue (bytes) = %d\n"), lim.msgmnb); + ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_BYTES : unit, + _("max size of message"), lim.msgmax, "\n", 0); + ipc_print_size(unit == IPC_UNIT_DEFAULT ? IPC_UNIT_BYTES : unit, + _("default max size of queue"), lim.msgmnb, "\n", 0); return; } case STATUS: @@ -438,7 +452,8 @@ static void do_msg (char format) printf (_("------ Messages Status --------\n")); printf (_("allocated queues = %d\n"), msginfo.msgpool); printf (_("used headers = %d\n"), msginfo.msgmap); - printf (_("used space = %d bytes\n"), msginfo.msgtql); + ipc_print_size(unit, _("used space ="), msginfo.msgtql, + unit == IPC_UNIT_DEFAULT ? _(" bytes\n") : "\n", 0); return; } case CREATOR: @@ -463,7 +478,8 @@ static void do_msg (char format) printf (_("------ Message Queues --------\n")); printf ("%-10s %-10s %-10s %-10s %-12s %-12s\n", _("key"), _("msqid"), _("owner"), _("perms"), - _("used-bytes"), _("messages")); + unit == IPC_UNIT_HUMAN ? _("size") : _("used-bytes"), + _("messages")); break; } @@ -508,10 +524,9 @@ static void do_msg (char format) printf ("%-10d %-10.10s", msgdsp->msg_perm.id, pw->pw_name); else printf ("%-10d %-10u", msgdsp->msg_perm.id, msgdsp->msg_perm.uid); - printf (" %-10o %-12ld %-12ld\n", - msgdsp->msg_perm.mode & 0777, - msgdsp->q_cbytes, - msgdsp->q_qnum); + printf (" %-10o ", msgdsp->msg_perm.mode & 0777); + ipc_print_size(unit, NULL, msgdsp->q_cbytes, NULL, -12); + printf (" %-12ld\n", msgdsp->q_qnum); break; } } @@ -520,7 +535,7 @@ static void do_msg (char format) return; } -static void print_shm(int shmid) +static void print_shm(int shmid, int unit) { struct shm_data *shmdata; @@ -535,8 +550,10 @@ static void print_shm(int shmid) shmdata->shm_perm.cuid, shmdata->shm_perm.cgid); printf(_("mode=%#o\taccess_perms=%#o\n"), shmdata->shm_perm.mode, shmdata->shm_perm.mode & 0777); - printf(_("bytes=%ju\tlpid=%u\tcpid=%u\tnattch=%jd\n"), - shmdata->shm_segsz, shmdata->shm_lprid, shmdata->shm_cprid, + ipc_print_size(unit, unit == IPC_UNIT_HUMAN ? _("size=") : _("bytes="), + shmdata->shm_segsz, "\t", 0); + printf(_("lpid=%u\tcpid=%u\tnattch=%jd\n"), + shmdata->shm_lprid, shmdata->shm_cprid, shmdata->shm_nattch); printf(_("att_time=%-26.24s\n"), shmdata->shm_atim ? ctime(&(shmdata->shm_atim)) : _("Not set")); @@ -548,8 +565,7 @@ static void print_shm(int shmid) ipc_shm_free_info(shmdata); } - -void print_msg(int msgid) +void print_msg(int msgid, int unit) { struct msg_data *msgdata; @@ -563,8 +579,12 @@ void print_msg(int msgid) msgdata->msg_perm.uid, msgdata->msg_perm.uid, msgdata->msg_perm.cuid, msgdata->msg_perm.cgid, msgdata->msg_perm.mode); - printf(_("cbytes=%jd\tqbytes=%jd\tqnum=%jd\tlspid=%d\tlrpid=%d\n"), - msgdata->q_cbytes, msgdata->q_qbytes, msgdata->q_qnum, + ipc_print_size(unit, unit == IPC_UNIT_HUMAN ? _("csize=") : _("cbytes="), + msgdata->q_cbytes, "\t", 0); + ipc_print_size(unit, unit == IPC_UNIT_HUMAN ? _("qsize=") : _("qbytes="), + msgdata->q_qbytes, "\t", 0); + printf("qnum=%jd\tlspid=%d\tlrpid=%d\n", + msgdata->q_qnum, msgdata->q_lspid, msgdata->q_lrpid); printf(_("send_time=%-26.24s\n"), msgdata->q_stime ? ctime(&msgdata->q_stime) : _("Not set")); -- cgit v1.2.3-55-g7522