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

Helge Deller posted 1 patch 4 weeks, 1 day ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260425144852.46590-1-deller@kernel.org
Maintainers: Laurent Vivier <laurent@vivier.eu>, Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
There is a newer version of this series
linux-user/main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] linux-user: Flush errors by using exit() instead of _exit() in error path
Posted by Helge Deller 4 weeks, 1 day ago
From: Helge Deller <deller@gmx.de>

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
Signed-off-by: Helge Deller <deller@gmx.de>
---
 linux-user/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/linux-user/main.c b/linux-user/main.c
index c49d1e91d2..84e110dfe9 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -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);
         }
     }
 
-- 
2.53.0
Re: [PATCH] linux-user: Flush errors by using exit() instead of _exit() in error path
Posted by Peter Maydell 4 weeks ago
On Sat, 25 Apr 2026 at 15:49, Helge Deller <deller@kernel.org> wrote:
>
> From: Helge Deller <deller@gmx.de>
>
> 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
> Signed-off-by: Helge Deller <deller@gmx.de>
> ---
>  linux-user/main.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/linux-user/main.c b/linux-user/main.c
> index c49d1e91d2..84e110dfe9 100644
> --- a/linux-user/main.c
> +++ b/linux-user/main.c
> @@ -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
Re: [PATCH] linux-user: Flush errors by using exit() instead of _exit() in error path
Posted by Helge Deller 4 weeks ago
On 4/26/26 15:01, Peter Maydell wrote:
> On Sat, 25 Apr 2026 at 15:49, Helge Deller <deller@kernel.org> wrote:
>>
>> From: Helge Deller <deller@gmx.de>
>>
>> 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
>> Signed-off-by: Helge Deller <deller@gmx.de>
>> ---
>>   linux-user/main.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/linux-user/main.c b/linux-user/main.c
>> index c49d1e91d2..84e110dfe9 100644
>> --- a/linux-user/main.c
>> +++ b/linux-user/main.c
>> @@ -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
Re: [PATCH] linux-user: Flush errors by using exit() instead of _exit() in error path
Posted by Warner Losh 4 weeks, 1 day ago
On Sat, Apr 25, 2026 at 8:50 AM Helge Deller <deller@kernel.org> wrote:

> From: Helge Deller <deller@gmx.de>
>
> 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
> Signed-off-by: Helge Deller <deller@gmx.de>
> ---
>  linux-user/main.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>

Reviewed-by: Warner Losh <imp@bsdimp.com>

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

Warner
Re: [PATCH] linux-user: Flush errors by using exit() instead of _exit() in error path
Posted by Helge Deller 4 weeks, 1 day ago
On 4/25/26 17:02, Warner Losh wrote:
> 
> 
> On Sat, Apr 25, 2026 at 8:50 AM Helge Deller <deller@kernel.org <mailto:deller@kernel.org>> wrote:
> 
>     From: Helge Deller <deller@gmx.de <mailto:deller@gmx.de>>
> 
>     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>
>     Signed-off-by: Helge Deller <deller@gmx.de <mailto:deller@gmx.de>>
>     ---
>       linux-user/main.c | 2 +-
>       1 file changed, 1 insertion(+), 1 deletion(-)
> 
> 
> Reviewed-by: Warner Losh <imp@bsdimp.com <mailto:imp@bsdimp.com>>

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

Ok.

> 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
Re: [PATCH] linux-user: Flush errors by using exit() instead of _exit() in error path
Posted by Warner Losh 4 weeks, 1 day ago
[[ Oops, my last message cc'd Pierrick's old address, maybe my tree is old
or it needs to update in MAINTAINERS ]]

On Sat, Apr 25, 2026 at 9:17 AM Helge Deller <deller@gmx.de> wrote:

> On 4/25/26 17:02, Warner Losh wrote:
> >
> >
> > On Sat, Apr 25, 2026 at 8:50 AM Helge Deller <deller@kernel.org <mailto:
> deller@kernel.org>> wrote:
> >
> >     From: Helge Deller <deller@gmx.de <mailto:deller@gmx.de>>
> >
> >     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>
> >     Signed-off-by: Helge Deller <deller@gmx.de <mailto:deller@gmx.de>>
> >     ---
> >       linux-user/main.c | 2 +-
> >       1 file changed, 1 insertion(+), 1 deletion(-)
> >
> >
> > Reviewed-by: Warner Losh <imp@bsdimp.com <mailto:imp@bsdimp.com>>
>
> Thanks!
>
> > Note: I've added the linux-user maintainers to the CC list for
> visibility.
>
> Ok.
>
> > 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.


> 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 <laurent@vivier.eu>
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
Re: [PATCH] linux-user: Flush errors by using exit() instead of _exit() in error path
Posted by Peter Maydell 4 weeks ago
On Sat, 25 Apr 2026 at 16:51, Warner Losh <imp@bsdimp.com> wrote:
> 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 <laurent@vivier.eu>
> 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.

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
Re: [PATCH] linux-user: Flush errors by using exit() instead of _exit() in error path
Posted by Philippe Mathieu-Daudé 4 weeks ago
On 26/4/26 15:06, Peter Maydell wrote:
> On Sat, 25 Apr 2026 at 16:51, Warner Losh <imp@bsdimp.com> wrote:
>> 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 <laurent@vivier.eu>
>> 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.
> 
> 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.

Could it be do discard any atexit() callback?