Commit graph

10282 commits

Author SHA1 Message Date
Bojan Novković
aa05d1b2ef db/hash.c: Fix flag check in hash_seq
Fixes:	3a686b851f
2025-08-01 11:33:00 +02:00
Bojan Novković
3a686b851f dbm_nextkey: Always return an error if we've reached the end of the database
POSIX.1 states that `dbm_nextkey` must return an invalid key
(i.e., `key.dptr == NULL`) after the end of the database was reached.
The current implementation of `hash_seq` will incorrectly restart
the key sequence after the end of the database is reached.

Fix this by checking the "current bucket" index when R_NEXT is passed.

Sponsored by:   Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D51635
Reviewed by:	markj
2025-07-31 17:27:24 +02:00
Bojan Novković
14598537ac db/hash.c: Allow O_WRONLY in dbm_open
The dbm(3) manpage explicitly states that O_WRONLY is not allowed in
dbm_open, but a more recent comment in ` __hash_open` suggests otherwise.
Furthermore, POSIX.1 allows O_WRONLY in dbm_open and states
that the underlying file must be opened for both reading and writing.

Fix this by correcting the O_WRONLY check and moving it further into
the function to make sure that the original flags are stored in hashp.

Sponsored by:   Klara, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D51514
2025-07-30 11:31:16 +02:00
Ed Maste
6924e4211b memchr.3: Operates on objects, not strings
Suggested by:	avg
Reviewed by:	avg
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D51523
2025-07-25 09:48:42 -04:00
Bojan Novković
e1e77dd23c dbm: Add tests for dbm_open
Sponsored by:   Klara, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D51492
2025-07-25 11:23:00 +02:00
Bojan Novković
108e2d1137 db/hash.c: Do not return an error when opening a missing database with O_CREAT
dbm_open currently returns an error when opening a missing database
with O_CREAT but creates the request database file. This is caused
by a buggy check in __hash_open which attempts to detect a new
(or empty) database file, but fails to take the O_CREAT flag into
account.

Fix this by expanding the check to include O_CREAT.

Fixes:	edcdc752ec
Sponsored by:	Klara, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D51491
2025-07-25 11:22:44 +02:00
Dag-Erling Smørgrav
9792103753 libc: Remove unused pselect() implementation.
This hasn't been needed since FreeBSD 5.0.

Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D51434
2025-07-24 16:01:09 +02:00
Dag-Erling Smørgrav
cd0756cef2 libc: Disable debugging code in the resolver.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D37319
2025-07-24 16:01:00 +02:00
Konstantin Belousov
441995e161 memchr.3: mention the len argument in the functions description
Otherwise, its use is only specified in the error case, which is
somewhat obscure.  Also properly typeset NULL.

Reviewed by:	pauamma_gundo.com
Discussed with:	emaste
Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
Differential revision:	https://reviews.freebsd.org/D51424
2025-07-22 18:11:40 +03:00
Dag-Erling Smørgrav
57ee56578c libc: Test time zone change detection.
While here, clean the detection code up a bit.

Sponsored by:	Klara, Inc.
Sponsored by:	NetApp, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D51343
2025-07-18 19:49:57 +02:00
Ricardo Branco
8768b60de1 lib: Fix calls that naively set F_SETFD.
With the recent inclusion of the FD_CLOFORK and FD_RESOLVE_BENEATH flags,
we must avoid clearing them when setting only FD_CLOEXEC.

Signed-off-by: Ricardo Branco <rbranco@suse.de>

Reviewed by:	kib, markj
MFC after:	1 month
Pull Request:	https://github.com/freebsd/freebsd-src/pull/1766
2025-07-17 17:00:32 +00:00
Timothy Pearson
8efa35fea3 libc/powerpc64: Fix swapcontext(3)
On PowerPC platforms a valid link to the Table of Contents (TOC) is
required for PLT lookups to function.  This TOC pointer is stored in
a dedicated register, and is used along with the stack pointer by both
C prologue and PLT lookup code.

When calling swapcontext() with uc_link != NULL, a PLT lookup to
setcontext(3) is attempted from within the _ctx_done context.  The
exiting process has usually trashed both r1 and r2 at this point,
leading to a crash within the PLT lookup before setcontext(2) is
reached to restore the linked context.

Save and restore r2 as in a regular function.  This ensures the
subsequent PLT lookup to setcontext(3) succeeds.

