Bug 259975 - sys/file.h fails when -D_POSIX_C_SOURCE=200809L is given
Summary: sys/file.h fails when -D_POSIX_C_SOURCE=200809L is given
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: 13.0-STABLE
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-toolchain (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-11-21 18:55 UTC by Yuri Victorovich
Modified: 2021-12-06 15:57 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Yuri Victorovich freebsd_committer freebsd_triage 2021-11-21 18:55:50 UTC
This line:
> $ cat x.c
> #include <sys/file.h>

fails when compiled with -D_POSIX_C_SOURCE=200809L:
> $ cc -c x.c -D_POSIX_C_SOURCE=200809L
> In file included from x.c:2:
> /usr/include/sys/file.h:233:2: error: unknown type name 'u_int'
>         u_int   xf_flag;        /* flags (see fcntl.h) */
>         ^
> 1 error generated.


The vtk-9,1,0 fails for this reason.

I am not sure but shouldn't base headers compile with any choices of C standard level?
Comment 1 Warner Losh freebsd_committer freebsd_triage 2021-11-21 19:24:27 UTC
Since sys/file.h is not defined by POSIX-1.2008, including it at all is undefined behavior.

Why does vtk need it? I'm unfamiliar with that program.
Comment 2 Warner Losh freebsd_committer freebsd_triage 2021-11-21 19:36:40 UTC
(In reply to Warner Losh from comment #1)

Also, the name u_int is only defined in sys/types.h, so this likely is unrelated to the POSIX_C_SOURCE stuff and instead based on the expectation that sys/files.h is independently includable.

But, u_int isn't visible when POSIX_C_SOURCE is defined (since it's hidden behind __BSD_VISIBLE. It also uses kvaddr_t, ksize_t and maybe a few others that should likely also be protected with __BSD_VISIBLE since POSIX_C_SOURCE says define what's in posix and nothing else.

I'd wager the name-space-clean fix is to make xfile only __BSD_VISIBLE, but that too would likely break vtk since it's already doing dubious things with it and including files that aren't defined in the posix standard.

Most likely, though, it wants to use flock (also not defined in POSIX.1-2008), which is the only function I could find in base man pages that mention sys/file.h inclusion. However, if it wants this extension, shouldn't it not be defining that it doesn't want extensions?
Comment 3 Yuri Victorovich freebsd_committer freebsd_triage 2021-11-21 19:52:58 UTC
The upstream bug report: https://gitlab.kitware.com/vtk/vtk/-/issues/18391
Comment 4 Yuri Victorovich freebsd_committer freebsd_triage 2021-11-21 19:54:33 UTC
VTK is a very large library for scientific and industrial visualization.
Comment 5 Warner Losh freebsd_committer freebsd_triage 2021-11-21 20:17:28 UTC
(In reply to Yuri Victorovich from comment #4)
  set (HDF_EXTRA_C_FLAGS -D_POSIX_C_SOURCE=200809L)
needs to be removed from ThirdParty/hdf5/vtkhdf5/config/cmake_ext_mod/ConfigureChecks.cmake in the FreeBSD port. It uses flock, which isn't in POSIX, so setting POSIX_C_SOURCE is likely inappropriate.

It's also a bug that we try to define xfile when __BSD_VISIBLE is defined, but fixing that wouldn't fix hdf5 inside of vtk.
Comment 6 Warner Losh freebsd_committer freebsd_triage 2021-11-21 20:54:28 UTC
The other workaround for this would be to add -D__BSD_VISIBLE=1 to the compliation.

The hdf sources already do something similar for _GNU_SOURCE to bring in extra functionality that's only in Linux, but we have no similar _BSD_SOURCE or _FREEBSD_SOURCE (NetBSD and OpenBSD both have _NETBSD_SORUCE or _OPENBSD_SOURCE respectively). My inclination is to define _FREEBSD_SOURCE and have hdf use it (and maybe __BSD_VISIBLE=1 as a transitionary workaround), but I'm open for the least painful way to be both compliant with POSIX_C_SOURCE and the desire for enabling extensions.
Comment 7 Yuri Victorovich freebsd_committer freebsd_triage 2021-11-21 21:01:00 UTC
(In reply to Warner Losh from comment #6)

Thanks Warner,

I'll use this workaround.


Yuri
Comment 8 Yuri Victorovich freebsd_committer freebsd_triage 2021-11-22 03:31:21 UTC
(In reply to Warner Losh from comment #6)

FYI: Adding -D__BSD_VISIBLE=1 causes another failure:
> /usr/include/time.h:175:23: error: unknown type name 'timer_t'; did you mean 'time_t'?
> int timer_oshandle_np(timer_t timerid);
>                       ^
> /usr/include/time.h:68:18: note: 'time_t' declared here
> typedef __time_t        time_t;
>                         ^

But adding -Du_int=unsigned works. So I would use this workaround for the time being until it is resolved outside of the port.
Comment 9 commit-hook freebsd_committer freebsd_triage 2021-11-24 09:40:12 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=a2b53e53a3f38dd89b64500a2d2a1727baffe4f9

commit a2b53e53a3f38dd89b64500a2d2a1727baffe4f9
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2021-11-24 09:25:47 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2021-11-24 09:38:09 +0000

    sys/file.h: Allow inclusion when compiling for a strict namespace

    Although not part of the standard, this file is sometimes included with
    -D_POSIX_C_SOURCE=<value> or -D_XOPEN_SOURCE=<value>. Limit those
    sturctures that use types hidden by __BSD_VISIBLE to when they are
    visible.

    PR:                     259975, 234205
    Sponsored by:           Netflix

 sys/sys/file.h | 4 ++++
 1 file changed, 4 insertions(+)
Comment 10 commit-hook freebsd_committer freebsd_triage 2021-12-06 15:56:27 UTC
A commit in branch stable/12 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=5fb6ac34479dc0e23b5ff8f3ae74c6266cf32476

commit 5fb6ac34479dc0e23b5ff8f3ae74c6266cf32476
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2021-11-24 09:25:47 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2021-12-06 15:54:10 +0000

    sys/file.h: Allow inclusion when compiling for a strict namespace

    Although not part of the standard, this file is sometimes included with
    -D_POSIX_C_SOURCE=<value> or -D_XOPEN_SOURCE=<value>. Limit those
    sturctures that use types hidden by __BSD_VISIBLE to when they are
    visible.

    PR:                     259975, 234205
    Sponsored by:           Netflix

    (cherry picked from commit a2b53e53a3f38dd89b64500a2d2a1727baffe4f9)

 sys/sys/file.h | 4 ++++
 1 file changed, 4 insertions(+)
Comment 11 commit-hook freebsd_committer freebsd_triage 2021-12-06 15:57:30 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=793fad727a33a5f539eb414e4f0b09aaee0a1b23

commit 793fad727a33a5f539eb414e4f0b09aaee0a1b23
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2021-11-24 09:25:47 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2021-12-06 15:55:58 +0000

    sys/file.h: Allow inclusion when compiling for a strict namespace

    Although not part of the standard, this file is sometimes included with
    -D_POSIX_C_SOURCE=<value> or -D_XOPEN_SOURCE=<value>. Limit those
    sturctures that use types hidden by __BSD_VISIBLE to when they are
    visible.

    PR:                     259975, 234205
    Sponsored by:           Netflix

    (cherry picked from commit a2b53e53a3f38dd89b64500a2d2a1727baffe4f9)

 sys/sys/file.h | 4 ++++
 1 file changed, 4 insertions(+)