0

I followed this link to create a daemon in Linux. It worked fine until I started calling ioctl. It seems like any call to ioctl is being completely ignored. I'm using ioctl to set the terminal and keyboard mode. The sample code is below. The code works without running it as a daemon. Do I have to do something else to get ioctl working?

int main(int argc, char **argv)
{
    skeleton_daemon(); // Function is the exact same as in the link
    int term = open("/dev/tty1", O_RDWR);

    syslog(LOG_NOTICE, "First daemon started.");
    sleep(5);
    

    // Should freeze the terminal and keyboard input but doesn't when running as daemon
    ioctl(term, KDSETMODE, KD_GRAPHICS);
    ioctl(term, KDSKBMODE, K_OFF);
    ioctl(term, 0x4B51, 1);

    sleep(5);

    ioctl(term, KDSETMODE, KD_TEXT);
    ioctl(term, KDSKBMODE, previousMode);
    ioctl(term, 0x4B51, 0);

    syslog(LOG_NOTICE, "First daemon terminated.");
    closelog();
    return EXIT_SUCCESS;
}
person
  • 313
  • 1
  • 10
  • As explained in @Glärbo's answer, a daemon has no controlling tty. It is detached from it. The link you refer to, detaches it. Even if it has one, this controlling terminal would be "/dev/tty" which is a synonym of the process controlling terminal (Not the hard coded value /dev/tty1). – Rachid K. Mar 08 '21 at 07:22

1 Answers1

1

Daemons do not have a controlling terminal (or a terminal at all), and usually have their standard input redirected from /dev/null. Their standard output and standard error is either redirected to /dev/null, or to a log file.

Even if the daemon had a terminal, those ioctls would only affect that terminal, and not other users.

Glärbo
  • 146
  • 2
  • But that’s why I reopen the terminal. Shouldn’t it be able to affect it if I reopen it? Say if I use the write syscall with the term fd it will write to the terminal as a daemon so why doesn’t octal work? – person Mar 08 '21 at 16:01
  • @person: "Affect"? It depends on exactly what you intend to do. Console ioctls only affect things if that console is the current virtual console. If you look at X.org or Wayland, they use either [openvt](https://www.man7.org/linux/man-pages/man1/openvt.1.html) to get the first unused virtual terminal (with the -s switch) or [chvt](https://www.man7.org/linux/man-pages/man1/chvt.1.html) to switch to a dedicated virtual terminal (usually `/dev/tty7`). – Glärbo Mar 09 '21 at 01:55
  • Oh, I see. ioctl only works on terminals that the process controls. Is it okay to leave out the setsid function to keep the terminal? Because when I do that it works exactly as I want it to. – person Mar 09 '21 at 03:52
  • @person: No, that's a separate issue. If you want a terminal to be your controlling terminal, use the TIOCSCTTY ioctl (see `man 4 tty_ioctl`). You'll want to do a (fork and in the child) setsid() to make sure current process is a process group leader of a new session without a controlling terminal, then open the tty, and use the TIOCSCTTY ioctl on that tty to make it the controlling terminal. For many console ioctls, that is not sufficient, as that particular virtual console must also be the current one for the ioctls to work. As I wrote, it depends on exactly what you are trying to do. – Glärbo Mar 09 '21 at 09:51
  • Ah that worked for exactly what I needed it for! – person Mar 09 '21 at 16:55