aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/cxxsupp/openmp/kmp_environment.c
blob: 75090d6c32c834e5229654a2bca222eb7d9ed6f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
 * kmp_environment.c -- Handle environment variables OS-independently.
 */


//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.txt for details.
//
//===----------------------------------------------------------------------===//


/*
    ------------------------------------------------------------------------------------------------
    We use GetEnvironmentVariable for Windows* OS instead of getenv because the act of
    loading a DLL on Windows* OS makes any user-set environment variables (i.e. with putenv())
    unavailable.  getenv() apparently gets a clean copy of the env variables as they existed
    at the start of the run.
    JH 12/23/2002
    ------------------------------------------------------------------------------------------------
    On Windows* OS, there are two environments (at least, see below):

        1. Environment maintained by Windows* OS on IA-32 architecture. 
	   Accessible through GetEnvironmentVariable(),
           SetEnvironmentVariable(), and GetEnvironmentStrings().

        2. Environment maintained by C RTL. Accessible through getenv(), putenv().

    putenv() function updates both C and Windows* OS on IA-32 architecture. getenv() function 
    search for variables in C RTL environment only. Windows* OS on IA-32 architecture functions work *only* 
    with Windows* OS on IA-32 architecture.

    Windows* OS on IA-32 architecture maintained by OS, so there is always only one Windows* OS on 
    IA-32 architecture per process. Changes in Windows* OS on IA-32 architecture are process-visible.

    C environment maintained by C RTL. Multiple copies of C RTL may be present in the process, and
    each C RTL maintains its own environment. :-(

    Thus, proper way to work with environment on Windows* OS is:

        1. Set variables with putenv() function -- both C and Windows* OS on
	   IA-32 architecture are being updated. Windows* OS on 
	   IA-32 architecture may be considered as primary target,
	   while updating C RTL environment is a free bonus.

        2. Get variables with GetEnvironmentVariable() -- getenv() does not 
	   search Windows* OS on IA-32 architecture, and can not see variables
	   set with SetEnvironmentVariable().

    2007-04-05 -- lev
    ------------------------------------------------------------------------------------------------
*/

#include "kmp_environment.h"

#include "kmp_os.h"    // KMP_OS_*.
#include "kmp.h"       //
#include "kmp_str.h"   // __kmp_str_*().
#include "kmp_i18n.h"

#if KMP_OS_UNIX
    #include <stdlib.h>    // getenv, setenv, unsetenv.
    #include <string.h>    // strlen, strcpy.
    #if KMP_OS_DARWIN
        #include <crt_externs.h>
        #define environ (*_NSGetEnviron())
    #else
        extern char * * environ;
    #endif
#elif KMP_OS_WINDOWS
    #include <windows.h>   // GetEnvironmentVariable, SetEnvironmentVariable, GetLastError.
#else
    #error Unknown or unsupported OS.
#endif


// TODO: Eliminate direct memory allocations, use string operations instead.

static inline
void *
allocate(
    size_t size
) {
    void * ptr = KMP_INTERNAL_MALLOC( size );
    if ( ptr == NULL ) {
	KMP_FATAL( MemoryAllocFailed );
    }; // if
    return ptr;
} // allocate


char *
__kmp_env_get( char const * name ) {

    char * result = NULL;

    #if KMP_OS_UNIX
        char const * value = getenv( name );
        if ( value != NULL ) {
            size_t len = KMP_STRLEN( value ) + 1;
            result = (char *) KMP_INTERNAL_MALLOC( len );
            if ( result == NULL ) {
		KMP_FATAL( MemoryAllocFailed );
            }; // if
            KMP_STRNCPY_S( result, len, value, len );
        }; // if
    #elif KMP_OS_WINDOWS
        /*
            We use GetEnvironmentVariable for Windows* OS instead of getenv because the act of
            loading a DLL on Windows* OS makes any user-set environment variables (i.e. with putenv())
            unavailable.  getenv() apparently gets a clean copy of the env variables as they existed
            at the start of the run.
            JH 12/23/2002
        */
        DWORD rc;
        rc = GetEnvironmentVariable( name, NULL, 0 );
        if ( ! rc ) {
            DWORD error = GetLastError();
            if ( error != ERROR_ENVVAR_NOT_FOUND ) {
                __kmp_msg(
                    kmp_ms_fatal,
                    KMP_MSG( CantGetEnvVar, name ),
                    KMP_ERR( error ),
                    __kmp_msg_null
                );
            }; // if
            // Variable is not found, it's ok, just continue.
        } else {
            DWORD len = rc;
            result = (char *) KMP_INTERNAL_MALLOC( len );
            if ( result == NULL ) {
		KMP_FATAL( MemoryAllocFailed );
            }; // if
            rc = GetEnvironmentVariable( name, result, len );
            if ( ! rc ) {
                // GetEnvironmentVariable() may return 0 if variable is empty.
                // In such a case GetLastError() returns ERROR_SUCCESS.
                DWORD error = GetLastError();
                if ( error != ERROR_SUCCESS ) {
                    // Unexpected error. The variable should be in the environment,
                    // and buffer should be large enough.
                    __kmp_msg(
                        kmp_ms_fatal,
                        KMP_MSG( CantGetEnvVar, name ),
                        KMP_ERR( error ),
                        __kmp_msg_null
                    );
                    KMP_INTERNAL_FREE( (void *) result );
                    result = NULL;
                }; // if
            }; // if
        }; // if
    #else
        #error Unknown or unsupported OS.
    #endif

    return result;

} // func __kmp_env_get


