| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | #include <stdio.h> | 
|  | 2 | #include <stdlib.h> | 
| Elliott Hughes | 2b7d75d | 2015-01-24 20:03:09 -0800 | [diff] [blame] | 3 | #include <string.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 4 | #include <time.h> | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 5 | #include <errno.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 6 |  | 
|  | 7 | #include <cutils/properties.h> | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 8 | #include <cutils/hashmap.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 9 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 10 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ | 
|  | 11 | #include <sys/_system_properties.h> | 
|  | 12 |  | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 13 | static int str_hash(void *key) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 14 | { | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 15 | return hashmapHash(key, strlen(key)); | 
|  | 16 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 17 |  | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 18 | static bool str_equals(void *keyA, void *keyB) | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | { | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 20 | return strcmp(keyA, keyB) == 0; | 
|  | 21 | } | 
|  | 22 |  | 
|  | 23 | static void announce(char *name, char *value) | 
|  | 24 | { | 
| Greg Hackmann | eeed45f | 2014-05-21 10:39:44 -0700 | [diff] [blame] | 25 | unsigned char *x; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 |  | 
| Greg Hackmann | eeed45f | 2014-05-21 10:39:44 -0700 | [diff] [blame] | 27 | for(x = (unsigned char *)value; *x; x++) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | if((*x < 32) || (*x > 127)) *x = '.'; | 
|  | 29 | } | 
|  | 30 |  | 
|  | 31 | fprintf(stderr,"%10d %s = '%s'\n", (int) time(0), name, value); | 
|  | 32 | } | 
|  | 33 |  | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 34 | static 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 |  | 
|  | 46 | static 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 |  | 
|  | 56 | static 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 77 | int watchprops_main(int argc, char *argv[]) | 
|  | 78 | { | 
| Mark Salyzyn | aa90776 | 2014-05-08 09:31:43 -0700 | [diff] [blame] | 79 | unsigned serial; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 |  | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 81 | Hashmap *watchlist = hashmapCreate(1024, str_hash, str_equals); | 
|  | 82 | if (!watchlist) | 
| Colin Cross | 8196316 | 2013-01-28 17:14:04 -0800 | [diff] [blame] | 83 | exit(1); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 |  | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 85 | __system_property_foreach(populate_watchlist, watchlist); | 
|  | 86 |  | 
| Mark Salyzyn | aa90776 | 2014-05-08 09:31:43 -0700 | [diff] [blame] | 87 | for(serial = 0;;) { | 
| Colin Cross | 8196316 | 2013-01-28 17:14:04 -0800 | [diff] [blame] | 88 | serial = __system_property_wait_any(serial); | 
| Greg Hackmann | 68e9b6b | 2013-02-13 10:49:50 -0800 | [diff] [blame] | 89 | __system_property_foreach(update_watchlist, watchlist); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 90 | } | 
|  | 91 | return 0; | 
|  | 92 | } |