1From 6cc1c22cc30320f56da552a76bd956db8f255b6a Mon Sep 17 00:00:00 2001
2From: Natanael Copa <ncopa@alpinelinux.org>
3Date: Wed, 18 Nov 2015 10:05:07 +0000
4Subject: [PATCH] Use configure to test for feature instead of platform
5
6Test for various functions instead of trying to keep track of what
7platform and what version of the given platform has support for what.
8
9This should make it easier to port to currently unknown platforms and
10will solve the issue if a platform add support for a missing feature in
11the future.
12
13The features we test for are:
14- getifaddrs
15- getauxval
16- issetugid
17- __secure_getenv
18
19This is needed for musl libc.
20
21Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
22[Retrieved (and slightly updated) from:
23http://cgit.openembedded.org/meta-openembedded/tree/meta-oe/recipes-support/open-vm-tools/open-vm-tools/0007-Use-configure-to-test-for-feature-instead-of-platfor.patch?h=sumo]
24Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
25---
26 open-vm-tools/configure.ac               |  4 ++++
27 open-vm-tools/lib/misc/idLinux.c         | 30 ++++++++++++++----------------
28 open-vm-tools/lib/nicInfo/nicInfoPosix.c |  8 ++++++--
29 3 files changed, 24 insertions(+), 18 deletions(-)
30
31Index: open-vm-tools/configure.ac
32===================================================================
33--- open-vm-tools.orig/configure.ac
34+++ open-vm-tools/configure.ac
35@@ -798,6 +798,7 @@ AC_CHECK_FUNCS(
36
37 AC_CHECK_FUNCS([ecvt])
38 AC_CHECK_FUNCS([fcvt])
39+AC_CHECK_FUNCS([getifaddrs getauxval issetugid __secure_getenv])
40
41 AC_CHECK_FUNC([mkdtemp], [have_mkdtemp=yes])
42
43@@ -1063,10 +1064,13 @@ AC_PATH_PROG(
44 ###
45
46 AC_CHECK_HEADERS([crypt.h])
47+AC_CHECK_HEADERS([ifaddrs.h])
48 AC_CHECK_HEADERS([inttypes.h])
49 AC_CHECK_HEADERS([stdint.h])
50 AC_CHECK_HEADERS([stdlib.h])
51 AC_CHECK_HEADERS([wchar.h])
52+AC_CHECK_HEADERS([net/if.h])
53+AC_CHECK_HEADERS([sys/auxv.h])
54 AC_CHECK_HEADERS([sys/inttypes.h])
55 AC_CHECK_HEADERS([sys/io.h])
56 AC_CHECK_HEADERS([sys/param.h]) # Required to make the sys/user.h check work correctly on FreeBSD
57Index: open-vm-tools/lib/misc/idLinux.c
58===================================================================
59--- open-vm-tools.orig/lib/misc/idLinux.c
60+++ open-vm-tools/lib/misc/idLinux.c
61@@ -27,12 +27,9 @@
62 #include <sys/syscall.h>
63 #include <string.h>
64 #include <unistd.h>
65-#ifdef __linux__
66-#if defined(__GLIBC__) && \
67-           (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
68+#ifdef HAVE_SYS_AUXV_H
69 #include <sys/auxv.h>
70 #endif
71-#endif
72 #ifdef __APPLE__
73 #include <sys/socket.h>
74 #include <TargetConditionals.h>
75@@ -997,31 +994,32 @@ Id_EndSuperUser(uid_t uid)  // IN:
76 static Bool
77 IdIsSetUGid(void)
78 {
79-#if defined(__ANDROID__)
80-   /* Android does not have a secure_getenv, so be conservative. */
81-   return TRUE;
82-#else
83    /*
84     * We use __secure_getenv, which returns NULL if the binary is
85-    * setuid or setgid. Alternatives include,
86+    * setuid or setgid, when issetugid or getauxval(AT_SECURE) is not
87+    * available. Alternatives include,
88     *
89-    *   a) getauxval(AT_SECURE); not available until glibc 2.16.
90-    *   b) __libc_enable_secure; may not be exported.
91+    *   a) issetugid(); not (yet?) available in glibc.
92+    *   b) getauxval(AT_SECURE); not available until glibc 2.16.
93+    *   c) __libc_enable_secure; may not be exported.
94     *
95-    * Use (a) when we are based on glibc 2.16, or newer.
96+    * Use (b) when we are based on glibc 2.16, or newer.
97     */
98
99-#if defined(__GLIBC__) && \
100-           (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16))
101+#if HAVE_ISSETUGID
102+   return issetugid();
103+#elif HAVE_GETAUXVAL
104    return getauxval(AT_SECURE) != 0;
105-#else
106+#elif HAVE___SECURE_GETENV
107    static const char envName[] = "VMW_SETUGID_TEST";
108
109    if (setenv(envName, "1", TRUE) == -1) {
110       return TRUE; /* Conservative */
111    }
112    return __secure_getenv(envName) == NULL;
113-#endif
114+#else
115+   /* Android does not have a secure_getenv, so be conservative. */
116+   return TRUE;
117 #endif
118 }
119 #endif
120Index: open-vm-tools/lib/nicInfo/nicInfoPosix.c
121===================================================================
122--- open-vm-tools.orig/lib/nicInfo/nicInfoPosix.c
123+++ open-vm-tools/lib/nicInfo/nicInfoPosix.c
124@@ -34,9 +34,13 @@
125 #include <sys/socket.h>
126 #include <sys/stat.h>
127 #include <errno.h>
128-#if defined(__FreeBSD__) || defined(__APPLE__)
129+#if HAVE_SYS_SYSCTL_H
130 # include <sys/sysctl.h>
131+#endif
132+#if HAVE_IFADDRS_H
133 # include <ifaddrs.h>
134+#endif
135+#if HAVE_NET_IF_H
136 # include <net/if.h>
137 #endif
138 #ifndef NO_DNET
139@@ -348,10 +352,7 @@ GuestInfoGetNicInfo(NicInfoV3 *nicInfo)
140  *
141  ******************************************************************************
142  */
143-#if defined(__FreeBSD__) || \
144-    defined(__APPLE__) || \
145-    defined(USERWORLD) || \
146-    (defined(__linux__) && defined(NO_DNET))
147+#if defined(NO_DNET) && defined(HAVE_GETIFADDRS)
148
149 char *
150 GuestInfoGetPrimaryIP(void)
151