Rich Felker [Fri, 18 Jul 2014 01:37:10 +0000 (21:37 -0400)]
remove useless infinite loop from end of exit function
this was originally added as a cheap but portable way to quell
warnings about reaching the end of a function that does not return,
but since _Exit is marked _Noreturn, it's not needed. removing it
makes the call to _Exit into a tail call and shaves off a few bytes of
code from minimal static programs.
Rich Felker [Thu, 17 Jul 2014 23:56:27 +0000 (19:56 -0400)]
fix crash in regexec for nonzero nmatch argument with REG_NOSUB
per POSIX, the nmatch and pmatch arguments are ignored when the regex
was compiled with REG_NOSUB.
Rich Felker [Thu, 17 Jul 2014 01:32:06 +0000 (21:32 -0400)]
work around constant folding bug 61144 in gcc 4.9.0 and 4.9.1
previously we detected this bug in configure and issued advice for a
workaround, but this turned out not to work. since then gcc 4.9.0 has
appeared in several distributions, and now 4.9.1 has been released
without a fix despite this being a wrong code generation bug which is
supposed to be a release-blocker, per gcc policy.
since the scope of the bug seems to affect only data objects (rather
than functions) whose definitions are overridable, and there are only
a very small number of these in musl, I am just changing them from
const to volatile for the time being. simply removing the const would
be sufficient to make gcc 4.9.1 work (the non-const case was
inadvertently fixed as part of another change in gcc), and this would
also be sufficient with 4.9.0 if we forced -O0 on the affected files
or on the whole build. however it's cleaner to just remove all the
broken compiler detection and use volatile, which will ensure that
they are never constant-folded. the quality of a non-broken compiler's
output should not be affected except for the fact that these objects
are no longer const and thus possibly add a few bytes to data/bss.
this change can be reconsidered and possibly reverted at some point in
the future when the broken gcc versions are no longer relevant.
Rich Felker [Thu, 17 Jul 2014 00:44:22 +0000 (20:44 -0400)]
simplify __stdio_exit static linking logic
the purpose of this logic is to avoid linking __stdio_exit unless any
stdio reads (which might require repositioning the file offset at exit
time) or writes (which might require flushing at exit time) could have
been performed.
previously, exit called two wrapper functions for __stdio_exit named
__flush_on_exit and __seek_on_exit. both of these functions actually
performed both tasks (seek and flushing) by calling the underlying
__stdio_exit. in order to avoid doing this twice, an overridable data
object __towrite_used was used to cause __seek_on_exit to act as a nop
when __towrite was linked.
now, exit only makes one call, directly to __stdio_exit. this is
satisfiable by a weak dummy definition in exit.c, but the real
definition is pulled in by either __toread.c or __towrite.c through
their referencing a symbol which is defined only in __stdio_exit.c.
Rich Felker [Sat, 12 Jul 2014 01:59:49 +0000 (21:59 -0400)]
implement the LOG_CONS option in syslog
this was previously a no-op, somewhat intentionally, because I failed
to understand that it only has an effect when sending to the logging
facility fails and thus is not the nuisance that it would be if always
sent output to the console.
Rich Felker [Sat, 12 Jul 2014 01:56:50 +0000 (21:56 -0400)]
suppress early syslog return when log socket cannot be opened
this behavior is no longer valid in general, and was never necessary.
if the LOG_PERROR option is set, output to stderr could still succeed.
also, when the LOG_CONS option is added, it will need syslog to
proceed even if opening the log socket fails.
Rich Felker [Sat, 12 Jul 2014 01:20:04 +0000 (21:20 -0400)]
implement the LOG_PERROR option in syslog
this is a nonstandard feature, but easy and inexpensive to add. since
the corresponding macro has always been defined in our syslog.h, it
makes sense to actually support it. applications may reasonably be
using the presence of the macro to assume that the feature is
supported.
the behavior of omitting the 'header' part of the log message does not
seem to be well-documented, but matches other implementations (at
least glibc) which have this option.
based on a patch by Clément Vasseur, but simplified using %n.
Clément Vasseur [Wed, 9 Jul 2014 12:34:18 +0000 (14:34 +0200)]
fix the %m specifier in syslog
errno must be saved upon vsyslog entry, otherwise its value could be
changed by some libc function before reaching the %m handler in
vsnprintf.
Rich Felker [Fri, 11 Jul 2014 04:29:44 +0000 (00:29 -0400)]
explicitly reject empty names in dynamic linker load_library function
previously passing an empty string for name resulted in failure, as
expected, but only after spurious syscalls, and it produced confusing
errno values (and thus dlerror strings).
in addition to dlopen calls, this issue affected use of LD_PRELOAD
with trailing whitespace or colon characters.
Rich Felker [Fri, 11 Jul 2014 04:26:12 +0000 (00:26 -0400)]
make dynamic linker accept colon as a separator for LD_PRELOAD
Rich Felker [Tue, 8 Jul 2014 23:40:44 +0000 (19:40 -0400)]
fix typo in microblaze setjmp asm
r24 was wrongly being saved at a misaligned offset of 30 rather than
the correct offset of 40 in the jmp_buf. the exact effects of this
error have not been studied, but it's clear that the value of r24 was
lost across setjmp/longjmp and the saved values of r21 and/or r22 may
also have been corrupted.
Rich Felker [Mon, 7 Jul 2014 03:10:35 +0000 (23:10 -0400)]
rename file containing pthread_cleanup_push and pop for consistency
Rich Felker [Mon, 7 Jul 2014 02:56:25 +0000 (22:56 -0400)]
rework cancellation weak alias logic not to depend on archive order
if the order of object files in the static archive libc.a was not
respected by the linker, the old logic could wrongly cause POSIX
symbols outside of the ISO C namespace to be pulled into pure C
programs. this should not happen with well-behaved linkers, but
relying on the link order was a bad idea anyway.
files are renamed to better reflect their contents now that they don't
need names to control their order as members in the archive file.
Rich Felker [Sun, 6 Jul 2014 05:34:13 +0000 (01:34 -0400)]
fix multiple issues in legacy function getpass
1. failure to output a newline after the password is read
2. fd leaks via missing FD_CLOEXEC
3. fd leaks via failure-to-close when any of the standard streams are
closed at the time of the call
4. wrongful fallback to use of stdin when opening /dev/tty fails
5. wrongful use of stderr rather than /dev/tty for prompt
6. failure to report error reading password
Rich Felker [Sun, 6 Jul 2014 03:29:55 +0000 (23:29 -0400)]
eliminate use of cached pid from thread structure
the main motivation for this change is to remove the assumption that
the tid of the main thread is also the pid of the process. (the value
returned by the set_tid_address syscall was used to fill both fields
despite it semantically being the tid.) this is historically and
presently true on linux and unlikely to change, but it conceivably
could be false on other systems that otherwise reproduce the linux
syscall api/abi.
only a few parts of the code were actually still using the cached pid.
in a couple places (aio and synccall) it was a minor optimization to
avoid a syscall. caching could be reintroduced, but lazily as part of
the public getpid function rather than at program startup, if it's
deemed important for performance later. in other places (cancellation
and pthread_kill) the pid was completely unnecessary; the tkill
syscall can be used instead of tgkill. this is actually a rather
subtle issue, since tgkill is supposedly a solution to race conditions
that can affect use of tkill. however, as documented in the commit
message for commit
7779dbd2663269b465951189b4f43e70839bc073, tgkill
does not actually solve this race; it just limits it to happening
within one process rather than between processes. we use a lock that
avoids the race in pthread_kill, and the use in the cancellation
signal handler is self-targeted and thus not subject to tid reuse
races, so both are safe regardless of which syscall (tgkill or tkill)
is used.
Rich Felker [Thu, 3 Jul 2014 01:46:41 +0000 (21:46 -0400)]
properly pass current locale to *_l functions when used internally
this change is presently non-functional since the callees do not yet
use their locale argument for anything.
Rich Felker [Thu, 3 Jul 2014 01:38:54 +0000 (21:38 -0400)]
consolidate str[n]casecmp_l into str[n]casecmp source files
this is mainly done for consistency with the ctype functions and to
declutter the src/locale directory.
Rich Felker [Thu, 3 Jul 2014 01:16:05 +0000 (21:16 -0400)]
consolidate *_l ctype/wctype functions into their non-_l source files
the main practical purposes of this commit are to remove a huge amount
of clutter from the src/locale directory, to cut down on the length of
the $(AR) and $(LD) command lines, and to reduce the amount of space
wasted by object file headers in the static libc.a. build time may
also be reduced, though this has not been measured.
as an additional justification, if there ever were a need for the
behavior of these functions to vary by locale, it would be necessary
for the non-_l versions to call the _l versions, so that linking the
former without the latter would not be possible anyway.
Rich Felker [Wed, 2 Jul 2014 23:33:19 +0000 (19:33 -0400)]
add locale framework
this commit adds non-stub implementations of setlocale, duplocale,
newlocale, and uselocale, along with the data structures and minimal
code needed for representing the active locale on a per-thread basis
and optimizing the common case where thread-local locale settings are
not in use.
at this point, the data structures only contain what is necessary to
represent LC_CTYPE (a single flag) and LC_MESSAGES (a name for use in
finding message translation files). representation for the other
categories will be added later; the expectation is that a single
pointer will suffice for each.
for LC_CTYPE, the strings "C" and "POSIX" are treated as special; any
other string is accepted and treated as "C.UTF-8". for other
categories, any string is accepted after being truncated to a maximum
supported length (currently 15 bytes). for LC_MESSAGES, the name is
kept regardless of whether libc itself can use such a message
translation locale, since applications using catgets or gettext should
be able to use message locales libc is not aware of. for other
categories, names which are not successfully loaded as locales (which,
at present, means all names) are treated as aliases for "C". setlocale
never fails.
locale settings are not yet used anywhere, so this commit should have
no visible effects except for the contents of the string returned by
setlocale.
Rich Felker [Wed, 2 Jul 2014 16:09:48 +0000 (12:09 -0400)]
fix failure of wide printf/scanf functions to set wide orientation
in some cases, these functions internally call a byte-based input or
output function before calling getwc/putwc, so they cannot rely on the
latter to set the orientation.
Rich Felker [Tue, 1 Jul 2014 23:06:48 +0000 (19:06 -0400)]
fix typo in a comment in __libc_start_main
Rich Felker [Tue, 1 Jul 2014 22:49:54 +0000 (18:49 -0400)]
fix incorrect return value for fwide function
when the orientation of the stream was already set, fwide was
incorrectly returning its argument (the requested orientation) rather
than the actual orientation of the stream.
Rich Felker [Tue, 1 Jul 2014 22:32:52 +0000 (18:32 -0400)]
fix ungrammatical comment in posix_spawn code
Rich Felker [Tue, 1 Jul 2014 22:27:19 +0000 (18:27 -0400)]
fix aliasing violations in mbtowc and mbrtowc
these functions were setting wc to point to wchar_t aliasing itself as
a "cheap" way to support null wc arguments. doing so was anything but
cheap, since even without the aliasing violation, it would limit the
compiler's ability to optimize.
making wc point to a dummy object is equally easy and does not suffer
from the above problems.
Rich Felker [Mon, 30 Jun 2014 05:18:14 +0000 (01:18 -0400)]
fix regression in mips dynamic linker
this issue caused the address of functions in shared libraries to
resolve to their PLT thunks in the main program rather than their
correct addresses. it was observed causing crashes, though the
mechanism of the crash was not thoroughly investigated. since the
issue is very subtle, it calls for some explanation:
on all well-behaved archs, GOT entries that belong to the PLT use a
special relocation type, typically called JMP_SLOT, so that the
dynamic linker can avoid having the jump destinations for the PLT
resolve to PLT thunks themselves (they also provide a definition for
the symbol, which must be used whenever the address of the function is
taken so that all DSOs see the same address).
however, the traditional mips PIC ABI lacked such a JMP_SLOT
relocation type, presumably because, due to the way PIC works, the
address of the PLT thunk was never needed and could always be ignored.
prior to commit
adf94c19666e687a728bbf398f9a88ea4ea19996, the mips
version of reloc.h contained a hack that caused all symbol lookups to
be treated like JMP_SLOT, inhibiting undefined symbols from ever being
used to resolve symbolic relocations. this hack goes all the way back
to commit
babf820180368f00742ec65b2050a82380d7c542, when the mips
dynamic linker was first made usable.
during the recent refactoring to eliminate arch-specific relocation
processing (commit
adf94c19666e687a728bbf398f9a88ea4ea19996), this
hack was overlooked and no equivalent functionality was provided in
the new code.
fixing the problem is not as simple as adding back an equivalent hack,
since there is now also a "non-PIC ABI" that can be used for the main
executable, which actually does use a PLT. the closest thing to
official documentation I could find for this ABI is nonpic.txt,
attached to Message-ID:
20080701202236.GA1534@caradoc.them.org, which
can be found in the gcc mailing list archives and elsewhere. per this
document, undefined symbols corresponding to PLT thunks have the
STO_MIPS_PLT bit set in the symbol's st_other field. thus, I have
added an arch-specific rule for mips, applied at the find_sym level
rather than the relocation level, to reject undefined symbols with the
STO_MIPS_PLT bit clear.
the previous hack of treating all mips relocations as JMP_SLOT-like,
rather than rejecting the unwanted symbols in find_sym, probably also
caused dlsym to wrongly return PLT thunks in place of the correct
address of a function under at least some conditions. this should now
be fixed, at least for global-scope symbol lookups.
Rich Felker [Mon, 30 Jun 2014 01:52:54 +0000 (21:52 -0400)]
fix regression in dynamic linker error reporting
due to a mistake when refactoring the error printing for the dynamic
linker (commit
7c73cacd09a51a87484db5689864743e4984a84d), all messages
were suppressed and replaced by blank lines.
Rich Felker [Wed, 25 Jun 2014 20:18:05 +0000 (16:18 -0400)]
release 1.1.3
Rich Felker [Wed, 25 Jun 2014 20:14:37 +0000 (16:14 -0400)]
add tarball-producing targets to Makefile for ease of release
my old, out-of-tree release script that performed a clone rather than
using git archive checked the VERSION file to make sure that it
matched before doing a release. I believe there should be a way to do
the same with git commands (without resorting to checking out the
desired tag) but I have not yet found a way, so care should be taken
when using these targets that the correctness of the VERSION file is
not overlooked.
Rich Felker [Wed, 25 Jun 2014 18:16:53 +0000 (14:16 -0400)]
update version reference in the README file
Rich Felker [Wed, 25 Jun 2014 18:16:15 +0000 (14:16 -0400)]
add note to INSTALL file about gcc 4.9.0 not being supported
Timo Teräs [Tue, 24 Jun 2014 10:06:38 +0000 (13:06 +0300)]
add routing protocols to getprotoent-family functions
iptables and quagga need them to work.
Rich Felker [Mon, 23 Jun 2014 21:20:49 +0000 (17:20 -0400)]
rename dynamic linker _start to _dlstart in the stub version
the renaming was previously applied to all real versions of the
function in commit
3fa2eb2aba8d6b54dec53e7ad4c37e17392b166f.
Rich Felker [Sun, 22 Jun 2014 04:39:03 +0000 (00:39 -0400)]
add __sysv_signal abi-compat alias for the signal function
it should be noted that the "real" __sysv_signal, which we do not
implement, is semantically different from signal. references to
__sysv_signal arise in code built against glibc under certain
combinations of feature test macros, and are almost surely
unintentional since the legacy sysv signal behavior has fundamental
race conditions that cannot be worked around and which make it
impossible to use safely.
Rich Felker [Sun, 22 Jun 2014 04:37:12 +0000 (00:37 -0400)]
add __xmknod and __xmknodat abi-compat functions
these are put alongside the similar functions for __xstat, etc. in
__xstat.c to avoid bloating the number of source files.
Rich Felker [Sun, 22 Jun 2014 04:29:32 +0000 (00:29 -0400)]
consolidate __xstat abi-compat functions into a single source file
these are mostly intended for use with dynamic linking (although they
can also be used statically with object files compiled against glibc
headers), so having them broken down into separate source files to
optimize for static linking is unlikely to be worth the cost having
more files in the source tree (which contributes to libc.a overhead,
compile time, link time, ar/linker command line size exhaustion, and
so on).
Rich Felker [Sat, 21 Jun 2014 23:24:15 +0000 (19:24 -0400)]
implement fmtmsg function
contributed by Isaac Dunham. this seems to be the last interface that
was missing for complete POSIX 2008 base + XSI coverage.
Rich Felker [Sat, 21 Jun 2014 23:21:05 +0000 (19:21 -0400)]
implement result address sorting in the resolver (getaddrinfo, etc.)
Rich Felker [Sat, 21 Jun 2014 11:44:46 +0000 (07:44 -0400)]
remove hack in syslog.h that resulted in aliasing violations
this issue affected the prioritynames and facilitynames arrays which
are only provided when requested (usually by syslogd implementations)
and which are presently defined as compound literals. the aliasing
violation seems to have been introduced as a workaround for bad
behavior by gcc's -Wwrite-strings option, but it caused compilers to
completely optimize out the contents of prioritynames and
facilitynames since, under many usage cases, the aliasing rules prove
that the contents are never accessed.
Rich Felker [Fri, 20 Jun 2014 20:10:48 +0000 (16:10 -0400)]
remove optimization-inhibiting behavior from configure's --enable-debug
this behavior turned out to be counter-intuitive to users and in any
case it's unnecessary. optimization can be disabled explicitly using
the --disable-optimize option, or both can be achieved without any
enable/disable options by passing CFLAGS="-O0 -g".
Timo Teräs [Fri, 20 Jun 2014 10:53:23 +0000 (13:53 +0300)]
fix gethostby*_r result pointer value on error
according to the documentation in the man pages, the GNU extension
functions gethostbyaddr_r, gethostbyname_r and gethostbyname2_r are
guaranteed to set the result pointer to NULL in case of error or no
result.
Rich Felker [Fri, 20 Jun 2014 06:13:38 +0000 (02:13 -0400)]
fix sendmmsg emulation return value for zero-length vector
this case is not even documented, but the kernel returns 0 here and it
makes sense to be consistent.
Rich Felker [Fri, 20 Jun 2014 04:25:12 +0000 (00:25 -0400)]
rename dynamic linker entry point from _start to _dlstart
the main motivation for this change is to aid in debugging. since the
main program's entry point is also named _start, it was difficult to
set breakpoints or quickly identify which _start execution stopped in.
Rich Felker [Fri, 20 Jun 2014 03:01:15 +0000 (23:01 -0400)]
implement sendmmsg and recvmmsg
these are not pure syscall wrappers because they have to work around
kernel API bugs on 64-bit archs. the workarounds could probably be
made somewhat more efficient, but at the cost of more complexity. this
may be revisited later.
Rich Felker [Thu, 19 Jun 2014 19:26:04 +0000 (15:26 -0400)]
add tlsdesc support for x86_64
Rich Felker [Thu, 19 Jun 2014 06:59:44 +0000 (02:59 -0400)]
separate __tls_get_addr implementation from dynamic linker/init_tls
such separation serves multiple purposes:
- by having the common path for __tls_get_addr alone in its own
function with a tail call to the slow case, code generation is
greatly improved.
- by having __tls_get_addr in it own file, it can be replaced on a
per-arch basis as needed, for optimization or ABI-specific purposes.
- by removing __tls_get_addr from __init_tls.c, a few bytes of code
are shaved off of static binaries (which are unlikely to use this
function unless the linker messed up).
Rich Felker [Thu, 19 Jun 2014 06:50:45 +0000 (02:50 -0400)]
add tlsdesc support for i386
Rich Felker [Thu, 19 Jun 2014 06:48:45 +0000 (02:48 -0400)]
optimize i386 ___tls_get_addr asm
Rich Felker [Thu, 19 Jun 2014 06:16:57 +0000 (02:16 -0400)]
change dynamic TLS installation strategy to optimize access
previously, accesses to dynamic TLS had to check two conditions before
being able to use a dtv slot: (1) that the module index was within the
bounds of the current dtv size, and (2) that the dynamic tls for the
requested module index was already installed in the dtv.
this commit changes the installation strategy so that, whenever an
attempt is made to access dynamic TLS that's not yet installed in the
dtv, the dynamic TLS for all lower-index modules is also installed.
thus it provides a new invariant: if a given module index is within
the bounds of the current dtv size, we automatically know that its TLS
is installed and directly available. the requirement that the second
condition (above) be checked is eliminated.
Rich Felker [Thu, 19 Jun 2014 06:01:06 +0000 (02:01 -0400)]
add arch-generic support for tlsdesc relocations to dynamic linker
this code is non-functional without further changes to link up the
arch-specific reloc types for tlsdesc and add asm implementations of
__tlsdesc_static and __tlsdesc_dynamic.
Rich Felker [Thu, 19 Jun 2014 04:42:28 +0000 (00:42 -0400)]
fix incorrect comparison loop condition in memmem
the logic for this loop was copied from null-terminated-string logic
in strstr without properly adapting it to work with explicit lengths.
presumably this error could result in false negatives (wrongly
comparing past the end of the needle/haystack), false positives
(stopping comparison early when the needle contains null bytes), and
crashes (from runaway reads past the end of mapped memory).
Rich Felker [Wed, 18 Jun 2014 07:05:42 +0000 (03:05 -0400)]
reduce code duplication in dynamic linker error paths
eventually this should help making dlerror thread-safe too.
Rich Felker [Wed, 18 Jun 2014 06:44:02 +0000 (02:44 -0400)]
refactor to remove arch-specific relocation code from dynamic linker
this was one of the main instances of ugly code duplication: all archs
use basically the same types of relocations, but roughly equivalent
logic was duplicated for each arch to account for the different naming
and numbering of relocation types and variation in whether REL or RELA
records are used.
as an added bonus, both REL and RELA are now supported on all archs,
regardless of which is used by the standard toolchain.
Rich Felker [Tue, 17 Jun 2014 18:32:43 +0000 (14:32 -0400)]
fix powerpc dynamic linker thread-pointer-relative relocations
processing of R_PPC_TPREL32 was ignoring the addend provided by the
RELA-style relocation and instead using the inline value as the
addend. this presumably broke dynamic-linked access to initial TLS in
cases where the addend was nonzero.
Rich Felker [Tue, 17 Jun 2014 17:56:54 +0000 (13:56 -0400)]
multiple fixes to sh (superh) dynamic linker relocations
the following issues are fixed:
- R_SH_REL32 was adding the load address of the module being relocated
to the result. this seems to have been a mistake in the original
port, since it does not match other dynamic linker implementations
and since adding a difference between two addresses (the symbol
value and the relocation address) to a load address does not make
sense.
- R_SH_TLS_DTPMOD32 was wrongly accepting an inline addend (i.e. using
+= rather than = on *reloc_addr) which makes no sense; addition is
not an operation that's defined on module ids.
- R_SH_TLS_DTPOFF32 and R_SH_TLS_TPOFF32 were wrongly using inline
addends rather than the RELA-provided addends.
in addition, handling of R_SH_GLOB_DAT, R_SH_JMP_SLOT, and R_SH_DIR32
are merged to all honor the addend. the first two should not need it
for correct usage generated by toolchains, but other dynamic linkers
allow addends here, and it simplifies the code anyway.
these issues were spotted while reviewing the code for the purpose of
refactoring this part of the dynamic linker. no testing was performed.
Rich Felker [Mon, 16 Jun 2014 07:09:07 +0000 (03:09 -0400)]
dynamic linker: permit error returns from arch-specific reloc function
the immediate motivation is supporting TLSDESC relocations which
require allocation and thus may fail (unless we pre-allocate), but
this mechanism should also be used for throwing an error on
unsupported or invalid relocation types, and perhaps in certain cases,
for reporting when a relocation is not satisfiable.
Clément Vasseur [Fri, 13 Jun 2014 16:27:56 +0000 (18:27 +0200)]
fix missing argument to syscall in fanotify_mark
Rich Felker [Thu, 12 Jun 2014 03:38:44 +0000 (23:38 -0400)]
support optional-argument extension to getopt via double-colon
this extension is not incompatible with the standard behavior of the
function, not expensive, and avoids requiring a replacement getopt
with full GNU extensions for a few important apps including busybox's
sed with the -i option.
Rich Felker [Tue, 10 Jun 2014 16:11:12 +0000 (12:11 -0400)]
fail configure on --enable-shared if -Bsymbolic-functions doesn't work
previously, a warning was issued in this case no matter what, even if
--disable-shared was used. now, the default for --enable-shared is
changed from "yes" to "auto", and the warning is issued by default,
but becomes an error if --enable-shared is used, and the test is
suppressed completely if --disable-shared is used.
Rich Felker [Tue, 10 Jun 2014 08:49:49 +0000 (04:49 -0400)]
simplify errno implementation
the motivation for the errno_ptr field in the thread structure, which
this commit removes, was to allow the main thread's errno to keep its
address when lazy thread pointer initialization was used. &errno was
evaluated prior to setting up the thread pointer and stored in
errno_ptr for the main thread; subsequently created threads would have
errno_ptr pointing to their own errno_val in the thread structure.
since lazy initialization was removed, there is no need for this extra
level of indirection; __errno_location can simply return the address
of the thread's errno_val directly. this does cause &errno to change,
but the change happens before entry to application code, and thus is
not observable.
Rich Felker [Tue, 10 Jun 2014 08:02:40 +0000 (04:02 -0400)]
replace all remaining internal uses of pthread_self with __pthread_self
prior to version 1.1.0, the difference between pthread_self (the
public function) and __pthread_self (the internal macro or inline
function) was that the former would lazily initialize the thread
pointer if it was not already initialized, whereas the latter would
crash in this case. since lazy initialization is no longer supported,
use of pthread_self no longer makes sense; it simply generates larger,
slower code.
Rich Felker [Tue, 10 Jun 2014 07:36:56 +0000 (03:36 -0400)]
add thread-pointer support for pre-2.6 kernels on i386
such kernels cannot support threads, but the thread pointer is also
important for other purposes, most notably stack protector. without a
valid thread pointer, all code compiled with stack protector will
crash. the same applies to any use of thread-local storage by
applications or libraries.
the concept of this patch is to fall back to using the modify_ldt
syscall, which has been around since linux 1.0, to setup the gs
segment register. since the kernel does not have a way to
automatically assign ldt entries, use of slot zero is hard-coded. if
this fallback path is used, __set_thread_area returns a positive value
(rather than the usual zero for success, or negative for error)
indicating to the caller that the thread pointer was successfully set,
but only for the main thread, and that thread creation will not work
properly. the code in __init_tp has been changed accordingly to record
this result for later use by pthread_create.
Rich Felker [Sat, 7 Jun 2014 08:09:21 +0000 (04:09 -0400)]
avoid spurious lookup failures from badly-behaved nameservers
the results of a dns query, whether it's performed as part of one of
the standard name-resolving functions or directly by res_send, should
be a function of the query, not of the particular nameserver that
responds to it. thus, all responses which indicate a failure or
refusal by the nameserver, as opposed to a positive or negative result
for the query, should be ignored.
the strategy used is to re-issue the query immediately (but with a
limit on the number of retries, in case the server is really broken)
when a response code of 2 (server failure, typically transient) is
seen, and otherwise take no action on bad responses (which generally
indicate a misconfigured nameserver or one which the client does not
have permission to use), allowing the normal retry interval to apply
and of course accepting responses from other nameservers queried in
parallel.
empirically this matches the traditional resolver behavior for
nameservers that respond with a code of 2 in the case where there is
just a single nameserver configured. the behavior diverges when
multiple nameservers are available, since musl is querying them in
parallel. in this case we are mildly more aggressive at retrying.
Rich Felker [Sat, 7 Jun 2014 00:46:05 +0000 (20:46 -0400)]
use default timezone from /etc/localtime if $TZ is unset/blank
the way this is implemented, it also allows explicit setting of
TZ=/etc/localtime even for suid programs. this is not a problem
because /etc/localtime is a trusted path, much like the trusted
zoneinfo search path.
Rich Felker [Fri, 6 Jun 2014 22:36:00 +0000 (18:36 -0400)]
release 1.1.2
Timo Teräs [Wed, 14 May 2014 07:53:56 +0000 (10:53 +0300)]
implement %y and %C specifiers in strptime
Rich Felker [Fri, 6 Jun 2014 19:43:16 +0000 (15:43 -0400)]
avoid invalid use of va_arg in open
reading the variadic mode argument is only valid when the O_CREAT flag
is present. this probably does not matter, but is needed for formal
correctness, and could affect LTO or other full-program analysis.
Rich Felker [Fri, 6 Jun 2014 19:42:42 +0000 (15:42 -0400)]
add O_CLOEXEC fallback for open and related functions
since there is no easy way to detect whether open honored or ignored
the O_CLOEXEC flag, the optimal solution to providing a fallback is
simply to make the fcntl syscall to set the close-on-exec flag
immediately after open returns.
Rich Felker [Fri, 6 Jun 2014 19:30:28 +0000 (15:30 -0400)]
optimize SOCK_CLOEXEC fallback for socket function
the fcntl function is heavy, so make the syscall directly instead.
also, avoid the code size and runtime overhead of querying the old
flags, since it's reasonable to assume nothing will be set on a
newly-created socket. this code is only used on old kernels which lack
proper atomic close-on-exec support, so future changes that might
invalidate such an assumption do not need to be considered.
Rich Felker [Fri, 6 Jun 2014 19:29:00 +0000 (15:29 -0400)]
add SOCK_CLOEXEC fallback for socketpair on old kernels
as usual, this is non-atomic, but better than producing an error or
failing to set the close-on-exec flag at all.
Szabolcs Nagy [Fri, 6 Jun 2014 18:44:54 +0000 (20:44 +0200)]
implement dn_comp RFC 1035 domain name compression
the input name is validated, the other parameters are assumed to be
valid (the list of already compressed names are not checked for
infinite reference loops or out-of-bound offsets).
names are handled case-sensitively for now.
Szabolcs Nagy [Fri, 6 Jun 2014 17:20:07 +0000 (19:20 +0200)]
accept trailing . and empty domain names
trailing . should be accepted in domain name strings by convention
(RFC 1034), host name lookup accepts "." but rejects empty "", res_*
interfaces also accept empty name following existing practice.
Rich Felker [Fri, 6 Jun 2014 07:29:36 +0000 (03:29 -0400)]
add vdso clock_gettime acceleration support to i386
Rich Felker [Fri, 6 Jun 2014 07:17:47 +0000 (03:17 -0400)]
fix fd leak in tmpfile when the fdopen operation fails
this condition could only happen due to malloc failure.
the fdopen operation is also moved to take place after the unlink to
minimize the window during which a link to the file exists in the
directory table.
Szabolcs Nagy [Thu, 5 Jun 2014 20:52:40 +0000 (22:52 +0200)]
fix the domain name length limit checks
A domain name is at most 255 bytes long (RFC 1035), but the string
representation is two bytes smaller so the strlen maximum is 253.
Szabolcs Nagy [Thu, 5 Jun 2014 20:32:42 +0000 (22:32 +0200)]
fix multiple validation issues in dns response label parsing
Due to an error introduced in commit
fcc522c92335783293ac19df318415cd97fbf66b,
checking of the remaining output buffer space was not performed correctly,
allowing malformed input to write past the end of the buffer.
In addition, the loop detection logic failed to account for the possibility
of infinite loops with no output, which would hang the function.
The output size is now limited more strictly so only names with valid length
are accepted.
Rich Felker [Thu, 5 Jun 2014 19:55:20 +0000 (15:55 -0400)]
fix missing function declarations in refactored ip literal parsing code
Rich Felker [Wed, 4 Jun 2014 23:33:20 +0000 (19:33 -0400)]
add support for reverse port lookups from services file to getnameinfo
this also affects the legacy getservbyport family, which uses
getnameinfo as its backend.
Rich Felker [Wed, 4 Jun 2014 20:53:39 +0000 (16:53 -0400)]
add support for reverse name lookups from hosts file to getnameinfo
this also affects the legacy gethostbyaddr family, which uses
getnameinfo as its backend.
some other minor changes associated with the refactoring of source
files are also made; in particular, the resolv.conf parser now uses
the same code that's used elsewhere to handle ip literals, so as a
side effect it can now accept a scope id for nameserver addressed with
link-local scope.
Rich Felker [Wed, 4 Jun 2014 08:39:34 +0000 (04:39 -0400)]
remove some dummy "ent" function aliases that duplicated real ones
the service and protocol functions are defined also in other files,
and the protocol ones are actually non-nops elsewhere, so the weak
definitions in ent.c could have prevented the strong definitions from
getting pulled in and used in some static programs.
Rich Felker [Wed, 4 Jun 2014 07:39:22 +0000 (03:39 -0400)]
simplify vasprintf implementation
the old implementation preallocated a buffer in order to try to avoid
calling vsnprintf more than once. not only did this potentially lead
to memory fragmentation from trimming with realloc; it also pulled in
realloc/free, which otherwise might not be needed in a static linked
program.
Rich Felker [Wed, 4 Jun 2014 06:24:38 +0000 (02:24 -0400)]
add support for ipv6 scope_id to getaddrinfo and getnameinfo
for all address types, a scope_id specified as a decimal value is
accepted. for addresses with link-local scope, a string containing the
interface name is also accepted.
some changes are made to error handling to avoid unwanted fallbacks in
the case where the scope_id is invalid: if an earlier name lookup
backend fails with an error rather than simply "0 results", this
failure now suppresses any later attempts with other backends.
in getnameinfo, a light "itoa" type function is added for generating
decimal scope_id results, and decimal port strings for services are
also generated using this function now so as not to pull in the
dependency on snprintf.
in netdb.h, a definition for the NI_NUMERICSCOPE flag is added. this
is required by POSIX (it was previously missing) and needed to allow
callers to suppress interface-name lookups.
Rich Felker [Tue, 3 Jun 2014 21:53:11 +0000 (17:53 -0400)]
fix if_nametoindex return value when interface does not exist
the return value is unsigned, so negative results for "errors" do not
make sense; 0 is the value reserved for when the interface name does
not exist.
Rich Felker [Tue, 3 Jun 2014 05:46:40 +0000 (01:46 -0400)]
fix negative response and non-response handling for dns queries
previously, all failures to obtain at least one address were treated
as nonexistant names (EAI_NONAME). this failed to account for the
possibility of transient failures (no response at all, or a response
with rcode of 2, server failure) or permanent failures that do not
indicate the nonexistence of the requested name. only an rcode of 3
should be treated as an indication of nonexistence.
Rich Felker [Tue, 3 Jun 2014 05:43:29 +0000 (01:43 -0400)]
fix some validation checks in dns response parsing code
since the buffer passed always has an actual size of 512 bytes, the
maximum possible response packet size, no out-of-bounds access was
possible; however, reading past the end of the valid portion of the
packet could cause the parser to attempt to process junk as answer
content.
Rich Felker [Mon, 2 Jun 2014 20:21:47 +0000 (16:21 -0400)]
fix incorrect end pointer in some cases when wcsrtombs stops early
when wcsrtombs stopped due to hitting zero remaining space in the
output buffer, it was wrongly clearing the position pointer as if it
had completed the conversion successfully.
this commit rearranges the code somewhat to make a clear separation
between the cases of ending due to running out of output buffer space,
and ending due to reaching the end of input or an illegal sequence in
the input. the new branches have been arranged with the hope of
optimizing more common cases, too.
Rich Felker [Mon, 2 Jun 2014 09:00:48 +0000 (05:00 -0400)]
remove cruft from old resolver and numeric ip parsing
the old resolver code used a function __ipparse which contained the
logic for inet_addr and inet_aton, which is needed in getaddrinfo.
this was phased out in the resolver overhaul in favor of directly
using inet_aton and inet_pton as appropriate.
this commit cleans up some stuff that was left behind.
Rich Felker [Mon, 2 Jun 2014 08:47:45 +0000 (04:47 -0400)]
switch standard resolver functions to use the new dns backend
this is the third phase of the "resolver overhaul" project.
this commit removes all of the old dns code, and switches the
__lookup_name backend (used by getaddrinfo, etc.) and the getnameinfo
function to use the newly implemented __res_mkquery and __res_msend
interfaces. for parsing the results, a new callback-based __dns_parse
function, based on __dns_get_rr from the old dns code, is used.
Rich Felker [Mon, 2 Jun 2014 08:27:54 +0000 (04:27 -0400)]
fix uninitialized variable in new __res_msend dns function
Rich Felker [Mon, 2 Jun 2014 06:13:57 +0000 (02:13 -0400)]
implement new dns backend, res_send and other legacy resolver functions
this is the second phase of the "resolver overhaul" project.
the key additions in this commit are the __res_msend and __res_mkquery
functions, which have been factored so as to provide a backend for
both the legacy res_* functions and the standard getaddrinfo and
getnameinfo functions. the latter however are still using the old
backend code; there is code duplication which still needs to be
removed, and this will be the next phase of the resolver overhaul.
__res_msend is derived from the old __dns_doqueries function, but
generalized to send arbitrary caller-provided packets in parallel
rather than producing the parallel queries itself. this allows it to
be used (completely trivially) as a backend for res_send. the
factored-out query generation code, with slightly more generality, is
now part of __res_mkquery.
Timo Teräs [Mon, 2 Jun 2014 05:16:08 +0000 (08:16 +0300)]
add ipsec and tunneling protocols to getprotoent-family functions
iptables and ipsec-tools among others require these to function
properly.
Rich Felker [Mon, 2 Jun 2014 05:31:28 +0000 (01:31 -0400)]
fix off-by-one in checking hostname length in new resolver backend
this bug was introduced in the recent resolver overhaul commits. it
likely had visible symptoms. these were probably limited to wrongly
accepting truncated versions of over-long names (vs rejecting them),
as opposed to stack-based overflows or anything more severe, but no
extensive checks were made. there have been no releases where this bug
was present.
Rich Felker [Sun, 1 Jun 2014 04:45:04 +0000 (00:45 -0400)]
improve getservbyname_r using new resolver backend
now that host and service lookup have been separated in the backend,
there's no need for service lookup functions to pull in the host
lookup code. moreover, dynamic allocation is no longer needed, so this
function should now be async-signal-safe. it's also significantly
smaller.
one change in getservbyname is also made: knowing that getservbyname_r
needs only two character pointers in the caller-provided buffer, some
wasted bss can be avoided.
Rich Felker [Sun, 1 Jun 2014 04:09:28 +0000 (00:09 -0400)]
improve gethostbyname2_r using new resolver backend
these changes reduce the size of the function somewhat and remove many
of its dependencies, including free. in principle it should now be
async-signal-safe, but this has not been verified in detail.
minor changes to error handling are also made.
Rich Felker [Sun, 1 Jun 2014 00:57:54 +0000 (20:57 -0400)]
refactor getaddrinfo and add support for most remaining features
this is the first phase of the "resolver overhaul" project.
conceptually, the results of getaddrinfo are a direct product of a
list of address results and a list of service results. the new code
makes this explicit by computing these lists separately and combining
the results. this adds support for services that have both tcp and udp
versions, where the caller has not specified which it wants, and
eliminates a number of duplicate code paths which were all producing
the final output addrinfo structures, but in subtly different ways,
making it difficult to implement any of the features which were
missing.
in addition to the above benefits, the refactoring allows for legacy
functions like gethostbyname to be implemented without using the
getaddrinfo function itself. such changes to the legacy functions have
not yet been made, however.
further improvements include matching of service alias names from
/etc/services (previously only the primary name was supported),
returning multiple results from /etc/hosts (previously only the first
matching line was honored), and support for the AI_V4MAPPED and AI_ALL
flags.
features which remain unimplemented are IDN translations (encoding
non-ASCII hostnames for DNS lookup) and the AI_ADDRCONFIG flag.
at this point, the DNS-based name resolving code is still based on the
old interfaces in __dns.c, albeit somewhat simpler in its use of them.
there may be some dead code which could already be removed, but
changes to this layer will be a later phase of the resolver overhaul.
Szabolcs Nagy [Fri, 30 May 2014 15:57:16 +0000 (17:57 +0200)]
fix POSIX namespace pollution in sys/types.h
int8_t, u_int8_t, etc types are moved under _BSD_SOURCE
Szabolcs Nagy [Fri, 30 May 2014 15:09:53 +0000 (17:09 +0200)]
use cleaner code for handling float rounding in vfprintf
CONCAT(0x1p,LDBL_MANT_DIG) is not safe outside of libc,
use 2/LDBL_EPSILON instead.
fix was proposed by Morten Welinder.
Szabolcs Nagy [Fri, 30 May 2014 12:15:48 +0000 (14:15 +0200)]
update netinet/in.h to match the current kernel uapi
from linux/in.h and linux/in6.h uapi headers the following
missing socket options were added:
IP_NODEFRAG - used with customized ipv4 headers
IPV6_RECVPATHMTU - for ipv6 path mtu
IPV6_PATHMTU - for ipv6 path mtu
IPV6_DONTFRAG - for ipv6 path mtu
IPV6_ADDR_PREFERENCES - RFC5014 Source Address Selection
IPV6_MINHOPCOUNT - RFC5082 Generalized TTL Security Mechanism
IPV6_ORIGDSTADDR - used by tproxy
IPV6_RECVORIGDSTADDR - used by tproxy
IPV6_TRANSPARENT - used by tproxy
IPV6_UNICAST_IF - ipv6 version of IP_UNICAST_IF
and socket option values:
IP_PMTUDISC_OMIT - value for IP_MTU_DISCOVER option, new in linux 3.14
IPV6_PMTUDISC_OMIT - same for IPV6_MTU_DISCOVER
IPV6_PMTUDISC_INTERFACE - ipv6 version of IP_PMTUDISC_INTERFACE
IPV6_PREFER_* - flags for IPV6_ADDR_PREFERENCES
not added: ipv6 flow info and flow label related definitions.
(it's unclear if libc should define these and namespace polluting
type name is involved so they are not provided for now)
Szabolcs Nagy [Fri, 30 May 2014 12:07:59 +0000 (14:07 +0200)]
add SO_BPF_EXTENSIONS socket option
this was introduced to query BPF extension support with getsockopt
in linux 3.14, commit
ea02f9411d9faa3553ed09ce0ec9f00ceae9885e
Szabolcs Nagy [Fri, 30 May 2014 11:47:44 +0000 (13:47 +0200)]
add sched_{get,set}attr syscall numbers and SCHED_DEADLINE macro
linux 3.14 introduced sched_getattr and sched_setattr syscalls in
commit
d50dde5a10f305253cbc3855307f608f8a3c5f73
and the related SCHED_DEADLINE scheduling policy in
commit
aab03e05e8f7e26f51dee792beddcb5cca9215a5
but struct sched_attr "extended scheduling parameters data structure"
is not yet exported to userspace (necessary for using the syscalls)
so related uapi definitions are not added yet.
Rich Felker [Fri, 30 May 2014 16:59:36 +0000 (12:59 -0400)]
fix breakage from recent syscall commits due to missing errno macros