简体   繁体   中英

Why does fopen(“/dev/null”, “w”) returns standard file descriptor on AIX?

We have a code as part of bigger application where we are trying to fopen() a /dev/null and return the file pointer. This code use to return a non-standard file descriptor earlier ( probably with AIX 6.1 or lower version ). It appears that after migrating/upgrading to AIX 7.1, the above code returns a standard file descriptor.

I was wondering if there is any fundamental change that happened to AIX 7.1 version that could potentially affect the fopen() system call?

I believe there was no source code change to the application that could result in the above change in fopen() output.

I tried with a simple, sample code (outside of my application) that does fopen on /dev/null; this seems to return fd of 3 always. But in my application, it returns 1. So, I am not able to understand where the problem is.

FILE *fp = fopen("/dev/null", "w");
fprintf(stdout, "fd = %d\n", fileno(fp)); // --> this prints 1 in my application, but print 3 in a sample code.

Sorry about the confusion. The below is the actual function:

FILE* GetDevNull()
{
    FILE* filesToClose[3];
    int count = 0;

    FILE* fp = fopen("/dev/null", "a");

    while(fp && fileno(fp) <= 3)
    {
        filesToClose[count++]=fp;
        fp = fopen("/dev/null", "a"); // this returns fileno(fp)=1 (STDOUT)
    }

    while(count)
        fclose(filesToClose[--count]); // STDOUT is closed here

        return fp;
}

As I understand, in case fopen returns 0, 1 or 2, they just cache the corresponding file pointers and then they close before returning an other file pointer ( that has a fd more than 2 ).

The background is this:

FILE* fplog = fopen("my.log", "w");
dup2(fileno(fplog), STDOUT_FILENO); // happening else where in the application

After the above, following is getting called.

FILE* fpnull = GetDevNull();

After the above call to GetDevNull(), now STDOUT doesn't point to my.log but to /dev/null.

So, the question is, why fopen() in GetDevNull() is returning 1 ? This appears to be happening after we migrated to AIX 7.1. So, I was wondering if there was any major update to AIX 7.1 that could have affected this?

I tried with a simple, sample code (outside of my application) that does fopen on /dev/null; this seems to return fd of 3 always. But in my application, it returns 1. So, I am not able to understand where the problem is.

If fopen("/any/path", "any mode") succeeds and the associated file has file number 1, then it must be the case that the program previously closed file number 1, possibly by closing its stdout stream, or that it started started with it closed. The file numbers for the standard streams are not inherently special. If one of them is available when you open a file then the file will get that number for its file descriptor, because the system uses the lowest available file descriptor number. This is intended behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM