Inappropriate ioctl for device

Dear all,

Problem goes like this:

I have a shell script which when run manually runs perfectly.
When same script is executed through a job schdeduler I get an error as Inappropriate ioctl for device and the script fails.

This problems seems quite guiling to me.
Any clues are heartly welcomed.

Thanks
Rishi

does your script have any "read" statements? is there any reason why the script's stdin or stdout would need to be connected to a terminal?

My script is not at all interactive and does not read from stdin.

yeah it do write to some /tmp directory while sorting the data.
And to my understanding this happens just when it tries to write to this mount.

Thanks for helping framing the problem more clear. hope this will help everyone to figure out the reason why it does happen?

Thanks
Rishi

You are doing something in your script directly or indirectly that does indeed require a tty, but it is not a read nor a write. Put some echo statement in your script "echo at point 1", "echo at point 2", etc until you see the statement that fails.

Thanks Perderabo for going over the question.
As the code exists in production and we cannot change code without lots of approvals.

Will like to elaborate the problem more clearly see if you can help.

We have a script say run_job.ksh which is executed from a scheduler.
This scripts calls a C executable, which itself invokes sort command.

The snippet of the code calling sort command is like this:

 sprintf(tmpSortFileName, "/tmp/temp%s", "tempIRBPAVAInputFile");
 
    /* Sort by carrier ID, company #, bill to RAO, statement date, BTN, ??? */
    sprintf(sortcmd,
             "sort -k 1.132,1.135 -k 1.118,1.119 -k 1.118,1.119 "\
                  "-k 1.112,1.114 -k 1.104,1.111 -k 1.142,1.151 "\
                  "-k 1.155,1.162 %s > %s", inputFileName, tmpSortFileName);
    system(sortcmd);
 
    if(errno)
    {
       fprintf(stderr,
              "Error sorting the input file %s...\nExiting...\n",
                inputFileName);
       exit(ERR);
    }

if same code we execute through a telnet session it succeeds while if we invoke the executable through scheduler we are thrown the error present in "if" condition i.e. Error sorting the input file

This is the sequence of error I receive in scheduler logs:

: Inappropriate ioctl for device
Error sorting the input file /tmp/tempIRBPAVAInputFile...
Exiting...

Any help will be highly appreciated.

That piece of c code is not the problem. Did you surround the this program with echo statements to be sure that it is causing the problem? If so, look further in the program for some tty specific operation.

yes, as I told I am not in position to add echo statments but there do exists a echo statment and data echoed is perfectly visible in log.

After tht echo call only the C executable is invoker with appropiate arguments.

Why i am worried with this pieve of code specifically is we have two functioncalls sortTempInputFile and sortInputFile.

First call "sortInputFile" is always successfull while second call where only filename is different fails "sortTempInputFile"

before invoking the sort command we even check the error code.

complete code of function is:

void sortTempInputFile(char *inputFileName)
{
    char sortcmd         [1024] = "\0";
    char tmpSortFileName [1024] = "\0";
 
    iFileName[0] = '\0';
 
    if(errno)
    {
       perror(" ");
    }
 
    sprintf(tmpSortFileName, "/tmp/temp%s", "tempIRBPAVAInputFile");
 
    /* Sort by carrier ID, company #, bill to RAO, statement date, BTN, ??? */
    sprintf(sortcmd,
             "sort -k 1.132,1.135 -k 1.118,1.119 -k 1.118,1.119 "\
                  "-k 1.112,1.114 -k 1.104,1.111 -k 1.142,1.151 "\
                  "-k 1.155,1.162 %s > %s", inputFileName, tmpSortFileName);
    system(sortcmd);
 
    if(errno)
    {
       fprintf(stderr,
              "Error sorting the input file %s...\nExiting...\n",
                inputFileName);
       exit(ERR);
    }
 
    sprintf(sortcmd, "chmod 777 %s", tmpSortFileName);
    system(sortcmd);
 
    if(errno)
    {
       fprintf(stderr,"%s%s\n",
               "Error changing permissions for the temp input file ...",
               "Exiting...");
 
       exit(ERR);
    }
 
    sprintf(sortcmd, "mv -f %s %s", tmpSortFileName, inputFileName);
    system(sortcmd);
 
    if(errno)
    {
       fprintf(stderr,"Error moving the input file ...\nExiting...\n");
       exit(ERR);
    }
 
}
 

I greped for tty in all the scripts but could not find one...is there anyway we will be connecting to tty?

You are not connecting to a tty. Somewhere you are assuming that you are connected to a tty and doing something that only works with a tty.

I do not see a problem with that function. Since your first function seems to work, it is more probable that the error is there. My guess is that the function you posted is never even invoked. After the first function sorts your file, what else does it do? What happens between the first function call and the second function call? Does any module include termio.h or termios.h? Anything requiring them could be the problem. And you of course have searched for an explicit call to ioctl(), right?

another possibility is....... is 'sort' what you think it is? Is it a possibility that there's another 'sort' in your $PATH that preceeds say '/bin/sort'?

I checked the code and there is no place i am connecting to tty. Though many functions are there to fprintf writing to stderr.

I am not using ioctl function anywhere. neither i used termio.h or termios.h.

To answer vergesh question, i double checked for path invoking anothe sort. So there is no possibility this could be a reason.

I will like to go with Perderabo statment that somewhere before itself the errno is getting reset to some error value and when during the call to function sortTempInputfile we first trap to check errno we exit.

Please let me know if there exists any other reasons for ioctl error thrown by unix?

regards
Rishi

Only reason is an inappropriate ioctl call. Since you can't change anything to debug this, there is nothing further I can suggest. You will just have to live it I guess.

Only when I issued "/bin/bash < SCRIPTFILE" to execute the scriptfile will I get error message "stty: standard input: Inappropriate ioctl for device". Whereas, when I "chmod" the scriptfile to a executable one and executed it, error message disappered and scriptfile was executed successful.

Any link between these two problems?