Re: [Msys2-users] Catching Ctrl-C/SIGINT and other signals

2016-12-05 Thread J-Donald Tournier
Any update on this issue...? Main thing I'd like to know is whether or not
this is expected behaviour. Is this is a bug in MSYS2 / MinGW that will be
fixed in due course, or do we need to think about other approaches to
catching a Ctrl-C event? If the example I'd provided is supposed to work,
which of these (signal() or SetConsoleCtrlHandler()) is the recommended /
supported approach for when the bug is eventually fixed? And if this isn't
a bug, is there a consensus as to how this should be done?

Thanks again,
Donald.

On Thu, 1 Dec 2016 at 13:28 J-Donald Tournier  wrote:

> Just downloaded Git for Windows, and ran this:
>
> $ export PATH=/c/msys64/mingw64/bin/:$PATH
> $ gcc catch_ctrl-c.cpp -o catch_ctrl-c
> $ ./catch_ctrl-c.exe
> - tick -
> - tick -
>
> $
>
> same result - no handler invoked on Ctrl-C...
>
> Not sure whether simply adding /c/msys64/mingw64/bin to the PATH was the
> right thing to do though...?
>
> Thanks!
> Donald.
>
> On Thu, 1 Dec 2016 at 12:29 Ray Donnelly  wrote:
>
> Can you try this on the Git for Windows shell/bash and see if it works
> there?
>
> On Thu, Dec 1, 2016 at 11:44 AM, J-Donald Tournier 
> wrote:
> > Hi all,
> >
> > I'm trying to catch signals and user interrupts in my application to
> perform
> > some cleanup, but having trouble getting this to work on MSYS2 (up to
> date
> > as of this morning). To get to the bottom of this, I've written this
> minimal
> > (not) working example:
> >
> > --
> > #include 
> > #include 
> > #include 
> > #include 
> > #include 
> >
> >
> > // WIN32 Ctrl handler:
> > BOOL WINAPI CtrlHandler (DWORD CtrlType)
> > {
> >   if (CtrlType == CTRL_C_EVENT) {
> > fprintf (stderr, "caught Ctrl-C (via CtrlHandler)\n");
> > exit(0);
> >   }
> >   fprintf (stderr, "caught something else (via CtrlHandler)\n");
> >   return true;
> > }
> >
> > // POSIX signal handler:
> > void signal_handler (int sig)
> > {
> >   if (sig == SIGINT) {
> > fprintf (stderr, "caught Ctrl-C (via signal_handler)\n");
> > exit (0);
> >   }
> >   fprintf (stderr, "caught something else (via signal_handler)\n");
> >   exit (0);
> > }
> >
> >
> > int main (int argc, char* argv[])
> > {
> >   // avoid buffering on stderr:
> >   setvbuf(stderr, NULL, _IONBF, 0);
> >
> >   // set Windows Ctrl handler:
> >   SetConsoleCtrlHandler (CtrlHandler, true);
> >
> >   // set POSIX signal handler:
> >   signal (SIGINT, signal_handler);
> >
> >
> >   /* the following does not compile:
> >
> >   // set signal handler via sigaction():
> >   struct sigaction act;
> >   act.sa_handler = _handler;
> >   sigfillset (_mask);
> >   act.sa_flags = 0;
> >   sigaction (SIGINT, , NULL);
> >
> >   */
> >
> >   // twiddle your thumbs until the user presses Ctrl-C:
> >   while (1) {
> > fprintf (stderr, "- tick -\n");
> > sleep (1);
> >   }
> >
> >   return 0;
> > }
> > --
> >
> > which I compile and run with:
> >
> > --
> > $ gcc catch_ctrl-c.cpp -o catch_ctrl-c && ./catch_ctrl-c.exe
> > - tick -
> > - tick -
> >
> > $
> > --
> >
> > In the above, I hit Ctrl-C after the second tick, but neither handler was
> > invoked. You'll also note I tried the sigaction() version too (what we
> use
> > on Linux, as recommended on the manpage), but if I uncomment this
> section,
> > it fails to compile on MSYS2, with the following error:
> > --
> > catch_ctrl-c.cpp: In function 'int main(int, char**)':
> > catch_ctrl-c.cpp:46:20: error: aggregate 'main(int, char**)::sigaction
> act'
> > has incomplete type and cannot be defined
> >struct sigaction act;
> > ^~~
> > catch_ctrl-c.cpp:48:27: error: 'sigfillset' was not declared in this
> scope
> >sigfillset (_mask);
> >^
> > catch_ctrl-c.cpp:50:32: error: invalid use of incomplete type 'struct
> > main(int, char**)::sigaction'
> >sigaction (SIGINT, , NULL);
> > ^
> > catch_ctrl-c.cpp:46:10: note: forward declaration of 'struct main(int,
> > char**)::sigaction'
> >struct sigaction act;
> >   ^
> > --
> >
> > Also tested with clang, same result. Does anyone know how to get this to
> > work in MSYS2?
> >
> > Thanks!
> > Donald.
> >
> > --
> > Dr J-Donald Tournier (PhD)
> >
> > Senior Lecturer, Biomedical Engineering
> > Division of Imaging Sciences & Biomedical Engineering
> > King's College London
> >
> > A: Department of Perinatal Imaging & Health, 1st Floor South Wing, St
> > Thomas' Hospital, London. SE1 7EH
> > T: +44 (0)20 7188 7118 ext 53613 <+44%2020%207188%207118>
> > W:
> >
> http://www.kcl.ac.uk/medicine/research/divisions/imaging/departments/biomedengineering
> >
> >
> 

