diff options
author | Silke Hofstra <silke@slxh.eu> | 2021-08-16 22:50:36 +0200 |
---|---|---|
committer | BenBE <BenBE@geshi.org> | 2021-10-27 21:20:59 +0200 |
commit | 696f79fe5099d510fc6ecc6d1e2f0ab3ae29e04e (patch) | |
tree | 2c9fa20e121b5c4b6cb90072fa016e5a642ddb65 /Process.c | |
parent | 4374a267bec655e704faaa379d1ed62eca90b71a (diff) |
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.
Diffstat (limited to 'Process.c')
-rw-r--r-- | Process.c | 24 |
1 files changed, 20 insertions, 4 deletions
@@ -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)) |