~mir-team/mir/in-process-egl+input-conglomeration

« back to all changes in this revision

Viewing changes to 3rd_party/android-input/android/system/core/libcutils/properties.c

Merged trunk and fixed issues

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2006 The Android Open Source Project
3
 
 *
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
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
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.
15
 
 */
16
 
 
17
 
#define LOG_TAG "properties"
18
 
 
19
 
#include <stdlib.h>
20
 
#include <string.h>
21
 
#include <unistd.h>
22
 
#include <cutils/sockets.h>
23
 
#include <errno.h>
24
 
#include <assert.h>
25
 
 
26
 
#include <cutils/properties.h>
27
 
#include "loghack.h"
28
 
 
29
 
#define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path))
30
 
 
31
 
#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
32
 
 
33
 
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
34
 
#include <sys/_system_properties.h>
35
 
 
36
 
int property_set(const char *key, const char *value)
37
 
{
38
 
    return __system_property_set(key, value);
39
 
}
40
 
 
41
 
int property_get(const char *key, char *value, const char *default_value)
42
 
{
43
 
    int len;
44
 
 
45
 
    len = __system_property_get(key, value);
46
 
    if(len > 0) {
47
 
        return len;
48
 
    }
49
 
    
50
 
    if(default_value) {
51
 
        len = strlen(default_value);
52
 
        memcpy(value, default_value, len + 1);
53
 
    }
54
 
    return len;
55
 
}
56
 
 
57
 
int property_list(void (*propfn)(const char *key, const char *value, void *cookie), 
58
 
                  void *cookie)
59
 
{
60
 
    char name[PROP_NAME_MAX];
61
 
    char value[PROP_VALUE_MAX];
62
 
    const prop_info *pi;
63
 
    unsigned n;
64
 
    
65
 
    for(n = 0; (pi = __system_property_find_nth(n)); n++) {
66
 
        __system_property_read(pi, name, value);
67
 
        propfn(name, value, cookie);
68
 
    }
69
 
    return 0;
70
 
}
71
 
 
72
 
#elif defined(HAVE_SYSTEM_PROPERTY_SERVER)
73
 
 
74
 
/*
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.
79
 
 */
80
 
#include <stdio.h>
81
 
#include <sys/types.h>
82
 
#include <sys/socket.h>
83
 
#include <sys/un.h>
84
 
#include <pthread.h>
85
 
 
86
 
static pthread_once_t gInitOnce = PTHREAD_ONCE_INIT;
87
 
static pthread_mutex_t gPropertyFdLock = PTHREAD_MUTEX_INITIALIZER;
88
 
static int gPropFd = -1;
89
 
 
90
 
/*
91
 
 * Connect to the properties server.
92
 
 *
93
 
 * Returns the socket descriptor on success.
94
 
 */
95
 
static int connectToServer(const char* fileName)
96
 
{
97
 
    int sock = -1;
98
 
    int cc;
99
 
 
100
 
    struct sockaddr_un addr;
101
 
    
102
 
    sock = socket(AF_UNIX, SOCK_STREAM, 0);
103
 
    if (sock < 0) {
104
 
        ALOGW("UNIX domain socket create failed (errno=%d)\n", errno);
105
 
        return -1;
106
 
    }
107
 
 
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));
112
 
    if (cc < 0) {
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));
117
 
        close(sock);
118
 
        return -1;
119
 
    }
120
 
 
121
 
    return sock;
122
 
}
123
 
 
124
 
/*
125
 
 * Perform one-time initialization.
126
 
 */
127
 
static void init(void)
128
 
{
129
 
    assert(gPropFd == -1);
130
 
 
131
 
    gPropFd = connectToServer(SYSTEM_PROPERTY_PIPE_NAME);
132
 
    if (gPropFd < 0) {
133
 
        //ALOGW("not connected to system property server\n");
134
 
    } else {
135
 
        //ALOGV("Connected to system property server\n");
136
 
    }
137
 
}
138
 
 
139
 
int property_get(const char *key, char *value, const char *default_value)
140
 
{
141
 
    char sendBuf[1+PROPERTY_KEY_MAX];
142
 
    char recvBuf[1+PROPERTY_VALUE_MAX];
143
 
    int len = -1;
144
 
 
145
 
    //ALOGV("PROPERTY GET [%s]\n", key);
146
 
 
147
 
    pthread_once(&gInitOnce, init);
148
 
    if (gPropFd < 0) {
149
 
        /* this mimics the behavior of the device implementation */
150
 
        if (default_value != NULL) {
151
 
            strcpy(value, default_value);
152
 
            len = strlen(value);
153
 
        }
154
 
        return len;
155
 
    }
156
 
 
157
 
    if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
158
 
 
159
 
    memset(sendBuf, 0xdd, sizeof(sendBuf));    // placate valgrind
160
 
 
161
 
    sendBuf[0] = (char) kSystemPropertyGet;
162
 
    strcpy(sendBuf+1, key);
163
 
 
164
 
    pthread_mutex_lock(&gPropertyFdLock);
165
 
    if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
166
 
        pthread_mutex_unlock(&gPropertyFdLock);
167
 
        return -1;
168
 
    }
169
 
    if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
170
 
        pthread_mutex_unlock(&gPropertyFdLock);
171
 
        return -1;
172
 
    }
173
 
    pthread_mutex_unlock(&gPropertyFdLock);
174
 
 
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);
179
 
            len = strlen(value);
180
 
        } else {
181
 
            /*
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.
186
 
             */
187
 
            value[0] = '\0';
188
 
            len = 0;
189
 
        }
190
 
    } else if (recvBuf[0] == 1) {
191
 
        strcpy(value, recvBuf+1);
192
 
        len = strlen(value);
193
 
    } else {
194
 
        ALOGE("Got strange response to property_get request (%d)\n",
195
 
            recvBuf[0]);
196
 
        assert(0);
197
 
        return -1;
198
 
    }
199
 
    //ALOGV("PROP [found=%d def='%s'] (%d) [%s]: [%s]\n",
200
 
    //    recvBuf[0], default_value, len, key, value);
201
 
 
202
 
    return len;
203
 
}
204
 
 
205
 
 
206
 
int property_set(const char *key, const char *value)
207
 
{
208
 
    char sendBuf[1+PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX];
209
 
    char recvBuf[1];
210
 
    int result = -1;
211
 
 
212
 
    //ALOGV("PROPERTY SET [%s]: [%s]\n", key, value);
213
 
 
214
 
    pthread_once(&gInitOnce, init);
215
 
    if (gPropFd < 0)
216
 
        return -1;
217
 
 
218
 
    if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
219
 
    if (strlen(value) >= PROPERTY_VALUE_MAX) return -1;
220
 
 
221
 
    memset(sendBuf, 0xdd, sizeof(sendBuf));    // placate valgrind
222
 
 
223
 
    sendBuf[0] = (char) kSystemPropertySet;
224
 
    strcpy(sendBuf+1, key);
225
 
    strcpy(sendBuf+1+PROPERTY_KEY_MAX, value);
226
 
 
227
 
    pthread_mutex_lock(&gPropertyFdLock);
228
 
    if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
229
 
        pthread_mutex_unlock(&gPropertyFdLock);
230
 
        return -1;
231
 
    }
232
 
    if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
233
 
        pthread_mutex_unlock(&gPropertyFdLock);
234
 
        return -1;
235
 
    }
236
 
    pthread_mutex_unlock(&gPropertyFdLock);
237
 
 
238
 
    if (recvBuf[0] != 1)
239
 
        return -1;
240
 
    return 0;
241
 
}
242
 
 
243
 
int property_list(void (*propfn)(const char *key, const char *value, void *cookie), 
244
 
                  void *cookie)
245
 
{
246
 
    //ALOGV("PROPERTY LIST\n");
247
 
    pthread_once(&gInitOnce, init);
248
 
    if (gPropFd < 0)
249
 
        return -1;
250
 
 
251
 
    return 0;
252
 
}
253
 
 
254
 
#else
255
 
 
256
 
/* SUPER-cheesy place-holder implementation for Win32 */
257
 
 
258
 
#include <cutils/threads.h>
259
 
 
260
 
static mutex_t  env_lock = MUTEX_INITIALIZER;
261
 
 
262
 
int property_get(const char *key, char *value, const char *default_value)
263
 
{
264
 
    char ename[PROPERTY_KEY_MAX + 6];
265
 
    char *p;
266
 
    int len;
267
 
    
268
 
    len = strlen(key);
269
 
    if(len >= PROPERTY_KEY_MAX) return -1;
270
 
    memcpy(ename, "PROP_", 5);
271
 
    memcpy(ename + 5, key, len + 1);
272
 
    
273
 
    mutex_lock(&env_lock);
274
 
 
275
 
    p = getenv(ename);
276
 
    if(p == 0) p = "";
277
 
    len = strlen(p);
278
 
    if(len >= PROPERTY_VALUE_MAX) {
279
 
        len = PROPERTY_VALUE_MAX - 1;
280
 
    }
281
 
    
282
 
    if((len == 0) && default_value) {
283
 
        len = strlen(default_value);
284
 
        memcpy(value, default_value, len + 1);
285
 
    } else {
286
 
        memcpy(value, p, len);
287
 
        value[len] = 0;
288
 
    }
289
 
 
290
 
    mutex_unlock(&env_lock);
291
 
    
292
 
    return len;
293
 
}
294
 
 
295
 
 
296
 
int property_set(const char *key, const char *value)
297
 
{
298
 
    char ename[PROPERTY_KEY_MAX + 6];
299
 
    char *p;
300
 
    int len;
301
 
    int r;
302
 
 
303
 
    if(strlen(value) >= PROPERTY_VALUE_MAX) return -1;
304
 
    
305
 
    len = strlen(key);
306
 
    if(len >= PROPERTY_KEY_MAX) return -1;
307
 
    memcpy(ename, "PROP_", 5);
308
 
    memcpy(ename + 5, key, len + 1);
309
 
 
310
 
    mutex_lock(&env_lock);
311
 
#ifdef HAVE_MS_C_RUNTIME
312
 
    {
313
 
        char  temp[256];
314
 
        snprintf( temp, sizeof(temp), "%s=%s", ename, value);
315
 
        putenv(temp);
316
 
        r = 0;
317
 
    }
318
 
#else    
319
 
    r = setenv(ename, value, 1);
320
 
#endif    
321
 
    mutex_unlock(&env_lock);
322
 
    
323
 
    return r;
324
 
}
325
 
 
326
 
int property_list(void (*propfn)(const char *key, const char *value, void *cookie), 
327
 
                  void *cookie)
328
 
{
329
 
    return 0;
330
 
}
331
 
 
332
 
#endif