~rsalveti/+junk/hybris-wip

« back to all changes in this revision

Viewing changes to debian/patches/0008-Implement-get-set-property-via-property-service.patch

  • Committer: Ricardo Salveti de Araujo
  • Date: 2013-08-06 15:32:08 UTC
  • Revision ID: ricardo.salveti@canonical.com-20130806153208-yky0725pkqqnw9ja
* New upstream snapshot
* Updating patches against latest upstream, dropping (already merged):
  -

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
From 9982f70b68aea1dc162cf08f121674914a30f24f Mon Sep 17 00:00:00 2001
 
2
From: Ricardo Salveti de Araujo <ricardo.salveti@canonical.com>
 
3
Date: Mon, 22 Jul 2013 17:36:19 -0300
 
4
Subject: [PATCH] Implement get/set property via property service
 
5
 
 
6
The property service is running inside the android init daemon and
 
7
creates a socket in /dev/socket/property_service which we can use
 
8
for setting properties (getting is done via an extra android patch
 
9
to add 'get' and 'list' support in the property service).
 
10
 
 
11
Signed-off-by: Ricardo Salveti de Araujo <ricardo.salveti@canonical.com>
 
12
---
 
13
 hybris/Makefile.am                            |   4 +-
 
14
 hybris/common/Makefile.am                     |   2 +-
 
15
 hybris/common/hooks.c                         |   3 +-
 
16
 hybris/common/properties.c                    | 179 ----------------
 
17
 hybris/common/properties.h                    |  26 ---
 
18
 hybris/configure.ac                           |   2 +
 
19
 hybris/include/Makefile.am                    |   4 +
 
20
 hybris/include/hybris/properties/properties.h |  58 ++++++
 
21
 hybris/properties/Makefile.am                 |  10 +
 
22
 hybris/properties/properties.c                | 287 ++++++++++++++++++++++++++
 
23
 hybris/utils/Makefile.am                      |  15 ++
 
24
 hybris/utils/getprop.c                        |  75 +++++++
 
25
 hybris/utils/setprop.c                        |  35 ++++
 
26
 13 files changed, 491 insertions(+), 209 deletions(-)
 
27
 delete mode 100644 hybris/common/properties.c
 
28
 delete mode 100644 hybris/common/properties.h
 
29
 create mode 100644 hybris/include/hybris/properties/properties.h
 
30
 create mode 100644 hybris/properties/Makefile.am
 
31
 create mode 100644 hybris/properties/properties.c
 
32
 create mode 100644 hybris/utils/Makefile.am
 
33
 create mode 100644 hybris/utils/getprop.c
 
34
 create mode 100644 hybris/utils/setprop.c
 
35
 
 
36
diff --git a/hybris/Makefile.am b/hybris/Makefile.am
 
37
index 8cd7eb1..ee0f593 100644
 
38
--- a/hybris/Makefile.am
 
39
+++ b/hybris/Makefile.am
 
40
@@ -1,4 +1,4 @@
 
41
-SUBDIRS = include common hardware
 
42
+SUBDIRS = include properties common hardware
 
43
 
 
44
 if HAS_ANDROID_4_2_0
 
45
 SUBDIRS += libsync
 
46
@@ -6,7 +6,7 @@ endif
 
47
 
 
48
 SUBDIRS += egl glesv1 glesv2 ui sf input camera media
 
49
 SUBDIRS += libnfc_nxp libnfc_ndef_nxp
 
50
-SUBDIRS += tests
 
51
+SUBDIRS += utils tests
 
52
 
 
53
 MAINTAINERCLEANFILES = \
 
54
        aclocal.m4 compile config.guess config.sub \
 
55
diff --git a/hybris/common/Makefile.am b/hybris/common/Makefile.am
 
56
index ddece59..0b78b96 100644
 
57
--- a/hybris/common/Makefile.am
 
58
+++ b/hybris/common/Makefile.am
 
59
@@ -17,7 +17,6 @@ endif
 
60
 libhybris_common_la_SOURCES = \
 
61
        hooks.c \
 
62
        hooks_shm.c \
 
63
-       properties.c \
 
64
        strlcpy.c \
 
65
        logging.c
 
66
 libhybris_common_la_CFLAGS = \
 
67
@@ -33,4 +32,5 @@ libhybris_common_la_LDFLAGS = \
 
68
        -ldl \
 
69
        -lrt \
 
70
        -pthread \
 
71
+       $(top_builddir)/properties/libandroid-properties.la \
 
