Esempio n. 1
0
	def __init__(self, mem_sk):
		self._iter = 0
		self.verb = def_verb
		css = socket.socketpair(socket.AF_UNIX, socket.SOCK_SEQPACKET)
		util.set_cloexec(css[1])
		print "`- Passing (ctl:%d, data:%d) pair to CRIU" % (css[0].fileno(), mem_sk.fileno())
		self._swrk = subprocess.Popen([criu_binary, "swrk", "%d" % css[0].fileno()])
		css[0].close()
		self._cs = css[1]
		self._last_req = -1
		self._mem_fd = mem_sk.fileno()
Esempio n. 2
0
 def __init__(self, mem_sk):
     self._iter = 0
     self.verb = def_verb
     css = socket.socketpair(socket.AF_UNIX, socket.SOCK_SEQPACKET)
     util.set_cloexec(css[1])
     print "`- Passing (ctl:%d, data:%d) pair to CRIU" % (css[0].fileno(),
                                                          mem_sk.fileno())
     self._swrk = subprocess.Popen(
         [criu_binary, "swrk", "%d" % css[0].fileno()])
     css[0].close()
     self._cs = css[1]
     self._last_req = -1
     self._mem_fd = mem_sk.fileno()
Esempio n. 3
0
	def __init__(self, mem_sk):
		self._iter = 0
		self.verb = def_verb
		self._track_mem = True
		self._shell_job = False
		css = socket.socketpair(socket.AF_UNIX, socket.SOCK_SEQPACKET)
		util.set_cloexec(css[1])
		logging.info("Passing (ctl:%d, data:%d) pair to CRIU", css[0].fileno(), mem_sk.fileno())
		self._swrk = subprocess.Popen([criu_binary, "swrk", "%d" % css[0].fileno()])
		css[0].close()
		self._cs = css[1]
		self._last_req = -1
		self._mem_fd = mem_sk.fileno()
Esempio n. 4
0
def establish(fdrpc, fdmem, fdfs):
	"""Construct required socket objects from file descriptors

	Expect that each file descriptor represent socket opened in blocking mode
	with domain AF_INET and type SOCK_STREAM.
	"""

	logging.info("Use existing connections, fdrpc=%d fdmem=%d fdfs=%d", fdrpc,
		fdmem, fdfs)

	rpc_sk = socket.fromfd(fdrpc, socket.AF_INET, socket.SOCK_STREAM)
	mem_sk = socket.fromfd(fdmem, socket.AF_INET, socket.SOCK_STREAM)
	fs_sk = socket.fromfd(fdfs, socket.AF_INET, socket.SOCK_STREAM)

	util.set_cloexec(rpc_sk)

	return connection(rpc_sk, mem_sk, fs_sk)
Esempio n. 5
0
def establish(fdrpc, fdmem, fdfs):
    """Construct required socket objects from file descriptors

	Expect that each file descriptor represent socket opened in blocking mode
	with domain AF_INET and type SOCK_STREAM.
	"""

    logging.info("Use existing connections, fdrpc=%d fdmem=%d fdfs=%d", fdrpc,
                 fdmem, fdfs)

    rpc_sk = socket.fromfd(fdrpc, socket.AF_INET, socket.SOCK_STREAM)
    mem_sk = socket.fromfd(fdmem, socket.AF_INET, socket.SOCK_STREAM)
    fs_sk = socket.fromfd(fdfs, socket.AF_INET, socket.SOCK_STREAM)

    util.set_cloexec(rpc_sk)

    return connection(rpc_sk, mem_sk, fs_sk)
Esempio n. 6
0
	def __init__(self, path):
		self._dirname = path
		self._dirfd = os.open(path, os.O_DIRECTORY)
		util.set_cloexec(self)
Esempio n. 7
0
	def __init__(self, conn, *args):
		self._srv = conn
		self._rpc_sk = self._make_sk()
		util.set_cloexec(self._rpc_sk)
		_rpc_proxy_caller(self._rpc_sk, RPC_CMD, "init_rpc")(args)
Esempio n. 8
0
	def __init__(self, host):
		sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		sk.bind(host)
		sk.listen(8)
		self._sk = sk
		util.set_cloexec(self)
Esempio n. 9
0
	def init_rpc(self, mgr, args):
		util.set_cloexec(self)
		self._master = mgr.make_master()
		self._master.on_connect(*args)
