Skip to content

Commit

Permalink
Merge pull request #350 from doronz88/bugfix/wait_for_pid
Browse files Browse the repository at this point in the history
processes: ios: refine `launch`
  • Loading branch information
doronz88 authored Feb 27, 2024
2 parents 686759b + 7252e8c commit 801ef04
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
44 changes: 28 additions & 16 deletions src/rpcclient/rpcclient/ios/processes.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import logging
from datetime import datetime, timedelta
from typing import Optional

from rpcclient.darwin.processes import DarwinProcesses
from rpcclient.darwin.processes import DarwinProcesses, Process
from rpcclient.exceptions import LaunchError
from rpcclient.structs.consts import SIGKILL

logger = logging.getLogger(__name__)

class IosProcesses(DarwinProcesses):

def launch(self, bundle_id: str, unlock_device: bool = True, disable_aslr: bool = False,
wait_for_debugger: bool = False, stdout: Optional[str] = None,
stderr: Optional[str] = None) -> int:
""" launch process using BackBoardService
https://github.com/swigger/debugserver-ios/blob/master/inc/BackBoardServices.framework/Headers
/BackBoardServices.h"""
class IosProcesses(DarwinProcesses):
def launch(self, bundle_id: str, kill_existing: bool = True, timeout: float = 1, unlock_device: bool = True,
disable_aslr: bool = False, wait_for_debugger: bool = False, stdout: Optional[str] = None,
stderr: Optional[str] = None) -> Process:
"""
launch process using BackBoardService
https://github.com/swigger/debugserver-ios/blob/master/inc/BackBoardServices.framework/Headers/BackBoardServices.h
"""
debug_options = {}
options = {}
sym = self._client.symbols
Expand All @@ -21,17 +26,24 @@ def launch(self, bundle_id: str, unlock_device: bool = True, disable_aslr: bool
debug_options[sym.BKSDebugOptionKeyStandardOutPath[0].py()] = stdout
if stderr is not None:
debug_options[sym.BKSDebugOptionKeyStandardErrorPath[0].py()] = stderr

options[sym.BKSOpenApplicationOptionKeyUnlockDevice[0].py()] = unlock_device
options[sym.BKSOpenApplicationOptionKeyDebuggingOptions[0].py()] = debug_options

bkssystem_service = self._client.objc_get_class('BKSSystemService').new().objc_symbol
bkssystem_service.openApplication_options_clientPort_withResult_(self._client.cf(bundle_id),
self._client.cf(options),
bkssystem_service.createClientPort(),
self._client.get_dummy_block())
bkssystem_service = self._client.symbols.objc_getClass('BKSSystemService').objc_call('new')
pid = bkssystem_service.objc_call('pidForApplication:', self._client.cf(bundle_id)).c_int32
if pid != -1 and kill_existing:
logger.info(f'Kill existing process {pid}')
self.kill(pid, SIGKILL)

bkssystem_service.objc_call(
'openApplication:options:clientPort:withResult:', self._client.cf(bundle_id), self._client.cf(options),
bkssystem_service.objc_call('createClientPort'), self._client.get_dummy_block())

start_time = datetime.now()
timeout = timedelta(seconds=timeout)
while datetime.now() - start_time < timeout:
pid = bkssystem_service.objc_call('pidForApplication:', self._client.cf(bundle_id)).c_int32

pid = bkssystem_service.pidForApplication_(self._client.cf(bundle_id)).c_int32
if pid == -1:
raise LaunchError()
return pid
return self.get_by_pid(pid)
3 changes: 1 addition & 2 deletions src/rpcclient/tests/test_processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def test_process_object(client):

@pytest.mark.ios
def test_launch_process(client):
pid = client.processes.launch('com.apple.MobileAddressBook')
client.processes.kill(pid)
client.processes.launch('com.apple.calculator').kill()


@pytest.mark.ios
Expand Down

0 comments on commit 801ef04

Please sign in to comment.