bitwiseworks / libc

LIBC Next (kLIBC fork)
9 stars 4 forks source link

_GNU_SOURCE and _POSIX_SOURCE #135

Closed komh closed 8 months ago

komh commented 11 months ago

Hi/2.

_GNU_SOURCE enables all the features including _POSIX_SOURCE and _POSIX_C_SOURCE. However, kLIBC does not enable _POSIX_SOURCE and _POSIX_C_SOURCE for the compatibility with EMX headers even with _GNU_SOURCE enabled .

OTOH, LIBCn enables both them if _GNU_SOURCE is enabled. This breaks the compatibility with kLIBC. For example,

/* compiling with -D_GNU_SOURCE will fail. */

#include <stdio.h>

#include <errno.h>

int main( void )
{
    printf("ESHUTDOWN = %d\n", ESHUTDOWN );

    return 0;
}

ESHUTDOWN is defined when _POSIX_SOURCE is not defined. See https://github.com/bitwiseworks/libc/blob/b246be5f6d3d9ff751e1d527dc3a945fbaf1ed76/src/emx/include/sys/errno.h#L143-L146

On kLIBC, there are no problems because _GNU_SOURCE does not define _POSIX_SOURCE. However, on LIBCn, this is defined by that.

So I think, _POSIX_SOURCE guards should be modified to check additional feature test macros such as __USE_EMX like:

#ifndef _POSIX_SOURCE to #if !defined(_POSIX_SOURCE) || defined(__USE_EMX)

This modified guard is already used in many places.

And this should be applied to _POSIX_C_SOURCE.

dmik commented 11 months ago

This LIBCn change was intentional, see #124 and #128 for details. What about the compatibility with kLIBC, I already mentioned in #137 that kLIBC and LIBCn should not be mixed and all current code should be updated towards the recent POSIX specs (and LIBCn). Which means your code sample should not build on recent GNU/Linux systems either until you fix it.

Please show your real life case if you want me to reconsider.

komh commented 11 months ago

The above sample code is built fine on GNU/Linux systems without any problems whether or not -D_GNU_SOURCE is given.

If only _POSIX_C_SOURCE is defined, #ifdef guards may have meanings. However, _GNU_SOURCE defines other macros such as _BSD_SOURCE, too. Therefore, ESHUTDOWN should be defined even if _POSIX_C_SOURCE is defined.

Otherwise, how can I use ESHUTDOWN when _GNU_SOURCE is defined ?

dmik commented 11 months ago

GLIBC defines both _POSIX_SOURCE and _POSIX_C_SOURCE and we depend on that in recent software. However, GLIBC doesn't use _BSD_SOURCE and this is where things go south for us. So may be we should indeed do as you suggest.

SilvanScherrer commented 8 months ago

I found this exact issue as well while working on gnutls tests. ESHUTDOWN is just not defined, as guarded by #ifndef _POSIX_SOURCE