Skip to content

Handle fork(2) failures #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/mp/gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

#include <algorithm>
#include <capnp/schema-parser.h>
#include <errno.h>
#include <fstream>
#include <map>
#include <set>
#include <sstream>
#include <system_error>
#include <unistd.h>
#include <vector>

Expand Down Expand Up @@ -137,6 +139,9 @@ void Generate(kj::StringPtr src_prefix,
args.emplace_back("--output=" capnp_PREFIX "/bin/capnpc-c++");
args.emplace_back(src_file);
int pid = fork();
if (pid == -1) {
throw std::system_error(errno, std::system_category(), "fork");
}
if (!pid) {
mp::ExecProcess(args);
}
Expand Down
5 changes: 5 additions & 0 deletions src/mp/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <mp/util.h>

#include <errno.h>
#include <kj/array.h>
#include <pthread.h>
#include <sstream>
Expand All @@ -13,6 +14,7 @@
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <system_error>
#include <unistd.h>

#if __linux__
Expand Down Expand Up @@ -83,6 +85,9 @@ int SpawnProcess(int& pid, FdToArgsFn&& fd_to_args)
}

pid = fork();
if (pid == -1) {
throw std::system_error(errno, std::system_category(), "fork");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I didn't know system_error took a third string argument. It'd be good to start using this more places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it also occurred to me that if std::system_category() modifies errno then std::system_error() may get the wrong value of errno:

#include <errno.h>
#include <stdio.h>

int f1() {
    errno = 3;
    return 20;
}   

void f2(int e, int x) {
    printf("%d %d\n", e, x);
}   

int main(int, char**) {
    errno = 2; // resulted from fork() or whatever
    f2(errno, f1());
    return 0;
}

produces:

# clang 9.0.1
$ clang++90 t.cc -o t && ./t
2 20

# gcc 9.2.0
$ g++9 t.cc -o t && ./t
3 20

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a possible concern, but in practice if that constructor was setting errno, it would probably break a lot of things. Also in the worst case error message text would just be less clear

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, safe to ignore in this case. Just the pattern func1(x, func2_may_possibly_modify_x()); is better to be avoided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

system_error took a third string argument. It'd be good to start using this more places.

#23 Tell std::system_error() which function failed

Cheerz!

}
if (close(fds[pid ? 0 : 1]) != 0) {
throw std::system_error(errno, std::system_category());
}
Expand Down