public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/4] Sync getcwd with gnulib
@ 2020-08-26 21:02 Adhemerval Zanella
  2020-08-26 21:02 ` [PATCH 2/4] linux: Remove __ASSUME_ATFCTS Adhemerval Zanella
                   ` (4 more replies)
  0 siblings, 5 replies; 21+ messages in thread
From: Adhemerval Zanella @ 2020-08-26 21:02 UTC (permalink / raw)
  To: libc-alpha

It sync with gnulib version d4c637e57.  The main changes are:

  - It defines HAVE_OPENAT to 1, D_INO_IN_DIRENT to 1,
    HAVE_MSVC_INVALID_PARAMETER_HANDLER to 0, and
    HAVE_MINIMALLY_WORKING_GETCWD to 0 for _LIBC.

  - It does not include pathmax.h header (glibc does not provide it).

  - It defines and used __close_nocancel_nostatus, stat64, __fstat64,
   __fstatat64, __lstat64, __fdopendir, __openat, __rewinddir,
   __openat64 for !LIBC to enable internal LFS calls.

This requires glibc changes should integrate seamlessly on gnulib.

Checked on x86_64-linux-gnu and i686-linux-gnu.
---
 sysdeps/posix/getcwd.c | 782 +++++++++++++++++++----------------------
 1 file changed, 369 insertions(+), 413 deletions(-)

diff --git a/sysdeps/posix/getcwd.c b/sysdeps/posix/getcwd.c
index f00b337a13..3876e1a641 100644
--- a/sysdeps/posix/getcwd.c
+++ b/sysdeps/posix/getcwd.c
@@ -15,518 +15,474 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-/* Wants:
-   AC_STDC_HEADERS
-   AC_DIR_HEADER
-   AC_UNISTD_H
-   AC_MEMORY_H
-   AC_CONST
-   AC_ALLOCA
- */
-
-/* AIX requires this to be the first thing in the file.  */
-#if defined _AIX && !defined __GNUC__
- #pragma alloca
-#endif
-
-#ifdef	HAVE_CONFIG_H
-# include "config.h"
+#if !_LIBC
+# include <config.h>
+# include <unistd.h>
+#else
+# define HAVE_OPENAT 1
+# define D_INO_IN_DIRENT 1
+# define HAVE_MSVC_INVALID_PARAMETER_HANDLER 0
+# define HAVE_MINIMALLY_WORKING_GETCWD 0
 #endif
 
 #include <errno.h>
-#include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <stdbool.h>
+#include <stddef.h>
 
-#ifdef	STDC_HEADERS
-# include <stddef.h>
-#endif
+#include <fcntl.h> /* For AT_FDCWD on Solaris 9.  */
 
-#if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS
-extern int errno;
+/* If this host provides the openat function or if we're using the
+   gnulib replacement function with a native fdopendir, then enable
+   code below to make getcwd more efficient and robust.  */
+#if defined HAVE_OPENAT || (defined GNULIB_OPENAT && defined HAVE_FDOPENDIR)
+# define HAVE_OPENAT_SUPPORT 1
+#else
+# define HAVE_OPENAT_SUPPORT 0
 #endif
+
 #ifndef __set_errno
-# define __set_errno(val) errno = (val)
+# define __set_errno(val) (errno = (val))
 #endif
 
-#ifndef	NULL
-# define NULL	0
+#include <dirent.h>
+#ifndef _D_EXACT_NAMLEN
+# define _D_EXACT_NAMLEN(d) strlen ((d)->d_name)
 #endif
-
-#if defined USGr3 && !defined DIRENT
-# define DIRENT
-#endif /* USGr3 */
-#if defined Xenix && !defined SYSNDIR
-# define SYSNDIR
-#endif /* Xenix */
-
-#if defined POSIX || defined DIRENT || defined __GNU_LIBRARY__
-# include <dirent.h>
-# ifndef __GNU_LIBRARY__
-#  define D_NAMLEN(d) strlen((d)->d_name)
-# else
-#  define HAVE_D_NAMLEN
-#  define D_NAMLEN(d) ((d)->d_namlen)
-# endif
-#else /* not POSIX or DIRENT */
-# define dirent		direct
-# define D_NAMLEN(d)	((d)->d_namlen)
-# define HAVE_D_NAMLEN
-# if defined USG && !defined sgi
-#  if defined SYSNDIR
-#   include <sys/ndir.h>
-#  else /* Not SYSNDIR */
-#   include "ndir.h"
-#  endif /* SYSNDIR */
-# else /* not USG */
-#  include <sys/dir.h>
-# endif /* USG */
-#endif /* POSIX or DIRENT or __GNU_LIBRARY__ */
-
-#if defined HAVE_UNISTD_H || defined __GNU_LIBRARY__
-# include <unistd.h>
+#ifndef _D_ALLOC_NAMLEN
+# define _D_ALLOC_NAMLEN(d) (_D_EXACT_NAMLEN (d) + 1)
 #endif
 
-#if defined STDC_HEADERS || defined __GNU_LIBRARY__ || defined POSIX
-# include <stdlib.h>
-# include <string.h>
-# define ANSI_STRING
-#else	/* No standard headers.  */
-
-# ifdef	USG
-
-#  include <string.h>
-#  ifdef NEED_MEMORY_H
-#   include <memory.h>
-#  endif
-#  define	ANSI_STRING
-
-# else	/* Not USG.  */
-
-#  ifdef NeXT
-
-#   include <string.h>
-
-#  else	/* Not NeXT.  */
-
-#   include <strings.h>
-
-#   ifndef bcmp
-extern int bcmp ();
-#   endif
-#   ifndef bzero
-extern void bzero ();
-#   endif
-#   ifndef bcopy
-extern void bcopy ();
-#   endif
-
-#  endif /* NeXT. */
-
-# endif	/* USG.  */
-
-extern char *malloc (), *realloc ();
-extern void free ();
-
-#endif /* Standard headers.  */
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
 
-#ifndef	ANSI_STRING
-# define memcpy(d, s, n)	bcopy((s), (d), (n))
-# define memmove memcpy
-#endif	/* Not ANSI_STRING.  */
-
-#ifndef MAX
-# define MAX(a, b) ((a) < (b) ? (b) : (a))
-#endif
-
-#ifdef _LIBC
+#if _LIBC
 # ifndef mempcpy
 #  define mempcpy __mempcpy
 # endif
-# define HAVE_MEMPCPY	1
 #endif
 
-#if !defined __alloca && !defined __GNU_LIBRARY__
-
-# ifdef	__GNUC__
-#  undef alloca
-#  define alloca(n)	__builtin_alloca (n)
-# else	/* Not GCC.  */
-#  if	defined sparc || defined HAVE_ALLOCA_H
-#   include <alloca.h>
-#  else	/* Not sparc or HAVE_ALLOCA_H.  */
-#   ifndef _AIX
-extern char *alloca ();
-#   endif /* Not _AIX.  */
-#  endif /* sparc or HAVE_ALLOCA_H.  */
-# endif	/* GCC.  */
-
-# define __alloca	alloca
-
+#ifndef MAX
+# define MAX(a, b) ((a) < (b) ? (b) : (a))
+#endif
+#ifndef MIN
+# define MIN(a, b) ((a) < (b) ? (a) : (b))
 #endif
 
-#if defined HAVE_LIMITS_H || defined STDC_HEADERS || defined __GNU_LIBRARY__
-# include <limits.h>
-#else
-# include <sys/param.h>
+/* In this file, PATH_MAX only serves as a threshold for choosing among two
+   algorithms.  */
+#ifndef PATH_MAX
+# define PATH_MAX 8192
 #endif
 
-#if defined _LIBC
-# include <not-cancel.h>
-# include <kernel-features.h>
+#if D_INO_IN_DIRENT
+# define MATCHING_INO(dp, ino) ((dp)->d_ino == (ino))
 #else
-# define __openat64_nocancel(dfd, name, mode) openat64 (dfd, name, mode)
-# define __close_nocancel_nostatus(fd) close (fd)
+# define MATCHING_INO(dp, ino) true
 #endif
 
-#ifndef PATH_MAX
-# ifdef	MAXPATHLEN
-#  define PATH_MAX MAXPATHLEN
-# else
-#  define PATH_MAX 1024
-# endif
+#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
+# include "msvc-inval.h"
 #endif
 
-#if !defined STDC_HEADERS && !defined __GNU_LIBRARY__
-# undef	size_t
-# define size_t	unsigned int
+#if !_LIBC
+# define __close_nocancel_nostatus close
+# define __getcwd rpl_getcwd
+# define stat64    stat
+# define __fstat64 fstat
+# define __fstatat64 fstatat
+# define __lstat64 lstat
+# define __closedir closedir
+# define __opendir opendir
+# define __readdir readdir
+# define __fdopendir fdopendir
+# define __openat openat
+# define __rewinddir rewinddir
+# define __openat64 openat
+#else
+# include <not-cancel.h>
 #endif
 
-#ifndef __GNU_LIBRARY__
-# define __lstat64	stat64
+/* The results of opendir() in this file are not used with dirfd and fchdir,
+   and we do not leak fds to any single-threaded code that could use stdio,
+   therefore save some unnecessary recursion in fchdir.c.
+   FIXME - if the kernel ever adds support for multi-thread safety for
+   avoiding standard fds, then we should use opendir_safer and
+   openat_safer.  */
+#ifdef GNULIB_defined_opendir
+# undef opendir
 #endif
-
-#ifndef _LIBC
-# define __rewinddir	rewinddir
+#ifdef GNULIB_defined_closedir
+# undef closedir
 #endif
 \f
-#ifndef _LIBC
-# define __getcwd getcwd
-#endif
+#if defined _WIN32 && !defined __CYGWIN__
+# if HAVE_MSVC_INVALID_PARAMETER_HANDLER
+static char *
+getcwd_nothrow (char *buf, size_t size)
+{
+  char *result;
 
-#ifndef GETCWD_RETURN_TYPE
-# define GETCWD_RETURN_TYPE char *
-#endif
+  TRY_MSVC_INVAL
+    {
+      result = _getcwd (buf, size);
+    }
+  CATCH_MSVC_INVAL
+    {
+      result = NULL;
+      errno = ERANGE;
+    }
+  DONE_MSVC_INVAL;
 
-#ifdef __ASSUME_ATFCTS
-# define __have_atfcts 1
-#elif IS_IN (rtld)
-static int __rtld_have_atfcts;
-# define __have_atfcts __rtld_have_atfcts
+  return result;
+}
+# else
+#  define getcwd_nothrow _getcwd
+# endif
+# define getcwd_system getcwd_nothrow
+#else
+# define getcwd_system getcwd
 #endif
 
-/* Get the pathname of the current working directory, and put it in SIZE
-   bytes of BUF.  Returns NULL if the directory couldn't be determined or
-   SIZE was too small.  If successful, returns BUF.  In GNU, if BUF is
-   NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
-   unless SIZE == 0, in which case it is as big as necessary.  */
+/* Get the name of the current working directory, and put it in SIZE
+   bytes of BUF.  Returns NULL with errno set if the directory couldn't be
+   determined or SIZE was too small.  If successful, returns BUF.  In GNU,
+   if BUF is NULL, an array is allocated with 'malloc'; the array is SIZE
+   bytes long, unless SIZE == 0, in which case it is as big as necessary.  */
 