72
        -version-info "$(LT_CURRENT)":"$(LT_REVISION)":"$(LT_AGE)"
 
73
diff --git a/hybris/common/hooks.c b/hybris/common/hooks.c
 
74
index 08f599d..715cf25 100644
 
75
--- a/hybris/common/hooks.c
 
76
+++ b/hybris/common/hooks.c
 
77
@@ -19,7 +19,6 @@
 
78
 
 
79
 #include <hybris/internal/floating_point_abi.h>
 
80
 
 
81
-#include "properties.h"
 
82
 #include "hooks_shm.h"
 
83
 
 
84
 #define _GNU_SOURCE
 
85
@@ -53,6 +52,8 @@
 
86
 #include <syslog.h>
 
87
 #include <locale.h>
 
88
 
 
89
+#include <hybris/properties/properties.h>
 
90
+
 
91
 static locale_t hybris_locale;
 
92
 static int locale_inited = 0;
 
93
 /* TODO:
 
94
diff --git a/hybris/common/properties.c b/hybris/common/properties.c
 
95
deleted file mode 100644
 
96
index 8ed27fd..0000000
 
97
--- a/hybris/common/properties.c
 
98
+++ /dev/null
 
99
@@ -1,179 +0,0 @@
 
100
-/*
 
101
- * Copyright (c) 2012 Carsten Munk <carsten.munk@gmail.com>
 
102
- *
 
103
- * Licensed under the Apache License, Version 2.0 (the "License");
 
104
- * you may not use this file except in compliance with the License.
 
105
- * You may obtain a copy of the License at
 
106
- *
 
107
- * http://www.apache.org/licenses/LICENSE-2.0
 
108
- *
 
109
- * Unless required by applicable law or agreed to in writing, software
 
110
- * distributed under the License is distributed on an "AS IS" BASIS,
 
111
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
112
- * See the License for the specific language governing permissions and
 
113
- * limitations under the License.
 
114
- *
 
115
- */
 
116
-
 
117
-#include <stddef.h>
 
118
-#include <string.h>
 
119
-#include <stdio.h>
 
120
-#include <stdlib.h>
 
121
-#include <fcntl.h>
 
122
-#include <sys/types.h>
 
123
-#include <sys/stat.h>
 
124
-#include "logging.h"
 
125
-
 
126
-#define PROP_NAME_MAX 32
 
127
-
 
128
-static char *find_key(const char *key)
 
129
-{
 
130
-       FILE *f = fopen("/system/build.prop", "r");
 
131
-       char buf[1024];
 
132
-       char *mkey, *value;
 
133
-
 
134
-       if (!f)
 
135
-               return NULL;
 
136
-
 
137
-       while (fgets(buf, 1024, f) != NULL) {
 
138
-               if (strchr(buf, '\r'))
 
139
-                       *(strchr(buf, '\r')) = '\0';
 
140
-               if (strchr(buf, '\n'))
 
141
-                       *(strchr(buf, '\n')) = '\0';
 
142
-
 
143
-               mkey = strtok(buf, "=");
 
144
-
 
145
-               if (!mkey)
 
146
-                       continue;
 
147
-
 
148
-               value = strtok(NULL, "=");
 
149
-               if (!value)
 
150
-                       continue;
 
151
-
 
152
-               if (strcmp(key, mkey) == 0) {
 
153
-                       fclose(f);
 
154
-                       return strdup(value);
 
155
-               }
 
156
-       }
 
157
-
 
158
-       fclose(f);
 
159
-       return NULL;
 
160
-}
 
161
-
 
162
-static char *find_key_kernel_cmdline(const char *key)
 
163
-{
 
164
-       char cmdline[1024];
 
165
-       char *ptr;
 
166
-       int fd;
 
167
-
 
168
-       fd = open("/proc/cmdline", O_RDONLY);
 
169
-       if (fd >= 0) {
 
170
-               int n = read(fd, cmdline, 1023);
 
171
-               if (n < 0) n = 0;
 
172
-
 
173
-               /* get rid of trailing newline, it happens */
 
174
-               if (n > 0 && cmdline[n-1] == '\n') n--;
 
175
-
 
176
-               cmdline[n] = 0;
 
177
-               close(fd);
 
178
-       } else {
 
179
-               cmdline[0] = 0;
 
180
-       }
 
181
-
 
182
-       ptr = cmdline;
 
183
-
 
184
-       while (ptr && *ptr) {
 
185
-               char *x = strchr(ptr, ' ');
 
186
-               if (x != 0) *x++ = 0;
 
187
-
 
188
-               char *name = ptr;
 
189
-               ptr = x;
 
190
-
 
191
-               char *value = strchr(name, '=');
 
192
-               int name_len = strlen(name);
 
193
-
 
194
-               if (value == 0) continue;
 
195
-               *value++ = 0;
 
196
-               if (name_len == 0) continue;
 
197
-
 
198
-               if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
 
199
-                       const char *boot_prop_name = name + 12;
 
200
-                       char prop[PROP_NAME_MAX];
 
201
-                       snprintf(prop, sizeof(prop), "ro.%s", boot_prop_name);
 
202
-                       if (strcmp(prop, key) == 0)
 
203
-                               return strdup(value);
 
204
-               }
 
205
-       }
 
