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
#define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path))
31
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
33
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
34
#include <sys/_system_properties.h>
36
int property_set(const char *key, const char *value)
38
return __system_property_set(key, value);
41
int property_get(const char *key, char *value, const char *default_value)
45
len = __system_property_get(key, value);
51
len = strlen(default_value);
52
memcpy(value, default_value, len + 1);
57
int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
60
char name[PROP_NAME_MAX];
61
char value[PROP_VALUE_MAX];
65
for(n = 0; (pi = __system_property_find_nth(n)); n++) {
66
__system_property_read(pi, name, value);
67
propfn(name, value, cookie);
72
#elif defined(HAVE_SYSTEM_PROPERTY_SERVER)
75
* The Linux simulator provides a "system property server" that uses IPC
76
* to set/get/list properties. The file descriptor is shared by all
77
* threads in the process, so we use a mutex to ensure that requests
78
* from multiple threads don't get interleaved.
81
#include <sys/types.h>
82
#include <sys/socket.h>
86
static pthread_once_t gInitOnce = PTHREAD_ONCE_INIT;
87
static pthread_mutex_t gPropertyFdLock = PTHREAD_MUTEX_INITIALIZER;
88
static int gPropFd = -1;
91
* Connect to the properties server.
93
* Returns the socket descriptor on success.
95
static int connectToServer(const char* fileName)
100
struct sockaddr_un addr;
102
sock = socket(AF_UNIX, SOCK_STREAM, 0);
104
ALOGW("UNIX domain socket create failed (errno=%d)\n", errno);
108
/* connect to socket; fails if file doesn't exist */
109
strcpy(addr.sun_path, fileName); // max 108 bytes
110
addr.sun_family = AF_UNIX;
111
cc = connect(sock, (struct sockaddr*) &addr, SUN_LEN(&addr));
113
// ENOENT means socket file doesn't exist
114
// ECONNREFUSED means socket exists but nobody is listening
115
//ALOGW("AF_UNIX connect failed for '%s': %s\n",
116
// fileName, strerror(errno));
125
* Perform one-time initialization.
127
static void init(void)
129
assert(gPropFd == -1);
131
gPropFd = connectToServer(SYSTEM_PROPERTY_PIPE_NAME);
133
//ALOGW("not connected to system property server\n");
135
//ALOGV("Connected to system property server\n");
139
int property_get(const char *key, char *value, const char *default_value)
141
char sendBuf[1+PROPERTY_KEY_MAX];
142
char recvBuf[1+PROPERTY_VALUE_MAX];
145
//ALOGV("PROPERTY GET [%s]\n", key);
147
pthread_once(&gInitOnce, init);
149
/* this mimics the behavior of the device implementation */
150
if (default_value != NULL) {
151
strcpy(value, default_value);
157
if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
159
memset(sendBuf, 0xdd, sizeof(sendBuf)); // placate valgrind
161
sendBuf[0] = (char) kSystemPropertyGet;
162
strcpy(sendBuf+1, key);
164
pthread_mutex_lock(&gPropertyFdLock);
165
if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
166
pthread_mutex_unlock(&gPropertyFdLock);
169
if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
170
pthread_mutex_unlock(&gPropertyFdLock);
173
pthread_mutex_unlock(&gPropertyFdLock);
175
/* first byte is 0 if value not defined, 1 if found */
176
if (recvBuf[0] == 0) {
177
if (default_value != NULL) {
178
strcpy(value, default_value);
182
* If the value isn't defined, hand back an empty string and
183
* a zero length, rather than a failure. This seems wrong,
184
* since you can't tell the difference between "undefined" and
185
* "defined but empty", but it's what the device does.
190
} else if (recvBuf[0] == 1) {
191
strcpy(value, recvBuf+1);
194
ALOGE("Got strange response to property_get request (%d)\n",
199
//ALOGV("PROP [found=%d def='%s'] (%d) [%s]: [%s]\n",
200
// recvBuf[0], default_value, len, key, value);
206
int property_set(const char *key, const char *value)
208
char sendBuf[1+PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX];
212
//ALOGV("PROPERTY SET [%s]: [%s]\n", key, value);
214
pthread_once(&gInitOnce, init);
218
if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
219
if (strlen(value) >= PROPERTY_VALUE_MAX) return -1;
221
memset(sendBuf, 0xdd, sizeof(sendBuf)); // placate valgrind
223
sendBuf[0] = (char) kSystemPropertySet;
224
strcpy(sendBuf+1, key);
225
strcpy(sendBuf+1+PROPERTY_KEY_MAX, value);
227
pthread_mutex_lock(&gPropertyFdLock);
228
if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
229
pthread_mutex_unlock(&gPropertyFdLock);
232
if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
233
pthread_mutex_unlock(&gPropertyFdLock);
236
pthread_mutex_unlock(&gPropertyFdLock);
243
int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
246
//ALOGV("PROPERTY LIST\n");
247
pthread_once(&gInitOnce, init);
256
/* SUPER-cheesy place-holder implementation for Win32 */
258
#include <cutils/threads.h>
260
static mutex_t env_lock = MUTEX_INITIALIZER;
262
int property_get(const char *key, char *value, const char *default_value)
264
char ename[PROPERTY_KEY_MAX + 6];
269
if(len >= PROPERTY_KEY_MAX) return -1;
270
memcpy(ename, "PROP_", 5);
271
memcpy(ename + 5, key, len + 1);
273
mutex_lock(&env_lock);
278
if(len >= PROPERTY_VALUE_MAX) {
279
len = PROPERTY_VALUE_MAX - 1;
282
if((len == 0) && default_value) {
283
len = strlen(default_value);
284
memcpy(value, default_value, len + 1);
286
memcpy(value, p, len);
290
mutex_unlock(&env_lock);
296
int property_set(const char *key, const char *value)
298
char ename[PROPERTY_KEY_MAX + 6];
303
if(strlen(value) >= PROPERTY_VALUE_MAX) return -1;
306
if(len >= PROPERTY_KEY_MAX) return -1;
307
memcpy(ename, "PROP_", 5);
308
memcpy(ename + 5, key, len + 1);
310
mutex_lock(&env_lock);
311
#ifdef HAVE_MS_C_RUNTIME
314
snprintf( temp, sizeof(temp), "%s=%s", ename, value);
319
r = setenv(ename, value, 1);
321
mutex_unlock(&env_lock);
326
int property_list(void (*propfn)(const char *key, const char *value, void *cookie),