1919#ifndef _ICD_ENVVARS_H_
2020#define _ICD_ENVVARS_H_
2121
22- // Environment variables
23- #if defined(__linux__ ) || defined(__APPLE__ )
22+ char * khrIcd_getenv (const char * name );
23+ char * khrIcd_secure_getenv (const char * name );
24+ void khrIcd_free_getenv (char * val );
2425
25- static inline char * khrIcd_getenv (const char * name ) {
26- // No allocation of memory necessary for Linux.
27- return getenv (name );
28- }
29-
30- static inline char * khrIcd_secure_getenv (const char * name ) {
31- #if defined(__APPLE__ )
32- // Apple does not appear to have a secure getenv implementation.
33- // The main difference between secure getenv and getenv is that secure getenv
34- // returns NULL if the process is being run with elevated privileges by a normal user.
35- // The idea is to prevent the reading of malicious environment variables by a process
36- // that can do damage.
37- // This algorithm is derived from glibc code that sets an internal
38- // variable (__libc_enable_secure) if the process is running under setuid or setgid.
39- return geteuid () != getuid () || getegid () != getgid () ? NULL : khrIcd_getenv (name );
40- #else
41- // Linux
42- #ifdef HAVE_SECURE_GETENV
43- return secure_getenv (name );
44- #elif defined(HAVE___SECURE_GETENV )
45- return __secure_getenv (name );
46- #else
47- #pragma message( \
48- "Warning: Falling back to non-secure getenv for environmental lookups! Consider" \
49- " updating to a different libc.")
50- return khrIcd_getenv (name );
51- #endif
5226#endif
53- }
54-
55- static inline void khrIcd_free_getenv (char * val ) {
56- // No freeing of memory necessary for Linux, but we should at least touch
57- // val to get rid of compiler warnings.
58- (void )val ;
59- }
60-
61- #elif defined(WIN32 )
62-
63- static inline char * khrIcd_getenv (const char * name ) {
64- char * retVal ;
65- DWORD valSize ;
66-
67- valSize = GetEnvironmentVariableA (name , NULL , 0 );
68-
69- // valSize DOES include the null terminator, so for any set variable
70- // will always be at least 1. If it's 0, the variable wasn't set.
71- if (valSize == 0 ) return NULL ;
72-
73- // Allocate the space necessary for the registry entry
74- retVal = (char * )malloc (valSize );
75-
76- if (NULL != retVal ) {
77- GetEnvironmentVariableA (name , retVal , valSize );
78- }
79-
80- return retVal ;
81- }
82-
83- static inline char * khrIcd_secure_getenv (const char * name ) {
84- // No secure version for Windows as far as I know
85- return khrIcd_getenv (name );
86- }
87-
88- static inline void khrIcd_free_getenv (char * val ) {
89- free ((void * )val );
90- }
91-
92- #else
93-
94- static inline char * khrIcd_getenv (const char * name ) {
95- // stub func
96- (void )name ;
97- return NULL ;
98- }
99- static inline void khrIcd_free_getenv (char * val ) {
100- // stub func
101- (void )val ;
102- }
103-
104- #endif
105-
106- #endif
0 commit comments