-GETCWD_RETURN_TYPE
+char *
 __getcwd (char *buf, size_t size)
 {
-#ifndef __ASSUME_ATFCTS
-  static const char dots[]
-    = "../../../../../../../../../../../../../../../../../../../../../../../\
-../../../../../../../../../../../../../../../../../../../../../../../../../../\
-../../../../../../../../../../../../../../../../../../../../../../../../../..";
-  const char *dotp = &dots[sizeof (dots)];
-  const char *dotlist = dots;
-  size_t dotsize = sizeof (dots) - 1;
+  /* Lengths of big file name components and entire file names, and a
+     deep level of file name nesting.  These numbers are not upper
+     bounds; they are merely large values suitable for initial
+     allocations, designed to be large enough for most real-world
+     uses.  */
+  enum
+    {
+      BIG_FILE_NAME_COMPONENT_LENGTH = 255,
+      BIG_FILE_NAME_LENGTH = MIN (4095, PATH_MAX - 1),
+      DEEP_NESTING = 100
+    };
+
+#if HAVE_OPENAT_SUPPORT
+  int fd = AT_FDCWD;
+  bool fd_needs_closing = false;
+#else
+  char dots[DEEP_NESTING * sizeof ".." + BIG_FILE_NAME_COMPONENT_LENGTH + 1];
+  char *dotlist = dots;
+  size_t dotsize = sizeof dots;
+  size_t dotlen = 0;
 #endif
-  int prev_errno = errno;
   DIR *dirstream = NULL;
-  bool fd_needs_closing = false;
-  int fd = AT_FDCWD;
-
-  char *path;
-#ifndef NO_ALLOCATION
+  dev_t rootdev, thisdev;
+  ino_t rootino, thisino;
+  char *dir;
+  register char *dirp;
+  struct stat64 st;
   size_t allocated = size;
+  size_t used;
+
+#if HAVE_MINIMALLY_WORKING_GETCWD
+  /* If AT_FDCWD is not defined, the algorithm below is O(N**2) and
+     this is much slower than the system getcwd (at least on
+     GNU/Linux).  So trust the system getcwd's results unless they
+     look suspicious.
+
+     Use the system getcwd even if we have openat support, since the
+     system getcwd works even when a parent is unreadable, while the
+     openat-based approach does not.
+
+     But on AIX 5.1..7.1, the system getcwd is not even minimally
+     working: If the current directory name is slightly longer than
+     PATH_MAX, it omits the first directory component and returns
+     this wrong result with errno = 0.  */
+
+# undef getcwd
+  dir = getcwd_system (buf, size);
+  if (dir || (size && errno == ERANGE))
+    return dir;
+
+  /* Solaris getcwd (NULL, 0) fails with errno == EINVAL, but it has
+     internal magic that lets it work even if an ancestor directory is
+     inaccessible, which is better in many cases.  So in this case try
+     again with a buffer that's almost always big enough.  */
+  if (errno == EINVAL && buf == NULL && size == 0)
+    {
+      char big_buffer[BIG_FILE_NAME_LENGTH + 1];
+      dir = getcwd_system (big_buffer, sizeof big_buffer);
+      if (dir)
+        return strdup (dir);
+    }
+
+# if HAVE_PARTLY_WORKING_GETCWD
+  /* The system getcwd works, except it sometimes fails when it
+     shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT.    */
+  if (errno != ERANGE && errno != ENAMETOOLONG && errno != ENOENT)
+    return NULL;
+# endif
+#endif
   if (size == 0)
     {
       if (buf != NULL)
-	{
-	  __set_errno (EINVAL);
-	  return NULL;
-	}
+        {
+          __set_errno (EINVAL);
+          return NULL;
+        }
 
-      allocated = PATH_MAX + 1;
+      allocated = BIG_FILE_NAME_LENGTH + 1;
     }
 
   if (buf == NULL)
     {
-      path = malloc (allocated);
-      if (path == NULL)
-	return NULL;
+      dir = malloc (allocated);
+      if (dir == NULL)
+        return NULL;
     }
   else
-#else
-# define allocated size
-#endif
-    path = buf;
+    dir = buf;
 
-  char *pathp = path + allocated;
-  *--pathp = '\0';
+  dirp = dir + allocated;
+  *--dirp = '\0';
 
-  struct stat64 st;
   if (__lstat64 (".", &st) < 0)
     goto lose;
-  dev_t thisdev = st.st_dev;
-  ino_t thisino = st.st_ino;
+  thisdev = st.st_dev;
+  thisino = st.st_ino;
 
   if (__lstat64 ("/", &st) < 0)
     goto lose;
-  dev_t rootdev = st.st_dev;
-  ino_t rootino = st.st_ino;
+  rootdev = st.st_dev;
+  rootino = st.st_ino;
 
   while (!(thisdev == rootdev && thisino == rootino))
     {
-      if (__have_atfcts >= 0)
-	  fd = __openat64_nocancel (fd, "..", O_RDONLY | O_CLOEXEC);
-      else
-	fd = -1;
-      if (fd >= 0)
-	{
-	  fd_needs_closing = true;
-	  if (__fstat64 (fd, &st) < 0)
-	    goto lose;
-	}
-#ifndef __ASSUME_ATFCTS
-      else if (errno == ENOSYS)
-	{
-	  __have_atfcts = -1;
-
-	  /* Look at the parent directory.  */
-	  if (dotp == dotlist)
-	    {
-# ifdef NO_ALLOCATION
-	      __set_errno (ENOMEM);
-	      goto lose;
-# else
-	      /* My, what a deep directory tree you have, Grandma.  */
-	      char *new;
-	      if (dotlist == dots)
-		{
-		  new = malloc (dotsize * 2 + 1);
-		  if (new == NULL)
-		    goto lose;
-#  ifdef HAVE_MEMPCPY
-		  dotp = mempcpy (new, dots, dotsize);
-#  else
-		  memcpy (new, dots, dotsize);
-		  dotp = &new[dotsize];
-#  endif
-		}
-	      else
-		{
-		  new = realloc ((void *) dotlist, dotsize * 2 + 1);
-		  if (new == NULL)
-		    goto lose;
-		  dotp = &new[dotsize];
-		}
-#  ifdef HAVE_MEMPCPY
-	      *((char *) mempcpy ((char *) dotp, new, dotsize)) = '\0';
-	      dotsize *= 2;
-#  else
-	      memcpy ((char *) dotp, new, dotsize);
-	      dotsize *= 2;
-	      new[dotsize] = '\0';
-#  endif
-	      dotlist = new;
-# endif
-	    }
-
-	  dotp -= 3;
+      struct dirent *d;
+      dev_t dotdev;
+      ino_t dotino;
+      bool mount_point;
+      int parent_status;
+      size_t dirroom;
+      size_t namlen;
+      bool use_d_ino = true;
 
-	  /* Figure out if this directory is a mount point.  */
-	  if (__lstat64 (dotp, &st) < 0)
-	    goto lose;
-	}
+      /* Look at the parent directory.  */
+#if HAVE_OPENAT_SUPPORT
+      fd = __openat64 (fd, "..", O_RDONLY);
+      if (fd < 0)
+        goto lose;
+      fd_needs_closing = true;
+      parent_status = __fstat64 (fd, &st);
+#else
+      dotlist[dotlen++] = '.';
+      dotlist[dotlen++] = '.';
+      dotlist[dotlen] = '\0';
+      parent_status = __lstat64 (dotlist, &st);
 #endif
-      else
-	goto lose;
+      if (parent_status != 0)
+        goto lose;
 
       if (dirstream && __closedir (dirstream) != 0)
-	{
-	  dirstream = NULL;
-	  goto lose;
-       }
+        {
+          dirstream = NULL;
+          goto lose;
+        }
 
-      dev_t dotdev = st.st_dev;
-      ino_t dotino = st.st_ino;
-      bool mount_point = dotdev != thisdev;
+      /* Figure out if this directory is a mount point.  */
+      dotdev = st.st_dev;
+      dotino = st.st_ino;
+      mount_point = dotdev != thisdev;
 
       /* Search for the last directory.  */
-      if (__have_atfcts >= 0)
-	dirstream = __fdopendir (fd);
-#ifndef __ASSUME_ATFCTS
-      else
-	dirstream = __opendir (dotp);
-#endif
+#if HAVE_OPENAT_SUPPORT
+      dirstream = __fdopendir (fd);
       if (dirstream == NULL)
-	goto lose;
+        goto lose;
       fd_needs_closing = false;
-
-      struct dirent *d;
-      bool use_d_ino = true;
-      while (1)
-	{
-	  /* Clear errno to distinguish EOF from error if readdir returns
-	     NULL.  */
-	  __set_errno (0);
-	  d = __readdir (dirstream);
-	  if (d == NULL)
-	    {
-	      if (errno == 0)
-		{
-		  /* When we've iterated through all directory entries
-		     without finding one with a matching d_ino, rewind the
-		     stream and consider each name again, but this time, using
-		     lstat64.  This is necessary in a chroot on at least one
-		     system.  */
-		  if (use_d_ino)
-		    {
-		      use_d_ino = false;
-		      __rewinddir (dirstream);
-		      continue;
-		    }
-
-		  /* EOF on dirstream, which means that the current directory
-		     has been removed.  */
-		  __set_errno (ENOENT);
-		}
-	      goto lose;
-	    }
-
-#ifdef _DIRENT_HAVE_D_TYPE
-	  if (d->d_type != DT_DIR && d->d_type != DT_UNKNOWN)
-	    continue;
-#endif
-	  if (d->d_name[0] == '.'
-	      && (d->d_name[1] == '\0'
-		  || (d->d_name[1] == '.' && d->d_name[2] == '\0')))
-	    continue;
-	  if (use_d_ino && !mount_point && (ino_t) d->d_ino != thisino)
-	    continue;
-
-	  if (__have_atfcts >= 0)
-	    {
-	      /* We don't fail here if we cannot stat64() a directory entry.
-		 This can happen when (network) filesystems fail.  If this
-		 entry is in fact the one we are looking for we will find
-		 out soon as we reach the end of the directory without
-		 having found anything.  */
-	      if (__fstatat64 (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
-		continue;
-	    }
-#ifndef __ASSUME_ATFCTS
-	  else
-	    {
-	      char name[dotlist + dotsize - dotp + 1 + _D_ALLOC_NAMLEN (d)];
-# ifdef HAVE_MEMPCPY
-	      char *tmp = mempcpy (name, dotp, dotlist + dotsize - dotp);
-	      *tmp++ = '/';
-	      strcpy (tmp, d->d_name);
-# else
-	      memcpy (name, dotp, dotlist + dotsize - dotp);
-	      name[dotlist + dotsize - dotp] = '/';
-	      strcpy (&name[dotlist + dotsize - dotp + 1], d->d_name);
-# endif
-	      /* We don't fail here if we cannot stat64() a directory entry.
-		 This can happen when (network) filesystems fail.  If this
-		 entry is in fact the one we are looking for we will find
-		 out soon as we reach the end of the directory without
-		 having found anything.  */
-	      if (__lstat64 (name, &st) < 0)
-		continue;
-	    }
+#else
+      dirstream = __opendir (dotlist);
+      if (dirstream == NULL)
+        goto lose;
+      dotlist[dotlen++] = '/';
 #endif
-	  if (S_ISDIR (st.st_mode)
-	      && st.st_dev == thisdev && st.st_ino == thisino)
-	    break;
-	}
-
-      size_t namlen = _D_EXACT_NAMLEN (d);
-
-      if ((size_t) (pathp - path) <= namlen)
-	{
-#ifndef NO_ALLOCATION
-	  if (size == 0)
-	    {
-	      size_t oldsize = allocated;
-
-	      allocated = 2 * MAX (allocated, namlen);
-	      char *tmp = realloc (path, allocated);
-	      if (tmp == NULL)
-		goto lose;
-
-	      /* Move current contents up to the end of the buffer.
-		 This is guaranteed to be non-overlapping.  */
-	      pathp = memcpy (tmp + allocated - (path + oldsize - pathp),
-			      tmp + (pathp - path),
-			      path + oldsize - pathp);
-	      path = tmp;
-	    }
-	  else
+      for (;;)
+        {
+          /* Clear errno to distinguish EOF from error if readdir returns
+             NULL.  */
+          __set_errno (0);
+          d = __readdir (dirstream);
+
+          /* When we've iterated through all directory entries without finding
+             one with a matching d_ino, rewind the stream and consider each
+             name again, but this time, using lstat.  This is necessary in a
+             chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
+             .., ../.., ../../.., etc. all had the same device number, yet the
+             d_ino values for entries in / did not match those obtained
+             via lstat.  */
+          if (d == NULL && errno == 0 && use_d_ino)
+            {
+              use_d_ino = false;
+              __rewinddir (dirstream);
+              d = __readdir (dirstream);
+            }
+
+          if (d == NULL)
+            {
+              if (errno == 0)
+                /* EOF on dirstream, which can mean e.g., that the current
+                   directory has been removed.  */
+                __set_errno (ENOENT);
+              goto lose;
+            }
+          if (d->d_name[0] == '.' &&
+              (d->d_name[1] == '\0' ||
+               (d->d_name[1] == '.' && d->d_name[2] == '\0')))
+            continue;
+
+          if (use_d_ino)
+            {
+              bool match = (MATCHING_INO (d, thisino) || mount_point);
+              if (! match)
+                continue;
+            }
+
+          {
+            int entry_status;
+#if HAVE_OPENAT_SUPPORT
+            entry_status = __fstatat64 (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW);
+#else
+            /* Compute size needed for this file name, or for the file
+               name ".." in the same directory, whichever is larger.
+               Room for ".." might be needed the next time through
+               the outer loop.  */
+            size_t name_alloc = _D_ALLOC_NAMLEN (d);
+            size_t filesize = dotlen + MAX (sizeof "..", name_alloc);
+
+            if (filesize < dotlen)
+              goto memory_exhausted;
+
+            if (dotsize < filesize)
+              {
+                /* My, what a deep directory tree you have, Grandma.  */
+                size_t newsize = MAX (filesize, dotsize * 2);
+                size_t i;
+                if (newsize < dotsize)
+                  goto memory_exhausted;
+                if (dotlist != dots)
+                  free (dotlist);
+                dotlist = malloc (newsize);
+                if (dotlist == NULL)
+                  goto lose;
+                dotsize = newsize;
+
+                i = 0;
+                do
+                  {
+                    dotlist[i++] = '.';
+                    dotlist[i++] = '.';
+                    dotlist[i++] = '/';
+                  }
+                while (i < dotlen);
+              }
+
+            memcpy (dotlist + dotlen, d->d_name, _D_ALLOC_NAMLEN (d));
+            entry_status = __lstat64 (dotlist, &st);
 #endif
-	    {
-	      __set_errno (ERANGE);
-	      goto lose;
-	    }
-	}
-      pathp -= namlen;
-      (void) memcpy (pathp, d->d_name, namlen);
-      *--pathp = '/';
+            /* We don't fail here if we cannot stat() a directory entry.
+               This can happen when (network) file systems fail.  If this
+               entry is in fact the one we are looking for we will find
+               out soon as we reach the end of the directory without
+               having found anything.  */
+            if (entry_status == 0 && S_ISDIR (st.st_mode)
+                && st.st_dev == thisdev && st.st_ino == thisino)
+              break;
+          }
+        }
+
+      dirroom = dirp - dir;
+      namlen = _D_EXACT_NAMLEN (d);
+
+      if (dirroom <= namlen)
+        {
+          if (size != 0)
+            {
+              __set_errno (ERANGE);
+              goto lose;
+            }
+          else
+            {
+              char *tmp;
+              size_t oldsize = allocated;
+
+              allocated += MAX (allocated, namlen);
+              if (allocated < oldsize
+                  || ! (tmp = realloc (dir, allocated)))
+                goto memory_exhausted;
+
+              /* Move current contents up to the end of the buffer.
+                 This is guaranteed to be non-overlapping.  */
+              dirp = memcpy (tmp + allocated - (oldsize - dirroom),
+                             tmp + dirroom,
+                             oldsize - dirroom);
+              dir = tmp;
+            }
+        }
+      dirp -= namlen;
+      memcpy (dirp, d->d_name, namlen);
+      *--dirp = '/';
 
       thisdev = dotdev;
       thisino = dotino;
     }
 
-  if (dirstream != NULL && __closedir (dirstream) != 0)
+  if (dirstream && __closedir (dirstream) != 0)
     {
       dirstream = NULL;
       goto lose;
     }
 
-  if (pathp == &path[allocated - 1])
-    *--pathp = '/';
+  if (dirp == &dir[allocated - 1])
+    *--dirp = '/';
 
-#ifndef __ASSUME_ATFCTS
+#if ! HAVE_OPENAT_SUPPORT
   if (dotlist != dots)
-    free ((void *) dotlist);
+    free (dotlist);
 #endif
 
-  size_t used = path + allocated - pathp;
-  memmove (path, pathp, used);
+  used = dir + allocated - dirp;
+  memmove (dir, dirp, used);
 
   if (size == 0)
     /* Ensure that the buffer is only as large as necessary.  */
-    buf = realloc (path, used);
+    buf = (used < allocated ? realloc (dir, used) : dir);
 
   if (buf == NULL)
-    /* Either buf was NULL all along, or `realloc' failed but
+    /* Either buf was NULL all along, or 'realloc' failed but
        we still have the original string.  */
-    buf = path;
-
-  /* Restore errno on successful return.  */
-  __set_errno (prev_errno);
+    buf = dir;
 
   return buf;
 
- lose:;
-  int save_errno = errno;
-#ifndef __ASSUME_ATFCTS
-  if (dotlist != dots)
-    free ((void *) dotlist);
-#endif
-  if (dirstream != NULL)
-    __closedir (dirstream);
-  if (fd_needs_closing)
-    __close_nocancel_nostatus (fd);
-#ifndef NO_ALLOCATION
-  if (buf == NULL)
-    free (path);
+ memory_exhausted:
+  __set_errno (ENOMEM);
+ lose:
+  {
+    int save = errno;
+    if (dirstream)
+      __closedir (dirstream);
+#if HAVE_OPENAT_SUPPORT
+    if (fd_needs_closing)
+       __close_nocancel_nostatus (fd);
+#else
+    if (dotlist != dots)
+      free (dotlist);
 #endif
-  __set_errno (save_errno);
+    if (buf == NULL)
+      free (dir);
+    __set_errno (save);
+  }
   return NULL;
 }
 
-- 
2.25.1


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH 2/4] linux: Remove __ASSUME_ATFCTS
  2020-08-26 21:02 [PATCH 1/4] Sync getcwd with gnulib Adhemerval Zanella
@ 2020-08-26 21:02 ` Adhemerval Zanella
  2020-08-26 21:02 ` [PATCH 3/4] Use LFS readdir in generic POSIX getcwd [BZ# 22899] Adhemerval Zanella
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 21+ messages in thread
From: Adhemerval Zanella @ 2020-08-26 21:02 UTC (permalink / raw)
  To: libc-alpha

The __have_atfcts is not used anywhere.

Checked on x86_64-linux-gnu.
---
 io/openat.c                               | 5 -----
 sysdeps/unix/sysv/linux/kernel-features.h | 4 ----
 2 files changed, 9 deletions(-)

diff --git a/io/openat.c b/io/openat.c
index a7ce65bee2..2f5a9f04de 100644
--- a/io/openat.c
+++ b/io/openat.c
@@ -23,11 +23,6 @@
 #include <sys/stat.h>
 #include <kernel-features.h>
 
-/* Some mostly-generic code (e.g. sysdeps/posix/getcwd.c) uses this variable
-   if __ASSUME_ATFCTS is not defined.  */
-#ifndef __ASSUME_ATFCTS
-int __have_atfcts;
-#endif
 
 /* Open FILE with access OFLAG.  Interpret relative paths relative to
    the directory associated with FD.  If O_CREAT or O_TMPFILE is in OFLAG, a
diff --git a/sysdeps/unix/sysv/linux/kernel-features.h b/sysdeps/unix/sysv/linux/kernel-features.h
index a6bbc3c94e..e648eecc0d 100644
--- a/sysdeps/unix/sysv/linux/kernel-features.h
+++ b/sysdeps/unix/sysv/linux/kernel-features.h
@@ -49,10 +49,6 @@
    SH this appeared first in 2.6.19-rc1, on ia64 in 2.6.22-rc1.  */
 #define __ASSUME_PSELECT	1
 
-/* The *at syscalls were introduced just after 2.6.16-rc1.  On PPC
-   they were introduced in 2.6.17-rc1, on SH in 2.6.19-rc1.  */
-#define __ASSUME_ATFCTS	1
-
 /* Support for inter-process robust mutexes was added in 2.6.17 (but
    some architectures lack futex_atomic_cmpxchg_inatomic in some
    configurations).  */
-- 
2.25.1


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH 3/4] Use LFS readdir in generic POSIX getcwd [BZ# 22899]
  2020-08-26 21:02 [PATCH 1/4] Sync getcwd with gnulib Adhemerval Zanella
  2020-08-26 21:02 ` [PATCH 2/4] linux: Remove __ASSUME_ATFCTS Adhemerval Zanella
@ 2020-08-26 21:02 ` Adhemerval Zanella
  2020-08-27  9:58   ` Florian Weimer
  2020-08-26 21:02 ` [PATCH 4/4] io: Reorganize the getcwd implementation Adhemerval Zanella
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 21+ messages in thread
From: Adhemerval Zanella @ 2020-08-26 21:02 UTC (permalink / raw)
  To: libc-alpha

Checked on x86_64-linux-gnu and i686-linux-gnu.
---
 sysdeps/posix/getcwd.c              | 9 +++++----
 sysdeps/unix/sysv/linux/readdir64.c | 4 ++++
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/sysdeps/posix/getcwd.c b/sysdeps/posix/getcwd.c
index 3876e1a641..1e6fc9b845 100644
--- a/sysdeps/posix/getcwd.c
+++ b/sysdeps/posix/getcwd.c
@@ -96,11 +96,12 @@
 # define __lstat64 lstat
 # define __closedir closedir
 # define __opendir opendir
-# define __readdir readdir
+# define __readdir64 readdir
 # define __fdopendir fdopendir
 # define __openat openat
 # define __rewinddir rewinddir
 # define __openat64 openat
+# define dirent64 dirent
 #else
 # include <not-cancel.h>
 #endif
@@ -259,7 +260,7 @@ __getcwd (char *buf, size_t size)
 
   while (!(thisdev == rootdev && thisino == rootino))
     {
-      struct dirent *d;
+      struct dirent64 *d;
       dev_t dotdev;
       ino_t dotino;
       bool mount_point;
@@ -312,7 +313,7 @@ __getcwd (char *buf, size_t size)
           /* Clear errno to distinguish EOF from error if readdir returns
              NULL.  */
           __set_errno (0);
-          d = __readdir (dirstream);
+          d = __readdir64 (dirstream);
 
           /* When we've iterated through all directory entries without finding
              one with a matching d_ino, rewind the stream and consider each
@@ -325,7 +326,7 @@ __getcwd (char *buf, size_t size)
             {
               use_d_ino = false;
               __rewinddir (dirstream);
-              d = __readdir (dirstream);
+              d = __readdir64 (dirstream);
             }
 
           if (d == NULL)
diff --git a/sysdeps/unix/sysv/linux/readdir64.c b/sysdeps/unix/sysv/linux/readdir64.c
index 7d4b0001b3..170a889c51 100644
--- a/sysdeps/unix/sysv/linux/readdir64.c
+++ b/sysdeps/unix/sysv/linux/readdir64.c
@@ -42,7 +42,11 @@ weak_alias (__readdir64, readdir)
 /* The compat code expects the 'struct direct' with d_ino being a __ino_t
    instead of __ino64_t.  */
 # include <shlib-compat.h>
+# if IS_IN(rtld)
+weak_alias (__readdir64, readdir64)
+# else
 versioned_symbol (libc, __readdir64, readdir64, GLIBC_2_2);
+# endif
 # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2)
 #  include <olddirent.h>
 #  define __READDIR   attribute_compat_text_section __old_readdir64
-- 
2.25.1


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH 4/4] io: Reorganize the getcwd implementation
  2020-08-26 21:02 [PATCH 1/4] Sync getcwd with gnulib Adhemerval Zanella
  2020-08-26 21:02 ` [PATCH 2/4] linux: Remove __ASSUME_ATFCTS Adhemerval Zanella
  2020-08-26 21:02 ` [PATCH 3/4] Use LFS readdir in generic POSIX getcwd [BZ# 22899] Adhemerval Zanella
@ 2020-08-26 21:02 ` Adhemerval Zanella
  2020-08-26 22:39   ` Paul Eggert
                     ` (2 more replies)
  2020-08-26 22:39 ` [PATCH 1/4] Sync getcwd with gnulib Paul Eggert
  2020-08-27  8:14 ` Florian Weimer
  4 siblings, 3 replies; 21+ messages in thread
From: Adhemerval Zanella @ 2020-08-26 21:02 UTC (permalink / raw)
  To: libc-alpha

The generic implementation uses two internal symbols: __getcwd_system
(which might be overriden by the system) and __getcwd_generic (the
generic implementation shared with gnulib).  The Linux implementation
is moved to __getcwd_system and generic POSIX implementation is moved
to __getcwd_generic.

This change aims to make the code sync with gnulib easier and simplify
the Linux override implementation.

The dl-fxstatat64 is not required anymore and adding it explicit issue
a duplicate symbol in libc.so linking.

Hurd still overrides the getcwd altogether and one possibility would
to be move its implementation to __getcwd_system and reimplement the
__getcwd_generic to be a empty one.

Checked on x86_64-linux-gnu and i686-linux-gnu.
---
 include/unistd.h                              |  2 +
 io/Makefile                                   |  2 +-
 sysdeps/posix/getcwd.c => io/getcwd-generic.c |  7 +--
 io/getcwd-system.c                            | 28 +++++++++
 io/getcwd.c                                   |  9 ++-
 sysdeps/unix/sysv/linux/Makefile              |  3 +-
 sysdeps/unix/sysv/linux/alpha/dl-fxstatat64.c |  1 -
 sysdeps/unix/sysv/linux/dl-fxstatat64.c       |  1 -
 sysdeps/unix/sysv/linux/dl-getcwd.c           |  1 -
 .../sysv/linux/{getcwd.c => getcwd-system.c}  | 58 +------------------
 .../sysv/linux/sparc/sparc64/dl-fxstatat64.c  |  1 -
 .../sysv/linux/wordsize-64/dl-fxstatat64.c    |  1 -
 12 files changed, 40 insertions(+), 74 deletions(-)
 rename sysdeps/posix/getcwd.c => io/getcwd-generic.c (99%)
 create mode 100644 io/getcwd-system.c
 delete mode 100644 sysdeps/unix/sysv/linux/alpha/dl-fxstatat64.c
 delete mode 100644 sysdeps/unix/sysv/linux/dl-fxstatat64.c
 delete mode 100644 sysdeps/unix/sysv/linux/dl-getcwd.c
 rename sysdeps/unix/sysv/linux/{getcwd.c => getcwd-system.c} (53%)
 delete mode 100644 sysdeps/unix/sysv/linux/sparc/sparc64/dl-fxstatat64.c
 delete mode 100644 sysdeps/unix/sysv/linux/wordsize-64/dl-fxstatat64.c

diff --git a/include/unistd.h b/include/unistd.h
index f48da2c7a3..792cfdff0b 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -76,6 +76,8 @@ extern int __lchown (const char *__file, __uid_t __owner,
 		     __gid_t __group);
 extern int __chdir (const char *__path) attribute_hidden;
 extern int __fchdir (int __fd) attribute_hidden;
+extern char *__getcwd_generic (char *__buf, size_t __size) attribute_hidden;
+extern char *__getcwd_system (char *__buf, size_t __size) attribute_hidden;
 extern char *__getcwd (char *__buf, size_t __size);
 libc_hidden_proto (__getcwd)
 extern int __rmdir (const char *__path) attribute_hidden;
diff --git a/io/Makefile b/io/Makefile
index cf380f3516..26dfe047c0 100644
--- a/io/Makefile
+++ b/io/Makefile
@@ -46,7 +46,7 @@ routines :=								\
 	close dup dup2 dup3 pipe pipe2					\
 	creat creat64							\
 	chdir fchdir							\
-	getcwd getwd getdirname						\
+	getcwd getwd getcwd-system getcwd-generic getdirname		\
 	chown fchown lchown fchownat					\
 	ttyname ttyname_r isatty					\
 	link linkat symlink symlinkat readlink readlinkat		\
diff --git a/sysdeps/posix/getcwd.c b/io/getcwd-generic.c
similarity index 99%
rename from sysdeps/posix/getcwd.c
rename to io/getcwd-generic.c
index 1e6fc9b845..e259ce14da 100644
--- a/sysdeps/posix/getcwd.c
+++ b/io/getcwd-generic.c
@@ -154,7 +154,7 @@ getcwd_nothrow (char *buf, size_t size)
    bytes long, unless SIZE == 0, in which case it is as big as necessary.  */
 
 char *
-__getcwd (char *buf, size_t size)
+__getcwd_generic (char *buf, size_t size)
 {
   /* Lengths of big file name components and entire file names, and a
      deep level of file name nesting.  These numbers are not upper
@@ -486,8 +486,3 @@ __getcwd (char *buf, size_t size)
   }
   return NULL;
 }
-
-#if defined _LIBC && !defined __getcwd
-libc_hidden_def (__getcwd)
-weak_alias (__getcwd, getcwd)
-#endif
diff --git a/io/getcwd-system.c b/io/getcwd-system.c
new file mode 100644
index 0000000000..4390479aa2
--- /dev/null
+++ b/io/getcwd-system.c
@@ -0,0 +1,28 @@
+/* Architectur specific getcwd implementation.  Generic implementation.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <unistd.h>
+
+/* This function is called by the generic 'getcwd' implementation to allow
+   a system to implement if the it provides a faster or simpler way to obtain
+   the current direction (e.g. through a syscall).  */
+char *
+__getcwd_system (char *buf, size_t size)
+{
+  return NULL;
+}
diff --git a/io/getcwd.c b/io/getcwd.c
index 0fabd98131..cf7a8e1a30 100644
--- a/io/getcwd.c
+++ b/io/getcwd.c
@@ -29,11 +29,10 @@
 char *
 __getcwd (char *buf, size_t size)
 {
-  __set_errno (ENOSYS);
-  return NULL;
+  char *r = __getcwd_system (buf, size);
+  if (r == NULL)
+    r = __getcwd_generic (buf, size);
+  return r;
 }
 libc_hidden_def (__getcwd)
 weak_alias (__getcwd, getcwd)
-
-stub_warning (__getcwd)
-stub_warning (getcwd)
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 9b2a253032..465ffe7104 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -280,8 +280,7 @@ tests += tst-fallocate tst-fallocate64 tst-o_path-locks
 endif
 
 ifeq ($(subdir),elf)
-sysdep-rtld-routines += dl-brk dl-sbrk dl-getcwd dl-openat64 dl-opendir \
-			dl-fxstatat64
+sysdep-rtld-routines += dl-brk dl-sbrk dl-openat64 dl-opendir
 
 libof-lddlibc4 = lddlibc4
 
diff --git a/sysdeps/unix/sysv/linux/alpha/dl-fxstatat64.c b/sysdeps/unix/sysv/linux/alpha/dl-fxstatat64.c
deleted file mode 100644
index 330b33f7c7..0000000000
--- a/sysdeps/unix/sysv/linux/alpha/dl-fxstatat64.c
+++ /dev/null
@@ -1 +0,0 @@
-#include "fxstatat.c"
diff --git a/sysdeps/unix/sysv/linux/dl-fxstatat64.c b/sysdeps/unix/sysv/linux/dl-fxstatat64.c
deleted file mode 100644
index d229d0ea0f..0000000000
--- a/sysdeps/unix/sysv/linux/dl-fxstatat64.c
+++ /dev/null
@@ -1 +0,0 @@
-#include <fxstatat64.c>
diff --git a/sysdeps/unix/sysv/linux/dl-getcwd.c b/sysdeps/unix/sysv/linux/dl-getcwd.c
deleted file mode 100644
index 4bd5657f1e..0000000000
--- a/sysdeps/unix/sysv/linux/dl-getcwd.c
+++ /dev/null
@@ -1 +0,0 @@
-#include "getcwd.c"
diff --git a/sysdeps/unix/sysv/linux/getcwd.c b/sysdeps/unix/sysv/linux/getcwd-system.c
similarity index 53%
rename from sysdeps/unix/sysv/linux/getcwd.c
rename to sysdeps/unix/sysv/linux/getcwd-system.c
index fabc4bb8cc..a7e8535b72 100644
--- a/sysdeps/unix/sysv/linux/getcwd.c
+++ b/sysdeps/unix/sysv/linux/getcwd-system.c
@@ -17,16 +17,8 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <assert.h>
-#include <errno.h>
-#include <limits.h>
-#include <stdlib.h>
 #include <unistd.h>
-#include <sys/param.h>
-
 #include <sysdep.h>
-#include <sys/syscall.h>
-
 
 /* If we compile the file for use in ld.so we don't need the feature
    that getcwd() allocates the buffers itself.  */
@@ -34,19 +26,10 @@
 # define NO_ALLOCATION	1
 #endif
 
-
-/* The "proc" filesystem provides an easy method to retrieve the value.
-   For each process, the corresponding directory contains a symbolic link
-   named `cwd'.  Reading the content of this link immediate gives us the
-   information.  But we have to take care for systems which do not have
-   the proc filesystem mounted.  Use the POSIX implementation in this case.  */
-static char *generic_getcwd (char *buf, size_t size);
-
 char *
-__getcwd (char *buf, size_t size)
+__getcwd_system (char *buf, size_t size)
 {
   char *path;
-  char *result;
 
 #ifndef NO_ALLOCATION
   size_t alloc_size = size;
@@ -58,7 +41,7 @@ __getcwd (char *buf, size_t size)
 	  return NULL;
 	}
 
-      alloc_size = MAX (PATH_MAX, __getpagesize ());
+      alloc_size = PATH_MAX;
     }
 
   if (buf == NULL)
@@ -75,7 +58,7 @@ __getcwd (char *buf, size_t size)
 
   int retval;
 
-  retval = INLINE_SYSCALL (getcwd, 2, path, alloc_size);
+  retval = INLINE_SYSCALL_CALL (getcwd, path, alloc_size);
   if (retval > 0 && path[0] == '/')
     {
 #ifndef NO_ALLOCATION
@@ -92,34 +75,6 @@ __getcwd (char *buf, size_t size)
       return buf;
     }
 
-  /* The system call either cannot handle paths longer than a page
-     or can succeed without returning an absolute path.  Just use the
-     generic implementation right away.  */
-  if (retval >= 0 || errno == ENAMETOOLONG)
-    {
-#ifndef NO_ALLOCATION
-      if (buf == NULL && size == 0)
-	{
-	  free (path);
-	  path = NULL;
-	}
-#endif
-
-      result = generic_getcwd (path, size);
-
-#ifndef NO_ALLOCATION
-      if (result == NULL && buf == NULL && size != 0)
-	free (path);
-#endif
-
-      return result;
-    }
-
-  /* It should never happen that the `getcwd' syscall failed because
-     the buffer is too small if we allocated the buffer ourselves
-     large enough.  */
-  assert (errno != ERANGE || buf != NULL || size != 0);
-
 #ifndef NO_ALLOCATION
   if (buf == NULL)
     free (path);
@@ -127,10 +82,3 @@ __getcwd (char *buf, size_t size)
 
   return NULL;
 }
-libc_hidden_def (__getcwd)
-weak_alias (__getcwd, getcwd)
-
-/* Get the code for the generic version.  */
-#define GETCWD_RETURN_TYPE	static char *
-#define __getcwd		generic_getcwd
-#include <sysdeps/posix/getcwd.c>
diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/dl-fxstatat64.c b/sysdeps/unix/sysv/linux/sparc/sparc64/dl-fxstatat64.c
deleted file mode 100644
index 330b33f7c7..0000000000
--- a/sysdeps/unix/sysv/linux/sparc/sparc64/dl-fxstatat64.c
+++ /dev/null
@@ -1 +0,0 @@
-#include "fxstatat.c"
diff --git a/sysdeps/unix/sysv/linux/wordsize-64/dl-fxstatat64.c b/sysdeps/unix/sysv/linux/wordsize-64/dl-fxstatat64.c
deleted file mode 100644
index 330b33f7c7..0000000000
--- a/sysdeps/unix/sysv/linux/wordsize-64/dl-fxstatat64.c
+++ /dev/null
@@ -1 +0,0 @@
-#include "fxstatat.c"
-- 
2.25.1


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 1/4] Sync getcwd with gnulib
  2020-08-26 21:02 [PATCH 1/4] Sync getcwd with gnulib Adhemerval Zanella
                   ` (2 preceding siblings ...)
  2020-08-26 21:02 ` [PATCH 4/4] io: Reorganize the getcwd implementation Adhemerval Zanella
