Skip to content

Commit 5c6a1d3

Browse files
committed
detached process
1 parent 4137bbd commit 5c6a1d3

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

sources/include/cage-core/process.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ namespace cage
1414
{
1515
String command;
1616
String workingDirectory;
17-
17+
sint32 priority = 0; // 0 = normal, -1 = lower priority, 1 = higher priority
1818
bool discardStdIn = false;
1919
bool discardStdOut = false;
2020
bool discardStdErr = false;
21-
22-
bool lowerPriority = false;
21+
bool detached = false;
2322

2423
ProcessCreateConfig(const String &command, const String &workingDirectory = "");
2524
};

sources/libcore/concurrent/process.cpp

+43-15
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,31 @@ namespace cage
134134
detail::memcpy(cmd2, config.command.c_str(), config.command.length());
135135

136136
DWORD flags = 0;
137-
if (config.lowerPriority)
137+
if (config.priority < 0)
138138
flags |= BELOW_NORMAL_PRIORITY_CLASS;
139+
else if (config.priority > 0)
140+
flags |= ABOVE_NORMAL_PRIORITY_CLASS;
141+
if (config.detached)
142+
flags |= DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP;
139143

140144
if (!CreateProcess(nullptr, cmd2, nullptr, nullptr, TRUE, flags, nullptr, workingDir.c_str(), &siStartInfo, &piProcInfo))
141145
CAGE_THROW_ERROR(SystemError, "CreateProcess", GetLastError());
142146

143147
hProcess.handle = piProcInfo.hProcess;
144148
hThread.handle = piProcInfo.hThread;
145149

150+
CAGE_LOG_CONTINUE(SeverityEnum::Info, "process", Stringizer() + "process id: " + (uint32)GetProcessId(hProcess.handle));
151+
146152
hPipeOutW.close();
147153
hPipeInR.close();
148154

149-
CAGE_LOG_CONTINUE(SeverityEnum::Info, "process", Stringizer() + "process id: " + (uint32)GetProcessId(hProcess.handle));
155+
if (config.detached)
156+
{
157+
hPipeOutR.close();
158+
hPipeInW.close();
159+
hProcess.close();
160+
hThread.close();
161+
}
150162
}
151163

152164
~ProcessImpl()
@@ -180,6 +192,8 @@ namespace cage
180192

181193
int wait()
182194
{
195+
if (hProcess.handle == 0)
196+
return 0;
183197
if (WaitForSingleObject(hProcess.handle, INFINITE) != WAIT_OBJECT_0)
184198
CAGE_THROW_ERROR(Exception, "WaitForSingleObject");
185199
DWORD ret = 0;
@@ -286,8 +300,8 @@ namespace cage
286300

287301
void close()
288302
{
289-
close(0);
290-
close(1);
303+
close(PIPE_READ);
304+
close(PIPE_WRITE);
291305
}
292306

293307
~AutoPipe() { close(); }
@@ -352,11 +366,18 @@ namespace cage
352366
CAGE_THROW_ERROR(SystemError, "failed to change working directory", errno);
353367

354368
// lower priority
355-
if (config.lowerPriority)
369+
if (config.priority < 0)
356370
{
357371
nice(3);
358372
// ignore errors here
359373
}
374+
// higher priority not available on linux
375+
376+
if (config.detached)
377+
{
378+
if (setsid() < 0)
379+
CAGE_THROW_ERROR(SystemError, "failed to detach process (setsid)", errno);
380+
}
360381

361382
// run child process image
362383
// using bin/sh to automatically split individual arguments
@@ -365,21 +386,25 @@ namespace cage
365386
// if we get here, an error occurred, but we are in the child process, so just exit
366387
exit(res);
367388
}
368-
else if (pid > 0)
369-
{
370-
// parent process
371-
372-
// close unused file descriptors
373-
aStdinPipe.close(PIPE_READ);
374-
aStdoutPipe.close(PIPE_WRITE);
375-
}
376-
else
389+
else if (pid < 0)
377390
{
378391
// failed to create child
379392
CAGE_THROW_ERROR(SystemError, "fork failed", errno);
380393
}
381394

395+
// parent process
382396
CAGE_LOG_CONTINUE(SeverityEnum::Info, "process", Stringizer() + "process id: " + pid);
397+
398+
// close unused file descriptors
399+
aStdinPipe.close(PIPE_READ);
400+
aStdoutPipe.close(PIPE_WRITE);
401+
402+
if (config.detached)
403+
{
404+
aStdinPipe.close(PIPE_WRITE);
405+
aStdoutPipe.close(PIPE_READ);
406+
pid = -1;
407+
}
383408
}
384409

385410
~ProcessImpl()
@@ -507,7 +532,10 @@ namespace cage
507532

508533
Holder<Process> newProcess(const ProcessCreateConfig &config)
509534
{
510-
return systemMemory().createImpl<Process, ProcessImpl>(config);
535+
auto r = systemMemory().createImpl<Process, ProcessImpl>(config);
536+
if (config.detached)
537+
return {};
538+
return r;
511539
}
512540

513541
namespace

0 commit comments

Comments
 (0)