Commit 94c44a8a by Vadim Godunko Committed by Pierre-Marie de Rodat

[Ada] Close file descriptors allocated for tty only once

2019-09-17  Vadim Godunko  <godunko@adacore.com>

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
parent 19716ceb
2019-09-17 Vadim Godunko <godunko@adacore.com>
* 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 <godunko@adacore.com>
* libgnat/g-expect.adb (Expect_Internal): Try to call 'poll' few
times.
......
......@@ -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 --
-----------------------------
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment