1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * you may not use this file except in compliance with the License.
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * You may obtain a copy of the License at
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * See the License for the specific language governing permissions and
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * limitations under the License.
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define LOG_TAG "properties"
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <string.h>
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <unistd.h>
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <cutils/sockets.h>
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <errno.h>
24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <assert.h>
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <cutils/properties.h>
27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "loghack.h"
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
30dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
31dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/_system_properties.h>
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
34dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint property_set(const char *key, const char *value)
35dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
36eb1f0c6fd162fe34d91e8766e53e440349130a36Brad Fitzpatrick    return __system_property_set(key, value);
37dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
38dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
39dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint property_get(const char *key, char *value, const char *default_value)
40dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
41dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int len;
42dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
43dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    len = __system_property_get(key, value);
44dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(len > 0) {
45dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return len;
46dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
47dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
48dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(default_value) {
49dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        len = strlen(default_value);
50dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        memcpy(value, default_value, len + 1);
51dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return len;
53dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
54dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
55dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint property_list(void (*propfn)(const char *key, const char *value, void *cookie),
56dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                  void *cookie)
57dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
58dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char name[PROP_NAME_MAX];
59dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char value[PROP_VALUE_MAX];
60dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    const prop_info *pi;
61dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned n;
62dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
63dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(n = 0; (pi = __system_property_find_nth(n)); n++) {
64dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        __system_property_read(pi, name, value);
65dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        propfn(name, value, cookie);
66dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
67dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
68dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
69dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
70dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#elif defined(HAVE_SYSTEM_PROPERTY_SERVER)
71dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
72dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*
73dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * The Linux simulator provides a "system property server" that uses IPC
74dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * to set/get/list properties.  The file descriptor is shared by all
75dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * threads in the process, so we use a mutex to ensure that requests
76dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * from multiple threads don't get interleaved.
77dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
78dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h>
79dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/types.h>
80dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/socket.h>
81dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/un.h>
82dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <pthread.h>
83dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
84dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic pthread_once_t gInitOnce = PTHREAD_ONCE_INIT;
85dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic pthread_mutex_t gPropertyFdLock = PTHREAD_MUTEX_INITIALIZER;
86dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int gPropFd = -1;
87dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
88dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*
89dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Connect to the properties server.
90dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
91dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Returns the socket descriptor on success.
92dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
93dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int connectToServer(const char* fileName)
94dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
95dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int sock = -1;
96dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int cc;
97dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
98dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct sockaddr_un addr;
99dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
100dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    sock = socket(AF_UNIX, SOCK_STREAM, 0);
101dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (sock < 0) {
102ae8b56c0d17a97aff0b98e6405c7cc9811bbbc3dSteve Block        ALOGW("UNIX domain socket create failed (errno=%d)\n", errno);
103dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
104dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
105dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
106dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /* connect to socket; fails if file doesn't exist */
107dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    strcpy(addr.sun_path, fileName);    // max 108 bytes
108dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    addr.sun_family = AF_UNIX;
109dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    cc = connect(sock, (struct sockaddr*) &addr, SUN_LEN(&addr));
110dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (cc < 0) {
111dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        // ENOENT means socket file doesn't exist
112dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        // ECONNREFUSED means socket exists but nobody is listening
113ae8b56c0d17a97aff0b98e6405c7cc9811bbbc3dSteve Block        //ALOGW("AF_UNIX connect failed for '%s': %s\n",
114dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        //    fileName, strerror(errno));
115dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        close(sock);
116dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
117dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
118dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
119dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return sock;
120dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
121dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
122dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*
123dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Perform one-time initialization.
124dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
125dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void init(void)
126dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
127dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    assert(gPropFd == -1);
128dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
129dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    gPropFd = connectToServer(SYSTEM_PROPERTY_PIPE_NAME);
130dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (gPropFd < 0) {
131ae8b56c0d17a97aff0b98e6405c7cc9811bbbc3dSteve Block        //ALOGW("not connected to system property server\n");
132dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else {
13369f4cd7f5add7a7c7f5915e5292aab7eb2a42e9fSteve Block        //ALOGV("Connected to system property server\n");
134dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
135dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
136dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
137dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint property_get(const char *key, char *value, const char *default_value)
138dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
139dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char sendBuf[1+PROPERTY_KEY_MAX];
140dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char recvBuf[1+PROPERTY_VALUE_MAX];
141dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int len = -1;
142dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
14369f4cd7f5add7a7c7f5915e5292aab7eb2a42e9fSteve Block    //ALOGV("PROPERTY GET [%s]\n", key);
144dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
145dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    pthread_once(&gInitOnce, init);
146dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (gPropFd < 0) {
147dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        /* this mimics the behavior of the device implementation */
148dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (default_value != NULL) {
149dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            strcpy(value, default_value);
150dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            len = strlen(value);
151dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
152dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return len;
153dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
154dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
155dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
156dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
157dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    memset(sendBuf, 0xdd, sizeof(sendBuf));    // placate valgrind
158dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
159dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    sendBuf[0] = (char) kSystemPropertyGet;
160dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    strcpy(sendBuf+1, key);
161dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
162dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    pthread_mutex_lock(&gPropertyFdLock);
163dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
164dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        pthread_mutex_unlock(&gPropertyFdLock);
165dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
166dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
167dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
168dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        pthread_mutex_unlock(&gPropertyFdLock);
169dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
170dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
171dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    pthread_mutex_unlock(&gPropertyFdLock);
172dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
173dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /* first byte is 0 if value not defined, 1 if found */
174dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (recvBuf[0] == 0) {
175dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (default_value != NULL) {
176dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            strcpy(value, default_value);
177dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            len = strlen(value);
178dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else {
179dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            /*
180dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project             * If the value isn't defined, hand back an empty string and
181dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project             * a zero length, rather than a failure.  This seems wrong,
182dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project             * since you can't tell the difference between "undefined" and
183dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project             * "defined but empty", but it's what the device does.
184dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project             */
185dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            value[0] = '\0';
186dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            len = 0;
187dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
188dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else if (recvBuf[0] == 1) {
189dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        strcpy(value, recvBuf+1);
190dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        len = strlen(value);
191dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else {
19201dda204cd28fe181691b4a44a51be7e5666d0c8Steve Block        ALOGE("Got strange response to property_get request (%d)\n",
193dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            recvBuf[0]);
194dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        assert(0);
195dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
196dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
19769f4cd7f5add7a7c7f5915e5292aab7eb2a42e9fSteve Block    //ALOGV("PROP [found=%d def='%s'] (%d) [%s]: [%s]\n",
198dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    //    recvBuf[0], default_value, len, key, value);
199dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
200dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return len;
201dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
202dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
203dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
204dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint property_set(const char *key, const char *value)
205dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
206dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char sendBuf[1+PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX];
207dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char recvBuf[1];
208dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int result = -1;
209dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
21069f4cd7f5add7a7c7f5915e5292aab7eb2a42e9fSteve Block    //ALOGV("PROPERTY SET [%s]: [%s]\n", key, value);
211dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
212dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    pthread_once(&gInitOnce, init);
213dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (gPropFd < 0)
214dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
215dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
216dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
217dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (strlen(value) >= PROPERTY_VALUE_MAX) return -1;
218dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
219dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    memset(sendBuf, 0xdd, sizeof(sendBuf));    // placate valgrind
220dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
221dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    sendBuf[0] = (char) kSystemPropertySet;
222dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    strcpy(sendBuf+1, key);
223dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    strcpy(sendBuf+1+PROPERTY_KEY_MAX, value);
224dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
225dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    pthread_mutex_lock(&gPropertyFdLock);
226dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
227dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        pthread_mutex_unlock(&gPropertyFdLock);
228dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
229dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
230dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
231dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        pthread_mutex_unlock(&gPropertyFdLock);
232dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
233dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
234dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    pthread_mutex_unlock(&gPropertyFdLock);
235dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
236dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (recvBuf[0] != 1)
237dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
238dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
239dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
240dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
241dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint property_list(void (*propfn)(const char *key, const char *value, void *cookie),
242dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                  void *cookie)
243dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
24469f4cd7f5add7a7c7f5915e5292aab7eb2a42e9fSteve Block    //ALOGV("PROPERTY LIST\n");
245dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    pthread_once(&gInitOnce, init);
246dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (gPropFd < 0)
247dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return -1;
248dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
249dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
250dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
251dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
252dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#else
253dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
254dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/* SUPER-cheesy place-holder implementation for Win32 */
255dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
256dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <cutils/threads.h>
257dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
258dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic mutex_t  env_lock = MUTEX_INITIALIZER;
259dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
260dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint property_get(const char *key, char *value, const char *default_value)
261dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
262dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char ename[PROPERTY_KEY_MAX + 6];
263dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *p;
264dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int len;
265dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
266dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    len = strlen(key);
267dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(len >= PROPERTY_KEY_MAX) return -1;
268dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    memcpy(ename, "PROP_", 5);
269dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    memcpy(ename + 5, key, len + 1);
270dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
271dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    mutex_lock(&env_lock);
272dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
273dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    p = getenv(ename);
274dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(p == 0) p = "";
275dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    len = strlen(p);
276dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(len >= PROPERTY_VALUE_MAX) {
277dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        len = PROPERTY_VALUE_MAX - 1;
278dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
279dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
280dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if((len == 0) && default_value) {
281dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        len = strlen(default_value);
282dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        memcpy(value, default_value, len + 1);
283dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else {
284dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        memcpy(value, p, len);
285dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        value[len] = 0;
286dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
287dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
288dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    mutex_unlock(&env_lock);
289dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
290dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return len;
291dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
292dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
293dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
294dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint property_set(const char *key, const char *value)
295dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
296dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char ename[PROPERTY_KEY_MAX + 6];
297dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *p;
298dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int len;
299dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int r;
300dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
301dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(strlen(value) >= PROPERTY_VALUE_MAX) return -1;
302dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
303dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    len = strlen(key);
304dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(len >= PROPERTY_KEY_MAX) return -1;
305dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    memcpy(ename, "PROP_", 5);
306dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    memcpy(ename + 5, key, len + 1);
307dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
308dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    mutex_lock(&env_lock);
309dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef HAVE_MS_C_RUNTIME
310dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    {
311dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        char  temp[256];
312dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        snprintf( temp, sizeof(temp), "%s=%s", ename, value);
313dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        putenv(temp);
314dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        r = 0;
315dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
316dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#else
317dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    r = setenv(ename, value, 1);
318dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
319dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    mutex_unlock(&env_lock);
320dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
321dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return r;
322dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
323dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
324dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint property_list(void (*propfn)(const char *key, const char *value, void *cookie),
325dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                  void *cookie)
326dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
327dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
328dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
329dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
330dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
331