watchprops.c revision 81963164113f98f5917c7d7990ae2b76f2d4044e
1#include <stdio.h>
2#include <stdlib.h>
3#include <time.h>
4
5#include <cutils/properties.h>
6
7#include <sys/atomics.h>
8
9#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
10#include <sys/_system_properties.h>
11
12typedef struct pwatch pwatch;
13
14struct pwatch
15{
16    const prop_info *pi;
17    unsigned serial;
18};
19
20static pwatch watchlist[1024];
21
22static void announce(const prop_info *pi)
23{
24    char name[PROP_NAME_MAX];
25    char value[PROP_VALUE_MAX];
26    char *x;
27
28    __system_property_read(pi, name, value);
29
30    for(x = value; *x; x++) {
31        if((*x < 32) || (*x > 127)) *x = '.';
32    }
33
34    fprintf(stderr,"%10d %s = '%s'\n", (int) time(0), name, value);
35}
36
37int watchprops_main(int argc, char *argv[])
38{
39    unsigned serial = 0;
40    unsigned count;
41    unsigned n;
42
43    for(n = 0; n < 1024; n++) {
44        watchlist[n].pi = __system_property_find_nth(n);
45        if (watchlist[n].pi == 0)
46            break;
47        watchlist[n].serial = __system_property_serial(watchlist[n].pi);
48    }
49
50    count = n;
51    if (count == 1024)
52        exit(1);
53
54    for(;;) {
55        serial = __system_property_wait_any(serial);
56        while(count < 1024){
57            watchlist[count].pi = __system_property_find_nth(count);
58            if (watchlist[count].pi == 0)
59                break;
60            watchlist[count].serial = __system_property_serial(watchlist[n].pi);
61            announce(watchlist[count].pi);
62            count++;
63            if(count == 1024) exit(1);
64        }
65
66        for(n = 0; n < count; n++){
67            unsigned tmp = __system_property_serial(watchlist[n].pi);
68            if(watchlist[n].serial != tmp) {
69                announce(watchlist[n].pi);
70                watchlist[n].serial = tmp;
71            }
72        }
73    }
74    return 0;
75}
76