Subversion Repositories HelenOS-historic

Rev

Rev 1326 | Rev 1735 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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