Currently, realpath() calls lstat() on each component and -- if it's a
symlink -- then call readlink() on it. All it uses the lstat() for is "is
this a symlink".
Umm, readlink() will tell you that, more cheaply! Just call readlink()
directly and if it returns EINVAL then its not a symlink.
Diff below makes this change for both libc and dl_realpath inside ld.so
ok?
Philip Guenther
Index: lib/libc/stdlib/realpath.c
===================================================================
RCS file: /data/src/openbsd/src/lib/libc/stdlib/realpath.c,v
retrieving revision 1.20
diff -u -p -r1.20 realpath.c
--- lib/libc/stdlib/realpath.c 13 Oct 2015 20:55:37 -0000 1.20
+++ lib/libc/stdlib/realpath.c 28 Aug 2016 00:08:20 -0000
@@ -27,8 +27,6 @@
* SUCH DAMAGE.
*/
-#include <sys/stat.h>
-
#include <errno.h>
#include <stdlib.h>
#include <string.h>
@@ -47,7 +45,6 @@
char *
realpath(const char *path, char *resolved)
{
- struct stat sb;
char *p, *q, *s;
size_t left_len, resolved_len;
unsigned symlinks;
@@ -148,21 +145,27 @@ realpath(const char *path, char *resolve
errno = ENAMETOOLONG;
goto err;
}
- if (lstat(resolved, &sb) != 0) {
- if (errno == ENOENT && p == NULL) {
- errno = serrno;
- return (resolved);
+ slen = readlink(resolved, symlink, sizeof(symlink) - 1);
+ if (slen < 0) {
+ switch (errno) {
+ case EINVAL:
+ /* not a symlink, continue to next component */
+ continue;
+ case ENOENT:
+ if (p == NULL) {
+ errno = serrno;
+ return (resolved);
+ }
+ /* FALLTHROUGH */
+ default:
+ goto err;
}
- goto err;
- }
- if (S_ISLNK(sb.st_mode)) {
+ } else {
if (symlinks++ > SYMLOOP_MAX) {
errno = ELOOP;
goto err;
}
- slen = readlink(resolved, symlink, sizeof(symlink) - 1);
- if (slen < 0)
- goto err;
+
symlink[slen] = '\0';
if (symlink[0] == '/') {
resolved[1] = 0;
Index: libexec/ld.so/dl_realpath.c
===================================================================
RCS file: /data/src/openbsd/src/libexec/ld.so/dl_realpath.c,v
retrieving revision 1.3
diff -u -p -r1.3 dl_realpath.c
--- libexec/ld.so/dl_realpath.c 19 Oct 2014 03:56:28 -0000 1.3
+++ libexec/ld.so/dl_realpath.c 28 Aug 2016 00:37:22 -0000
@@ -28,11 +28,10 @@
*/
#include <sys/types.h>
+#include <sys/errno.h>
#include <limits.h>
#include "archdep.h"
-#define ENOENT -2
-
/*
* This file was copied from libc/stdlib/realpath.c and modified for ld.so's
* syscall method which returns -errno.
@@ -48,12 +47,11 @@
char *
_dl_realpath(const char *path, char *resolved)
{
- struct stat sb;
const char *p, *s;
char *q;
size_t left_len, resolved_len;
unsigned symlinks;
- int slen, mem_allocated, ret;
+ int slen, mem_allocated;
char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
if (path[0] == '\0') {
@@ -135,27 +133,33 @@ _dl_realpath(const char *path, char *res
}
/*
- * 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 readlink() it. If
+ * readlink() fails we still can return successfully if
+ * it wasn't a exists but isn't a symlink, or if there
+ * are no more path components left.
*/
resolved_len = _dl_strlcat(resolved, next_token, PATH_MAX);
if (resolved_len >= PATH_MAX) {
goto err;
}
- if ((ret = _dl_lstat(resolved, &sb)) != 0) {
- if (ret == ENOENT && p == NULL) {
- return (resolved);
+ slen = _dl_readlink(resolved, symlink, sizeof(symlink) - 1);
+ if (slen < 0) {
+ switch (slen) {
+ case -EINVAL:
+ /* not a symlink, continue to next component */
+ continue;
+ case -ENOENT:
+ if (p == NULL)
+ return (resolved);
+ /* FALLTHROUGH */
+ default:
+ goto err;
}
- goto err;
- }
- if (S_ISLNK(sb.st_mode)) {
+ } else {
if (symlinks++ > SYMLOOP_MAX) {
goto err;
}
- slen = _dl_readlink(resolved, symlink, sizeof(symlink)
- 1);
- if (slen < 0)
- goto err;
+
symlink[slen] = '\0';
if (symlink[0] == '/') {
resolved[1] = 0;