Signed-off-by: Timothy Pearson <tpearson@raptorengineering.com>

MFC after:	1 week
Pull Request:	https://github.com/freebsd/freebsd-src/pull/1759
2025-07-13 14:00:56 -04:00
Dag-Erling Smørgrav
920b218391 readdir: Fix error check.
Now that dd_size is unsigned, we need to check if the return value from
getdirentries() was negative before assigning it to dd_size.

While here, simplify the scandir_error test case slightly, and verify
that calling readdir() again after EOF still returns NULL.

Fixes:		42e613018d
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D51266
2025-07-11 22:43:32 +02:00
Jessica Clarke
2c444fdb0c libc,libthr: Remove __pthread_distribute_static_tls
This private API is no longer used by rtld-elf so can be removed.

Reviewed by:	kib
Differential Revision:	https://reviews.freebsd.org/D50921
2025-07-10 20:00:28 +01:00
Dag-Erling Smørgrav
42e613018d opendir, readdir, telldir: Use the correct types.
Use either size_t or off_t (as appropriate) instead of long.

Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D51210
2025-07-09 22:34:22 +02:00
Dag-Erling Smørgrav
dd81cc2bc5 getdirentries: Return ENOTDIR if not a directory.
This is both more logical and more useful than EINVAL.

While here, also check for VBAD and return EBADF in that case.  This can
happen if the underlying filesystem got forcibly unmounted after the
directory was opened.  Previously, this would also have returned EINVAL,
which wasn't right but wasn't wrong either; however, ENOTDIR would not
be appropriate.

MFC after:	never
Sponsored by:	Klara, Inc.
Reviewed by:	kevans, kib
Differential Revision:	https://reviews.freebsd.org/D51209
2025-07-09 22:34:18 +02:00
Dag-Erling Smørgrav
ccf937320a libc: Finish removing fscandir{,_b}().
These only existed for a few days before being renamed, so there's no
reason to continue to carry compatibility shims for them.

Fixes:		deeebfdeca
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D50981
2025-07-09 21:17:06 +02:00
Dag-Erling Smørgrav
2187622436 fts: Fix option list indentation.
Fixes:		da2025a0e8
Sponsored by:	Klara, Inc.
Reviewed by:	bcr
Differential Revision:	https://reviews.freebsd.org/D51208
2025-07-09 15:20:08 +02:00
Kenny Levinsen
da9e73e5d4 wordexp(3): Handle ECHILD from waitpid
If the calling process has used SIG_IGN as handler or set the
SA_NOCLDWAIT flag for SIGCHLD, processes will be automatically reaped on
exit and calls to waitpid(3) will therefore fail with ECHILD.

We waitpid primarily to reap our child so that the caller does not have
to worry about it. ECHILD indicates that there is no child to reap, so
we can just treat that as a success and move on.

Signed-off-by: Kenny Levinsen <kl@kl.wtf>
Tested by: Jan Beich
Pull Request: https://github.com/freebsd/freebsd-src/pull/1675
2025-07-08 23:27:33 +02:00
Dag-Erling Smørgrav
c08e019c6c opendir, fdopendir: Add tests, clean up.
* Add test cases for opendir() and fdopendir().
* Drop O_NONBLOCK from opendir(); it was added a long time ago to avoid
  blocking if given a closed named pipe, but now we use O_DIRECTORY,
  which ensures that we get ENOTDIR in that case.
* While here, remove unused #includes left over from the split.

Sponsored by:	Klara, Inc.
Reviewed by:	kevans, markj
Differential Revision:	https://reviews.freebsd.org/D51126
2025-07-08 21:41:21 +02:00
Konstantin Belousov
72c848089f libc: only append exterr herald and text if exterr was recorded
__uexterr_format: return empty string on no error, instead of "No error"
err(): check for the first symbol of the exterr to decide if there is
anything to print.

Requested and reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
Differential revision:	https://reviews.freebsd.org/D51204
2025-07-08 18:55:54 +03:00
Ricardo Branco
9911028f53 libc: mkostemp(3) should support O_CLOFORK
Reviewed by:	kib, markj
MFC after:	1 month
Pull Request:	https://github.com/freebsd/freebsd-src/pull/1758
2025-07-08 15:43:39 +00:00
Konstantin Belousov
01a679715f err(3): print extended error if available
Reviewed by:	asomers, brooks
Sponsored by:	The FreeBSD Foundation
Differential revision:	https://reviews.freebsd.org/D51141
2025-07-07 21:44:22 +03:00
Konstantin Belousov
283c1bd8ce uexterr_format(): simplify output when ext error string is available
If the extended error string is provided by kernel, return only the
string, which is supposedly enough to identify exact cause of the error.
If the string is not provided, print the technically looking gibberish
which still allows to identify location with kernel sources.

