summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Boccassi <luca.boccassi@microsoft.com>2020-12-09 14:27:03 +0000
committerGitHub <noreply@github.com>2020-12-09 14:27:03 +0000
commit141d3a14d484a747490d0409b6ca45ad03f7d812 (patch)
treefa1ad6f1415f41f6b44761c01ff8b95d09519b2c
parentecd3c86b60655d892f5ec68549226cc5fa4be818 (diff)
parentf2835dd4a69996807f40efaed857287fdca8de93 (diff)
downloadsystemd-141d3a14d484a747490d0409b6ca45ad03f7d812.tar.gz
Merge pull request #17882 from poettering/logind-async-close
logind: fix closing of button input devices
-rw-r--r--src/basic/async.h6
-rw-r--r--src/login/logind-button.c30
2 files changed, 13 insertions, 23 deletions
diff --git a/src/basic/async.h b/src/basic/async.h
index 9ada32c994..e0bbaa5658 100644
--- a/src/basic/async.h
+++ b/src/basic/async.h
@@ -1,7 +1,13 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
+#include <sys/types.h>
+
+#include "macro.h"
+
int asynchronous_job(void* (*func)(void *p), void *arg);
int asynchronous_sync(pid_t *ret_pid);
int asynchronous_close(int fd);
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(int, asynchronous_close);
diff --git a/src/login/logind-button.c b/src/login/logind-button.c
index 60de2dccad..0ee6702068 100644
--- a/src/login/logind-button.c
+++ b/src/login/logind-button.c
@@ -8,12 +8,12 @@
#include "sd-messages.h"
#include "alloc-util.h"
+#include "async.h"
#include "fd-util.h"
#include "logind-button.h"
#include "missing_input.h"
#include "string-util.h"
#include "util.h"
-#include "async.h"
#define CONST_MAX5(a, b, c, d, e) CONST_MAX(CONST_MAX(a, b), CONST_MAX(CONST_MAX(c, d), e))
@@ -60,11 +60,7 @@ void button_free(Button *b) {
sd_event_source_unref(b->io_event_source);
sd_event_source_unref(b->check_event_source);
- if (b->fd >= 0)
- /* If the device has been unplugged close() returns
- * ENODEV, let's ignore this, hence we don't use
- * safe_close() */
- (void) asynchronous_close(b->fd);
+ asynchronous_close(b->fd);
free(b->name);
free(b->seat);
@@ -72,19 +68,9 @@ void button_free(Button *b) {
}
int button_set_seat(Button *b, const char *sn) {
- char *s;
-
assert(b);
- assert(sn);
- s = strdup(sn);
- if (!s)
- return -ENOMEM;
-
- free(b->seat);
- b->seat = s;
-
- return 0;
+ return free_and_strdup(&b->seat, sn);
}
static void button_lid_switch_handle_action(Manager *manager, bool is_edge) {
@@ -327,14 +313,14 @@ static int button_set_mask(const char *name, int fd) {
}
int button_open(Button *b) {
- _cleanup_close_ int fd = -1;
+ _cleanup_(asynchronous_closep) int fd = -1;
const char *p;
char name[256];
int r;
assert(b);
- b->fd = safe_close(b->fd);
+ b->fd = asynchronous_close(b->fd);
p = strjoina("/dev/input/", b->name);
@@ -345,12 +331,10 @@ int button_open(Button *b) {
r = button_suitable(fd);
if (r < 0)
return log_warning_errno(r, "Failed to determine whether input device %s is relevant to us: %m", p);
- if (r == 0) {
- b->fd = TAKE_FD(fd);
+ if (r == 0)
return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL),
"Device %s does not expose keys or switches relevant to us, ignoring.", p);
- }
-
+
if (ioctl(fd, EVIOCGNAME(sizeof name), name) < 0)
return log_error_errno(errno, "Failed to get input name for %s: %m", p);