Skip to content

Instantly share code, notes, and snippets.

@KayEss
Last active August 29, 2015 14:27
Show Gist options
  • Save KayEss/ecf1f197a77515e84a97 to your computer and use it in GitHub Desktop.
Save KayEss/ecf1f197a77515e84a97 to your computer and use it in GitHub Desktop.
General form for Linux system call
// Signal handling.... sigh
template<typename F> inline
int syscall(F f) {
int result{};
do {
result = f();
} while ( result == -1 && errno == EINTR );
return result;
}
// Using it
void open_shut(const char *pathname) {
int opened = syscall([&]() { return open(pathname, O_CREAT | O_CLOEXEC); });
if ( opened >= 0 ) {
// Use the file descripter
syscall([&]() { return close(opened); });
} else {
// Couldn't open, no need to close
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment