2
* Copyright (C) 2006 The Android Open Source Project
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
#define LOG_TAG "properties"
22
#include <cutils/sockets.h>
26
#include <cutils/properties.h>
29
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
31
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
32
#include <sys/_system_properties.h>
34
int property_set(const char *key, const char *value)
36
return __system_property_set(key, value);
39
int property_get(const char *key, char *value, const char *default_value)
43
len = __system_property_get(key, value);
49
len = strlen(default_value);
50
memcpy(value, default_value, len + 1);
55
int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
58
char name[PROP_NAME_MAX];
59
char value[PROP_VALUE_MAX];
63
for(n = 0; (pi = __system_property_find_nth(n)); n++) {
64
__system_property_read(pi, name, value);
65
propfn(name, value, cookie);
70
#elif defined(HAVE_SYSTEM_PROPERTY_SERVER)
73
* The Linux simulator provides a "system property server" that uses IPC
74
* to set/get/list properties. The file descriptor is shared by all
75
* threads in the process, so we use a mutex to ensure that requests
76
* from multiple threads don't get interleaved.
79
#include <sys/types.h>
80
#include <sys/socket.h>
84
static pthread_once_t gInitOnce = PTHREAD_ONCE_INIT;
85
static pthread_mutex_t gPropertyFdLock = PTHREAD_MUTEX_INITIALIZER;
86
static int gPropFd = -1;
89
* Connect to the properties server.
91
* Returns the socket descriptor on success.
93
static int connectToServer(const char* fileName)
98
struct sockaddr_un addr;
100
sock = socket(AF_UNIX, SOCK_STREAM, 0);
102
ALOGW("UNIX domain socket create failed (errno=%d)\n", errno);
106
/* connect to socket; fails if file doesn't exist */
107
strcpy(addr.sun_path, fileName); // max 108 bytes
108
addr.sun_family = AF_UNIX;
109
cc = connect(sock, (struct sockaddr*) &addr, SUN_LEN(&addr));
111
// ENOENT means socket file doesn't exist
112
// ECONNREFUSED means socket exists but nobody is listening
113
//ALOGW("AF_UNIX connect failed for '%s': %s\n",
114
// fileName, strerror(errno));
123
* Perform one-time initialization.
125
static void init(void)
127
assert(gPropFd == -1);
129
gPropFd = connectToServer(SYSTEM_PROPERTY_PIPE_NAME);
131
//ALOGW("not connected to system property server\n");
133
//ALOGV("Connected to system property server\n");
137
int property_get(const char *key, char *value, const char *default_value)
139
char sendBuf[1+PROPERTY_KEY_MAX];
140
char recvBuf[1+PROPERTY_VALUE_MAX];
143
//ALOGV("PROPERTY GET [%s]\n", key);
145
pthread_once(&gInitOnce, init);
147
/* this mimics the behavior of the device implementation */
148
if (default_value != NULL) {
149
strcpy(value, default_value);
155
if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
157
memset(sendBuf, 0xdd, sizeof(sendBuf)); // placate valgrind
159
sendBuf[0] = (char) kSystemPropertyGet;
160
strcpy(sendBuf+1, key);
162
pthread_mutex_lock(&gPropertyFdLock);
163
if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
164
pthread_mutex_unlock(&gPropertyFdLock);
167
if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
168
pthread_mutex_unlock(&gPropertyFdLock);
171
pthread_mutex_unlock(&gPropertyFdLock);
173
/* first byte is 0 if value not defined, 1 if found */
174
if (recvBuf[0] == 0) {
175
if (default_value != NULL) {
176
strcpy(value, default_value);
180
* If the value isn't defined, hand back an empty string and
181
* a zero length, rather than a failure. This seems wrong,
182
* since you can't tell the difference between "undefined" and
183
* "defined but empty", but it's what the device does.
188
} else if (recvBuf[0] == 1) {
189
strcpy(value, recvBuf+1);
192
ALOGE("Got strange response to property_get request (%d)\n",
197
//ALOGV("PROP [found=%d def='%s'] (%d) [%s]: [%s]\n",
198
// recvBuf[0], default_value, len, key, value);
204
int property_set(const char *key, const char *value)
206
char sendBuf[1+PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX];
210
//ALOGV("PROPERTY SET [%s]: [%s]\n", key, value);
212
pthread_once(&gInitOnce, init);
216
if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
217
if (strlen(value) >= PROPERTY_VALUE_MAX) return -1;
219
memset(sendBuf, 0xdd, sizeof(sendBuf)); // placate valgrind
221
sendBuf[0] = (char) kSystemPropertySet;
222
strcpy(sendBuf+1, key);
223
strcpy(sendBuf+1+PROPERTY_KEY_MAX, value);
225
pthread_mutex_lock(&gPropertyFdLock);
226
if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
227
pthread_mutex_unlock(&gPropertyFdLock);
230
if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
231
pthread_mutex_unlock(&gPropertyFdLock);
234
pthread_mutex_unlock(&gPropertyFdLock);
241
int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
244
//ALOGV("PROPERTY LIST\n");
245
pthread_once(&gInitOnce, init);
254
/* SUPER-cheesy place-holder implementation for Win32 */
256
#include <cutils/threads.h>
258
static mutex_t env_lock = MUTEX_INITIALIZER;
260
int property_get(const char *key, char *value, const char *default_value)
262
char ename[PROPERTY_KEY_MAX + 6];
267
if(len >= PROPERTY_KEY_MAX) return -1;
268
memcpy(ename, "PROP_", 5);
269
memcpy(ename + 5, key, len + 1);
271
mutex_lock(&env_lock);
276
if(len >= PROPERTY_VALUE_MAX) {
277
len = PROPERTY_VALUE_MAX - 1;
280
if((len == 0) && default_value) {
281
len = strlen(default_value);
282
memcpy(value, default_value, len + 1);
284
memcpy(value, p, len);
288
mutex_unlock(&env_lock);
294
int property_set(const char *key, const char *value)
296
char ename[PROPERTY_KEY_MAX + 6];
301
if(strlen(value) >= PROPERTY_VALUE_MAX) return -1;
304
if(len >= PROPERTY_KEY_MAX) return -1;
305
memcpy(ename, "PROP_", 5);
306
memcpy(ename + 5, key, len + 1);
308
mutex_lock(&env_lock);
309
#ifdef HAVE_MS_C_RUNTIME
312
snprintf( temp, sizeof(temp), "%s=%s", ename, value);
317
r = setenv(ename, value, 1);
319
mutex_unlock(&env_lock);
324
int property_list(void (*propfn)(const char *key, const char *value, void *cookie),