-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessMacOS.cpp
More file actions
35 lines (30 loc) · 1002 Bytes
/
ProcessMacOS.cpp
File metadata and controls
35 lines (30 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <signal.h> // kill
#include <spawn.h> // posix_spawn
#include <unistd.h> // STDIN_FILENO
#include <iostream>
#include "Help.h"
#include "Process.h"
class _Process;
class _Process {
pid_t pid{0};
public:
bool spawn(char const* const argument[]) {
posix_spawn_file_actions_t action;
posix_spawn_file_actions_init(&action);
posix_spawn_file_actions_addinherit_np(&action, STDIN_FILENO);
posix_spawn_file_actions_addinherit_np(&action, STDOUT_FILENO);
posix_spawn_file_actions_addinherit_np(&action, STDERR_FILENO);
if (posix_spawn(&pid, argument[0], &action, nullptr,
const_cast<char* const*>(argument), nullptr) != 0) {
perror("posix_spawn failed");
return false;
}
posix_spawn_file_actions_destroy(&action);
return true;
}
void kill() { ::kill(pid, SIGINT); }
};
bool Process::spawn(char const* const argument[]) {
return implementation->spawn(argument);
}
void Process::kill() { implementation->kill(); }