login: use helper functions for fd store

This commit is contained in:
Yu Watanabe 2022-08-15 20:05:32 +09:00
parent 50e23ac667
commit 2720b6f23c
2 changed files with 8 additions and 28 deletions

View file

@ -10,6 +10,7 @@
#include "alloc-util.h"
#include "bus-util.h"
#include "daemon-util.h"
#include "fd-util.h"
#include "logind-session-dbus.h"
#include "logind-session-device.h"
@ -376,19 +377,11 @@ error:
}
void session_device_free(SessionDevice *sd) {
int r;
assert(sd);
/* Make sure to remove the pushed fd. */
if (sd->pushed_fd) {
r = sd_notifyf(false,
"FDSTOREREMOVE=1\n"
"FDNAME=session-%s-device-%u-%u",
sd->session->id, major(sd->dev), minor(sd->dev));
if (r < 0)
log_warning_errno(r, "Failed to remove file descriptor from the store, ignoring: %m");
}
if (sd->pushed_fd)
(void) notify_remove_fd_warnf("session-%s-device-%u-%u", sd->session->id, major(sd->dev), minor(sd->dev));
session_device_stop(sd);
session_device_notify(sd, SESSION_DEVICE_RELEASE);
@ -469,7 +462,6 @@ unsigned session_device_try_pause_all(Session *s) {
}
int session_device_save(SessionDevice *sd) {
_cleanup_free_ char *m = NULL;
const char *id;
int r;
@ -489,13 +481,7 @@ int session_device_save(SessionDevice *sd) {
id = sd->session->id;
assert(*(id + strcspn(id, "-\n")) == '\0');
r = asprintf(&m, "FDSTORE=1\n"
"FDNAME=session-%s-device-%u-%u\n",
id, major(sd->dev), minor(sd->dev));
if (r < 0)
return r;
r = sd_pid_notify_with_fds(0, false, m, &sd->fd, 1);
r = notify_push_fdf(sd->fd, "session-%s-device-%u-%u", id, major(sd->dev), minor(sd->dev));
if (r < 0)
return r;

View file

@ -438,7 +438,7 @@ static int deliver_fd(Manager *m, const char *fdname, int fd) {
static int manager_attach_fds(Manager *m) {
_cleanup_strv_free_ char **fdnames = NULL;
int r, n;
int n;
/* Upon restart, PID1 will send us back all fds of session devices that we previously opened. Each
* file descriptor is associated with a given session. The session ids are passed through FDNAMES. */
@ -455,15 +455,9 @@ static int manager_attach_fds(Manager *m) {
if (deliver_fd(m, fdnames[i], fd) >= 0)
continue;
/* Hmm, we couldn't deliver the fd to any session device object? If so, let's close the fd */
safe_close(fd);
/* Remove from fdstore as well */
r = sd_notifyf(false,
"FDSTOREREMOVE=1\n"
"FDNAME=%s", fdnames[i]);
if (r < 0)
log_warning_errno(r, "Failed to remove file descriptor from the store, ignoring: %m");
/* Hmm, we couldn't deliver the fd to any session device object? If so, let's close the fd
* and remove it from fdstore. */
close_and_notify_warn(fd, fdnames[i]);
}
return 0;