简体   繁体   English

为什么fopen(“ / dev / null”,“ w”)在AIX上返回标准文件描述符?

[英]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. 作为较大应用程序的一部分,我们有一个代码,其中我们尝试fopen() / dev / null并返回文件指针。 This code use to return a non-standard file descriptor earlier ( probably with AIX 6.1 or lower version ). 该代码用于更早地返回非标准文件描述符(可能在AIX 6.1或更低版本中)。 It appears that after migrating/upgrading to AIX 7.1, the above code returns a standard file descriptor. 看来,在迁移/升级到AIX 7.1之后,以上代码返回了一个标准文件描述符。

I was wondering if there is any fundamental change that happened to AIX 7.1 version that could potentially affect the fopen() system call? 我想知道AIX 7.1版本是否发生了任何根本变化,可能会影响fopen()系统调用?

I believe there was no source code change to the application that could result in the above change in fopen() output. 我相信没有对应用程序进行源代码更改,不会导致fopen()输出的上述更改。

I tried with a simple, sample code (outside of my application) that does fopen on /dev/null; 我尝试了一个简单的示例代码(在我的应用程序外部),该代码在/ dev / null上打开了; this seems to return fd of 3 always. 这似乎总是返回fd为3。 But in my application, it returns 1. So, I am not able to understand where the problem is. 但是在我的应用程序中,它返回1。因此,我无法理解问题出在哪里。

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 ). 据我了解,万一fopen返回0、1或2,它们只是缓存相应的文件指针,然后在返回另一个文件指针(fd大于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. 在上面对GetDevNull()的调用之后,现在STDOUT不再指向my.log,而是指向/ dev / null。

So, the question is, why fopen() in GetDevNull() is returning 1 ? 因此,问题是,为什么GetDevNull()中的fopen()返回1? This appears to be happening after we migrated to AIX 7.1. 在我们迁移到AIX 7.1之后,这似乎正在发生。 So, I was wondering if there was any major update to AIX 7.1 that could have affected this? 因此,我想知道AIX 7.1是否有任何重大更新会影响到这一点?

I tried with a simple, sample code (outside of my application) that does fopen on /dev/null; 我尝试了一个简单的示例代码(在我的应用程序外部),该代码在/ dev / null上打开了; this seems to return fd of 3 always. 这似乎总是返回fd为3。 But in my application, it returns 1. So, I am not able to understand where the problem is. 但是在我的应用程序中,它返回1。因此,我无法理解问题出在哪里。

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. 如果fopen("/any/path", "any mode")成功并且关联文件的文件号为1,则必须是该程序先前关闭了文件号为1的情况(可能是通过关闭其stdout流),或者从关闭开始开始。 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. 这是预期的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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