Skip to contentSkip to conversation

linux-user: Use getcwd syscall directly

PATCH Applied Andreas Schwab 2 participants 3 messages Jul 23, 2020 – Jul 27, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Use getcwd syscall directly

The glibc getcwd function returns different errors than the getcwd syscall, which triggers an assertion failure in the glibc getcwd function when running under the emulation.

---
 linux-user/syscall.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
linux-user/syscall.c +1 -8
index b9144b18fc..e4e46867e8 100644
@@ -388,14 +388,7 @@ static bitmask_transtbl fcntl_flags_tbl[] = {
   { 0, 0, 0, 0 }
 };
 
-static int sys_getcwd1(char *buf, size_t size)
-{
-  if (getcwd(buf, size) == NULL) {
-      /* getcwd() sets errno */
-      return (-1);
-  }
-  return strlen(buf)+1;
-}
+_syscall2(int, sys_getcwd1, char *, buf, size_t, size)
 
 #ifdef TARGET_NR_utimensat
 #if defined(__NR_utimensat)
Signature · 7 lines
2.26.2


-- 
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."
Reviewed-by on v1 1/1
#1 lore raw

Le 23/07/2020 à 12:27, Andreas Schwab a écrit :

Quoted 20 lines

The glibc getcwd function returns different errors than the getcwd syscall, which triggers an assertion failure in the glibc getcwd function when running under the emulation.

---
 linux-user/syscall.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
linux-user/syscall.c +0 -2
index b9144b18fc..e4e46867e8 100644
@@ -388,14 +388,7 @@ static bitmask_transtbl fcntl_flags_tbl[] = {
   { 0, 0, 0, 0 }
 };
 
-static int sys_getcwd1(char *buf, size_t size)
-{

linux-user/syscall.c static bitmask_transtbl fcntl_flags_tbl[] = { · line 394

Quoted 10 lines
linux-user/syscall.c +1 -6
-  if (getcwd(buf, size) == NULL) {
-      /* getcwd() sets errno */
-      return (-1);
-  }
-  return strlen(buf)+1;
-}
+_syscall2(int, sys_getcwd1, char *, buf, size_t, size)
 
 #ifdef TARGET_NR_utimensat
 #if defined(__NR_utimensat)

I'll add in the commit message the explanation you did on your previous attempt:

"When the syscall returns ENAMETOOLONG,
the glibc wrapper uses a fallback implementation that potentially handles an unlimited path length, and returns with ERANGE if the provided buffer is too small. The qemu emulation cannot distinguish the two cases, and thus always returns ERANGE. This is unexpected by the glibc wrapper."

Thanks,
Laurent

#2 lore raw
In reply to Andreas Schwab

Le 23/07/2020 à 12:27, Andreas Schwab a écrit :

Quoted 20 lines

The glibc getcwd function returns different errors than the getcwd syscall, which triggers an assertion failure in the glibc getcwd function when running under the emulation.

---
 linux-user/syscall.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
linux-user/syscall.c +0 -2
index b9144b18fc..e4e46867e8 100644
@@ -388,14 +388,7 @@ static bitmask_transtbl fcntl_flags_tbl[] = {
   { 0, 0, 0, 0 }
 };
 
-static int sys_getcwd1(char *buf, size_t size)
-{

linux-user/syscall.c static bitmask_transtbl fcntl_flags_tbl[] = { · line 394

Quoted 10 lines
linux-user/syscall.c +1 -6
-  if (getcwd(buf, size) == NULL) {
-      /* getcwd() sets errno */
-      return (-1);
-  }
-  return strlen(buf)+1;
-}
+_syscall2(int, sys_getcwd1, char *, buf, size_t, size)
 
 #ifdef TARGET_NR_utimensat
 #if defined(__NR_utimensat)

Applied to my linux-user-for-5.1 branch.

Thanks,
Laurent