Exemple #1
0
    def on_close(self):
        if self.fd is not None:
            self.log.info('Closing fd %d' % self.fd)

        if getattr(self, 'pid', 0) == 0:
            self.log.info('pid is 0')
            return

        try:
            ioloop.remove_handler(self.fd)
        except Exception:
            self.log.error('handler removal fail', exc_info=True)

        try:
            os.close(self.fd)
        except Exception:
            self.log.debug('closing fd fail', exc_info=True)

        try:
            os.kill(self.pid, signal.SIGKILL)
            os.waitpid(self.pid, 0)
        except Exception:
            self.log.debug('waitpid fail', exc_info=True)

        TermWebSocket.terminals.remove(self)
        self.log.info('Websocket closed')

        if self.application.systemd and not len(TermWebSocket.terminals):
            self.log.info('No more terminals, exiting...')
            sys.exit(0)
Exemple #2
0
 def recv(*args):
     data = pipe.stdout.readline()
     if data:
         result.append(data)
     elif pipe.poll() is not None:
         ioloop.remove_handler(fd)
         callback(''.join(result))
Exemple #3
0
 def recv(*args):
     data = pipe.stdout.readline()
     if data:
         result.append(data)
     elif pipe.poll() is not None:
         ioloop.remove_handler(fd)
         callback(''.join(result))
Exemple #4
0
    def on_close(self):
        print "term close", self.uid
        if self.closed:
            return
        self.closed = True

        if getattr(self, 'pid', 0) == 0:
            return

        utils.rm_user_info(self.uid, self.pid)

        try:
            ioloop.remove_handler(self.fd)
        except Exception:
            pass

        try:
            os.close(self.fd)
        except Exception:
            pass
        try:
            os.kill(self.pid, signal.SIGKILL)
            os.waitpid(self.pid, 0)
        except Exception:
            pass
        Terminal.terminals.remove(self)
Exemple #5
0
    def on_close(self):
        if getattr(self, 'pid', 0) == 0:
            self.log.info('pid is 0')
            return
        try:
            self.writer.write(u('\x04'))
            self.writer.flush()
        except Exception:
            self.log.debug('closing term fail', exc_info=True)

        try:
            os.close(self.fd)
        except Exception:
            self.log.debug('closing fd fail', exc_info=True)

        try:
            os.waitpid(self.pid, 0)
        except Exception:
            self.log.debug('waitpid fail', exc_info=True)

        try:
            ioloop.remove_handler(self.fd)
        except Exception:
            self.log.debug('handler removal fail', exc_info=True)

        self.log.info('Websocket closed')
Exemple #6
0
    def on_close(self):
        print "term close", self.uid
        if self.closed:
            return
        self.closed = True

        if getattr(self, 'pid', 0) == 0:
            return

        utils.rm_user_info(self.uid, self.pid)

        try:
            ioloop.remove_handler(self.fd)
        except Exception:
            pass

        try:
            os.close(self.fd)
        except Exception:
            pass
        try:
            os.kill(self.pid, signal.SIGKILL)
            os.waitpid(self.pid, 0)
        except Exception:
            pass
        Terminal.terminals.remove(self)
    def stop_ioloop(self):
        """
        Method to stop ioloop for paho-mqtt mosquitto clients so that it cannot 
        process any more read/write events for the sockets.

        Actually the paho-mqtt mosquitto socket has been closed, so bascially this
        method removed the tornaod ioloop handler for this socket.

        """ 

        # pi('stop_ioloop')

        self._sock = self._client.socket()
        
        # # removing tornado iooloop handler
        # print '[MosquittoClient] removing tornado handler now'

        if self._sock: 

            # print 'self._sock is present, hence removing handler'

            ioloop.remove_handler(self._sock.fileno()) 

            # updating close state of ioloop 
            self._ioloopClosed = True


        else: 
            LOGGER.warning('[MosquittoClient] client socket is closed already')
Exemple #8
0
    def stop_ioloop(self):
        """
        Method to stop ioloop for paho-mqtt mosquitto clients so that it cannot 
        process any more read/write events for the sockets.

        Actually the paho-mqtt mosquitto socket has been closed, so bascially this
        method removed the tornaod ioloop handler for this socket.

        """

        # pi('stop_ioloop')

        self._sock = self._client.socket()

        # # removing tornado iooloop handler
        # print '[MosquittoClient] removing tornado handler now'

        if self._sock:

            # print 'self._sock is present, hence removing handler'

            ioloop.remove_handler(self._sock.fileno())

            # updating close state of ioloop
            self._ioloopClosed = True

        else:
            LOGGER.warning('[MosquittoClient] client socket is closed already')
