Home | History | Annotate | Download | only in toolbox
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <time.h>
      4 #include <errno.h>
      5 
      6 #include <cutils/properties.h>
      7 #include <cutils/hashmap.h>
      8 
      9 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
     10 #include <sys/_system_properties.h>
     11 
     12 static int str_hash(void *key)
     13 {
     14     return hashmapHash(key, strlen(key));
     15 }
     16 
     17 static bool str_equals(void *keyA, void *keyB)
     18 {
     19     return strcmp(keyA, keyB) == 0;
     20 }
     21 
     22 static void announce(char *name, char *value)
     23 {
     24     unsigned char *x;
     25 
     26     for(x = (unsigned char *)value; *x; x++) {
     27         if((*x < 32) || (*x > 127)) *x = '.';
     28     }
     29 
     30     fprintf(stderr,"%10d %s = '%s'\n", (int) time(0), name, value);
     31 }
     32 
     33 static void add_to_watchlist(Hashmap *watchlist, const char *name,
     34         const prop_info *pi)
     35 {
     36     char *key = strdup(name);
     37     unsigned *value = malloc(sizeof(unsigned));
     38     if (!key || !value)
     39         exit(1);
     40 
     41     *value = __system_property_serial(pi);
     42     hashmapPut(watchlist, key, value);
     43 }
     44 
     45 static void populate_watchlist(const prop_info *pi, void *cookie)
     46 {
     47     Hashmap *watchlist = cookie;
     48     char name[PROP_NAME_MAX];
     49     char value_unused[PROP_VALUE_MAX];
     50 
     51     __system_property_read(pi, name, value_unused);
     52     add_to_watchlist(watchlist, name, pi);
     53 }
     54 
     55 static void update_watchlist(const prop_info *pi, void *cookie)
     56 {
     57     Hashmap *watchlist = cookie;
     58     char name[PROP_NAME_MAX];
     59     char value[PROP_VALUE_MAX];
     60     unsigned *serial;
     61 
     62     __system_property_read(pi, name, value);
     63     serial = hashmapGet(watchlist, name);
     64     if (!serial) {
     65         add_to_watchlist(watchlist, name, pi);
     66         announce(name, value);
     67     } else {
     68         unsigned tmp = __system_property_serial(pi);
     69         if (*serial != tmp) {
     70             *serial = tmp;
     71             announce(name, value);
     72         }
     73     }
     74 }
     75 
     76 int watchprops_main(int argc, char *argv[])
     77 {
     78     unsigned serial;
     79 
     80     Hashmap *watchlist = hashmapCreate(1024, str_hash, str_equals);
     81     if (!watchlist)
     82         exit(1);
     83 
     84     __system_property_foreach(populate_watchlist, watchlist);
     85 
     86     for(serial = 0;;) {
     87         serial = __system_property_wait_any(serial);
     88         __system_property_foreach(update_watchlist, watchlist);
     89     }
     90     return 0;
     91 }
     92