Esempio n. 10
0
def main():
    a = argparse.ArgumentParser(description='Rasberry Pi LED flashing thingy')
    a.add_argument('--pipe',
                   '-p',
                   help='Pi-blaster pipe',
                   default='/dev/pi-blaster')
    args = a.parse_args()

    state = {}
    state['pipe'] = open(args.pipe, 'w')
    state['c_red'] = {
        'anim': anims.sine,
        'speed': 1,
        'delay': 0,
        'brightness': 1,
        'channel': 2
    }
    state['c_green'] = {
        'anim': anims.sine,
        'speed': 1,
        'delay': 1 / 3,
        'brightness': 1,
        'channel': 5
    }
    state['c_blue'] = {
        'anim': anims.sine,
        'speed': 1,
        'delay': 2 / 3,
        'brightness': 1,
        'channel': 6
    }

    # Maps file descriptors to netstate instances
    connections = {}

    spec = ctimerfd.itimerspec()
    spec.it_interval.tv_sec = 0
    spec.it_interval.tv_nsec = long(1e9 / 24)
    spec.it_value.tv_sec = 0
    spec.it_value.tv_nsec = 1
    t = ctimerfd.timerfd_create(ctimerfd.CLOCK_MONOTONIC,
                                ctimerfd.TFD_CLOEXEC | ctimerfd.TFD_NONBLOCK)
    ctimerfd.timerfd_settime(t, 0, ctypes.pointer(spec), None)

    s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
    s.bind(('', 12345))
    s.listen(5)

    epoll = select.epoll()
    util.set_cloexec(
        epoll.fileno()
    )  # XXX As of Python 2.3, flags=EPOLL_CLOEXEC can be used when creating the epoll instead
    epoll.register(t, select.POLLIN)
    epoll.register(s, select.POLLIN)

    while True:
        for fd, event in eintr_wrap(epoll.poll):
            # timer
            if fd == t and event & select.POLLIN:
                os.read(t, 8)
                wrap(on_timer, state)
            # listening socket
            elif fd == s.fileno() and event & select.POLLIN:
                conn, addr = eintr_wrap(s.accept)
                print 'Connection accepted from [{}]:{} (fd={})'.format(
                    addr[0], addr[1], conn.fileno())
                conn.setblocking(False)
                epoll.register(conn.fileno(), select.POLLIN)
                connections[conn.fileno()] = netstate(conn, bytearray())
            # connection socket
            elif fd in connections and event & select.POLLIN:
                ns = connections[fd]
                while True:
                    x = eagain_wrap(eintr_wrap, ns.socket.recv, 4096)
                    if x is None:
                        break
                    if len(x) == 0:
                        print 'Connection closed (fd={})'.format(
                            ns.socket.fileno())
                        del connections[ns.socket.fileno()]
                        epoll.unregister(ns.socket.fileno())
                        ns.socket.close()
                        break
                    ns.buf.extend(x)
                while True:
                    try:
                        i = ns.buf.index('\r\n')
                    except ValueError:
                        break
                    args = [bytes(x) for x in ns.buf[:i].split()]
                    del ns.buf[:i + 2]

                    fn = commands.commands.get(args[0], commands.unknown)
                    try:
                        fn(ns.socket, args[1:], state)
                    except Exception as e:
                        eintr_wrap(ns.socket.send, b'500 {}\r\n'.format(e))
                        traceback.print_exc()
            # anything else is unexpected
            else:
                msg = 'Bad event (fd={} event={})'.format(fd, event)
                raise Exception(msg)
Esempio n. 11
0
def main ():
        a = argparse.ArgumentParser (description='Rasberry Pi LED flashing thingy')
        a.add_argument ('--pipe', '-p', help='Pi-blaster pipe', default='/dev/pi-blaster')
        args = a.parse_args ()

        state = {}
        state['pipe'] = open (args.pipe, 'w')
        state['c_red'] =   {'anim': anims.sine, 'speed': 1, 'delay': 0,   'brightness': 1, 'channel': 2}
        state['c_green'] = {'anim': anims.sine, 'speed': 1, 'delay': 1/3, 'brightness': 1, 'channel': 5}
        state['c_blue'] =  {'anim': anims.sine, 'speed': 1, 'delay': 2/3, 'brightness': 1, 'channel': 6}

        # Maps file descriptors to netstate instances
        connections = {}

        spec = ctimerfd.itimerspec ()
        spec.it_interval.tv_sec = 0
        spec.it_interval.tv_nsec = long (1e9/24)
        spec.it_value.tv_sec = 0
        spec.it_value.tv_nsec = 1
        t = ctimerfd.timerfd_create (ctimerfd.CLOCK_MONOTONIC, ctimerfd.TFD_CLOEXEC|ctimerfd.TFD_NONBLOCK)
        ctimerfd.timerfd_settime (t, 0, ctypes.pointer (spec), None)

        s = socket.socket (socket.AF_INET6, socket.SOCK_STREAM)
        s.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.setsockopt (socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
        s.bind (('', 12345))
        s.listen (5)

        epoll = select.epoll ()
        util.set_cloexec (epoll.fileno ()) # XXX As of Python 2.3, flags=EPOLL_CLOEXEC can be used when creating the epoll instead
        epoll.register (t, select.POLLIN)
        epoll.register (s, select.POLLIN)

        while True:
                for fd, event in eintr_wrap (epoll.poll):
                        # timer
                        if fd == t and event & select.POLLIN:
                                os.read (t, 8)
                                wrap (on_timer, state)
                        # listening socket
                        elif fd == s.fileno () and event & select.POLLIN:
                                conn, addr = eintr_wrap (s.accept)
                                print 'Connection accepted from [{}]:{} (fd={})'.format (addr[0], addr[1], conn.fileno ())
                                conn.setblocking (False)
                                epoll.register (conn.fileno (), select.POLLIN)
                                connections [conn.fileno ()] = netstate (conn, bytearray ())
                        # connection socket
                        elif fd in connections and event & select.POLLIN:
                                ns = connections[fd]
                                while True:
                                        x = eagain_wrap (eintr_wrap, ns.socket.recv, 4096)
                                        if x is None:
                                                break
                                        if len (x) == 0:
                                                print 'Connection closed (fd={})'.format (ns.socket.fileno ())
                                                del connections [ns.socket.fileno ()]
                                                epoll.unregister (ns.socket.fileno ())
                                                ns.socket.close ()
                                                break
                                        ns.buf.extend (x)
                                while True:
                                        try:
                                                i = ns.buf.index ('\r\n')
                                        except ValueError:
                                                break
                                        args = [bytes (x) for x in ns.buf[:i].split ()]
                                        del ns.buf[:i+2]

                                        fn = commands.commands.get (args[0], commands.unknown)
                                        try:
                                                fn (ns.socket, args[1:], state)
                                        except Exception as e:
                                                eintr_wrap (ns.socket.send, b'500 {}\r\n'.format (e))
                                                traceback.print_exc ()
                        # anything else is unexpected
                        else:
                                msg = 'Bad event (fd={} event={})'.format (fd, event)
                                raise Exception (msg)