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 ?

Li-Guangda
  • 257
  • 2
  • 3
  • 11

2 Answers2

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.

user10489
  • 6,740
  • What do you mean "been assigned to it" ? – Li-Guangda Feb 27 '22 at 06: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 '22 at 06: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.