-static int manager_attach_fds(Manager *m) {
- _cleanup_strv_free_ char **fdnames = NULL;
- int n, i, fd;
-
- /* 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. */
-
- n = sd_listen_fds_with_names(true, &fdnames);
- if (n <= 0)
- return n;
-
- for (i = 0; i < n; i++) {
- struct stat st;
- SessionDevice *sd;
- Session *s;
- char *id;
-
- fd = SD_LISTEN_FDS_START + i;
-
- id = startswith(fdnames[i], "session-");
- if (!id)
- continue;
-
- s = hashmap_get(m->sessions, id);
- if (!s) {
- /* If the session doesn't exist anymore, the associated session
- * device attached to this fd doesn't either. Let's simply close
- * this fd. */
- log_debug("Failed to attach fd for unknown session: %s", id);
- close_nointr(fd);
- continue;
- }
-
- if (fstat(fd, &st) < 0) {
- /* The device is allowed to go away at a random point, in which
- * case fstat failing is expected. */
- log_debug_errno(errno, "Failed to stat device fd for session %s: %m", id);
- close_nointr(fd);
- continue;
- }
-
- sd = hashmap_get(s->devices, &st.st_rdev);
- if (!sd) {
- /* Weird we got an fd for a session device which wasn't
- * recorded in the session state file... */
- log_warning("Got fd for missing session device [%u:%u] in session %s",
- major(st.st_rdev), minor(st.st_rdev), s->id);
- close_nointr(fd);
- continue;
- }
-
- log_debug("Attaching fd to session device [%u:%u] for session %s",
- major(st.st_rdev), minor(st.st_rdev), s->id);
-
- session_device_attach_fd(sd, fd, s->was_active);
- }
-
- return 0;
-}
-