summaryrefslogtreecommitdiffstats
path: root/src/login/logind-session-device.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/login/logind-session-device.c')
-rw-r--r--src/login/logind-session-device.c54
1 files changed, 26 insertions, 28 deletions
diff --git a/src/login/logind-session-device.c b/src/login/logind-session-device.c
index 44d8d52..c924f1e 100644
--- a/src/login/logind-session-device.c
+++ b/src/login/logind-session-device.c
@@ -11,6 +11,7 @@
#include "alloc-util.h"
#include "bus-util.h"
#include "daemon-util.h"
+#include "device-util.h"
#include "fd-util.h"
#include "logind-session-dbus.h"
#include "logind-session-device.h"
@@ -114,7 +115,8 @@ static int sd_drmdropmaster(int fd) {
}
static int session_device_open(SessionDevice *sd, bool active) {
- int fd, r;
+ _cleanup_close_ int fd = -EBADF;
+ int r;
assert(sd);
assert(sd->type != DEVICE_TYPE_UNKNOWN);
@@ -132,10 +134,8 @@ static int session_device_open(SessionDevice *sd, bool active) {
/* Weird legacy DRM semantics might return an error even though we're master. No way to detect
* that so fail at all times and let caller retry in inactive state. */
r = sd_drmsetmaster(fd);
- if (r < 0) {
- (void) close_nointr(fd);
+ if (r < 0)
return r;
- }
} else
/* DRM-Master is granted to the first user who opens a device automatically (ughh,
* racy!). Hence, we just drop DRM-Master in case we were the first. */
@@ -153,7 +153,7 @@ static int session_device_open(SessionDevice *sd, bool active) {
break;
}
- return fd;
+ return TAKE_FD(fd);
}
static int session_device_start(SessionDevice *sd) {
@@ -239,22 +239,21 @@ static void session_device_stop(SessionDevice *sd) {
}
static DeviceType detect_device_type(sd_device *dev) {
- const char *sysname, *subsystem;
- DeviceType type = DEVICE_TYPE_UNKNOWN;
+ const char *sysname;
- if (sd_device_get_sysname(dev, &sysname) < 0 ||
- sd_device_get_subsystem(dev, &subsystem) < 0)
- return type;
+ if (sd_device_get_sysname(dev, &sysname) < 0)
+ return DEVICE_TYPE_UNKNOWN;
- if (streq(subsystem, "drm")) {
+ if (device_in_subsystem(dev, "drm")) {
if (startswith(sysname, "card"))
- type = DEVICE_TYPE_DRM;
- } else if (streq(subsystem, "input")) {
+ return DEVICE_TYPE_DRM;
+
+ } else if (device_in_subsystem(dev, "input")) {
if (startswith(sysname, "event"))
- type = DEVICE_TYPE_EVDEV;
+ return DEVICE_TYPE_EVDEV;
}
- return type;
+ return DEVICE_TYPE_UNKNOWN;
}
static int session_device_verify(SessionDevice *sd) {
@@ -315,31 +314,28 @@ static int session_device_verify(SessionDevice *sd) {
if (sd->device->seat != sd->session->seat)
return -EPERM;
- sd->node = strdup(node);
- if (!sd->node)
- return -ENOMEM;
-
- return 0;
+ return strdup_to(&sd->node, node);
}
-int session_device_new(Session *s, dev_t dev, bool open_device, SessionDevice **out) {
+int session_device_new(Session *s, dev_t dev, bool open_device, SessionDevice **ret) {
SessionDevice *sd;
int r;
assert(s);
- assert(out);
if (!s->seat)
return -EPERM;
- sd = new0(SessionDevice, 1);
+ sd = new(SessionDevice, 1);
if (!sd)
return -ENOMEM;
- sd->session = s;
- sd->dev = dev;
- sd->fd = -EBADF;
- sd->type = DEVICE_TYPE_UNKNOWN;
+ *sd = (SessionDevice) {
+ .session = s,
+ .dev = dev,
+ .fd = -EBADF,
+ .type = DEVICE_TYPE_UNKNOWN,
+ };
r = session_device_verify(sd);
if (r < 0)
@@ -370,7 +366,9 @@ int session_device_new(Session *s, dev_t dev, bool open_device, SessionDevice **
LIST_PREPEND(sd_by_device, sd->device->session_devices, sd);
- *out = sd;
+ if (ret)
+ *ret = sd;
+
return 0;
error: