Starts a new process.
67 {
68 std::string proc;
69 proc.append(executable);
70 proc.append(" ");
71 proc.append(args);
72 std::cout << "Starting " << proc << std::endl;
73# ifdef _WIN32
74 STARTUPINFO start_info;
75 PROCESS_INFORMATION proc_info;
76 GetStartupInfo(&start_info);
77 if (!CreateProcess(nullptr, const_cast<char *>(proc.c_str()), nullptr,
78 nullptr,
FALSE, CREATE_NO_WINDOW | DETACHED_PROCESS,
79 nullptr, nullptr, &start_info, &proc_info))
80 return;
81# else
82 int pid = fork();
83 if (pid != 0) {
84 } else {
85# ifdef __linux__
86
87
88 prctl(PR_SET_PDEATHSIG, 2, 0, 0, 0);
89# endif
90 std::string mutable_args(args);
91 int argc = 1;
92 for (
auto ch : mutable_args) {
94 ++argc;
95 }
96 }
97 std::unique_ptr<char *[]> argv(new char *[argc + 2]);
98 std::string argv0(executable);
99 argv[0] = &argv0[0];
100 argv[1] = &mutable_args[0];
101 argc = 2;
102 bool inquote = false;
103 for (
int i = 0; mutable_args[
i]; ++
i) {
104 if (!inquote && mutable_args[
i] ==
' ') {
105 mutable_args[
i] =
'\0';
106 argv[argc++] = &mutable_args[
i + 1];
107 }
else if (mutable_args[
i] ==
'"') {
108 inquote = !inquote;
109 mutable_args[
i] =
' ';
110 }
111 }
112 argv[argc] = nullptr;
113 execvp(executable, argv.get());
114 }
115# endif
116}