History log of /freebsd-current/bin/ps/ps.c
Revision Date Author Comments
# 3f0b80bc 23-Apr-2024 Jamie Landeg-Jones <jamie@catflap.org>

ps: handle incorrect -D values

I just noticed a slight issue with the '-D' option. Basically,
I accidentally typed something along the lines of:

ps -Dboth-p303

I.E. missing out the "space". Instead of giving an error, it behaved as
if I'd just typed "ps".

Looking at bin/ps/ps.c, where the -D option is parsed, it doesn't
error-out if there is no valid match.

This commit fixes the bug.


# 0b8224d1 24-Nov-2023 Warner Losh <imp@FreeBSD.org>

Remove copyright strings ifdef'd out

We've ifdef'd out the copyright strings for some time now. Go ahead and
remove the ifdefs. Plus whatever other detritis was left over from other
recent removals. These copyright strings are present in the comments and
are largely from CSRG's attempt at adding their copyright to every
binary file (which modern interpretations of the license doesn't
require).

Sponsored by: Netflix


# 90aea514 23-Nov-2023 Warner Losh <imp@FreeBSD.org>

bin: Remove ancient SCCS tags.

Remove ancient SCCS tags from the tree, automated scripting, with two
minor fixup to keep things compiling. All the common forms in the tree
were removed with a perl script.

Sponsored by: Netflix


# 1818f3fd 21-Nov-2023 Brooks Davis <brooks@FreeBSD.org>

ps: s/kern.max_pid/kern.pid_max/ in a comment

Sponsored by: DARPA


# 5c0a1c15 24-Aug-2023 Piotr Pawel Stefaniak <pstef@FreeBSD.org>

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


# f06e32e9 29-Jul-2023 Piotr Pawel Stefaniak <pstef@FreeBSD.org>

Revert "ps: extend the non-standard option -d (tree view) to work with -p"

This reverts commit ca8c0d5e811048ad67d0955642c5b486e9c0f3d2.

By commiting ca8c0d5e8110 I was hoping that the existing option -d
could just be extended to work with -p to implement a feature that was
and I think is still needed, that is to show all descendant processes
of a given process id or a set of process ids.

After a complaint from -current which may represent a wider
dissatisfaction with this change in the program's behavior, I think it
will be better to revert ca8c0d5e8110 and reintroduce this feature
using a separate option -D.


# 1d386b48 16-Aug-2023 Warner Losh <imp@FreeBSD.org>

Remove $FreeBSD$: one-line .c pattern

Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/


# 820ac126 25-Apr-2023 Mateusz Piotrowski <0mp@FreeBSD.org>

ps: Add libxo to usage message

MFC after: 1 week
Sponsored by: Klara Inc.


# 3f46bf40 25-Apr-2023 Mateusz Piotrowski <0mp@FreeBSD.org>

ps: Fix synopsis

In the -L mode, the -L flag is not optional.

MFC after: 3 days
Sponsored by: Klara Inc.


# 3322d1c0 03-Oct-2021 Alfonso <gfunni234@gmail.com>

ps(1): Use calloc instead of malloc and memset.

Pull Request: https://github.com/freebsd/freebsd-src/pull/546
MFC after: 1 week
Sponsored by: NVIDIA Networking


# bf27a225 24-Sep-2021 Math Ieu <sigsys@gmail.com>

ps: fix `ps -aa`

Passing the -a flag multiple times made ps show no processes.

Differential Revision: https://reviews.freebsd.org/D27215


# c7f893a4 28-Jul-2020 Mark Johnston <markj@FreeBSD.org>

ps(1): Fix formatting of the "command" field for kernel threads.

When -H is specified, for kernel threads the command is formatted as
"<proc name>/<td name>" 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


# ac4b8a1c 27-Jun-2020 Piotr Pawel Stefaniak <pstef@FreeBSD.org>

ps(1): don't try to handle non-SMP systems

As reported by kib, sysctl machdep.smp_active doesn't exist and on UP we
return CPU 0 for all threads anyway.

Reported by: kib


# 4f47f511 27-Jun-2020 Piotr Pawel Stefaniak <pstef@FreeBSD.org>

ps(1): reuse keyword "cpu" to show CPU number

This flag will now show the processor number on which a process is running.

