Commit Graph

2233 Commits

Author SHA1 Message Date
Jim Warner
c76144ead1 top: added utf8 multibyte support to additional fields
When any process' command line contains multibyte utf8
characters, two separate display problems could arise.

1. If that COMMAND column is not displayed as the very
last field, then field(s) to the right are misaligned.

2. Even when last, should utf8 string length (not that
display length) exceed allowable screen width, it will
nonetheless suffer from improper premature truncation.

Number 1 is less of a concern since the cmdline column
is likely to always be the last field to be displayed,
if only to enable right and left scrolling provisions.

Number 2 is much more likely to occur, especially with
additional fields which might be shown before COMMAND.
Or, forest view child tasks can yield the same effect.

So, this commit will permit the correct utf8 multibyte
display regardless of field position or string length.

And, we'll bring top into line with the ps program for
additional fields potentially subject to utf8 display.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2020-12-29 08:42:28 +11:00
Jim Warner
be22291257 ps: extend utf8 multibyte support to additional fields
Form its inception (back in May of 2011), escaped_copy
has always been a flawed function. It does not operate
on 'escaped' strings but instead treats all input as a
regular string incapable of containing utf8 sequences.

As such, it should only be used for strings guaranteed
to NOT contain multibyte characters (like supgid). For
all other strings, which could contain utf8 stuff, the
correct function should have been that escape_str guy.

So this commit changes nearly every escaped_copy call.

[ note: unlike the newlib guy, the master ps program ]
[ cannot properly handle utf8 multibyte sequences in ]
[ in the recently introduced 'exe' field shown below ]

Reference(s):
. Jun 2018, introduced 'exe' field
commit b556bf5ba8
. May 2011, original escaped_copy (cmdline, cgroup)
commit 7b0fc19e9d

Signed-off-by: Jim Warner <james.warner@comcast.net>
2020-12-29 08:42:28 +11:00
Jim Warner
9165f79fc1 top: correct one spelling oops in a programmer comment
Signed-off-by: Jim Warner <james.warner@comcast.net>
2020-12-29 08:42:28 +11:00
Craig Small
c64584a7b1 misc: Add NEWS for ps sorting 2020-12-22 16:26:35 +11:00
Craig Small
fbb3b54319 Merge branch 'topimiettinen/procps-ps-sort-label'
References:
 procps-ng/procps!99
2020-12-22 16:24:47 +11:00
Topi Miettinen
173d5214db ps: add lots of sorting functions
Use NULL in format_array to indicate that the print function shall be
used also for sorting. Change sr_nop() to NULL for all fields which
don't use pr_nop() for printing.

Before the commit (note that '--sort label' has no effect: the rows
are not sorted according to label field):
$ ps -A -o command,label --sort label | grep /lib/systemd
/lib/systemd/systemd-journa system_u:system_r:syslogd_t:s0
/lib/systemd/systemd-udevd  system_u:system_r:udev_t:s0-s0:c0.c1023
/lib/systemd/systemd-networ system_u:system_r:systemd_networkd_t:s0
/lib/systemd/systemd-resolv system_u:system_r:systemd_resolved_t:s0
/lib/systemd/systemd-timesy system_u:system_r:ntpd_t:s0
/lib/systemd/systemd-logind system_u:system_r:systemd_logind_t:s0
/lib/systemd/systemd --user user_u:user_r:user_t:s0
/lib/systemd/systemd --user root:sysadm_r:sysadm_t:s0-s0:c0.c1023
grep /lib/systemd           user_u:user_r:user_t:s0

After the commit, '--sort label' works and the output is sorted:
$ ps -A -o command,label --sort label | grep /lib/systemd
/lib/systemd/systemd --user root:sysadm_r:sysadm_t:s0-s0:c0.c1023
/lib/systemd/systemd-timesy system_u:system_r:ntpd_t:s0
/lib/systemd/systemd-journa system_u:system_r:syslogd_t:s0
/lib/systemd/systemd-logind system_u:system_r:systemd_logind_t:s0
/lib/systemd/systemd-networ system_u:system_r:systemd_networkd_t:s0
/lib/systemd/systemd-resolv system_u:system_r:systemd_resolved_t:s0
/lib/systemd/systemd-udevd  system_u:system_r:udev_t:s0-s0:c0.c1023
/lib/systemd/systemd --user user_u:user_r:user_t:s0
grep /lib/systemd           user_u:user_r:user_t:s0

