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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/*
* Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
*
* 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 <string.h>
#include <unistd.h>
#include "sockets.h"
#include <errno.h>
#include <assert.h>
#include "properties.h"
//#include "loghack.h"
#include "sysdeps.h"
#define TRACE_TAG TRACE_PROPERTIES
#include "sdb.h"
#define HAVE_TIZEN_PROPERTY
#ifdef HAVE_TIZEN_PROPERTY
#define HAVE_PTHREADS
#include <stdio.h>
#include "threads.h"
static mutex_t env_lock = MUTEX_INITIALIZER;
#define TIZEN_PROPERTY_FILE "/tmp/.sdb.conf" /* tizen specific*/
#define PROPERTY_SEPARATOR "="
struct config_node {
char *key;
char value[PROPERTY_VALUE_MAX];
} sdbd_config[] = {
{ "service.sdb.tcp.port", "0" },
{ NULL, "" }
};
void property_save();
int read_line(const int fd, char* ptr, const unsigned int maxlen);
static void property_init(void)
{
int fd;
int i = 0;
char buffer[PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX+1];
char *tok = NULL;
fd = unix_open(TIZEN_PROPERTY_FILE, O_RDONLY);
if (fd < 0)
return;
for(;;) {
if(read_line(fd, buffer, PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX+1) < 0)
break;
tok = strtok(buffer, PROPERTY_SEPARATOR);
for (i = 0; sdbd_config[i].key; i++) {
if (!strcmp(tok, sdbd_config[i].key)) {
tok = strtok(NULL, PROPERTY_SEPARATOR);
strncpy(sdbd_config[i].value, tok, PROPERTY_VALUE_MAX);
D("property init key=%s, value=%s\n", sdbd_config[i].key, tok);
}
}
}
sdb_close(fd);
D("called property_init\n");
}
void property_save()
{
int fd;
int i = 0;
char buffer[PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX+1];
mutex_lock(&env_lock);
if (access(TIZEN_PROPERTY_FILE, F_OK) == 0) // if exist
sdb_unlink(TIZEN_PROPERTY_FILE);
fd = unix_open(TIZEN_PROPERTY_FILE, O_WRONLY | O_CREAT | O_APPEND, 0640);
if (fd <0 ) {
mutex_unlock(&env_lock);
return;
}
for (i = 0; sdbd_config[i].key; i++) {
sprintf(buffer,"%s%s%s\n", sdbd_config[i].key, PROPERTY_SEPARATOR, sdbd_config[i].value);
sdb_write(fd, buffer, strlen(buffer));
}
sdb_close(fd);
mutex_unlock(&env_lock);
}
int property_set(const char *key, const char *value)
{
int i = 0;
mutex_lock(&env_lock);
for (i = 0; sdbd_config[i].key; i++) {
if (!strcmp(key,sdbd_config[i].key)) {
strncpy(sdbd_config[i].value, value, PROPERTY_VALUE_MAX);
D("property set key=%s, value=%s\n", key, value);
break;
}
}
mutex_unlock(&env_lock);
property_save();
return -1;
}
int property_get(const char *key, char *value, const char *default_value)
{
int len = 0;
int i = 0;
property_init();
mutex_lock(&env_lock);
for (i = 0; sdbd_config[i].key; i++) {
if (!strcmp(key,sdbd_config[i].key)) {
len = strlen(sdbd_config[i].value);
memcpy(value, sdbd_config[i].value, len + 1);
D("property get key=%s, value=%s\n", key, value);
mutex_unlock(&env_lock);
return len;
}
}
if(default_value) {
len = strlen(default_value);
memcpy(value, default_value, len + 1);
D("by default, property get key=%s, value=%s\n", key, value);
}
mutex_unlock(&env_lock);
return len;
}
int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
void *cookie)
{
return 0;
}
int read_line(const int fd, char* ptr, const unsigned int maxlen)
{
unsigned int n = 0;
char c[2];
int rc;
while(n != maxlen) {
if((rc = sdb_read(fd, c, 1)) != 1)
return -1; // eof or read err
if(*c == '\n') {
ptr[n] = 0;
return n;
}
ptr[n++] = *c;
}
return -1; // no space
}
#elif defined(HAVE_LIBC_SYSTEM_PROPERTIES)
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
//#include <sys/_system_properties.h>
int property_set(const char *key, const char *value)
{
return __system_property_set(key, value);
}
int property_get(const char *key, char *value, const char *default_value)
{
int len;
len = __system_property_get(key, value);
if(len > 0) {
return len;
}
if(default_value) {
len = strlen(default_value);
memcpy(value, default_value, len + 1);
}
return len;
}
int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
void *cookie)
{
char name[PROP_NAME_MAX];
char value[PROP_VALUE_MAX];
const prop_info *pi;
unsigned n;
for(n = 0; (pi = __system_property_find_nth(n)); n++) {
__system_property_read(pi, name, value);
propfn(name, value, cookie);
}
return 0;
}
#elif defined(HAVE_SYSTEM_PROPERTY_SERVER)
/*
* The Linux simulator provides a "system property server" that uses IPC
* to set/get/list properties. The file descriptor is shared by all
* threads in the process, so we use a mutex to ensure that requests
* from multiple threads don't get interleaved.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <pthread.h>
static pthread_once_t gInitOnce = PTHREAD_ONCE_INIT;
static pthread_mutex_t gPropertyFdLock = PTHREAD_MUTEX_INITIALIZER;
static int gPropFd = -1;
/*
* Connect to the properties server.
*
* Returns the socket descriptor on success.
*/
static int connectToServer(const char* fileName)
{
int sock = -1;
int cc;
struct sockaddr_un addr;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
D("UNIX domain socket create failed (errno=%d)\n", errno);
return -1;
}
/* connect to socket; fails if file doesn't exist */
strcpy(addr.sun_path, fileName); // max 108 bytes
addr.sun_family = AF_UNIX;
cc = connect(sock, (struct sockaddr*) &addr, SUN_LEN(&addr));
if (cc < 0) {
// ENOENT means socket file doesn't exist
// ECONNREFUSED means socket exists but nobody is listening
D("AF_UNIX connect failed for '%s': %s\n",
fileName, strerror(errno));
sdb_close(sock);
return -1;
}
return sock;
}
/*
* Perform one-time initialization.
*/
static void init(void)
{
assert(gPropFd == -1);
gPropFd = connectToServer(SYSTEM_PROPERTY_PIPE_NAME);
if (gPropFd < 0) {
D("not connected to system property server\n");
} else {
D("Connected to system property server\n");
}
}
int property_get(const char *key, char *value, const char *default_value)
{
char sendBuf[1+PROPERTY_KEY_MAX];
char recvBuf[1+PROPERTY_VALUE_MAX];
int len = -1;
D("PROPERTY GET [%s]\n", key);
pthread_once(&gInitOnce, init);
if (gPropFd < 0) {
/* this mimics the behavior of the device implementation */
if (default_value != NULL) {
strcpy(value, default_value);
len = strlen(value);
}
return len;
}
if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
memset(sendBuf, 0xdd, sizeof(sendBuf)); // placate valgrind
sendBuf[0] = (char) kSystemPropertyGet;
strcpy(sendBuf+1, key);
pthread_mutex_lock(&gPropertyFdLock);
if (sdb_write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
pthread_mutex_unlock(&gPropertyFdLock);
return -1;
}
if (sdb_read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
pthread_mutex_unlock(&gPropertyFdLock);
return -1;
}
pthread_mutex_unlock(&gPropertyFdLock);
/* first byte is 0 if value not defined, 1 if found */
if (recvBuf[0] == 0) {
if (default_value != NULL) {
strcpy(value, default_value);
len = strlen(value);
} else {
/*
* If the value isn't defined, hand back an empty string and
* a zero length, rather than a failure. This seems wrong,
* since you can't tell the difference between "undefined" and
* "defined but empty", but it's what the device does.
*/
value[0] = '\0';
len = 0;
}
} else if (recvBuf[0] == 1) {
strcpy(value, recvBuf+1);
len = strlen(value);
} else {
D("Got strange response to property_get request (%d)\n",
recvBuf[0]);
assert(0);
return -1;
}
D("PROP [found=%d def='%s'] (%d) [%s]: [%s]\n",
recvBuf[0], default_value, len, key, value);
return len;
}
int property_set(const char *key, const char *value)
{
char sendBuf[1+PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX];
char recvBuf[1];
int result = -1;
D("PROPERTY SET [%s]: [%s]\n", key, value);
pthread_once(&gInitOnce, init);
if (gPropFd < 0)
return -1;
if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
if (strlen(value) >= PROPERTY_VALUE_MAX) return -1;
memset(sendBuf, 0xdd, sizeof(sendBuf)); // placate valgrind
sendBuf[0] = (char) kSystemPropertySet;
strcpy(sendBuf+1, key);
strcpy(sendBuf+1+PROPERTY_KEY_MAX, value);
pthread_mutex_lock(&gPropertyFdLock);
if (sdb_write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
pthread_mutex_unlock(&gPropertyFdLock);
return -1;
}
if (sdb_read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
pthread_mutex_unlock(&gPropertyFdLock);
return -1;
}
pthread_mutex_unlock(&gPropertyFdLock);
if (recvBuf[0] != 1)
return -1;
return 0;
}
int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
void *cookie)
{
D("PROPERTY LIST\n");
pthread_once(&gInitOnce, init);
if (gPropFd < 0)
return -1;
return 0;
}
#else
/* SUPER-cheesy place-holder implementation for Win32 */
#define HAVE_PTHREADS /*tizen specific */
#include <stdio.h>
#include "threads.h"
static mutex_t env_lock = MUTEX_INITIALIZER;
int property_get(const char *key, char *value, const char *default_value)
{
char ename[PROPERTY_KEY_MAX + 6];
char *p;
int len;
len = strlen(key);
if(len >= PROPERTY_KEY_MAX) return -1;
memcpy(ename, "PROP_", 5);
memcpy(ename + 5, key, len + 1);
mutex_lock(&env_lock);
p = getenv(ename);
if(p == 0) p = "";
len = strlen(p);
if(len >= PROPERTY_VALUE_MAX) {
len = PROPERTY_VALUE_MAX - 1;
}
if((len == 0) && default_value) {
len = strlen(default_value);
memcpy(value, default_value, len + 1);
} else {
memcpy(value, p, len);
value[len] = 0;
}
mutex_unlock(&env_lock);
D("get [key=%s value='%s:%s']\n", key, ename, value);
return len;
}
int property_set(const char *key, const char *value)
{
char ename[PROPERTY_KEY_MAX + 6];
char *p;
int len;
int r;
if(strlen(value) >= PROPERTY_VALUE_MAX) return -1;
len = strlen(key);
if(len >= PROPERTY_KEY_MAX) return -1;
memcpy(ename, "PROP_", 5);
memcpy(ename + 5, key, len + 1);
mutex_lock(&env_lock);
#ifdef HAVE_MS_C_RUNTIME
{
char temp[256];
snprintf( temp, sizeof(temp), "%s=%s", ename, value);
putenv(temp);
r = 0;
}
#else
r = setenv(ename, value, 1);
#endif
mutex_unlock(&env_lock);
D("set [key=%s value='%s%s']\n", key, ename, value);
return r;
}
int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
void *cookie)
{
return 0;
}
#endif
|