| 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 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 9 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ | 
|  | 10 | #include <sys/_system_properties.h> | 
|  | 11 |  | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 12 | static int str_hash(void *key) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 13 | { | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 14 | return hashmapHash(key, strlen(key)); | 
|  | 15 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 16 |  | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 17 | static bool str_equals(void *keyA, void *keyB) | 
| 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 | return strcmp(keyA, keyB) == 0; | 
|  | 20 | } | 
|  | 21 |  | 
|  | 22 | static void announce(char *name, char *value) | 
|  | 23 | { | 
| Greg Hackmann | eeed45f | 2014-05-21 10:39:44 -0700 | [diff] [blame] | 24 | unsigned char *x; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 |  | 
| Greg Hackmann | eeed45f | 2014-05-21 10:39:44 -0700 | [diff] [blame] | 26 | for(x = (unsigned char *)value; *x; x++) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | if((*x < 32) || (*x > 127)) *x = '.'; | 
|  | 28 | } | 
|  | 29 |  | 
|  | 30 | fprintf(stderr,"%10d %s = '%s'\n", (int) time(0), name, value); | 
|  | 31 | } | 
|  | 32 |  | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 33 | static void add_to_watchlist(Hashmap *watchlist, const char *name, | 
|  | 34 | const prop_info *pi) | 
|  | 35 | { | 
|  | 36 | char *key = strdup(name); | 
|  | 37 | unsigned *value = malloc(sizeof(unsigned)); | 
|  | 38 | if (!key || !value) | 
|  | 39 | exit(1); | 
|  | 40 |  | 
|  | 41 | *value = __system_property_serial(pi); | 
|  | 42 | hashmapPut(watchlist, key, value); | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | static void populate_watchlist(const prop_info *pi, void *cookie) | 
|  | 46 | { | 
|  | 47 | Hashmap *watchlist = cookie; | 
|  | 48 | char name[PROP_NAME_MAX]; | 
|  | 49 | char value_unused[PROP_VALUE_MAX]; | 
|  | 50 |  | 
|  | 51 | __system_property_read(pi, name, value_unused); | 
|  | 52 | add_to_watchlist(watchlist, name, pi); | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | static void update_watchlist(const prop_info *pi, void *cookie) | 
|  | 56 | { | 
|  | 57 | Hashmap *watchlist = cookie; | 
|  | 58 | char name[PROP_NAME_MAX]; | 
|  | 59 | char value[PROP_VALUE_MAX]; | 
|  | 60 | unsigned *serial; | 
|  | 61 |  | 
|  | 62 | __system_property_read(pi, name, value); | 
|  | 63 | serial = hashmapGet(watchlist, name); | 
|  | 64 | if (!serial) { | 
|  | 65 | add_to_watchlist(watchlist, name, pi); | 
|  | 66 | announce(name, value); | 
|  | 67 | } else { | 
|  | 68 | unsigned tmp = __system_property_serial(pi); | 
|  | 69 | if (*serial != tmp) { | 
|  | 70 | *serial = tmp; | 
|  | 71 | announce(name, value); | 
|  | 72 | } | 
|  | 73 | } | 
|  | 74 | } | 
|  | 75 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | int watchprops_main(int argc, char *argv[]) | 
|  | 77 | { | 
| Mark Salyzyn | aa90776 | 2014-05-08 09:31:43 -0700 | [diff] [blame] | 78 | unsigned serial; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 79 |  | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 80 | Hashmap *watchlist = hashmapCreate(1024, str_hash, str_equals); | 
|  | 81 | if (!watchlist) | 
| Colin Cross | 8196316 | 2013-01-28 17:14:04 -0800 | [diff] [blame] | 82 | exit(1); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 83 |  | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 84 | __system_property_foreach(populate_watchlist, watchlist); | 
|  | 85 |  | 
| Mark Salyzyn | aa90776 | 2014-05-08 09:31:43 -0700 | [diff] [blame] | 86 | for(serial = 0;;) { | 
| Colin Cross | 8196316 | 2013-01-28 17:14:04 -0800 | [diff] [blame] | 87 | serial = __system_property_wait_any(serial); | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 88 | __system_property_foreach(update_watchlist, watchlist); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | } | 
|  | 90 | return 0; | 
|  | 91 | } |