Why should I use fileno(stderr) instead of STDERR_FILENO

From: "Dima Rybakov (Tlt)" <dim001r(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Cc: george(dot)v(dot)tarasov(at)gmail(dot)com
Subject: Why should I use fileno(stderr) instead of STDERR_FILENO
Date: 2024-04-13 22:58:21
Message-ID: CAH898o7kUvqRN64wgHXPJv3ZA3cX-o_FkZzNpmNzW3jJP9CAmA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Dear pgsql hackers,

I found a mixture of fileno(stderr) and STDERR_FILENO in PostgreSQL source
code which in most cases mean the same. But I have found recently that
Microsoft's fileno() _fileno() can return -2 sometimes. After some
experiments I found that applications running as windows service have
problems with stderr. I.e. fileno(stderr) returns -2 (negative two) in
windows service mode. That causes some issues with the logging collector.
Meanwhile the value of STDERR_FILENO always equals 2 and does not depend on
application mode because it is a macro.

I wonder if there are hidden advantages of using fileno(stderr) ?? Should I
use only "fileno(stderr)" or using STDERR_FILENO is acceptable too ?? Are
there cases when I should not use STDERR_FILENO ??

Sincerely,
Dmitry

Additional references
1. BUG #18400
2.
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fileno?view=msvc-170
Quote: "If stdout or stderr is not associated with an output stream (for
example, in a Windows application without a console window), the file
descriptor returned is -2. ..."

Browse pgsql-hackers by date

  From Date Subject
Next Message Dmitry Koterov 2024-04-13 23:17:42 Re: In MacOS, psql reacts on SIGINT in a strange fashion (Linux is fine)
Previous Message Tomas Vondra 2024-04-13 21:04:33 Re: Parallel CREATE INDEX for BRIN indexes