diff --git a/libc/Android.mk b/libc/Android.mk index 72192117f..a8b4ebdd5 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -195,7 +195,6 @@ libc_common_src_files := \ bionic/ptsname_r.c \ bionic/pututline.c \ bionic/pwrite.c \ - bionic/realpath.c \ bionic/reboot.c \ bionic/recv.c \ bionic/sched_cpualloc.c \ @@ -317,6 +316,9 @@ libc_bionic_src_files := \ bionic/__vsprintf_chk.cpp \ bionic/wchar.cpp \ +libc_upstream_freebsd_src_files := \ + upstream-freebsd/lib/libc/stdlib/realpath.c \ + libc_upstream_netbsd_src_files := \ upstream-netbsd/common/lib/libc/hash/sha1/sha1.c \ upstream-netbsd/common/lib/libc/inet/inet_addr.c \ @@ -744,6 +746,29 @@ LOCAL_SYSTEM_SHARED_LIBRARIES := include $(BUILD_STATIC_LIBRARY) +# ======================================================== +# libc_freebsd.a - upstream FreeBSD C library code +# ======================================================== +# +# These files are built with the freebsd-compat.h header file +# automatically included. + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := $(libc_upstream_freebsd_src_files) +LOCAL_CFLAGS := \ + $(libc_common_cflags) \ + -I$(LOCAL_PATH)/upstream-freebsd \ + -I$(LOCAL_PATH)/upstream-freebsd/libc/include \ + -include upstream-freebsd/freebsd-compat.h +LOCAL_C_INCLUDES := $(libc_common_c_includes) +LOCAL_MODULE := libc_freebsd +LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk +LOCAL_SYSTEM_SHARED_LIBRARIES := + +include $(BUILD_STATIC_LIBRARY) + + # ======================================================== # libc_netbsd.a - upstream NetBSD C library code # ======================================================== @@ -796,7 +821,7 @@ LOCAL_CFLAGS := $(libc_common_cflags) \ LOCAL_C_INCLUDES := $(libc_common_c_includes) LOCAL_MODULE := libc_common LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk -LOCAL_WHOLE_STATIC_LIBRARIES := libbionic_ssp libc_bionic libc_netbsd +LOCAL_WHOLE_STATIC_LIBRARIES := libbionic_ssp libc_bionic libc_freebsd libc_netbsd LOCAL_SYSTEM_SHARED_LIBRARIES := include $(BUILD_STATIC_LIBRARY) diff --git a/libc/upstream-freebsd/freebsd-compat.h b/libc/upstream-freebsd/freebsd-compat.h new file mode 100644 index 000000000..08dec1529 --- /dev/null +++ b/libc/upstream-freebsd/freebsd-compat.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _BIONIC_FREEBSD_COMPAT_H_included +#define _BIONIC_FREEBSD_COMPAT_H_included + +#endif diff --git a/libc/bionic/realpath.c b/libc/upstream-freebsd/lib/libc/stdlib/realpath.c similarity index 88% rename from libc/bionic/realpath.c rename to libc/upstream-freebsd/lib/libc/stdlib/realpath.c index b9098319a..8fd5457af 100644 --- a/libc/bionic/realpath.c +++ b/libc/upstream-freebsd/lib/libc/stdlib/realpath.c @@ -30,8 +30,9 @@ static char sccsid[] = "@(#)realpath.c 8.1 (Berkeley) 2/16/94"; #endif /* LIBC_SCCS and not lint */ #include -__FBSDID("$FreeBSD: release/9.0.0/lib/libc/stdlib/realpath.c 217144 2011-01-08 11:04:30Z kib $"); +__FBSDID("$FreeBSD$"); +#include "namespace.h" #include #include @@ -39,6 +40,7 @@ __FBSDID("$FreeBSD: release/9.0.0/lib/libc/stdlib/realpath.c 217144 2011-01-08 1 #include #include #include +#include "un-namespace.h" /* * Find the real name of path, by removing all ".", ".." and symlink @@ -52,7 +54,7 @@ realpath(const char * __restrict path, char * __restrict resolved) char *p, *q, *s; size_t left_len, resolved_len; unsigned symlinks; - int m, serrno, slen; + int m, slen; char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX]; if (path == NULL) { @@ -63,7 +65,6 @@ realpath(const char * __restrict path, char * __restrict resolved) errno = ENOENT; return (NULL); } - serrno = errno; if (resolved == NULL) { resolved = malloc(PATH_MAX); if (resolved == NULL) @@ -130,8 +131,29 @@ realpath(const char * __restrict path, char * __restrict resolved) resolved[resolved_len++] = '/'; resolved[resolved_len] = '\0'; } - if (next_token[0] == '\0') + if (next_token[0] == '\0') { + /* + * Handle consequential slashes. The path + * before slash shall point to a directory. + * + * Only the trailing slashes are not covered + * by other checks in the loop, but we verify + * the prefix for any (rare) "//" or "/\0" + * occurence to not implement lookahead. + */ + if (lstat(resolved, &sb) != 0) { + if (m) + free(resolved); + return (NULL); + } + if (!S_ISDIR(sb.st_mode)) { + if (m) + free(resolved); + errno = ENOTDIR; + return (NULL); + } continue; + } else if (strcmp(next_token, ".") == 0) continue; else if (strcmp(next_token, "..") == 0) { @@ -149,9 +171,7 @@ realpath(const char * __restrict path, char * __restrict resolved) } /* - * Append the next path component and lstat() it. If - * lstat() fails we still can return successfully if - * there are no more path components left. + * Append the next path component and lstat() it. */ resolved_len = strlcat(resolved, next_token, PATH_MAX); if (resolved_len >= PATH_MAX) { @@ -161,10 +181,6 @@ realpath(const char * __restrict path, char * __restrict resolved) return (NULL); } if (lstat(resolved, &sb) != 0) { - if (errno == ENOENT && p == NULL) { - errno = serrno; - return (resolved); - } if (m) free(resolved); return (NULL); @@ -210,7 +226,8 @@ realpath(const char * __restrict path, char * __restrict resolved) symlink[slen] = '/'; symlink[slen + 1] = 0; } - left_len = strlcat(symlink, left, sizeof(left)); + left_len = strlcat(symlink, left, + sizeof(symlink)); if (left_len >= sizeof(left)) { if (m) free(resolved); diff --git a/libc/upstream-freebsd/namespace.h b/libc/upstream-freebsd/namespace.h new file mode 100644 index 000000000..a3f850ef6 --- /dev/null +++ b/libc/upstream-freebsd/namespace.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _BIONIC_FREEBSD_NAMESPACE_H_included +#define _BIONIC_FREEBSD_NAMESPACE_H_included + +#endif diff --git a/libc/upstream-freebsd/un-namespace.h b/libc/upstream-freebsd/un-namespace.h new file mode 100644 index 000000000..a3f850ef6 --- /dev/null +++ b/libc/upstream-freebsd/un-namespace.h @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _BIONIC_FREEBSD_NAMESPACE_H_included +#define _BIONIC_FREEBSD_NAMESPACE_H_included + +#endif diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp index 8735100d4..fed39f87b 100644 --- a/tests/stdlib_test.cpp +++ b/tests/stdlib_test.cpp @@ -17,6 +17,8 @@ #include #include +#include +#include #include #include @@ -70,3 +72,40 @@ TEST(stdlib, posix_memalign) { // Can't align to a non-power of 2. ASSERT_EQ(EINVAL, posix_memalign(&p, 81, 128)); } + +TEST(stdlib, realpath__NULL_filename) { + errno = 0; + char* p = realpath(NULL, NULL); + ASSERT_TRUE(p == NULL); + ASSERT_EQ(EINVAL, errno); +} + +TEST(stdlib, realpath__empty_filename) { + errno = 0; + char* p = realpath("", NULL); + ASSERT_TRUE(p == NULL); + ASSERT_EQ(ENOENT, errno); +} + +TEST(stdlib, realpath__ENOENT) { + errno = 0; + char* p = realpath("/this/directory/path/almost/certainly/does/not/exist", NULL); + ASSERT_TRUE(p == NULL); + ASSERT_EQ(ENOENT, errno); +} + +TEST(stdlib, realpath) { + // Get the name of this executable. + char executable_path[PATH_MAX]; + int rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path)); + ASSERT_NE(rc, -1); + executable_path[rc] = '\0'; + + char buf[PATH_MAX + 1]; + char* p = realpath("/proc/self/exe", buf); + ASSERT_STREQ(executable_path, p); + + p = realpath("/proc/self/exe", NULL); + ASSERT_STREQ(executable_path, p); + free(p); +}