AntonMoryakov [Tue, 14 Jan 2025 15:06:49 +0000 (18:06 +0300)]
sys-utils: fix add NULL check for mnt_fs_get_target return value
The static analyzer flagged a potential issue: the return value of
mnt_fs_get_target(fs) could be NULL, but it was dereferenced without
a check. This could lead to undefined behavior.
Added a NULL check before using the tgt pointer. If tgt is NULL,
the current iteration is skipped.
ChanChanges:
- Added if (!tgt) check before using tgt.
Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
Mark Harfouche [Mon, 13 Jan 2025 23:07:20 +0000 (18:07 -0500)]
Include errno.h within lsfd.c
This is needed for ENOSYS to be defined
libgcc: 14.2.0
```
2025-01-13T22:59:57.1752408Z misc-utils/lsfd.c: In function 'kcmp':
2025-01-13T22:59:57.1762407Z misc-utils/lsfd.c:66:9: error: 'errno' undeclared (first use in this function)
2025-01-13T22:59:57.1762947Z 66 | errno = ENOSYS;
2025-01-13T22:59:57.1770931Z | ^~~~~
2025-01-13T22:59:57.1771263Z misc-utils/lsfd.c:40:1: note: 'errno' is defined in header '<errno.h>'; did you forget to '#include <errno.h>'?
2025-01-13T22:59:57.1771555Z 39 | #include <sys/syscall.h>
2025-01-13T22:59:57.1771767Z +++ |+#include <errno.h>
2025-01-13T22:59:57.1771955Z 40 |
2025-01-13T22:59:57.1772231Z misc-utils/lsfd.c:66:9: note: each undeclared identifier is reported only once for each function it appears in
2025-01-13T22:59:57.1772507Z 66 | errno = ENOSYS;
2025-01-13T22:59:57.1772684Z | ^~~~~
2025-01-13T22:59:57.1779860Z misc-utils/lsfd.c:66:17: error: 'ENOSYS' undeclared (first use in this function)
2025-01-13T22:59:57.1780285Z 66 | errno = ENOSYS;
2025-01-13T22:59:57.1780499Z | ^~~~~~
2025-01-13T22:59:57.2204441Z CC misc-utils/lsfd-lsfd-decode-file-flags.o
2025-01-13T22:59:57.2803898Z make[2]: *** [Makefile:13624: misc-utils/lsfd-lsfd.o]
```
When login is almost done it chdir()'s into the user's home directory.
It currently uses pwd->pw_dir, i.e the home dir actually assigned to the
user.
OTOH it explicitly allows overriding HOME (see comment in
init_environ()), and explicitly imports PAM's env vars (which quite
possibly contain $HOME) into its env block.
This patch makes sure that HOME is *properly* overridable via the env
var and via PAM: it actually honours it for the chdir().
(Background: I am working on a PAM module that under some conditions
would like to redirect the home dir to some other dir, and while I can
nicely set $HOME from the PAM module, it has not as much as effect as I
wish, because whie the rest of userspace respects it login so far didn't
and leaves the user in the wrong dir).
Karel Zak [Mon, 13 Jan 2025 11:06:23 +0000 (12:06 +0100)]
hardlink: fix memory corruption in read buffers
The size of the eq->buf_a and eq->buf_b buffers depends on the
readsize setting. This setting is modified by ul_fileeq_set_size(), so
the buffers need to be resized accordingly. Deallocating is
sufficient, as they will be allocated later with the correct size.
Addresses: https://github.com/util-linux/util-linux/issues/3330 Signed-off-by: Karel Zak <kzak@redhat.com>
Samuel Thibault [Sun, 12 Jan 2025 15:39:44 +0000 (16:39 +0100)]
Fix non-Linux build
This fixes non-Linux builds, by:
- making sfdisk discard option conditioned by availability of BLKDISCARD
- defining and using blkid_probe_get_buffer only if O_DIRECT is
available
- always building src/fs_statmount.c and src/tab_listmount.c, they
already contain proper conditions to make them void if support is not
available.
Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Karel Zak [Tue, 3 Dec 2024 17:56:41 +0000 (18:56 +0100)]
libmount: read all types of kernel messages
Previously, libmount only read error messages from fsopen() file
descriptor. This commit improves the library to read all messages
and keep them in their original format ("<type> <mesg>") in the library mount context.
Applications can now read all messages by using mnt_context_get_mesgs().
Furthermore, private functions have been implemented to include new
library-specific messages in the log. Currently, these messages are
only managed in mnt_context_get_excode(), but it would be beneficial
to relocate them to the appropriate locations where errors are
triggered.
In the future, mnt_context_get_excode() should only be utilized by
basic applications that require an one error message. For more
critical purposes (e.g. mount(8)), it will be recommended to utilize
the messages array.
The public function mnt_context_get_excode() has been modified to use
the new functionality and provide messages in a backwardly compatible
way.
Karel Zak [Mon, 11 Nov 2024 14:05:59 +0000 (15:05 +0100)]
findmnt: improve --help output
The current help output is quite lengthy. It would be beneficial to
organize it into sections, such as data sources, data filters, and
options. This would make it easier for users to navigate and
understand the available features.
Karel Zak [Mon, 11 Nov 2024 13:52:53 +0000 (14:52 +0100)]
findmnt: improve reliability of match testing
The "match" is defined if a string or void data is defined for the
matching function. It is necessary to test both variants instead of
relying solely on get_match(), which only returns string data.
This patch introduces the is_defined_match() macro to hide this detail
in the code and make it more readable.
Karel Zak [Mon, 23 Sep 2024 14:39:24 +0000 (16:39 +0200)]
libmount: improve fs->stmnt_done mask use
It is possible that one field in the libmnt_fs struct requires
fetching multiple statmount fields using multiple STATMOUNT_* mask
items. This requires changes to the way the fs->stmnt_done mask is
used.
Karel Zak [Thu, 15 Aug 2024 08:26:31 +0000 (10:26 +0200)]
libmount: add support for listmount()
The new listmount() syscall returns a list of unique mount IDs (just
uint64_t per node, nothing else). It makes it very fast and efficient.
* libmount supports two scenarios:
- fetch the whole mount table by mnt_table_fetch_listmount(); this is
an alternative to mnt_table_parse_file()
- on demand; this mode is an extension to the current functionality,
when enabled by mnt_table_enable_listmount(), then mnt_table_next_fs()
will ask the kernel for data by listmount.
If mnt_table_next_fs() iterates on the mount table in reverse order
(MNT_ITER_BACKWARD), then it reads mount nodes from the kernel in
reverse order too.
The advantage of the on-demand mode is that on machines with a huge
mount table (thousands of nodes), we can work with only a subset of
the table (usually the last few nodes with the most recently
mounted filesystems), and the kernel does not have to compose a complete
huge table. This should be an improvement over the mountinfo file.
The default is to read 512 nodes (IDs) by one listmount() call. This
size can be altered by mnt_table_listmount_set_stepsiz(). The default
size should be large enough for usual Linux machines.
It's also possible to set a sub-tree by mnt_table_listmount_set_id()
and a namespace by mnt_table_listmount_set_ns().
If libmnt_statmnt (on-demand statmount()) is assigned to the table,
then all filesystems in the table are automatically assigned to this
statmount() setup too. This allows for a completely on-demand
scenario.
while (mnt_table_next_fs(tb, itr, &fs) == 0) {
if (strcmp("vfat", mnt_fs_get_fstype(fs)) == 0)
print("%s", mnt_fs_get_fs_options(fs));
}
In this example, mnt_table_next_fs() serves as the frontend for
listmount() and mnt_fs_get_...() serves as the frontend for
statmount(). The fs-options are read from kernel only for "vfat"
filesystems.
Karel Zak [Wed, 14 Aug 2024 15:10:45 +0000 (17:10 +0200)]
libmount: add support for statmount()
* introduce libmnt_statmnt object to store statmount mask and buffer
(etc.)
* add API for libmnt_fs to retrieve data from the kernel using
statmount()
* support on-demand statmount() from old mnt_fs_get_...() functions
* allow libmnt_statmnt to be shared and reused between libmnt_fs
instances
* allow libmnt_statmnt to be assigned and used for filesystems in the
table (assign libmnt_statmnt to the table to make it usable for all
filesystems).
* allow temporary disabling of fetching data from the kernel to avoid
unwanted recursion in certain use-cases
* support namespaces for statmount() based on the libmnt_fs namespace
ID setting
* allow user-defined statmount masks to overwrite the default
Karel Zak [Tue, 11 Jun 2024 09:55:39 +0000 (11:55 +0200)]
libmount: Add API to get/set unique IDs
Since the Linux kernel version 6.8, there are two types of IDs
available: the "old" ID used in /proc/self/mountinfo, and a new 64-bit
unique ID that is never recycled. This new ID is provided by the
statx(STATX_MNT_ID_UNIQUE) and statmount() syscalls.
Note that this patch only adds the API for retrieving these unique
IDs, but the backing code has not been implemented yet.
Karel Zak [Mon, 22 Jul 2024 12:12:37 +0000 (14:12 +0200)]
libmount: add mount-api-utils.h to mountP.h
The new syscalls (statmount and fd-based mount) are already being
utilized in various areas within the library. Therefore, it would be
more efficient to include the syscalls header file in the private main
library header file.
Karel Zak [Wed, 14 Aug 2024 13:33:44 +0000 (15:33 +0200)]
include/mount-api-utils: add statmount and listmount
* Make the includes and ifdefs in the file more generic, to cover
other interfaces besides just HAVE_MOUNTFD_API.
* Add #ifdef HAVE_STATMOUNT_API to the header file.
* Add fallbacks for statmount and listmount masks and flags.
* Add a local definition of the structs mnt_id_req and statmount.
We do not want to depend on kernel headers for now, as all of this is
still under development. Installed headers may provide old versions,
and a solution based on #ifdef will still require local definition of
the structs.
* Add wrappers for the statmount() and listmount() syscalls.
* Add sys_statmount(), a wrapper for statmount() that reallocates the
statmount buffer (on EOVERFLOW errno) if it is not large enough.
Note that for now, we are directly using syscalls and do not require
libc support for statmount() and listmount(). The kernel API is very
extensible, as it is based on structs rather than syscall arguments.
Therefore, I doubt we will see any libc support in the near future.
The code uses the "ul_" prefix for locally defined variables and
functions to prevent conflicts with kernel headers and potential
future versions of libc.
Karel Zak [Wed, 8 Jan 2025 11:40:30 +0000 (12:40 +0100)]
Merge branch 'revise--test-cases' of https://github.com/masatake/util-linux
* 'revise--test-cases' of https://github.com/masatake/util-linux:
tests: (lsfd::mkfds_vsock) skip if diag socket for AF_VSOCK is unavailable
tests: (test_mkfds::sockdiag) support AF_VSOCK family
tests: (lsns::filter) skip if /proc/self/uid_map is not writable
Sam James [Wed, 8 Jan 2025 06:49:40 +0000 (06:49 +0000)]
meson: add missing `is_disabler` checks
Noticed this when looking at why the `check` target isn't created
with `-Dauto_features=disabled`. This doesn't fix that but it does
fix a bunch of issues I noticed along the way after a hint from Eli Schwartz.
Karel Zak [Fri, 3 Jan 2025 09:48:25 +0000 (10:48 +0100)]
Merge branch 'lsfd--vsock' of https://github.com/masatake/util-linux
* 'lsfd--vsock' of https://github.com/masatake/util-linux:
lsfd: support AF_VSOCK sockets
tests: (lsfd-functions.bash) add a missing constant
tests: (lsfd) quote '$' in patterns in a case/esac block
Use gtk-doc constructs to mark up formatted sections of documentation
so that they display properly in the rendered output.
- Enclose code samples with informalexample+programlisting tags
- Enclose preformatted blocks of text (tables) in same
- Present numbered lists in markdown format (leading 1., 2., etc.)
so that they're translated into HTML ordered lists
Karel Zak [Thu, 26 Dec 2024 11:06:08 +0000 (12:06 +0100)]
Merge branch 'lsfd--hyperlinks' of https://github.com/masatake/util-linux
* 'lsfd--hyperlinks' of https://github.com/masatake/util-linux:
lsfd: don't enable hyperlinks for deleted files
lsfd: enable hyperlinks only for regular files and directories
lsfd: add --hyperlink command line option
tests: (test_sysinfo) add a helper to call xgethostname
lsfd: consolidate add_column()
Karel Zak [Thu, 26 Dec 2024 10:34:10 +0000 (11:34 +0100)]
fstab.5 mount.8: add note about field separator
The value of the mount option X-mount.idmap= can contain spaces as
separators between mappings. Unfortunately, this conflicts with the
fstab format where fields are separated by spaces. Using quotes does
not solve this issue because fields are separated before they are
parsed.
This limitation of the fstab parser within libmount is due to backward
compatibility. The fstab format is a de-facto standard and is parsed
by many tools and libraries, including third-party shell scripts. It
would not be practical to introduce any improvements in libmount that
would make fstabs unparseable in other places.
Users must understand that in-field spaces must be escaped by \040.
For example, X-mount.idmap="0:0:1\040500:1000:1".
Addresses: https://github.com/util-linux/util-linux/issues/2829 Signed-off-by: Karel Zak <kzak@redhat.com>
LiviaMedeiros [Mon, 16 Dec 2024 10:40:37 +0000 (11:40 +0100)]
tests: add skips when IPv6 is not supported
Before this change on systems with disabled IPv6 (e.g. `CONFIG_IPV6 is
not set` in kernel config), `lsfd/mkfds-tcp6` and `lsfd/mkfds-udp6` were
failing, and `lsfd/option-inet` was hanging forever, making it
impossible to complete the tests.
Karel Zak [Mon, 2 Dec 2024 11:50:29 +0000 (12:50 +0100)]
lsfd: consolidate add_column()
* use only column IDs in add_column() callers
* don't work with "struct colinfo" in callers
* rename add_column_by_id() to add_hidden_column() as it's very
special function used only by filters