Python Forum
Determine if keyboard interrupt versus SIGINT trapped?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Determine if keyboard interrupt versus SIGINT trapped?
#1
Determine if keyboard interrupt versus SIGINT trapped?

Not sure there is a way to determine this per se? Looked via Google for method, but the best I could find or rather think of was:

Trap SIGINT and KeyboardInterrupt via signal handler, and test if session is interactive, say via sys.__stdin__.isatty(). But this would not be completely accurate, because if SIGINT is sent to python (session, i.e. executing script) from somewhere else, it still might have been an interactive session versus called via cron or as service emulation under systemd. Adding a test to see if systemd active maybe? Such as if psutil.Process(os.getpid()).ppid() == 1? But that is still not 100% bullet proof, right?
Reply
#2
What is the use case for this?
Reply
#3
I thought I explained that above, but to clarify, to determine the true source of the signal/interrupt, we have a situation where an operator can 'kill' the script while running interactive or via system unit file instance. As an audit control measure we want to be able within the script to tell the difference, if possible, if the SIGINT came from the operator or from monitoring control system. Thus, we want to know if Keyboard Interrupt was done or the SIGINT was from a our monitoring and control system, or if SIGINT was generated by a 3rd party source. Since we can track the SIGINT from the monitoring and control system, we need to still track KeyboardInterrupt or 3rd party source sending SIGINT. The auditors want the MOST details as possible, so the question was posted. Just saying any SIGINT or KeyboardInterrupt not from the monitor and control system, is too much of a catch all for our auditors, unfortunately. If I can prove, with reasonable effort, that there is not method available to avoid the catch all scenario, the auditors will compromise. This thread is part of the investigative effort to prove the is not a reasonable method to do as they requested, and the catch all concept will have to be accepted as best possible option at this time.
Reply
#4
I don't think Python can do that directly however, in C there is a function named sigaction() that gives information about the process that sent the signal. You could find ways of calling this function by writing a C extension that invokes sigaction() or by using the ctypes module.
Reply
#5
(Sep-29-2022, 08:07 AM)Gribouillis Wrote: I don't think Python can do that directly however, in C there is a function named sigaction() that gives information about the process that sent the signal. You could find ways of calling this function by writing a C extension that invokes sigaction() or by using the ctypes module.

Thank you for that information, that is likely what we all need to do.
Reply
#6
Actually somebody has already written a wrapper of sigaction() and sigqueue() with ctypes: there is a module signal2 in Pypi. It was uploaded in 2011 and I don't know if it will work for Python 3 but it looks worth trying.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  add interrupt for next task kucingkembar 0 777 Oct-07-2022, 12:15 PM
Last Post: kucingkembar
  Enabling interrupt on Adafruits button/led board Moris526 0 2,025 Apr-30-2021, 03:29 PM
Last Post: Moris526
  python delay without interrupt the whole code Nick_tkinter 4 5,163 Feb-22-2021, 10:51 PM
Last Post: nilamo
  Adafruits Neotrellis interrupt with RAsp and Python Moris526 5 3,576 Jan-01-2021, 11:43 PM
Last Post: Moris526
  Interrupt for Adafruits Neotrellis button/led board Moris526 0 1,806 Dec-28-2020, 05:42 AM
Last Post: Moris526
  input interrupt Nickd12 1 4,273 Dec-09-2020, 05:01 PM
Last Post: Gribouillis
  Wsgiref versus Requisitions JohnnyCoffee 1 1,635 Jun-14-2020, 05:15 PM
Last Post: Larz60+
  Config file entry as list versus string? taziuk 2 1,831 Apr-25-2020, 12:01 PM
Last Post: ndc85430
  How to determine pen color from an image? robie972003 2 2,402 Mar-24-2019, 10:06 PM
Last Post: robie972003
  determine if an number is in a list Dbeah 7 3,794 Nov-06-2018, 12:11 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020