summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdlib
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/stdlib')
-rw-r--r--lib/libc/stdlib/Makefile.inc8
-rw-r--r--lib/libc/stdlib/atexit.c30
-rw-r--r--lib/libc/stdlib/getopt.34
-rw-r--r--lib/libc/stdlib/getopt_long.34
-rw-r--r--lib/libc/stdlib/hcreate.32
-rw-r--r--lib/libc/stdlib/malloc.37
-rw-r--r--lib/libc/stdlib/malloc.c32
-rw-r--r--lib/libc/stdlib/ptsname.32
-rw-r--r--lib/libc/stdlib/rand.311
-rw-r--r--lib/libc/stdlib/random.31
-rw-r--r--lib/libc/stdlib/realpath.343
-rw-r--r--lib/libc/stdlib/realpath.c49
-rw-r--r--lib/libc/stdlib/strtod.36
-rw-r--r--lib/libc/stdlib/strtonum.33
14 files changed, 149 insertions, 53 deletions
diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc
index 1d06dd3..d5b151c 100644
--- a/lib/libc/stdlib/Makefile.inc
+++ b/lib/libc/stdlib/Makefile.inc
@@ -2,7 +2,7 @@
# $FreeBSD$
# machine-independent stdlib sources
-.PATH: ${.CURDIR}/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib
+.PATH: ${.CURDIR}/${LIBC_ARCH}/stdlib ${.CURDIR}/stdlib
MISRCS+=_Exit.c a64l.c abort.c abs.c atexit.c atof.c atoi.c atol.c atoll.c \
bsearch.c div.c exit.c getenv.c getopt.c getopt_long.c \
@@ -16,7 +16,7 @@ MISRCS+=_Exit.c a64l.c abort.c abs.c atexit.c atof.c atoi.c atol.c atoll.c \
SYM_MAPS+= ${.CURDIR}/stdlib/Symbol.map
# machine-dependent stdlib sources
-.sinclude "${.CURDIR}/${MACHINE_ARCH}/stdlib/Makefile.inc"
+.sinclude "${.CURDIR}/${LIBC_ARCH}/stdlib/Makefile.inc"
MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \
div.3 exit.3 getenv.3 getopt.3 getopt_long.3 getsubopt.3 \
@@ -51,3 +51,7 @@ MLINKS+=tsearch.3 tdelete.3 tsearch.3 tfind.3 tsearch.3 twalk.3
CFLAGS+= -DMALLOC_PRODUCTION
.endif
+.if ${CC:T:Mclang} == "clang"
+# XXX: Temporary workaround for LLVM PR 9352
+CFLAGS+= ${.IMPSRC:T:Mmalloc.c:C/^.+$/-no-integrated-as/}
+.endif
diff --git a/lib/libc/stdlib/atexit.c b/lib/libc/stdlib/atexit.c
index efd1264..511172a 100644
--- a/lib/libc/stdlib/atexit.c
+++ b/lib/libc/stdlib/atexit.c
@@ -37,6 +37,7 @@ static char sccsid[] = "@(#)atexit.c 8.2 (Berkeley) 7/3/94";
__FBSDID("$FreeBSD$");
#include "namespace.h"
+#include <link.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
@@ -54,6 +55,7 @@ static pthread_mutex_t atexit_mutex = PTHREAD_MUTEX_INITIALIZER;
#define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
#define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
+#define _MUTEX_DESTROY(x) if (__isthreaded) _pthread_mutex_destroy(x)
struct atexit {
struct atexit *next; /* next in list */
@@ -119,7 +121,7 @@ atexit(void (*func)(void))
int error;
fn.fn_type = ATEXIT_FN_STD;
- fn.fn_ptr.std_func = func;;
+ fn.fn_ptr.std_func = func;
fn.fn_arg = NULL;
fn.fn_dso = NULL;
@@ -138,7 +140,7 @@ __cxa_atexit(void (*func)(void *), void *arg, void *dso)
int error;
fn.fn_type = ATEXIT_FN_CXA;
- fn.fn_ptr.cxa_func = func;;
+ fn.fn_ptr.cxa_func = func;
fn.fn_arg = arg;
fn.fn_dso = dso;
@@ -146,6 +148,9 @@ __cxa_atexit(void (*func)(void *), void *arg, void *dso)
return (error);
}
+#pragma weak __pthread_cxa_finalize
+void __pthread_cxa_finalize(const struct dl_phdr_info *);
+
/*
* Call all handlers registered with __cxa_atexit for the shared
* object owning 'dso'. Note: if 'dso' is NULL, then all remaining
@@ -154,18 +159,28 @@ __cxa_atexit(void (*func)(void *), void *arg, void *dso)
void
__cxa_finalize(void *dso)
{
+ struct dl_phdr_info phdr_info;
struct atexit *p;
struct atexit_fn fn;
- int n;
+ int n, has_phdr;
+
+ if (dso != NULL)
+ has_phdr = _rtld_addr_phdr(dso, &phdr_info);
+ else
+ has_phdr = 0;
_MUTEX_LOCK(&atexit_mutex);
for (p = __atexit; p; p = p->next) {
for (n = p->ind; --n >= 0;) {
if (p->fns[n].fn_type == ATEXIT_FN_EMPTY)
continue; /* already been called */
- if (dso != NULL && dso != p->fns[n].fn_dso)
- continue; /* wrong DSO */
fn = p->fns[n];
+ if (dso != NULL && dso != fn.fn_dso) {
+ /* wrong DSO ? */
+ if (!has_phdr || !__elf_phdr_match_addr(
+ &phdr_info, fn.fn_ptr.cxa_func))
+ continue;
+ }
/*
Mark entry to indicate that this particular handler
has already been called.
@@ -182,4 +197,9 @@ __cxa_finalize(void *dso)
}
}
_MUTEX_UNLOCK(&atexit_mutex);
+ if (dso == NULL)
+ _MUTEX_DESTROY(&atexit_mutex);
+
+ if (has_phdr && &__pthread_cxa_finalize != NULL)
+ __pthread_cxa_finalize(&phdr_info);
}
diff --git a/lib/libc/stdlib/getopt.3 b/lib/libc/stdlib/getopt.3
index a966624..3fe4453 100644
--- a/lib/libc/stdlib/getopt.3
+++ b/lib/libc/stdlib/getopt.3
@@ -66,11 +66,11 @@ may contain the following elements: individual characters, and
characters followed by a colon to indicate an option argument
is to follow.
For example, an option string
-.Li "\&""x""
+.Li \&"x"
recognizes an option
.Dq Fl x ,
and an option string
-.Li "\&""x:""
+.Li \&"x:"
recognizes an option and argument
.Dq Fl x Ar argument .
It does not matter to
diff --git a/lib/libc/stdlib/getopt_long.3 b/lib/libc/stdlib/getopt_long.3
index 352af08..72f6534 100644
--- a/lib/libc/stdlib/getopt_long.3
+++ b/lib/libc/stdlib/getopt_long.3
@@ -469,9 +469,9 @@ The
.Fn getopt_long
and
.Fn getopt_long_only
-functions first appeared in
+functions first appeared in the
.Tn GNU
-libiberty.
+libiberty library.
The first
.Bx
implementation of
diff --git a/lib/libc/stdlib/hcreate.3 b/lib/libc/stdlib/hcreate.3
index cd82720..2466c9f 100644
--- a/lib/libc/stdlib/hcreate.3
+++ b/lib/libc/stdlib/hcreate.3
@@ -29,8 +29,8 @@
.\" $FreeBSD$
.\"
.Dd July 6, 2008
-.Os
.Dt HCREATE 3
+.Os
.Sh NAME
.Nm hcreate , hdestroy , hsearch
.Nd manage hash search table
diff --git a/lib/libc/stdlib/malloc.3 b/lib/libc/stdlib/malloc.3
index 9b9fe96..5f09733 100644
--- a/lib/libc/stdlib/malloc.3
+++ b/lib/libc/stdlib/malloc.3
@@ -218,7 +218,7 @@ thread-specific cache garbage collection sweeps.
Garbage collection is actually performed incrementally, one size
class at a time, in order to avoid large collection pauses.
The default sweep interval is 8192;
-.Ev JEMALLOC_OPTIONS=14g
+.Ev MALLOC_OPTIONS=14g
will disable garbage collection.
.It H
Double/halve the number of thread-specific cache slots per size
@@ -232,7 +232,7 @@ See the
.Dq G
option for related tuning information.
The default number of cache slots is 128;
-.Ev JEMALLOC_OPTIONS=7h
+.Ev MALLOC_OPTIONS=7h
will disable thread-specific caching.
Note that one cache slot per size class is not a valid
configuration due to implementation details.
@@ -415,7 +415,7 @@ Allocation requests that are more than half the quantum, but no more than the
minimum cacheline-multiple size class (see the
.Dq Q
option) are rounded up to the nearest multiple of the quantum.
-Allocation requests that are more than the minumum cacheline-multiple size
+Allocation requests that are more than the minimum cacheline-multiple size
class, but no more than the minimum subpage-multiple size class (see the
.Dq C
option) are rounded up to the nearest multiple of the cacheline size (64).
@@ -550,7 +550,6 @@ allocation functions.
.El
.Sh EXAMPLES
To dump core whenever a problem occurs:
-.Pp
.Bd -literal -offset indent
ln -s 'A' /etc/malloc.conf
.Ed
diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c
index 295a168..48d9c14 100644
--- a/lib/libc/stdlib/malloc.c
+++ b/lib/libc/stdlib/malloc.c
@@ -180,6 +180,7 @@ __FBSDID("$FreeBSD$");
#include <errno.h>
#include <limits.h>
+#include <link.h>
#include <pthread.h>
#include <sched.h>
#include <stdarg.h>
@@ -194,6 +195,8 @@ __FBSDID("$FreeBSD$");
#include "un-namespace.h"
+#include "libc_private.h"
+
#define RB_COMPACT
#include "rb.h"
#if (defined(MALLOC_TCACHE) && defined(MALLOC_STATS))
@@ -231,7 +234,7 @@ __FBSDID("$FreeBSD$");
#ifdef __sparc64__
# define LG_QUANTUM 4
# define LG_SIZEOF_PTR 3
-# define NO_TLS
+# define TLS_MODEL /* default */
#endif
#ifdef __amd64__
# define LG_QUANTUM 4
@@ -249,8 +252,13 @@ __FBSDID("$FreeBSD$");
# define LG_SIZEOF_PTR 2
# define NO_TLS
#endif
-#ifdef __powerpc__
+#ifdef __powerpc64__
+# define LG_QUANTUM 4
+# define LG_SIZEOF_PTR 3
+# define TLS_MODEL /* default */
+#elif defined(__powerpc__)
# define LG_QUANTUM 4
+# define LG_SIZEOF_PTR 2
# define TLS_MODEL /* default */
#endif
#ifdef __s390x__
@@ -5348,14 +5356,20 @@ small_size2bin_init_hard(void)
static unsigned
malloc_ncpus(void)
{
+ int mib[2];
unsigned ret;
- size_t retlen = sizeof(ret);
- int mib[] = {CTL_HW, HW_NCPU};
-
- if (sysctl(mib, sizeof(mib) / sizeof(int), &ret, &retlen,
- (void *)0, 0) == -1) {
- /* Error. */
- ret = 1;
+ int error;
+ size_t len;
+
+ error = _elf_aux_info(AT_NCPUS, &ret, sizeof(ret));
+ if (error != 0 || ret == 0) {
+ mib[0] = CTL_HW;
+ mib[1] = HW_NCPU;
+ len = sizeof(ret);
+ if (sysctl(mib, 2, &ret, &len, (void *)NULL, 0) == -1) {
+ /* Error. */
+ ret = 1;
+ }
}
return (ret);
diff --git a/lib/libc/stdlib/ptsname.3 b/lib/libc/stdlib/ptsname.3
index f674a54..48da3bd 100644
--- a/lib/libc/stdlib/ptsname.3
+++ b/lib/libc/stdlib/ptsname.3
@@ -32,8 +32,8 @@
.\" $FreeBSD$
.\"
.Dd August 20, 2008
-.Os
.Dt PTSNAME 3
+.Os
.Sh NAME
.Nm grantpt ,
.Nm ptsname ,
diff --git a/lib/libc/stdlib/rand.3 b/lib/libc/stdlib/rand.3
index 9b24795..3eb1a1d 100644
--- a/lib/libc/stdlib/rand.3
+++ b/lib/libc/stdlib/rand.3
@@ -32,7 +32,7 @@
.\" @(#)rand.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD$
.\"
-.Dd May 25, 1999
+.Dd October 6, 2010
.Dt RAND 3
.Os
.Sh NAME
@@ -100,7 +100,16 @@ provides the same functionality as
A pointer to the context value
.Fa ctx
must be supplied by the caller.
+.Pp
+For better generator quality, use
+.Xr random 3
+or
+.Xr lrand48 3 .
+Applications requiring cryptographic quality randomness should use
+.Xr arc4random 3 .
.Sh SEE ALSO
+.Xr arc4random 3 ,
+.Xr lrand48 3 ,
.Xr random 3 ,
.Xr random 4
.Sh STANDARDS
diff --git a/lib/libc/stdlib/random.3 b/lib/libc/stdlib/random.3
index aacf6e3..c6502bf 100644
--- a/lib/libc/stdlib/random.3
+++ b/lib/libc/stdlib/random.3
@@ -173,6 +173,7 @@ detects that the state information has been garbled, error
messages are printed on the standard error output.
.Sh SEE ALSO
.Xr arc4random 3 ,
+.Xr lrand48 3 ,
.Xr rand 3 ,
.Xr srand 3 ,
.Xr random 4
diff --git a/lib/libc/stdlib/realpath.3 b/lib/libc/stdlib/realpath.3
index 4205a3d..fec5258 100644
--- a/lib/libc/stdlib/realpath.3
+++ b/lib/libc/stdlib/realpath.3
@@ -31,7 +31,7 @@
.\" @(#)realpath.3 8.2 (Berkeley) 2/16/94
.\" $FreeBSD$
.\"
-.Dd February 16, 1994
+.Dd April 19, 2010
.Dt REALPATH 3
.Os
.Sh NAME
@@ -43,7 +43,7 @@
.In sys/param.h
.In stdlib.h
.Ft "char *"
-.Fn realpath "const char *pathname" "char resolved_path[PATH_MAX]"
+.Fn realpath "const char *pathname" "char *resolved_path"
.Sh DESCRIPTION
The
.Fn realpath
@@ -56,15 +56,16 @@ and
in
.Fa pathname ,
and copies the resulting absolute pathname into
-the memory referenced by
+the memory pointed to by
.Fa resolved_path .
The
.Fa resolved_path
argument
.Em must
-refer to a buffer capable of storing at least
+point to a buffer capable of storing at least
.Dv PATH_MAX
-characters.
+characters, or be
+.Dv NULL .
.Pp
The
.Fn realpath
@@ -82,13 +83,22 @@ The
function returns
.Fa resolved_path
on success.
+If the function was supplied
+.Dv NULL
+as
+.Fa resolved_path ,
+and operation did not cause errors, the returned value is
+a null-terminated string in a buffer allocated by a call to
+.Fn malloc 3 .
If an error occurs,
.Fn realpath
returns
.Dv NULL ,
-and
+and if
.Fa resolved_path
-contains the pathname which caused the problem.
+is not
+.Dv NULL ,
+the array that it points to contains the pathname which caused the problem.
.Sh ERRORS
The function
.Fn realpath
@@ -99,6 +109,18 @@ for any of the errors specified for the library functions
.Xr readlink 2
and
.Xr getcwd 3 .
+.Sh SEE ALSO
+.Xr getcwd 3
+.\" .Sh STANDARDS
+.\" The
+.\" .Fn realpath
+.\" function conforms to
+.\" .St -p1003.1-2001 .
+.Sh HISTORY
+The
+.Fn realpath
+function first appeared in
+.Bx 4.4 .
.Sh CAVEATS
This implementation of
.Fn realpath
@@ -111,10 +133,3 @@ under certain circumstances, return a relative
.Fa resolved_path
when given a relative
.Fa pathname .
-.Sh "SEE ALSO"
-.Xr getcwd 3
-.Sh HISTORY
-The
-.Fn realpath
-function first appeared in
-.Bx 4.4 .
diff --git a/lib/libc/stdlib/realpath.c b/lib/libc/stdlib/realpath.c
index 3082f5f..66bb8da 100644
--- a/lib/libc/stdlib/realpath.c
+++ b/lib/libc/stdlib/realpath.c
@@ -43,23 +43,36 @@ __FBSDID("$FreeBSD$");
#include "un-namespace.h"
/*
- * char *realpath(const char *path, char resolved[PATH_MAX]);
- *
* Find the real name of path, by removing all ".", ".." and symlink
* components. Returns (resolved) on success, or (NULL) on failure,
* in which case the path which caused trouble is left in (resolved).
*/
char *
-realpath(const char *path, char resolved[PATH_MAX])
+realpath(const char * __restrict path, char * __restrict resolved)
{
struct stat sb;
char *p, *q, *s;
size_t left_len, resolved_len;
unsigned symlinks;
- int serrno, slen;
+ int m, serrno, slen;
char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
+ if (path == NULL) {
+ errno = EINVAL;
+ return (NULL);
+ }
+ if (path[0] == '\0') {
+ errno = ENOENT;
+ return (NULL);
+ }
serrno = errno;
+ if (resolved == NULL) {
+ resolved = malloc(PATH_MAX);
+ if (resolved == NULL)
+ return (NULL);
+ m = 1;
+ } else
+ m = 0;
symlinks = 0;
if (path[0] == '/') {
resolved[0] = '/';
@@ -70,13 +83,20 @@ realpath(const char *path, char resolved[PATH_MAX])
left_len = strlcpy(left, path + 1, sizeof(left));
} else {
if (getcwd(resolved, PATH_MAX) == NULL) {
- strlcpy(resolved, ".", PATH_MAX);
+ if (m)
+ free(resolved);
+ else {
+ resolved[0] = '.';
+ resolved[1] = '\0';
+ }
return (NULL);
}
resolved_len = strlen(resolved);
left_len = strlcpy(left, path, sizeof(left));
}
if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
+ if (m)
+ free(resolved);
errno = ENAMETOOLONG;
return (NULL);
}
@@ -92,6 +112,8 @@ realpath(const char *path, char resolved[PATH_MAX])
p = strchr(left, '/');
s = p ? p : left + left_len;
if (s - left >= sizeof(next_token)) {
+ if (m)
+ free(resolved);
errno = ENAMETOOLONG;
return (NULL);
}
@@ -102,6 +124,8 @@ realpath(const char *path, char resolved[PATH_MAX])
memmove(left, s + 1, left_len + 1);
if (resolved[resolved_len - 1] != '/') {
if (resolved_len + 1 >= PATH_MAX) {
+ if (m)
+ free(resolved);
errno = ENAMETOOLONG;
return (NULL);
}
@@ -133,6 +157,8 @@ realpath(const char *path, char resolved[PATH_MAX])
*/
resolved_len = strlcat(resolved, next_token, PATH_MAX);
if (resolved_len >= PATH_MAX) {
+ if (m)
+ free(resolved);
errno = ENAMETOOLONG;
return (NULL);
}
@@ -141,16 +167,23 @@ realpath(const char *path, char resolved[PATH_MAX])
errno = serrno;
return (resolved);
}
+ if (m)
+ free(resolved);
return (NULL);
}
if (S_ISLNK(sb.st_mode)) {
if (symlinks++ > MAXSYMLINKS) {
+ if (m)
+ free(resolved);
errno = ELOOP;
return (NULL);
}
slen = readlink(resolved, symlink, sizeof(symlink) - 1);
- if (slen < 0)
+ if (slen < 0) {
+ if (m)
+ free(resolved);
return (NULL);
+ }
symlink[slen] = '\0';
if (symlink[0] == '/') {
resolved[1] = 0;
@@ -171,6 +204,8 @@ realpath(const char *path, char resolved[PATH_MAX])
if (p != NULL) {
if (symlink[slen - 1] != '/') {
if (slen + 1 >= sizeof(symlink)) {
+ if (m)
+ free(resolved);
errno = ENAMETOOLONG;
return (NULL);
}
@@ -179,6 +214,8 @@ realpath(const char *path, char resolved[PATH_MAX])
}
left_len = strlcat(symlink, left, sizeof(left));
if (left_len >= sizeof(left)) {
+ if (m)
+ free(resolved);
errno = ENAMETOOLONG;
return (NULL);
}
diff --git a/lib/libc/stdlib/strtod.3 b/lib/libc/stdlib/strtod.3
index ec8ad51..e19e5fe 100644
--- a/lib/libc/stdlib/strtod.3
+++ b/lib/libc/stdlib/strtod.3
@@ -32,7 +32,7 @@
.\" @(#)strtod.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD$
.\"
-.Dd March 2, 2003
+.Dd May 11, 2010
.Dt STRTOD 3
.Os
.Sh NAME
@@ -169,12 +169,10 @@ The
.Fn strtod
function
conforms to
-.St -isoC-99 ,
-with the exception of the bug noted below.
+.St -isoC-99 .
.Sh AUTHORS
The author of this software is
.An David M. Gay .
-.Pp
.Bd -literal
Copyright (c) 1998 by Lucent Technologies
All Rights Reserved
diff --git a/lib/libc/stdlib/strtonum.3 b/lib/libc/stdlib/strtonum.3
index 90f0b57..b83aadd 100644
--- a/lib/libc/stdlib/strtonum.3
+++ b/lib/libc/stdlib/strtonum.3
@@ -12,7 +12,7 @@
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
-.\" $OpenBSD: strtonum.3,v 1.12 2005/10/26 11:37:58 jmc Exp $
+.\" $OpenBSD: strtonum.3,v 1.13 2006/04/25 05:15:42 tedu Exp $
.\" $FreeBSD$
.\"
.Dd April 29, 2004
@@ -23,7 +23,6 @@
.Nd "reliably convert string value to an integer"
.Sh SYNOPSIS
.In stdlib.h
-.In limits.h
.Ft long long
.Fo strtonum
.Fa "const char *nptr"
OpenPOWER on IntegriCloud