Re: [Msys2-users] Catching Ctrl-C/SIGINT and other signals

2016-12-01 Thread J-Donald Tournier
Just downloaded Git for Windows, and ran this:

$ export PATH=/c/msys64/mingw64/bin/:$PATH
$ gcc catch_ctrl-c.cpp -o catch_ctrl-c
$ ./catch_ctrl-c.exe
- tick -
- tick -

$

same result - no handler invoked on Ctrl-C...

Not sure whether simply adding /c/msys64/mingw64/bin to the PATH was the
right thing to do though...?

Thanks!
Donald.

On Thu, 1 Dec 2016 at 12:29 Ray Donnelly  wrote:

> Can you try this on the Git for Windows shell/bash and see if it works
> there?
>
> On Thu, Dec 1, 2016 at 11:44 AM, J-Donald Tournier 
> wrote:
> > Hi all,
> >
> > I'm trying to catch signals and user interrupts in my application to
> perform
> > some cleanup, but having trouble getting this to work on MSYS2 (up to
> date
> > as of this morning). To get to the bottom of this, I've written this
> minimal
> > (not) working example:
> >
> > --
> > #include 
> > #include 
> > #include 
> > #include 
> > #include 
> >
> >
> > // WIN32 Ctrl handler:
> > BOOL WINAPI CtrlHandler (DWORD CtrlType)
> > {
> >   if (CtrlType == CTRL_C_EVENT) {
> > fprintf (stderr, "caught Ctrl-C (via CtrlHandler)\n");
> > exit(0);
> >   }
> >   fprintf (stderr, "caught something else (via CtrlHandler)\n");
> >   return true;
> > }
> >
> > // POSIX signal handler:
> > void signal_handler (int sig)
> > {
> >   if (sig == SIGINT) {
> > fprintf (stderr, "caught Ctrl-C (via signal_handler)\n");
> > exit (0);
> >   }
> >   fprintf (stderr, "caught something else (via signal_handler)\n");
> >   exit (0);
> > }
> >
> >
> > int main (int argc, char* argv[])
> > {
> >   // avoid buffering on stderr:
> >   setvbuf(stderr, NULL, _IONBF, 0);
> >
> >   // set Windows Ctrl handler:
> >   SetConsoleCtrlHandler (CtrlHandler, true);
> >
> >   // set POSIX signal handler:
> >   signal (SIGINT, signal_handler);
> >
> >
> >   /* the following does not compile:
> >
> >   // set signal handler via sigaction():
> >   struct sigaction act;
> >   act.sa_handler = _handler;
> >   sigfillset (_mask);
> >   act.sa_flags = 0;
> >   sigaction (SIGINT, , NULL);
> >
> >   */
> >
> >   // twiddle your thumbs until the user presses Ctrl-C:
> >   while (1) {
> > fprintf (stderr, "- tick -\n");
> > sleep (1);
> >   }
> >
> >   return 0;
> > }
> > --
> >
> > which I compile and run with:
> >
> > --
> > $ gcc catch_ctrl-c.cpp -o catch_ctrl-c && ./catch_ctrl-c.exe
> > - tick -
> > - tick -
> >
> > $
> > --
> >
> > In the above, I hit Ctrl-C after the second tick, but neither handler was
> > invoked. You'll also note I tried the sigaction() version too (what we
> use
> > on Linux, as recommended on the manpage), but if I uncomment this
> section,
> > it fails to compile on MSYS2, with the following error:
> > --
> > catch_ctrl-c.cpp: In function 'int main(int, char**)':
> > catch_ctrl-c.cpp:46:20: error: aggregate 'main(int, char**)::sigaction
> act'
> > has incomplete type and cannot be defined
> >struct sigaction act;
> > ^~~
> > catch_ctrl-c.cpp:48:27: error: 'sigfillset' was not declared in this
> scope
> >sigfillset (_mask);
> >^
> > catch_ctrl-c.cpp:50:32: error: invalid use of incomplete type 'struct
> > main(int, char**)::sigaction'
> >sigaction (SIGINT, , NULL);
> > ^
> > catch_ctrl-c.cpp:46:10: note: forward declaration of 'struct main(int,
> > char**)::sigaction'
> >struct sigaction act;
> >   ^
> > --
> >
> > Also tested with clang, same result. Does anyone know how to get this to
> > work in MSYS2?
> >
> > Thanks!
> > Donald.
> >
> > --
> > Dr J-Donald Tournier (PhD)
> >
> > Senior Lecturer, Biomedical Engineering
> > Division of Imaging Sciences & Biomedical Engineering
> > King's College London
> >
> > A: Department of Perinatal Imaging & Health, 1st Floor South Wing, St
> > Thomas' Hospital, London. SE1 7EH
> > T: +44 (0)20 7188 7118 ext 53613 <+44%2020%207188%207118>
> > W:
> >
> http://www.kcl.ac.uk/medicine/research/divisions/imaging/departments/biomedengineering
> >
> >
> --
> >
> > ___
> > Msys2-users mailing list
> > Msys2-users@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/msys2-users
> >
>
-- 
*Dr J-Donald Tournier (PhD)*

