Skip to content

Commit

Permalink
Merge pull request #4298 from CyberShadow/fork-nogc
Browse files Browse the repository at this point in the history
std.process: Enforce nothrow/@nogc in forks
  • Loading branch information
burner committed May 26, 2016
2 parents 088f7bf + 7eae1b9 commit 0d33ee0
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions std/process.d
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,11 @@ private Pid spawnProcessImpl(in char[][] args,
auto id = core.sys.posix.unistd.fork();
if (id < 0)
throw ProcessException.newFromErrno("Failed to spawn new process");
if (id == 0)

void forkChild() nothrow @nogc
{
pragma(inline, true);

// Child process

// Set the working directory.
Expand Down Expand Up @@ -455,7 +458,12 @@ private Pid spawnProcessImpl(in char[][] args,

// Get the maximum number of file descriptors that could be open.
rlimit r;
errnoEnforce(getrlimit(RLIMIT_NOFILE, &r) == 0);
if (getrlimit(RLIMIT_NOFILE, &r) != 0)
{
core.sys.posix.stdio.perror("getrlimit");
core.sys.posix.unistd._exit(1);
assert(0);
}
immutable maxDescriptors = cast(int)r.rlim_cur;

// The above, less stdin, stdout, and stderr
Expand Down Expand Up @@ -513,6 +521,11 @@ private Pid spawnProcessImpl(in char[][] args,
// If execution fails, exit as quickly as possible.
core.sys.posix.stdio.perror("spawnProcess(): Failed to execute program");
core.sys.posix.unistd._exit(1);
}

if (id == 0)
{
forkChild();
assert (0);
}
else
Expand Down Expand Up @@ -779,7 +792,7 @@ version (Posix) unittest

// Sets or unsets the FD_CLOEXEC flag on the given file descriptor.
version (Posix)
private void setCLOEXEC(int fd, bool on)
private void setCLOEXEC(int fd, bool on) nothrow @nogc
{
import core.sys.posix.fcntl;
auto flags = fcntl(fd, F_GETFD);
Expand Down Expand Up @@ -3754,7 +3767,7 @@ else version (OSX)
import core.stdc.string;
import core.sys.posix.unistd;

void browse(const(char)[] url)
void browse(const(char)[] url) nothrow @nogc
{
const(char)*[5] args;

Expand Down Expand Up @@ -3790,7 +3803,7 @@ else version (Posix)
import core.stdc.string;
import core.sys.posix.unistd;

void browse(const(char)[] url)
void browse(const(char)[] url) nothrow @nogc
{
const(char)*[3] args;

Expand Down

0 comments on commit 0d33ee0

Please sign in to comment.