206
-
 
207
-       return NULL;
 
208
-}
 
209
-
 
210
-int property_get(const char *key, char *value, const char *default_value)
 
211
-{
 
212
-       char *ret = NULL; 
 
213
-
 
214
-       //printf("property_get: %s\n", key);
 
215
-
 
216
-       /* default */
 
217
-       ret = find_key(key);
 
218
-
 
219
-#if 0
 
220
- if (strcmp(key, "ro.kernel.qemu") == 0)
 
221
- {
 
222
-    ret = "0";
 
223
- }  
 
224
- else if (strcmp(key, "ro.hardware") == 0)
 
225
- { 
 
226
-    ret = "tenderloin";
 
227
- } 
 
228
- else if (strcmp(key, "ro.product.board") == 0)
 
229
- {
 
230
-    ret = "tenderloin";
 
231
- }
 
232
- else if (strcmp(key, "ro.board.platform") == 0)
 
233
- { 
 
234
-    ret = "msm8660";
 
235
- }
 
236
- else if (strcmp(key, "ro.arch") == 0)
 
237
- {
 
238
-    ret = "armeabi";
 
239
- }
 
240
- else if (strcmp(key, "debug.composition.type") == 0)
 
241
- {
 
242
-    ret = "c2d"; 
 
243
- }
 
244
- else if (strcmp(key, "debug.sf.hw") == 0)
 
245
- {
 
246
-   ret = "1";
 
247
- }
 
248
- else if (strcmp(key, "debug.gr.numframebuffers") == 0)
 
249
- { 
 
250
-   ret = "1"; 
 
251
- }  
 
252
-#endif
 
253
-       if (ret == NULL) {
 
254
-               /* Property might be available via /proc/cmdline */
 
255
-               ret = find_key_kernel_cmdline(key);
 
256
-       }
 
257
-
 
258
-       if (ret) {
 
259
-               TRACE("found %s for %s\n", key, ret);
 
260
-               strcpy(value, ret);
 
261
-               free(ret);
 
262
-               return strlen(value);
 
263
-       } else if (default_value != NULL) {
 
264
-               strcpy(value, default_value);
 
265
-               return strlen(value);
 
266
-       }
 
267
-
 
268
-       return 0;
 
269
-}
 
270
-
 
271
-int property_set(const char *key, const char *value)
 
272
-{
 
273
-       printf("property_set: %s %s\n", key, value);
 
274
-       TRACE("property_set: %s %s\n", key, value);
 
275
-       return 0;
 
276
-}
 
277
-
 
278
-// vim:ts=4:sw=4:noexpandtab
 
279
diff --git a/hybris/common/properties.h b/hybris/common/properties.h
 
280
deleted file mode 100644
 
281
index ea4e28f..0000000
 
282
--- a/hybris/common/properties.h
 
283
+++ /dev/null
 
284
@@ -1,26 +0,0 @@
 
285
-/*
 
286
- * Copyright (c) 2012 Carsten Munk <carsten.munk@gmail.com>
 
287
- *
 
288
- * Licensed under the Apache License, Version 2.0 (the "License");
 
289
- * you may not use this file except in compliance with the License.
 
290
- * You may obtain a copy of the License at
 
291
- *
 
292
- * http://www.apache.org/licenses/LICENSE-2.0
 
293
- *
 
294
- * Unless required by applicable law or agreed to in writing, software
 
295
- * distributed under the License is distributed on an "AS IS" BASIS,
 
296
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
297
- * See the License for the specific language governing permissions and
 
298
- * limitations under the License.
 
299
- *
 
300
- */
 
301
-
 
302
-#ifndef PROPERTIES_H_
 
303
-#define PROPERTIES_H_
 
304
-
 
305
-int property_set(const char *key, const char *value);
 