@ 2020-08-26 22:39 ` Paul Eggert
  2020-08-27 11:07   ` Adhemerval Zanella
  2020-08-27  8:14 ` Florian Weimer
  4 siblings, 1 reply; 21+ messages in thread
From: Paul Eggert @ 2020-08-26 22:39 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha

Thanks for looking into this. To help move this along from the Gnulib point of 
view, I merged the Gnulib-relevant parts of those proposed glibc patches into 
Gnulib, here:

https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=615b43e1f914687b6ccd40c79a05626098a361d7

but this Gnulib patch has the following further changes to get this to work, and 
these further changes should be in the glibc version too:

>    - It does not include pathmax.h header (glibc does not provide it).

That #include can be moved into a previous "#if !_LIBC" section.

> +# define __getcwd rpl_getcwd

This should define __getcwd_generic, not __getcwd, since the function name changed.



Also, here are some minor refactorings to the resulting getcwd-generic.c that 
would improve readability. If you add these I plan to merge them into Gnulib too.

* Prefer "#if _LIBC" to "#if !_LIBC", interchanging 'then' and 'else' parts.

* Put those !_LIBC #defines in alphabetic order.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 4/4] io: Reorganize the getcwd implementation
  2020-08-26 21:02 ` [PATCH 4/4] io: Reorganize the getcwd implementation Adhemerval Zanella
