- if (s->vtfd < 0)
- return;
-
- /* The virtual terminal can potentially be entering in hung-up state at any time
- * depending on when the controlling process exits.
- *
- * If the controlling process exits while we're restoring the virtual terminal,
- * the VT will enter in hung-up state and we'll fail at restoring it. To prevent
- * this case, we kick off the current controlling process (if any) in a child
- * process so logind doesn't play around with tty ownership.
- *
- * If the controlling process already exited, getting a fresh handle to the
- * virtual terminal reset the hung-up state. */
- r = safe_fork("(logind)", FORK_REOPEN_LOG|FORK_CLOSE_ALL_FDS|FORK_RESET_SIGNALS|FORK_WAIT|FORK_LOG, &pid);
- if (r == 0) {
- char path[sizeof("/dev/tty") + DECIMAL_STR_MAX(s->vtnr)];
- int vt;
-
- /* We must be a session leader in order to become the controlling process. */
- pid = setsid();
- if (pid < 0) {
- log_error_errno(errno, "Failed to become session leader: %m");
- _exit(EXIT_FAILURE);
- }
+ /* We need to get a fresh handle to the virtual terminal,
+ * since the old file-descriptor is potentially in a hung-up
+ * state after the controlling process exited; we do a
+ * little dance to avoid having the terminal be available
+ * for reuse before we've cleaned it up.
+ */
+ old_fd = TAKE_FD(s->vtfd);