summaryrefslogtreecommitdiffstats
path: root/toolbox/watchprops.c
blob: bf82882f13217f86bc2da5953a890ad99143f4ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>

#include <cutils/properties.h>
#include <cutils/hashmap.h>

#include <sys/atomics.h>

#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include <sys/_system_properties.h>

static int str_hash(void *key)
{
    return hashmapHash(key, strlen(key));
}

static bool str_equals(void *keyA, void *keyB)
{
    return strcmp(keyA, keyB) == 0;
}

static void announce(char *name, char *value)
{
    char *x;
    
    for(x = value; *x; x++) {
        if((*x < 32) || (*x > 127)) *x = '.';
    }

    fprintf(stderr,"%10d %s = '%s'\n", (int) time(0), name, value);
}

static void add_to_watchlist(Hashmap *watchlist, const char *name,
        const prop_info *pi)
{
    char *key = strdup(name);
    unsigned *value = malloc(sizeof(unsigned));
    if (!key || !value)
        exit(1);

    *value = __system_property_serial(pi);
    hashmapPut(watchlist, key, value);
}

static void populate_watchlist(const prop_info *pi, void *cookie)
{
    Hashmap *watchlist = cookie;
    char name[PROP_NAME_MAX];
    char value_unused[PROP_VALUE_MAX];

    __system_property_read(pi, name, value_unused);
    add_to_watchlist(watchlist, name, pi);
}

static void update_watchlist(const prop_info *pi, void *cookie)
{
    Hashmap *watchlist = cookie;
    char name[PROP_NAME_MAX];
    char value[PROP_VALUE_MAX];
    unsigned *serial;

    __system_property_read(pi, name, value);
    serial = hashmapGet(watchlist, name);
    if (!serial) {
        add_to_watchlist(watchlist, name, pi);
        announce(name, value);
    } else {
        unsigned tmp = __system_property_serial(pi);
        if (*serial != tmp) {
            *serial = tmp;
            announce(name, value);
        }
    }
}

int watchprops_main(int argc, char *argv[])
{
    unsigned serial = 0;
    unsigned count = 0;
    unsigned n;
    
    Hashmap *watchlist = hashmapCreate(1024, str_hash, str_equals);
    if (!watchlist)
        exit(1);

    __system_property_foreach(populate_watchlist, watchlist);

    for(;;) {
        serial = __system_property_wait_any(serial);
        __system_property_foreach(update_watchlist, watchlist);
    }
    return 0;
}