0

Given that a simple program, as following:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char **argv)
{
  int fd = -1;

  if (access("/dev/tty", F_OK) == 0)
    {
      fd = open("/dev/tty", O_RDWR);
      if (fd == -1)
        perror("open() :");
    }

   return 0;
}

compile and make it as init (the frist process invoked by kernel), then reboot, but it would get the result:

open() :No such device or address

How to explain this ?

3
  • This sounds like an X Y problem. Why do you want to open /dev/tty ? Explain that, and you will get a better answer.
    – user10489
    Feb 27, 2022 at 6:51
  • The idea is to make the /dev/tty as the controlling terminal of the process ...
    – Li-Guangda
    Feb 27, 2022 at 7:12
  • No. /dev/tty is not a terminal. Use another device.
    – user10489
    Feb 27, 2022 at 12:29

2 Answers 2

2

/dev/tty is a reference to the controlling terminal for a process. But in order for it to have a controlling terminal, a real terminal has to be opened and been assigned to the process.

Perhaps instead of opening /dev/tty you should open /dev/tty0 or /dev/console or /dev/tty1

If a process does not have a controlling terminal, and it opens a tty or pty, if nothing else is using that tty as a controlling terminal already, then the process will be assigned that tty as a controlling terminal. It is also possible to open a tty and assign it as the controlling terminal with an IOCTL if the conditions are correct.

However /dev/tty is not a real tty, so these conditions don't apply to it.

2
  • What do you mean "been assigned to it" ?
    – Li-Guangda
    Feb 27, 2022 at 6:40
  • expanded answer. But you're focusing on the wrong part. You have to open a real tty first and /dev/tty is not a real tty and doesn't point to one until you have a controlling terminal.
    – user10489
    Feb 27, 2022 at 6:48
1

Answer of @user110489 works for me.

What you have to do is replace the /dev/tty with /dev/console.

My system Configs:

OS - windows 10
IDE - VSCode
Error arrise in - husky hooks.
3
  • 4
    Please don’t post a new answer just to say that another answer worked for you — especially if you aren't using Unix/Linux. May 26, 2022 at 9:50
  • 1
    This probably should have been a comment. /dev/console sometimes is another special tty
    – user10489
    May 26, 2022 at 11:09
  • This is posted because I came here searching for husky hooks' solution. And I guess many other may have faced the same error. May 31, 2022 at 5:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .