aboutsummaryrefslogtreecommitdiffstats
path: root/include/fileutils.h
AgeCommit message (Collapse)AuthorFilesLines
2025-10-15lib/fileutils: add is_dotdir_dirent()Karel Zak1-0/+8
This code pattern is repeated on many places, let's move it to simple inline function. Signed-off-by: Karel Zak <kzak@redhat.com>
2025-09-01lib/canonicalize: introduce generic drop-permission callerKarel Zak1-1/+4
* add ul_restricted_path_oper() to fileutils.c * use it as backed for canonicalize_path_restricted() Signed-off-by: Karel Zak <kzak@redhat.com>
2024-07-02lib/fileutils: add ul_basename()Karel Zak1-0/+1
Unfortunately, the basename() function can be affected by the creativity of different libc authors, resulting in varying behavior across implementations. Instead, use a local implementation to ensure consistency and portability. Signed-off-by: Karel Zak <kzak@redhat.com>
2023-08-31libmount: fix statx() includesKarel Zak1-2/+2
Using sys/stat.h and linux/stat is too tricky.h together. It seems better to rely on libc and use sys/stat.h only. Users affected by old libc must update to use recent util-linux. Fixes: https://github.com/util-linux/util-linux/issues/2448 Signed-off-by: Karel Zak <kzak@redhat.com>
2023-08-03libmount: check for struct statxMarkus Mayer1-1/+1
Let's ensure struct statx exists before we try to use it. Checking for the existence of linux/stat.h is not sufficient. This is because [uapi/]linux/stat.h has existed since Linux 3.7, however struct statx was only introduced with Linux 4.11. The problem arises if one happens ot be using kernel headers from within the aforementioned range, such as Linux 4.9. CC libmount/src/la-utils.lo In file included from libmount/src/utils.c:31: ./include/fileutils.h:100:33: warning: declaration of 'struct statx' will not be visible outside of this function [-Wvisibility] unsigned int mask, struct statx *stx) ^ libmount/src/utils.c:117:16: error: variable has incomplete type 'struct statx' struct statx stx = { 0 }; ^ libmount/src/utils.c:117:10: note: forward declaration of 'struct statx' struct statx stx = { 0 }; ^ libmount/src/utils.c:125:5: error: use of undeclared identifier 'STATX_TYPE' STATX_TYPE ^ libmount/src/utils.c:126:8: error: use of undeclared identifier 'STATX_MODE' | STATX_MODE ^ libmount/src/utils.c:127:8: error: use of undeclared identifier 'STATX_INO' | STATX_INO, ^ 1 warning and 4 errors generated. make[4]: *** [Makefile:11269: libmount/src/la-utils.lo] Error 1 Checking for the presence of struct statx explicitly avoids this problem. Signed-off-by: Markus Mayer <mmayer@broadcom.com>
2023-03-02include/fileutils: remove duplicated includeKarel Zak1-1/+0
Signed-off-by: Karel Zak <kzak@redhat.com>
2023-02-14include: improve statx fallbackKarel Zak1-2/+2
Addresses: https://github.com/util-linux/util-linux/pull/2065 Signed-off-by: Karel Zak <kzak@redhat.com>
2023-02-13include: cleanup close_range fallbackKarel Zak1-3/+1
* let's use && operator rather than if-if * define HAVE_CLOSE_RANGE only if callback is really avalable Signed-off-by: Karel Zak <kzak@redhat.com>
2023-02-13include: add fallback for statxKarel Zak1-0/+14
Fixes: https://github.com/util-linux/util-linux/pull/2065 Signed-off-by: Karel Zak <kzak@redhat.com>
2023-01-12include: add missing license linesKarel Zak1-0/+4
The header files are usually based on code from lib/. This commit copies relevant license headers from lib/ to include/ to keep things consistent. The very generic things (e.g. MBR definitions) are always public domain. Fixes: https://github.com/util-linux/util-linux/issues/2010 Signed-off-by: Karel Zak <kzak@redhat.com>
2022-01-20libblkid: reopen floppy without O_NONBLOCKKarel Zak1-0/+3
Vladimir Sementsov-Ogievskiy wrote: > The commit "floppy: reintroduce O_NDELAY fix" was removed from kernel, > so we faced the bug described and discussed here: > https://bugzilla.suse.com/show_bug.cgi?id=3D1181018 > > Discussion in kernel list on reverting the commit: > https://www.spinics.net/lists/stable/msg493061.html > > In short, I can quote Jiri Kosina's comment: > > opening floppy device node with O_NONBLOCK is asking for all kinds > of trouble > > So opening floppy with O_NONBLOCK in blkid leads to failure of blkid, > probable failure of mount and unpleasant error messages in dmesg (see > also patch 02 for details). Based on patch from Vladimir. CC: Jiri Kosina <jkosina@suse.cz> Reported-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Tested-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Signed-off-by: Karel Zak <kzak@redhat.com>
2021-11-18Do not include sys/syscall.h if not availableDavid Engraf1-2/+2
Some platforms do not provide sys/syscall.h. The configure script already checks for the existance of the file. Include sys/syscall.h only in case HAVE_SYS_SYSCALL_H has been set.
2021-07-15login: add callback for close_range()Karel Zak1-2/+0
Let's make it more robust for old kernels where is no close_range() or in cases when it's unsuccessful. Reported-by: Florian Weimer <fweimer@redhat.com> Signed-off-by: Karel Zak <kzak@redhat.com>
2021-07-14login: fix close_range() useKarel Zak1-2/+2
This new syscall comes with three arguments (see kernel commit 278a5fbaed89dacd04e9d052f4594ffd0e0585de). Not sure why util-linux assume only two. Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1981729 Signed-off-by: Karel Zak <kzak@redhat.com>
2021-05-06lib/fileutils: close fd if fdopen is failedMasatake YAMATO1-1/+6
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
2021-01-28lib: use ul_prefix for close_all_fds() and mkdir_p()Karel Zak1-2/+2
The names are too generic and it makes problems in static libraries. Addresses: https://gitlab.com/cryptsetup/cryptsetup/-/issues/623 Signed-off-by: Karel Zak <kzak@redhat.com>
2020-12-03lib/fileutils: make close_all_fds() to be similar with close_range()Sami Kerola1-2/+4
The util-linux close_all_fds() serves the same purpose as close_range() that will over time obsolete local implementation completely. For upcoming few years it is best to have a fallback that uses same input arguments as the new system call. That allows surrounding code and variables not to be affected by version of mass file descriptor closing function. Proposed-by: Karel Zak <kzak@redhat.com> Reference: https://github.com/karelzak/util-linux/pull/1205#discussion_r534080128 Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-12-03login: use close_range() system call when possibleSami Kerola1-0/+14
This system call was added while back, but does not have glibc support yet. Lets try to use the new facility early on. The promise in reference link is that the new call is faster, and works when /proc is not mounted. Reference: https://lwn.net/Articles/789000/ Reference: https://lwn.net/Articles/789023/ Reference: https://kernelnewbies.org/LinuxChanges#Linux_5.9.New_close_range.28.29_system_call_for_easier_closing_of_file_descriptors Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2020-11-09ul_copy_file: make defines for return valuesEgor Chelak1-0/+2
Suggested-by: Sami Kerola <kerolasa@iki.fi> Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2020-11-09vipw: move copyfile to the libEgor Chelak1-0/+2
Also, a bug in pw_tmpfile was fixed: copyfile used tmp_file to report errors, but pw_tmpfile only assigned that variable _after_ calling copyfile. Suggested-by: Sami Kerola <kerolasa@iki.fi> Reviewed-by: Sami Kerola <kerolasa@iki.fi> Signed-off-by: Egor Chelak <egor.chelak@gmail.com>
2019-10-17lib/fileutils: add close_all_fds()Karel Zak1-0/+2
The classic way which is based on file-descriptors table size is pretty expensive (due to table size) and forces code to do many unnecessary close() calls. It seems better to use /proc/self/fds and close used descriptors only. Addresses: https://github.com/karelzak/util-linux/issues/883 Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-11lib/fileutils: add xreaddir()Karel Zak1-0/+15
Remove duplicate code and keep only one implementation in include/fileutils.h. Signed-off-by: Karel Zak <kzak@redhat.com>
2016-03-07build-sys: add and use openat build conditionalsRuediger Meier1-0/+2
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-03-07lib: move fopen_at() to fileutils.h, remove at.hRuediger Meier1-0/+10
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-29lib: provide fallback if mkostemp(3) missingRuediger Meier1-0/+2
It's missing on OSX. CC: Yuriy M. Kaminskiy <yumkam@gmail.com> Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2015-11-22include/fileutils: add is_same_inode() checkSami Kerola1-0/+12
Check if a file descriptor and path or stat structure are represent the same file. This function is needed for TACTOU avoidance. Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-08-24chsh, chfn, vipw: fix filenames collisionKarel Zak1-3/+3
The utils when compiled WITHOUT libuser then mkostemp()ing "/etc/%s.XXXXXX" where the filename prefix is argv[0] basename. An attacker could repeatedly execute the util with modified argv[0] and after many many attempts mkostemp() may generate suffix which makes sense. The result maybe temporary file with name like rc.status ld.so.preload or krb5.keytab, etc. Note that distros usually use libuser based ch{sh,fn} or stuff from shadow-utils. It's probably very minor security bug. Addresses: CVE-2015-5224 Signed-off-by: Karel Zak <kzak@redhat.com>
2015-06-08lib/fileutils: Add new dup_fd_cloexec functionGuillem Jover1-0/+1
This function duplicates and marks a file descriptor as close-on-exec. Takes care of build and run-time support for the fcntl F_DUPFD_CLOEXEC command, and other errors. Signed-off-by: Guillem Jover <guillem@hadrons.org>
2014-12-05libmount: add new libmnt_monitor APIKarel Zak1-0/+6
It's usually enough to us [e]poll() to monitor kernel mount table, but there is no way how to monitor changes in userspace mount options (e.g. _netdev). The management of these mount options is completely hidden in libmount and /rub/mount/utab is private libmount file. This patch introduces new libmnt_mount API to monitor also userspace mount table. Signed-off-by: Karel Zak <kzak@redhat.com>
2014-06-18lib/fileutils: xalloc stuff in library-like codeKarel Zak1-2/+3
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-06-09lib/fileutils: add stripoff_last_component() from libmountKarel Zak1-0/+1
Signed-off-by: Karel Zak <kzak@redhat.com>
2014-06-09lib/fileutils: add mkdir_p() from libmountKarel Zak1-0/+2
2013-04-03include/fileutils: use O_CLOEXEC for mktemp stuffKarel Zak1-1/+1
Signed-off-by: Karel Zak <kzak@redhat.com>
2012-06-02fileutils: xmkstemp() interface changeDave Reisner1-3/+3
We can not let the user control where TMPDIR is for this tempfile. This will be where we write the updated passwd file, and must be capable of being moved atomically with rename(2). Therefore, it cannot be on a different device, or setpwnam() and vipw/vigr programs will invariably fail with EXDEV. Signed-off-by: Dave Reisner <dreisner@archlinux.org> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2012-04-23lib/fileutils: add get_fd_tabsize()Karel Zak1-1/+4
as a fallback for the function getdtablesize() Signed-off-by: Karel Zak <kzak@redhat.com>
2012-03-18fileutils: differentiate xmkstemp and xfmkstempSami Kerola1-1/+15
Let developer to choose, case by case, what sort of return value is best in her code. The xmkstemp() is for users who want file descriptor as return value of the function, xfmkstemp() will return FILE pointer. Proposed-By: Karel Zak <kzak@redhat.com> CC: Davidlohr Bueso <dave@gnu.org> Reference: http://marc.info/?l=util-linux-ng&m=133129570124003&w=2 Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2012-03-18lib: add fileutils function collectionSami Kerola1-0/+6
The fileutils contains xmkstemp function will create temporary file safe and reusable manner. Reference: http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO.html#TEMPORARY-FILES CC: Davidlohr Bueso <dave@gnu.org> Signed-off-by: Sami Kerola <kerolasa@iki.fi>