title: how to open /dev/console in C
body: <p>I was reading wayland/weston code, the setting up tty part. I found it tries to acquire an available tty for doing KMS and start windows.</p>
  <p>This is how it does:</p>
  <pre><code>    if (!wl-&gt;new_user) {
              wl-&gt;tty = STDIN_FILENO;
      } else if (tty) {
              t = ttyname(STDIN_FILENO);
              if (t &amp;&amp; strcmp(t, tty) == 0)
                      wl-&gt;tty = STDIN_FILENO;
              else
                      wl-&gt;tty = open(tty, O_RDWR | O_NOCTTY);
      } else {
              int tty0 = open(&quot;/dev/tty0&quot;, O_WRONLY | O_CLOEXEC);
              char filename[16];
  
              if (tty0 &lt; 0)
                      error(1, errno, &quot;could not open tty0&quot;);
  
              if (ioctl(tty0, VT_OPENQRY, &amp;wl-&gt;ttynr) &lt; 0 || wl-&gt;ttynr == -1)
                      error(1, errno, &quot;failed to find non-opened console&quot;); 
  
              snprintf(filename, sizeof filename, &quot;/dev/tty%d&quot;, wl-&gt;ttynr);
              wl-&gt;tty = open(filename, O_RDWR | O_NOCTTY);
              close(tty0);
      }
  </code></pre>
  <p>in src/weston-launch.c.</p>
  <p>It tries to <code>open('/dev/tty0')</code> and find a tty that available if no tty is specified.</p>
  <p>But you can't do that, neither <code>/dev/tty0</code> nor 'available tty' belongs to you. I tested with my simpler version. And of course I couldn't open <code>/dev/tty0</code>.</p>
  <p>Do you guys know how this magic is done?</p>