diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-03-18 19:22:43 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-03-18 19:31:34 +0100 |
commit | 03e334a1c7dc8c20c38902aa039440763acc9b17 (patch) | |
tree | bc30b522de8ef9c251bf3ff2fe2d52c92dd8b1ea /src/tty-ask-password-agent/tty-ask-password-agent.c | |
parent | 9459781ee66eb57709c8b8701701365ba60a9f1c (diff) |
util: replace close_nointr_nofail() by a more useful safe_close()
safe_close() automatically becomes a NOP when a negative fd is passed,
and returns -1 unconditionally. This makes it easy to write lines like
this:
fd = safe_close(fd);
Which will close an fd if it is open, and reset the fd variable
correctly.
By making use of this new scheme we can drop a > 200 lines of code that
was required to test for non-negative fds or to reset the closed fd
variable afterwards.
Diffstat (limited to 'src/tty-ask-password-agent/tty-ask-password-agent.c')
-rw-r--r-- | src/tty-ask-password-agent/tty-ask-password-agent.c | 25 |
1 files changed, 8 insertions, 17 deletions
diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c index fa4d660215..1d067af229 100644 --- a/src/tty-ask-password-agent/tty-ask-password-agent.c +++ b/src/tty-ask-password-agent/tty-ask-password-agent.c @@ -234,11 +234,8 @@ static int ask_password_plymouth( r = 0; finish: - if (notify >= 0) - close_nointr_nofail(notify); - - if (fd >= 0) - close_nointr_nofail(fd); + safe_close(notify); + safe_close(fd); free(packet); @@ -372,7 +369,7 @@ static int parse_password(const char *filename, char **wall) { r = ask_password_tty(message, not_after, filename, &password); if (arg_console) { - close_nointr_nofail(tty_fd); + safe_close(tty_fd); release_terminal(); } @@ -419,8 +416,7 @@ static int parse_password(const char *filename, char **wall) { finish: fclose(f); - if (socket_fd >= 0) - close_nointr_nofail(socket_fd); + safe_close(socket_fd); free(packet); free(socket_name); @@ -492,7 +488,7 @@ static bool wall_tty_match(const char *path) { return true; /* What, we managed to open the pipe? Then this tty is filtered. */ - close_nointr_nofail(fd); + safe_close(fd); return false; } @@ -614,14 +610,9 @@ static int watch_passwords(void) { r = 0; finish: - if (notify >= 0) - close_nointr_nofail(notify); - - if (signal_fd >= 0) - close_nointr_nofail(signal_fd); - - if (tty_block_fd >= 0) - close_nointr_nofail(tty_block_fd); + safe_close(notify); + safe_close(signal_fd); + safe_close(tty_block_fd); return r; } |