*Senior Lecturer, **Biomedical Engineering*

*Division of Imaging Sciences & Biomedical EngineeringKing's College London*


*A: Department of Perinatal Imaging & Health, 1st Floor South Wing, St
Thomas' Hospital, London. SE1 7EH*
*T: +44 (0)20 7188 7118 ext 53613*
*W: 

Re: [Msys2-users] Catching Ctrl-C/SIGINT and other signals

2016-12-01 Thread Ray Donnelly
Can you try this on the Git for Windows shell/bash and see if it works there?

On Thu, Dec 1, 2016 at 11:44 AM, J-Donald Tournier  wrote:
> Hi all,
>
> I'm trying to catch signals and user interrupts in my application to perform
> some cleanup, but having trouble getting this to work on MSYS2 (up to date
> as of this morning). To get to the bottom of this, I've written this minimal
> (not) working example:
>
> --
> #include 
> #include 
> #include 
> #include 
> #include 
>
>
> // WIN32 Ctrl handler:
> BOOL WINAPI CtrlHandler (DWORD CtrlType)
> {
>   if (CtrlType == CTRL_C_EVENT) {
> fprintf (stderr, "caught Ctrl-C (via CtrlHandler)\n");
> exit(0);
>   }
>   fprintf (stderr, "caught something else (via CtrlHandler)\n");
>   return true;
> }
>
> // POSIX signal handler:
> void signal_handler (int sig)
> {
>   if (sig == SIGINT) {
> fprintf (stderr, "caught Ctrl-C (via signal_handler)\n");
> exit (0);
>   }
>   fprintf (stderr, "caught something else (via signal_handler)\n");
>   exit (0);
> }
>
>
> int main (int argc, char* argv[])
> {
>   // avoid buffering on stderr:
>   setvbuf(stderr, NULL, _IONBF, 0);
>
>   // set Windows Ctrl handler:
>   SetConsoleCtrlHandler (CtrlHandler, true);
>
>   // set POSIX signal handler:
>   signal (SIGINT, signal_handler);
>
>
>   /* the following does not compile:
>
>   // set signal handler via sigaction():
>   struct sigaction act;
>   act.sa_handler = _handler;
>   sigfillset (_mask);
>   act.sa_flags = 0;
>   sigaction (SIGINT, , NULL);
>
>   */
>
>   // twiddle your thumbs until the user presses Ctrl-C:
>   while (1) {
> fprintf (stderr, "- tick -\n");
> sleep (1);
>   }
>
>   return 0;
> }
> --
>
> which I compile and run with:
>
> --
> $ gcc catch_ctrl-c.cpp -o catch_ctrl-c && ./catch_ctrl-c.exe
> - tick -
> - tick -
>
> $
> --
>
> In the above, I hit Ctrl-C after the second tick, but neither handler was
> invoked. You'll also note I tried the sigaction() version too (what we use
> on Linux, as recommended on the manpage), but if I uncomment this section,
> it fails to compile on MSYS2, with the following error:
> --
> catch_ctrl-c.cpp: In function 'int main(int, char**)':
> catch_ctrl-c.cpp:46:20: error: aggregate 'main(int, char**)::sigaction act'
> has incomplete type and cannot be defined
>struct sigaction act;
> ^~~
> catch_ctrl-c.cpp:48:27: error: 'sigfillset' was not declared in this scope
>sigfillset (_mask);
>^
> catch_ctrl-c.cpp:50:32: error: invalid use of incomplete type 'struct
> main(int, char**)::sigaction'
>sigaction (SIGINT, , NULL);
> ^
> catch_ctrl-c.cpp:46:10: note: forward declaration of 'struct main(int,
> char**)::sigaction'
>struct sigaction act;
>   ^
> --
>
> Also tested with clang, same result. Does anyone know how to get this to
> work in MSYS2?
>
> Thanks!
> Donald.
>
> --
> Dr J-Donald Tournier (PhD)
>
> Senior Lecturer, Biomedical Engineering
> Division of Imaging Sciences & Biomedical Engineering
> King's College London
>
> A: Department of Perinatal Imaging & Health, 1st Floor South Wing, St
> Thomas' Hospital, London. SE1 7EH
> T: +44 (0)20 7188 7118 ext 53613
> W:
> http://www.kcl.ac.uk/medicine/research/divisions/imaging/departments/biomedengineering
>
> --
>
> ___
> Msys2-users mailing list
> Msys2-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/msys2-users
>

