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