+ if (error)
+ goto out;
+
+ /*
+ * If S_CTTYREF is set, some other thread did an open
+ * and was able to set the flag, now perform a close, else
+ * set the flag.
+ */
+ session_lock(sessp);
+ if (cttyflag == (sessp->s_flags & S_CTTYREF))
+ sessp->s_flags |= S_CTTYREF;
+ else
+ doclose = 1;
+ session_unlock(sessp);
+
+ /*
+ * We have to take a reference here to make sure a close
+ * gets called during revoke. Note that once a controlling
+ * tty gets opened by this driver, the only way close will
+ * get called is when the session leader , whose controlling
+ * tty is ttyvp, exits and vnode is revoked. We cannot
+ * redirect close from this driver because underlying controlling
+ * terminal might change and close may get redirected to a
+ * wrong vnode causing panic.
+ */
+ if (doclose) {
+ devsw_unlock(dev, S_IFCHR);
+ VNOP_CLOSE(ttyvp, flag, &context);
+ devsw_lock(dev, S_IFCHR);
+ } else {
+ error = vnode_ref(ttyvp);
+ }