[Python-bugs-list] [ python-Bugs-505427 ] socket module fails to build on HPUX10

noreply@sourceforge.net noreply@sourceforge.net
Wed, 13 Nov 2002 10:58:26 -0800


Bugs item #505427, was opened at 2002-01-18 11:12
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=505427&group_id=5470

Category: Build
Group: Python 2.2
Status: Open
Resolution: Accepted
Priority: 5
Submitted By: Eddy De Greef (edg)
Assigned to: Neal Norwitz (nnorwitz)
Summary: socket module fails to build on HPUX10

Initial Comment:
On HPUX10, the variable h_errno is undeclared.
Modules/socketmodule.c, line 1975:

		PyH_Err(h_errno);

unless _XOPEN_SOURCE_EXTENDED is defined before
netdb.h is included.

Another option is to add an external declaration:

#ifdef __hpux
extern int h_errno;
#endif


----------------------------------------------------------------------

>Comment By: Neal Norwitz (nnorwitz)
Date: 2002-11-13 13:58

Message:
Logged In: YES 
user_id=33168

h_errno doesn't get expanded from the macro because it is
declared before including netdb.h which defines the macro.

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2002-11-13 13:35

Message:
Logged In: YES 
user_id=33168

Martin, I agree with both your observations.  I don't know
why it wasn't a problem on HPUX11.  I agree with checking
that h_errno is not defined.  I will look through the
compiler/preprocessor output a bit more and try to make sure
everything seems ok before checking in.

Eddy, thanks for all your input.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-11-13 13:26

Message:
Logged In: YES 
user_id=21627

I propose a slightly modified patch, but first a few
observations:

On HPUX10, there is no way to get a thread-safe h_errno, and
a h_errno declaration can be obtained by either defining
_XOPEN_SOURCE_EXTENDED, or declaring it yourself.

On HPUX11, h_errno is always declared (whether or not
_XOPEN_SOURCE_EXTENDED is defined). If _REENTRANT is also
defined, h_errno is thread-safe and is a macro.

If these observations are correct, I think the following
patch should satisfy all needs:

#if defined(__hpux) && !defined(h_errno)
extern int h_errno;
#endif

What do you think?

I'm actually surprised that Neal's patch compiles for
HPUX11. Shouldn't this expand to

extern int (*__h_errno());

and thus conflict with the earlier declaration of __h_errno
as a function?

----------------------------------------------------------------------

Comment By: Eddy De Greef (edg)
Date: 2002-11-13 13:25

Message:
Logged In: YES 
user_id=73597

What if other parts of the code access h_errno indirectly
(eg, via libc)? That could still pose thread-safety problems.
But I guess this is so unlikely, that it's probably safe.

I've applied the patch to 2.2.2 and it compiles and works
(without the _XOPEN_SOURCE_EXTENDED flag).

Thanks.

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2002-11-13 11:41

Message:
Logged In: YES 
user_id=33168

On HPUX 11, socketmodule.c builds fine with or without the
patch.  All the tests run successfully with or without this
patch.

Since python provides its own lock (see
USE_GETHOSTBYNAME_LOCK) around access to h_errno (actually
gethost_common() which accesses h_errno), it seems to me the
usage will still be thread safe.

The reason to not use _XOPEN_SOURCE_EXTENDED is the
potential breakage.  Especially since this is working on HPUX11.
I don't see any way to distinguish between HPUX10 and HPUX11.

----------------------------------------------------------------------

Comment By: Eddy De Greef (edg)
Date: 2002-11-13 04:01

Message:
Logged In: YES 
user_id=73597

As I wrote before, this patch is not thread-safe on HPUX11
(see the man page section below), so I don't think it should
be applied.



----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-11-13 03:34

Message:
Logged In: YES 
user_id=21627

Since Eddy indicated that that patch helps on HP-UX 10 as
well, and since it definitely won't break other systems, I
think this is a good idea, please apply it.

For 2.3, the definition of _XOPEN_SOURCE_EXTENDED should be
sufficient. So I think you can safely close this after
you've applied the patch; if Eddy finds further problems on
HPUX10, he should then submit a new report.

Thanks for looking into this.

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2002-11-12 22:33

Message:
Logged In: YES 
user_id=33168

Martin, do you think it is acceptable to add the patch below
to the 2.2.2 branch?  Do you have any ideas for a better
patch?  I don't have access to HPUX 10, I only have access
to HPUX 11.

If this patch is acceptable, assign back to me.  I will test
it on HPUX 11 to make sure it doesn't break.

Eddy, if Martin accepts this, could you also test that patch
to verify that 2.2.2 works?

+++ Modules/socketmodule.c      13 Nov 2002 03:29:55 -0000
@@ -130,6 +130,9 @@
 #include <os2.h>
 #endif
 
