After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 692085 - stderr and stdout are not always file descriptors 1 and 2
stderr and stdout are not always file descriptors 1 and 2
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
2.35.x
Other Windows
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2013-01-19 15:54 UTC by Erik van Pienbroek
Modified: 2015-10-08 15:34 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Proposed patch (1.22 KB, patch)
2013-01-19 15:54 UTC, Erik van Pienbroek
none Details | Review
Change message system to use fputs instead of write (3.61 KB, patch)
2015-04-09 16:05 UTC, Ignacio Casal Quinteiro (nacho)
none Details | Review
Change message system to use fputs instead of write (3.74 KB, patch)
2015-04-09 16:06 UTC, Ignacio Casal Quinteiro (nacho)
none Details | Review
Change message system to use fputs instead of write (4.03 KB, patch)
2015-04-09 16:09 UTC, Ignacio Casal Quinteiro (nacho)
accepted-commit_now Details | Review

Description Erik van Pienbroek 2013-01-19 15:54:23 UTC
Created attachment 233870 [details] [review]
Proposed patch

By default g_log_default_handler always assumes that stdout
and stderr are file descriptors 1 and 2. On Win32 this isn't
always the case as the win32 API functions AttachConsole and
freopen can be used to dynamically attach GUI applications to
a console and the file descriptors of stderr and stdout will
become different than 1 and 2.

Fixed this by using the fileno function to find out the
correct file descriptors instead of assuming stdout and
stderr are always file descriptors 1 and 2
Comment 1 Allison Karlitskaya (desrt) 2013-01-19 16:45:55 UTC
I'm surprised to find out that g_log() sometimes writes to stdout...

Anyway, looking at the code it would appear that the only time this value is used is to pass as the first argument to this function:

static void
write_string (int          fd,
              const gchar *string)
{
  write (fd, string, strlen (string));
}


So we could probably just use the FILE* directly and use fputs() instead.

One problem with this approach is that mklevel_prefix is called from the recursive log handler which tries very hard not to call anything 'fancy' that may allocate memory or other system resources.  This may be part of the reason that we try to use write() here instead of stdio.  Is it possible that calling fileno() could also do some fancy internal stuff on Windows (like allocating the fd if it does not already exist)?  In any case, I wouldn't be surprised if it is at least taking a lock...
Comment 2 Erik van Pienbroek 2013-05-20 12:51:43 UTC
(In reply to comment #1)
> Is it possible that calling
> fileno() could also do some fancy internal stuff on Windows (like allocating
> the fd if it does not already exist)?

No idea, fileno() is part of the Win32 API so we don't know what happens internally. At http://msdn.microsoft.com/en-us/library/zs6wbdhx%28v=vs.80%29.aspx you can find the documentation about this API but it doesn't tell us anything about locks or memory allocations.
Comment 3 Dan Winship 2013-05-20 12:55:32 UTC
(In reply to comment #1)
> So we could probably just use the FILE* directly and use fputs() instead.

though as just discussed on the mailing list, there are other places in glib where we make this assumption too
Comment 4 Ignacio Casal Quinteiro (nacho) 2015-04-09 16:05:52 UTC
Created attachment 301215 [details] [review]
Change message system to use fputs instead of write
Comment 5 Ignacio Casal Quinteiro (nacho) 2015-04-09 16:06:53 UTC
Created attachment 301216 [details] [review]
Change message system to use fputs instead of write

Windows might use a different fd number for stdout or stderr
so instead of using fd numbers change it to use the FILE directly.
Comment 6 Ignacio Casal Quinteiro (nacho) 2015-04-09 16:09:19 UTC
Created attachment 301217 [details] [review]
Change message system to use fputs instead of write

By default g_log_default_handler always assumes that stdout
and stderr are file descriptors 1 and 2. On Win32 this isn't
always the case as the win32 API functions AttachConsole and
freopen can be used to dynamically attach GUI applications to
a console and the file descriptors of stderr and stdout will
become different than 1 and 2.

Fix it by using fputs with the FILE directly instead of
using the file descriptors.
Comment 7 Allison Karlitskaya (desrt) 2015-10-08 15:21:41 UTC
Review of attachment 301217 [details] [review]:

Looks fine.

Thanks for the good commit message.
Comment 8 Ignacio Casal Quinteiro (nacho) 2015-10-08 15:34:49 UTC
Thanks for the review. Pushed as: https://git.gnome.org/browse/glib/commit/?id=fb9df27776b116d9e8552c0b7b3109245a9c0d26