--
___
Msys2-users mailing list
Msys2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/msys2-users


[Msys2-users] Catching Ctrl-C/SIGINT and other signals

2016-12-01 Thread J-Donald Tournier
Hi all,

I'm trying to catch signals and user interrupts in my application to
perform some cleanup, but having trouble getting this to work on MSYS2 (up
to date as of this morning). To get to the bottom of this, I've written
this minimal (not) working example:

--
#include 
#include 
#include 
#include 
#include 


// WIN32 Ctrl handler:
BOOL WINAPI CtrlHandler (DWORD CtrlType)
{
  if (CtrlType == CTRL_C_EVENT) {
fprintf (stderr, "caught Ctrl-C (via CtrlHandler)\n");
exit(0);
  }
  fprintf (stderr, "caught something else (via CtrlHandler)\n");
  return true;
}

// POSIX signal handler:
void signal_handler (int sig)
{
  if (sig == SIGINT) {
fprintf (stderr, "caught Ctrl-C (via signal_handler)\n");
exit (0);
  }
  fprintf (stderr, "caught something else (via signal_handler)\n");
  exit (0);
}


int main (int argc, char* argv[])
{
  // avoid buffering on stderr:
  setvbuf(stderr, NULL, _IONBF, 0);

  // set Windows Ctrl handler:
  SetConsoleCtrlHandler (CtrlHandler, true);

  // set POSIX signal handler:
  signal (SIGINT, signal_handler);


  /* the following does not compile:

  // set signal handler via sigaction():
  struct sigaction act;
  act.sa_handler = _handler;
  sigfillset (_mask);
  act.sa_flags = 0;
  sigaction (SIGINT, , NULL);

  */

  // twiddle your thumbs until the user presses Ctrl-C:
  while (1) {
fprintf (stderr, "- tick -\n");
sleep (1);
  }

  return 0;
}
--

which I compile and run with:

--
$ gcc catch_ctrl-c.cpp -o catch_ctrl-c && ./catch_ctrl-c.exe
- tick -
- tick -

$
--

In the above, I hit Ctrl-C after the second tick, but neither handler was
invoked. You'll also note I tried the sigaction() version too (what we use
on Linux, as recommended on the manpage), but if I uncomment this section,
it fails to compile on MSYS2, with the following error:
--
catch_ctrl-c.cpp: In function 'int main(int, char**)':
catch_ctrl-c.cpp:46:20: error: aggregate 'main(int, char**)::sigaction act'
has incomplete type and cannot be defined
   struct sigaction act;
^~~
catch_ctrl-c.cpp:48:27: error: 'sigfillset' was not declared in this scope
   sigfillset (_mask);
   ^
catch_ctrl-c.cpp:50:32: error: invalid use of incomplete type 'struct
main(int, char**)::sigaction'
   sigaction (SIGINT, , NULL);
^
catch_ctrl-c.cpp:46:10: note: forward declaration of 'struct main(int,
char**)::sigaction'
   struct sigaction act;
  ^
--

Also tested with clang, same result. Does anyone know how to get this to
work in MSYS2?

Thanks!
Donald.

-- 
*Dr J-Donald Tournier (PhD)*

*Senior Lecturer, **Biomedical Engineering*

*Division of Imaging Sciences & Biomedical EngineeringKing's College London*


*A: Department of Perinatal Imaging & Health, 1st Floor South Wing, St
Thomas' Hospital, London. SE1 7EH*
*T: +44 (0)20 7188 7118 ext 53613*
*W: 
http://www.kcl.ac.uk/medicine/research/divisions/imaging/departments/biomedengineering
*
--
___
Msys2-users mailing list
Msys2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/msys2-users