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
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2002-2017. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "sys.h"
#include "erl_sys_driver.h"
#include "erl_alloc.h"
static erts_osenv_t sysenv_global_env;
static erts_rwmtx_t sysenv_rwmtx;
static void import_initial_env(void);
void erts_sys_env_init() {
erts_rwmtx_init(&sysenv_rwmtx, "environ", NIL,
ERTS_LOCK_FLAGS_PROPERTY_STATIC | ERTS_LOCK_FLAGS_CATEGORY_GENERIC);
erts_osenv_init(&sysenv_global_env);
import_initial_env();
}
const erts_osenv_t *erts_sys_rlock_global_osenv() {
erts_rwmtx_rlock(&sysenv_rwmtx);
return &sysenv_global_env;
}
erts_osenv_t *erts_sys_rwlock_global_osenv() {
erts_rwmtx_rwlock(&sysenv_rwmtx);
return &sysenv_global_env;
}
void erts_sys_runlock_global_osenv() {
erts_rwmtx_runlock(&sysenv_rwmtx);
}
void erts_sys_rwunlock_global_osenv() {
erts_rwmtx_rwunlock(&sysenv_rwmtx);
}
int erts_sys_explicit_host_getenv(char *key, char *value, size_t *size) {
size_t new_size = GetEnvironmentVariableA(key, value, (DWORD)*size);
if(new_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
return 0;
} else if(new_size > *size) {
return -1;
}
*size = new_size;
return 1;
}
int erts_sys_explicit_8bit_putenv(char *key, char *value) {
WCHAR *wide_key, *wide_value;
int key_length, value_length;
int result;
/* Note that we do *NOT* honor the filename encoding flags (+fnu/+fnl)
* here; the previous implementation used SetEnvironmentVariableA and
* things may break if we step away from that. */
key_length = MultiByteToWideChar(CP_ACP, 0, key, -1, NULL, 0);
value_length = MultiByteToWideChar(CP_ACP, 0, value, -1, NULL, 0);
/* Report "not found" if either string isn't convertible. */
if(key_length == 0 || value_length == 0) {
return 0;
}
wide_key = erts_alloc(ERTS_ALC_T_TMP, key_length * sizeof(WCHAR));
wide_value = erts_alloc(ERTS_ALC_T_TMP, value_length * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, key, -1, wide_key, key_length);
MultiByteToWideChar(CP_ACP, 0, value, -1, wide_value, value_length);
{
erts_osenv_data_t env_key, env_value;
erts_osenv_t *env;
env = erts_sys_rwlock_global_osenv();
/* -1 to exclude the NUL terminator. */
env_key.length = (key_length - 1) * sizeof(WCHAR);
env_key.data = wide_key;
env_value.length = (value_length - 1) * sizeof(WCHAR);
env_value.data = wide_value;
result = erts_osenv_put_native(env, &env_key, &env_value);
erts_sys_rwunlock_global_osenv();
}
erts_free(ERTS_ALC_T_TMP, wide_key);
erts_free(ERTS_ALC_T_TMP, wide_value);
return result;
}
int erts_sys_explicit_8bit_getenv(char *key, char *value, size_t *size) {
erts_osenv_data_t env_key, env_value;
int key_length, value_length, result;
WCHAR *wide_key, *wide_value;
key_length = MultiByteToWideChar(CP_ACP, 0, key, -1, NULL, 0);
/* Report "not found" if the string isn't convertible. */
if(key_length == 0) {
return 0;
}
wide_key = erts_alloc(ERTS_ALC_T_TMP, key_length * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, key, -1, wide_key, key_length);
/* We assume that the worst possible size is twice the output buffer width,
* as we could theoretically be on a code page that requires surrogates. */
value_length = (*size) * 2;
wide_value = erts_alloc(ERTS_ALC_T_TMP, value_length * sizeof(WCHAR));
{
const erts_osenv_t *env = erts_sys_rlock_global_osenv();
/* -1 to exclude the NUL terminator. */
env_key.length = (key_length - 1) * sizeof(WCHAR);
env_key.data = wide_key;
env_value.length = value_length * sizeof(WCHAR);
env_value.data = wide_value;
result = erts_osenv_get_native(env, &env_key, &env_value);
erts_sys_runlock_global_osenv();
}
if(result == 1 && env_value.length > 0) {
/* This function doesn't NUL-terminate if the provided size is >= 0,
* so we pass (*size - 1) to reserve space for it and then do it
* manually. */
*size = WideCharToMultiByte(CP_ACP, 0, env_value.data,
env_value.length / sizeof(WCHAR), value, *size - 1, NULL, NULL);
if(*size == 0) {
if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
result = -1;
} else {
result = 0;
}
}
} else {
*size = 0;
}
if(*size > 0) {
value[*size] = '\0';
}
erts_free(ERTS_ALC_T_TMP, wide_key);
erts_free(ERTS_ALC_T_TMP, wide_value);
return result;
}
static void import_initial_env(void) {
WCHAR *environment_block, *current_variable;
environment_block = GetEnvironmentStringsW();
current_variable = environment_block;
while(wcslen(current_variable) > 0) {
WCHAR *separator_index = wcschr(current_variable, L'=');
/* We tolerate environment variables starting with '=' as the per-drive
* working directories are stored this way. */
if(separator_index == current_variable) {
separator_index = wcschr(separator_index + 1, L'=');
}
if(separator_index != NULL && separator_index != current_variable) {
erts_osenv_data_t env_key, env_value;
env_key.length = (separator_index - current_variable) * sizeof(WCHAR);
env_key.data = current_variable;
env_value.length = (wcslen(separator_index) - 1) * sizeof(WCHAR);
env_value.data = separator_index + 1;
erts_osenv_put_native(&sysenv_global_env, &env_key, &env_value);
}
current_variable += wcslen(current_variable) + 1;
}
FreeEnvironmentStringsW(environment_block);
}
|