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>
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>
Before "strlen(outbuf)", if one of the pr_*() functions forgot to do it.
This prevents an out-of-bounds read in strlen(), and an out-of-bounds
write in "outbuf[sz] = '\n'". Another solution would be to replace
strlen() with strnlen(), but this is not used anywhere else in the
code-base and may not exist in all libc's.
pr_bsdstart(): Replace "strcpy(outbuf," with "snprintf(outbuf, COLWID,"
(which is used in all surrounding functions). (side note: the fact that
many pr_*() functions simply return "snprintf(outbuf, COLWID," justifies
the "amount" checks added to show_one_proc() by the "ps/output.c:
Replace strcpy() with snprintf() in show_one_proc()." patch)
pr_stime(): Check the return value of strftime() (in case of an error,
"the contents of the array are undefined").
help_pr_sig(): Handle the "len < 8" case, otherwise "sig+len-8" may
point outside the sig string.
pr_context(): Handle the empty string case, or else "outbuf[len-1]"
points outside outbuf.
Enforce a maximum max_rightward of OUTBUF_SIZE-1, because it is used in
constructs such as "snprintf(outbuf, max_rightward+1," (we could remove
the extra check at the beginning of forest_helper() now, but we decided
to leave it, as a precaution and reminder).
The minimum max_rightward check is not strictly needed, because it is
unsigned. However, we decided to add it anyway:
- most of the other variables are signed;
- make it visually clear that this case is properly handled;
- ideally, the minimum max_rightward should be 1, not 0 (to prevent
integer overflows such as "max_rightward-1"), but this might change
the behavior/output of ps, so we decided against it, for now.
Instead, we fixed the only function that overflows if max_rightward is
0. Also, enforce the same safe range for max_leftward, although it is
never used throughout the code-base.
This patch solves several problems:
1/ Limit the number of characters written (to outbuf) to OUTBUF_SIZE-1
(-1 for the null-terminator).
2/ Always null-terminate outbuf at q.
3/ Move the "rightward" checks *before* the strcpy() calls.
4/ Avoid an integer overflow in these checks (e.g., rightward-4).
The previous commit had one minor bug in it because the fields need
to be alphabetical and times comes after timeout.
Added NEWS item for this feature
Added another testsuite check for new flags in case they
disappear or go strange one day.
References:
commit 8a94ed6111
The ps program generally supports multi-byte sequences
in strings representing user and group names. However,
should a multi-byte sequence span the maximum width of
a column, the '+' inserted by ps to signify truncation
will corrupt that sequence, misaligning the text line.
Unfortunately, there's insufficient info returned from
the escape_str function (who calls escape_str_utf8) to
provide a robust response. So, this commit will revert
to the old standby of displaying a number when the '+'
character would've corrupted that multi-byte sequence.
Signed-off-by: Jim Warner <james.warner@comcast.net>
Reference(s):
proc/readproc.c: In function 'statm2proc'
proc/readproc.c:627:9: warning: variable 'num' set but not used [-Wunused-but-set-variable]
ps/output.c: In function 'pr_context':
ps/output.c:1273:14: warning: unused variable 'tried_load' [-Wunused-variable]
ps/output.c:1272:16: warning: unused variable 'ps_is_selinux_enabled' [-Wunused-variable]
ps/output.c:1272:16: warning: 'ps_is_selinux_enabled' defined but not used [-Wunused-variable]
ps/output.c:1273:14: warning: 'tried_load' defined but not used [-Wunused-variable]
ps/output.c:1837:18: warning: 'shortsort_array_count' defined but not used [-Wunused-const-variable=]
ps/output.c:1803:18: warning: 'aix_array_count' defined but not used [-Wunused-const-variable=]
ps/parser.c: In function 'arg_type':
ps/parser.c:1098:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
ps/parser.c:1099:34: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'if'
ps/sortformat.c: In function 'format_parse':
ps/sortformat.c:241:1: warning: label 'out' defined but not used [-Wunused-label]
ps/stacktrace.c:176:13: warning: 'stack_trace_sigsegv' defined but not used [-Wunused-function]
watch.c: In function 'process_ansi':
watch.c:234:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
watch.c:237:2: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'if'
Signed-off-by: Jim Warner <james.warner@comcast.net>
This patch ensures that namespace output will look the
same whether ps is built under a 32 or a 64-bit model.
[ it would have been easier to change that library's ]
[ data type, but we'll avoid breaking that ABI again ]
Signed-off-by: Jim Warner <james.warner@comcast.net>
While a Debian bug report referenced below was limited
to the 'eip' and 'esp' fields, this patch also extends
address width adaptations to some other addresses too.
[ and, we do so in a far less invasive manner than a ]
[ redhat approach shown below adding two new fields! ]
Reference(s):
. new debian bug report
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=846361
. old redhat solution
https://bugzilla.redhat.com/show_bug.cgi?id=244152
Signed-off-by: Jim Warner <james.warner@comcast.net>
dlopen() functionality is only used when SELinux support is enabled, so
<dlfcn.h> only needs to be included when ENABLE_LIBSELINUX is
defined. This fixes the build in configurations where <dlfcn.h> is not
available.
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
This patch adapts the ps program to a newly add proc_t
field and provides for new support in that top program
along with his man document (ps was already ok there).
Signed-off-by: Jim Warner <james.warner@comcast.net>
We'll now display a '?' for any systemd field when our
library was built without that above configure option.
Since the man page documents such fields this approach
is far superior to that old (confusing) error message:
. error: unknown user-defined format specifier "slice"
Signed-off-by: Jim Warner <james.warner@comcast.net>
If SELINUX is enabled but the machine is using another MAC system
(like apparmor), ps will fallback to just parsing
"/proc/%d/attr/current", otherwise the label/context would not
be properly displayed in that case.
References:
https://bugs.debian.org/786956
Signed-off-by: Craig Small <csmall@enc.com.au>
The cgroup field while shown as a vector is a concatenated
string, so alot of the complexity of sorting and displaying
has gone.
This change simplifies the cgroup sorting and adds display
and sorting for the name attribute of the cgroup, if found.
Signed-off-by: Craig Small <csmall@enc.com.au>
A rather small fix to sort by cgroup. This sorting function
could be used for other string vector entries, but I can't
see why you want to for, say, environment.
Reference:
https://bugs.debian.org/692279
Signed-off-by: Craig Small <csmall@enc.com.au>
ps has two columns showing the same data which is elapsed time, just
the format is changed:
etimes - elapsed time in seconds
etime - elapsed time in DD-hh:mm:ss
ps used to only sort by etime but not etimes, by making etimes
and alias of etime for sorting both flags work.
References:
https://bugs.debian.org/794619
Signed-off-by: Craig Small <csmall@enc.com.au>
This patch was made necessary by those library changes
in support of recently revised/simplified wchan logic.
In addition, this commit eliminates a broken alternate
'namelist' provision which was intended to allow users
to specify a System.map file to be used in translating
addresses into function names. But, the real effect of
the now defunct 'N' and '-n' options was to indirectly
force addresses (not names) to be displayed since such
user named map files could not be successfully parsed.
Besides when the required FRAME_POINTER kconfig option
is absent there is no address to translate and when it
is present /proc/PID/wchan is already translated. Thus
an alternate mapping is unnecessary and inappropriate.
[ we'll forgive POSIX for documenting '-n namelist' ]
Reference(s):
http://www.freelists.org/post/procps/WCHAN,11
Signed-off-by: Jim Warner <james.warner@comcast.net>
Library systemd-login offers possibility to display
name of a systemd slice unit for specific pid.
This patch adds output option "slice" which will
show name of systemd slice unit.
To maintain compatibility with non-systemd systems,
procps must be configured with --with-systemd option
to enable this option.
Sometimes with libselinux present but SELinux inactive
the context reported is "unconfined" which contains an
embedded newline. This then causes misalignment of any
subsequent data. So, ps will now protect against that.
Reference(s):
http://www.freelists.org/post/procps/enablelibselinux-switch,14
Signed-off-by: Jim Warner <james.warner@comcast.net>
Previously the libselinux support was present
in the sources, but disabled with a preprocessor
condition (#if 0).
From now the libselinux support can be enabled with
the --enable-libselinux switch available
in the configuration script. That way is more
flexible than local patches modifying the condition
value from 0 to 1.
ps : This patch removes sd_ prefix from recently added systemd output options
to let them look more tied with the system.
Patch does not change behaviour of these options, only modifies their
representation to user.
Library systemd-login offers possibility to display
name of seat for a session on multi-seat systems.
This patch adds output option "sd_seat" which will
show name of seat or "-", when name of seat can not
be determined, but "seat0" should always exist.
To maintain compatibility with non-systemd systems,
procps must be configured with --with-systemd option
to enable this option.
Library systemd-login offers possibility to display
name of systemd user unit for specific pid. Note that not all
processes are part of a user unit.
This patch adds output option "sd_uunit" which will
show name of user unit or "-", when process does not belong
to any user unit. This is similar to "sd_unit" but applies
to user units instead of system units.
To maintain compatibility with non-systemd systems,
procps must be configured with --with-systemd option
to enable this option.
Library systemd-login offers possibility to display
the name of the VM or container which process belongs to.
This patch adds output option "sd_machine" which will
show machine name or "-" when the name can not be determined.
To maintain compatibility with non-systemd systems,
procps must be configured with --with-systemd option
to enable this option.
Library systemd-login offers possibility to display the Unix
user identifier of the owner of the session of a process.
This information will also be displayed for user processes which
are shared between multiple login sessions of the same user,
where sd_session will be blank.
This patch adds output option "sd_ouid" which will show
user UID or "-", when there is no owner for a process.
To maintain compatibility with non-systemd systems,
procps must be configured with --with-systemd option
to enable this option.
Library systemd-login offers possibility to display name
of login session for specific pid.
Note that not all processes are part of a login session
(e.g. system service processes, user processes that are shared
between multiple sessions of the same user, or kernel threads).
This patch adds output option "sd_session" which will
show name of session or "-", when process does not belong
to any session.
To maintain compatibility with non-systemd systems,
procps must be configured with --with-systemd option
to enable this option.
Library systemd-login offers possibility to display
name of a systemd unit file for specific pid. Note that
not all processes are part of a system unit/service
(e.g. user processes, or kernel threads).
This patch adds output option "sd_unit" which will
show name of systemd unit or "-", when process does not
belong to any unit.
To maintain compatibility with non-systemd systems,
procps must be configured with --with-systemd option
to enable this option.
Each process in Linux has a /proc/<pid>/ns directory which contains
symbolic links to pipes that identify which namespaces that process
belongs to. This patch adds support for ps to display that information
optionally.
Signed-off-by: Aristeu Rozanski <arozansk@redhat.com>
The UNIX and POSIX standards require that user and
group names be printed as decimal integers when there
is insufficient room. This has led to a constant
stream of bug reports.
With this commit, long names will be truncated and
displayed with a trailing visual clue.
To avoid truncation. the UNIX and POSIX way to change
column width is to rename the column:
ps -o pid,user=CumbersomeUserNames -o comm
The easy way is to directly specify the desired width:
ps -o pid,user:19,comm
Reference:
http://www.freelists.org/post/procps/rhbz737215-ps-does-not-resolve-some-user-names
Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit restores the missing space between command
lines and the environment when the later is being
displayed. Below is a brief history of that elusive
character.
commit bb4f08ba29
Date: Thu Aug 11 07:42:14 2011 +1000
The ps program was altered for improved args/comm
compliance. At this time, the needed space was
present due to a buglet in the new library
read_unvectored function used by fill_cmdline_cvt.
commit a5881b5a4e
Date: Thu Dec 8 10:19:38 2011 -0600
The trailing space was eliminated so that the
file2strvec and fill_cmdline_cvt returned
command lines contained no trailing space.
However, this created a buglet when control group
hierarchies were displayed and the final cgroup
was empty.
This is also where the undetected ps buglet was
created.
commit c3a1239efe
Date: Sun Dec 11 12:00:50 2011 -0600
The control group anomaly was fixed but the impact
on ps args/environ was still not detected.
Signed-off-by: Jim Warner <james.warner@comcast.net>