Reviewed by:	asomers, brooks
Sponsored by:	The FreeBSD Foundation
Differential revision:	https://reviews.freebsd.org/D51141
2025-07-07 21:44:14 +03:00
Ricardo Branco
efadb5c2a9 Add manpages for O_CLOFORK flag and others
Reviewed by:	kib
MFC after:	1 month
Pull Request:	https://github.com/freebsd/freebsd-src/pull/1698
2025-07-06 23:08:41 +00:00
Ricardo Branco
971f738679 kern: Make dup3() support O_CLOFORK
Reviewed by:	kib
MFC after:	1 month
Pull Request:	https://github.com/freebsd/freebsd-src/pull/1698
2025-07-06 23:08:37 +00:00
Mark Johnston
245ff4c4b2 libc: Add inotify support
inotify_init() and inotify_init1() are implemented using __specialfd(2).
inotify_add_watch() is implemented in terms of inotify_add_watch_at(2).

Reviewed by:	kib
MFC after:	3 months
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D50315
2025-07-04 14:42:33 +00:00
Dag-Erling Smørgrav
5e96f4006d opendir: Simplify is_unionstack().
Sponsored by:	Klara, Inc.
Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D51118
2025-07-02 12:22:29 +02:00
Dag-Erling Smørgrav
8d02b7190d fts: Add test cases for unreadable directories.
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D51098
2025-07-02 12:22:29 +02:00
Dag-Erling Smørgrav
bc624c9735 fts: Clarify what FTS_DNR is used for.
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D51097
2025-07-02 12:22:29 +02:00
Dag-Erling Smørgrav
8ebc076866 scandir: Code cleanup.
Sponsored by:	Klara, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D51050
2025-06-26 09:37:06 +02:00
Dag-Erling Smørgrav
62e0f12f51 scandir: Propagate errors from readdir().
Currently, if `readdir()` fails, `scandir()` simply returns a partial
result (or a null result if it fails before any entries were selected).
There is no way within the current API design to return both a partial
result and an error indicator, so err on the side of caution: if an
error occurs, discard any partial result and return the error instead.

MFC after:	1 week
Reported by:	Maxim Suhanov <dfirblog@gmail.com>
Sponsored by:	Klara, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D51046
2025-06-26 09:37:00 +02:00
Konstantin Belousov
6458400813 csu: C standard requires errno on main() entry
Reported by:	Greg Becker <greg.becker@klarasystems.com>
Reviewed by:	emaste, kevans, markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Also see:	https://reviews.freebsd.org/D50998
Differential revision:	https://reviews.freebsd.org/D50997
2025-06-24 04:11:17 +03:00
Dag-Erling Smørgrav
0a5b763d98 libc: Rename fscandir{,_b}() to fdscandir{,_b}().
This seems to fit the pattern better (e.g. fdopendir()).

I've added weak references to ease the transition, but since it's only
been a few days, we can remove them (and the ObsoleteFiles entries for
the manual pages) before we branch stable/15.

Fixes:		deeebfdeca
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D50980
2025-06-23 15:03:10 +02:00
Bojan Novković
b2ac169802 glob: Delete trailing whitespace in 'struct glob_limit' definition
Sponsored by: Klara, Inc.
2025-06-23 11:58:41 +02:00
Bojan Novković
66d780ab1d glob.3: Fix punctuation mistakes
Fixes: 1e0743f54d
Sponsored by: Klara, Inc.
2025-06-23 11:34:45 +02:00
Dag-Erling Smørgrav
7fe39bec80 glob: Improve callback tests.
Most importantly, they need to run without privileges, since root is
allowed to read a directory regardless of its permission bits.

