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