// TODO: Find and replace all regular free() with __kmp_env_free().

void
__kmp_env_free( char const * * value ) {

    KMP_DEBUG_ASSERT( value != NULL );
    KMP_INTERNAL_FREE( (void *) * value );
    * value = NULL;

} // func __kmp_env_free



int
__kmp_env_exists( char const * name ) {

    #if KMP_OS_UNIX
        char const * value = getenv( name );
        return ( ( value == NULL ) ? ( 0 ) : ( 1 ) );
    #elif KMP_OS_WINDOWS
        DWORD rc;
        rc = GetEnvironmentVariable( name, NULL, 0 );
        if ( rc == 0 ) {
            DWORD error = GetLastError();
            if ( error != ERROR_ENVVAR_NOT_FOUND ) {
                __kmp_msg(
                    kmp_ms_fatal,
                    KMP_MSG( CantGetEnvVar, name ),
                    KMP_ERR( error ),
                    __kmp_msg_null
                );
            }; // if
            return 0;
        }; // if
        return 1;
    #else
        #error Unknown or unsupported OS.
    #endif

} // func __kmp_env_exists



void
__kmp_env_set( char const * name, char const * value, int overwrite ) {

    #if KMP_OS_UNIX
        int rc = setenv( name, value, overwrite );
        if ( rc != 0 ) {
            // Dead code. I tried to put too many variables into Linux* OS
            // environment on IA-32 architecture. When application consumes
            // more than ~2.5 GB of memory, entire system feels bad. Sometimes
            // application is killed (by OS?), sometimes system stops 
            // responding... But this error message never appears. --ln
            __kmp_msg(
                kmp_ms_fatal,
                KMP_MSG( CantSetEnvVar, name ),
                KMP_HNT( NotEnoughMemory ),
                __kmp_msg_null
            );
        }; // if
    #elif KMP_OS_WINDOWS
        BOOL rc;
        if ( ! overwrite ) {
            rc = GetEnvironmentVariable( name, NULL, 0 );
            if ( rc ) {
                // Variable exists, do not overwrite.
                return;
            }; // if
            DWORD error = GetLastError();
            if ( error != ERROR_ENVVAR_NOT_FOUND ) {
                __kmp_msg(
                    kmp_ms_fatal,
                    KMP_MSG( CantGetEnvVar, name ),
                    KMP_ERR( error ),
                    __kmp_msg_null
                );
            }; // if
        }; // if
        rc = SetEnvironmentVariable( name, value );
        if ( ! rc ) {
            DWORD error = GetLastError();
            __kmp_msg(
                kmp_ms_fatal,
                KMP_MSG( CantSetEnvVar, name ),
                KMP_ERR( error ),
                __kmp_msg_null
            );
        }; // if
    #else
        #error Unknown or unsupported OS.
    #endif

} // func __kmp_env_set



void
__kmp_env_unset( char const * name ) {

    #if KMP_OS_UNIX
        unsetenv( name );
    #elif KMP_OS_WINDOWS
        BOOL rc = SetEnvironmentVariable( name, NULL );
        if ( ! rc ) {
            DWORD error = GetLastError();
            __kmp_msg(
                kmp_ms_fatal,
                KMP_MSG( CantSetEnvVar, name ),
                KMP_ERR( error ),
                __kmp_msg_null
            );
        }; // if
    #else
        #error Unknown or unsupported OS.
    #endif

} // func __kmp_env_unset

