Search code examples
clinuxdaemonioctl

How do I use ioctl in a daemon?


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;
}

Solution

  • 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.