Subversion Repositories HelenOS

Rev

Rev 1963 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1963 Rev 1968
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
#include <sysinfo/sysinfo.h>
29
#include <sysinfo/sysinfo.h>
30
#include <mm/slab.h>
30
#include <mm/slab.h>
31
#include <print.h>
31
#include <print.h>
32
#include <syscall/copy.h>
32
#include <syscall/copy.h>
33
 
33
 
34
sysinfo_item_t *_root = NULL;
34
sysinfo_item_t *_root = NULL;
35
 
35
 
36
 
36
 
37
static sysinfo_item_t *sysinfo_find_item(const char *name, sysinfo_item_t *subtree)
37
static sysinfo_item_t *sysinfo_find_item(const char *name, sysinfo_item_t *subtree)
38
{
38
{
39
    if (subtree == NULL)
39
    if (subtree == NULL)
40
        return NULL;
40
        return NULL;
41
   
41
   
42
    while (subtree != NULL) {
42
    while (subtree != NULL) {
43
        int i = 0;
43
        int i = 0;
44
        char *a = (char *) name;
44
        char *a = (char *) name;
45
        char *b = subtree->name;
45
        char *b = subtree->name;
46
       
46
       
47
        while ((a[i] == b[i]) && (b[i]))
47
        while ((a[i] == b[i]) && (b[i]))
48
            i++;
48
            i++;
49
       
49
       
50
        if ((!a[i]) && (!b[i]))  /* Last name in path matches */
50
        if ((!a[i]) && (!b[i]))  /* Last name in path matches */
51
            return subtree;
51
            return subtree;
52
       
52
       
53
        if ((a[i] == '.') && (!b[i])) { /* Middle name in path matches */
53
        if ((a[i] == '.') && (!b[i])) { /* Middle name in path matches */
54
            if (subtree->subinfo_type == SYSINFO_SUBINFO_TABLE)
54
            if (subtree->subinfo_type == SYSINFO_SUBINFO_TABLE)
55
                return sysinfo_find_item(a + i + 1, subtree->subinfo.table);
55
                return sysinfo_find_item(a + i + 1, subtree->subinfo.table);
56
           
56
           
57
            //if (subtree->subinfo_type == SYSINFO_SUBINFO_FUNCTION) /* Subinfo managed by subsystem */
57
            //if (subtree->subinfo_type == SYSINFO_SUBINFO_FUNCTION) /* Subinfo managed by subsystem */
58
            //  return NULL; 
58
            //  return NULL; 
59
           
59
           
60
            return NULL; /* No subinfo */
60
            return NULL; /* No subinfo */
61
        }
61
        }
62
        /* No matches try next */
62
        /* No matches try next */
63
        subtree = subtree->next;
63
        subtree = subtree->next;
64
        i = 0;
64
        i = 0;
65
    }
65
    }
66
    return NULL;
66
    return NULL;
67
}
67
}
68
 
68
 