306
-int property_get(const char *key, char *value, const char *default_value);
 
307
-
 
308
-#endif
 
309
-
 
310
-// vim:ts=4:sw=4:noexpandtab
 
311
diff --git a/hybris/configure.ac b/hybris/configure.ac
 
312
index a9fdfa4..94e209b 100644
 
313
--- a/hybris/configure.ac
 
314
+++ b/hybris/configure.ac
 
315
@@ -133,6 +133,7 @@ AM_CONDITIONAL([HAS_ANDROID_2_3_0], [test $android_headers_major -ge 2 -a $andro
 
316
 
 
317
 AC_CONFIG_FILES([
 
318
        Makefile
 
319
+       properties/Makefile
 
320
        common/Makefile
 
321
        common/gingerbread/Makefile
 
322
        common/ics/Makefile
 
323
@@ -165,6 +166,7 @@ AC_CONFIG_FILES([
 
324
        camera/Makefile
 
325
        media/Makefile
 
326
        include/Makefile
 
327
+       utils/Makefile
 
328
        tests/Makefile
 
329
 ])
 
330
 
 
331
diff --git a/hybris/include/Makefile.am b/hybris/include/Makefile.am
 
332
index bd33587..349c276 100644
 
333
--- a/hybris/include/Makefile.am
 
334
+++ b/hybris/include/Makefile.am
 
335
@@ -70,3 +70,7 @@ mediainclude_HEADERS = \
 
336
        hybris/media/surface_texture_client_hybris.h \
 
337
        hybris/media/recorder_compatibility_layer.h
 
338
 
 
339
+propertiesincludedir = $(includedir)/hybris/properties
 
340
+propertiesinclude_HEADERS = \
 
341
+       hybris/properties/properties.h
 
342
+
 
343
diff --git a/hybris/include/hybris/properties/properties.h b/hybris/include/hybris/properties/properties.h
 
344
new file mode 100644
 
345
index 0000000..12d56b6
 
346
--- /dev/null
 
347
+++ b/hybris/include/hybris/properties/properties.h
 
348
@@ -0,0 +1,58 @@
 
349
+/*
 
350
+ * Copyright (c) 2012 Carsten Munk <carsten.munk@gmail.com>
 
351
+ *               2013 Simon Busch <morphis@gravedo.de>
 
352
+ *               2008 The Android Open Source Project
 
353
+ *               2013 Canonical Ltd
 
354
+ *
 
355
+ * Licensed under the Apache License, Version 2.0 (the "License");
 
356
+ * you may not use this file except in compliance with the License.
 
357
+ * You may obtain a copy of the License at
 
358
+ *
 
359
+ * http://www.apache.org/licenses/LICENSE-2.0
 
360
+ *
 
361
+ * Unless required by applicable law or agreed to in writing, software
 
362
+ * distributed under the License is distributed on an "AS IS" BASIS,
 
363
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
364
+ * See the License for the specific language governing permissions and
 
365
+ * limitations under the License.
 
366
+ *
 
367
+ */
 
368
+
 
369
+#ifndef PROPERTIES_H_
 
370
+#define PROPERTIES_H_
 
371
+
 
372
+#include <stdint.h>
 
373
+#include <unistd.h>
 
374
+#include <stdint.h>
 
375
+
 
376
+/* Based on Android */
 
377
+#define PROP_SERVICE_NAME "property_service"
 
378
+
 
379
+#define PROP_NAME_MAX 32
 
380
+#define PROP_VALUE_MAX 92
 
381
+
 
382
+/* Only SETPROP is defined by Android, for GETPROP and LISTPROP to work
 
383
+ * an extended Android init service needs to be in place */
 
384
+#define PROP_MSG_SETPROP 1
 
385
+#define PROP_MSG_GETPROP 2
 
386
+#define PROP_MSG_LISTPROP 3
 
387
+
 
388
+#ifdef __cplusplus
 
389
+extern "C" {
 
390
+#endif
 
391
+
 
392
+       typedef struct prop_msg_s {
 
393
+               unsigned cmd;
 
394
+               char name[PROP_NAME_MAX];
 
395
+               char value[PROP_VALUE_MAX];
 
396
+       } prop_msg_t;
 
397
+
 
398
+       int property_set(const char *key, const char *value);
 
399
+       int property_get(const char *key, char *value, const char *default_value);
 
400
+       int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie);
 
401
+
 
402
+#ifdef __cplusplus
 
403
+}
 
404
+#endif
 
405
+
 
406
+#endif // PROPERTIES_H_
 
407
diff --git a/hybris/properties/Makefile.am b/hybris/properties/Makefile.am
 
408
new file mode 100644
 
409
index 0000000..00f5d8d
 
410
--- /dev/null
 
411
+++ b/hybris/properties/Makefile.am
 
412
@@ -0,0 +1,10 @@
 
413
+lib_LTLIBRARIES = \
 
414
+       libandroid-properties.la
 
415
+
 
416
+libandroid_properties_la_SOURCES = properties.c
 
417
+libandroid_properties_la_CFLAGS = -I$(top_srcdir)/include
 
418
+if WANT_DEBUG
 
419
+libandroid_properties_la_CFLAGS += -ggdb -O0
 
420
+endif
 
421
+libandroid_properties_la_LDFLAGS = \
 
422
+       -version-info "1":"0":"0"
 
423
diff --git a/hybris/properties/properties.c b/hybris/properties/properties.c
 
424
new file mode 100644
 
425
index 0000000..cc350e2
 
426
--- /dev/null
 
427
+++ b/hybris/properties/properties.c
 
428
@@ -0,0 +1,287 @@
 
429
+/*
 
430
+ * Copyright (c) 2012 Carsten Munk <carsten.munk@gmail.com>
 
431
+ *               2008 The Android Open Source Project
 
432
+ *               2013 Simon Busch <morphis@gravedo.de>
 
433
+ *               2013 Canonical Ltd
 
434
+ *
 
435
+ * Licensed under the Apache License, Version 2.0 (the "License");
 
436
+ * you may not use this file except in compliance with the License.
 
437
+ * You may obtain a copy of the License at
 
438
+ *
 
439
+ * http://www.apache.org/licenses/LICENSE-2.0
 
440
+ *
 
441
+ * Unless required by applicable law or agreed to in writing, software
 
442
+ * distributed under the License is distributed on an "AS IS" BASIS,
 
443
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
444
+ * See the License for the specific language governing permissions and
 
445
+ * limitations under the License.
 
446
+ *
 
447
+ */
 
448
+
 
449
+#include <stddef.h>
 
450
+#include <string.h>
 
451
+#include <stdio.h>
 
452
+#include <stdlib.h>
 
453
+#include <fcntl.h>
 
454
+#include <sys/types.h>
 
455
+#include <sys/stat.h>
 
456
+#define __USE_GNU
 
457
+#include <unistd.h>
 
458
+#include <errno.h>
 
459
+#include <sys/socket.h>
 
460
+#include <sys/un.h>
 
461
+#include <sys/select.h>
 
462
+#include <sys/types.h>
 
463
+#include <netinet/in.h>
 
464
+#include <poll.h>
 
465
+
 
466
+#include <hybris/properties/properties.h>
 
467
+
 
468
+static const char property_service_socket[] = "/dev/socket/" PROP_SERVICE_NAME;
 
469
+
 
470
+/* Find a key value from a static /system/build.prop file */
 
471
+static char *find_key(const char *key)
 
472
+{
 
473
+       FILE *f = fopen("/system/build.prop", "r");
 
474
+       char buf[1024];
 
475
+       char *mkey, *value;
 
476
+
 
477
+       if (!f)
 
478
+               return NULL;
 
479
+
 
480
+       while (fgets(buf, 1024, f) != NULL) {
 
481
+               if (strchr(buf, '\r'))
 
482
+                       *(strchr(buf, '\r')) = '\0';
 
483
+               if (strchr(buf, '\n'))
 
484
+                       *(strchr(buf, '\n')) = '\0';
 
485
+
 
486
+               mkey = strtok(buf, "=");
 
487
+
 
488
+               if (!mkey)
 
489
+                       continue;
 
490
+
 
491
+               value = strtok(NULL, "=");
 
492
+               if (!value)
 
493
+                       continue;
 
494
+
 
495
+               if (strcmp(key, mkey) == 0) {
 
496
+                       fclose(f);
 
497
+                       return strdup(value);
 
498
+               }
 
499
+       }
 
500
+
 
501
+       fclose(f);
 
502
+       return NULL;
 
503
+}
 
504
+
 
505
+/* Find a key value from the kernel command line, which is parsed
 
506
+ * by Android at init (on an Android working system) */
 
507
+static char *find_key_kernel_cmdline(const char *key)
 
508
+{
 
509
+       char cmdline[1024];
 
510
+       char *ptr;
 
511
+       int fd;
 
512
+
 
513
+       fd = open("/proc/cmdline", O_RDONLY);
 
514
+       if (fd >= 0) {
 
515
+               int n = read(fd, cmdline, 1023);
 
516
+               if (n < 0) n = 0;
 
517
+
 
518
+               /* get rid of trailing newline, it happens */
 
519
+               if (n > 0 && cmdline[n-1] == '\n') n--;
 
520
+
 
521
+               cmdline[n] = 0;
 
522
+               close(fd);
 
523
+       } else {
 
524
+               cmdline[0] = 0;
 
525
+       }
 
526
+
 
527
+       ptr = cmdline;
 
528
+
 
529
+       while (ptr && *ptr) {
 
530
+               char *x = strchr(ptr, ' ');
 
531
+               if (x != 0) *x++ = 0;
 
532
+
 
533
+               char *name = ptr;
 
534
+               ptr = x;
 
535
+
 
536
+               char *value = strchr(name, '=');
 
537
+               int name_len = strlen(name);
 
538
+
 
539
+               if (value == 0) continue;
 
540
+               *value++ = 0;
 
541
+               if (name_len == 0) continue;
 
542
+
 
543
+               if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
 
544
+                       const char *boot_prop_name = name + 12;
 
545
+                       char prop[PROP_NAME_MAX];
 
546
+                       snprintf(prop, sizeof(prop), "ro.%s", boot_prop_name);
 
547
+                       if (strcmp(prop, key) == 0)
 
548
+                               return strdup(value);
 
549
+               }
 
550
+       }
 
551
+
 
552
+       return NULL;
 
553
+}
 
554
+
 
555
+/* Get/Set a property from the Android Init property socket */
 
556
+static int send_prop_msg(prop_msg_t *msg,
 
557
+               void (*propfn)(const char *, const char *, void *),
 
558
+               void *cookie)
 
559
+{
 
560
+       struct pollfd pollfds[1];
 
561
+       union {
 
562
+               struct sockaddr_un addr;
 
563
+               struct sockaddr addr_g;
 
564
+       } addr;
 
565
+       socklen_t alen;
 
566
+       size_t namelen;
 
567
+       int s;
 
568
+       int r;
 
569
+       int result = -1;
 
570
+
 
571
+       s = socket(AF_LOCAL, SOCK_STREAM, 0);
 
572
+       if (s < 0) {
 
573
+               return result;
 
574
+       }
 
575
+
 
576
+       memset(&addr, 0, sizeof(addr));
 
577
+       namelen = strlen(property_service_socket);
 
578
+       strncpy(addr.addr.sun_path, property_service_socket,
 
579
+                       sizeof(addr.addr.sun_path));
 
580
+       addr.addr.sun_family = AF_LOCAL;
 
581
+       alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
 
582
+
 
583
+       if (TEMP_FAILURE_RETRY(connect(s, &addr.addr_g, alen) < 0)) {
 
584
+               close(s);
 
585
+               return result;
 
586
+       }
 
587
+
 
588
+       r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg_t), 0));
 
