Craig Small [Wed, 2 Nov 2022 11:18:44 +0000 (22:18 +1100)]
docs: Add note about fuser and mount namespaces
fuser won't match block devices that are mounted by
processes in a different mount namespace. This is
because the file descriptor will be from the process
namespace, not the "viewer" (e.g. fuser) namespace.
Craig Small [Mon, 18 Jul 2022 10:16:42 +0000 (20:16 +1000)]
killall: use kill if pidfd_send_signal() fails
The pidfd_send_signal() system call appeared in Linux 5.1
If psmisc is build on a system before then, or a non-Linux
system, then kill() is used instead. However if psmisc is
built on a Linux >= 5.1 system but run on a < 5.1 Linux
system the system call fails and killall doesn't work.
The fix, as proposed by Peter T. Breuer, is to try
pidfd_send_signal() and if the return value is < 0 and
errno is ENOSYS then we know at runtime the system call
failed and we fall through to trusty old kill().
Note, this means that killall on systems below 5.1 still
have the race PID condition that the pidfd calls fix.
Craig Small [Fri, 18 Mar 2022 21:51:22 +0000 (08:51 +1100)]
fuser: Do not path check sockets
The referenced commit fixed issue #10 where NFS servers have the same ID
and fuser could get confused. That fix was to do a path compare between
the user requested path and the path of the mount.
Block devices were skipped, because /dev/sda2 is not the same as /home
string-wise, even if sda2 was mounted as /home
The new issue is, sockets fail to match when using a mount point. That
is because "socket:[1547]" is not the same as
"/home/user/.gnupg/S.gpg-agent" string-wise, even if named socket 1547
uses that path.
The fix is to skip the path checks when looking at sockets. I'm not sure
if that means we will have confusion with named sockets that have a path
on NFS shares or not but I'm not sure there is a fix for those.
Craig Small [Thu, 17 Mar 2022 06:52:35 +0000 (17:52 +1100)]
killall: use openat and pidfd_send_signal
Using openat and pidfd_send_signal by openig the pid directory
at the start means we cannot have the situation where the PID
has been reused and we filter/kill the wrong process.
In other words, is the open(/proc/1234) the same process as
kill(1234) ? Using pidfd we can be sure.
While the current fallback method might obtain the correct AppArmor
context by checking /proc/self/attr/current, it is not guaranteed that
this value will be the context attributed by AppArmor. The current
interface being used upstream is /proc/self/attr/apparmor/current, and
that can be obtained by using the AppArmor library functions.
In order to avoid link time dependencies, we are loading the apparmor
library dynamically, just like is currently done by SELinux.
The security context code was intermingled with SELinux specific code.
This change refactors it out into its specific function.
There was also some whitespace fixes.
Craig Small [Mon, 21 Jun 2021 12:19:44 +0000 (22:19 +1000)]
pstree: Don't stop compact with pgids
Showing pgids used to disable compaction. But, despite the vague
NPTL documentation, its not possible to have different PGID in the
same thread group, so lack of compaction is not required.
With setpgid(), the last thread in the process that calls it
wins. There might be a debate about should this be the right way
but that's a kernel thing.
References:
#34
Test program https://gitlab.com/-/snippets/2094161
https://man7.org/linux/man-pages/man7/nptl.7.html
Craig Small [Mon, 21 Jun 2021 12:11:36 +0000 (22:11 +1000)]
pstree: check pid with show parents
If the -s option was used then we didn't check the return of
find_proc(). This meant if you used a pid that was for no process
it returned NULL and the whole tree was shown.
pstree checks for find_proc() returning NULL and errors out.
Craig Small [Fri, 5 Feb 2021 23:58:54 +0000 (10:58 +1100)]
build-sys: Don't require po4a for installation
The build system tested for the presence of po4a binary at the
install step. psmisc ships with translated man pages so doesn't
need po4a for install/uninstallation.
Craig Small [Thu, 28 Jan 2021 11:32:41 +0000 (22:32 +1100)]
build-sys: Ignore c files in temp directory
If the build system had a temporary psmisc-* directory
and update-potfiles was run, then the temporary files were included
in the list, causing a lot of problems later.
Craig Small [Tue, 5 Jan 2021 00:40:36 +0000 (11:40 +1100)]
pstree: Dynamically link to SELinux and expand -Z
pstree will dynamically link to libselinux if available.
The -Z flag now looks the same as ps -Z and uses SELinux contexts
if available or whatever is in /proc/PID/attr/current otherwise.
This brings the pstree output the same as ps, in fact I lifted
the code from ps/output.c
Craig Small [Tue, 27 Oct 2020 10:59:25 +0000 (21:59 +1100)]
fuser: Check pathname only on non-block devices
The referenced commit we would check the pathname to
ensure it matched our target. This worked fine for
real files. However for block devices it would fail
because "/dev/sda1" doesn't match "/mnt/myfile".
We only check the pathname if the thing we are matching
against is not a block file.
Thanks to @MarsChan for the report and also the suggested
fix!
Craig Small [Fri, 22 May 2020 06:21:10 +0000 (16:21 +1000)]
fuser: Less confused about duplicate dev_id
NFS mounts from the same server have the same device ID. This means
using the -m option a process using one of those mounts will be
"found" in all of the others too.
lsof doesn't have this confusion as it checks the real path against
the mount point and only matches if they start the same.
I think it would be confused with double stacked NFS shares such
as /nfs/SHARE1/blah/SHARE2 with the open file in SHARE2 but
there are limits.
Craig Small [Mon, 2 Mar 2020 11:00:25 +0000 (22:00 +1100)]
pstree: minor snprintf fix
The referenced commit used size_t as a return value for
snprintf.
Coverity found the negative check against size_t but the real
problem was using size_t in the first place as an error
returned by snprintf would never be detected.
Craig Small [Mon, 2 Mar 2020 10:56:02 +0000 (21:56 +1100)]
killall: minor str length changes
reworked some of the string handling to check for strchr and
strrchr return values. Removed check for unsigned to be negative,
that's not going to happen!