FedoraForum.org - Fedora Support Forums and Community
Results 1 to 13 of 13
  1. #1
    Join Date
    Nov 2015
    Location
    Reston, VA
    Posts
    87

    SIGTERM from keyboard

    In my C++ program run on the terminal, I would like to type a character that
    would send a signal that could be caught and processed in my handler,
    created with ::sigaction(). CRTL-C sends SIGKILL, which is ignored in my handler,
    and I can find no key that would send SIGTERM which is handled as I specify.
    Any suggestions?

    Thanks,
    Mike.

  2. #2
    Join Date
    Sep 2009
    Posts
    2,173

    Re: SIGTERM from keyboard

    Try ctrl-Z, it sends SIGSTP iirc.

    dd_wizard

  3. #3
    Join Date
    Nov 2015
    Location
    Reston, VA
    Posts
    87

    Re: SIGTERM from keyboard

    Thanks but SIGSTP is undefined; SIGSTOP is an illegal argument of ::sigaction().

    Mike.

  4. #4
    Join Date
    Sep 2009
    Posts
    2,173

    Re: SIGTERM from keyboard

    It should have been SIGTSTP, but that may not get there. It tells the shell to suspend execution.

    dd_wizard

  5. #5
    Join Date
    Nov 2015
    Location
    Reston, VA
    Posts
    87

    Re: SIGTERM from keyboard

    Yes,

    SIGTSTP works with ctrl-z
    and
    SIGQUIT works with ctrl \

    Mike.

  6. #6
    Join Date
    Sep 2009
    Posts
    2,173

    Re: SIGTERM from keyboard

    Do you get the core dump with ctrl \? I worked out how to control a shell script with ctrl-z, but you get a core dump with ctrl-\.

    dd_wizard

  7. #7
    Join Date
    Oct 2010
    Location
    Canberra
    Posts
    2,921

    Re: SIGTERM from keyboard

    I am a bit puzzled by your statement that Ctrl-C sends SIGKILL (9). It should send SIGINT (2) which you can catch in your handler.

    If you run stty -a it will list the keyboard generated signals.

  8. #8
    Join Date
    Nov 2015
    Location
    Reston, VA
    Posts
    87

    Re: SIGTERM from keyboard

    dd_wizard:
    As far as I can see, there is no core dump. ctrl z and ctrl \ appear to work the same. I note
    that I called ::sigaction() for each of them set to the same handler.

    ocratato:
    I quote man 2 sigaction:
    " ... signum specifies the signal and can be any valid signal except SIGKILL and SIGSTOP."
    I don't know why.

    Mike.

  9. #9
    Join Date
    Oct 2010
    Location
    Canberra
    Posts
    2,921

    Re: SIGTERM from keyboard

    Quote Originally Posted by MikeBerger
    I quote man 2 sigaction:
    " ... signum specifies the signal and can be any valid signal except SIGKILL and SIGSTOP."
    I don't know why.
    Those signals don't get delivered to the application, but are handled by the kernel - to either kill it or remove it from the scheduler's queue.

    Getting back to your original question - Ctrl-C should be generating a SIGINT rather than SIGTERM. If you don't catch the SIGINT your program would be terminated.

    Added: To get a core dump you need to run the command "ulimit -c unlimited". This will result in core dumps from Ctrl-\ (as well as program crashes) for programs run from the same instance of bash.
    Last edited by ocratato; 9th March 2018 at 04:06 AM.

  10. #10
    Join Date
    Nov 2015
    Location
    Reston, VA
    Posts
    87

    Re: SIGTERM from keyboard

    Yes, I tried SIGINT and it works with ctrl-c.
    Thanks,
    Mike.

  11. #11
    Join Date
    Oct 2010
    Location
    Canberra
    Posts
    2,921

    Re: SIGTERM from keyboard

    Good to see that it works.

    The other thing to be aware of when writing a signal handler is that the number of system calls available is much reduced - and does not include malloc - so C++ new should not be used. The annoying thing is that the other calls might work, but are not safe - they could cause the program to lock up.

  12. #12
    Join Date
    Nov 2015
    Location
    Reston, VA
    Posts
    87

    Re: SIGTERM from keyboard

    Can I use C++ delete in the signal handler?
    Thanks,
    Mike.

  13. #13
    Join Date
    Oct 2010
    Location
    Canberra
    Posts
    2,921

    Re: SIGTERM from keyboard

    Quote Originally Posted by MikeBerger
    Can I use C++ delete in the signal handler?
    Thanks,
    Mike.
    Since the free() call is not safe, I believe the answer is no.

    The problem is that malloc and free take a lock to prevent two processes (or threads or signal handlers) from allocating/freeing at the same time.
    Thus there is a small chance that the signal will be called while your program is in the middle of another malloc/free, and the signal handler won't be able to get a lock, and will hang waiting for an unlock that can never come.

    In practice it will probably work, so you might use it for debugging, for example, but for a reliable program it would not be a good idea.

Similar Threads

  1. Replies: 0
    Last Post: 1st April 2015, 05:30 PM
  2. Apache SIGTERM issues
    By Makkura666 in forum Servers & Networking
    Replies: 0
    Last Post: 23rd April 2012, 12:12 PM
  3. ddclient:srv01 ddclient[4155]: WARNING: caught SIGTERM; exiting
    By wprauchholz in forum Servers & Networking
    Replies: 0
    Last Post: 9th February 2006, 05:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •