c - Installing signal handler with Python -


(there follow question here)

i working on trying write python based init system linux i'm having issue getting signals python init script. 'man 2 kill' page:

the signals can sent process id 1, init process,   init has explicitly installed signal handlers. 

in python based init, have test function , signal handler setup call function:

def sigtest(sig, frm):     print "caught sighup!"  signal.signal(signal.sighup, sigtest) 

from tty (the init script executes sh on tty) if send signal, ignored , text never printed. kill -hup 1

i found issue because wrote reaping function python init reap child processes die, zombied, took awhile figure out python never getting sigchld signal. ensure environment sane, wrote c program fork , have child send pid 1 signal , did register.

how install signal handler system acknowledge if signal.signal(sig, func) isn't working?

im going try using ctypes register handler c code , see if works, rather pure python answer if @ possible.

ideas?

( i'm not programmer, im in on head here :p )

test code below...

import os import sys import time import signal   def sigtest(sig, frm):     print "sigint caught"  print "forking ash" cpid = os.fork() if cpid == 0:     os.closerange(0, 4)     sys.stdin = open('/dev/tty2', 'r')     sys.stdout = open('/dev/tty2', 'w')     sys.stderr = open('/dev/tty2', 'w')     os.execv('/bin/ash', ('ash',))  print "ash started on tty2"  signal.signal(signal.sighup, sigtest)  while true:     time.sleep(5.0) 

signal handlers work in python. there problems. 1 handler won't run until interpreter re-enters it's bytecode interpreter. if program blocked in c function signal handler not called until returns. don't show code waiting. using signal.pause()?

another if in system call exception after singal handler returns. need wrap system calls retry handler (at least on linux).

it's interesting writing init replacement... that's process manager. proctools code might interest you, since handle sigchld.

by way, code:

import signal  def sigtest(sig, frm):     print "sigint caught"  signal.signal(signal.sighup, sigtest)  while true:     signal.pause() 

does work on system.


Comments