The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <time.h> |
Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 4 | #include <errno.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 5 | |
| 6 | #include <cutils/properties.h> |
Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 7 | #include <cutils/hashmap.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 8 | |
| 9 | #include <sys/atomics.h> |
| 10 | |
| 11 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 12 | #include <sys/_system_properties.h> |
| 13 | |
Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 14 | static int str_hash(void *key) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 15 | { |
Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 16 | return hashmapHash(key, strlen(key)); |
| 17 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 18 | |
Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 19 | static bool str_equals(void *keyA, void *keyB) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 20 | { |
Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 21 | return strcmp(keyA, keyB) == 0; |
| 22 | } |
| 23 | |
| 24 | static void announce(char *name, char *value) |
| 25 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | char *x; |
| 27 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | |
Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 78 | int watchprops_main(int argc, char *argv[]) |
| 79 | { |
Colin Cross | 8196316 | 2013-01-28 17:14:04 -0800 | [diff] [blame] | 80 | unsigned serial = 0; |
Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 81 | unsigned count = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | unsigned n; |
| 83 | |
Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 84 | Hashmap *watchlist = hashmapCreate(1024, str_hash, str_equals); |
| 85 | if (!watchlist) |
Colin Cross | 8196316 | 2013-01-28 17:14:04 -0800 | [diff] [blame] | 86 | exit(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 87 | |
Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 88 | __system_property_foreach(populate_watchlist, watchlist); |
| 89 | |
Colin Cross | 8196316 | 2013-01-28 17:14:04 -0800 | [diff] [blame] | 90 | for(;;) { |
| 91 | serial = __system_property_wait_any(serial); |
Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 92 | __system_property_foreach(update_watchlist, watchlist); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | } |
| 94 | return 0; |
| 95 | } |