PR:		287694
Fixes:		4d7c31bca2
Sponsored by:	Klara, Inc.
Reviewed by:	bnovkov
Differential Revision:	https://reviews.freebsd.org/D50965
2025-06-23 10:44:31 +02:00
Gleb Smirnoff
220cdd1b39 libc/rpc/svc_nl: plg a memory leak
CID:	1591126
2025-06-20 15:37:47 -07:00
Dag-Erling Smørgrav
c187b673bb scandir: Fix behavior when no entries match.
In the previous commit, I removed the initial initialization of the
`names` array, not realizing that `scandir()` is expected to return
a non-null (but empty) array of entries if no entries matched.
Restore the historical behavior, document it, and add a test.

Fixes:		deeebfdeca
Sponsored by:	Klara, Inc.
Reviewed by:	kevans, allanjude, markj
Differential Revision:	https://reviews.freebsd.org/D50949
2025-06-20 18:13:48 +02:00
Dag-Erling Smørgrav
deeebfdeca libc: Add fscandir(), fscandir_b(), scandirat_b().
While here, clean up scandir() a bit and improve the documentation.

MFC after:	never
Sponsored by:	Klara, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D50935
2025-06-20 13:10:23 +02:00
Sergey A. Osokin
22c7815118 exec(3): add missing execvpe(3) to MLINKS
Reviewed by:	glebius
2025-06-18 17:40:22 -04:00
Alexander Ziaee
1b6936e125
manuals: Align our tree with upstream C23 macro
Switch all instances of the -isoC-2024 macro to -isoC-2023 selected by
upstream. Keep -isoC-2024 defined, but deprecated, for backwards compat.

MFC after:      3 days (I will resolve merge conflicts with cdefs)
2025-06-12 17:24:10 -04:00
Ricardo Branco
b0334450aa include: ssp: fortify <signal.h>
sig2str(3)

Reviewed by: imp, kib, des, jilles
Pull Request: https://github.com/freebsd/freebsd-src/pull/1696
Closes: https://github.com/freebsd/freebsd-src/pull/1696
2025-06-11 17:16:22 -06:00
Ricardo Branco
d580567dfb Add tests for sig2str() / str2sig()
Reviewed by: imp, kib, des, jilles
Pull Request: https://github.com/freebsd/freebsd-src/pull/1696
2025-06-11 17:16:22 -06:00
Ricardo Branco
3d12567133 Add the POSIX sig2str(3) & str2sig(3) calls
Signed-off-by: Ricardo Branco <rbranco@suse.de>
Reviewed by: imp, kib, des, jilles
Pull Request: https://github.com/freebsd/freebsd-src/pull/1696
2025-06-11 17:16:22 -06:00
Ricardo Branco
86f06e612b Add POSIX NSIG_MAX & _SC_NSIG
Signed-off-by: Ricardo Branco <rbranco@suse.de>
Reviewed by: imp, kib
Pull Request: https://github.com/freebsd/freebsd-src/pull/1710
Closes: https://github.com/freebsd/freebsd-src/pull/1710
2025-06-11 17:16:20 -06:00
Brooks Davis
9623040eee closefrom: always use close_range(2)
close_range(2) was added prior to 13.0.  There is no need to support
a new userspace on such an old kernel.

Reviewed by:	kevans, imp
Differential Revision:	https://reviews.freebsd.org/D50730
2025-06-06 22:58:29 +01:00
Brooks Davis
a5b0b2a7d4 C runtime: add kernel version guards on exterrctl
This allows userspace to run on a (somewhat) out of date kernel.

Avoid a __FreeBSD_version bump and use the bump from a02180cf60 which
has occured since exterrctl was added.

Reviewed by:	kevans
Fixes:		b9c8a07d4d ("C runtime: enable extended error reporting from kernel")
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D50687
2025-06-04 19:09:19 +01:00
Jessica Clarke
786ced56a4 libc: Fix glob.c build with GCC
We don't expose the dummy struct pointer in the header file, so
gl_errblk is just a void * when lacking compiler support for blocks and
we need to cast to the former for CALL_BLOCK to work. If blocks support
is present this should be a redundant cast to the type that gl_errblk
already is.

Fixes:	1e0743f54d ("glob: Add blocks support")
2025-06-03 15:19:04 +01:00
Jessica Clarke
680f1a39ee libc: Allow more complex expressions for CALL_BLOCK first argument
For the case where the compiler supports blocks we only allow the first
expression to have an operator if it has as high precedence as a
function call, which for blocks effectively means member access and
subscripting only, not even a dereference nor a cast. Parenthesise this,
as is the case for the missing compiler support case, so that it's more
general.
2025-06-03 15:19:04 +01:00