@ 2020-08-26 22:39   ` Paul Eggert
  2020-08-27 12:35   ` Adhemerval Zanella
  2020-08-27 19:20   ` [PATCH v2] " Adhemerval Zanella
  2 siblings, 0 replies; 21+ messages in thread
From: Paul Eggert @ 2020-08-26 22:39 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha

On 8/26/20 2:02 PM, Adhemerval Zanella via Libc-alpha wrote:
> +/* Architectur specific getcwd implementation.  Generic implementation.

Missing "e" at the end of "Architecture".

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 1/4] Sync getcwd with gnulib
  2020-08-26 21:02 [PATCH 1/4] Sync getcwd with gnulib Adhemerval Zanella
                   ` (3 preceding siblings ...)
  2020-08-26 22:39 ` [PATCH 1/4] Sync getcwd with gnulib Paul Eggert
@ 2020-08-27  8:14 ` Florian Weimer
  2020-08-27 10:53   ` Adhemerval Zanella
  4 siblings, 1 reply; 21+ messages in thread
From: Florian Weimer @ 2020-08-27  8:14 UTC (permalink / raw)
  To: Adhemerval Zanella via Libc-alpha

* Adhemerval Zanella via Libc-alpha:

> +#if HAVE_MINIMALLY_WORKING_GETCWD
> +  /* If AT_FDCWD is not defined, the algorithm below is O(N**2) and
> +     this is much slower than the system getcwd (at least on
> +     GNU/Linux).  So trust the system getcwd's results unless they
> +     look suspicious.
> +
> +     Use the system getcwd even if we have openat support, since the
> +     system getcwd works even when a parent is unreadable, while the
> +     openat-based approach does not.
> +
> +     But on AIX 5.1..7.1, the system getcwd is not even minimally
> +     working: If the current directory name is slightly longer than
> +     PATH_MAX, it omits the first directory component and returns
> +     this wrong result with errno = 0.  */
> +
> +# undef getcwd
> +  dir = getcwd_system (buf, size);
> +  if (dir || (size && errno == ERANGE))
> +    return dir;

This conflicts with the getcwd_system implementation does not set errno.

> +  /* Solaris getcwd (NULL, 0) fails with errno == EINVAL, but it has
> +     internal magic that lets it work even if an ancestor directory is
> +     inaccessible, which is better in many cases.  So in this case try
> +     again with a buffer that's almost always big enough.  */
> +  if (errno == EINVAL && buf == NULL && size == 0)
> +    {
> +      char big_buffer[BIG_FILE_NAME_LENGTH + 1];
> +      dir = getcwd_system (big_buffer, sizeof big_buffer);
> +      if (dir)
> +        return strdup (dir);
> +    }

> +          /* When we've iterated through all directory entries without finding
> +             one with a matching d_ino, rewind the stream and consider each
> +             name again, but this time, using lstat.  This is necessary in a
> +             chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
> +             .., ../.., ../../.., etc. all had the same device number, yet the
> +             d_ino values for entries in / did not match those obtained
> +             via lstat.  */
> +          if (d == NULL && errno == 0 && use_d_ino)
> +            {
> +              use_d_ino = false;
> +              __rewinddir (dirstream);
> +              d = __readdir (dirstream);
> +            }

I'm not sure if it's worthwhile to have such code in glibc.

The generic getcwd isn't used by Hurd, right?  Would it make sense to
have a trimmed-down Linux implementation as well?

Thanks,
Florian


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 3/4] Use LFS readdir in generic POSIX getcwd [BZ# 22899]
  2020-08-26 21:02 ` [PATCH 3/4] Use LFS readdir in generic POSIX getcwd [BZ# 22899] Adhemerval Zanella
@ 2020-08-27  9:58   ` Florian Weimer
  0 siblings, 0 replies; 21+ messages in thread
From: Florian Weimer @ 2020-08-27  9:58 UTC (permalink / raw)
  To: Adhemerval Zanella via Libc-alpha

* Adhemerval Zanella via Libc-alpha:

> diff --git a/sysdeps/unix/sysv/linux/readdir64.c b/sysdeps/unix/sysv/linux/readdir64.c
> index 7d4b0001b3..170a889c51 100644
> --- a/sysdeps/unix/sysv/linux/readdir64.c
> +++ b/sysdeps/unix/sysv/linux/readdir64.c
> @@ -42,7 +42,11 @@ weak_alias (__readdir64, readdir)
>  /* The compat code expects the 'struct direct' with d_ino being a __ino_t
>     instead of __ino64_t.  */
>  # include <shlib-compat.h>
> +# if IS_IN(rtld)
> +weak_alias (__readdir64, readdir64)
> +# else
>  versioned_symbol (libc, __readdir64, readdir64, GLIBC_2_2);
> +# endif

I don't think we need the full getcwd in the dynamic loader.  We only
use the fallback code if the kernel getcwd fails.  But that code does
not produce a path that is usable for the dynamic loader because it
cannot be used in open calls (it's too long for that).

I think for rtld, we should build getcwd without the fallback, and then
the change above won't be needed.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 1/4] Sync getcwd with gnulib
  2020-08-27  8:14 ` Florian Weimer
@ 2020-08-27 10:53   ` Adhemerval Zanella
  2020-08-27 10:58     ` Florian Weimer
  0 siblings, 1 reply; 21+ messages in thread
From: Adhemerval Zanella @ 2020-08-27 10:53 UTC (permalink / raw)
  To: Florian Weimer; +Cc: Adhemerval Zanella via Libc-alpha

On Thu, Aug 27, 2020 at 5:14 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * Adhemerval Zanella via Libc-alpha:
>
> > +#if HAVE_MINIMALLY_WORKING_GETCWD
> > +  /* If AT_FDCWD is not defined, the algorithm below is O(N**2) and
> > +     this is much slower than the system getcwd (at least on
> > +     GNU/Linux).  So trust the system getcwd's results unless they
> > +     look suspicious.
> > +
> > +     Use the system getcwd even if we have openat support, since the
> > +     system getcwd works even when a parent is unreadable, while the
> > +     openat-based approach does not.
> > +
> > +     But on AIX 5.1..7.1, the system getcwd is not even minimally
> > +     working: If the current directory name is slightly longer than
> > +     PATH_MAX, it omits the first directory component and returns
> > +     this wrong result with errno = 0.  */
> > +
> > +# undef getcwd
> > +  dir = getcwd_system (buf, size);
> > +  if (dir || (size && errno == ERANGE))
> > +    return dir;
>
> This conflicts with the getcwd_system implementation does not set errno.
>
> > +  /* Solaris getcwd (NULL, 0) fails with errno == EINVAL, but it has
> > +     internal magic that lets it work even if an ancestor directory is
> > +     inaccessible, which is better in many cases.  So in this case try
> > +     again with a buffer that's almost always big enough.  */
> > +  if (errno == EINVAL && buf == NULL && size == 0)
> > +    {
> > +      char big_buffer[BIG_FILE_NAME_LENGTH + 1];
> > +      dir = getcwd_system (big_buffer, sizeof big_buffer);
> > +      if (dir)
> > +        return strdup (dir);
> > +    }
>
> > +          /* When we've iterated through all directory entries without finding
> > +             one with a matching d_ino, rewind the stream and consider each
> > +             name again, but this time, using lstat.  This is necessary in a
> > +             chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
> > +             .., ../.., ../../.., etc. all had the same device number, yet the
> > +             d_ino values for entries in / did not match those obtained
> > +             via lstat.  */
> > +          if (d == NULL && errno == 0 && use_d_ino)
> > +            {
> > +              use_d_ino = false;
> > +              __rewinddir (dirstream);
> > +              d = __readdir (dirstream);
> > +            }
>
> I'm not sure if it's worthwhile to have such code in glibc.
>
> The generic getcwd isn't used by Hurd, right?  Would it make sense to
> have a trimmed-down Linux implementation as well?

This specific part is not used by glibc, for LIBC my patch sets
HAVE_MINIMALLY_WORKING_GETCWD
explicit to 0. I just added to sync with gnulib implementation and
make future changes less complex
(and it also checks if gnulib code to disable the LIBC part is working
correctly).

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 1/4] Sync getcwd with gnulib
  2020-08-27 10:53   ` Adhemerval Zanella
@ 2020-08-27 10:58     ` Florian Weimer
  2020-08-27 11:06       ` Adhemerval Zanella
  0 siblings, 1 reply; 21+ messages in thread
From: Florian Weimer @ 2020-08-27 10:58 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: Adhemerval Zanella via Libc-alpha

* Adhemerval Zanella:

> This specific part is not used by glibc, for LIBC my patch sets
> HAVE_MINIMALLY_WORKING_GETCWD
> explicit to 0. I just added to sync with gnulib implementation and
> make future changes less complex
> (and it also checks if gnulib code to disable the LIBC part is working
> correctly).

I meant: Is anything of this used on Hurd at all?  I don't think so.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 1/4] Sync getcwd with gnulib
  2020-08-27 10:58     ` Florian Weimer