Exemple #9
0
    def on_close(self):
        if getattr(self, "pid", 0) == 0:
            self.log.info("pid is 0")
            return
        try:
            self.writer.write(u("\x04"))
            self.writer.flush()
        except Exception:
            self.log.debug("closing term fail", exc_info=True)

        try:
            os.close(self.fd)
        except Exception:
            self.log.debug("closing fd fail", exc_info=True)

        try:
            os.waitpid(self.pid, 0)
        except Exception:
            self.log.debug("waitpid fail", exc_info=True)

        try:
            ioloop.remove_handler(self.fd)
        except Exception:
            self.log.debug("handler removal fail", exc_info=True)

        self.log.info("Websocket closed")
def handle_client(cli_addr, fd, event):
    print event, IOLoop.WRITE
    s = fd_map[fd]
    if event & IOLoop.READ:  # receive the data
        data = s.recv(1024)
        if data:
            print "receive %s from %s" % (data, cli_addr)
            ioloop.update_handler(fd, IOLoop.WRITE)
            message_queue_map[s].put(data)
        else:
            print "closing %s" % cli_addr
            ioloop.remove_handler(fd)
            s.close()
            del message_queue_map[s]
    if event & IOLoop.WRITE:
        try:
            next_msg = message_queue_map[s].get_nowait()
        except Queue.Empty:
            print "%s Queue Empty" % cli_addr
            ioloop.update_handler(fd, IOLoop.READ)  # CHANGE THE SITUATION
        else:
            print "sending %s to %s" % (next_msg, cli_addr)
            s.send(next_msg)
            ioloop.update_handler(fd, IOLoop.READ)
    if event & IOLoop.ERROR:
        print "%s EXCEPTION ON" % cli_addr
        ioloop.remove_handler(fd)
        s.close()
        del message_queue_map[s]
Exemple #11
0
    def on_close(self):
        if self.fd is not None:
            self.log.info('Closing fd %d' % self.fd)

        if getattr(self, 'pid', 0) == 0:
            self.log.info('pid is 0')
            return

        try:
            ioloop.remove_handler(self.fd)
        except Exception:
            self.log.error('handler removal fail', exc_info=True)

        try:
            os.close(self.fd)
        except Exception:
            self.log.debug('closing fd fail', exc_info=True)

        try:
            os.kill(self.pid, signal.SIGKILL)
            os.waitpid(self.pid, 0)
        except Exception:
            self.log.debug('waitpid fail', exc_info=True)

        TermWebSocket.terminals.remove(self)
        self.log.info('Websocket closed')

        if self.application.systemd and not len(TermWebSocket.terminals):
            self.log.info('No more terminals, exiting...')
            sys.exit(0)
Exemple #12
0
    def close(self):
        if self.closed:
            return
        self.closed = True
        if self.fd is not None:
            log.info('Closing fd %d' % self.fd)

        if getattr(self, 'pid', 0) == 0:
            log.info('pid is 0')
            return

        utils.rm_user_info(self.uid, self.pid)

        try:
            ioloop.remove_handler(self.fd)
        except Exception:
            log.error('handler removal fail', exc_info=True)

        try:
            os.close(self.fd)
        except Exception:
            log.debug('closing fd fail', exc_info=True)

        try:
            os.kill(self.pid, signal.SIGHUP)
            os.kill(self.pid, signal.SIGCONT)
            os.waitpid(self.pid, 0)
        except Exception:
            log.debug('waitpid fail', exc_info=True)

        del self.sessions[self.session]
Exemple #13
0
    def close(self):
        if self.closed:
            return
        self.closed = True
        if self.fd is not None:
            log.info('Closing fd %d' % self.fd)

        if getattr(self, 'pid', 0) == 0:
            log.info('pid is 0')
            return

        utils.rm_user_info(self.uid, self.pid)

        try:
            ioloop.remove_handler(self.fd)
        except Exception:
            log.error('handler removal fail', exc_info=True)

        try:
            os.close(self.fd)
        except Exception:
            log.debug('closing fd fail', exc_info=True)

        try:
            os.kill(self.pid, signal.SIGHUP)
            os.kill(self.pid, signal.SIGCONT)
            os.waitpid(self.pid, 0)
        except Exception:
            log.debug('waitpid fail', exc_info=True)

        del self.sessions[self.session]
