@@ -134,19 +134,31 @@ namespace cage
134
134
detail::memcpy (cmd2, config.command .c_str (), config.command .length ());
135
135
136
136
DWORD flags = 0 ;
137
- if (config.lowerPriority )
137
+ if (config.priority < 0 )
138
138
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;
139
143
140
144
if (!CreateProcess (nullptr , cmd2, nullptr , nullptr , TRUE , flags, nullptr , workingDir.c_str (), &siStartInfo, &piProcInfo))
141
145
CAGE_THROW_ERROR (SystemError, " CreateProcess" , GetLastError ());
142
146
143
147
hProcess.handle = piProcInfo.hProcess ;
144
148
hThread.handle = piProcInfo.hThread ;
145
149
150
+ CAGE_LOG_CONTINUE (SeverityEnum::Info, " process" , Stringizer () + " process id: " + (uint32)GetProcessId (hProcess.handle ));
151
+
146
152
hPipeOutW.close ();
147
153
hPipeInR.close ();
148
154
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
+ }
150
162
}
151
163
152
164
~ProcessImpl ()
@@ -180,6 +192,8 @@ namespace cage
180
192
181
193
int wait ()
182
194
{
195
+ if (hProcess.handle == 0 )
196
+ return 0 ;
183
197
if (WaitForSingleObject (hProcess.handle , INFINITE) != WAIT_OBJECT_0)
184
198
CAGE_THROW_ERROR (Exception, " WaitForSingleObject" );
185
199
DWORD ret = 0 ;
@@ -286,8 +300,8 @@ namespace cage
286
300
287
301
void close ()
288
302
{
289
- close (0 );
290
- close (1 );
303
+ close (PIPE_READ );
304
+ close (PIPE_WRITE );
291
305
}
292
306
293
307
~AutoPipe () { close (); }
@@ -352,11 +366,18 @@ namespace cage
352
366
CAGE_THROW_ERROR (SystemError, " failed to change working directory" , errno);
353
367
354
368
// lower priority
355
- if (config.lowerPriority )
369
+ if (config.priority < 0 )
356
370
{
357
371
nice (3 );
358
372
// ignore errors here
359
373
}
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
+ }
360
381
361
382
// run child process image
362
383
// using bin/sh to automatically split individual arguments
@@ -365,21 +386,25 @@ namespace cage
365
386
// if we get here, an error occurred, but we are in the child process, so just exit
366
387
exit (res);
367
388
}
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 )
377
390
{
378
391
// failed to create child
379
392
CAGE_THROW_ERROR (SystemError, " fork failed" , errno);
380
393
}
381
394
395
+ // parent process
382
396
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
+ }
383
408
}
384
409
385
410
~ProcessImpl ()
@@ -507,7 +532,10 @@ namespace cage
507
532
508
533
Holder<Process> newProcess (const ProcessCreateConfig &config)
509
534
{
510
- return systemMemory ().createImpl <Process, ProcessImpl>(config);
535
+ auto r = systemMemory ().createImpl <Process, ProcessImpl>(config);
536
+ if (config.detached )
537
+ return {};
538
+ return r;
511
539
}
512
540
513
541
namespace
0 commit comments