def power_get_do(self, target): pidfile = os.path.join( target.state_dir, "rsync-%s:%d.pid" % (self.address, self.port)) pid = commonl.process_alive(pidfile, self.path) if pid != None: return True return False
def get(self, target, component): # power interface kws = dict(target.kws) kws.update(self.kws) # bring in runtime properties (override the rest) kws.update(target.fsdb.get_as_dict()) kws['component'] = component return commonl.process_alive(self.pidfile % kws, self.path) != None
def power_get_do(self, target): pidfile = os.path.join(target.state_dir, "socat-" + self.tunnel_id + ".pid") pid = commonl.process_alive(pidfile, self.path) if pid != None: return True return False
def verify(self, target, component, cmdline_expanded): kws = dict(target.kws) kws.update(self.kws) # bring in runtime properties (override the rest) kws.update(target.fsdb.get_as_dict()) kws['component'] = component return commonl.process_alive(self.pidfile % kws, self.check_path) != None
def power_get_do(self, target): self._init_for_process(target) dhcpd_pid = commonl.process_alive(self.dhcpd_pidfile, self.dhcpd_path) tftpd_pid = commonl.process_alive(self.tftpd_pidfile, self.tftpd_path) if dhcpd_pid != None and tftpd_pid != None: return True elif dhcpd_pid == None and tftpd_pid == None: return False else: self.log.warning("Inconstent PIDs DHCPD %s TFTP %s: stopping\n", dhcpd_pid, tftpd_pid) if dhcpd_pid: commonl.process_terminate(dhcpd_pid, self.dhcpd_pidfile, "dhcpd[%d]" % dhcpd_pid) if tftpd_pid: commonl.process_terminate(tftpd_pid, self.tftpd_pidfile, "tftpd[%d]" % tftpd_pid) return False
def power_get_do(self, target): if self.target == None: self.target = target else: assert self.target == target self._init_for_process(target) dhcpd_pid = commonl.process_alive(self.dhcpd_pidfile, self.dhcpd_path) if dhcpd_pid != None: return True else: return False
def _delete_tunnel(target, tunnel_id): # tunnel_id is the name of the property that holds the tunnel name port_pid = target.fsdb.get(tunnel_id) if port_pid != None: _, pids = port_pid.split(" ", 2) pid = int(pids) if commonl.process_alive(pid, "/usr/bin/socat"): commonl.process_terminate(pid, tag="socat's tunnel [%s]: " % tunnel_id) target.fsdb.set(tunnel_id, None)
def _delete_tunnel(target, local_port, pid=None): if pid == None: pid = target.fsdb.get("interfaces.tunnel.%s.__id" % local_port) try: if isinstance(pid, int): if commonl.process_alive(pid, "/usr/bin/socat"): commonl.process_terminate(pid, tag="socat's tunnel [%s]: " % local_port) finally: # whatever happens, just wipe all info about it because # this might be a corrupted entry prefix = "interfaces.tunnel.%s" % local_port target.fsdb.set(prefix + ".__id", None) target.fsdb.set(prefix + ".ip_addr", None) target.fsdb.set(prefix + ".protocol", None) target.fsdb.set(prefix + ".port", None)
def power_get_do(self, target): self.pid = None self.pid_s = None # Gather the PID for OpenOCD pid_s = target.fsdb.get("openocd.pid") if pid_s == None: return False try: pid = int(pid_s) except ValueError as e: # Invalid format, wipe target.fsdb.put("openocd.pid", None) return False openocd_path = target.fsdb.get("openocd.path") pid = commonl.process_alive(pid, openocd_path) if pid: self.pid = pid self.pid_s = pid_s return True return False
def power_on_do(self, target): powered = self.power_get_do(target) if powered: return # power_get_do() has filled self.pid # start openocd if not running, try a few times top = 4 for count in range(top): try: self._power_on_do_openocd_start() if self._power_on_do_openocd_verify(): break self.log.error("openocd (%d/%d) not responding", count + 1, top) except self.error as e: # Sometimes openocd fails to open the device with a # -EINTR, so we are going to just retry self.log.error("openocd (%d/%d) init error: %s", count + 1, top, e) if self.pid and commonl.process_alive(self.pid, self.openocd_path): self._power_off_do_kill() time.sleep(0.5) else: raise RuntimeError("openocd failed to start")
def _power_get_bsp(self, bsp): cmdline = self.fsdb.get("qemu-cmdline-%s" % bsp) if cmdline == None: return None r = commonl.process_alive(self.pidfile[bsp], cmdline) return r
def _power_get(self): cmdline = self.fsdb.get("qemu-cmdline") if cmdline == None: return None r = commonl.process_alive(self.pidfile, cmdline) return r
def verify(self, target, component, cmdline_expanded): pidfile = os.path.join(target.state_dir, component + "-jtagd.pid") return commonl.process_alive(pidfile, self.check_path) \ and commonl.tcp_port_busy(self.tcp_port)
def get(self, target, component): # power interface kws = dict(target.kws) kws.update(self.kws) kws['component'] = component return commonl.process_alive(self.pidfile % kws, self.path) != None
def put_tunnel(self, target, who, args, _files, _user_path): """ Setup a TCP/UDP/SCTP v4 or v5 tunnel to the target Parameters are same as :meth:`ttbl.tt_interface.request_process` Parameters specified in the *args* dictionary from the HTTP interface: :param str ip_addr: target's IP address to use (it must be listed on the targets's tags *ipv4_address* or *ipv6_address*). :param int port: port to redirect to :param str protocol: Protocol to tunnel: {udp,sctp,tcp}[{4,6}] :returns dict: dicionary with a single key *result* set ot the *local_port* where to TCP connect to reach the tunnel. """ ip_addr, port, protocol, tunnel_id = self._check_args( self.arg_get(args, 'ip_addr', basestring), self.arg_get(args, 'port', int), self.arg_get(args, 'protocol', basestring), ) self._ip_addr_validate(target, ip_addr) with target.target_owned_and_locked(who): for tunnel_id in target.fsdb.keys("interfaces.tunnel.*.protocol"): prefix = tunnel_id[:-len(".protocol")] _ip_addr = target.fsdb.get(prefix + ".ip_addr") _protocol = target.fsdb.get(prefix + ".protocol") _port = target.fsdb.get(prefix + ".port") _pid = target.fsdb.get(prefix + ".__id") _lport = prefix[len("interfaces.tunnel."):] if _ip_addr == ip_addr \ and _protocol == protocol \ and _port == port \ and commonl.process_alive(_pid, "/usr/bin/socat"): # there is already an active tunnel for this port # and it is alive, so use that return dict(result=int(_lport)) local_port = commonl.tcp_port_assigner( port_range=ttbl.config.tcp_port_range) ip_addr = ipaddress.ip_address(unicode(ip_addr)) if isinstance(ip_addr, ipaddress.IPv6Address): # beacause socat (and most others) likes it like that ip_addr = "[%s]" % ip_addr # this could be refactored using daemon_c, but it'd be # harder to follow the code and it is not really needed. p = subprocess.Popen([ "/usr/bin/socat", "-ly", "-lp", tunnel_id, "%s-LISTEN:%d,fork,reuseaddr" % (protocol, local_port), "%s:%s:%s" % (protocol, ip_addr, port) ], shell=False, cwd=target.state_dir, close_fds=True) pid = commonl.process_started(p.pid, "/usr/bin/socat", verification_f=commonl.tcp_port_busy, verification_f_args=(local_port, ), tag="socat-" + tunnel_id, log=target.log) if p.returncode != None: raise RuntimeError("TUNNEL %s: socat exited with %d" % (tunnel_id, p.returncode)) ttbl.daemon_pid_add(p.pid) # FIXME: race condition if it # died? target.fsdb.set("interfaces.tunnel.%s.__id" % local_port, p.pid) target.fsdb.set("interfaces.tunnel.%s.ip_addr" % local_port, str(ip_addr)) target.fsdb.set("interfaces.tunnel.%s.protocol" % local_port, protocol) target.fsdb.set("interfaces.tunnel.%s.port" % local_port, port) return dict(result=local_port)