Why should I use fileno(stderr) instead of STDERR_FILENO

  • Jump to comment-1
    Dima Rybakov (Tlt)<dim001r@gmail.com>
    Apr 13, 2024, 10:58 PM UTC
    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. ..."