Exemple #14
0
 def read(*args):
     data = pipe.stdout.readline()
     if data:
         callback(data)
     elif pipe.poll() is not None:
         ioloop.remove_handler(fd)
         callback(None)
    def _on_stderr(self, fd, events):
        """Monitor engine's ``stderr``. """
        ioloop = tornado.ioloop.IOLoop.instance()

        if events & ioloop.ERROR:
            ioloop.remove_handler(fd)
        else:
            self._read_stderr()
Exemple #16
0
 def _stop_handler(self):
     self.process_count -= 1
     if hasattr(self, 'timer'):
         if self.timer != None:
             self.timer.stop()
             self.timer = None
     elif hasattr(self, 'fd'):
         if self.fd != None:
             ioloop = tornado.ioloop.IOLoop.instance()
             ioloop.remove_handler(self.fd)
             self.fd = None
Exemple #17
0
  def __on_complete(self, callback, socket_file, *args):
    logging.debug('ForgeD __run_process_as_user events: %s', str(args))
    ioloop = tornado.ioloop.IOLoop.instance()
    ioloop.remove_handler(self.process_sox.fileno())
    socket_file = self.process_sox.getsockname()
    self.process_sox.close()
    os.remove(socket_file)

    assert self.tornado_conn.poll()
    result = self.tornado_conn.recv()
    self.tornado_conn.close()
    callback(result)
Exemple #18
0
 def _on_complete(self, callback, *args):
     """Callback-In-The-Middle to do some cleanup before calling the
     actual callback.
     """
     logging.debug('FD Events: %s', str(args))
     # task completed, remove handler & socket
     ioloop = tornado.ioloop.IOLoop().instance()
     ioloop.remove_handler(self.celery_socket.fileno())
     fname = self.celery_socket.getsockname()
     self.celery_socket.close()
     os.remove(fname)
     # sanity check
     assert self.celery_result.ready()
     # run callback
     callback(self.celery_result.result)
Exemple #19
0
 def _on_complete(self, callback, *args):
     """Callback-In-The-Middle to do some cleanup before calling the
     actual callback.
     """
     logging.debug('FD Events: %s', str(args))
     # task completed, remove handler & socket
     ioloop = tornado.ioloop.IOLoop().instance()
     ioloop.remove_handler(self.celery_socket.fileno())
     fname = self.celery_socket.getsockname()
     self.celery_socket.close()
     os.remove(fname)
     # sanity check
     assert self.celery_result.ready()
     # run callback
     callback(self.celery_result.result)
Exemple #20
0
    def rm_user(self, user, ws_handler):    
        if (user, ws_handler) in self.clients:
            self.clients.remove((user, ws_handler))

        # If no one is attached just kill the julia....            
        if self.clients == []:
            global ioloop
            ioloop.remove_handler(self.h.stdout.fileno())
            
            self.h.kill()
            # TODO : Collect returncode to avoid zombies.....
            
            # remove from global map.
            global g_sessions
            del g_sessions[self.session]
Exemple #21
0
 def _on_complete(self, callback, *args):
     """Callback-In-The-Middle to do some cleanup before calling the
     actual callback.
     """
     logging.debug('FD Events: %s', str(args))
     # task completed, remove handler & socket, get result from pipe
     ioloop = tornado.ioloop.IOLoop().instance()
     ioloop.remove_handler(self.process_socket.fileno())
     fname = self.process_socket.getsockname()
     self.process_socket.close()
     os.remove(fname)
     # sanity check
     assert self.tor_conn.poll()
     # run callback
     result = self.tor_conn.recv()
     self.tor_conn.close()
     callback(result)
Exemple #22
0
 def _on_complete(self, callback, *args):
     """Callback-In-The-Middle to do some cleanup before calling the
     actual callback.
     """
     logging.debug('FD Events: %s', str(args))
     # task completed, remove handler & socket, get result from pipe
     ioloop = tornado.ioloop.IOLoop().instance()
     ioloop.remove_handler(self.process_socket.fileno())
     fname = self.process_socket.getsockname()
     self.process_socket.close()
     os.remove(fname)
     # sanity check
     assert self.tor_conn.poll()
     # run callback
     result = self.tor_conn.recv()
     self.tor_conn.close()
     callback(result)
Exemple #23
0
    def shell_handler(self, fd, events):
        if events & ioloop.READ:
            try:
                read = self.reader.read()
            except IOError:
                self.log.info("READ>%r" % read)
                self.write_message("DIE")
                return

            self.log.info("READ>%r" % read)
            self.write_message(read.decode("utf-8", "replace"))

        if events & ioloop.ERROR:
            self.log.info("Closing due to ioloop fd handler error")
            ioloop.remove_handler(self.fd)

            # Terminated
            self.on_close()
            self.close()
