diff mbox series

target/arm/arm-semi: Don't let the guest close stdin/stdout/stderr

Message ID 20200124172954.28481-1-peter.maydell@linaro.org
State New
Headers show
Series target/arm/arm-semi: Don't let the guest close stdin/stdout/stderr | expand

Commit Message

Peter Maydell Jan. 24, 2020, 5:29 p.m. UTC
The guest can use the semihosting API to open a handle
corresponding to QEMU's own stdin, stdout, or stderr.
When the guest closes this handle, we should not
close the underlying host stdin/stdout/stderr
the way we would do if the handle corresponded to
a host fd we'd opened on behalf of the guest in SYS_OPEN.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 target/arm/arm-semi.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

Alex Bennée Jan. 24, 2020, 7:57 p.m. UTC | #1
Peter Maydell <peter.maydell@linaro.org> writes:

> The guest can use the semihosting API to open a handle
> corresponding to QEMU's own stdin, stdout, or stderr.
> When the guest closes this handle, we should not
> close the underlying host stdin/stdout/stderr
> the way we would do if the handle corresponded to
> a host fd we'd opened on behalf of the guest in SYS_OPEN.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

> ---
>  target/arm/arm-semi.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/target/arm/arm-semi.c b/target/arm/arm-semi.c
> index 788fe61b51a..8718fd01948 100644
> --- a/target/arm/arm-semi.c
> +++ b/target/arm/arm-semi.c
> @@ -403,6 +403,15 @@ static uint32_t host_closefn(ARMCPU *cpu, GuestFD *gf)
>  {
>      CPUARMState *env = &cpu->env;
>  
> +    /*
> +     * Only close the underlying host fd if it's one we opened on behalf
> +     * of the guest in SYS_OPEN.
> +     */
> +    if (gf->hostfd == STDIN_FILENO ||
> +        gf->hostfd == STDOUT_FILENO ||
> +        gf->hostfd == STDERR_FILENO) {
> +        return 0;
> +    }
>      return set_swi_errno(env, close(gf->hostfd));
>  }
Philippe Mathieu-Daudé Jan. 27, 2020, 7:49 a.m. UTC | #2
On 1/24/20 8:57 PM, Alex Bennée wrote:
> Peter Maydell <peter.maydell@linaro.org> writes:
> 
>> The guest can use the semihosting API to open a handle
>> corresponding to QEMU's own stdin, stdout, or stderr.
>> When the guest closes this handle, we should not
>> close the underlying host stdin/stdout/stderr
>> the way we would do if the handle corresponded to
>> a host fd we'd opened on behalf of the guest in SYS_OPEN.
>>
>> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> 
> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

>> ---
>>   target/arm/arm-semi.c | 9 +++++++++
>>   1 file changed, 9 insertions(+)
>>
>> diff --git a/target/arm/arm-semi.c b/target/arm/arm-semi.c
>> index 788fe61b51a..8718fd01948 100644
>> --- a/target/arm/arm-semi.c
>> +++ b/target/arm/arm-semi.c
>> @@ -403,6 +403,15 @@ static uint32_t host_closefn(ARMCPU *cpu, GuestFD *gf)
>>   {
>>       CPUARMState *env = &cpu->env;
>>   
>> +    /*
>> +     * Only close the underlying host fd if it's one we opened on behalf
>> +     * of the guest in SYS_OPEN.
>> +     */
>> +    if (gf->hostfd == STDIN_FILENO ||
>> +        gf->hostfd == STDOUT_FILENO ||
>> +        gf->hostfd == STDERR_FILENO) {
>> +        return 0;
>> +    }
>>       return set_swi_errno(env, close(gf->hostfd));
>>   }
diff mbox series

Patch

diff --git a/target/arm/arm-semi.c b/target/arm/arm-semi.c
index 788fe61b51a..8718fd01948 100644
--- a/target/arm/arm-semi.c
+++ b/target/arm/arm-semi.c
@@ -403,6 +403,15 @@  static uint32_t host_closefn(ARMCPU *cpu, GuestFD *gf)
 {
     CPUARMState *env = &cpu->env;
 
+    /*
+     * Only close the underlying host fd if it's one we opened on behalf
+     * of the guest in SYS_OPEN.
+     */
+    if (gf->hostfd == STDIN_FILENO ||
+        gf->hostfd == STDOUT_FILENO ||
+        gf->hostfd == STDERR_FILENO) {
+        return 0;
+    }
     return set_swi_errno(env, close(gf->hostfd));
 }