Skip to content

Commit dc06ba0

Browse files
committed
Implement process control cpp bindings
1 parent b49d519 commit dc06ba0

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Jackson Coxson
2+
3+
#pragma once
4+
#include <idevice++/bindings.hpp>
5+
#include <idevice++/remote_server.hpp>
6+
#include <idevice++/result.hpp>
7+
#include <memory>
8+
9+
namespace IdeviceFFI {
10+
11+
using ProcessControlPtr =
12+
std::unique_ptr<ProcessControlHandle, FnDeleter<ProcessControlHandle, process_control_free>>;
13+
14+
class ProcessControl {
15+
public:
16+
// Factory: borrows the RemoteServer; not consumed
17+
static Result<ProcessControl, FfiError> create(RemoteServer& server);
18+
19+
Result<u_int64_t, FfiError> launch_app(std::string bundle_id,
20+
Option<std::vector<std::string>> env_vars,
21+
Option<std::vector<std::string>> arguments,
22+
bool start_suspended,
23+
bool kill_existing);
24+
Result<void, FfiError> kill_app(u_int64_t pid);
25+
Result<void, FfiError> disable_memory_limit(u_int64_t pid);
26+
27+
~ProcessControl() noexcept = default;
28+
ProcessControl(ProcessControl&&) noexcept = default;
29+
ProcessControl& operator=(ProcessControl&&) noexcept = default;
30+
ProcessControl(const ProcessControl&) = delete;
31+
ProcessControl& operator=(const ProcessControl&) = delete;
32+
33+
ProcessControlHandle* raw() const noexcept { return handle_.get(); }
34+
static ProcessControl adopt(ProcessControlHandle* h) noexcept { return ProcessControl(h); }
35+
36+
private:
37+
explicit ProcessControl(ProcessControlHandle* h) noexcept : handle_(h) {}
38+
ProcessControlPtr handle_{};
39+
};
40+
41+
} // namespace IdeviceFFI

cpp/src/process_control.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Jackson Coxson
2+
3+
#include <idevice++/process_control.hpp>
4+
5+
namespace IdeviceFFI {
6+
7+
Result<ProcessControl, FfiError> ProcessControl::create(RemoteServer& server) {
8+
ProcessControlHandle* out = nullptr;
9+
FfiError e(::process_control_new(server.raw(), &out));
10+
if (e) {
11+
return Err(e);
12+
}
13+
return Ok(ProcessControl::adopt(out));
14+
}
15+
16+
Result<u_int64_t, FfiError> ProcessControl::launch_app(std::string bundle_id,
17+
Option<std::vector<std::string>> env_vars,
18+
Option<std::vector<std::string>> arguments,
19+
bool start_suspended,
20+
bool kill_existing) {
21+
std::vector<const char*> c_env_vars;
22+
size_t env_vars_len = 0;
23+
if (env_vars.is_some()) {
24+
c_env_vars.reserve(env_vars.unwrap().size());
25+
for (auto& a : env_vars.unwrap()) {
26+
c_env_vars.push_back(a.c_str());
27+
}
28+
}
29+
30+
std::vector<const char*> c_arguments;
31+
size_t arguments_len = 0;
32+
if (arguments.is_some()) {
33+
c_arguments.reserve(arguments.unwrap().size());
34+
for (auto& a : arguments.unwrap()) {
35+
c_arguments.push_back(a.c_str());
36+
}
37+
}
38+
39+
u_int64_t pid = 0;
40+
41+
FfiError e(::process_control_launch_app(
42+
handle_.get(),
43+
bundle_id.c_str(),
44+
c_env_vars.empty() ? nullptr : const_cast<const char* const*>(c_env_vars.data()),
45+
env_vars_len,
46+
c_arguments.empty() ? nullptr : const_cast<const char* const*>(c_arguments.data()),
47+
arguments_len,
48+
start_suspended,
49+
kill_existing,
50+
&pid));
51+
if (e) {
52+
return Err(e);
53+
}
54+
return Ok(pid);
55+
}
56+
57+
Result<void, FfiError> ProcessControl::kill_app(u_int64_t pid) {
58+
FfiError e(::process_control_kill_app(handle_.get(), pid));
59+
if (e) {
60+
return Err(e);
61+
}
62+
return Ok();
63+
}
64+
65+
Result<void, FfiError> ProcessControl::disable_memory_limit(u_int64_t pid) {
66+
FfiError e(::process_control_disable_memory_limit(handle_.get(), pid));
67+
if (e) {
68+
return Err(e);
69+
}
70+
return Ok();
71+
}
72+
73+
} // namespace IdeviceFFI

0 commit comments

Comments
 (0)