Exemple #24
0
    def shell_handler(self, fd, events):
        if events & ioloop.READ:
            try:
                read = self.reader.read()
            except IOError:
                self.log.info('READ>%r' % read)
                self.write_message('DIE')
                return

            self.log.info('READ>%r' % read)
            self.write_message(read.decode('utf-8', 'replace'))

        if events & ioloop.ERROR:
            self.log.info('Closing due to ioloop fd handler error')
            ioloop.remove_handler(self.fd)

            # Terminated
            self.on_close()
            self.close()
Exemple #25
0
    def shell_handler(self, fd, events):
        if events & ioloop.READ:
            try:
                read = self.reader.read()
            except IOError:
                self.log.info('READ>%r' % read)
                self.write_message('DIE')
                return

            self.log.info('READ>%r' % read)
            self.write_message(read.decode('utf-8', 'replace'))

        if events & ioloop.ERROR:
            self.log.info('Closing due to ioloop fd handler error')
            ioloop.remove_handler(self.fd)

            # Terminated
            self.on_close()
            self.close()
Exemple #26
0
    def stop_ioloop(self):
        """
        Method to stop ioloop for paho-mqtt mosquitto clients so that it cannot
        process any more read/write events for the sockets.

        Actually the paho-mqtt mosquitto socket has been closed, so bascially this
        method removed the tornaod ioloop handler for this socket.

        """

        self._sock = self._client.socket()

        # removing tornado iooloop handler

        if self._sock:
            ioloop.remove_handler(self._sock.fileno())
            # updating close state of ioloop
            self._ioloopclosed = True
        else:
            LOGGER.warning('client socket is closed already')
Exemple #27
0
    def on_close(self):
        if getattr(self, "pid", 0) == 0:
            self.log.info("pid is 0")
            return

        try:
            os.close(self.fd)
        except Exception:
            self.log.debug("closing fd fail", exc_info=True)

        try:
            os.kill(self.pid, signal.SIGKILL)
            os.waitpid(self.pid, 0)
        except Exception:
            self.log.debug("waitpid fail", exc_info=True)

        try:
            ioloop.remove_handler(self.fd)
        except Exception:
            self.log.debug("handler removal fail", exc_info=True)

        self.log.info("Websocket closed")
Exemple #28
0
    def on_close(self):
        if getattr(self, 'pid', 0) == 0:
            self.log.info('pid is 0')
            return

        try:
            os.close(self.fd)
        except Exception:
            self.log.debug('closing fd fail', exc_info=True)

        try:
            os.kill(self.pid, signal.SIGKILL)
            os.waitpid(self.pid, 0)
        except Exception:
            self.log.debug('waitpid fail', exc_info=True)

        try:
            ioloop.remove_handler(self.fd)
        except Exception:
            self.log.debug('handler removal fail', exc_info=True)

        self.log.info('Websocket closed')
Exemple #29
0
 def end_untar_pkgs(fileno, event):
     if proc.poll() is None:
         return
     ioloop.remove_handler(fileno)
     os.remove(filename)
     callback(install_all_bundles())
Exemple #30
0
 def end_untar_pkgs(fileno, event):
     if proc.poll() is None:
         return
     ioloop.remove_handler(fileno)
     os.remove(filename)
     callback(install_all_bundles())
Exemple #31
0
def input_remove(fd):
  ioloop.remove_handler(fd)
Exemple #32
0
def _recv_dns_msg(callback, ioloop, sock, fd, events):
    ret = Record.unpack(sock.recvfrom(1024)[0])
    ioloop.remove_handler(fd)
    callback(ret)
Exemple #33
0
       def wait_for_accept(fd,events): # TODO more intelligently check for events?
            try:
                result = sock.accept()
                callback(result)
            except socket.error, e:
                if e.args[0] not in (errno.EWOULDBLOCK, errno.EAGAIN):
                    raise

       ioloop.add_handler(sock.fileno(), wait_for_accept, ioloop.READ)

    sock.setblocking(0)
    try:
        result = yield wrap_add_handler()
        raise Return(result)
    finally:
        ioloop.remove_handler(sock.fileno())

@coroutine
def wait(proc):
    poll_period=datetime.timedelta(milliseconds=10)
    while True:
        p=proc.poll()
        if p is not None:
            raise Return(p)

        @return_future
        def pause(callback):
            ioloop.add_timeout(poll_period,callback)

        yield pause()
Exemple #34
0
def _recv_dns_msg(callback, ioloop, sock, fd, events):
  ret = Record.unpack(sock.recvfrom(1024)[0])
  ioloop.remove_handler(fd)
  callback(ret)