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/login/logind-session.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/login/logind-session.c')
-rw-r--r-- | src/login/logind-session.c | 18 |
1 files changed, 6 insertions, 12 deletions
diff --git a/src/login/logind-session.c b/src/login/logind-session.c index 33ab09ea52..8c517f46a7 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -407,8 +407,7 @@ int session_load(Session *s) { trigger the EOF. */ fd = session_create_fifo(s); - if (fd >= 0) - close_nointr_nofail(fd); + safe_close(fd); } if (realtime) { @@ -864,13 +863,8 @@ int session_create_fifo(Session *s) { static void session_remove_fifo(Session *s) { assert(s); - if (s->fifo_event_source) - s->fifo_event_source = sd_event_source_unref(s->fifo_event_source); - - if (s->fifo_fd >= 0) { - close_nointr_nofail(s->fifo_fd); - s->fifo_fd = -1; - } + s->fifo_event_source = sd_event_source_unref(s->fifo_event_source); + s->fifo_fd = safe_close(s->fifo_fd); if (s->fifo_path) { unlink(s->fifo_path); @@ -950,7 +944,7 @@ static int session_open_vt(Session *s) { s->vtfd = open(path, O_RDWR | O_CLOEXEC | O_NONBLOCK | O_NOCTTY); if (s->vtfd < 0) { log_error("cannot open VT %s of session %s: %m", path, s->id); - return -1; + return -errno; } return s->vtfd; @@ -1022,13 +1016,13 @@ void session_restore_vt(Session *s) { if (read_one_line_file("/sys/module/vt/parameters/default_utf8", &utf8) >= 0 && *utf8 == '1') kb = K_UNICODE; + ioctl(vt, KDSKBMODE, kb); mode.mode = VT_AUTO; ioctl(vt, VT_SETMODE, &mode); - close_nointr_nofail(vt); - s->vtfd = -1; + s->vtfd = safe_close(s->vtfd); } bool session_is_controller(Session *s, const char *sender) { |