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.
This commit is contained in:
Silke Hofstra
2021-08-16 22:50:36 +02:00
committed by BenBE
parent 4374a267be
commit 696f79fe50
13 changed files with 71 additions and 29 deletions

View File

@ -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))