From nobody@FreeBSD.org  Mon Apr 25 08:15:51 2011
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 76CBA106564A
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 25 Apr 2011 08:15:51 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id 650D98FC13
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 25 Apr 2011 08:15:51 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id p3P8Fo3W030510
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 25 Apr 2011 08:15:50 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id p3P8FoLj030509;
	Mon, 25 Apr 2011 08:15:50 GMT
	(envelope-from nobody)
Message-Id: <201104250815.p3P8FoLj030509@red.freebsd.org>
Date: Mon, 25 Apr 2011 08:15:50 GMT
From: Robert Andersson <streambag@streambag.se>
To: freebsd-gnats-submit@FreeBSD.org
Subject: sys/types.h can't be included when _XOPEN_SOURCE is defined
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         156637
>Category:       kern
>Synopsis:       [headers] [patch] sys/types.h can't be included when _XOPEN_SOURCE is defined
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Apr 25 08:20:18 UTC 2011
>Closed-Date:    
>Last-Modified:  Mon May 02 00:07:16 UTC 2011
>Originator:     Robert Andersson
>Release:        FreeBSD 8.2-RELEASE i386
>Organization:
>Environment:
System: FreeBSD maya 8.2-RELEASE FreeBSD 8.2-RELEASE #0: Fri Feb 18 02:24:46 UTC
 2011 root@almeida.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC i386

>Description:
When including <sys/file.h> with _XOPEN_SOURCE defined to 500 or higher, compila
tion will fail with a message similar to this one (using clang, gcc fails with a
 similar message):

In file included from main.c:3:
/usr/include/sys/file.h:161:2: error: unknown type name 'u_int'
        u_int   xf_flag;        /* flags (see fcntl.h) */

u_int is defined in types.h, but it is wrapped in a #if __BSD_VISIBLE.

__BSD_VISIBLE is defined in cdefs.h only if _POSIX_C_SOURCE is not defined (whic
h it is if _XOPEN_SOURCE is defined).

I found the following (short) thread about this problem from 2009:
http://www.mail-archive.com/freebsd-hackers@freebsd.org/msg69469.html

>How-To-Repeat:
Try to compile the following code:

#define _XOPEN_SOURCE 500
#include <sys/file.h>

int main(int argc, char *argv[]) {
    return 0;
}

>Fix:
I'm not sure what the correct solution is. Any one of the following would solve the problem:
* sys/file.h should not use u_int
* The relevant parts of sys/file.h should be wrapped in #if __BSD_VISIBLE
* The definition of u_int should not be wrapped in #if __BSD_VISIBLE
* _XOPEN_SOURCE >= 500 should not imply _POSIX_C_SOURCE
* _POSIX_C_SOURCE should not stop __BSD_VISIBLE from being defined.

>Release-Note:
>Audit-Trail:

From: Bruce Evans <brde@optusnet.com.au>
To: Robert Andersson <streambag@streambag.se>
Cc: freebsd-gnats-submit@freebsd.org, freebsd-bugs@freebsd.org
Subject: Re: misc/156637: sys/types.h can't be included when _XOPEN_SOURCE
 is defined
Date: Mon, 25 Apr 2011 19:10:00 +1000 (EST)

 I've used the following fix (the first of the above) for 10 years or
 so (it got lost in fixes for mounds of style bugs in <sys/file.h>).
 I didn't notice it in connection with _XOPEN_SOURCE, but by general
 principles.
 
 % Index: file.h
 % ===================================================================
 % RCS file: /home/ncvs/src/sys/sys/file.h,v
 % retrieving revision 1.65
 % diff -u -2 -r1.65 file.h
 % --- file.h	19 Jun 2004 11:38:00 -0000	1.65
 % +++ file.h	20 Jun 2004 02:11:04 -0000
 % @@ -151,5 +142,5 @@
 %  	void	*xf_data;	/* file descriptor specific data */
 %  	void	*xf_vnode;	/* vnode pointer */
 % -	u_int	xf_flag;	/* flags (see fcntl.h) */
 % +	unsigned xf_flag;	/* flags (see fcntl.h) */
 %  };
 %
 
 Bruce

From: Robert Andersson <streambag@streambag.se>
To: Bruce Evans <brde@optusnet.com.au>
Cc: freebsd-gnats-submit@freebsd.org, freebsd-bugs@freebsd.org
Subject: Re: misc/156637: sys/types.h can't be included when _XOPEN_SOURCE
 is defined
Date: Mon, 25 Apr 2011 17:44:31 +0200

 Hi,
 
 Thanks for responding! Your patch definitely solves the problem. I
 found this when trying to port an application to FreeBSD. I guess I can
 work around the problem by patching the code using file.h
 to define __BSD_VISIBLE, but it would be great
 if this fix could be committed so that the workaround can be removed
 eventually.
 
 Cheers,
 Robert

From: Garrett Wollman <wollman@hergotha.csail.mit.edu>
To: streambag@streambag.se
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: misc/156637: sys/types.h can't be included when _XOPEN_SOURCE is
	defined
Date: Mon, 25 Apr 2011 13:51:31 -0400 (EDT)

 >When including <sys/file.h> with _XOPEN_SOURCE defined to 500 or higher, compila
 >tion will fail with a message similar to this one (using clang, gcc fails with a
 > similar message):
 
 Which edition of the standard specifies <sys/file.h>?  It's not in my 
 copy of Issue 6 (SUSv3) or Issue 7 (SUSv4). 
  
 I'd say it's the application code that is in error.  It should not be
 defining _XOPEN_SOURCE and then including (implementation private)
 header files which are not defined in the relevant standard.
 
 Do we seriously need to start writing our headers like:
 
 #include <sys/cdefs.h>
 #ifndef __BSD_VISIBLE
 #error "This is a non-standard header, but you have specified strict standard compliance."
 #endif
 
 ?  This probably goes along with my fix to <sys/cdefs.h> which does:
 
 #ifdef __BSD_VISIBLE
 #error "Application defined preprocessor macro in the implementation namespace."
 #endif
 
 -GAWollman 

From: Robert Andersson <streambag@streambag.se>
To: Garrett Wollman <wollman@hergotha.csail.mit.edu>
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: misc/156637: sys/types.h can't be included when _XOPEN_SOURCE
 is defined
Date: Wed, 27 Apr 2011 21:22:55 +0200

 I think you are right. I was confused about what _XOPEN_SOURCE meant.
 
 By the way, I solved the problem by making sure that the
 application only uses standardized headers. In this case it meant using
 fcntl instead of flock. Hopefully, I will be able to push that upstream.
 
 As far as I'm concerned, this PR can be closed. Thanks for your help
 and sorry for the noise.
 
 / Robert
>Unformatted:
