Subversion Repositories HelenOS-historic

Rev

Rev 1325 | Rev 1735 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1321 vana 1
/*
2
 * Copyright (C) 2006 Jakub Vana
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1317 vana 29
#include <sysinfo/sysinfo.h>
30
#include <mm/slab.h>
31
#include <print.h>
1318 vana 32
#include <syscall/copy.h>
1317 vana 33
 
1326 decky 34
sysinfo_item_t *_root = NULL;
1317 vana 35
 
36
 
1326 decky 37
static sysinfo_item_t *sysinfo_find_item(const char *name, sysinfo_item_t *subtree)
1317 vana 38
{
1326 decky 39
    if (subtree == NULL)
40
        return NULL;
41
 
42
    while (subtree != NULL) {
43
        int i = 0;
44
        char *a = (char *) name;
45
        char *b = subtree->name;
46
 
47
        while ((a[i] == b[i]) && (b[i]))
48
            i++;
49
 
50
        if ((!a[i]) && (!b[i]))  /* Last name in path matches */
51
            return subtree;
52
 
53
        if ((a[i] == '.') && (!b[i])) { /* Middle name in path matches */
54
            if (subtree->subinfo_type == SYSINFO_SUBINFO_TABLE)
55
                return sysinfo_find_item(a + i + 1, subtree->subinfo.table);
56
 
57
            //if (subtree->subinfo_type == SYSINFO_SUBINFO_FUNCTION) /* Subinfo managed by subsystem */
58
            //  return NULL; 
59
 
60
            return NULL; /* No subinfo */
1317 vana 61
        }
1326 decky 62
        /* No matches try next */
63
        subtree = subtree->next;
64
        i = 0;
1317 vana 65
    }
66
    return NULL;
67
}
68
 
1326 decky 69
static sysinfo_item_t *sysinfo_create_path(const char *name, sysinfo_item_t **psubtree)
1317 vana 70
{
71
    sysinfo_item_t *subtree;
72
    subtree = *psubtree;
73
 
1326 decky 74
    if (subtree == NULL) {
75
            sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
76
            int i = 0, j;
1317 vana 77
 
78
            ASSERT(item);
79
            *psubtree = item;
1326 decky 80
            item->next = NULL;
81
            item->val_type = SYSINFO_VAL_UNDEFINED;
82
            item->subinfo.table = NULL;
1317 vana 83
 
1326 decky 84
            while (name[i] && (name[i] != '.'))
85
                i++;
1317 vana 86
 
1326 decky 87
            item->name = malloc(i, 0);
88
            ASSERT(item->name);
1317 vana 89
 
1326 decky 90
            for (j = 0; j < i; j++)
91
                item->name[j] = name[j];
92
            item->name[j] = 0;
1317 vana 93
 
1326 decky 94
            if (name[i]) { /* =='.' */
95
                item->subinfo_type = SYSINFO_SUBINFO_TABLE;
96
                return sysinfo_create_path(name + i + 1, &(item->subinfo.table));
1317 vana 97
            }
1326 decky 98
            item->subinfo_type = SYSINFO_SUBINFO_NONE;
1317 vana 99
            return item;
100
    }
101
 
1326 decky 102
    while (subtree != NULL) {
103
        int i = 0, j;
104
        char *a = (char *) name;
105
        char *b = subtree->name;
106
 
107
        while ((a[i] == b[i]) && (b[i]))
108
            i++;
109
 
110
        if ((!a[i]) && (!b[i])) /* Last name in path matches */
111
            return subtree;
112
 
113
        if ((a[i] == '.') && (!b[i])) { /* Middle name in path matches */
114
            if (subtree->subinfo_type == SYSINFO_SUBINFO_TABLE)
115
                return sysinfo_create_path(a + i + 1, &(subtree->subinfo.table));
116
 
117
            if (subtree->subinfo_type == SYSINFO_SUBINFO_NONE) {
118
                subtree->subinfo_type = SYSINFO_SUBINFO_TABLE;
119
                return sysinfo_create_path(a + i + 1,&(subtree->subinfo.table));
120
            }
121
 
122
            //if (subtree->subinfo_type == SYSINFO_SUBINFO_FUNCTION) /* Subinfo managed by subsystem */
123
            //  return NULL; 
124
 
1317 vana 125
            return NULL;
126
        }
127
        /* No matches try next or create new*/
1326 decky 128
        if (subtree->next == NULL) {
129
            sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
1317 vana 130
 
131
            ASSERT(item);
1326 decky 132
            subtree->next = item;
133
            item->next = NULL;
134
            item->val_type = SYSINFO_VAL_UNDEFINED;
135
            item->subinfo.table = NULL;
1317 vana 136
 
1326 decky 137
            i = 0;
138
            while (name[i] && (name[i] != '.'))
139
                i++;
1317 vana 140
 
1326 decky 141
            item->name = malloc(i, 0);
142
            ASSERT(item->name);
143
 
144
            for (j = 0; j < i; j++)
145
                item->name[j] = name[j];
146
 
147
            item->name[j] = 0;
1317 vana 148
 
1326 decky 149
            if(name[i]) { /* =='.' */
150
                item->subinfo_type = SYSINFO_SUBINFO_TABLE;
151
                return sysinfo_create_path(name + i + 1, &(item->subinfo.table));
1317 vana 152
            }
1326 decky 153
            item->subinfo_type = SYSINFO_SUBINFO_NONE;
1317 vana 154
            return item;
1326 decky 155
        } else {
156
            subtree = subtree->next;
157
            i = 0;
1317 vana 158
        }  
159
    }
160
    panic("Not reached\n");
161
    return NULL;
162
}
163
 
