Skip to contentSkip to conversation

linux-user: Flush errors by using exit() instead of _exit() in error path

PATCH In review Helge Deller 5 participants 8 messages Apr 25 – Apr 27

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Flush errors by using exit() instead of _exit() in error path

From: Helge Deller <[email protected]>

Qemu user mode does not properly flushes error messages related to bad arguments when exiting (at least when the output is piped to a file instead of running on a terminal).
Ensure that we always flush by using exit() instead of _exit().

Reported by: Tobias Bergkvist
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2544

---
 linux-user/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/main.c +1 -1
index c49d1e91d2..84e110dfe9 100644
@@ -767,7 +767,7 @@ int main(int argc, char **argv, char **envp)
         execfd = open(exec_path, O_RDONLY);
         if (execfd < 0) {
             printf("Error while loading %s: %s\n", exec_path, strerror(errno));
-            _exit(EXIT_FAILURE);
+            exit(EXIT_FAILURE);
         }
     }
 
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Helge Deller

From: Helge Deller <[email protected]>

Qemu user mode does not properly flushes error messages related to bad arguments when exiting (at least when the output is piped to a file instead of running on a terminal).
Ensure that we always flush by using exit() instead of _exit().

Quoted 7 lines

Reported by: Tobias Bergkvist
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2544

---
 linux-user/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Note: I've added the linux-user maintainers to the CC list for visibility. bsd-user has the same problem.

Warner

#2 lore raw
Quoted 13 lines · Warner Losh

On Sat, Apr 25, 2026 at 8:50 AM Helge Deller <[email protected] <mailto:[email protected]>> wrote:

From: Helge Deller <[email protected] <mailto:[email protected]>>

Qemu user mode does not properly flushes error messages related to bad arguments when exiting (at least when the output is piped to a file instead of running on a terminal).
Ensure that we always flush by using exit() instead of _exit().

Reported by: Tobias Bergkvist
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2544 <https://gitlab.com/qemu-project/qemu/-/work_items/2544>

Quoted 7 lines
Signed-off-by Helge Deller <[email protected] <mailto:[email protected]>>

---
 linux-user/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by Warner Losh <[email protected] <mailto:[email protected]>>

Thanks!

Quoted 1 line

Note: I've added the linux-user maintainers to the CC list for visibility.

Ok.

Quoted 1 line

bsd-user has the same problem.

I hesitate to change things I don't not test at all :-(
Even this patch here is not widely tested, but I think we need to add it. If things break we see reports anyway.

Helge

#3 lore raw

[[ Oops, my last message cc'd Pierrick's old address, maybe my tree is old or it needs to update in MAINTAINERS ]]

Quoted 27 lines · Helge Deller
Quoted 3 lines · Warner Losh

On Sat, Apr 25, 2026 at 8:50 AM Helge Deller <[email protected] <mailto:

Quoted 4 lines · [email protected]>> wrote:

From: Helge Deller <[email protected] <mailto:[email protected]>>

Qemu user mode does not properly flushes error messages related to

bad

Quoted 6 lines

arguments when exiting (at least when the output is piped to a file instead of running on a terminal).
Ensure that we always flush by using exit() instead of _exit().

Reported by: Tobias Bergkvist
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2544 <

https://gitlab.com/qemu-project/qemu/-/work_items/2544>

Quoted 7 lines
Signed-off-by Helge Deller <[email protected] <mailto:[email protected]>>

---
linux-user/main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by Warner Losh <[email protected] <mailto:[email protected]>>

Thanks!

Quoted 8 lines
Quoted 1 line

Note: I've added the linux-user maintainers to the CC list for

visibility.

Ok.

Quoted 1 line

bsd-user has the same problem.

I hesitate to change things I don't not test at all :-(

I can, since I do a lot with bsd-user.

Quoted 3 lines

Even this patch here is not widely tested, but I think we need to add it. If things break we see reports anyway.

I don't think this will break anything. The printf can't possibly work, either
we must change it to a write or do the exit. exit is done everywhere else. This has been wrong since:

commit 03cfd8faa7ffb7201e2949b99c2f35b1fef7078b

Author: Laurent Vivier <[email protected]>
Date:   Fri Aug 30 01:46:44 2013 +0200

linux-user: add support of binfmt_misc 'O' flag
...
+ if (execfd < 0) {
+ printf("Error while loading %s: %s\n", filename,
strerror(-execfd));
+ _exit(1);
+ }
...
but the full commit log doesn't say why. Laaurent was one of the people I cc'd before
I chased this back to this commit. :).

I'll go ahead and make the bsd-user/main.c change (the error message is a little different, and there's
a pull request against the bsd-user upstream I need to land in the area around it, IIRC).

