From 655b6084a5133d01a513c976b8b2567a71ba30c4 Mon Sep 17 00:00:00 2001 From: Timo Weingärtner Date: Fri, 9 May 2014 22:21:52 +0200 Subject: throw system_error instead of calling perror, exit --- ssh-agent-filter.C | 50 ++++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/ssh-agent-filter.C b/ssh-agent-filter.C index 36677f9..e2777c7 100644 --- a/ssh-agent-filter.C +++ b/ssh-agent-filter.C @@ -48,6 +48,10 @@ using std::flush; #include using std::runtime_error; +#include +using std::system_error; +using std::system_category; + #include using std::pair; using std::move; @@ -57,6 +61,7 @@ using std::count; #include +#include #include #include #include @@ -113,10 +118,8 @@ string base64_encode (string const & s) { } void cloexec (int fd) { - if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC)) { - perror("fcntl"); - exit(EX_UNAVAILABLE); - } + if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC)) + throw system_error(errno, system_category(), "fcntl"); } void arm(std::ios & stream) { @@ -133,10 +136,8 @@ int make_upstream_agent_conn () { exit(EX_UNAVAILABLE); } - if ((sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1) { - perror("socket"); - exit(EX_UNAVAILABLE); - } + if ((sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1) + throw system_error(errno, system_category(), "socket"); cloexec(sock); addr.sun_family = AF_UNIX; @@ -148,10 +149,8 @@ int make_upstream_agent_conn () { strcpy(addr.sun_path, path); - if (connect(sock, reinterpret_cast(&addr), sizeof(addr))) { - perror("connect"); - exit(EX_UNAVAILABLE); - } + if (connect(sock, reinterpret_cast(&addr), sizeof(addr))) + throw system_error(errno, system_category(), "connect"); return sock; } @@ -160,10 +159,8 @@ int make_listen_sock () { int sock; struct sockaddr_un addr; - if ((sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1) { - perror("socket"); - exit(EX_UNAVAILABLE); - } + if ((sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1) + throw system_error(errno, system_category(), "socket"); cloexec(sock); addr.sun_family = AF_UNIX; @@ -175,15 +172,11 @@ int make_listen_sock () { strcpy(addr.sun_path, path.c_str()); - if (bind(sock, reinterpret_cast(&addr), sizeof(addr))) { - perror("bind"); - exit(EX_UNAVAILABLE); - } + if (bind(sock, reinterpret_cast(&addr), sizeof(addr))) + throw system_error(errno, system_category(), "bind"); - if (listen(sock, 0)) { - perror("listen"); - exit(EX_UNAVAILABLE); - } + if (listen(sock, 0)) + throw system_error(errno, system_category(), "listen"); return sock; } @@ -308,8 +301,7 @@ bool confirm (string const & question) { char const * args[3] = { sap, question.c_str(), nullptr }; // see execvp(3p) for cast rationale execvp(sap, const_cast(args)); - perror("exec"); - exit(EX_UNAVAILABLE); + throw system_error(errno, system_category(), "exec"); } else { // parent int status; @@ -470,10 +462,8 @@ int main (int const argc, char const * const * const argv) { if (not debug) { pid_t pid = fork(); - if (pid == -1) { - perror("fork"); - exit(EX_OSERR); - } + if (pid == -1) + throw system_error(errno, system_category(), "fork"); if (pid > 0) { cout << "SSH_AUTH_SOCK='" << path.native() << "'; export SSH_AUTH_SOCK;" << endl; cout << "SSH_AGENT_PID='" << pid << "'; export SSH_AGENT_PID;" << endl; -- cgit v1.2.3