Signed-off-by: Topi Miettinen <toiwoton@gmail.com>
2020-12-22 16:18:17 +11:00
Craig Small
5f859b30d3 pgrep: Remove memory leak
This is part of !118 where @tt.rantala found a memory leak.
The other part of !118 may come later if the performance change
is significant.

References:
 procps-ng/procps!118
2020-12-22 16:08:49 +11:00
Craig Small
79196a0b9e Merge branch 'axxName/procps-master'
References:
 procps-ng/procps!63
2020-12-22 15:43:07 +11:00
Alexandr Miasnikov
f3afe3ce57 If you use echo -e $(put sgr0)"text" - reset color attributes, it turns out the artifact (Btext 2020-12-22 15:41:44 +11:00
Craig Small
3dcaf97773 ps: Add NEWS and test for exe output
Only need this for master, as Jim took care of exe
handling for newlib in the ad4269 change.

References:
 commit b556bf5ba8
 commit ad4269f118
2020-12-22 15:31:39 +11:00
Jan Rybar
b556bf5ba8 ps: new format option 'exe' now shows executable path if unavail from cmdline
Red Hat Bugzilla #1399206
2020-12-22 04:24:57 +00:00
Craig Small
fa389631ca build-sys: Make pwait test dependent on pwait built
As pwait building is conditional, testsuite should
only run pwait tests if there is a pwait binary.
2020-12-22 15:21:39 +11:00
Craig Small
e0a02f7989 Merge branch 'awilfox/procps-utmpx-support'
References:
 procps-ng/procps!67
2020-12-22 14:59:47 +11:00
A. Wilcox
3c1fed7d65 w: Use POSIX <utmpx.h> functions where available
<utmp.h> has been deprecated since 2001 in favour of <utmpx.h>.

On glibc systems, utmp is just an alias to utmpx, so there is no
functional change using one over the other.

However, on the musl libc, a library (utmps) can be used to provide
utmpx functionality - but not utmp.  This means that procps either
doesn't work properly (`w` shows nothing under musl with default no-op
implementation), or fails to build (utmps provides utmpx.h but no
utmp.h).

This commit will use utmpx.h where available, which allows `w` to work
correctly with utmps and has no change on glibc systems.
2020-12-22 14:59:15 +11:00
Craig Small
bf92199ccb Merge branch 'teknoraver/procps-master'
References:
 procps-ng/procps!83
2020-12-22 14:32:26 +11:00
Craig Small
c2e69b34a7 NEWS: Add notice for pidof -q 2020-12-22 14:32:03 +11:00
Matteo Croce
b067ecc886 pidof: allow to suppress output
Often pidof is used in shell scripts in this form:

    if pidof daemon >/dev/null; then
        ...
    fi

The redirection to /dev/null is needed because otherwise the script
would output the found PIDs.
Let's add a -q option which, similary to grep, just sets the exit code.
Also exit on first match, as there is no reason to proceed further when
at least a process is matched.

Tested with:

    $ ./pidof bash
    17701 14019 5276 2967
    $ echo $?
    0
    $ ./pidof bashx
    $ echo $?
    1
    $ ./pidof -q bash
    $ echo $?
    0
    $ ./pidof -q bashx
    $ echo $?
    1
2020-12-22 14:31:17 +11:00
Raphaël Jakse
79097e55e4 kill: Fix argument handling for negative PIDs 2020-12-22 03:16:47 +00:00
Craig Small
5c55464aab Merge branch 'nipunn1313/procps-watch_colors2'
References:
 procps-ng/procps!109
2020-12-22 13:54:50 +11:00
Nipunn Koorapati
3549f49df0 Supporting bright colors with ncurses 2020-12-22 13:53:04 +11:00
Nipunn Koorapati
a23c941d8b Support simple 8 bit ansi escape sequences
See https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
Support standard and high intensity colors. The default
ncurses colors ARE the high intensity colors - represented
via an 8 bit ansi escape sequence here.
2020-12-22 13:53:00 +11:00
Craig Small
6285bdd3b5 Merge branch 'nipunn1313/procps-watch_colors'
References:
 procps-ng/procps!106
