Last active
August 29, 2015 14:27
-
-
Save KayEss/ecf1f197a77515e84a97 to your computer and use it in GitHub Desktop.
General form for Linux system call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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