69
static sysinfo_item_t *sysinfo_create_path(const char *name, sysinfo_item_t **psubtree)
69
static sysinfo_item_t *sysinfo_create_path(const char *name, sysinfo_item_t **psubtree)
70
{
70
{
71
    sysinfo_item_t *subtree;
71
    sysinfo_item_t *subtree;
72
    subtree = *psubtree;
72
    subtree = *psubtree;
73
   
73
   
74
    if (subtree == NULL) {
74
    if (subtree == NULL) {
75
            sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
75
            sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
76
            int i = 0, j;
76
            int i = 0, j;
77
           
77
           
78
            ASSERT(item);
78
            ASSERT(item);
79
            *psubtree = item;
79
            *psubtree = item;
80
            item->next = NULL;
80
            item->next = NULL;
81
            item->val_type = SYSINFO_VAL_UNDEFINED;
81
            item->val_type = SYSINFO_VAL_UNDEFINED;
82
            item->subinfo.table = NULL;
82
            item->subinfo.table = NULL;
83
 
83
 
84
            while (name[i] && (name[i] != '.'))
84
            while (name[i] && (name[i] != '.'))
85
                i++;
85
                i++;
86
           
86
           
87
            item->name = malloc(i, 0);
87
            item->name = malloc(i, 0);
88
            ASSERT(item->name);
88
            ASSERT(item->name);
89
 
89
 
90
            for (j = 0; j < i; j++)
90
            for (j = 0; j < i; j++)
91
                item->name[j] = name[j];
91
                item->name[j] = name[j];
92
            item->name[j] = 0;
92
            item->name[j] = 0;
93
           
93
           
94
            if (name[i]) { /* =='.' */
94
            if (name[i]) { /* =='.' */
95
                item->subinfo_type = SYSINFO_SUBINFO_TABLE;
95
                item->subinfo_type = SYSINFO_SUBINFO_TABLE;
96
                return sysinfo_create_path(name + i + 1, &(item->subinfo.table));
96
                return sysinfo_create_path(name + i + 1, &(item->subinfo.table));
97
            }
97
            }
98
            item->subinfo_type = SYSINFO_SUBINFO_NONE;
98
            item->subinfo_type = SYSINFO_SUBINFO_NONE;
99
            return item;
99
            return item;
100
    }
100
    }
101
 
101
 
102
    while (subtree != NULL) {
102
    while (subtree != NULL) {
103
        int i = 0, j;
103
        int i = 0, j;
104
        char *a = (char *) name;
104
        char *a = (char *) name;
105
        char *b = subtree->name;
105
        char *b = subtree->name;
106
       
106
       
107
        while ((a[i] == b[i]) && (b[i]))
107
        while ((a[i] == b[i]) && (b[i]))
108
            i++;
108
            i++;
109
       
109
       
110
        if ((!a[i]) && (!b[i])) /* Last name in path matches */
110
        if ((!a[i]) && (!b[i])) /* Last name in path matches */
111
            return subtree;
111
            return subtree;
112
       
112
       
113
        if ((a[i] == '.') && (!b[i])) { /* Middle name in path matches */
113
        if ((a[i] == '.') && (!b[i])) { /* Middle name in path matches */
114
            if (subtree->subinfo_type == SYSINFO_SUBINFO_TABLE)
114
            if (subtree->subinfo_type == SYSINFO_SUBINFO_TABLE)
115
                return sysinfo_create_path(a + i + 1, &(subtree->subinfo.table));
115
                return sysinfo_create_path(a + i + 1, &(subtree->subinfo.table));
116
           
116
           
117
            if (subtree->subinfo_type == SYSINFO_SUBINFO_NONE) {
117
            if (subtree->subinfo_type == SYSINFO_SUBINFO_NONE) {
118
                subtree->subinfo_type = SYSINFO_SUBINFO_TABLE;
118
                subtree->subinfo_type = SYSINFO_SUBINFO_TABLE;
119
                return sysinfo_create_path(a + i + 1,&(subtree->subinfo.table));
119
                return sysinfo_create_path(a + i + 1,&(subtree->subinfo.table));
120
            }
120
            }
121
           
121
           
122
            //if (subtree->subinfo_type == SYSINFO_SUBINFO_FUNCTION) /* Subinfo managed by subsystem */
122
            //if (subtree->subinfo_type == SYSINFO_SUBINFO_FUNCTION) /* Subinfo managed by subsystem */
123
            //  return NULL; 
123
            //  return NULL; 
124
           
124
           
125
            return NULL;
125
            return NULL;
126
        }
126
        }
127
        /* No matches try next or create new*/
127
        /* No matches try next or create new*/
128
        if (subtree->next == NULL) {
128
        if (subtree->next == NULL) {
129
            sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
129
            sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
130
           
130
           
131
            ASSERT(item);
131
            ASSERT(item);
132
            subtree->next = item;
132
            subtree->next = item;
133
            item->next = NULL;
133
            item->next = NULL;
134
            item->val_type = SYSINFO_VAL_UNDEFINED;
134
            item->val_type = SYSINFO_VAL_UNDEFINED;
135
            item->subinfo.table = NULL;
135
            item->subinfo.table = NULL;
136
 
136
 
137
            i = 0;
137
            i = 0;
138
            while (name[i] && (name[i] != '.'))
138
            while (name[i] && (name[i] != '.'))
139
                i++;
139
                i++;
140
 
140
 
141
            item->name = malloc(i, 0);
141
            item->name = malloc(i, 0);
142
            ASSERT(item->name);
142
            ASSERT(item->name);
143
           
143
           
144
            for (j = 0; j < i; j++)
144
            for (j = 0; j < i; j++)
145
                item->name[j] = name[j];
145
                item->name[j] = name[j];
146
           
146
           
147
            item->name[j] = 0;
147
            item->name[j] = 0;
148
 
148
 
149
            if(name[i]) { /* =='.' */
149
            if(name[i]) { /* =='.' */
150
                item->subinfo_type = SYSINFO_SUBINFO_TABLE;
150
                item->subinfo_type = SYSINFO_SUBINFO_TABLE;
151
                return sysinfo_create_path(name + i + 1, &(item->subinfo.table));
151
                return sysinfo_create_path(name + i + 1, &(item->subinfo.table));
152
            }
152
            }
153
            item->subinfo_type = SYSINFO_SUBINFO_NONE;
153
            item->subinfo_type = SYSINFO_SUBINFO_NONE;
154
            return item;
154
            return item;
155
        } else {
155
        } else {
156
            subtree = subtree->next;
156
            subtree = subtree->next;
157
            i = 0;
157
            i = 0;
158
        }  
158
        }  
159
    }
159
    }
160
    panic("Not reached\n");
160
    panic("Not reached\n");
161
    return NULL;
161
    return NULL;
162
}
162
}
163
 
163
 
164
void sysinfo_set_item_val(const char *name, sysinfo_item_t **root, __native val)
164
void sysinfo_set_item_val(const char *name, sysinfo_item_t **root, __native val)
165
{
165
{
166
    if (root == NULL)
166
    if (root == NULL)
167
        root = &_root;
167
        root = &_root;
168
   
168
   
169
    /* If already created create only returns pointer
169
    /* If already created create only returns pointer
170
       If not, create it */
170
       If not, create it */
171
    sysinfo_item_t *item = sysinfo_create_path(name, root);
171
    sysinfo_item_t *item = sysinfo_create_path(name, root);
172
   
172
   
173
    if (item != NULL) { /* If in subsystem, unable to create or return so unable to set */
173
    if (item != NULL) { /* If in subsystem, unable to create or return so unable to set */
174
        item->val.val=val;                  
174
        item->val.val=val;                  
175
        item->val_type = SYSINFO_VAL_VAL;
175
        item->val_type = SYSINFO_VAL_VAL;
176
    }
176
    }
177
}
177
}
178
 
178
 
179
void sysinfo_set_item_function(const char *name, sysinfo_item_t **root, sysinfo_val_fn_t fn)
179
void sysinfo_set_item_function(const char *name, sysinfo_item_t **root, sysinfo_val_fn_t fn)
180
{
180
{
181
    if (root == NULL)
181
    if (root == NULL)
182
        root = &_root;
182
        root = &_root;
183
   
183
   
184
    /* If already created create only returns pointer
184
    /* If already created create only returns pointer
185
       If not, create it */
185
       If not, create it */
186
    sysinfo_item_t *item = sysinfo_create_path(name, root);
186
    sysinfo_item_t *item = sysinfo_create_path(name, root);
187
   
187
   
188
    if (item != NULL) { /* If in subsystem, unable to create or return so  unable to set */
188
    if (item != NULL) { /* If in subsystem, unable to create or return so  unable to set */
189
        item->val.fn=fn;                  
189
        item->val.fn=fn;                  
190
        item->val_type = SYSINFO_VAL_FUNCTION;
190
        item->val_type = SYSINFO_VAL_FUNCTION;
191
    }
191
    }
192
}
192
}
193
 
193
 
194
 
194
 
195
void sysinfo_set_item_undefined(const char *name, sysinfo_item_t **root)
195
void sysinfo_set_item_undefined(const char *name, sysinfo_item_t **root)
196
{
196
{
197
    if (root == NULL)
197
    if (root == NULL)
198
        root = &_root;
198
        root = &_root;
199
   
199
   
200
    /* If already created create only returns pointer
200
    /* If already created create only returns pointer
201
       If not, create it */
201
       If not, create it */
202
    sysinfo_item_t *item = sysinfo_create_path(name, root);
202
    sysinfo_item_t *item = sysinfo_create_path(name, root);
203
   
203
   
204
    if (item != NULL)
204
    if (item != NULL)
205
        item->val_type = SYSINFO_VAL_UNDEFINED;
205
        item->val_type = SYSINFO_VAL_UNDEFINED;
206
}
206
}
207
 
207
 
208
 
208
 
209
void sysinfo_dump(sysinfo_item_t **proot, int depth)
209
void sysinfo_dump(sysinfo_item_t **proot, int depth)
210
{
210
{
211
    sysinfo_item_t *root;
211
    sysinfo_item_t *root;
212
    if (proot == NULL)
212
    if (proot == NULL)
213
        proot = &_root;
213
        proot = &_root;
214
   
214
   
215
    root = *proot;
215
    root = *proot;
216
   
216
   
217
    while (root != NULL) {
217
    while (root != NULL) {
218
        int i;
218
        int i;
219
        __native val = 0;
219
        __native val = 0;
220
        char *vtype = NULL;
220
        char *vtype = NULL;
221
       
221
       
222
       
222
       
223
        for (i = 0; i < depth; i++)
223
        for (i = 0; i < depth; i++)
224
            printf("  ");
224
            printf("  ");
225
       
225
       
226
        switch (root->val_type) {
226
        switch (root->val_type) {
227
            case SYSINFO_VAL_UNDEFINED:
227
            case SYSINFO_VAL_UNDEFINED:
228
                val = 0;
228
                val = 0;
229
                vtype = "UND";
229
                vtype = "UND";
230
                break;
230
                break;
231
            case SYSINFO_VAL_VAL:
231
            case SYSINFO_VAL_VAL:
232
                val = root->val.val;
232
                val = root->val.val;
233
                vtype = "VAL";
233
                vtype = "VAL";
234
                break;
234
                break;
235
            case SYSINFO_VAL_FUNCTION:
235
            case SYSINFO_VAL_FUNCTION:
236
                val = ((sysinfo_val_fn_t) (root->val.fn)) (root);
236
                val = ((sysinfo_val_fn_t) (root->val.fn)) (root);
237
                vtype = "FUN";
237
                vtype = "FUN";
238
                break;
238
                break;
239
        }
239
        }
240
       
240
       
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"));
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"));
242
       
242
       
243
        if (root->subinfo_type == SYSINFO_SUBINFO_TABLE)
243
        if (root->subinfo_type == SYSINFO_SUBINFO_TABLE)
244
            sysinfo_dump(&(root -> subinfo.table), depth + 1);
244
            sysinfo_dump(&(root -> subinfo.table), depth + 1);
245
       
245
       
246
        root = root->next;
246
        root = root->next;
247
    }
247
    }
248
}
248
}
249
 
249
 
250
sysinfo_rettype_t sysinfo_get_val(const char *name, sysinfo_item_t **root)
250
sysinfo_rettype_t sysinfo_get_val(const char *name, sysinfo_item_t **root)
251
{
251
{
252
    // TODO: Implement Subsystem subinfo (by function implemented subinfo)
252
    // TODO: Implement Subsystem subinfo (by function implemented subinfo)
253
 
253
 
254
    sysinfo_rettype_t ret = {0, false};
254
    sysinfo_rettype_t ret = {0, false};
255
 
255
 
256
    if (root == NULL)
256
    if (root == NULL)
257
        root = &_root;
257
        root = &_root;
258
   
258
   
259
    sysinfo_item_t *item = sysinfo_find_item(name, *root);
259
    sysinfo_item_t *item = sysinfo_find_item(name, *root);
260
   
260
   
261
    if (item != NULL) {
261
    if (item != NULL) {
262
        if (item->val_type == SYSINFO_VAL_UNDEFINED)
262
        if (item->val_type == SYSINFO_VAL_UNDEFINED)
263
            return ret;
263
            return ret;
264
        else
264
        else
265
            ret.valid = true;
265
            ret.valid = true;
266
       
266
       
267
        if (item->val_type == SYSINFO_VAL_VAL)
267
        if (item->val_type == SYSINFO_VAL_VAL)
268
            ret.val = item->val.val;
268
            ret.val = item->val.val;
269
        else
269
        else
270
            ret.val = ((sysinfo_val_fn_t) (item->val.fn)) (item);
270
            ret.val = ((sysinfo_val_fn_t) (item->val.fn)) (item);
271
    }
271
    }
272
    return ret;
272
    return ret;
273
}
273
}
274
 
274
 
275
__native sys_sysinfo_valid(__native ptr, __native len)
275
__native sys_sysinfo_valid(__native ptr, __native len)
276
{
276
{
277
    char *str;
277
    char *str;
278
    sysinfo_rettype_t ret = {0, 0};
278
    sysinfo_rettype_t ret = {0, 0};
279
    str = malloc(len + 1, 0);
279
    str = malloc(len + 1, 0);
280
   
280
   
281
    ASSERT(str);
281
    ASSERT(str);
282
    if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len])))
282
    if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len])))
283
        ret = sysinfo_get_val(str, NULL);
283
        ret = sysinfo_get_val(str, NULL);
284
   
284
   
285
    free(str);
285
    free(str);
286
    return ret.valid;
286
    return ret.valid;
287
}
287
}
288
 
288
 
289
__native sys_sysinfo_value(__native ptr, __native len)
289
__native sys_sysinfo_value(__native ptr, __native len)
290
{
290
{
291
    char *str;
291
    char *str;
292
    sysinfo_rettype_t ret = {0, 0};
292
    sysinfo_rettype_t ret = {0, 0};
293
    str = malloc(len + 1, 0);
293
    str = malloc(len + 1, 0);
294
   
294
   
295
    ASSERT(str);
295
    ASSERT(str);
296
    if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len])))
296
    if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len])))
297
        ret = sysinfo_get_val(str, NULL);
297
        ret = sysinfo_get_val(str, NULL);
298
   
298
   
299
    free(str);
299
    free(str);
300
    return ret.val;
300
    return ret.val;
301
}
301
}
302
 
302