blob: bf82882f13217f86bc2da5953a890ad99143f4ab [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#include <stdio.h>
2#include <stdlib.h>
3#include <time.h>
Greg Hackmann68e9b6b2013-02-13 10:49:50 -08004#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08005
6#include <cutils/properties.h>
Greg Hackmann68e9b6b2013-02-13 10:49:50 -08007#include <cutils/hashmap.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08008
9#include <sys/atomics.h>
10
11#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
12#include <sys/_system_properties.h>
13
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080014static int str_hash(void *key)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080015{
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080016 return hashmapHash(key, strlen(key));
17}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080019static bool str_equals(void *keyA, void *keyB)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080020{
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080021 return strcmp(keyA, keyB) == 0;
22}
23
24static void announce(char *name, char *value)
25{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026 char *x;
27
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028 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
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080035static 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
47static 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
57static 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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078int watchprops_main(int argc, char *argv[])
79{
Colin Cross81963162013-01-28 17:14:04 -080080 unsigned serial = 0;
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080081 unsigned count = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082 unsigned n;
83
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080084 Hashmap *watchlist = hashmapCreate(1024, str_hash, str_equals);
85 if (!watchlist)
Colin Cross81963162013-01-28 17:14:04 -080086 exit(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080088 __system_property_foreach(populate_watchlist, watchlist);
89
Colin Cross81963162013-01-28 17:14:04 -080090 for(;;) {
91 serial = __system_property_wait_any(serial);
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080092 __system_property_foreach(update_watchlist, watchlist);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093 }
94 return 0;
95}