589
+
 
590
+       if (r == sizeof(prop_msg_t)) {
 
591
+               pollfds[0].fd = s;
 
592
+               pollfds[0].events = 0;
 
593
+               // We successfully wrote to the property server, so use recv
 
594
+               // in case we need to get a property. Once the other side is
 
595
+               // finished, the socket is closed.
 
596
+               while ((r = recv(s, msg, sizeof(prop_msg_t), 0)) > 0) {
 
597
+                       if (r != sizeof(prop_msg_t)) {
 
598
+                               close(s);
 
599
+                               return result;
 
600
+                       }
 
601
+
 
602
+                       if (propfn)
 
603
+                               propfn(msg->name, msg->value, cookie);
 
604
+               }
 
605
+
 
606
+               if (r >= 0)
 
607
+                       result = 0;
 
608
+       }
 
609
+
 
610
+       close(s);
 
611
+       return result;
 
612
+}
 
613
+
 
614
+int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
 
615
+               void *cookie)
 
616
+{
 
617
+       int err;
 
618
+       prop_msg_t msg;
 
619
+
 
620
+       memset(&msg, 0, sizeof(msg));
 
621
+       msg.cmd = PROP_MSG_LISTPROP;
 
622
+
 
623
+       err = send_prop_msg(&msg, propfn, cookie);
 
624
+       if (err < 0) {
 
625
+               return err;
 
626
+       }
 
627
+
 
628
+       return 0;
 
629
+}
 
