blob: cd62922cf671c635c405251d242be5d78e8d48bc [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#include <stdio.h>
2#include <stdlib.h>
Elliott Hughes2b7d75d2015-01-24 20:03:09 -08003#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08004#include <time.h>
Greg Hackmann68e9b6b2013-02-13 10:49:50 -08005#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08006
7#include <cutils/properties.h>
Greg Hackmann68e9b6b2013-02-13 10:49:50 -08008#include <cutils/hashmap.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08009
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080010#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
11#include <sys/_system_properties.h>
12
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080013static int str_hash(void *key)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080014{
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080015 return hashmapHash(key, strlen(key));
16}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080017
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080018static bool str_equals(void *keyA, void *keyB)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019{
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080020 return strcmp(keyA, keyB) == 0;
21}
22
23static void announce(char *name, char *value)
24{
Greg Hackmanneeed45f2014-05-21 10:39:44 -070025 unsigned char *x;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
Greg Hackmanneeed45f2014-05-21 10:39:44 -070027 for(x = (unsigned char *)value; *x; x++) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028 if((*x < 32) || (*x > 127)) *x = '.';
29 }
30
31 fprintf(stderr,"%10d %s = '%s'\n", (int) time(0), name, value);
32}
33
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080034static 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
46static 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
56static 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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077int watchprops_main(int argc, char *argv[])
78{
Mark Salyzynaa907762014-05-08 09:31:43 -070079 unsigned serial;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080081 Hashmap *watchlist = hashmapCreate(1024, str_hash, str_equals);
82 if (!watchlist)
Colin Cross81963162013-01-28 17:14:04 -080083 exit(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080085 __system_property_foreach(populate_watchlist, watchlist);
86
Mark Salyzynaa907762014-05-08 09:31:43 -070087 for(serial = 0;;) {
Colin Cross81963162013-01-28 17:14:04 -080088 serial = __system_property_wait_any(serial);
Greg Hackmann68e9b6b2013-02-13 10:49:50 -080089 __system_property_foreach(update_watchlist, watchlist);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090 }
91 return 0;
92}