summaryrefslogtreecommitdiffstats
path: root/toolbox/watchprops.c
diff options
context:
space:
mode:
authorColin Cross <ccross@android.com>2013-06-19 10:33:42 -0700
committerAndroid Git Automerger <android-git-automerger@android.com>2013-06-19 10:33:42 -0700
commita44c31cc91bce7124e896adb072a10f2740a96b4 (patch)
tree52dba7f95c49242933ea5eb88da7a380e704f1c7 /toolbox/watchprops.c
parent19188955b50ef6cb133780f66384e9fa055acfac (diff)
parente755dfd43812ebd9a0d0d6a2106316f343ffb5a2 (diff)
downloadsystem_core-a44c31cc91bce7124e896adb072a10f2740a96b4.zip
system_core-a44c31cc91bce7124e896adb072a10f2740a96b4.tar.gz
system_core-a44c31cc91bce7124e896adb072a10f2740a96b4.tar.bz2
am e755dfd4: Merge changes I1db3137b,I0f66144e
* commit 'e755dfd43812ebd9a0d0d6a2106316f343ffb5a2': toolbox: reimplement watchprops using __system_property_foreach() libcutils: reimplement property_list() using __system_property_foreach()
Diffstat (limited to 'toolbox/watchprops.c')
-rw-r--r--toolbox/watchprops.c98
1 files changed, 59 insertions, 39 deletions
diff --git a/toolbox/watchprops.c b/toolbox/watchprops.c
index 3418d62..bf82882 100644
--- a/toolbox/watchprops.c
+++ b/toolbox/watchprops.c
@@ -1,32 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
+#include <errno.h>
#include <cutils/properties.h>
+#include <cutils/hashmap.h>
#include <sys/atomics.h>
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include <sys/_system_properties.h>
-typedef struct pwatch pwatch;
-
-struct pwatch
+static int str_hash(void *key)
{
- const prop_info *pi;
- unsigned serial;
-};
+ return hashmapHash(key, strlen(key));
+}
-static pwatch watchlist[1024];
+static bool str_equals(void *keyA, void *keyB)
+{
+ return strcmp(keyA, keyB) == 0;
+}
-static void announce(const prop_info *pi)
+static void announce(char *name, char *value)
{
- char name[PROP_NAME_MAX];
- char value[PROP_VALUE_MAX];
char *x;
- __system_property_read(pi, name, value);
-
for(x = value; *x; x++) {
if((*x < 32) || (*x > 127)) *x = '.';
}
@@ -34,42 +32,64 @@ static void announce(const prop_info *pi)
fprintf(stderr,"%10d %s = '%s'\n", (int) time(0), name, value);
}
+static void add_to_watchlist(Hashmap *watchlist, const char *name,
+ const prop_info *pi)
+{
+ char *key = strdup(name);
+ unsigned *value = malloc(sizeof(unsigned));
+ if (!key || !value)
+ exit(1);
+
+ *value = __system_property_serial(pi);
+ hashmapPut(watchlist, key, value);
+}
+
+static void populate_watchlist(const prop_info *pi, void *cookie)
+{
+ Hashmap *watchlist = cookie;
+ char name[PROP_NAME_MAX];
+ char value_unused[PROP_VALUE_MAX];
+
+ __system_property_read(pi, name, value_unused);
+ add_to_watchlist(watchlist, name, pi);
+}
+
+static void update_watchlist(const prop_info *pi, void *cookie)
+{
+ Hashmap *watchlist = cookie;
+ char name[PROP_NAME_MAX];
+ char value[PROP_VALUE_MAX];
+ unsigned *serial;
+
+ __system_property_read(pi, name, value);
+ serial = hashmapGet(watchlist, name);
+ if (!serial) {
+ add_to_watchlist(watchlist, name, pi);
+ announce(name, value);
+ } else {
+ unsigned tmp = __system_property_serial(pi);
+ if (*serial != tmp) {
+ *serial = tmp;
+ announce(name, value);
+ }
+ }
+}
+
int watchprops_main(int argc, char *argv[])
{
unsigned serial = 0;
- unsigned count;
+ unsigned count = 0;
unsigned n;
- for(n = 0; n < 1024; n++) {
- watchlist[n].pi = __system_property_find_nth(n);
- if (watchlist[n].pi == 0)
- break;
- watchlist[n].serial = __system_property_serial(watchlist[n].pi);
- }
-
- count = n;
- if (count == 1024)
+ Hashmap *watchlist = hashmapCreate(1024, str_hash, str_equals);
+ if (!watchlist)
exit(1);
+ __system_property_foreach(populate_watchlist, watchlist);
+
for(;;) {
serial = __system_property_wait_any(serial);
- while(count < 1024){
- watchlist[count].pi = __system_property_find_nth(count);
- if (watchlist[count].pi == 0)
- break;
- watchlist[count].serial = __system_property_serial(watchlist[n].pi);
- announce(watchlist[count].pi);
- count++;
- if(count == 1024) exit(1);
- }
-
- for(n = 0; n < count; n++){
- unsigned tmp = __system_property_serial(watchlist[n].pi);
- if(watchlist[n].serial != tmp) {
- announce(watchlist[n].pi);
- watchlist[n].serial = tmp;
- }
- }
+ __system_property_foreach(update_watchlist, watchlist);
}
return 0;
}