630
+
 
631
+static int property_get_socket(const char *key, char *value, const char *default_value)
 
632
+{
 
633
+       int err;
 
634
+       int len;
 
635
+       prop_msg_t msg;
 
636
+
 
637
+       memset(&msg, 0, sizeof(msg));
 
638
+       msg.cmd = PROP_MSG_GETPROP;
 
639
+
 
640
+       if (key) {
 
641
+               strncpy(msg.name, key, sizeof(msg.name));
 
642
+               err = send_prop_msg(&msg, NULL, NULL);
 
643
+               if (err < 0)
 
644
+                       return err;
 
645
+       }
 
646
+
 
647
+       /* In case it's null, just use the default */
 
648
+       if ((strlen(msg.value) == 0) && (default_value)) {
 
649
+               if (strlen(default_value) >= PROP_VALUE_MAX) return -1;
 
650
+               len = strlen(default_value);
 
651
+               memcpy(msg.value, default_value, len + 1);
 
652
+       }
 
653
+
 
654
+       strncpy(value, msg.value, sizeof(msg.value));
 
655
+
 
656
+       return 0;
 
657
+}
 
658
+
 
659
+int property_get(const char *key, char *value, const char *default_value)
 
660
+{
 
661
+       char *ret = NULL;
 
662
+
 
663
+       if ((key) && (strlen(key) >= PROP_NAME_MAX)) return -1;
 
664
+
 
665
+       if (property_get_socket(key, value, default_value) == 0) {
 
666
+               if (value)
 
667
+                       return strlen(value);
 
668
+               else
 
669
+                       return 0;
 
670
+       }
 
671
+
 
672
+       /* In case the socket is not available, parse the file by hand */
 
673
+       if ((ret = find_key(key)) == NULL) {
 
674
+               /* Property might be available via /proc/cmdline */
 
675
+               ret = find_key_kernel_cmdline(key);
 
676
+       }
 
677
+
 
678
+       if (ret) {
 
679
+               strcpy(value, ret);
 
680
+               free(ret);
 
681
+               return strlen(value);
 
682
+       } else if (default_value != NULL) {
 
683
+               strcpy(value, default_value);
 
684
+               return strlen(value);
 
685
+       } else {
 
686
+               value = '\0';
 
687
+       }
 
688
+
 
689
+       return 0;
 
690
+}
 