2020-12-22 13:48:15 +11:00
Nipunn Koorapati
ffa556633f Spaces to tabs 2020-12-22 13:44:54 +11:00
Nipunn Koorapati
a3a5db1311 Add comments and limit to standard colors 2020-12-22 13:43:30 +11:00
Nipunn Koorapati
8d0928f072 bugfix - increment endpointer when parsing color_escape_sequence 2020-12-22 13:43:30 +11:00
Nipunn Koorapati
486a3e30e1 Support simple 8 bit ansi escape sequences
See https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
Support standard and high intensity colors. The default
ncurses colors ARE the high intensity colors - represented
via an 8 bit ansi escape sequence here.
2020-12-22 13:43:25 +11:00
Craig Small
e2470edc8c Merge branch 'antonblanchard/procps-proc-stat-buffer'
References:
 procps-ng/procps!105
2020-12-22 13:18:58 +11:00
Anton Blanchard
1b9ea61116 Increase BUFFSIZE to handle very large /proc/stat files
We read /proc/stat using a statically sized buffer. This was increased
to 64kB in 2005 via commit 777fcd3cf1 "/proc/stat for 1024 CPUs".

Unfortunately in 2020, 1024 CPUs is not enough for anyone. I have a
large machine where /proc/stat is 74kB, and vmstat gives incorrect
output.

Double the buffer to 128kB in the confidence that 2048 CPUs is actually
enough for anyone.

Signed-off-by: Anton Blanchard <anton@ozlabs.org>
2020-12-22 13:18:44 +11:00
Craig Small
4a54ae97a0 testsuite: Add pwait tests
They're pretty simple tests but its something.

References:
  commit 09327c2b77
2020-12-22 12:25:44 +11:00
Craig Small
980983db74 Merge branch 'alxu/procps-pwait'
New command, pwait! Waits for another process to finish just like
pgrep finds or pkill kills another process.

References:
 procps-ng/procps!97

Signed-off-by: Craig Small <csmall@dropbear.xyz>
2020-12-22 12:16:09 +11:00
Alex Xu (Hello71)
09327c2b77 pgrep: add pwait 2020-12-22 12:09:16 +11:00
Craig Small
03fac47bd2 Merge branch 'nipunn1313/procps-spaces_to_tabs'
References:
 procps-ng/procps!108
2020-12-22 11:55:10 +11:00
Nipunn Koorapati
5d155068ce Convert spaces to tabs in watch.c 2020-12-22 11:54:52 +11:00
Craig Small
9c4ad4c419 Merge branch 'nipunn1313/procps-configure_ncurses'
Macs have the wide stuff in the ncurses (not ncursesw) library.

References:
 procps-ng/procps!110
 procps-ng/procps#47
2020-12-22 11:45:47 +11:00
Nipunn Koorapati
e0f47aab1c watch: Support wide char ncurses on mac with enable-watch8bit
Fixes #47
2020-12-22 11:45:20 +11:00
Samanta Navarro
b0c8e3408b misc: fix typos in manual pages
Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
2020-12-22 00:37:29 +00:00
Jan Rybar
584c65ba37 pkill manpage to document '-e' option 2020-12-22 00:30:50 +00:00
Jan Rybar
bf6c096dbd pidof: new option to show cmdline-less PIDs (kernel workers), empty input fixed 2020-12-22 00:14:41 +00:00
Craig Small
a04ea67d6f Merge branch 'brenns10/procps-master'
References:
 procps-ng/procps!119
2020-12-22 11:08:52 +11:00
Stephen Brennan
31343570e1 Set TZ to avoid repeated stat("/etc/localtime")
With glibc, each time the strftime() function is used (twice per process
in a typical ps -fe run), a stat("/etc/localtime") system call is used
to determine the timezone. Not only does this add extra system call
overhead, but when multiple ps processes are trying to access this
file (or multiple glibc programs using strftime) in parallel, this can
trigger significant lock contention within the OS kernel.

Since ps is not intended to run for long periods of time as a
daemon (during which the system timezone could be altered and PS might
reasonably be expected to adapt its output), there is no benefit to
repeatedly doing this stat(). To stop this behavior, explicitly set the
TZ variable to its default value (:/etc/localtime) whenever it is unset.
glibc will then cache the stat() result.
2020-12-22 11:08:36 +11:00
Craig Small
930dad118e Merge branch 'ratijas/procps-man-watch'
References:
 procps-ng/procps!121
2020-12-22 11:03:57 +11:00
ivan tkachenko
bfaa431052 docs: Consistent style across watch.1 2020-12-22 11:02:58 +11:00
Craig Small
fc93e2919d misc: Document sysctl change bug
While sysctl did change the order of /run and /etc to match
systemd in the referenced commit, the Debian bug report that
brought it to light was not documented.

References:
 commit 24a1574f0a
 https://bugs.debian.org/950788
