If we already have an instance of a process with this name, auto increment ports to avoid a conflict
340470
private void incrementPortsIfDuplicateName(String configName, ProcessConfigBean config) {
        int startedCount = getNumberOfInstancesStarted(configName);

        int debugPort = config.getDebugPort();
        if ( debugPort != -1) {
            config.setDebugPort(debugPort + startedCount);
        }

        int remotingPort = config.getRemotingPort();
        if (remotingPort != -1) {
            config.setRemotingPort(remotingPort + startedCount);
        }
    }
1337443
def start_process(self, name):
        
        process_name, proc = self.new_process(name)
        LOGGER.info('Spawning %s process for %s', process_name, name)

        # Append the process to the consumer process list
        self.consumers[name].processes[process_name] = proc

        # Start the process
        try:
            proc.start()
        except IOError as error:
            LOGGER.critical('Failed to start %s for %s: %r',
                            process_name, name, error)
            del self.consumers[name].process[process_name]
241028
def _reinit_daq_daemons(sender, instance, **kwargs):
    
    if type(instance) is Device:
        try:
            bp = BackgroundProcess.objects.get(pk=instance.protocol_id)
        except:
            return False
        bp.restart()
    elif type(instance) is Variable:
        try:
            bp = BackgroundProcess.objects.get(pk=instance.device.protocol_id)
        except:
            return False
        bp.restart()
    elif type(instance) is Scaling:
        for bp_pk in list(instance.variable_set.all().values_list('device__protocol_id').distinct()):
            try:
                bp = BackgroundProcess.objects.get(pk=bp_pk)
            except:
                return False
            bp.restart()
    else:
        logger.debug('post_save from %s' % type(instance))
798466
def create_execve(original_name):
    

    def new_execve(path, args, env):
        import os
        send_process_created_message()
        return getattr(os, original_name)(path, patch_args(args), env)

    return new_execve
142837
def select_port(default_port=20128):
    """"""
    import socket
    while True:
        try:
            sock = socket.socket(socket.AF_INET,
                                 socket.SOCK_STREAM,
                                 socket.IPPROTO_TCP)
#            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            sock.bind( ("127.0.0.1", default_port) )
        except socket.error as _msg:  # analysis:ignore
            default_port += 1
        else:
            break
        finally:
            sock.close()
            sock = None
    return default_port
1138228
def remove_port(port)
      case port.type
      when :input
        input_ports.delete(port.name)
      when :output
        output_ports.delete(port.name)
      end
    end
124613
def delete_port_if_removed(self, port):
        """"""
        instance_type = self.get_instance_type(port)
        if not instance_type:
            return
        port_type = instance_type + a_const.PORT_SUFFIX
        if not db_lib.port_provisioned(port['id']):
            p_res = MechResource(port['id'], port_type, a_const.DELETE)
            self.provision_queue.put(p_res)
855556
def set_process_name():
        
        proc = multiprocessing.current_process()
        for offset in range(0, len(sys.argv)):
            if sys.argv[offset] == '-c':
                name = sys.argv[offset + 1].split('/')[-1]
                proc.name = name.split('.')[0]
                break
717644
def update_name(self, name):
        """"""
        if name != self.name:
            self.parent.executables[name] = self
            del self.parent.executables[self.name]
            self.name = name
1111126
def update_ports_tree(ports_tree):
    '''
    
    '''
    _check_config_exists()
    if ports_tree:
        cmd = 'poudriere ports -u -p {0}'.format(ports_tree)
    else:
        cmd = 'poudriere ports -u'
    ret = __salt__['cmd.run'](cmd)
    return ret
1114903
def process_exists(pid=None):
    
    if not pid:
        pid = current_pid()
    elif callable(pid):
        pid = pid()

    if pid and psutil.pid_exists(pid):
        pname = psutil.Process(pid).name()
        if os.name == 'nt':
            return os.path.splitext(pname)[0], pid
        return pname, pid
    return None, None
549989
def delete_instance_if_removed(self, port):
        """"""
        instance_type = self.get_instance_type(port)
        if not instance_type:
            return
        if not db_lib.instance_provisioned(port['device_id']):
            i_res = MechResource(port['device_id'], instance_type,
                                 a_const.DELETE)
            self.provision_queue.put(i_res)
1160605
def _copyPort(port: LPort, targetParent: Union[LPort], reverseDirection):
    
    d = port.direction
    side = port.side
    if reverseDirection:
        d = PortType.opposite(d)
        side = PortSide.opposite(side)

    newP = LPort(targetParent.parentNode, d, side, name=port.name)
    if isinstance(targetParent, LPort):
        targetParent.children.append(newP)
        newP.parent = targetParent
    else:
        targetParent.getPortSideView(side).append(newP)

    for ch in port.children:
        _copyPort(ch, newP, reverseDirection)

    return newP
535896
def spawn_process(self, process=None):
        
        if process is None:
            return False
        # start new child process
        pid = fork()
        if pid != 0:
            # parent process
            process.pid = pid
            self.PROCESSES[process.process_id] = process
            connections.close_all()
            return True
        # child process
        process.pid = getpid()
        # connection.connection.close()
        # connection.connection = None
        process.pre_init_process()
        process.init_process()
        process.run()
        sys.exit(0)
174484
def get_extra_pids(self):
        
        while not self.is_retired:
            for proc in psutil.process_iter():
                for conn in proc.connections():
                    if conn.laddr.port == self.port:
                        self.logger.debug(f".NET socket server for {self.name} appears to have pid {proc.pid}")
                        return [proc.pid]
            if self.is_executable_configured():
                return []
            time.sleep(1)
            if self.dotnet_executable_path is None:
                self.logger.info("Can't auto-start .NET executable because no executable is configured. "
                                 "Please start the .NET bot manually!")
            else:
                self.logger.info(f"Can't auto-start .NET executable because {self.dotnet_executable_path} "
                                 "is not found. Please start the .NET bot manually!")
80991
def pre_fork(self, process_manager):
        '''
        
        '''
        salt.transport.mixins.auth.AESReqServerMixin.pre_fork(self, process_manager)
        if USE_LOAD_BALANCER:
            self.socket_queue = multiprocessing.Queue()
            process_manager.add_process(
                LoadBalancerServer, args=(self.opts, self.socket_queue)
            )
        elif not salt.utils.platform.is_windows():
            self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            _set_tcp_keepalive(self._socket, self.opts)
            self._socket.setblocking(0)
            self._socket.bind((self.opts['interface'], int(self.opts['ret_port'])))
348868
def port(self, name):
        
        for p in self.ports:
            if p.name == name:
                return p