- io = pa_iochannel_new(c->mainloop, STDIN_FILENO, STDOUT_FILENO);
- assert(io);
- pa_iochannel_set_noclose(io, 1);
-
- m->userdata = pa_cli_new(c, io, m);
- assert(m->userdata);
+ /* We try to open the controlling tty anew here. This has the
+ * benefit of giving us a new fd that doesn't share the O_NDELAY
+ * flag with fds 0, 1, or 2. Since pa_iochannel_xxx needs O_NDELAY
+ * on its fd using those fds directly could set O_NDELAY which
+ * fprintf() doesn't really like, resulting in truncated output
+ * of log messages, particularly because if stdout and stderr are
+ * dup'ed they share the same O_NDELAY, too. */
+
+#ifndef OS_IS_WIN32
+ if ((fd = pa_open_cloexec("/dev/tty", O_RDWR|O_NONBLOCK, 0)) >= 0) {
+ io = pa_iochannel_new(m->core->mainloop, fd, fd);
+ pa_log_debug("Managed to open /dev/tty.");
+ }
+ else
+#endif
+ {
+ io = pa_iochannel_new(m->core->mainloop, STDIN_FILENO, STDOUT_FILENO);
+ pa_iochannel_set_noclose(io, true);
+ pa_log_debug("Failed to open /dev/tty, using stdin/stdout fds instead.");
+ }