From owner-svn-src-all@freebsd.org Thu May 7 16:56:19 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 602182DE281; Thu, 7 May 2020 16:56:19 +0000 (UTC) (envelope-from pstef@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) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 49J03R1yT2z4Psb; Thu, 7 May 2020 16:56:19 +0000 (UTC) (envelope-from pstef@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 3A2901C429; Thu, 7 May 2020 16:56:19 +0000 (UTC) (envelope-from pstef@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 047GuJYt033140; Thu, 7 May 2020 16:56:19 GMT (envelope-from pstef@FreeBSD.org) Received: (from pstef@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 047GuJl0033139; Thu, 7 May 2020 16:56:19 GMT (envelope-from pstef@FreeBSD.org) Message-Id: <202005071656.047GuJl0033139@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: pstef set sender to pstef@FreeBSD.org using -f From: Piotr Pawel Stefaniak Date: Thu, 7 May 2020 16:56:19 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r360786 - head/bin/ps X-SVN-Group: head X-SVN-Commit-Author: pstef X-SVN-Commit-Paths: head/bin/ps X-SVN-Commit-Revision: 360786 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 07 May 2020 16:56:19 -0000 Author: pstef Date: Thu May 7 16:56:18 2020 New Revision: 360786 URL: https://svnweb.freebsd.org/changeset/base/360786 Log: ps: extend the non-standard option -d (tree view) to work with -p Initially it seemed that there were multiple possible ways to do it. Processing option -p could conditionally add selected processes and their descendants to the list for further work, but it is not guaranteed to know whether the -d option has been used or not, and it also doesn't have access to the process list just yet. There is also descendant_sort() which has access to all possibly needed information, but serves the purely post-processing purpose of sorting output. Then there is the loop that uses invocation information and full process list to create a list of processes for final display. It seems the most natural place to implement this, but indeterminate state of the process list and volatility of the final list that is being created obstruct adding an elegant search for all elements of process descendancy trees. So I opted for adding another loop, just before the one I mentioned above. For all selected processes it conditionally adds direct descendants to the end of this list of selected processes. Possible usage: * ps -auxd -p $$ * ps -auxd -p 1 * while x=$(pgrep svnlite); do clear; ps auxd -p $x; sleep 2; done * ps -auxd -p `pgrep make` Reviewed by: kevans, kaktus (earlier version) MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D24380 Modified: head/bin/ps/ps.c Modified: head/bin/ps/ps.c ============================================================================== --- head/bin/ps/ps.c Thu May 7 16:40:33 2020 (r360785) +++ head/bin/ps/ps.c Thu May 7 16:56:18 2020 (r360786) @@ -506,7 +506,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 && !descendancy) { what = KERN_PROC_PID | showthreads; flag = *pidlist.l.pids; nselectors = 0; @@ -544,6 +544,14 @@ main(int argc, char *argv[]) if ((kp == NULL && errno != ESRCH) || (kp != NULL && nentries < 0)) xo_errx(1, "%s", kvm_geterr(kd)); nkept = 0; + if (descendancy) + for (elem = 0; elem < pidlist.count; elem++) + for (i = 0; i < nentries; i++) + 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; + } if (nentries > 0) { if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) xo_errx(1, "malloc failed");