summaryrefslogtreecommitdiff
path: root/src/login
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-03-18 19:22:43 +0100
committerLennart Poettering <lennart@poettering.net>2014-03-18 19:31:34 +0100
commit03e334a1c7dc8c20c38902aa039440763acc9b17 (patch)
treebc30b522de8ef9c251bf3ff2fe2d52c92dd8b1ea /src/login
parent9459781ee66eb57709c8b8701701365ba60a9f1c (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')
-rw-r--r--src/login/logind-button.c2
-rw-r--r--src/login/logind-inhibit.c12
-rw-r--r--src/login/logind-session.c18
-rw-r--r--src/login/logind.c6
-rw-r--r--src/login/pam-module.c2
-rw-r--r--src/login/test-inhibit.c4
6 files changed, 15 insertions, 29 deletions
diff --git a/src/login/logind-button.c b/src/login/logind-button.c
index 060978dd34..2561d13c67 100644
--- a/src/login/logind-button.c
+++ b/src/login/logind-button.c
@@ -72,7 +72,7 @@ void button_free(Button *b) {
if (b->fd >= 0) {
/* If the device has been unplugged close() returns
* ENODEV, let's ignore this, hence we don't use
- * close_nointr_nofail() */
+ * safe_close() */
close(b->fd);
}
diff --git a/src/login/logind-inhibit.c b/src/login/logind-inhibit.c
index d19d648129..8b329abd79 100644
--- a/src/login/logind-inhibit.c
+++ b/src/login/logind-inhibit.c
@@ -253,8 +253,7 @@ int inhibitor_load(Inhibitor *i) {
int fd;
fd = inhibitor_create_fifo(i);
- if (fd >= 0)
- close_nointr_nofail(fd);
+ safe_close(fd);
}
return 0;
@@ -320,13 +319,8 @@ int inhibitor_create_fifo(Inhibitor *i) {
void inhibitor_remove_fifo(Inhibitor *i) {
assert(i);
- if (i->event_source)
- i->event_source = sd_event_source_unref(i->event_source);
-
- if (i->fifo_fd >= 0) {
- close_nointr_nofail(i->fifo_fd);
- i->fifo_fd = -1;
- }
+ i->event_source = sd_event_source_unref(i->event_source);
+ i->fifo_fd = safe_close(i->fifo_fd);
if (i->fifo_path) {
unlink(i->fifo_path);
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) {
diff --git a/src/login/logind.c b/src/login/logind.c
index 8ba8a9155b..db9882323f 100644
--- a/src/login/logind.c
+++ b/src/login/logind.c
@@ -149,8 +149,7 @@ void manager_free(Manager *m) {
sd_event_source_unref(m->udev_button_event_source);
sd_event_source_unref(m->lid_switch_ignore_event_source);
- if (m->console_active_fd >= 0)
- close_nointr_nofail(m->console_active_fd);
+ safe_close(m->console_active_fd);
if (m->udev_seat_monitor)
udev_monitor_unref(m->udev_seat_monitor);
@@ -169,8 +168,7 @@ void manager_free(Manager *m) {
sd_bus_unref(m->bus);
sd_event_unref(m->event);
- if (m->reserve_vt_fd >= 0)
- close_nointr_nofail(m->reserve_vt_fd);
+ safe_close(m->reserve_vt_fd);
strv_free(m->kill_only_users);
strv_free(m->kill_exclude_users);
diff --git a/src/login/pam-module.c b/src/login/pam-module.c
index 195d4d574e..9873dd547a 100644
--- a/src/login/pam-module.c
+++ b/src/login/pam-module.c
@@ -484,7 +484,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
r = pam_set_data(handle, "systemd.session-fd", INT_TO_PTR(session_fd+1), NULL);
if (r != PAM_SUCCESS) {
pam_syslog(handle, LOG_ERR, "Failed to install session fd.");
- close_nointr_nofail(session_fd);
+ safe_close(session_fd);
return r;
}
}
diff --git a/src/login/test-inhibit.c b/src/login/test-inhibit.c
index 70b8314e3f..70780c30af 100644
--- a/src/login/test-inhibit.c
+++ b/src/login/test-inhibit.c
@@ -101,11 +101,11 @@ int main(int argc, char*argv[]) {
assert(fd2 >= 0);
print_inhibitors(bus);
- close_nointr_nofail(fd1);
+ safe_close(fd1);
sleep(1);
print_inhibitors(bus);
- close_nointr_nofail(fd2);
+ safe_close(fd2);
sleep(1);
print_inhibitors(bus);