+#if defined(__hpux)
+extern int h_errno;
+#endif
 
 #include <sys/types.h>


----------------------------------------------------------------------

Comment By: Eddy De Greef (edg)
Date: 2002-11-04 05:45

Message:
Logged In: YES 
user_id=73597

I've downloaded the latest 2.3 snapshot (11-4) and it is
indeed fixed, thanks.
Yes, I still had the problem with 2.2.2.


----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2002-11-02 15:07

Message:
Logged In: YES 
user_id=33168

Eddy, _XOPEN_SOURCE_EXTENDED is now defined in pyconfig.h
for 2.3.  Can you verify this fixes your problem?  Are you
still having a problem with 2.2.2?

----------------------------------------------------------------------

Comment By: Eddy De Greef (edg)
Date: 2002-01-22 04:41

Message:
Logged In: YES 
user_id=73597

On HPUX-10, the man page states the following:

  SYNOPSIS
       #include <sys/socket.h>
       #include <netinet/in.h>
       #include <netdb.h>

       extern int h_errno;

       struct hostent *gethostent(void);

       int gethostent_r(struct hostent *result,
                        struct hostent_data *buffer);

       struct hostent *gethostbyname(const char *name);

       int gethostbyname_r(const char *name,
                           struct hostent *result,
                           struct hostent_data *buffer);

       struct hostent *gethostbyaddr(const char *addr,
                                     int len,
                                     int type);

       _XOPEN_SOURCE_EXTENDED only
       struct hostent *gethostbyaddr(const void *addr,
                                     size_t len,
                                     int type);

       int gethostbyaddr_r(const char *addr,
                           int len,
                           int type,
                           struct hostent *result,
                           struct hostent_data *buffer);

       int sethostent(int stayopen);

       int sethostent_r(int stayopen, 
                        struct hostent_data *buffer);

       int endhostent(void);

       int endhostent_r(struct hostent_data *buffer);

       _XOPEN_SOURCE_EXTENDED only
       void sethostent(int stayopen);
       void endhostent(void);

  ...

  ERRORS
       If the name server is being used and
       gethostbyname() or gethostbyaddr() returns a NULL
       pointer, the external integer h_errno contains one
       of the following values:

            HOST_NOT_FOUND
              No such host is known.

            TRY_AGAIN              
              This is usually a temporary error. The local
              server did not receive a response from an
              authoritative server. A retry at some later
              time may succeed.

            NO_RECOVERY
               This is a non-recoverable error.

            NO_ADDRESS
               The requested name is valid but does not
               have an IP address; this is not a temporary
               error. This means another type of request
               to the name server will result in an
               answer.

       If the name server is not being used, the value of
       h_errno may not be meaningful.

/usr/include/netdb.h includes the following fragment:
      
   #ifdef _XOPEN_SOURCE_EXTENDED
   extern int h_errno;
   #endif

So, although this is not mentioned in the man page, 
_XOPEN_SOURCE_EXTENDED must be defined to include 
the declaration.


On HPUX11, the man pages says more or less the same,
but adds the following:

WARNINGS
     Programs that use the interfaces described in
     this manpage cannot be linked statically because
     the implementations of these functions employ
     dynamic loading and linking of shared objects at
     run time.

     h_errno is referenced as an extern int for single
     thread applications and is defined as function
     call macro for multithreaded applications in file
     /usr/include/netdb.h. Applications that reference
     h_errno need to include /usr/include/netdb.h.


/usr/include/netdb.h contains the following:

/*
 * Error return codes from gethostbyname() and
gethostbyaddr()
 */
#ifndef h_ERRNO_KT_DEFINED
#define h_ERRNO_KT_DEFINED
#ifdef _REENTRANT
#ifdef _PROTOTYPES            /* 64 bit: add _PROTOTYPES */
extern int *__h_errno(void);  /* 64 bit: add _PROTOTYPES */
#else /* _PROTOTYPES */       /* 64 bit: add _PROTOTYPES */
extern int *__h_errno();
#endif /* _PROTOTYPES */      /* 64 bit: add _PROTOTYPES */
#define h_errno        (*__h_errno())
#else /* _REENTRANT */
extern int h_errno;
#endif /* REENTRANT */
#endif /* ! h_ERRNO_KT_DEFINED */
#endif /* _NETDB_INCLUDED */


So, the only safe way to get things working on HPUX-10 and
HPUX-11 is to define _XOPEN_SOURCE_EXTENDED before netdb.h
is included. The hardcoded declaration I mentioned before
isn't safe.



----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-01-18 16:09

Message:
Logged In: YES 
user_id=21627

s/HPUX/HPUX11/

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-01-18 16:09

Message:
Logged In: YES 
user_id=21627

Does the second version of your patch also work on HPUX? Can
you point to official documentation on that matter?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=505427&group_id=5470