691
+
 
692
+int property_set(const char *key, const char *value)
 
693
+{
 
694
+       int err;
 
695
+       prop_msg_t msg;
 
696
+
 
697
+       if (key == 0) return -1;
 
698
+       if (value == 0) value = "";
 
699
+       if (strlen(key) >= PROP_NAME_MAX) return -1;
 
700
+       if (strlen(value) >= PROP_VALUE_MAX) return -1;
 
701
+
 
702
+       memset(&msg, 0, sizeof(msg));
 
703
+       msg.cmd = PROP_MSG_SETPROP;
 
704
+       strncpy(msg.name, key, sizeof(msg.name));
 
705
+       strncpy(msg.value, value, sizeof(msg.value));
 
706
+
 
707
+       err = send_prop_msg(&msg, NULL, NULL);
 
708
+       if (err < 0) {
 
709
+               return err;
 
710
+       }
 
711
+
 
712
+       return 0;
 
713
+}
 
714
+
 
715
+// vim:ts=4:sw=4:noexpandtab
 
716
diff --git a/hybris/utils/Makefile.am b/hybris/utils/Makefile.am
 
717
new file mode 100644
 
718
index 0000000..47dcc62
 
719
--- /dev/null
 
720
+++ b/hybris/utils/Makefile.am
 
721
@@ -0,0 +1,15 @@
 
722
+bin_PROGRAMS = \
 
723
+       getprop \
 
724
+       setprop
 
725
+
 
726
+getprop_SOURCES = getprop.c
 
727
+getprop_CFLAGS = \
 
728
+       -I$(top_srcdir)/include
 
729
+getprop_LDADD = \
 
730
+       $(top_builddir)/properties/libandroid-properties.la
 
731
+
 
732
+setprop_SOURCES = setprop.c
 
733
+setprop_CFLAGS = \
 
734
+       -I$(top_srcdir)/include
 
735
+setprop_LDADD = \
 
736
+       $(top_builddir)/properties/libandroid-properties.la
 
737
diff --git a/hybris/utils/getprop.c b/hybris/utils/getprop.c
 
738
new file mode 100644
 
739
index 0000000..c0110d1
 
740
--- /dev/null
 
741
+++ b/hybris/utils/getprop.c
 
742
@@ -0,0 +1,75 @@
 
