From c7f893a42b9572f1152d163290202b855953a768 Mon Sep 17 00:00:00 2001 From: Mark Johnston Date: Tue, 28 Jul 2020 15:26:19 +0000 Subject: [PATCH] ps(1): Fix formatting of the "command" field for kernel threads. When -H is specified, for kernel threads the command is formatted as "/" and truncated to MAXCOMLEN. But each of the proc name and td name may be up to MAXCOMLEN bytes in length. Also handle the ki_moretdname field to ensure that the full thread name gets printed. This is already handled correctly when formatting for "-o tdname". Reported by: freqlabs Reviewed by: freqlabs MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D25840 --- bin/ps/ps.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/bin/ps/ps.c b/bin/ps/ps.c index f6d32e5411fe..d3cfc669d581 100644 --- a/bin/ps/ps.c +++ b/bin/ps/ps.c @@ -1264,6 +1264,7 @@ fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki, static void saveuser(KINFO *ki) { + char tdname[COMMLEN + 1]; char *argsp; if (ki->ki_p->ki_flag & P_INMEM) { @@ -1280,12 +1281,14 @@ saveuser(KINFO *ki) * save arguments if needed */ if (needcomm) { - if (ki->ki_p->ki_stat == SZOMB) + if (ki->ki_p->ki_stat == SZOMB) { ki->ki_args = strdup(""); - else if (UREADOK(ki) || (ki->ki_p->ki_args != NULL)) + } else if (UREADOK(ki) || (ki->ki_p->ki_args != NULL)) { + (void)snprintf(tdname, sizeof(tdname), "%s%s", + ki->ki_p->ki_tdname, ki->ki_p->ki_moretdname); ki->ki_args = fmt(kvm_getargv, ki, - ki->ki_p->ki_comm, ki->ki_p->ki_tdname, MAXCOMLEN); - else { + ki->ki_p->ki_comm, tdname, COMMLEN * 2 + 1); + } else { asprintf(&argsp, "(%s)", ki->ki_p->ki_comm); ki->ki_args = argsp; }