From: Vadim Godunko Date: Tue, 17 Sep 2019 08:01:48 +0000 (+0000) Subject: [Ada] Close file descriptors allocated for tty only once X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=94c44a8a3000348707a456f8ebc1b1d67b688ea4;p=gcc.git [Ada] Close file descriptors allocated for tty only once 2019-09-17 Vadim Godunko gcc/ada/ * libgnat/g-exptty.ads (Close_Input): New subprogram. * libgnat/g-exptty.adb (Close_Input): New subprogram. (Close): Move close of TTY to Close_Input. * terminals.c (__gnat_close_tty): Set file descriptors to invalid value after close. From-SVN: r275783 --- diff --git a/gcc/ada/ChangeLog b/gcc/ada/ChangeLog index ab8c8a52226..1ef2c628201 100644 --- a/gcc/ada/ChangeLog +++ b/gcc/ada/ChangeLog @@ -1,3 +1,11 @@ +2019-09-17 Vadim Godunko + + * libgnat/g-exptty.ads (Close_Input): New subprogram. + * libgnat/g-exptty.adb (Close_Input): New subprogram. + (Close): Move close of TTY to Close_Input. + * terminals.c (__gnat_close_tty): Set file descriptors to + invalid value after close. + 2019-09-17 Vadim Godunko * libgnat/g-expect.adb (Expect_Internal): Try to call 'poll' few diff --git a/gcc/ada/libgnat/g-exptty.adb b/gcc/ada/libgnat/g-exptty.adb index 4f0300fbd8d..b193448a0d0 100644 --- a/gcc/ada/libgnat/g-exptty.adb +++ b/gcc/ada/libgnat/g-exptty.adb @@ -74,9 +74,6 @@ package body GNAT.Expect.TTY is procedure Free_Process (Process : System.Address); pragma Import (C, Free_Process, "__gnat_free_process"); - procedure Close_TTY (Process : System.Address); - pragma Import (C, Close_TTY, "__gnat_close_tty"); - begin -- If we haven't already closed the process @@ -123,10 +120,6 @@ package body GNAT.Expect.TTY is Status := Descriptor.Exit_Status; end if; - if not On_Windows then - Close_TTY (Descriptor.Process); - end if; - Free_Process (Descriptor.Process'Address); Descriptor.Process := System.Null_Address; @@ -141,6 +134,47 @@ package body GNAT.Expect.TTY is Close (Descriptor, Status); end Close; + ----------------- + -- Close_Input -- + ----------------- + + overriding procedure Close_Input + (Descriptor : in out TTY_Process_Descriptor) + is + function TTY_FD + (Handle : System.Address) return GNAT.OS_Lib.File_Descriptor; + pragma Import (C, TTY_FD, "__gnat_tty_fd"); + + procedure Close_TTY (Process : System.Address); + pragma Import (C, Close_TTY, "__gnat_close_tty"); + + begin + if not On_Windows and then Descriptor.Process /= System.Null_Address then + -- Check whether input/output/error streams use master descriptor and + -- reset corresponding members. + + if Descriptor.Input_Fd = TTY_FD (Descriptor.Process) then + Descriptor.Input_Fd := Invalid_FD; + end if; + + if Descriptor.Output_Fd = TTY_FD (Descriptor.Process) then + Descriptor.Output_Fd := Invalid_FD; + end if; + + if Descriptor.Error_Fd = TTY_FD (Descriptor.Process) then + Descriptor.Error_Fd := Invalid_FD; + end if; + + -- Close master descriptor. + + Close_TTY (Descriptor.Process); + end if; + + -- Call parent's implementation to close all remaining descriptors. + + Process_Descriptor (Descriptor).Close_Input; + end Close_Input; + ----------------------------- -- Close_Pseudo_Descriptor -- ----------------------------- diff --git a/gcc/ada/libgnat/g-exptty.ads b/gcc/ada/libgnat/g-exptty.ads index 57aa8d792cc..81068a69ed7 100644 --- a/gcc/ada/libgnat/g-exptty.ads +++ b/gcc/ada/libgnat/g-exptty.ads @@ -134,6 +134,8 @@ private Cmd : String; Args : System.Address); + procedure Close_Input (Descriptor : in out TTY_Process_Descriptor); + Still_Active : constant Integer := -1; type TTY_Process_Descriptor is new Process_Descriptor with record diff --git a/gcc/ada/terminals.c b/gcc/ada/terminals.c index 320ad284306..0ce3fb788f9 100644 --- a/gcc/ada/terminals.c +++ b/gcc/ada/terminals.c @@ -1648,8 +1648,8 @@ __gnat_new_tty (void) */ void __gnat_close_tty (pty_desc* desc) { - if (desc->master_fd >= 0) close (desc->master_fd); - if (desc->slave_fd >= 0) close (desc->slave_fd); + if (desc->master_fd >= 0) { close (desc->master_fd); desc->master_fd = -1; } + if (desc->slave_fd >= 0) { close (desc->slave_fd); desc->slave_fd = -1; } } /* __gnat_tty_name - return slave side device name