This change was inspired by PR129965. Initially I didn't think that the
patch attached to it was correct -- it sacrificed ki_estcpu use in "cpu"
for ki_lastcpu and I thought that the old functionality should be kept and
the new (cpu#) one added to it. But I've since discovered that ki_estcpu is
sched_4bsd-specific. What's worse, it represents the same thing as
ki_pctcpu, except ki_pctcpu is universal -- so "%cpu" has been using it
successfully. Therefore, I've decided to replace information based on
ki_estcpu with information based on ki_oncpu/ki_lastcpu.

Key parts of the code and manual changes were borrowed from top(1).

PR: 129965
Reported by: Nikola Knežević
MFC after: 1 week
Differential Revision: https://reviews.freebsd.org/D25377


# 26161fcf 09-Jun-2020 Yuri Pankov <yuripv@FreeBSD.org>

ps: remove xo_no_setlocale() call

Apparently libxo was fixed to do the right thing on FreeBSD,
and calling xo_no_setlocale() is no longer needed.

Reported by: phil


# aa8ab146 07-Jun-2020 Yuri Pankov <yuripv@FreeBSD.org>

ps: use %hs instead of %s format specifiers

Use %hs (locale-based encoding) instead of %s (UTF-8) format for
strings that are expected to be in current locale encoding (date/time,
process names/argument list).

PR: 241491
Reviewed by: phil
Differential Revision: https://reviews.freebsd.org/D22160


# ca8c0d5e 07-May-2020 Piotr Pawel Stefaniak <pstef@FreeBSD.org>

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


# c6a5ff71 12-Jun-2018 Eitan Adler <eadler@FreeBSD.org>

ps(1): fix some nits

- fracmem and mempages are double. ki_rssize should be too
- remove default case that is fully covered by all existing cases
- mark usage as dead


# 30417f2c 09-Mar-2018 Mike Karels <karels@FreeBSD.org>

Change ps(1) output width to unlimited if not interactive

Apply patch submitted with PR 217159 to make ps use unlimited
width when not associated with a terminal (i.e., none of stdout, stdin,
or stderr is a tty). Update comments and man page correspondingly.
This change was requested to work around lack of -ww in scripts from
third-party packages, including Hadoop, and adds a small measure of
Linux compatibility. Hopefully few if any non-interactive scripts
depend on the old default of 79.

PR: 217159
Submitted by: n.deepak at gmail.com
Reviewed by: vangyzen jhb
Differential Revision: https://reviews.freebsd.org/D14614


# b93bb974 27-Feb-2018 Mike Karels <karels@FreeBSD.org>

Revert r314685 in ps

Revert r314685, and add a comment describing the original
behavior and the intent.

Reviewed by: dab@ vangyzen@ jhb@
Differential Revision: https://reviews.freebsd.org/D14530


# 8a16b7a1 20-Nov-2017 Pedro F. Giffuni <pfg@FreeBSD.org>

General further adoption of SPDX licensing ID tags.

Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.


# d7026b96 06-Oct-2017 Edward Tomasz Napierala <trasz@FreeBSD.org>

Fix kvm_getprocs(3) error reporting in ps(1).

Previously it just didn't work at all - kvm_getprocs(3) doesn't update
the &nentries when it returns NULL. The end result was that ps(1) showed
garbage data instead of reporting kinfo_proc size mismatch.

Reviewed by: cem
Obtained from: CheriBSD
MFC after: 2 weeks
Sponsored by: DARPA, AFRL
Differential Revision: https://reviews.freebsd.org/D12414


# ef1d40da 06-Mar-2017 Conrad Meyer <cem@FreeBSD.org>

ps(1): Fix -w + UNLIMITED handling

A follow-up fix for r314685.

Because the -w flag is parsed after ps(1) infers termwidth from COLUMNS and
stdout, and UNLIMITED happens to be the zero value, the single -w flag in
combination with a non-terminal stdout or COLUMNS=0 could result in output
truncated at 131 characters. (Despite the output being unlimited without
-w.)

Obviously, adding more -w shouldn't truncate output lines.

The committed patch is from bdrewery@, and I've reviewed and tested it.

Submitted by: bdrewery@
Reported by: bdrewery@
Sponsored by: Dell EMC Isilon


# 9c4241c3 04-Mar-2017 Conrad Meyer <cem@FreeBSD.org>

ps(1): Only detect terminal width if stdout is a tty

If stdout isn't a tty, use unlimited width output rather than truncating to
79 characters. This is helpful for shell scripts or e.g., 'ps | grep foo'.

This hardcoded width has some history: In The Beginning of History[0], the
width of ps was hardcoded as 80 bytes. In 1985, Bloom@ added detection
using TIOCGWINSZ on stdin.[1] In 1986, Kirk merged a change to check
stdout's window size instead. In 1990, the fallback checks to stderr and
stdin's TIOCGWINSZ were added by Marc@, with the commit message "new
version."[2]

OS X Darwin has a very similar modification to ps(1), which simply sets
UNLIMITED for all non-tty outputs.[3] I've chosen to respect COLUMNS
instead of behaving identically to Darwin here, but I don't feel strongly
about that. We could match OS X for parity if that is desired.

[0]: https://svnweb.freebsd.org/csrg/bin/ps/ps.c?annotate=1065
[1]: https://svnweb.freebsd.org/csrg/bin/ps/ps.c?r1=18105&r2=18106
[2]:
https://svnweb.freebsd.org/csrg/bin/ps/ps.c?r1=40675&r2=40674&pathrev=40675
[3]:
https://opensource.apple.com/source/adv_cmds/adv_cmds-168/ps/ps.c.auto.html

PR: 217159
Reported by: Deepak Nagaraj <n.deepak at gmail.com>


# fbbd9655 28-Feb-2017 Warner Losh <imp@FreeBSD.org>

Renumber copyright clause 4

Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by: Jan Schaumann <jschauma@stevens.edu>
Pull Request: https://github.com/freebsd/freebsd/pull/96


# 81fc45fc 23-Jul-2016 Konstantin Belousov <kib@FreeBSD.org>

Addm missed required call to xo_finish() when only header is printed.

Reported by: pho
Sponsored by: The FreeBSD Foundation
MFC after: 1 week


# 5912ca59 24-May-2016 Don Lewis <truckman@FreeBSD.org>

Fix CID 1011370 (Resource leak) in ps.

There is no need to to call strdup() on the value returned by fmt().
The latter calls fmt_argv() which always returns a dynamically
allocated string, and calling strdup() on that leaks the memory
allocated by fmt_argv(). Wave some const magic on ki_args and
ki_env to make the direct assignment happy. This requires a tweak
to the asprintf() case to avoid a const vs. non-const mismatch.

Reported by: Coverity
CID: 1011370
MFC after: 1 week


# ec23a763 18-Apr-2016 Marcelo Araujo <araujo@FreeBSD.org>

Use NULL instead of 0 for pointers.

kvm_open(3) will return NULL when it cannot access kernel virtual memory.

MFC after: 2 weeks.


# 16eb3576 01-Jun-2015 Marcelo Araujo <araujo@FreeBSD.org>

Remove unused variable, this variable fmtstr was introduced at revision r225868
and it is not used anymore after the convertion to use libxo at revision r283304.

Differential Revision: D2678
Reviewed by: marcel


# 8beb1a2f 22-May-2015 Marcel Moolenaar <marcel@FreeBSD.org>

Convert to use libxo. Document use of libxo as well.

Obtained from: Phil Shafer <phil@juniper.net>
Sponsored by: Juniper Networks, Inc.


# 484f5781 31-Jan-2015 Pedro F. Giffuni <pfg@FreeBSD.org>

Prevent access to an uninitialized variable

The "-h" option may access an uninitialized value. Prevent it
by properly initializing the value.

CID: 1006559


# 13767130 02-May-2014 Bryan Drewery <bdrewery@FreeBSD.org>

Add -J to filter by matching jail IDs and names.

-J 0 can be used to show only host processes.

Patch partially based on work by bz@

PR: bin/78763
MFC after: 2 weeks
Relnotes: yes


# 9f0b6e5e 18-Jan-2013 John Baldwin <jhb@FreeBSD.org>

- Move 'showthreads' check out of fmt.c.
- Update shadow copy of fmt_argv() prototype in w.c and fix calls for
additional parameter.


# 1c67ef12 18-Jan-2013 John Baldwin <jhb@FreeBSD.org>

Include the thread name along with the command name when displaying the
command name of a thread from a multi-threaded process that doesn't have
an available argument list (such as kernel processes) and threads display
is enabled via -H.

Reviewed by: alfred, delphij, eric@vangyzen.net
MFC after: 1 week


# 64b0bf0b 12-Dec-2012 Pawel Jakub Dawidek <pjd@FreeBSD.org>

Use kern.max_pid sysctl to obtain maximum PID number instead of using local
define.

Reviewed by: jhb


# 42856668 29-Aug-2012 Ed Maste <emaste@FreeBSD.org>

Avoid passing uninitialized stack to addelem() if called with an empty arg.

PR: bin/171174


# 38494eff 07-Jan-2012 Ulrich Spörlein <uqs@FreeBSD.org>

Spelling fixes for bin/


# 57c87bfa 22-Nov-2011 Mikolaj Golub <trociny@FreeBSD.org>

No need in procfs(5).

MFC after: 2 weeks


# 1d1143ec 29-Sep-2011 Edward Tomasz Napierala <trasz@FreeBSD.org>

Make ps(1) automatically size its column widths.


# bf2fe08e 22-May-2011 Ulrich Spörlein <uqs@FreeBSD.org>

Fix some typos under bin/

Found by: codespell


# 649fde88 12-Apr-2011 Edward Tomasz Napierala <trasz@FreeBSD.org>

Get rid of DSIZ; instead just call the sizing function if provided.


# a7d5f7eb 19-Oct-2010 Jamie Gritton <jamie@FreeBSD.org>

A new jail(8) with a configuration file, to replace the work currently done
by /etc/rc.d/jail.


# fe0506d7 09-Mar-2010 Marcel Moolenaar <marcel@FreeBSD.org>

Create the altix project branch. The altix project will add support
for the SGI Altix 350 to FreeBSD/ia64. The hardware used for porting
is a two-module system, consisting of a base compute module and a
CPU expansion module. SGI's NUMAFlex architecture can be an excellent
platform to test CPU affinity and NUMA-aware features in FreeBSD.


# b9d3fd67 18-Feb-2010 Rebecca Cran <brucec@FreeBSD.org>

MFC r203688:

Initialize the execfile argument to NULL instead of _PATH_DEVNULL. This allows
the -M option to be used without specifying -N.

PR: bin/138146
Approved by: rrs (mentor)


# c08dcaf1 08-Feb-2010 Rebecca Cran <brucec@FreeBSD.org>

Initialize the execfile argument to NULL instead of _PATH_DEVNULL. This allows the -M option to be used without specifying -N.

PR: bin/138146
Approved by: rrs (mentor)
MFC after: 3 days


# aa14b5ab 23-Jul-2009 Brian Somers <brian@FreeBSD.org>

Add the -d switch to the usage message.

Submitted by: Emil Mikulic - emil at dmr dot ath dot cx
Approved by: re (kib)
MFC after: 1 week


# 8ba7efda 17-May-2009 Brian Somers <brian@FreeBSD.org>

Remove redundant whitespace


# 044fce53 16-May-2009 Brian Somers <brian@FreeBSD.org>

Add a -d option to ps to display descendant info with the output.
This is similar to linux's -H (or -f) switch.

MFC after: 3 weeks


# d7f03759 19-Oct-2008 Ulf Lilleengen <lulf@FreeBSD.org>

- Import the HEAD csup code which is the basis for the cvsmode work.


# 0c025af9 18-Jul-2008 Kevin Lo <kevlo@FreeBSD.org>

Remove unnessasary cast


# 8a529f59 08-Nov-2007 John Baldwin <jhb@FreeBSD.org>

Teach ps(1) to parse pts TT values (i.e. '0', '1') for the -t flag.

MFC after: 1 week
Reported by: kris


# 7ab24ea3 26-Oct-2007 Julian Elischer <julian@FreeBSD.org>

Introduce a way to make pure kernal threads.
kthread_add() takes the same parameters as the old kthread_create()
plus a pointer to a process structure, and adds a kernel thread
to that process.

kproc_kthread_add() takes the parameters for kthread_add,
plus a process name and a pointer to a pointer to a process instead of just
a pointer, and if the proc * is NULL, it creates the process to the
specifications required, before adding the thread to it.

All other old kthread_xxx() calls return, but act on (struct thread *)
instead of (struct proc *). One reason to change the name is so that
any old kernel modules that are lying around and expect kthread_create()
to make a process will not just accidentally link.

fix top to show kernel threads by their thread name in -SH mode
add a tdnam formatting option to ps to show thread names.

make all idle threads actual kthreads and put them into their own idled process.
make all interrupt threads kthreads and put them in an interd process
(mainly for aesthetic and accounting reasons)
rename proc 0 to be 'kernel' and it's swapper thread is now 'swapper'

man page fixes to follow.


# b61ce5b0 16-Sep-2007 Jeff Roberson <jeff@FreeBSD.org>

- Move all of the PS_ flags into either p_flag or td_flags.
- p_sflag was mostly protected by PROC_LOCK rather than the PROC_SLOCK or
previously the sched_lock. These bugs have existed for some time.
- Allow swapout to try each thread in a process individually and then
swapin the whole process if any of these fail. This allows us to move
most scheduler related swap flags into td_flags.
- Keep ki_sflag for backwards compat but change all in source tools to
use the new and more correct location of P_INMEM.

Reported by: pho
Reviewed by: attilio, kib
Approved by: re (kensmith)


# a89237ae 09-Feb-2005 Ruslan Ermilov <ru@FreeBSD.org>

Sync program's usage() with manpage's SYNOPSIS.


# d8654c64 19-Nov-2004 Christian S.J. Peron <csjp@FreeBSD.org>

Use statfs instead of getmntinfo(). This will make the procfs checks
play nicer in prisons. It also simplifies things.

Reviewed by: rwatson
Bumped into by: Jilles Tjoelker


# a951d1f8 14-Nov-2004 Christian S.J. Peron <csjp@FreeBSD.org>

Currently if the user specifies -e and procfs is not mounted on /proc,
printing of the process environment will fail silently.

-define a function which will check to see if procfs is mounted on /proc
-Implement this test if the user specified -e
-If procfs is not mounted on /proc and -e was specified, print a warning.
informing the user that procfs(5) is required.

Reviewed by: wes, rwatson


# 832b0b55 13-Nov-2004 Ruslan Ermilov <ru@FreeBSD.org>

Removed bitrot.


# f2fbfae6 27-Jun-2004 Garance A Drosehn <gad@FreeBSD.org>

Improve checking for `ps -t <dev>', and give better error messages when
an invalid <dev> is specified. Aside: It turns out that the S_ISCHR()
check is true for almost every device that we have (not just tty's).


# bd6233fd 23-Jun-2004 Garance A Drosehn <gad@FreeBSD.org>

Fix a bug I introduced by some last-minute changes in -r 1.102. I ended
up checking the wrong variable for NULL.

Submitted by: bde


# 23b8efad 23-Jun-2004 Garance A Drosehn <gad@FreeBSD.org>

Rework the logic for `-t <tty>', such that it accepts "ttyp0" and "console",
in addition to "/dev/ttyp0" or "p0" and "/dev/console" or "co".


# bdf8ab46 23-Jun-2004 Garance A Drosehn <gad@FreeBSD.org>

Change "struct varent" to use the standard queue(8) macros, instead of
using it's own version of the same basic algorithm.

Submitted by: part by Cyrille Lefevre, part of it done by me


# dd693acf 23-Jun-2004 Garance A Drosehn <gad@FreeBSD.org>

Add a check for defunct processes in saveuser(), so the output for "args"
(aka "command") will display "<defunct>", as does the output from "comm"
for those processes. Also do better checking for malloc() failures.

Submitted by: Cyrille Lefevre


# 31172f6e 23-Jun-2004 Garance A Drosehn <gad@FreeBSD.org>

Include the `-c' option in the usage() message.

Submitted by: Cyrille Lefevre


# b4b24324 23-Jun-2004 Garance A Drosehn <gad@FreeBSD.org>

In the sorting routine, sort by thread-id if two processes have the same PID.

Submitted by: Cyrille Lefevre


# 4bac4483 20-Jun-2004 Garance A Drosehn <gad@FreeBSD.org>

Have the main() routine calculate %CPU and (if needed) memory information
when copying per-process info before starting to sort the list. This way,
sort-by-CPU or sort-by-memory will only calculate values once-per-process,
instead of twice-per-comparison. Also take advantage of this to simplify
the pscomp() routine.


# ecbb06f8 01-Jun-2004 Garance A Drosehn <gad@FreeBSD.org>

Try to change the isdigitch() macro to something that Bruce won't roll
his eyes at quite so much... (actually someone else pointed this out
to me a long time ago, but apparently I never fixed it)


# c23b00b7 01-Jun-2004 Garance A Drosehn <gad@FreeBSD.org>

A few more style-fixes from Bruce. The only non-cosmetic change
is to drop a call to setuid() which has not been needed for years.

Noticed by: bde


# a9626fb3 01-Jun-2004 Garance A Drosehn <gad@FreeBSD.org>

Since I'm not ready to add the non-standard ADD_PS_LISTRESET feature,
remove the #ifdef for it for now. I might add the feature for real at
some later date, there isn't much reason for the #ifdef for now.


# 25113083 01-Jun-2004 Garance A Drosehn <gad@FreeBSD.org>

Make a few style-istic improvements to the previous commits.

Noticed by: bde


# de1702f4 31-May-2004 Garance A Drosehn <gad@FreeBSD.org>

Fix so `ps' catches and complains about null-values specified for a
process id, instead of using pid==0. Ie, `ps -p 12,' and `ps -p ,12'
are now errors (instead of being treated like `ps -p 0 -p 12').

Noticed by: Cyrille Lefevre on freebsd-arch


# bf46a3bf 31-May-2004 Garance A Drosehn <gad@FreeBSD.org>

Additional tiny adjustment to kludge-option processing so `ps t p0'
is treated like `ps -t p0', instead of changing it to `ps -T p0'.
Note that `ps t' is still changed to `ps -T', since that is one of
the main reasons for this kludge processing...

Noticed by: Jilles Tjoelker on freebsd-arch


# c675340a 31-May-2004 Garance A Drosehn <gad@FreeBSD.org>

Rewrite the kludge-option processing to improve how it handles a few
more special situations. This is the code which process `ps blah',
when "blah" does not include a leading '-'.

This change also removes a long-undocumented BACKWARD_COMPATIBILITY
compile-time option, where:
ps -options arg1 arg2
(with no '-' on "arg1" and "arg2") was treated as:
ps -options -N arg1 -M arg2

This also changes `ps' to check for any additional arguments after
processing all the '-'-options, and attempt to use those arguments as
a pid or pidlist. If an extra argument is not a valid pidlist, then
`ps' will print an error and exit. This seems a more generally useful
extension of the kludge-option processing than the -N/-M behavior, and
has fewer confusing side-effects.

Reviewed by: freebsd-arch


# 259fcfac 23-May-2004 Garance A Drosehn <gad@FreeBSD.org>

Add the 'sid' info to the output of `ps -j', to make up for the 'sess'
(session-pointer) info which was dropped from `ps' earlier in 5.x.

PR: bin/59423
Submitted by: Jilles Tjoelker


# 2631c777 23-May-2004 Garance A Drosehn <gad@FreeBSD.org>

Fix the kludge-old-options processing so `ps tpt' will be treated the
same as `ps -tpt', instead of being changed into `ps -tpT'.

PR: bin/52489
Submitted by: Jilles Tjoelker
MFC after: 1 week


# 7bd42165 22-May-2004 Garance A Drosehn <gad@FreeBSD.org>

Change `ps' to use the KERN_PROC_RGID and KERN_PROC_SESSION options
(if trying to match only one real-group or one session-id), now that
those options are implemented in src/sys/kern/kern_proc.c (v1.203).

PR: bin/65803 (a very tiny piece of the PR)
Submitted by: Cyrille Lefevre


# 6195fb41 06-Apr-2004 Mark Murray <markm@FreeBSD.org>

Remove clause 3 from the UCB licenses.

OK'ed by: imp, core


# b193355d 04-Apr-2004 Garance A Drosehn <gad@FreeBSD.org>

Drop the include of <stdint.h>, since r1.84 removed references of intmax_t.


# 9bf69950 04-Apr-2004 Garance A Drosehn <gad@FreeBSD.org>

Add back the `-e' option, which was mistakenly dropped when cleaning
up the PS_ARGS string in revision 1.69 (and which was apparently not
missed by anyone...).


# d822163f 03-Apr-2004 Garance A Drosehn <gad@FreeBSD.org>

Give a name of 'l' (list) to the union in struct listinfo. This is
because some compilers (such as gcc 2.95.4) do not support having an
unnamed union for a field in a struct.


# 0b42be7c 29-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Switch to using strtoul() for parsing a potential UID or GID, which gets
this to correctly handle UID's and GID's larger than 2147483647.

Noticed by: bde
MFC after: 1 week


# f35e0715 29-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Bruce would really like the prototype for fmt() to be split across lines
this way (although I still think it "looks weird"...).

Requested by: bde
MFC after: 1 week


# ba50b0e0 29-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Minor style fixes, mostly adding indent-protection on some comment-blocks.

Noticed by: bde
MFC after: 1 week


# 5bd7b1f3 29-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Replace pscomp() with a cleaner version, mostly written by bde (*).
This corrects a problem of lost-precision for `-r' (sort-by-CPU). Also,
for sort-by-CPU and sort-by-memory, any processes which have the same
value CPU or MEMORY are now sorted by TTY and then (if needed) by pid.

(* - I just added the NODEV checks, after doing some testing of my own)

Submitted by: bde
MFC after: 1 week


# c46bb4b3 28-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Oops. Remove some ';'s in #defines added by a previous update.

Noticed by: bde


# c7910c3a 28-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Have this source explicitly include <sys/proc.h>, since it references
values such as P_CONTROLT and PS_INMEM. But this still won't define
PID_MAX for us, since that is hidden inside of '#ifdef _KERNEL'.

Noticed by: bde


# 87e47750 28-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Since "kp" is a pointer, I should be comparing against NULL not 0.

Noticed by: bde


# 352b6523 28-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Various style improvements, mostly in comments and indentation.

Suggested by: bde (well, for most of them)


# ca62e195 28-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

In the routines I've been working on, sort the variable declartions
so that non-pointers are listed after pointer-type variables.

Noticed by: bde


# 1d2324f4 28-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Split two 'fmt' strings so they're easier to read on 80-char windows.


# de800cd4 28-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Sort the declarations of global variables.


# 4857f240 28-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Sort the routine prototypes.


# 5dfd7724 27-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

If <x> is a process id that does not exist, then just print the header
(if any) and exit, thus matching the behavior on -stable and other OS's.
My earlier attempt to fix this (v1.65) only seemed to work because of a
lucky random value in nentries (which was not being initialized back
when I tested that earlier patch).


# e3c4e1dd 27-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

If a non-existent user is given as part of `-U userlist', treat it as
a fatal error instead of a minor warning. It is possible that a few
users are used to the previous behavior, but I'm claiming it was a bug.


# 29d58b10 27-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Explicitly wrap two long-ish linesi of code, to make them easier to read.


# b34f38ae 27-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Change the #if-ish logic which is used to add the `-f' option when `ps'
is compiled with LAZY_PS, so that there is only one PS_ARGS string to
modify when changing the option-list. Also get `-f' to show up in the
usage() statement when compiled with LAZY_PS.


# 4a355d17 27-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Move the 'f' case so it shows up in the right place, alphabetically.


# a4c8a745 27-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Support more POSIX/SUSv3 options:

- Change `-p' to allow a list of process IDs, and `-t' to allow a list
of terminal names, instead of only a single value for each.
- Add the `-A' option of SUSv3, which is exactly the same as `-ax'.
- Add the `-G gidlist' (group id).
- Allow any of these "selector options" to be specified multiple times,
and have `ps' keep adding to a given list -- instead of replacing the
previously-specified values.
- Fix interactions between selector-options, so that: "If any are
specified, ... ps shall select the processes represented by the
inclusive OR of all the selection-criteria options." (from SUSv3)
- Add a `-X' option, which is the reverse of the `-x' option.

- various minor improvements in parsing and error handling.

This does not get us to match POSIX/SUSv3, but it gets us closer. The
`-g pgidlist', `-R ruserlist' and `-s sidlist' options mentioned in
freebsd-standards are still under debate, so they skipped for now.
It should be true that this introduces no user-visible incompatible
changes, except to support "new stuff" that was not supported before.


# 2e6c6ac4 17-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Fix 'ps -p proclist' and 'ps -u userlist' so the command returns non-zero
if no processes were matched. Also sorts the list of 'int's in main, as
long as I had to add another one...

Noticed by: Nate Lawson
MFC after: 10 days


# 4e8b6a6f 17-Mar-2004 Garance A Drosehn <gad@FreeBSD.org>

Improvements to 'ps -p <x>'. If <x> is a process id that does not exist, then
just print the header (if any) and exit, thus matching the behavior on -stable
and other OS's.

Also adds support for <x> being a comma-separated list of processes, and does
a much better checking for invalid-values of <x>, such as 'ps -p someword'.

Reviewed by: mentioned on freebsd-current
MFC after: 10 days


# d75c1d83 22-Feb-2004 Daniel Eischen <deischen@FreeBSD.org>

Allow the -H option to show threads when selecting by uid, tty,
and pid.


# 48b8c0de 12-Jun-2003 Scott Long <scottl@FreeBSD.org>

Add the -H option to ps(1) to display all kernel visible threads in each
process. The default behavior of showing only the process is retained as
the default.


# eaed5652 05-Feb-2003 Philippe Charnier <charnier@FreeBSD.org>

Display elapsed time (-o etime) using [[dd-]hh:]mm:ss, which according to
Solaris man page is the POSIX way.

Reviewed by: jmallett


# 78b1878a 18-Jan-2003 Juli Mallett <jmallett@FreeBSD.org>

Per-variable\ entry headers, to allow the 'ps -otime -otime=FOO' or similar
case to do the right thing and affect exactly one column. This is consistent
with GNU ps(1) in BSD mode, and POLA.


# fde411d5 18-Jan-2003 Juli Mallett <jmallett@FreeBSD.org>

When inserting a non-user-specified (e.g. not via -o or -O) format, don't dupe
one that is already there. This is consistent with GNU ps(1)'s BSD mode, and
POLA.

Reported by: Andy Farkas <andyf@speednet.com.au>
Tested by: Andy Farkas <andyf@speednet.com.au>


# 2af538eb 23-Oct-2002 Robert Watson <rwatson@FreeBSD.org>

Use the MAC interface to list process MAC labels rather than using
the LOMAC-specific interface (which is being deprecated). The
revised LOMAC using the MAC framework will export levels listable
using this mechanism.

Approved by: re
Obtained from: TrustedBSD Project
Sponsored by: DARPA, Network Associates Laboratories


# daed3ad6 02-Sep-2002 Juli Mallett <jmallett@FreeBSD.org>

Prevent ps(1) from doing idiotic munging of things in a -ofmt= string.

God I hate the backwards compatability crap here.


# a755f1c9 08-Jul-2002 Robert Drehmel <robert@FreeBSD.org>

- Use (MAXLOGNAME - 1) where UT_NAMESIZE was used to be able to
(-)remove the inclusions of <utmp.h>.


# 2749b141 29-Jun-2002 David E. O'Brien <obrien@FreeBSD.org>

Consistently use FBSDID


# 41623b2d 20-Jun-2002 Maxim Sobolev <sobomax@FreeBSD.org>

Don't try to decode old-style options if the argv[1] begins with `-' and the
second character represents some option taking an argument. This fixes
problem when ps(1) is invoked for examply as follows:

$ ps -Ufoobar1234

the above example results in option string being interpreted as
-U foobarp1234 - note extra `p'.

Reported by: Vladimir Sotnikov <vovan@kyivstar.net>
MFC after: 2 weeks


# ba2cd770 06-Jun-2002 Juli Mallett <jmallett@FreeBSD.org>

Use a global `now' variable for the current time, and initialise it at
startup, right after calling setlocale(3).


# 4fa7d788 05-Jun-2002 Juli Mallett <jmallett@FreeBSD.org>

Widen the scope of fmt.c::1.19 and consistently use errx(3) if malloc(3) [or
realloc(3)] happens to fail, everywhere in ps(1).

Discussed with: bde, charnier (a while ago)

fmt_argv() can no longer return NULL, so don't bother checking.

Submitted by: bde


# c0716492 05-Jun-2002 Juli Mallett <jmallett@FreeBSD.org>

Comma seperate format lists, since space is no longer up to the task.

Poked by: David Wolfskill <david@catwhisker.org>
Pointy hat to: jmallett


# 4f18100d 04-Jun-2002 Tim J. Robbins <tjr@FreeBSD.org>

Respect setting of the COLUMNS environment variable (SUSv3)


# a5b0f302 06-Apr-2002 Chris D. Faulhaber <jedgar@FreeBSD.org>

errx()/strerror() -> err()


# d9a5f890 21-Feb-2002 Matthew Dillon <dillon@FreeBSD.org>

Revert wchan functionality. Add 'mwchan' to supply new duel mutex/msleep
functionality and make it the default.

With additional improvements by: Mark Peek <mp@FreeBSD.org>


# 871e8d8c 03-Feb-2002 Mark Murray <markm@FreeBSD.org>

WARNS=4 fixes (incomplete, so set NO_WERROR), and lots of extra
cleanup courtesy of automatic checking (lint).


# 46251dde 01-Feb-2002 Warner Losh <imp@FreeBSD.org>

o __P has been reoved
o Old-style K&R declarations have been converted to new C89 style
o register has been removed
o prototype for main() has been removed (gcc3 makes it an error)
o int main(int argc, char *argv[]) is the preferred main definition.
o Attempt to not break style(9) conformance for declarations more than
they already are.


# 9e1c120c 28-Jan-2002 Ruslan Ermilov <ru@FreeBSD.org>

Remove a stray `:' after `v' in the getopt() call.

Submitted by: bde


# 831c910a 28-Jan-2002 Ruslan Ermilov <ru@FreeBSD.org>

GC the -W option. kvm(3) doesn't read swap for almost 10 years.

PR: docs/34134
Reviewed by: bde, peter
MFC after: 1 month


# f1ff35ae 14-Jan-2002 Maxim Sobolev <sobomax@FreeBSD.org>

Add missed includes.

Reviewed by: md5


# 7304f61f 26-Nov-2001 Brian Feldman <green@FreeBSD.org>

Add LOMAC options (the "Z" flag in both cases) to display extra information
in ls(1) and ps(1).

Sponsored by: DARPA, NAI Labs


# fbb75ab8 24-Aug-2001 Peter Wemm <peter@FreeBSD.org>

Handle kvm_getprocs() returning nothing. Dont pass -1 to a size
for malloc().


# 380434d4 15-Aug-2001 Brian Somers <brian@FreeBSD.org>

Only capitalise the last `t' in ps's first argument when it's
actually part of an option argument.

Submitted by: Chris Costello <chris@calldei.com>
MFC after: 6 weeks


# 5782e272 29-May-2001 Warner Losh <imp@FreeBSD.org>

Use PATH_MAX rather than MAXPATHLEN.


# 25bba4f6 03-May-2001 Mark Murray <markm@FreeBSD.org>

Depollute headers now that the VM headers DTRT.


# 325a83b4 01-May-2001 Mark Murray <markm@FreeBSD.org>

Compensate for header dethreading.


# 52f8369e 06-Apr-2001 Brian Somers <brian@FreeBSD.org>

The sess column went away last December with v1.26 of keyword.c
Remove it from ``jfmt''.

Forgotten by: mckusick


# f59105ee 02-Mar-2001 Andrey A. Chernov <ache@FreeBSD.org>

Small optimization: set use_ampm only when needed


# 08017519 02-Mar-2001 Andrey A. Chernov <ache@FreeBSD.org>

Use AM/PM time only when available in locale


# e0aa5ab71 23-Jan-2001 John Baldwin <jhb@FreeBSD.org>

- Catch up to new proc flags.


# 1f7d2501 12-Dec-2000 Kirk McKusick <mckusick@FreeBSD.org>

Change the proc information returned from the kernel so that it
no longer contains kernel specific data structures, but rather
only scalar values and structures that are already part of the
kernel/user interface, specifically rusage and rtprio. It no
longer contains proc, session, pcred, ucred, procsig, vmspace,
pstats, mtx, sigiolst, klist, callout, pasleep, or mdproc. If
any of these changed in size, ps, w, fstat, gcore, systat, and
top would all stop working. The new structure has over 200 bytes
of unassigned space for future values to be added, yet is nearly
100 bytes smaller per entry than the structure that it replaced.


# cf22dcfc 25-Sep-2000 Brian Somers <brian@FreeBSD.org>

Support multiple (comma separated) names as arguments to -U

PR: 11051


# f8c9c11c 07-Jul-2000 Will Andrews <will@FreeBSD.org>

Fix the exit code for the case where nentries == 0; if a PID doesn't exist,
ps(1) should not be returning a success code (0), it should return an
error code (1). This was fixed on OpenBSD over 3 years ago.

PR: 19069
Submitted by: Jim Sloan <odinn@atlantabiker.net>
Reviewed by: rwatson


# 602a69d6 16-Nov-1999 Peter Wemm <peter@FreeBSD.org>

Use eproc.e_stats, not a series of crude hacks to fetch it from the u-area.
Also, fix some indentation that got messed up somehow..


# b9df5231 16-Nov-1999 Poul-Henning Kamp <phk@FreeBSD.org>

Introduce commandline caching in the kernel.

This fixes some nasty procfs problems for SMP, makes ps(1) run much faster,
and makes ps(1) even less dependent on /proc which will aid chroot and
jails alike.

To disable this facility and revert to previous behaviour:
sysctl -w kern.ps_arg_cache_limit=0

For full details see the current@FreeBSD.org mail-archives.


# bdfebd84 14-Nov-1999 Kris Kennaway <kris@FreeBSD.org>

Typo (appropiate -> appropriate)

Obtained from: OpenBSD (inspired by)


# 2a456239 27-Aug-1999 Peter Wemm <peter@FreeBSD.org>

$Id$ -> $FreeBSD$


# bb227406 01-Apr-1999 Bill Paul <wpaul@FreeBSD.org>

On FreeBSD/alpha, ps(1) does not correctly report process start times
and CPU runtime because it can't access the user area via /proc/<pid>/mem.
This is because the uarea is not mapped into the process address space
at USRSTACK on the alpha like it is on the x86.

Since I'm haven't been able to wrap my brain around the VM system enough
to be able to figure out how to achieve this mapping, and since it's
questionable that such an architectural change is correct, I implemented
a workaround to allow ps(1) to read the uarea from /dev/kmem using
kvm_read() instead of from the process address space via kvm_uread().
The kludge is hidden inside #ifdef __alpha__/#endif so as not to impact
the x86. (Note that top(1) probably uses this same gimmick since it works
on FreeBSD/alpha.)

Reviewed by: dfr


# 85082fc3 30-Jun-1998 Poul-Henning Kamp <phk@FreeBSD.org>

Pick up kernel variables/constants using sysctl rather than through /dev/mem

Use /dev/null for opening the kvm library, we don't need access to /dev/mem
anymore.

ps can now run without the setgid(kmem) bit. If it does it will not be
able to show argv/envp for another uid's processes unless you are root.


# c9a8d1f4 15-May-1998 Philippe Charnier <charnier@FreeBSD.org>

Correct use of .Nm. Add rcsid.


# 6425bb43 18-Feb-1998 Dima Ruban <dima@FreeBSD.org>

cosmetic change for optstring


# 3ac5e955 05-Dec-1997 John Dyson <dyson@FreeBSD.org>

Add an option to building PS, so that the upages are explicitly paged in only
for users who are root, or in group wheel. This is useful on large timesharing
systems where a PS command can cause the system to grind to a halt. The
ability to get the information isn't diminished for those who really need the
additional detail (administrators.) Normal users won't see any difference unless
the processes are swapped out. The "really get it mode" is invoked by the
use of an additional flag in the command string "-f". New/old behavior is
selectable with a compile option.

PR: 5196
Submitted by: Matt Dillon <dillon@best.net>


# 940cca66 03-Aug-1997 Peter Wemm <peter@FreeBSD.org>

Kill #ifndef NEWVM etc. It affected a lot of other things besides
VM structure (eg: credentials etc) and it's highly unlikely we'll ever
get to see the "tainted" BSD<=4.3 VM code in public use. Although it
indicated the way some things used to be done, it obfuscates things too
much.


# 749d4bb6 06-Jun-1997 Philippe Charnier <charnier@FreeBSD.org>

Cosmetic change in usage string.


# 6a2d726b 28-Apr-1997 Jordan K. Hubbard <jkh@FreeBSD.org>

Dynamically adjust size of displayed username to the longest username which
appears, not the longest _maximum_ username (this should probably also go
into 2.2, for the day when we bump up the username length there too).

Submitted-By: Terry Lambert <terry@lambert.org>


# 93ef08af 28-Mar-1997 Warner Losh <imp@FreeBSD.org>

compare return value from getopt against -1 rather than EOF, per the final
posix standard on the topic.


# b97fa2ef 22-Feb-1997 Peter Wemm <peter@FreeBSD.org>

Revert $FreeBSD$ to $Id$


# 1130b656 14-Jan-1997 Jordan K. Hubbard <jkh@FreeBSD.org>

Make the long-awaited change from $Id$ to $FreeBSD$

This will make a number of things easier in the future, as well as (finally!)
avoiding the Id-smashing problem which has plagued developers for so long.

Boy, I'm glad we're not using sup anymore. This update would have been
insane otherwise.


# 78b09ffe 13-Dec-1996 Steve Price <steve@FreeBSD.org>

-Wall cleaning.


# 03fd7487 09-Nov-1996 Jeffrey Hsu <hsu@FreeBSD.org>

Remove unneeded #include <sys/proc.h>.


# db91faac 21-Oct-1996 Peter Wemm <peter@FreeBSD.org>

Implement a -c option to ps to display the short command name instead of
the full argument vector.

I've bumped into a few things that expected this switch to be present,
the most recent was the snmp package in ports. I'm not 100% sure of the
origins of this, but Linux has it, so does the "BSD-compatable" version
of ps on our SVR4 systems (so I assume SunOS has it too).


# b2496d93 20-Jan-1996 Mike Pritchard <mpp@FreeBSD.org>

Use the correct buffer size from limits.h for the error buffer
passed to kvm_open. Closes PR# 476.

Submitted by: Jeffrey Hsu <hsu@freebsd.org>


# d007c603 12-Jan-1996 Peter Wemm <peter@FreeBSD.org>

oops. I forgot to add the "[-U username]" option to the usage string.


# 73eb8310 25-Dec-1995 Peter Wemm <peter@FreeBSD.org>

Implement a new option to ps.. `-U username'. This allows you to
list the processes belonging to a particular user without having to use
`-u' and grepping for the username. Basically you can now get a short
`ps -x' like list (with more space for the command) for other users.


# 2bf4b9cf 26-Oct-1995 Andrey A. Chernov <ache@FreeBSD.org>

Change local to LC_ALL, there is no bitmask


# 0c56c7b2 23-Oct-1995 Andrey A. Chernov <ache@FreeBSD.org>

Add setlocale LC_CTYPE|LC_TIME


# 2162b2d2 29-May-1995 Rodney W. Grimes <rgrimes@FreeBSD.org>

Remove trailing whitespace.

Reviewed by: phk


# 0fd510b7 19-Mar-1995 Joerg Wunsch <joerg@FreeBSD.org>

You will find enclosed some changes to make gcc -Wall more happy in
/usr/src/bin. Note that some patches are still needed in that directory.

I (Joerg) finished most of Philippe's cleanup. /bin/sh will still
need *allot* of work, however.

Submitted by: charnier@lirmm.fr (Philippe Charnier)


# 0ec663d5 17-Oct-1994 David Greenman <dg@FreeBSD.org>

Use kvm_uread instead of kvm_read to access the upages.


# 8b6f5f5f 02-Oct-1994 David Greenman <dg@FreeBSD.org>

On second thought...back out previous commit.


# 793ced0e 02-Oct-1994 David Greenman <dg@FreeBSD.org>

Include rtprio.h


# 89730b29 23-Sep-1994 David Greenman <dg@FreeBSD.org>

Added $Id$


# 4b88c807 26-May-1994 Rodney W. Grimes <rgrimes@FreeBSD.org>

BSD 4.4 Lite bin Sources