// -------------------------------------------------------------------------------------------------

/*
    Intel OpenMP RTL string representation of environment: just a string of characters, variables
    are separated with vertical bars, e. g.:

        "KMP_WARNINGS=0|KMP_AFFINITY=compact|"

    Empty variables are allowed and ignored:

        "||KMP_WARNINGS=1||"

*/

static
void
___kmp_env_blk_parse_string(
    kmp_env_blk_t * block,   // M: Env block to fill.
    char const *    env      // I: String to parse.
) {

    char const chr_delimiter   = '|';
    char const str_delimiter[] = { chr_delimiter, 0 };

    char *          bulk       = NULL;
    kmp_env_var_t * vars       = NULL;
    int             count      = 0;  // Number of used elements in vars array.
    int             delimiters = 0;  // Number of delimiters in input string.

    // Copy original string, we will modify the copy.
    bulk = __kmp_str_format( "%s", env );

    // Loop thru all the vars in environment block. Count delimiters (maximum number of variables
    // is number of delimiters plus one).
    {
        char const * ptr = bulk;
        for ( ; ; ) {
            ptr = strchr( ptr, chr_delimiter );
            if ( ptr == NULL ) {
                break;
            }; // if
            ++ delimiters;
            ptr += 1;
        }; // forever
    }

    // Allocate vars array.
    vars = (kmp_env_var_t *) allocate( ( delimiters + 1 ) * sizeof( kmp_env_var_t ) );

    // Loop thru all the variables.
    {
        char * var;     // Pointer to variable (both name and value).
        char * name;    // Pointer to name of variable.
        char * value;   // Pointer to value.
        char * buf;     // Buffer for __kmp_str_token() function.
        var = __kmp_str_token( bulk, str_delimiter, & buf );      // Get the first var.
        while ( var != NULL ) {
            // Save found variable in vars array.
            __kmp_str_split( var, '=', & name, & value );
            KMP_DEBUG_ASSERT( count < delimiters + 1 );
            vars[ count ].name  = name;
            vars[ count ].value = value;
            ++ count;
            // Get the next var.
            var = __kmp_str_token( NULL, str_delimiter, & buf );
        }; // while
    }

    // Fill out result.
    block->bulk  = bulk;
    block->vars  = vars;
    block->count = count;

}; // ___kmp_env_blk_parse_string



/*
    Windows* OS (actually, DOS) environment block is a piece of memory with environment variables. Each
    variable is terminated with zero byte, entire block is terminated with one extra zero byte, so
    we have two zero bytes at the end of environment block, e. g.:

        "HOME=C:\\users\\lev\x00OS=Windows_NT\x00\x00"

    It is not clear how empty environment is represented. "\x00\x00"?
*/

#if KMP_OS_WINDOWS
static
void
___kmp_env_blk_parse_windows(
    kmp_env_blk_t * block,   // M: Env block to fill.
    char const *    env      // I: Pointer to Windows* OS (DOS) environment block.
) {

    char *          bulk  = NULL;
    kmp_env_var_t * vars  = NULL;
    int             count = 0;     // Number of used elements in vars array.
    int             size  = 0;     // Size of bulk.

    char * name;    // Pointer to name of variable.
    char * value;   // Pointer to value.

    if ( env != NULL ) {

        // Loop thru all the vars in environment block. Count variables, find size of block.
        {
            char const * var;     // Pointer to beginning of var.
            int          len;     // Length of variable.
            count = 0;
            var = env;            // The first variable starts and beginning of environment block.
            len = KMP_STRLEN( var );
            while ( len != 0 ) {
                ++ count;
                size = size + len + 1;
                var = var + len + 1; // Move pointer to the beginning of the next variable.
                len = KMP_STRLEN( var );
            }; // while
            size = size + 1;         // Total size of env block, including terminating zero byte.
        }

        // Copy original block to bulk, we will modify bulk, not original block.
        bulk = (char *) allocate( size );
        KMP_MEMCPY_S( bulk, size, env, size );
        // Allocate vars array.
        vars = (kmp_env_var_t *) allocate( count * sizeof( kmp_env_var_t ) );

        // Loop thru all the vars, now in bulk.
        {
            char * var;     // Pointer to beginning of var.
            int    len;     // Length of variable.
            count = 0;
            var = bulk;
            len = KMP_STRLEN( var );
            while ( len != 0 ) {
                // Save variable in vars array.
                __kmp_str_split( var, '=', & name, & value );
                vars[ count ].name  = name;
                vars[ count ].value = value;
                ++ count;
                // Get the next var.
                var = var + len + 1;
                len = KMP_STRLEN( var );
            }; // while
        }

    }; // if

    // Fill out result.
    block->bulk  = bulk;
    block->vars  = vars;
    block->count = count;

}; // ___kmp_env_blk_parse_windows
#endif


