Inappropriate ioctl for device

Sample program tty, this will be get called from my script test_script.

#include <stdio.h>
#include <unistd.h>
#define TTY_NAME_MAX        32
#define STDIN_FILENO    0   /* Standard input.  */

int main(void) {
    int     ret = 0;
    char    local_device_file[TTY_NAME_MAX];
        printf("\npid = %d\n",getpid());
        ret = ttyname_r(STDIN_FILENO, local_device_file, TTY_NAME_MAX);
        printf("ret = %d, local_device_file = %s\n",ret,local_device_file);
        return 0;
}

--------------------------------------------------------------------
Working condition:

test_script:

echo "running test script"
/users/ghonnaia/tty

Output:

bgl-xdm-140:199> ./test_script
running test script
pid = 6249
ret = 0, local_device_file = /dev/pts/399

--------------------------------------------------------------------
Fail Condition:

test_script:

echo "running test script"
/users/ghonnaia/tty &

Output:

bgl-xdm-140:197> ./test_script
running test script
bgl-xdm-140:198> 
pid = 5951
ret = 25, local_device_file = 

Return value 25 means Inappropriate ioctl for device.

Can anyone please explain me why this error comes when we make saple program to run in backgound in a script.

Thanks,
Gajendra

Because a background program doesn't control the terminal, only the foreground.

I disagree with Corona688. Being in the foreground or background shouldn't affect what ioctl's a device supports.

It is my belief that the cause of the malfunction is that STDIN is not a terminal because the shell is redirecting STDIN from /dev/null when it runs the process (or pipeline) in the background.

I don't know what system you're running, but on a circa 2007 Debian Linux install, /usr/include/asm-generic/errno-base.h (which is the ultimate destination of the #include breadcrumb trail beginning at /usr/include/errno.h) has this to say:

#define ENOTTY          25      /* Not a typewriter */

Regards,
Alister

Yes I too agree with alister...only a foreground process can read from the terminal preventing multiple processes from competing for terminal input as there's only one process in the foreground at any instant...

To be more precise, any process in the foreground process group can read from the terminal, and there can be many of them in that group.

Regards,
Alister

Thanks you all for replying with good information. Now I understand the problem and will avoid calling the app in background.

Alister, I'm using a customized Linux OS for embedded application.

#define ENOTTY 25 /* Inappropriate I/O control operation */

Regards,
Gajendra

You can call it in the background, but to keep the tty as standard input requires overriding the shell's redirection. Note that if the process tries to read while not in the foreground process group, it will be signaled and put to sleep.

Not knowing what you are trying to achieve, it's impossible to say if I just gave you good or bad advice. Still, I wanted to mention that it is possible to workaround this ENOTTY.

Regards,
Alister

Hi Alister,

My script calls the program to suspend console timeout. Issue seen when program run in background in script. What I have to modify in the script to avoid the issue. How to override the shell's redirectrion to tty?

Thanks,
Gajendra