Warner

#6 lore raw
In reply to Helge Deller
Quoted 20 lines · Helge Deller

From: Helge Deller <[email protected]>

Qemu user mode does not properly flushes error messages related to bad arguments when exiting (at least when the output is piped to a file instead of running on a terminal).
Ensure that we always flush by using exit() instead of _exit().

Reported by: Tobias Bergkvist
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2544

---
 linux-user/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/main.c +0 -0
index c49d1e91d2..84e110dfe9 100644

linux-user/main.c int main(int argc, char **argv, char **envp) · line 772

Quoted 7 lines
linux-user/main.c +1 -1
@@ -767,7 +767,7 @@ int main(int argc, char **argv, char **envp)
         execfd = open(exec_path, O_RDONLY);
         if (execfd < 0) {
             printf("Error while loading %s: %s\n", exec_path, strerror(errno));
-            _exit(EXIT_FAILURE);
+            exit(EXIT_FAILURE);
         }
     }

How about the other _exit() further down in the function for the case where loader_exec() fails ? That seems like it would have the same problem.

-- PMM

#4 lore raw
In reply to Warner Losh
Quoted 15 lines · Warner Losh

I don't think this will break anything. The printf can't possibly work, either we must change it to a write or do the exit. exit is done everywhere else. This has been wrong since:

commit 03cfd8faa7ffb7201e2949b99c2f35b1fef7078b

Author: Laurent Vivier <[email protected]>
Date:   Fri Aug 30 01:46:44 2013 +0200

linux-user: add support of binfmt_misc 'O' flag
...

(quoted diff) +4 -0
+    if (execfd < 0) {
+        printf("Error while loading %s: %s\n", filename, strerror(-execfd));
+        _exit(1);
+    }
...
Quoted 1 line

but the full commit log doesn't say why.

My guess is that the "print an error and exit" if() block was simply copied from the bit further down that does:

    if (ret != 0) {
        printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
        _exit(EXIT_FAILURE);
    }

for the loader_exec failure case. That one dates back to commit 74cd30b811cc2f in 2003, which changes some exit() calls to _exit(). I have no idea why, and the commit message is cryptic and useless. But it seems unlikely that the reason is still relevant today.

-- PMM

#7 lore raw
In reply to Peter Maydell
Quoted 16 lines · Peter Maydell
Quoted 15 lines · Helge Deller

From: Helge Deller <[email protected]>

Qemu user mode does not properly flushes error messages related to bad arguments when exiting (at least when the output is piped to a file instead of running on a terminal).
Ensure that we always flush by using exit() instead of _exit().

Reported by: Tobias Bergkvist
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/2544

---
  linux-user/main.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines

linux-user/main.c int main(int argc, char **argv, char **envp) · line 772

Quoted 12 lines
linux-user/main.c +1 -1
index c49d1e91d2..84e110dfe9 100644
@@ -767,7 +767,7 @@ int main(int argc, char **argv, char **envp)
          execfd = open(exec_path, O_RDONLY);
          if (execfd < 0) {
              printf("Error while loading %s: %s\n", exec_path, strerror(errno));
-            _exit(EXIT_FAILURE);
+            exit(EXIT_FAILURE);
          }
      }

How about the other _exit() further down in the function for the case where loader_exec() fails ? That seems like it would have the same problem.

I'll send a seperate patch for this one. Will then include in next pull request series.

Thanks!
Helge

#5 lore raw
In reply to Peter Maydell
Quoted 26 lines · Peter Maydell
Quoted 15 lines · Warner Losh

I don't think this will break anything. The printf can't possibly work, either we must change it to a write or do the exit. exit is done everywhere else. This has been wrong since:

commit 03cfd8faa7ffb7201e2949b99c2f35b1fef7078b

Author: Laurent Vivier <[email protected]>
Date:   Fri Aug 30 01:46:44 2013 +0200

linux-user: add support of binfmt_misc 'O' flag
...

(quoted diff) +4 -0
+    if (execfd < 0) {
+        printf("Error while loading %s: %s\n", filename, strerror(-execfd));
+        _exit(1);
+    }
...
Quoted 1 line

but the full commit log doesn't say why.

My guess is that the "print an error and exit" if() block was simply copied from the bit further down that does:

     if (ret != 0) {
         printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
         _exit(EXIT_FAILURE);
     }
Quoted 5 lines

for the loader_exec failure case. That one dates back to commit 74cd30b811cc2f in 2003, which changes some exit() calls to _exit(). I have no idea why, and the commit message is cryptic and useless. But it seems unlikely that the reason is still relevant today.

Could it be do discard any atexit() callback?