comp.lang.ada
 help / color / mirror / Atom feed
* GNAT and SIGTERM (Linux x86_64)
@ 2010-10-26 15:52 Bryan
  2010-10-26 16:36 ` Yannick Duchêne (Hibou57)
  0 siblings, 1 reply; 4+ messages in thread
From: Bryan @ 2010-10-26 15:52 UTC (permalink / raw)


I'm trying to capture SIGTERM in a very simple test program but I'm
running into some issues. When I execute the code (at the end of
message), I get the following output in my terminal when I send "kill -
SIGTERM <PID>:

> ./entry_point
SIGTERM handler is NOT reserved
!!! SIGTERM handler is NOT attached !!!
Terminated

I attached the signal handler in signals.ads, but it seems the handler
is still not attached.  The sample program in the Big Book of Ada
Programming for Linux attaches the handler in the package
specification, and I tried to follow the sample program.  To make
things more interesting, If I run this same code on a Mac OS X with
GNAT, I get the following as my output:

> ./entry_point
SIGTERM handler is reserved

raised PROGRAM_ERROR : Interrupt 15 is reserved

This happens even with the Unreserve_All_Interrupts in signals.ads.
The behavior of both the Linux and the Mac binary are leading me to--
perhaps wrongly--consider that there is something I am missing with
GNAT to allow it to work with UNIX signals.

Questions:
1) Do my pragma statements need to be in the specification or the
body?
2) Do I need to pass some sort of flag to gnat to enable signals?

I would greatly appreciate any input or advice on this issue.

--------------------------------------
Test program source code:
--------------------------------------
-- Signals.ads
with Ada.Interrupts; use Ada.Interrupts;
with Ada.Interrupts.Names; use Ada.Interrupts.Names;
package Signals is
  pragma Unreserve_All_Interrupts;
  protected type Signal_Handler is
    procedure Handle_SIGTERM;
    pragma Attach_Handler(Handle_SIGTERM, SIGTERM);
    pragma Interrupt_State (SIGTERM, System);
  end Signal_Handler;
end Signals;

-- Signals.adb
with Ada.Text_IO; use Ada.Text_IO;
package body Signals is
  protected body Signal_Handler is
    procedure Handle_SIGTERM is
    begin
      Put_Line( "SIGTERM caught!" );
    end Handle_SIGTERM;
  end Signal_Handler;
end Signals;

-- Entrypoint.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Interrupts; use Ada.Interrupts;
with Ada.Interrupts.Names; use Ada.Interrupts.Names;
with Signals; use Signals;

procedure Entry_Point is
  Shutdown_Flag : Boolean := False;
begin
  if Is_Reserved(SIGTERM) then
    Put_Line("SIGTERM handler is reserved");
  else
    Put_Line("SIGTERM handler is NOT reserved");
  end if;

  if Is_Attached(SIGTERM) then
    Put_Line("SIGTERM handler is attached");
  else
    Put_Line("!!! SIGTERM handler is NOT attached !!!");
  end if;

  loop
    -- do nothing
    exit when Shutdown_Flag = True;
  end loop;
end Entry_Point;



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GNAT and SIGTERM (Linux x86_64)
  2010-10-26 15:52 GNAT and SIGTERM (Linux x86_64) Bryan
@ 2010-10-26 16:36 ` Yannick Duchêne (Hibou57)
  2010-10-27  7:43   ` Ludovic Brenta
  0 siblings, 1 reply; 4+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2010-10-26 16:36 UTC (permalink / raw)


Le Tue, 26 Oct 2010 17:52:57 +0200, Bryan <brobinson.eng@gmail.com> a  
écrit:

> I'm trying to capture SIGTERM in a very simple test program but I'm
> running into some issues. When I execute the code (at the end of
> message), I get the following output in my terminal when I send "kill -
> SIGTERM <PID>:
>
>> ./entry_point
> SIGTERM handler is NOT reserved
> !!! SIGTERM handler is NOT attached !!!
> Terminated
>
> [...]
> --------------------------------------
> Test program source code:
> --------------------------------------
> -- Signals.ads
> with Ada.Interrupts; use Ada.Interrupts;
> with Ada.Interrupts.Names; use Ada.Interrupts.Names;
> package Signals is
>   pragma Unreserve_All_Interrupts;
>   protected type Signal_Handler is
>     procedure Handle_SIGTERM;
>     pragma Attach_Handler(Handle_SIGTERM, SIGTERM);
>     pragma Interrupt_State (SIGTERM, System);
>   end Signal_Handler;
> end Signals;
>
> -- Signals.adb
> with Ada.Text_IO; use Ada.Text_IO;
> package body Signals is
>   protected body Signal_Handler is
>     procedure Handle_SIGTERM is
>     begin
>       Put_Line( "SIGTERM caught!" );
>     end Handle_SIGTERM;
>   end Signal_Handler;
> end Signals;
>
> -- Entrypoint.adb
> with Ada.Text_IO; use Ada.Text_IO;
> with Ada.Interrupts; use Ada.Interrupts;
> with Ada.Interrupts.Names; use Ada.Interrupts.Names;
> with Signals; use Signals;
>
> procedure Entry_Point is
>   Shutdown_Flag : Boolean := False;
> begin
>   if Is_Reserved(SIGTERM) then
>     Put_Line("SIGTERM handler is reserved");
>   else
>     Put_Line("SIGTERM handler is NOT reserved");
>   end if;
>
>   if Is_Attached(SIGTERM) then
>     Put_Line("SIGTERM handler is attached");
>   else
>     Put_Line("!!! SIGTERM handler is NOT attached !!!");
>   end if;
>
>   loop
>     -- do nothing
>     exit when Shutdown_Flag = True;
>   end loop;
> end Entry_Point;

Signal_Handler is a type, and a type does not live on its own. To make it  
“real”, you have to instantiate an entity of that type.

In few word : in Entry_Point, you forget to declare a variable of type  
Signal_Handler.

Try this :


  procedure Entry_Point is
     Shutdown_Flag : Boolean := False;
     Handler : Signal_Handler; -- < Add this line
  begin
     [... remaining of you cool stuff]
  end Entry_Point;

It works :)


-- 
Si les chats miaulent et font autant de vocalises bizarres, c’est pas pour  
les chiens.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GNAT and SIGTERM (Linux x86_64)
  2010-10-26 16:36 ` Yannick Duchêne (Hibou57)
