diff mbox series

[v2,5/5] io: Reorganize the getcwd implementation

Message ID 20200901203704.172996-5-adhemerval.zanella@linaro.org
State New
Headers show
Series [v2,1/5] Sync getcwd with gnulib | expand

Commit Message

Adhemerval Zanella Sept. 1, 2020, 8:37 p.m. UTC
Changes from previous version:

  - Do not build the generic version for loader.

--

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                                   | 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 -
 .../sysv/linux/{getcwd.c => getcwd-system.c}  | 23 ++-------------
 .../sysv/linux/sparc/sparc64/dl-fxstatat64.c  |  1 -
 .../sysv/linux/wordsize-64/dl-fxstatat64.c    |  1 -
 12 files changed, 46 insertions(+), 40 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} (76%)
 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 mbox series

Patch

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 99%
rename from sysdeps/posix/getcwd.c
rename to io/getcwd-generic.c
index 3958bcecba..3b4be6bdaa 100644
--- a/sysdeps/posix/getcwd.c
+++ b/io/getcwd-generic.c
@@ -153,7 +153,7 @@  getcwd_nothrow (char *buf, size_t size)
    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_generic (char *buf, size_t size)
 {
   /* Lengths of big file name components and entire file names, and a
@@ -486,8 +486,3 @@  __getcwd_generic (char *buf, size_t size)
   }
   return NULL;
 }
-
-#if defined _LIBC && !defined GETCWD_RETURN_TYPE
-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 5079f33655..3abb39e865 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -281,8 +281,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 76%
rename from sysdeps/unix/sysv/linux/getcwd.c
rename to sysdeps/unix/sysv/linux/getcwd-system.c
index 5ef0b092ec..571b9688cd 100644
--- a/sysdeps/unix/sysv/linux/getcwd.c
+++ b/sysdeps/unix/sysv/linux/getcwd-system.c
@@ -17,25 +17,13 @@ 
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <errno.h>
-#include <limits.h>
+#include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
-
 #include <sysdep.h>
-#include <sys/syscall.h>
-
-#if !IS_IN (rtld)
-/* The generic implementation is used for valid paths larger than
-   PATH_MAX (where the syscall returns a positive value with errno
-   set to ENAMETOOLONG).  */
-#define GETCWD_RETURN_TYPE	static char *
-#include <sysdeps/posix/getcwd.c>
-#endif
 
 char *
-__getcwd (char *buf, size_t size)
+__getcwd_system (char *buf, size_t size)
 {
   char *r = NULL;
   int len;
@@ -73,12 +61,5 @@  __getcwd (char *buf, size_t size)
   if (len > 0 && r[0] == '/')
     return r;
 
-#if !IS_IN (rtld)
-  if (len >=0 || errno == ENAMETOOLONG)
-    return __getcwd_generic (buf, size);
-#endif
-
   return NULL;
 }
-libc_hidden_def (__getcwd)
-weak_alias (__getcwd, getcwd)
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"