-#ifndef DOS_NT
- int pgid = tcgetpgrp (fd); /* If tcgetpgrp succeeds, fd is the ctty. */
- if (pgid != -1)
- {
-#if defined (USG5)
- setpgrp ();
- no_controlling_tty = 1;
-#elif defined (CYGWIN)
- setsid ();
- no_controlling_tty = 1;
-#else
-#ifdef TIOCNOTTY /* Try BSD ioctls. */
- sigset_t blocked;
- sigemptyset (&blocked);
- sigaddset (&blocked, SIGTTOU);
- pthread_sigmask (SIG_BLOCK, &blocked, 0);
- fd = emacs_open (DEV_TTY, O_RDWR, 0);
- if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
- {
- no_controlling_tty = 1;
- }
- if (fd != -1)
- emacs_close (fd);
- pthread_sigmask (SIG_UNBLOCK, &blocked, 0);
-#else
-# error "Unknown system."
-#endif /* ! TIOCNOTTY */
-#endif /* ! USG */
+ /* If tcgetpgrp succeeds, fd is the controlling terminal,
+ so dissociate it by invoking setsid. */
+ if (tcgetpgrp (fd) >= 0 && setsid () < 0)
+ {
+#ifdef TIOCNOTTY
+ /* setsid failed, presumably because Emacs is already a process
+ group leader. Fall back on the obsolescent way to dissociate
+ a controlling tty. */
+ block_tty_out_signal ();
+ ioctl (fd, TIOCNOTTY, 0);
+ unblock_tty_out_signal ();
+#endif