@ 2020-08-27 11:06       ` Adhemerval Zanella
  2020-08-27 11:10         ` Florian Weimer
  0 siblings, 1 reply; 21+ messages in thread
From: Adhemerval Zanella @ 2020-08-27 11:06 UTC (permalink / raw)
  To: Florian Weimer; +Cc: Adhemerval Zanella via Libc-alpha

On Thu, Aug 27, 2020 at 7:58 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * Adhemerval Zanella:
>
> > This specific part is not used by glibc, for LIBC my patch sets
> > HAVE_MINIMALLY_WORKING_GETCWD
> > explicit to 0. I just added to sync with gnulib implementation and
> > make future changes less complex
> > (and it also checks if gnulib code to disable the LIBC part is working
> > correctly).
>
> I meant: Is anything of this used on Hurd at all?  I don't think so.

No, Hurd re implements getcwd.c.  I personally would prefer to get
streamline version without all
the clutter required to build on all the platforms gnulib support, but
my impression from other
gnulib code sync was that we should avoid it.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 1/4] Sync getcwd with gnulib
  2020-08-26 22:39 ` [PATCH 1/4] Sync getcwd with gnulib Paul Eggert
@ 2020-08-27 11:07   ` Adhemerval Zanella
  0 siblings, 0 replies; 21+ messages in thread
From: Adhemerval Zanella @ 2020-08-27 11:07 UTC (permalink / raw)
  To: Paul Eggert; +Cc: GNU C Library

On Wed, Aug 26, 2020 at 7:39 PM Paul Eggert <eggert@cs.ucla.edu> wrote:
>
> Thanks for looking into this. To help move this along from the Gnulib point of
> view, I merged the Gnulib-relevant parts of those proposed glibc patches into
> Gnulib, here:
>
> https://git.savannah.gnu.org/cgit/gnulib.git/commit/?id=615b43e1f914687b6ccd40c79a05626098a361d7
>
> but this Gnulib patch has the following further changes to get this to work, and
> these further changes should be in the glibc version too:
>
> >    - It does not include pathmax.h header (glibc does not provide it).
>
> That #include can be moved into a previous "#if !_LIBC" section.
>
> > +# define __getcwd rpl_getcwd
>
> This should define __getcwd_generic, not __getcwd, since the function name changed.
>
>
>
> Also, here are some minor refactorings to the resulting getcwd-generic.c that
> would improve readability. If you add these I plan to merge them into Gnulib too.
>
> * Prefer "#if _LIBC" to "#if !_LIBC", interchanging 'then' and 'else' parts.
>
> * Put those !_LIBC #defines in alphabetic order.

Thanks, I will check the modifications you did on gnulib and add back
on my patch.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 1/4] Sync getcwd with gnulib
  2020-08-27 11:06       ` Adhemerval Zanella
@ 2020-08-27 11:10         ` Florian Weimer
  2020-08-27 11:33           ` Adhemerval Zanella
  0 siblings, 1 reply; 21+ messages in thread
From: Florian Weimer @ 2020-08-27 11:10 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: Adhemerval Zanella via Libc-alpha

* Adhemerval Zanella:

> On Thu, Aug 27, 2020 at 7:58 AM Florian Weimer <fweimer@redhat.com> wrote:
>>
>> * Adhemerval Zanella:
>>
>> > This specific part is not used by glibc, for LIBC my patch sets
>> > HAVE_MINIMALLY_WORKING_GETCWD
>> > explicit to 0. I just added to sync with gnulib implementation and
>> > make future changes less complex
>> > (and it also checks if gnulib code to disable the LIBC part is working
>> > correctly).
>>
>> I meant: Is anything of this used on Hurd at all?  I don't think so.
>
> No, Hurd re implements getcwd.c.  I personally would prefer to get
> streamline version without all the clutter required to build on all
> the platforms gnulib support, but my impression from other gnulib code
> sync was that we should avoid it.

Hmm.  Maybe I will give this a try and see how much code is left after
the cleanup.  This shouldn't block the current sync if you want to go
ahead with it.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 1/4] Sync getcwd with gnulib
  2020-08-27 11:10         ` Florian Weimer