/*
    Unix environment block is a array of pointers to variables, last pointer in array is NULL:

        { "HOME=/home/lev", "TERM=xterm", NULL }
*/

static
void
___kmp_env_blk_parse_unix(
    kmp_env_blk_t * block,   // M: Env block to fill.
    char * *        env      // I: Unix environment to parse.
) {

    char *          bulk  = NULL;
    kmp_env_var_t * vars  = NULL;
    int             count = 0;
    int             size  = 0;    // Size of bulk.

    // Count number of variables and length of required bulk.
    {
        count = 0;
        size  = 0;
        while ( env[ count ] != NULL ) {
            size += KMP_STRLEN( env[ count ] ) + 1;
            ++ count;
        }; // while
    }

    // Allocate memory.
    bulk = (char *) allocate( size );
    vars = (kmp_env_var_t *) allocate( count * sizeof( kmp_env_var_t ) );

    // Loop thru all the vars.
    {
        char * var;     // Pointer to beginning of var.
        char * name;    // Pointer to name of variable.
        char * value;   // Pointer to value.
        int    len;     // Length of variable.
        int    i;
        var = bulk;
        for ( i = 0; i < count; ++ i ) {
            // Copy variable to bulk.
            len = KMP_STRLEN( env[ i ] );
            KMP_MEMCPY_S( var, size, env[ i ], len + 1 );
            // Save found variable in vars array.
            __kmp_str_split( var, '=', & name, & value );
            vars[ i ].name  = name;
            vars[ i ].value = value;
            // Move pointer.
            var += len + 1;
        }; // for
    }

    // Fill out result.
    block->bulk  = bulk;
    block->vars  = vars;
    block->count = count;

}; // ___kmp_env_blk_parse_unix



void
__kmp_env_blk_init(
    kmp_env_blk_t * block,  // M: Block to initialize.
    char const *    bulk    // I: Initialization string, or NULL.
) {

    if ( bulk != NULL ) {
        ___kmp_env_blk_parse_string( block, bulk );
    } else {
        #if KMP_OS_UNIX
            ___kmp_env_blk_parse_unix( block, environ );
        #elif KMP_OS_WINDOWS
            {
                char * mem = GetEnvironmentStrings();
                if ( mem == NULL ) {
                    DWORD error = GetLastError();
                    __kmp_msg(
                        kmp_ms_fatal,
                        KMP_MSG( CantGetEnvironment ),
                        KMP_ERR( error ),
                        __kmp_msg_null
                    );
                }; // if
                ___kmp_env_blk_parse_windows( block, mem );
                FreeEnvironmentStrings( mem );
            }
        #else
            #error Unknown or unsupported OS.
        #endif
    }; // if

} // __kmp_env_blk_init



static
int
___kmp_env_var_cmp(                              // Comparison function for qsort().
    kmp_env_var_t const * lhs,
    kmp_env_var_t const * rhs
) {
    return strcmp( lhs->name, rhs->name );
}

void
__kmp_env_blk_sort(
    kmp_env_blk_t * block  // M: Block of environment variables to sort.
) {

    qsort(
        (void *) block->vars,
        block->count,
        sizeof( kmp_env_var_t ),
        ( int ( * )( void const *, void const * ) ) & ___kmp_env_var_cmp
    );

} // __kmp_env_block_sort



void
__kmp_env_blk_free(
    kmp_env_blk_t * block  // M: Block of environment variables to free.
) {

    KMP_INTERNAL_FREE( (void *) block->vars );
    KMP_INTERNAL_FREE( (void *) block->bulk );

    block->count = 0;
    block->vars  = NULL;
    block->bulk  = NULL;

} // __kmp_env_blk_free



char const *               // R: Value of variable or NULL if variable does not exist.
__kmp_env_blk_var(
    kmp_env_blk_t * block, // I: Block of environment variables.
    char const *    name   // I: Name of variable to find.
) {

    int i;
    for ( i = 0; i < block->count; ++ i ) {
        if ( strcmp( block->vars[ i ].name, name ) == 0 ) {
            return block->vars[ i ].value;
        }; // if
    }; // for
    return NULL;

} // __kmp_env_block_var


// end of file //