2020-12-22 10:55:18 +11:00
Jim Warner
2a5b8658d2 top: a small safeguard added to the ATEOJ_RPTHSH logic
If a hash results report was output (via ATEOJ_RPTHSH)
a portion is devoted to occupied table entries ordered
by depth. There is a possibility that some depths will
not be found among existing occupied table entries and
to avoid any confusion probably should not be printed.

[ to illustrate the potential for confusion prior to ]
[ this patch, force a very small table size (like 8) ]
[ and then arrange to trigger the end-of-job report. ]

So this patch ensures only 'in use' entries are shown.

[ admittedly, all of the remaining logic in the loop ]
[ could/should be subordinate to this new 'if' test, ]
[ but we will keep the change to a minimum. besides, ]
[ there's no harm subtracting/adding a zero numdepth ]
[ especially since the chance of a zero is very low. ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2020-10-24 18:29:13 +11:00
Craig Small
cb13448179 free,slabtop,uptime: complain about extra ops
free, slabtop and uptime would happily take extra command line
arguments and doing nothing about them. The programs now check
optind after option processing and will give you usage screen
if there is anything extra.

References:
 procps-ng/procps#181
2020-10-19 22:36:06 +11:00
Craig Small
d748966e39 watch: Add no linewrap option
For long lines from a process, watch would wrap them around to the
next. While this default option has it uses, sometimes you want to
just cut those long lines down.

watch has a -w flag which will truncate the lines to the number
of columns. A few simple lines to do this new trick.

I think I caught all the ANSI state correctly but there might be
a chance it bleeds to the next row.

References:
 procps-ng/procps#182
2020-10-19 22:03:44 +11:00
Jim Warner
d10c8fccc9 top: just a small tweak to that history hash algorithm
This patch just raises the size of the hash table used
to calculate elapsed task stuff. The net result should
be less need for 'chaining' under pid hash collisions.

[ the hash scheme is intentionally kept as primitive ]
[ and, therefore, as fast as possible. it employs an ]
[ 'and' approach versus a 'mod' operation since both ]
[ yield similar distribution but the former approach ]
[ was 4 fewer cpu instructions in terms of overhead. ]

[ additionally, for hash collisions, 'chaining' uses ]
[ an array index rather than the usual pointer since ]
[ the HST_t guys may move when they are reallocated. ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2020-10-11 17:51:49 +11:00
Craig Small
3e1c00d051 ps: Match on truncated 16 char for -C
The referenced commit the comm length was increased from 16 to 64
characters to handle the larger command names for things like kernel
threads.

However most user processes are limited to 15 characters which means
if you try something like ps -C myprogramisbiggerthansixteen this would
fail to match because /proc/<PID>/comm would only be myprogramisbigg

ps now checks the comm length and if it is 15 and if the given match
is 15 or more, it will only match the first 15 characters.

This is also how killall has worked for about a year.

Thanks to Jean Delvare <jdelvare@suse.de> for the note.

References:
 commit 14005a371e
 commit psmisc/psmisc@1188315cd0

Signed-off-by: Craig Small <csmall@dropbear.xyz>
2020-09-24 22:01:22 +10:00
Jim Warner
6e1715d9eb top: fix additional SEGVs if no tasks were displayable
This patch is an outgrowth of that commit shown below.

Many additional potential segmentation faults might be
encountered if interactive commands are opened up to a
user when a '-p' switch has a single non-existent pid.

[ always the 'k', 'L', 'r', 'Y' keys & maybe 'v' too ]

So, this patch will restrict such a loser (oops, user)
to a reduced subset of normal commands until he/she/it
quits then restarts top with something to be displayed
or issues the '=' command overriding that '-p' switch.

Reference(s):
commit f57a0301e3

Signed-off-by: Jim Warner <james.warner@comcast.net>
2020-09-16 14:27:31 +10:00
Jim Warner
f57a0301e3 top: fix potential SEGV when no tasks were displayable
This patch fixes a nearly decade old bug discovered by
Frederik Deweerdt. His merge request shown below would
be an adequate solution except for iterative overhead.

This alternate patch will represent substantially less
overhead for an admittedly extremely rare possibility.

Reference(s):
https://gitlab.com/procps-ng/procps/-/merge_requests/114

And-thanks-to: Frederik Deweerdt <fdeweerdt@fastly.com>
Signed-off-by: Jim Warner <james.warner@comcast.net>
2020-09-13 10:28:50 +10:00