@ 2020-08-27 11:33           ` Adhemerval Zanella
  0 siblings, 0 replies; 21+ messages in thread
From: Adhemerval Zanella @ 2020-08-27 11:33 UTC (permalink / raw)
  To: Florian Weimer; +Cc: Adhemerval Zanella via Libc-alpha



On 27/08/2020 08:10, Florian Weimer wrote:
> * Adhemerval Zanella:
> 
>> On Thu, Aug 27, 2020 at 7:58 AM Florian Weimer <fweimer@redhat.com> wrote:
>>>
>>> * Adhemerval Zanella:
>>>
>>>> This specific part is not used by glibc, for LIBC my patch sets
>>>> HAVE_MINIMALLY_WORKING_GETCWD
>>>> explicit to 0. I just added to sync with gnulib implementation and
>>>> make future changes less complex
>>>> (and it also checks if gnulib code to disable the LIBC part is working
>>>> correctly).
>>>
>>> I meant: Is anything of this used on Hurd at all?  I don't think so.
>>
>> No, Hurd re implements getcwd.c.  I personally would prefer to get
>> streamline version without all the clutter required to build on all
>> the platforms gnulib support, but my impression from other gnulib code
>> sync was that we should avoid it.
> 
> Hmm.  Maybe I will give this a try and see how much code is left after
> the cleanup.  This shouldn't block the current sync if you want to go
> ahead with it.

The io/getcwd-system.os text size from the last patch in this set is 1439
on x86_64.  I think resulting size reduction possible on ld.so would 
something like that.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 4/4] io: Reorganize the getcwd implementation
  2020-08-26 21:02 ` [PATCH 4/4] io: Reorganize the getcwd implementation Adhemerval Zanella
  2020-08-26 22:39   ` Paul Eggert
@ 2020-08-27 12:35   ` Adhemerval Zanella
  2020-08-27 13:21     ` Florian Weimer
  2020-08-27 17:29     ` Adhemerval Zanella
  2020-08-27 19:20   ` [PATCH v2] " Adhemerval Zanella
  2 siblings, 2 replies; 21+ messages in thread
From: Adhemerval Zanella @ 2020-08-27 12:35 UTC (permalink / raw)
  To: libc-alpha, Florian Weimer



On 26/08/2020 18:02, Adhemerval Zanella wrote:
> The generic implementation uses two internal symbols: __getcwd_system
> (which might be overriden by the system) and __getcwd_generic (the
> generic implementation shared with gnulib).  The Linux implementation
> is moved to __getcwd_system and generic POSIX implementation is moved
> to __getcwd_generic.
> 
> This change aims to make the code sync with gnulib easier and simplify
> the Linux override implementation.
> 
> The dl-fxstatat64 is not required anymore and adding it explicit issue
> a duplicate symbol in libc.so linking.
> 
> Hurd still overrides the getcwd altogether and one possibility would
> to be move its implementation to __getcwd_system and reimplement the
> __getcwd_generic to be a empty one.
> 
> Checked on x86_64-linux-gnu and i686-linux-gnu.

Hi Florian,

With this patch above applied over this set I remove the generic
implementation from rtld.  It allowed some code simplification and the
resulting ld.so size change from:

$ size elf/ld.so
   text    data     bss     dec     hex filename
 164592    7304     392  172288   2a100 elf/ld.so

To

$ size elf/ld.so
   text    data     bss     dec     hex filename
 162222    7304     392  169918   297be elf/ld.so

It also has de advantage of not pulling the generic implementation on
hurd build (which does not use it anyway).

---

diff --git a/elf/dl-object.c b/elf/dl-object.c
index d2cdf135cc..aab7265717 100644
--- a/elf/dl-object.c
+++ b/elf/dl-object.c
@@ -202,30 +202,12 @@ _dl_new_object (char *realname, const char *libname, int type,
 	}
       else
 	{
-	  size_t len = realname_len;
-	  char *result = NULL;
-
-	  /* Get the current directory name.  */
-	  origin = NULL;
-	  do
-	    {
-	      char *new_origin;
-
-	      len += 128;
-	      new_origin = (char *) realloc (origin, len);
-	      if (new_origin == NULL)
-		/* We exit the loop.  Note that result == NULL.  */
-		break;
-	      origin = new_origin;
-	    }
-	  while ((result = __getcwd (origin, len - realname_len)) == NULL
-		 && errno == ERANGE);
-
-	  if (result == NULL)
+	  /* The rtld __getcwd implementation does not handle paths larger
+	     than PATH_MAX (which would be invalid to be used on subsequent
+	     open calls).  */
+	  origin = __getcwd (NULL, 0);
+	  if (origin == NULL)
 	    {
-	      /* We were not able to determine the current directory.
-		 Note that free(origin) is OK if origin == NULL.  */
-	      free (origin);
 	      origin = (char *) -1;
 	      goto out;
 	    }
diff --git a/io/Makefile b/io/Makefile
index 26dfe047c0..57cb778790 100644
--- a/io/Makefile
+++ b/io/Makefile
@@ -46,7 +46,7 @@ routines :=								\
 	close dup dup2 dup3 pipe pipe2					\
 	creat creat64							\
 	chdir fchdir							\
-	getcwd getwd getcwd-system getcwd-generic getdirname		\
+	getcwd getwd getcwd-system getdirname				\
 	chown fchown lchown fchownat					\
 	ttyname ttyname_r isatty					\
 	link linkat symlink symlinkat readlink readlinkat		\
diff --git a/io/getcwd.c b/io/getcwd.c
index cf7a8e1a30..574f51085b 100644
--- a/io/getcwd.c
+++ b/io/getcwd.c
@@ -19,6 +19,10 @@
 #include <unistd.h>
 #include <stddef.h>
 
+#if !IS_IN(rtld)
+#include <getcwd-generic.c>
+#endif
+
 /* Get the pathname of the current working directory,
    and put it in SIZE bytes of BUF.  Returns NULL if the
    directory couldn't be determined or SIZE was too small.
@@ -30,8 +34,10 @@ char *
 __getcwd (char *buf, size_t size)
 {
   char *r = __getcwd_system (buf, size);
+#if !IS_IN(rtld)
   if (r == NULL)
     r = __getcwd_generic (buf, size);
+#endif
   return r;
 }
 libc_hidden_def (__getcwd)
diff --git a/sysdeps/unix/sysv/linux/getcwd-system.c b/sysdeps/unix/sysv/linux/getcwd-system.c
index a7e8535b72..8526b1465b 100644
--- a/sysdeps/unix/sysv/linux/getcwd-system.c
+++ b/sysdeps/unix/sysv/linux/getcwd-system.c
@@ -20,18 +20,11 @@
 #include <unistd.h>
 #include <sysdep.h>
 
-/* If we compile the file for use in ld.so we don't need the feature
-   that getcwd() allocates the buffers itself.  */
-#if IS_IN (rtld)
-# define NO_ALLOCATION	1
-#endif
-
 char *
 __getcwd_system (char *buf, size_t size)
 {
   char *path;
 
-#ifndef NO_ALLOCATION
   size_t alloc_size = size;
   if (size == 0)
     {
@@ -51,9 +44,6 @@ __getcwd_system (char *buf, size_t size)
 	return NULL;
     }
   else
-#else
-# define alloc_size size
-#endif
     path = buf;
 
   int retval;
@@ -61,7 +51,6 @@ __getcwd_system (char *buf, size_t size)
   retval = INLINE_SYSCALL_CALL (getcwd, path, alloc_size);
   if (retval > 0 && path[0] == '/')
     {
-#ifndef NO_ALLOCATION
       if (buf == NULL && size == 0)
 	/* Ensure that the buffer is only as large as necessary.  */
 	buf = realloc (path, (size_t) retval);
@@ -70,15 +59,12 @@ __getcwd_system (char *buf, size_t size)
 	/* Either buf was NULL all along, or `realloc' failed but
 	   we still have the original string.  */
 	buf = path;
-#endif
 
       return buf;
     }
 
-#ifndef NO_ALLOCATION
   if (buf == NULL)
     free (path);
-#endif
 
   return NULL;
 }

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 4/4] io: Reorganize the getcwd implementation
  2020-08-27 12:35   ` Adhemerval Zanella
@ 2020-08-27 13:21     ` Florian Weimer
  2020-08-27 13:40       ` Adhemerval Zanella
  2020-08-27 17:29     ` Adhemerval Zanella
  1 sibling, 1 reply; 21+ messages in thread
From: Florian Weimer @ 2020-08-27 13:21 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha

* Adhemerval Zanella:

> With this patch above applied over this set I remove the generic
> implementation from rtld.  It allowed some code simplification and the
> resulting ld.so size change from:
>
> $ size elf/ld.so
>    text    data     bss     dec     hex filename
>  164592    7304     392  172288   2a100 elf/ld.so
>
> To
>
> $ size elf/ld.so
>    text    data     bss     dec     hex filename
>  162222    7304     392  169918   297be elf/ld.so
>
> It also has de advantage of not pulling the generic implementation on
> hurd build (which does not use it anyway).

Thanks for doing the experiment.

> diff --git a/io/getcwd.c b/io/getcwd.c
> index cf7a8e1a30..574f51085b 100644
> --- a/io/getcwd.c
> +++ b/io/getcwd.c
> @@ -19,6 +19,10 @@
>  #include <unistd.h>
>  #include <stddef.h>
>  
> +#if !IS_IN(rtld)
> +#include <getcwd-generic.c>
> +#endif
> +
>  /* Get the pathname of the current working directory,
>     and put it in SIZE bytes of BUF.  Returns NULL if the
>     directory couldn't be determined or SIZE was too small.
> @@ -30,8 +34,10 @@ char *
>  __getcwd (char *buf, size_t size)
>  {
>    char *r = __getcwd_system (buf, size);
> +#if !IS_IN(rtld)
>    if (r == NULL)
>      r = __getcwd_generic (buf, size);
> +#endif
>    return r;
>  }
>  libc_hidden_def (__getcwd)

Right, that's what I had in mind.

> diff --git a/sysdeps/unix/sysv/linux/getcwd-system.c b/sysdeps/unix/sysv/linux/getcwd-system.c
> index a7e8535b72..8526b1465b 100644
> --- a/sysdeps/unix/sysv/linux/getcwd-system.c
> +++ b/sysdeps/unix/sysv/linux/getcwd-system.c
> @@ -20,18 +20,11 @@
>  #include <unistd.h>
>  #include <sysdep.h>
>  
> -/* If we compile the file for use in ld.so we don't need the feature
> -   that getcwd() allocates the buffers itself.  */
> -#if IS_IN (rtld)
> -# define NO_ALLOCATION	1
> -#endif
> -
>  char *
>  __getcwd_system (char *buf, size_t size)
>  {
>    char *path;
>  
> -#ifndef NO_ALLOCATION
>    size_t alloc_size = size;
>    if (size == 0)
>      {
> @@ -51,9 +44,6 @@ __getcwd_system (char *buf, size_t size)
>  	return NULL;
>      }
>    else
> -#else
> -# define alloc_size size
> -#endif
>      path = buf;
>  
>    int retval;

That part I'm less sure about.  I think this could allocate a 64K page
that's never freed?  Maybe that's a bit excessive.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 4/4] io: Reorganize the getcwd implementation
  2020-08-27 13:21     ` Florian Weimer
@ 2020-08-27 13:40       ` Adhemerval Zanella
  0 siblings, 0 replies; 21+ messages in thread
From: Adhemerval Zanella @ 2020-08-27 13:40 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-alpha



On 27/08/2020 10:21, Florian Weimer wrote:
> * Adhemerval Zanella:
> 
>> With this patch above applied over this set I remove the generic
>> implementation from rtld.  It allowed some code simplification and the
>> resulting ld.so size change from:
>>
>> $ size elf/ld.so
>>    text    data     bss     dec     hex filename
>>  164592    7304     392  172288   2a100 elf/ld.so
>>
>> To
>>
>> $ size elf/ld.so
>>    text    data     bss     dec     hex filename
>>  162222    7304     392  169918   297be elf/ld.so
>>
>> It also has de advantage of not pulling the generic implementation on
>> hurd build (which does not use it anyway).
> 
> Thanks for doing the experiment.
> 
>> diff --git a/io/getcwd.c b/io/getcwd.c
>> index cf7a8e1a30..574f51085b 100644
>> --- a/io/getcwd.c
>> +++ b/io/getcwd.c
>> @@ -19,6 +19,10 @@
>>  #include <unistd.h>
>>  #include <stddef.h>
>>  
>> +#if !IS_IN(rtld)
>> +#include <getcwd-generic.c>
>> +#endif
>> +
>>  /* Get the pathname of the current working directory,
>>     and put it in SIZE bytes of BUF.  Returns NULL if the
>>     directory couldn't be determined or SIZE was too small.
>> @@ -30,8 +34,10 @@ char *
>>  __getcwd (char *buf, size_t size)
>>  {
>>    char *r = __getcwd_system (buf, size);
>> +#if !IS_IN(rtld)
>>    if (r == NULL)
>>      r = __getcwd_generic (buf, size);
>> +#endif
>>    return r;
>>  }
>>  libc_hidden_def (__getcwd)
> 
> Right, that's what I had in mind.
> 
>> diff --git a/sysdeps/unix/sysv/linux/getcwd-system.c b/sysdeps/unix/sysv/linux/getcwd-system.c
>> index a7e8535b72..8526b1465b 100644
>> --- a/sysdeps/unix/sysv/linux/getcwd-system.c
>> +++ b/sysdeps/unix/sysv/linux/getcwd-system.c
>> @@ -20,18 +20,11 @@
>>  #include <unistd.h>
>>  #include <sysdep.h>
>>  
>> -/* If we compile the file for use in ld.so we don't need the feature
>> -   that getcwd() allocates the buffers itself.  */
>> -#if IS_IN (rtld)
>> -# define NO_ALLOCATION	1
>> -#endif
>> -
>>  char *
>>  __getcwd_system (char *buf, size_t size)
>>  {
>>    char *path;
>>  
>> -#ifndef NO_ALLOCATION
>>    size_t alloc_size = size;
>>    if (size == 0)
>>      {
>> @@ -51,9 +44,6 @@ __getcwd_system (char *buf, size_t size)
>>  	return NULL;
>>      }
>>    else
>> -#else
>> -# define alloc_size size
>> -#endif
>>      path = buf;
>>  
>>    int retval;
> 
> That part I'm less sure about.  I think this could allocate a 64K page
> that's never freed?  Maybe that's a bit excessive.

The original patch limits the maximum allocation to PATH_MAX and for
a successful __NR_getcwd syscall the memory will be realloced to fit the
returned size from kernel.  So the worst case of returning the maximum
allocation size would just happen if realloc fails.

Another possibility would be to use the the scratch_buffer strategy I used
on my realpath Linux optimization [1]: call __NR_getcwd in a loop and either
return the strdup if scratch_buffer did not allocated memory or the resulting
buffer otherwise.  I think once the realpath is up I can send a patch with
this strategy, but it trades some performance (to avoid calling realloc)
with some extra syscalls in case or large paths.

[1] https://sourceware.org/pipermail/libc-alpha/2020-August/116935.html


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 4/4] io: Reorganize the getcwd implementation
  2020-08-27 12:35   ` Adhemerval Zanella
  2020-08-27 13:21     ` Florian Weimer
@ 2020-08-27 17:29     ` Adhemerval Zanella
  1 sibling, 0 replies; 21+ messages in thread
From: Adhemerval Zanella @ 2020-08-27 17:29 UTC (permalink / raw)
  To: libc-alpha, Florian Weimer



On 27/08/2020 09:35, Adhemerval Zanella wrote:
> diff --git a/elf/dl-object.c b/elf/dl-object.c
> index d2cdf135cc..aab7265717 100644
> --- a/elf/dl-object.c
> +++ b/elf/dl-object.c
> @@ -202,30 +202,12 @@ _dl_new_object (char *realname, const char *libname, int type,
>  	}
>        else
>  	{
> -	  size_t len = realname_len;
> -	  char *result = NULL;
> -
> -	  /* Get the current directory name.  */
> -	  origin = NULL;
> -	  do
> -	    {
> -	      char *new_origin;
> -
> -	      len += 128;
> -	      new_origin = (char *) realloc (origin, len);
> -	      if (new_origin == NULL)
> -		/* We exit the loop.  Note that result == NULL.  */
> -		break;
> -	      origin = new_origin;
> -	    }
> -	  while ((result = __getcwd (origin, len - realname_len)) == NULL
> -		 && errno == ERANGE);
> -
> -	  if (result == NULL)
> +	  /* The rtld __getcwd implementation does not handle paths larger
> +	     than PATH_MAX (which would be invalid to be used on subsequent
> +	     open calls).  */
> +	  origin = __getcwd (NULL, 0);
> +	  if (origin == NULL)
>  	    {
> -	      /* We were not able to determine the current directory.
> -		 Note that free(origin) is OK if origin == NULL.  */
> -	      free (origin);
>  	      origin = (char *) -1;
>  	      goto out;
>  	    }

And this part is obviously wrong since the algorithm will append the realname
on the resulting string afterward.  I will send a update version with the
rtld code size optimization, but without this bogus change.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v2] io: Reorganize the getcwd implementation
  2020-08-26 21:02 ` [PATCH 4/4] io: Reorganize the getcwd implementation Adhemerval Zanella
  2020-08-26 22:39   ` Paul Eggert
  2020-08-27 12:35   ` Adhemerval Zanella
@ 2020-08-27 19:20   ` Adhemerval Zanella
  2020-08-27 23:44     ` Paul Eggert
  2 siblings, 1 reply; 21+ messages in thread
From: Adhemerval Zanella @ 2020-08-27 19:20 UTC (permalink / raw)
  To: libc-alpha

Changes from previous version:

  - Do not build the generic implementation for rtld.
  - Fix a missing include on Linux implementation (for PATH_MAX).

---

The generic implementation uses two internal symbols: __getcwd_system
(which might be overriden by the system) and __getcwd_generic (the
generic implementation shared with gnulib).  The Linux implementation
is moved to __getcwd_system and generic POSIX implementation is moved
to __getcwd_generic.

For rtld, the fallback code is not enabled since it would only used
for path larger than PATH_MAX and subsequent open calls would fail
with ENAMETOOLONG.  It allows to simplify the code on _dl_new_object
and remove the NO_ALLOCATION build switch on Linux implementation.

This change aims to make the code sync with gnulib easier and simplify
the Linux override implementation.

The dl-fxstatat64 is not required anymore and adding it explicit issue
a duplicate symbol in libc.so linking.

Hurd still overrides the getcwd altogether and one possibility would
to be move its implementation to __getcwd_system and reimplement the
__getcwd_generic to be a empty one.

Checked on x86_64-linux-gnu and i686-linux-gnu.
---
 elf/dl-object.c                               |  31 ++--
 include/unistd.h                              |   2 +
 io/Makefile                                   |   2 +-
 sysdeps/posix/getcwd.c => io/getcwd-generic.c |   9 +-
 io/getcwd-system.c                            |  28 ++++
 io/getcwd.c                                   |  16 ++-
 sysdeps/unix/sysv/linux/Makefile              |   3 +-
 sysdeps/unix/sysv/linux/alpha/dl-fxstatat64.c |   1 -
 sysdeps/unix/sysv/linux/dl-fxstatat64.c       |   1 -
 sysdeps/unix/sysv/linux/dl-getcwd.c           |   1 -
 sysdeps/unix/sysv/linux/getcwd-system.c       |  69 +++++++++
 sysdeps/unix/sysv/linux/getcwd.c              | 136 ------------------
 .../sysv/linux/sparc/sparc64/dl-fxstatat64.c  |   1 -
 .../sysv/linux/wordsize-64/dl-fxstatat64.c    |   1 -
 14 files changed, 125 insertions(+), 176 deletions(-)
 rename sysdeps/posix/getcwd.c => io/getcwd-generic.c (98%)
 create mode 100644 io/getcwd-system.c
 delete mode 100644 sysdeps/unix/sysv/linux/alpha/dl-fxstatat64.c
 delete mode 100644 sysdeps/unix/sysv/linux/dl-fxstatat64.c
 delete mode 100644 sysdeps/unix/sysv/linux/dl-getcwd.c
 create mode 100644 sysdeps/unix/sysv/linux/getcwd-system.c
 delete mode 100644 sysdeps/unix/sysv/linux/getcwd.c
 delete mode 100644 sysdeps/unix/sysv/linux/sparc/sparc64/dl-fxstatat64.c
 delete mode 100644 sysdeps/unix/sysv/linux/wordsize-64/dl-fxstatat64.c

diff --git a/elf/dl-object.c b/elf/dl-object.c
index d2cdf135cc..bddef4485f 100644
--- a/elf/dl-object.c
+++ b/elf/dl-object.c
@@ -202,38 +202,29 @@ _dl_new_object (char *realname, const char *libname, int type,
 	}
       else
 	{
-	  size_t len = realname_len;
-	  char *result = NULL;
-
-	  /* Get the current directory name.  */
-	  origin = NULL;
-	  do
+	  /* The rtld __getcwd implementation does not handle paths larger
+	     than PATH_MAX (which would be invalid to be used on subsequent
+	     open calls).  */
+	  origin = __getcwd (NULL, 0);
+	  if (origin == NULL)
 	    {
-	      char *new_origin;
-
-	      len += 128;
-	      new_origin = (char *) realloc (origin, len);
-	      if (new_origin == NULL)
-		/* We exit the loop.  Note that result == NULL.  */
-		break;
-	      origin = new_origin;
+	      origin = (char *) -1;
+	      goto out;
 	    }
-	  while ((result = __getcwd (origin, len - realname_len)) == NULL
-		 && errno == ERANGE);
-
+	  size_t len = strlen (origin);
+	  char *result = realloc (origin, len + realname_len);
 	  if (result == NULL)
 	    {
-	      /* We were not able to determine the current directory.
-		 Note that free(origin) is OK if origin == NULL.  */
 	      free (origin);
 	      origin = (char *) -1;
 	      goto out;
 	    }
+	  origin = result;
+	  cp = origin + len;
 
 	  /* Find the end of the path and see whether we have to add a
 	     slash.  We could use rawmemchr but this need not be
 	     fast.  */
-	  cp = (strchr) (origin, '\0');
 	  if (cp[-1] != '/')
 	    *cp++ = '/';
 	}
diff --git a/include/unistd.h b/include/unistd.h
index f48da2c7a3..792cfdff0b 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -76,6 +76,8 @@ extern int __lchown (const char *__file, __uid_t __owner,
 		     __gid_t __group);
 extern int __chdir (const char *__path) attribute_hidden;
 extern int __fchdir (int __fd) attribute_hidden;
+extern char *__getcwd_generic (char *__buf, size_t __size) attribute_hidden;
+extern char *__getcwd_system (char *__buf, size_t __size) attribute_hidden;
 extern char *__getcwd (char *__buf, size_t __size);
 libc_hidden_proto (__getcwd)
 extern int __rmdir (const char *__path) attribute_hidden;
diff --git a/io/Makefile b/io/Makefile
index cf380f3516..57cb778790 100644
--- a/io/Makefile
+++ b/io/Makefile
@@ -46,7 +46,7 @@ routines :=								\
 	close dup dup2 dup3 pipe pipe2					\
 	creat creat64							\
 	chdir fchdir							\
-	getcwd getwd getdirname						\
+	getcwd getwd getcwd-system getdirname				\
 	chown fchown lchown fchownat					\
 	ttyname ttyname_r isatty					\
 	link linkat symlink symlinkat readlink readlinkat		\
diff --git a/sysdeps/posix/getcwd.c b/io/getcwd-generic.c
similarity index 98%
rename from sysdeps/posix/getcwd.c
rename to io/getcwd-generic.c
index 1e6fc9b845..3b4be6bdaa 100644
--- a/sysdeps/posix/getcwd.c
+++ b/io/getcwd-generic.c
@@ -89,7 +89,7 @@
 
 #if !_LIBC
 # define __close_nocancel_nostatus close
-# define __getcwd rpl_getcwd
+# define __getcwd_generic rpl_getcwd
 # define stat64    stat
 # define __fstat64 fstat
 # define __fstatat64 fstatat
@@ -154,7 +154,7 @@ getcwd_nothrow (char *buf, size_t size)
    bytes long, unless SIZE == 0, in which case it is as big as necessary.  */
 
 char *
