}
static void session_restore_vt(Session *s) {
- int r, vt, old_fd;
+ int r;
- /* 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);
+ r = vt_restore(s->vtfd);
+ if (r == -EIO) {
+ int vt, old_fd;
- vt = session_open_vt(s);
- safe_close(old_fd);
+ /* It might happen if the controlling process exited before or while we were
+ * restoring the VT as it would leave the old file-descriptor in a hung-up
+ * state. In this case let's retry with a fresh handle to the virtual terminal. */
- if (vt < 0)
- return;
+ /* 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);
+
+ vt = session_open_vt(s);
+ safe_close(old_fd);
+
+ if (vt >= 0)
+ r = vt_restore(vt);
+ }
- r = vt_restore(vt);
if (r < 0)
log_warning_errno(r, "Failed to restore VT, ignoring: %m");