@ 2010-10-27  7:43   ` Ludovic Brenta
  2010-10-27 14:21     ` Bryan
  0 siblings, 1 reply; 4+ messages in thread
From: Ludovic Brenta @ 2010-10-27  7:43 UTC (permalink / raw)


Yannick Duchêne wrote on comp.lang.ada:
> Signal_Handler is a type, and a type does not live on its own. To make it  
> “real”, you have to instantiate an entity of that type.

Good catch.

> In few word : in Entry_Point, you forget to declare a variable of type  
> Signal_Handler.
>
> Try this :
>
>   procedure Entry_Point is
>      Shutdown_Flag : Boolean := False;
>      Handler : Signal_Handler; -- < Add this line
>   begin
>      [... remaining of you cool stuff]
>   end Entry_Point;
>
> It works :)

Or, simply remove the "type" keyword from the declaration of
Signal_Handler, i.e.

--- signals.ads before
+++ signals.ads after
@@ -6,6 +6,6 @@
 with Ada.Interrupts.Names; use Ada.Interrupts.Names;
 package Signals is
   pragma Unreserve_All_Interrupts;
-  protected type Signal_Handler is
+  protected Signal_Handler is
     procedure Handle_SIGTERM;
     pragma Attach_Handler(Handle_SIGTERM, SIGTERM);
     pragma Interrupt_State (SIGTERM, System);

--
Ludovic Brenta.
I love hand-writing unified diffs in the morning.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: GNAT and SIGTERM (Linux x86_64)
  2010-10-27  7:43   ` Ludovic Brenta
@ 2010-10-27 14:21     ` Bryan
  0 siblings, 0 replies; 4+ messages in thread
From: Bryan @ 2010-10-27 14:21 UTC (permalink / raw)


Thank you both for the help.  I should have realized that if I didn't
instantiate an instance of the type, how could it be brought into the
entry_point program?

I made the change and it does work on Linux.  Interestingly, SIGTERM
is still reserved on OS X.  It must be something specific about OS X,
but I really just wanted to get this working on Linux.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-10-27 14:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-26 15:52 GNAT and SIGTERM (Linux x86_64) Bryan
2010-10-26 16:36 ` Yannick Duchêne (Hibou57)
2010-10-27  7:43   ` Ludovic Brenta
2010-10-27 14:21     ` Bryan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox