From nobody Thu Aug 24 16:37:10 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RWpdf3FrSz4rLY4; Thu, 24 Aug 2023 16:37:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RWpdf2hmzz4S4y; Thu, 24 Aug 2023 16:37:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1692895030; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=0C3TXr8dWDIUjHs838+yfkwaYOaGdL56Hjsoo5dgLYY=; b=omR1eQHEDUgZ0cZjjUg4lOzbeYzvsBuZQShQFvh0JChnegFvDbdHyDqTwI/V/zl43Xo6dt 2DgcCdiuKFYY+jwRd6zeG98X7bnZq0/GqU+Rza1tLsGnjaBtMVAIT5BYrLQJD9o97JxzzM uSX+6HJ5Uxtqt6QWUzDVFK027ZvJ29HYsZ9x+FI7R/RiYRm7bVxl2MMdVUcmVh05L2awVo FqLFmeUf3FSpNYcwGjmzZLfSFGk+AMV/iqprq4chmvuvl+huak8QI1e/eTnAqxhM9uep+a Xmfxkl1VjeLOjLuMvVGke7Q7nJOeBj5Dw85lou+kfn+MgsuX357R+aZUuyVOxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1692895030; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=0C3TXr8dWDIUjHs838+yfkwaYOaGdL56Hjsoo5dgLYY=; b=ug8wF62CqdKd+mc9WSNQPa2QjRslzccH+K7er8IySJCSVvxLom7CIuUJEE7PDrzMqNFJ/1 U+S4Crvoh8ZJI+XsWJVbvXw0OqnheuapHTF6rwp8hvYHCGZ4Kw7iFot+ELYkoBixuYEHu1 wnc4gyIdnL9AmhnkcqJG1xUQkN2YBrQig0tm9tWo8bJu52kcPo4OwphmpTzg+wbShLbG9H cxd/drgtwvlsiG1eaeeFOTpIjs/sOH3Of+yCzWlP8l4aMBRQgnc4fA6kP/4UuhJgvHgEM+ 3PSAI4WMzUklucm9OlrXtjBSuH6vIfRDVroG9uqVLom+M+zjUSZNgdyPQv6sEw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1692895030; a=rsa-sha256; cv=none; b=DDtPmkfciyiIcHqsMnvTgtAv4SDrZwuOwIiLoFAT+I+xzuhxwDYx+fkl6GU9EUFqeCYSm/ zd4sW2M783VxXFomkm2ht2I9cpVvb0RgHkvrwUjLV4p0cuG0Ml6df3UltLtEI+rnAhjiI6 RR9cLNwW89azfvNWrp++irK8tYTQ2Rk+OS2gE9lFP09XHBlEkDOggDdr3xu0YAK9428fD0 xpNiBlgWM6+53mSRazA7nAmJeVqAuFsV+UxerTu5G1aTO90ZGFMcIl6P8Y6jlNMSJEsY2F HVPECy1pXJtI3xod1+LCHmKFhH9Ze01K0Ks4euO4jmxp5Pr1/DWr/cIdoG8ncQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RWpdf1lxvzZPv; Thu, 24 Aug 2023 16:37:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 37OGbAI5011168; Thu, 24 Aug 2023 16:37:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 37OGbAfl011165; Thu, 24 Aug 2023 16:37:10 GMT (envelope-from git) Date: Thu, 24 Aug 2023 16:37:10 GMT Message-Id: <202308241637.37OGbAfl011165@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Piotr Pawel Stefaniak Subject: git: 5c0a1c15ff8c - main - ps: add a new option -D to reimplement tree traversal List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: pstef X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 5c0a1c15ff8cb66128f4826ace8ba91e0a31486d Auto-Submitted: auto-generated The branch main has been updated by pstef: URL: https://cgit.FreeBSD.org/src/commit/?id=5c0a1c15ff8cb66128f4826ace8ba91e0a31486d commit 5c0a1c15ff8cb66128f4826ace8ba91e0a31486d Author: Piotr Pawel Stefaniak AuthorDate: 2023-08-24 16:29:46 +0000 Commit: Piotr Pawel Stefaniak CommitDate: 2023-08-24 16:35:56 +0000 ps: add a new option -D to reimplement tree traversal It takes a non-optional parameter string, one of "up", "down", or "both" that can request tree traversal in the chosen directions. This adds PIDs from the paths to the selection of PIDs and can be used together with -d to draw a subset of the process tree. Differential Revision: https://reviews.freebsd.org/D41231 --- bin/ps/ps.1 | 15 ++++++++++++++- bin/ps/ps.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/bin/ps/ps.1 b/bin/ps/ps.1 index 5fdecc4a3d4c..23868d8b3009 100644 --- a/bin/ps/ps.1 +++ b/bin/ps/ps.1 @@ -28,7 +28,7 @@ .\" .\" @(#)ps.1 8.3 (Berkeley) 4/18/94 .\" -.Dd April 25, 2023 +.Dd August 24, 2023 .Dt PS 1 .Os .Sh NAME @@ -39,6 +39,7 @@ .Op Fl -libxo .Op Fl aCcdefHhjlmrSTuvwXxZ .Op Fl O Ar fmt | Fl o Ar fmt +.Op Fl D Ar up | down | both .Op Fl G Ar gid Ns Op , Ns Ar gid Ns Ar ... .Op Fl J Ar jid Ns Op , Ns Ar jid Ns Ar ... .Op Fl M Ar core @@ -146,6 +147,18 @@ relative to each other. Note that this option has no effect if the .Dq command column is not the last column displayed. +.It Fl D +Expand the list of selected processes based on the process tree. +.Dq UP +will add the ancestor processes, +.Dq DOWN +will add the descendant processes, and +.Dq BOTH +will add both the ancestor and the descendant processes. +.Fl D +does not imply +.Fl d , +but works well with it. .It Fl e Display the environment as well. .It Fl f diff --git a/bin/ps/ps.c b/bin/ps/ps.c index 8554a2eaa0a2..3a7a0f54cca0 100644 --- a/bin/ps/ps.c +++ b/bin/ps/ps.c @@ -168,7 +168,7 @@ static char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz," "%cpu,%mem,command"; static char Zfmt[] = "label"; -#define PS_ARGS "AaCcde" OPT_LAZY_f "G:gHhjJ:LlM:mN:O:o:p:rSTt:U:uvwXxZ" +#define PS_ARGS "AaCcD:de" OPT_LAZY_f "G:gHhjJ:LlM:mN:O:o:p:rSTt:U:uvwXxZ" int main(int argc, char *argv[]) @@ -188,6 +188,8 @@ main(int argc, char *argv[]) int fwidthmin, fwidthmax; char errbuf[_POSIX2_LINE_MAX]; char fmtbuf[_POSIX2_LINE_MAX]; + enum { NONE = 0, UP = 1, DOWN = 2, BOTH = 1 | 2 } directions = NONE; + struct { int traversed; int initial; } pid_count; (void) setlocale(LC_ALL, ""); time(&now); /* Used by routines in print.c. */ @@ -262,6 +264,20 @@ main(int argc, char *argv[]) case 'c': cflag = 1; break; + case 'D': { + size_t len = strlen(optarg); + + if (len <= 2 && + strncasecmp(optarg, "up", len) == 0) + directions |= UP; + else if (len <= 4 && + strncasecmp(optarg, "down", len) == 0) + directions |= DOWN; + else if (len <= 4 && + strncasecmp(optarg, "both", len) == 0) + directions |= BOTH; + break; + } case 'd': descendancy = 1; break; @@ -502,7 +518,7 @@ main(int argc, char *argv[]) what = KERN_PROC_PGRP | showthreads; flag = *pgrplist.l.pids; nselectors = 0; - } else if (pidlist.count == 1) { + } else if (pidlist.count == 1 && directions == NONE) { what = KERN_PROC_PID | showthreads; flag = *pidlist.l.pids; nselectors = 0; @@ -537,6 +553,33 @@ main(int argc, char *argv[]) if ((kp == NULL && errno != ESRCH) || (kp != NULL && nentries < 0)) xo_errx(1, "%s", kvm_geterr(kd)); nkept = 0; + pid_count.initial = pidlist.count; + if (directions & DOWN) + for (elem = 0; elem < pidlist.count; elem++) + for (i = 0; i < nentries; i++) { + if (kp[i].ki_ppid == kp[i].ki_pid) + continue; + if (kp[i].ki_ppid == pidlist.l.pids[elem]) { + if (pidlist.count >= pidlist.maxcount) + expand_list(&pidlist); + pidlist.l.pids[pidlist.count++] = kp[i].ki_pid; + } + } + pid_count.traversed = pidlist.count; + if (directions & UP) + for (elem = 0; elem < pidlist.count; elem++) { + if (elem >= pid_count.initial && elem < pid_count.traversed) + continue; + for (i = 0; i < nentries; i++) { + if (kp[i].ki_ppid == kp[i].ki_pid) + continue; + if (kp[i].ki_pid == pidlist.l.pids[elem]) { + if (pidlist.count >= pidlist.maxcount) + expand_list(&pidlist); + pidlist.l.pids[pidlist.count++] = kp[i].ki_ppid; + } + } + } if (nentries > 0) { if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) xo_errx(1, "malloc failed"); @@ -1448,10 +1491,11 @@ usage(void) { #define SINGLE_OPTS "[-aCcde" OPT_LAZY_f "HhjlmrSTuvwXxZ]" - (void)xo_error("%s\n%s\n%s\n%s\n", + (void)xo_error("%s\n%s\n%s\n%s\n%s\n", "usage: ps [--libxo] " SINGLE_OPTS " [-O fmt | -o fmt]", " [-G gid[,gid...]] [-J jid[,jid...]] [-M core] [-N system]", " [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]", + " [-D up | down | both]", " ps [--libxo] -L"); exit(1); }