743
+/*
 
744
+ * Copyright (c) 2008 The Android Open Source Project
 
745
+ *               2013 Canonical Ltd
 
746
+ *
 
747
+ * Licensed under the Apache License, Version 2.0 (the "License");
 
748
+ * you may not use this file except in compliance with the License.
 
749
+ * You may obtain a copy of the License at
 
750
+ *
 
751
+ * http://www.apache.org/licenses/LICENSE-2.0
 
752
+ *
 
753
+ * Unless required by applicable law or agreed to in writing, software
 
754
+ * distributed under the License is distributed on an "AS IS" BASIS,
 
755
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
756
+ * See the License for the specific language governing permissions and
 
757
+ * limitations under the License.
 
758
+ *
 
759
+ */
 
760
+
 
761
+#include <stdio.h>
 
762
+#include <stdlib.h>
 
763
+#include <string.h>
 
764
+
 
765
+#include <hybris/properties/properties.h>
 
766
+
 
767
+typedef struct {
 
768
+       int count;
 
769
+       char** items;
 
770
+} list_t;
 
771
+
 
772
+static void record_prop(const char* key, const char* name, void* opaque)
 
773
+{
 
774
+       list_t *list = (list_t *) opaque;
 
775
+
 
776
+       char temp[PROP_VALUE_MAX + PROP_NAME_MAX + 16];
 
777
+       snprintf(temp, sizeof(temp), "[%s]: [%s]", key, name);
 
778
+       list->items = realloc(list->items, (list->count + 1) * sizeof(char **));
 
779
+       list->items[list->count++] = strdup(temp);
 
780
+}
 
781
+
 
782
+static void list_properties(void)
 
783
+{
 
784
+       int n;
 
785
+
 
786
+       list_t list;
 
787
+       memset(&list, 0, sizeof(list_t));
 
788
+
 
789
+       /* Record properties in the string list */
 
790
+       if (property_list(record_prop, &list) < 0)
 
791
+               return;
 
792
+
 
793
+       for (n = 0; n < list.count; n++) {
 
794
+               printf("%s\n", (char *) list.items[n]);
 
795
+       }
 
796
+}
 
797
+
 
798
+int main(int argc, char *argv[])
 
799
+{
 
800
+       int n = 0;
 
801
+
 
802
+       if (argc == 1) {
 
803
+               list_properties();
 
804
+       } else {
 
805
+               char value[PROP_VALUE_MAX];
 
806
+               char *default_value;
 
807
+               if (argc > 2) {
 
808
+                       default_value = argv[2];
 
809
+               } else {
 
810
+                       default_value = "";
 
811
+               }
 
812
+
 
813
+               property_get(argv[1], value, default_value);
 
814
+               printf("%s\n", value);
 
815
+       }
 
816
+       return 0;
 
817
+}
 
818
diff --git a/hybris/utils/setprop.c b/hybris/utils/setprop.c
 
819
new file mode 100644
 
820
index 0000000..fc08178
 
821
--- /dev/null
 
822
+++ b/hybris/utils/setprop.c
 
823
@@ -0,0 +1,35 @@
 
824
+/*
 
825
+ * Copyright (c) 2008 The Android Open Source Project
 
826
+ *               2013 Canonical Ltd
 
827
+ *
 
828
+ * Licensed under the Apache License, Version 2.0 (the "License");
 
829
+ * you may not use this file except in compliance with the License.
 
830
+ * You may obtain a copy of the License at
 
831
+ *
 
832
+ * http://www.apache.org/licenses/LICENSE-2.0
 
833
+ *
 
834
+ * Unless required by applicable law or agreed to in writing, software
 
835
+ * distributed under the License is distributed on an "AS IS" BASIS,
 
836
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
837
+ * See the License for the specific language governing permissions and
 
838
+ * limitations under the License.
 
839
+ *
 
840
+ */
 
841
+
 
842
+#include <stdio.h>
 
843
+#include <hybris/properties/properties.h>
 
844
+
 
845
+int main(int argc, char *argv[])
 
846
+{
 
847
+       if (argc != 3) {
 
848
+               fprintf(stderr, "usage: setprop <key> <value>\n");
 
849
+               return 1;
 
850
+       }
 
851
+
 
852
+       if (property_set(argv[1], argv[2])){
 
853
+               fprintf(stderr, "could not set property\n");
 
854
+               return 1;
 
855
+       }
 
856
+
 
857
+       return 0;
 
858
+}
 
859
-- 
 
860
1.8.3.2
 
861