-__getcwd (char *buf, size_t size)
+__getcwd_generic (char *buf, size_t size)
 {
   /* Lengths of big file name components and entire file names, and a
      deep level of file name nesting.  These numbers are not upper
@@ -486,8 +486,3 @@ __getcwd (char *buf, size_t size)
   }
   return NULL;
 }
-
-#if defined _LIBC && !defined __getcwd
-libc_hidden_def (__getcwd)
-weak_alias (__getcwd, getcwd)
-#endif
diff --git a/io/getcwd-system.c b/io/getcwd-system.c
new file mode 100644
index 0000000000..b0ae6271ab
--- /dev/null
+++ b/io/getcwd-system.c
@@ -0,0 +1,28 @@
+/* Architecture specific getcwd implementation.  Generic implementation.
+   Copyright (C) 2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <unistd.h>
+
+/* This function is called by the generic 'getcwd' implementation to allow
+   a system to implement if the it provides a faster or simpler way to obtain
+   the current direction (e.g. through a syscall).  */
+char *
+__getcwd_system (char *buf, size_t size)
+{
+  return NULL;
+}
diff --git a/io/getcwd.c b/io/getcwd.c
index 0fabd98131..0e0a790368 100644
--- a/io/getcwd.c
+++ b/io/getcwd.c
@@ -19,6 +19,11 @@
 #include <unistd.h>
 #include <stddef.h>
 
+/* The rtld does not need to handle path larger than PATH_MAX.  */
+#if !IS_IN(rtld)
+#include <getcwd-generic.c>
+#endif
+
 /* Get the pathname of the current working directory,
    and put it in SIZE bytes of BUF.  Returns NULL if the
    directory couldn't be determined or SIZE was too small.
@@ -29,11 +34,12 @@
 char *
 __getcwd (char *buf, size_t size)
 {
-  __set_errno (ENOSYS);
-  return NULL;
+  char *r = __getcwd_system (buf, size);
+#if !IS_IN(rtld)
+  if (r == NULL)
+    r = __getcwd_generic (buf, size);
+#endif
+  return r;
 }
 libc_hidden_def (__getcwd)
 weak_alias (__getcwd, getcwd)
-
-stub_warning (__getcwd)
-stub_warning (getcwd)
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 9b2a253032..465ffe7104 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -280,8 +280,7 @@ tests += tst-fallocate tst-fallocate64 tst-o_path-locks
 endif
 
 ifeq ($(subdir),elf)
-sysdep-rtld-routines += dl-brk dl-sbrk dl-getcwd dl-openat64 dl-opendir \
-			dl-fxstatat64
+sysdep-rtld-routines += dl-brk dl-sbrk dl-openat64 dl-opendir
 
 libof-lddlibc4 = lddlibc4
 
diff --git a/sysdeps/unix/sysv/linux/alpha/dl-fxstatat64.c b/sysdeps/unix/sysv/linux/alpha/dl-fxstatat64.c
deleted file mode 100644
index 330b33f7c7..0000000000
--- a/sysdeps/unix/sysv/linux/alpha/dl-fxstatat64.c
+++ /dev/null
@@ -1 +0,0 @@
-#include "fxstatat.c"
diff --git a/sysdeps/unix/sysv/linux/dl-fxstatat64.c b/sysdeps/unix/sysv/linux/dl-fxstatat64.c
deleted file mode 100644
index d229d0ea0f..0000000000
--- a/sysdeps/unix/sysv/linux/dl-fxstatat64.c
+++ /dev/null
@@ -1 +0,0 @@
-#include <fxstatat64.c>
diff --git a/sysdeps/unix/sysv/linux/dl-getcwd.c b/sysdeps/unix/sysv/linux/dl-getcwd.c
deleted file mode 100644
index 4bd5657f1e..0000000000
--- a/sysdeps/unix/sysv/linux/dl-getcwd.c
+++ /dev/null
@@ -1 +0,0 @@
-#include "getcwd.c"
diff --git a/sysdeps/unix/sysv/linux/getcwd-system.c b/sysdeps/unix/sysv/linux/getcwd-system.c
new file mode 100644
index 0000000000..a2df688127
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/getcwd-system.c
@@ -0,0 +1,69 @@
+/* Determine current working directory.  Linux version.
+   Copyright (C) 1997-2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <sysdep.h>
+
+char *
+__getcwd_system (char *buf, size_t size)
+{
+  char *path;
+
+  size_t alloc_size = size;
+  if (size == 0)
+    {
+      if (buf != NULL)
+	{
+	  __set_errno (EINVAL);
+	  return NULL;
+	}
+
+      alloc_size = PATH_MAX;
+    }
+
+  if (buf == NULL)
+    {
+      path = malloc (alloc_size);
+      if (path == NULL)
+	return NULL;
+    }
+  else
+    path = buf;
+
+  int r = INLINE_SYSCALL_CALL (getcwd, path, alloc_size);
+  if (r > 0 && path[0] == '/')
+    {
+      if (buf == NULL && size == 0)
+	/* Ensure that the buffer is only as large as necessary.  */
+	buf = realloc (path, r);
+
+      if (buf == NULL)
+	/* Either buf was NULL all along, or `realloc' failed but
+	   we still have the original string.  */
+	buf = path;
+
+      return buf;
+    }
+
+  if (buf == NULL)
+    free (path);
+
+  return NULL;
+}
diff --git a/sysdeps/unix/sysv/linux/getcwd.c b/sysdeps/unix/sysv/linux/getcwd.c
deleted file mode 100644
index fabc4bb8cc..0000000000
--- a/sysdeps/unix/sysv/linux/getcwd.c
+++ /dev/null
@@ -1,136 +0,0 @@
-/* Determine current working directory.  Linux version.
-   Copyright (C) 1997-2020 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, see
-   <https://www.gnu.org/licenses/>.  */
-
-#include <assert.h>
-#include <errno.h>
-#include <limits.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/param.h>
-
-#include <sysdep.h>
-#include <sys/syscall.h>
-
-
-/* If we compile the file for use in ld.so we don't need the feature
-   that getcwd() allocates the buffers itself.  */
-#if IS_IN (rtld)
-# define NO_ALLOCATION	1
-#endif
-
-
-/* The "proc" filesystem provides an easy method to retrieve the value.
-   For each process, the corresponding directory contains a symbolic link
-   named `cwd'.  Reading the content of this link immediate gives us the
-   information.  But we have to take care for systems which do not have
-   the proc filesystem mounted.  Use the POSIX implementation in this case.  */
-static char *generic_getcwd (char *buf, size_t size);
-
-char *
-__getcwd (char *buf, size_t size)
-{
-  char *path;
-  char *result;
-
-#ifndef NO_ALLOCATION
-  size_t alloc_size = size;
-  if (size == 0)
-    {
-      if (buf != NULL)
-	{
-	  __set_errno (EINVAL);
-	  return NULL;
-	}
-
-      alloc_size = MAX (PATH_MAX, __getpagesize ());
-    }
-
-  if (buf == NULL)
-    {
-      path = malloc (alloc_size);
-      if (path == NULL)
-	return NULL;
-    }
-  else
-#else
-# define alloc_size size
-#endif
-    path = buf;
-
-  int retval;
-
-  retval = INLINE_SYSCALL (getcwd, 2, path, alloc_size);
-  if (retval > 0 && path[0] == '/')
-    {
-#ifndef NO_ALLOCATION
-      if (buf == NULL && size == 0)
-	/* Ensure that the buffer is only as large as necessary.  */
-	buf = realloc (path, (size_t) retval);
-
-      if (buf == NULL)
-	/* Either buf was NULL all along, or `realloc' failed but
-	   we still have the original string.  */
-	buf = path;
-#endif
-
-      return buf;
-    }
-
-  /* The system call either cannot handle paths longer than a page
-     or can succeed without returning an absolute path.  Just use the
-     generic implementation right away.  */
-  if (retval >= 0 || errno == ENAMETOOLONG)
-    {
-#ifndef NO_ALLOCATION
-      if (buf == NULL && size == 0)
-	{
-	  free (path);
-	  path = NULL;
-	}
-#endif
-
-      result = generic_getcwd (path, size);
-
-#ifndef NO_ALLOCATION
-      if (result == NULL && buf == NULL && size != 0)
-	free (path);
-#endif
-
-      return result;
-    }
-
-  /* It should never happen that the `getcwd' syscall failed because
-     the buffer is too small if we allocated the buffer ourselves
-     large enough.  */
-  assert (errno != ERANGE || buf != NULL || size != 0);
-
-#ifndef NO_ALLOCATION
-  if (buf == NULL)
-    free (path);
-#endif
-
-  return NULL;
-}
-libc_hidden_def (__getcwd)
-weak_alias (__getcwd, getcwd)
-
-/* Get the code for the generic version.  */
-#define GETCWD_RETURN_TYPE	static char *
-#define __getcwd		generic_getcwd
-#include <sysdeps/posix/getcwd.c>
diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/dl-fxstatat64.c b/sysdeps/unix/sysv/linux/sparc/sparc64/dl-fxstatat64.c
deleted file mode 100644
index 330b33f7c7..0000000000
--- a/sysdeps/unix/sysv/linux/sparc/sparc64/dl-fxstatat64.c
+++ /dev/null
@@ -1 +0,0 @@
-#include "fxstatat.c"
diff --git a/sysdeps/unix/sysv/linux/wordsize-64/dl-fxstatat64.c b/sysdeps/unix/sysv/linux/wordsize-64/dl-fxstatat64.c
deleted file mode 100644
index 330b33f7c7..0000000000
--- a/sysdeps/unix/sysv/linux/wordsize-64/dl-fxstatat64.c
+++ /dev/null
@@ -1 +0,0 @@
-#include "fxstatat.c"
-- 
2.25.1


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] io: Reorganize the getcwd implementation
  2020-08-27 19:20   ` [PATCH v2] " Adhemerval Zanella
@ 2020-08-27 23:44     ` Paul Eggert
  2020-08-31 18:27       ` Adhemerval Zanella
  0 siblings, 1 reply; 21+ messages in thread
From: Paul Eggert @ 2020-08-27 23:44 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha

This patch doesn't apply to glibc master. 'git am' says:

Applying: io: Reorganize the getcwd implementation
error: patch failed: sysdeps/posix/getcwd.c:89
error: sysdeps/posix/getcwd.c: patch does not apply
Patch failed at 0001 io: Reorganize the getcwd implementation

and plain 'patch' says:

patching file io/getcwd-generic.c (renamed from sysdeps/posix/getcwd.c)
Hunk #1 FAILED at 89.
Hunk #2 FAILED at 154.
Hunk #3 succeeded at 529 with fuzz 1 (offset 43 lines).
2 out of 3 hunks FAILED -- saving rejects to file io/getcwd-generic.c.rej

Perhaps I should be applying it to some particular commit? Or perhaps you need 
to rebase it or something.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2] io: Reorganize the getcwd implementation
  2020-08-27 23:44     ` Paul Eggert
@ 2020-08-31 18:27       ` Adhemerval Zanella
  0 siblings, 0 replies; 21+ messages in thread
From: Adhemerval Zanella @ 2020-08-31 18:27 UTC (permalink / raw)
  To: Paul Eggert; +Cc: libc-alpha



On 27/08/2020 20:44, Paul Eggert wrote:
> This patch doesn't apply to glibc master. 'git am' says:
> 
> Applying: io: Reorganize the getcwd implementation
> error: patch failed: sysdeps/posix/getcwd.c:89
> error: sysdeps/posix/getcwd.c: patch does not apply
> Patch failed at 0001 io: Reorganize the getcwd implementation
> 
> and plain 'patch' says:
> 
> patching file io/getcwd-generic.c (renamed from sysdeps/posix/getcwd.c)
> Hunk #1 FAILED at 89.
> Hunk #2 FAILED at 154.
> Hunk #3 succeeded at 529 with fuzz 1 (offset 43 lines).
> 2 out of 3 hunks FAILED -- saving rejects to file io/getcwd-generic.c.rej
> 
> Perhaps I should be applying it to some particular commit? Or perhaps you need to rebase it or something.

Sorry, I should have make it explicit it is an updated for the 4/4 patch
on the set [1]. I will send a updated version now you updated gnulib side
along with some changes on the linux implementation and probably a fix for
BZ#26545 as well.

[1] https://sourceware.org/pipermail/libc-alpha/2020-August/117296.html

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2020-08-31 18:27 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-26 21:02 [PATCH 1/4] Sync getcwd with gnulib Adhemerval Zanella
2020-08-26 21:02 ` [PATCH 2/4] linux: Remove __ASSUME_ATFCTS Adhemerval Zanella
2020-08-26 21:02 ` [PATCH 3/4] Use LFS readdir in generic POSIX getcwd [BZ# 22899] Adhemerval Zanella
2020-08-27  9:58   ` Florian Weimer
2020-08-26 21:02 ` [PATCH 4/4] io: Reorganize the getcwd implementation Adhemerval Zanella
2020-08-26 22:39   ` Paul Eggert
2020-08-27 12:35   ` Adhemerval Zanella
2020-08-27 13:21     ` Florian Weimer
2020-08-27 13:40       ` Adhemerval Zanella
2020-08-27 17:29     ` Adhemerval Zanella
2020-08-27 19:20   ` [PATCH v2] " Adhemerval Zanella
2020-08-27 23:44     ` Paul Eggert
2020-08-31 18:27       ` Adhemerval Zanella
2020-08-26 22:39 ` [PATCH 1/4] Sync getcwd with gnulib Paul Eggert
2020-08-27 11:07   ` Adhemerval Zanella
2020-08-27  8:14 ` Florian Weimer
2020-08-27 10:53   ` Adhemerval Zanella
2020-08-27 10:58     ` Florian Weimer
2020-08-27 11:06       ` Adhemerval Zanella
2020-08-27 11:10         ` Florian Weimer
2020-08-27 11:33           ` Adhemerval Zanella

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).