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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* 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.
*/
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define LOG_TAG "PropertyManager"
#include <cutils/log.h>
#include "PropertyManager.h"
PropertyManager::PropertyManager() {
mNamespaces = new PropertyNamespaceCollection();
pthread_mutex_init(&mLock, NULL);
}
PropertyManager::~PropertyManager() {
PropertyNamespaceCollection::iterator it;
for (it = mNamespaces->begin(); it != mNamespaces->end();) {
delete (*it);
it = mNamespaces->erase(it);
}
delete mNamespaces;
}
PropertyNamespace *PropertyManager::lookupNamespace_UNLOCKED(const char *ns) {
PropertyNamespaceCollection::iterator ns_it;
for (ns_it = mNamespaces->begin(); ns_it != mNamespaces->end(); ++ns_it) {
if (!strcasecmp(ns, (*ns_it)->getName()))
return (*ns_it);
}
errno = ENOENT;
return NULL;
}
Property *PropertyManager::lookupProperty_UNLOCKED(PropertyNamespace *ns, const char *name) {
PropertyCollection::iterator it;
for (it = ns->getProperties()->begin();
it != ns->getProperties()->end(); ++it) {
if (!strcasecmp(name, (*it)->getName()))
return (*it);
}
errno = ENOENT;
return NULL;
}
int PropertyManager::attachProperty(const char *ns_name, Property *p) {
PropertyNamespace *ns;
LOGD("Attaching property %s to namespace %s", p->getName(), ns_name);
pthread_mutex_lock(&mLock);
if (!(ns = lookupNamespace_UNLOCKED(ns_name))) {
LOGD("Creating namespace %s", ns_name);
ns = new PropertyNamespace(ns_name);
mNamespaces->push_back(ns);
}
if (lookupProperty_UNLOCKED(ns, p->getName())) {
errno = EADDRINUSE;
pthread_mutex_unlock(&mLock);
LOGE("Failed to register property %s.%s (%s)",
ns_name, p->getName(), strerror(errno));
return -1;
}
ns->getProperties()->push_back(p);
pthread_mutex_unlock(&mLock);
return 0;
}
int PropertyManager::detachProperty(const char *ns_name, Property *p) {
PropertyNamespace *ns;
LOGD("Detaching property %s from namespace %s", p->getName(), ns_name);
pthread_mutex_lock(&mLock);
if (!(ns = lookupNamespace_UNLOCKED(ns_name))) {
pthread_mutex_unlock(&mLock);
LOGE("Namespace '%s' not found", ns_name);
return -1;
}
PropertyCollection::iterator it;
for (it = ns->getProperties()->begin();
it != ns->getProperties()->end(); ++it) {
if (!strcasecmp(p->getName(), (*it)->getName())) {
delete ((*it));
ns->getProperties()->erase(it);
pthread_mutex_unlock(&mLock);
return 0;
}
}
LOGE("Property %s.%s not found", ns_name, p->getName());
pthread_mutex_unlock(&mLock);
errno = ENOENT;
return -1;
}
int PropertyManager::doSet(Property *p, int idx, const char *value) {
if (p->getReadOnly()) {
errno = EROFS;
return -1;
}
if (p->getType() == Property::Type_STRING) {
return p->set(idx, value);
} else if (p->getType() == Property::Type_INTEGER) {
int tmp;
errno = 0;
tmp = strtol(value, (char **) NULL, 10);
if (errno) {
LOGE("Failed to convert '%s' to int", value);
errno = EINVAL;
return -1;
}
return p->set(idx, tmp);
} else if (p->getType() == Property::Type_IPV4) {
struct in_addr tmp;
if (!inet_aton(value, &tmp)) {
LOGE("Failed to convert '%s' to ipv4", value);
errno = EINVAL;
return -1;
}
return p->set(idx, &tmp);
} else {
LOGE("Property '%s' has an unknown type (%d)", p->getName(),
p->getType());
errno = EINVAL;
return -1;
}
errno = ENOENT;
return -1;
}
int PropertyManager::doGet(Property *p, int idx, char *buffer, size_t max) {
if (p->getType() == Property::Type_STRING) {
if (p->get(idx, buffer, max)) {
LOGW("String property %s get failed (%s)", p->getName(),
strerror(errno));
return -1;
}
}
else if (p->getType() == Property::Type_INTEGER) {
int tmp;
if (p->get(idx, &tmp)) {
LOGW("Integer property %s get failed (%s)", p->getName(),
strerror(errno));
return -1;
}
snprintf(buffer, max, "%d", tmp);
} else if (p->getType() == Property::Type_IPV4) {
struct in_addr tmp;
if (p->get(idx, &tmp)) {
LOGW("IPV4 property %s get failed (%s)", p->getName(),
strerror(errno));
return -1;
}
strncpy(buffer, inet_ntoa(tmp), max);
} else {
LOGE("Property '%s' has an unknown type (%d)", p->getName(),
p->getType());
errno = EINVAL;
return -1;
}
return 0;
}
/*
* IPropertyManager methods
*/
int PropertyManager::set(const char *name, const char *value) {
LOGD("set %s = '%s'", name, value);
pthread_mutex_lock(&mLock);
PropertyNamespaceCollection::iterator ns_it;
for (ns_it = mNamespaces->begin(); ns_it != mNamespaces->end(); ++ns_it) {
PropertyCollection::iterator p_it;
for (p_it = (*ns_it)->getProperties()->begin();
p_it != (*ns_it)->getProperties()->end(); ++p_it) {
for (int i = 0; i < (*p_it)->getNumElements(); i++) {
char fqn[255];
char tmp[8];
sprintf(tmp, "_%d", i);
snprintf(fqn, sizeof(fqn), "%s.%s%s",
(*ns_it)->getName(), (*p_it)->getName(),
((*p_it)->getNumElements() > 1 ? tmp : ""));
if (!strcasecmp(name, fqn)) {
pthread_mutex_unlock(&mLock);
return doSet((*p_it), i, value);
}
}
}
}
LOGE("Property %s not found", name);
pthread_mutex_unlock(&mLock);
errno = ENOENT;
return -1;
}
const char *PropertyManager::get(const char *name, char *buffer, size_t max) {
pthread_mutex_lock(&mLock);
PropertyNamespaceCollection::iterator ns_it;
for (ns_it = mNamespaces->begin(); ns_it != mNamespaces->end(); ++ns_it) {
PropertyCollection::iterator p_it;
for (p_it = (*ns_it)->getProperties()->begin();
p_it != (*ns_it)->getProperties()->end(); ++p_it) {
for (int i = 0; i < (*p_it)->getNumElements(); i++) {
char fqn[255];
char tmp[8];
sprintf(tmp, "_%d", i);
snprintf(fqn, sizeof(fqn), "%s.%s%s",
(*ns_it)->getName(), (*p_it)->getName(),
((*p_it)->getNumElements() > 1 ? tmp : ""));
if (!strcasecmp(name, fqn)) {
pthread_mutex_unlock(&mLock);
if (doGet((*p_it), i, buffer, max))
return NULL;
return buffer;
}
}
}
}
LOGE("Property %s not found", name);
pthread_mutex_unlock(&mLock);
errno = ENOENT;
return NULL;
}
android::List<char *> *PropertyManager::createPropertyList(const char *prefix) {
android::List<char *> *c = new android::List<char *>();
pthread_mutex_lock(&mLock);
PropertyNamespaceCollection::iterator ns_it;
for (ns_it = mNamespaces->begin(); ns_it != mNamespaces->end(); ++ns_it) {
PropertyCollection::iterator p_it;
for (p_it = (*ns_it)->getProperties()->begin();
p_it != (*ns_it)->getProperties()->end(); ++p_it) {
for (int i = 0; i < (*p_it)->getNumElements(); i++) {
char fqn[255];
char tmp[8];
sprintf(tmp, "_%d", i);
snprintf(fqn, sizeof(fqn), "%s.%s%s",
(*ns_it)->getName(), (*p_it)->getName(),
((*p_it)->getNumElements() > 1 ? tmp : ""));
if (!prefix ||
(prefix && !strncasecmp(fqn, prefix, strlen(prefix)))) {
c->push_back(strdup(fqn));
}
}
}
}
pthread_mutex_unlock(&mLock);
return c;
}
|