1326 decky 164
void sysinfo_set_item_val(const char *name, sysinfo_item_t **root, __native val)
1317 vana 165
{
1326 decky 166
    if (root == NULL)
167
        root = &_root;
168
 
169
    /* If already created create only returns pointer
170
       If not, create it */
171
    sysinfo_item_t *item = sysinfo_create_path(name, root);
172
 
173
    if (item != NULL) { /* If in subsystem, unable to create or return so unable to set */
1317 vana 174
        item->val.val=val;                  
1326 decky 175
        item->val_type = SYSINFO_VAL_VAL;
176
    }
1317 vana 177
}
178
 
1326 decky 179
void sysinfo_set_item_function(const char *name, sysinfo_item_t **root, sysinfo_val_fn_t fn)
1317 vana 180
{
1326 decky 181
    if (root == NULL)
182
        root = &_root;
183
 
184
    /* If already created create only returns pointer
185
       If not, create it */
186
    sysinfo_item_t *item = sysinfo_create_path(name, root);
187
 
188
    if (item != NULL) { /* If in subsystem, unable to create or return so  unable to set */
1317 vana 189
        item->val.fn=fn;                  
1326 decky 190
        item->val_type = SYSINFO_VAL_FUNCTION;
191
    }
1317 vana 192
}
193
 
194
 
1326 decky 195
void sysinfo_set_item_undefined(const char *name, sysinfo_item_t **root)
1317 vana 196
{
1326 decky 197
    if (root == NULL)
198
        root = &_root;
199
 
200
    /* If already created create only returns pointer
201
       If not, create it */
202
    sysinfo_item_t *item = sysinfo_create_path(name, root);
203
 
204
    if (item != NULL)
205
        item->val_type = SYSINFO_VAL_UNDEFINED;
1317 vana 206
}
207
 
208
 
1326 decky 209
void sysinfo_dump(sysinfo_item_t **proot, int depth)
1317 vana 210
{
211
    sysinfo_item_t *root;
1326 decky 212
    if (proot == NULL)
213
        proot = &_root;
214
 
1317 vana 215
    root = *proot;
216
 
1326 decky 217
    while (root != NULL) {
1317 vana 218
        int i;
1326 decky 219
        __native val = 0;
220
        char *vtype = NULL;
1317 vana 221
 
222
 
1326 decky 223
        for (i = 0; i < depth; i++)
224
            printf("  ");
1317 vana 225
 
1326 decky 226
        switch (root->val_type) {
227
            case SYSINFO_VAL_UNDEFINED:
228
                val = 0;
229
                vtype = "UND";
230
                break;
231
            case SYSINFO_VAL_VAL:
232
                val = root->val.val;
233
                vtype = "VAL";
234
                break;
235
            case SYSINFO_VAL_FUNCTION:
236
                val = ((sysinfo_val_fn_t) (root->val.fn)) (root);
237
                vtype = "FUN";
238
                break;
239
        }
1317 vana 240
 
1326 decky 241
        printf("%s    %s val:%d(%X) sub:%s\n", root->name, vtype, val, val, (root->subinfo_type == SYSINFO_SUBINFO_NONE) ? "NON" : ((root->subinfo_type == SYSINFO_SUBINFO_TABLE) ? "TAB" : "FUN"));
1317 vana 242
 
1326 decky 243
        if (root->subinfo_type == SYSINFO_SUBINFO_TABLE)
244
            sysinfo_dump(&(root -> subinfo.table), depth + 1);
245
 
246
        root = root->next;
247
    }
1317 vana 248
}
249
 
1326 decky 250
sysinfo_rettype_t sysinfo_get_val(const char *name, sysinfo_item_t **root)
1317 vana 251
{
1326 decky 252
    // TODO: Implement Subsystem subinfo (by function implemented subinfo)
1317 vana 253
 
1326 decky 254
    sysinfo_rettype_t ret = {0, false};
1317 vana 255
 
1326 decky 256
    if (root == NULL)
257
        root = &_root;
258
 
259
    sysinfo_item_t *item = sysinfo_find_item(name, *root);
260
 
261
    if (item != NULL) {
262
        if (item->val_type == SYSINFO_VAL_UNDEFINED)
1317 vana 263
            return ret;
1326 decky 264
        else
265
            ret.valid = true;
266
 
267
        if (item->val_type == SYSINFO_VAL_VAL)
268
            ret.val = item->val.val;
269
        else
270
            ret.val = ((sysinfo_val_fn_t) (item->val.fn)) (item);
1317 vana 271
    }
272
    return ret;
273
}
1318 vana 274
 
1326 decky 275
__native sys_sysinfo_valid(__native ptr, __native len)
1318 vana 276
{
277
    char *str;
1326 decky 278
    sysinfo_rettype_t ret = {0, 0};
279
    str = malloc(len + 1, 0);
280
 
1318 vana 281
    ASSERT(str);
1326 decky 282
    if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len])))
283
        ret = sysinfo_get_val(str, NULL);
284
 
1318 vana 285
    free(str);
286
    return ret.valid;
287
}
288
 
1326 decky 289
__native sys_sysinfo_value(__native ptr, __native len)
1318 vana 290
{
291
    char *str;
1326 decky 292
    sysinfo_rettype_t ret = {0, 0};
293
    str = malloc(len + 1, 0);
294
 
1318 vana 295
    ASSERT(str);
1326 decky 296
    if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len])))
297
        ret = sysinfo_get_val(str, NULL);
298
 
1318 vana 299
    free(str);
300
    return ret.val;
301
}