From 696f79fe5099d510fc6ecc6d1e2f0ab3ae29e04e Mon Sep 17 00:00:00 2001 From: Silke Hofstra Date: Mon, 16 Aug 2021 22:50:36 +0200 Subject: Dynamically scale the ST_UID size to support 32-bit UIDs While most Unix-like systems use 16-bit user IDs, Linux supports 32-bit UIDs since version 2.6. UIDs above 65535 are used for UID namespacing of containers, where a container has its own set of 16-bit user IDs. Processes in such containers will have (much) larger UIDs than 65535. Because the current format strings for `ST_UID` and `USER` are `%5d` and `%9d` respectively, processes with such UIDs lead to misaligned columns. Dynamically scale the `ST_UID` column and increase the size of `USER` to 10 characters (length of UINT32_MAX) to ensure that the user ID always fits. Additionally: clean up how the titlebuffer size calculation and ensure the PID column has a minimum size of 5. --- Process.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'Process.c') diff --git a/Process.c b/Process.c index f0fc67fa..eb4ca820 100644 --- a/Process.c +++ b/Process.c @@ -41,17 +41,33 @@ static const char* const kthreadID = "KTHREAD"; static uid_t Process_getuid = (uid_t)-1; -int Process_pidDigits = 7; +int Process_pidDigits = PROCESS_MIN_PID_DIGITS; +int Process_uidDigits = PROCESS_MIN_UID_DIGITS; void Process_setupColumnWidths() { int maxPid = Platform_getMaxPid(); if (maxPid == -1) return; + if (maxPid < (int)pow(10, PROCESS_MIN_PID_DIGITS)) { + Process_pidDigits = PROCESS_MIN_PID_DIGITS; + return; + } + Process_pidDigits = ceil(log10(maxPid)); assert(Process_pidDigits <= PROCESS_MAX_PID_DIGITS); } +void Process_setUidColumnWidth(uid_t maxUid) { + if (maxUid < (uid_t)pow(10, PROCESS_MIN_UID_DIGITS)) { + Process_uidDigits = PROCESS_MIN_UID_DIGITS; + return; + } + + Process_uidDigits = ceil(log10(maxUid)); + assert(Process_uidDigits <= PROCESS_MAX_UID_DIGITS); +} + void Process_printBytes(RichString* str, unsigned long long number, bool coloring) { char buffer[16]; int len; @@ -885,7 +901,7 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field break; } break; - case ST_UID: xSnprintf(buffer, n, "%5d ", this->st_uid); break; + case ST_UID: xSnprintf(buffer, n, "%*d ", Process_uidDigits, this->st_uid); break; case TIME: Process_printTime(str, this->time, coloring); return; case TGID: if (this->tgid == this->pid) @@ -908,11 +924,11 @@ void Process_writeField(const Process* this, RichString* str, ProcessField field attr = CRT_colors[PROCESS_SHADOW]; if (this->user) { - Process_printLeftAlignedField(str, attr, this->user, 9); + Process_printLeftAlignedField(str, attr, this->user, 10); return; } - xSnprintf(buffer, n, "%-9d ", this->st_uid); + xSnprintf(buffer, n, "%-10d ", this->st_uid); break; default: if (DynamicColumn_writeField(this, str, field)) -- cgit v1.2.3