def kill( pid, signal = 15, timeout = 0 ): '''kills the process specified by pid that may be a process id or a Child object. The process is killed with the provided signal (by default 15). If the process is not dead after <timeout> seconds the function exist anyway''' # a dispatcher function required to activate the minimal timeout def fake_dispatcher(): return True notifier.dispatcher_add( fake_dispatcher ) if isinstance( pid, Child ): if pid.pid: pid = pid.pid else: return pid.exitcode os.kill( pid, signal ) countdown = CountDown( timeout ) while countdown(): dead_pid, sts = os.waitpid( pid, os.WNOHANG ) if dead_pid == pid: break notifier.step() else: # remove dispatcher function notifier.dispatcher_remove( fake_dispatcher ) return None # remove dispatcher function notifier.dispatcher_remove( fake_dispatcher ) if os.WIFSIGNALED( sts ): return -os.WTERMSIG( sts ) return os.WEXITSTATUS( sts )
def __init__( self, port = 6670, ssl = True, unix = None, magic = True, magicClass = MagicBucket, load_ressources = True ): '''Initializes the socket to listen for requests''' signals.Provider.__init__( self ) # loading resources if load_ressources: CORE.info( 'Loading resources ...' ) self.reload() # register dispatch function to reload UCR Variables on change notifier.dispatcher_add(self._get_ucr_inotify_callback()) CORE.info( 'Initialising server process' ) self.__port = port self.__unix = unix self.__ssl = ssl if self.__unix: CORE.info( 'Using a UNIX socket' ) self.__realsocket = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM ) else: CORE.info( 'Using a TCP socket' ) try: self.__realsocket = socket.socket( socket.AF_INET6, socket.SOCK_STREAM ) except: CORE.warn( 'Cannot open socket with AF_INET6 (Python reports socket.has_ipv6 is %s), trying AF_INET' % socket.has_ipv6 ) self.__realsocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) self.__realsocket.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) self.__realsocket.setblocking( 0 ) fcntl.fcntl(self.__realsocket.fileno(), fcntl.F_SETFD, 1) if self.__ssl and not self.__unix: CORE.info( 'Setting up SSL configuration' ) self.crypto_context = SSL.Context( SSL.SSLv23_METHOD ) self.crypto_context.set_cipher_list('DEFAULT') self.crypto_context.set_options( SSL.OP_NO_SSLv2 ) self.crypto_context.set_verify( SSL.VERIFY_PEER, self.__verify_cert_cb ) dir = '/etc/univention/ssl/%s.%s' % ( ucr[ 'hostname' ], ucr[ 'domainname' ] ) try: self.crypto_context.use_privatekey_file( os.path.join( dir, 'private.key' ) ) self.crypto_context.use_certificate_file( os.path.join( dir, 'cert.pem' ) ) self.crypto_context.load_verify_locations( os.path.join( dir, '/etc/univention/ssl/ucsCA', 'CAcert.pem' ) ) except SSL.Error, e: # SSL is not possible CRYPT.process( 'Setting up SSL configuration failed: %s' % str( e ) ) CRYPT.warn( 'Communication will not be encrypted!' ) self.__ssl = False self.crypto_context = None self.__realsocket.bind( ( '', self.__port ) ) CRYPT.info( 'Server listening to unencrypted connections' ) self.__realsocket.listen( SERVER_MAX_CONNECTIONS ) if self.crypto_context: self.connection = SSL.Connection( self.crypto_context , self.__realsocket ) self.connection.setblocking(0) self.connection.bind( ( '', self.__port ) ) self.connection.set_accept_state() CRYPT.info( 'Server listening to SSL connections' ) self.connection.listen( SERVER_MAX_CONNECTIONS )
def __init__( self, name, function, callback ): self._name = name self._function = function self._callback = callback self._result = None self._trace = None self._exc_info = None self._finished = False self._id = None self._lock = thread.allocate_lock() global _threads if not _threads: notifier.dispatcher_add( _simple_threads_dispatcher ) _threads.append( self )
def __init__( self, name, function, callback ): self._name = name self._function = function self._callback = callback self._result = None self._trace = None self._exc_info = None self._finished = False self._thread = threading.Thread(target=self._run, name=self._name) self._lock = threading.Lock() global _threads if not _threads: notifier.dispatcher_add( _simple_threads_dispatcher ) _threads.append( self )
def __init__(self, cmd, stdout=True, stderr=True, shell=False): """ Init the child process 'cmd'. This can either be a string or a list of arguments (similar to popen2). stdout and stderr of the child process can be handled by connecting to the signals 'stdout' and/or 'stderr'. The signal functions have one argument that is of type list and contains all new lines. By setting one of the boolean arguments 'stdout' and 'stderr' to False the monitoring of these files can be deactivated. Signals: 'stderr' ( pid, line ) emitted when the IO_Handler reported another line of input (from stdout). pid : PID of the process that produced the output line : line of output 'stdout' ( pid, line ) emitted when the IO_Handler reported another line of input (from stderr). pid : PID of the process that produced the output line : line of output """ signals.Provider.__init__(self) if stderr: self.signal_new('stderr') if stdout: self.signal_new('stdout') self.signal_new('killed') if not shell and not isinstance(cmd, (list, tuple)): self._cmd = shlex.split( str(cmd)) # shlex.split can not handle Unicode strings else: self._cmd = cmd self._shell = shell if not shell and self._cmd: self._name = self._cmd[0].split('/')[-1] else: self._name = '<unknown>' self.stopping = False self.pid = None self.child = None self.__dead = True self.__kill_timer = None global _processes if not _processes: notifier.dispatcher_add(_watcher) _processes.append(self)
def __init__( self, cmd, stdout = True, stderr = True, shell = False ): """ Init the child process 'cmd'. This can either be a string or a list of arguments (similar to popen2). stdout and stderr of the child process can be handled by connecting to the signals 'stdout' and/or 'stderr'. The signal functions have one argument that is of type list and contains all new lines. By setting one of the boolean arguments 'stdout' and 'stderr' to False the monitoring of these files can be deactivated. Signals: 'stderr' ( pid, line ) emitted when the IO_Handler reported another line of input (from stdout). pid : PID of the process that produced the output line : line of output 'stdout' ( pid, line ) emitted when the IO_Handler reported another line of input (from stderr). pid : PID of the process that produced the output line : line of output """ signals.Provider.__init__( self ) if stderr: self.signal_new( 'stderr' ); if stdout: self.signal_new( 'stdout' ); self.signal_new( 'killed' ); if not shell and not isinstance( cmd, ( list, tuple ) ): self._cmd = shlex.split( str( cmd ) ) # shlex.split can not handle Unicode strings else: self._cmd = cmd self._shell = shell if not shell and self._cmd: self._name = self._cmd[ 0 ].split( '/' )[ -1 ] else: self._name = '<unknown>' self.stopping = False self.pid = None self.child = None self.__dead = True self.__kill_timer = None global _processes if not _processes: notifier.dispatcher_add( _watcher ) _processes.append( self )
def run(command, timeout=0, stdout=True, stderr=True, shell=True): '''Runs a child process with the <command> and waits <timeout> seconds for its termination. If <stdout> is True the standard output is written to a temporary file. The same can be done for the standard error output with the argument <stderr>. If <shell> is True the command is passed to a shell. The return value is a Child object. The member variable <pid> is set if the process is still running after <timeout> seconds otherwise <exitcode> is set.''' # a dispatcher function required to activate the minimal timeout def fake_dispatcher(): return True notifier.dispatcher_add(fake_dispatcher) countdown = CountDown(timeout) out = err = None if stdout: out = tempfile.NamedTemporaryFile() if stderr: err = tempfile.NamedTemporaryFile() if isinstance(command, basestring): command = shlex.split(command) child = subprocess.Popen(command, shell=shell, stdout=out, stderr=err) while countdown(): exitcode = child.poll() if exitcode != None: break notifier.step() # remove dispatcher function notifier.dispatcher_remove(fake_dispatcher) # prepare return code ret = Child(stdout=out, stderr=err) if child.returncode == None: ret.pid = child.pid else: # move to beginning of files if out: out.seek(0) if err: err.seek(0) ret.exitcode = child.returncode return ret
def run( command, timeout = 0, stdout = True, stderr = True, shell = True ): '''Runs a child process with the <command> and waits <timeout> seconds for its termination. If <stdout> is True the standard output is written to a temporary file. The same can be done for the standard error output with the argument <stderr>. If <shell> is True the command is passed to a shell. The return value is a Child object. The member variable <pid> is set if the process is still running after <timeout> seconds otherwise <exitcode> is set.''' # a dispatcher function required to activate the minimal timeout def fake_dispatcher(): return True notifier.dispatcher_add( fake_dispatcher ) countdown = CountDown( timeout ) out = err = None if stdout: out = tempfile.NamedTemporaryFile() if stderr: err = tempfile.NamedTemporaryFile() if isinstance( command, basestring ): command = shlex.split( command ) child = subprocess.Popen( command, shell = shell, stdout = out, stderr = err ) while countdown(): exitcode = child.poll() if exitcode != None: break notifier.step() # remove dispatcher function notifier.dispatcher_remove( fake_dispatcher ) # prepare return code ret = Child( stdout = out, stderr = err ) if child.returncode == None: ret.pid = child.pid else: # move to beginning of files if out: out.seek( 0 ) if err: err.seek( 0 ) ret.exitcode = child.returncode return ret
def kill(pid, signal=15, timeout=0): '''kills the process specified by pid that may be a process id or a Child object. The process is killed with the provided signal (by default 15). If the process is not dead after <timeout> seconds the function exist anyway''' # a dispatcher function required to activate the minimal timeout def fake_dispatcher(): return True notifier.dispatcher_add(fake_dispatcher) if isinstance(pid, Child): if pid.pid: pid = pid.pid else: return pid.exitcode os.kill(pid, signal) countdown = CountDown(timeout) while countdown(): dead_pid, sts = os.waitpid(pid, os.WNOHANG) if dead_pid == pid: break notifier.step() else: # remove dispatcher function notifier.dispatcher_remove(fake_dispatcher) return None # remove dispatcher function notifier.dispatcher_remove(fake_dispatcher) if os.WIFSIGNALED(sts): return -os.WTERMSIG(sts) return os.WEXITSTATUS(sts)
import os, sys #import twisted import notifier notifier.init( notifier.TWISTED ) def hello( *args ): print 'Hello World' # notifier-timer testfunction def timer_test(): print "timer_test" # notifier.dispatcher_add( notifier.Callback( dispatcher_test, 1, 2, 3 ) ) return True def dispatcher_test( a, b, c ): print 'dispatcher', a, b, c return True def _stdin( fd ): print 'read: ' + os.read( fd, 512 ) notifier.socket_remove( 0 ) return False notifier.socket_add( 0, _stdin ) notifier.timer_add( 4000, notifier.Callback( timer_test ) ) notifier.dispatcher_add( notifier.Callback( dispatcher_test, 1, 2, 3 ) ) notifier.loop()
import time def timeout(data): print 'timeout', time.time() print ' data :', data return True def zero(data): print 'timeout', time.time() print ' data :', data return True def dispatch(data): #print 'dispatch', data return True # when no argument is given to init default is GENERIC notifier.init(notifier.GENERIC, recursive_depth=5) notifier.timer_add(1000, notifier.Callback(timeout, 'hello')) #notifier.timer_add( 0, notifier.Callback( zero, 'hello' ) ) notifier.dispatcher_add(notifier.Callback(dispatch, 'hello')) #notifier.dispatcher_add( notifier.Callback( dispatch, 'hello' ), False ) notifier.loop()
button = gtk.Button('Hello World') button.connect('clicked', hello) window.add(button) button.show() window.show() # notifier-timer testfunction def timer_test(): print "timer_test" # notifier.dispatcher_add( notifier.Callback( dispatcher_test, 1, 2, 3 ) ) return True def dispatcher_test(a, b, c): # print 'dispatcher', a, b, c return True def _stdin(fd): notifier.socket_remove(0) return False notifier.socket_add(0, _stdin) notifier.timer_add(4000, notifier.Callback(timer_test)) notifier.dispatcher_add(notifier.Callback(dispatcher_test, 1, 2, 3)) notifier.loop()
self.setActiveWindow( self.dialog ) self.button = qt.QPushButton( 'Hello World', self.dialog ) self.dialog.show() qt.QObject.connect( self.button, qt.SIGNAL( 'clicked()' ), self.clickedButton ) self.timer_id = notifier.timer_add( 1000, self.timerTest ) self.dispatch_it = 10 def recvQuit( self, mmsg, data = None ): self.quit() def clickedButton( self ): print "bye" self.quit( 1 ) def timerTest( self ): print 'tick' return True def _dispatch( self ): print 'dispatch' self.dispatch_it -= 1 return self.dispatch_it > 0 if __name__ == '__main__': notifier.init( notifier.QT ) app = QTestApp() notifier.dispatcher_add( app._dispatch ) print 'exit code: %d' % notifier.loop()
# 02110-1301 USA import notifier import time def timeout( data ): print 'timeout', time.time() print ' data :', data return True def zero( data ): print 'timeout', time.time() print ' data :', data return True def dispatch( data ): #print 'dispatch', data return True # when no argument is given to init default is GENERIC notifier.init( notifier.GENERIC, recursive_depth = 5 ) notifier.timer_add( 1000, notifier.Callback( timeout, 'hello' ) ) #notifier.timer_add( 0, notifier.Callback( zero, 'hello' ) ) notifier.dispatcher_add( notifier.Callback( dispatch, 'hello' ) ) #notifier.dispatcher_add( notifier.Callback( dispatch, 'hello' ), False ) notifier.loop()