1 | /*
|
---|
2 | * Copyright (c) 2006 Jakub Jermar
|
---|
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 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <libc.h>
|
---|
36 | #include <sysinfo.h>
|
---|
37 | #include <str.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <malloc.h>
|
---|
40 | #include <stdbool.h>
|
---|
41 | #include <stddef.h>
|
---|
42 |
|
---|
43 | /** Get sysinfo keys size
|
---|
44 | *
|
---|
45 | * @param path Sysinfo path.
|
---|
46 | * @param value Pointer to store the keys size.
|
---|
47 | *
|
---|
48 | * @return EOK if the keys were successfully read.
|
---|
49 | *
|
---|
50 | */
|
---|
51 | static int sysinfo_get_keys_size(const char *path, size_t *size)
|
---|
52 | {
|
---|
53 | return (int) __SYSCALL3(SYS_SYSINFO_GET_KEYS_SIZE, (sysarg_t) path,
|
---|
54 | (sysarg_t) str_size(path), (sysarg_t) size);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /** Get sysinfo keys
|
---|
58 | *
|
---|
59 | * @param path Sysinfo path.
|
---|
60 | * @param value Pointer to store the keys size.
|
---|
61 | *
|
---|
62 | * @return Keys read from sysinfo or NULL if the
|
---|
63 | * sysinfo item has no subkeys.
|
---|
64 | * The returned non-NULL pointer should be
|
---|
65 | * freed by free().
|
---|
66 | *
|
---|
67 | */
|
---|
68 | char *sysinfo_get_keys(const char *path, size_t *size)
|
---|
69 | {
|
---|
70 | /*
|
---|
71 | * The size of the keys might change during time.
|
---|
72 | * Unfortunatelly we cannot allocate the buffer
|
---|
73 | * and transfer the keys as a single atomic operation.
|
---|
74 | */
|
---|
75 |
|
---|
76 | /* Get the keys size */
|
---|
77 | int ret = sysinfo_get_keys_size(path, size);
|
---|
78 | if ((ret != EOK) || (size == 0)) {
|
---|
79 | /*
|
---|
80 | * Item with no subkeys.
|
---|
81 | */
|
---|
82 | *size = 0;
|
---|
83 | return NULL;
|
---|
84 | }
|
---|
85 |
|
---|
86 | char *data = malloc(*size);
|
---|
87 | if (data == NULL) {
|
---|
88 | *size = 0;
|
---|
89 | return NULL;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* Get the data */
|
---|
93 | size_t sz;
|
---|
94 | ret = __SYSCALL5(SYS_SYSINFO_GET_KEYS, (sysarg_t) path,
|
---|
95 | (sysarg_t) str_size(path), (sysarg_t) data, (sysarg_t) *size,
|
---|
96 | (sysarg_t) &sz);
|
---|
97 | if (ret == EOK) {
|
---|
98 | *size = sz;
|
---|
99 | return data;
|
---|
100 | }
|
---|
101 |
|
---|
102 | free(data);
|
---|
103 | *size = 0;
|
---|
104 | return NULL;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Get sysinfo item type
|
---|
108 | *
|
---|
109 | * @param path Sysinfo path.
|
---|
110 | *
|
---|
111 | * @return Sysinfo item type.
|
---|
112 | *
|
---|
113 | */
|
---|
114 | sysinfo_item_val_type_t sysinfo_get_val_type(const char *path)
|
---|
115 | {
|
---|
116 | return (sysinfo_item_val_type_t) __SYSCALL2(SYS_SYSINFO_GET_VAL_TYPE,
|
---|
117 | (sysarg_t) path, (sysarg_t) str_size(path));
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Get sysinfo numerical value
|
---|
121 | *
|
---|
122 | * @param path Sysinfo path.
|
---|
123 | * @param value Pointer to store the numerical value to.
|
---|
124 | *
|
---|
125 | * @return EOK if the value was successfully read and
|
---|
126 | * is of SYSINFO_VAL_VAL type.
|
---|
127 | *
|
---|
128 | */
|
---|
129 | int sysinfo_get_value(const char *path, sysarg_t *value)
|
---|
130 | {
|
---|
131 | return (int) __SYSCALL3(SYS_SYSINFO_GET_VALUE, (sysarg_t) path,
|
---|
132 | (sysarg_t) str_size(path), (sysarg_t) value);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Get sysinfo binary data size
|
---|
136 | *
|
---|
137 | * @param path Sysinfo path.
|
---|
138 | * @param size Pointer to store the binary data size.
|
---|
139 | *
|
---|
140 | * @return EOK if the value was successfully read and
|
---|
141 | * is of SYSINFO_VAL_DATA type.
|
---|
142 | *
|
---|
143 | */
|
---|
144 | static int sysinfo_get_data_size(const char *path, size_t *size)
|
---|
145 | {
|
---|
146 | return (int) __SYSCALL3(SYS_SYSINFO_GET_DATA_SIZE, (sysarg_t) path,
|
---|
147 | (sysarg_t) str_size(path), (sysarg_t) size);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Get sysinfo binary data
|
---|
151 | *
|
---|
152 | * @param path Sysinfo path.
|
---|
153 | * @param size Pointer to store the binary data size.
|
---|
154 | *
|
---|
155 | * @return Binary data read from sysinfo or NULL if the
|
---|
156 | * sysinfo item value type is not binary data.
|
---|
157 | * The returned non-NULL pointer should be
|
---|
158 | * freed by free().
|
---|
159 | *
|
---|
160 | */
|
---|
161 | void *sysinfo_get_data(const char *path, size_t *size)
|
---|
162 | {
|
---|
163 | /*
|
---|
164 | * The binary data size might change during time.
|
---|
165 | * Unfortunatelly we cannot allocate the buffer
|
---|
166 | * and transfer the data as a single atomic operation.
|
---|
167 | */
|
---|
168 |
|
---|
169 | /* Get the binary data size */
|
---|
170 | int ret = sysinfo_get_data_size(path, size);
|
---|
171 | if ((ret != EOK) || (size == 0)) {
|
---|
172 | /*
|
---|
173 | * Not a binary data item
|
---|
174 | * or an empty item.
|
---|
175 | */
|
---|
176 | *size = 0;
|
---|
177 | return NULL;
|
---|
178 | }
|
---|
179 |
|
---|
180 | void *data = malloc(*size);
|
---|
181 | if (data == NULL) {
|
---|
182 | *size = 0;
|
---|
183 | return NULL;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* Get the data */
|
---|
187 | size_t sz;
|
---|
188 | ret = __SYSCALL5(SYS_SYSINFO_GET_DATA, (sysarg_t) path,
|
---|
189 | (sysarg_t) str_size(path), (sysarg_t) data, (sysarg_t) *size,
|
---|
190 | (sysarg_t) &sz);
|
---|
191 | if (ret == EOK) {
|
---|
192 | *size = sz;
|
---|
193 | return data;
|
---|
194 | }
|
---|
195 |
|
---|
196 | free(data);
|
---|
197 | *size = 0;
|
---|
198 | return NULL;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /** Get sysinfo property
|
---|
202 | *
|
---|
203 | * @param path Sysinfo path.
|
---|
204 | * @param name Property name.
|
---|
205 | * @param size Pointer to store the binary data size.
|
---|
206 | *
|
---|
207 | * @return Property value read from sysinfo or NULL if the
|
---|
208 | * sysinfo item value type is not binary data.
|
---|
209 | * The returned non-NULL pointer should be
|
---|
210 | * freed by free().
|
---|
211 | *
|
---|
212 | */
|
---|
213 | void *sysinfo_get_property(const char *path, const char *name, size_t *size)
|
---|
214 | {
|
---|
215 | size_t total_size;
|
---|
216 | void *data = sysinfo_get_data(path, &total_size);
|
---|
217 | if ((data == NULL) || (total_size == 0)) {
|
---|
218 | *size = 0;
|
---|
219 | return NULL;
|
---|
220 | }
|
---|
221 |
|
---|
222 | size_t pos = 0;
|
---|
223 | while (pos < total_size) {
|
---|
224 | /* Process each property with sanity checks */
|
---|
225 | size_t cur_size = str_nsize(data + pos, total_size - pos);
|
---|
226 | if (((char *) data)[pos + cur_size] != 0)
|
---|
227 | break;
|
---|
228 |
|
---|
229 | bool found = (str_cmp(data + pos, name) == 0);
|
---|
230 |
|
---|
231 | pos += cur_size + 1;
|
---|
232 | if (pos >= total_size)
|
---|
233 | break;
|
---|
234 |
|
---|
235 | /* Process value size */
|
---|
236 | size_t value_size;
|
---|
237 | memcpy(&value_size, data + pos, sizeof(value_size));
|
---|
238 |
|
---|
239 | pos += sizeof(value_size);
|
---|
240 | if ((pos >= total_size) || (pos + value_size > total_size))
|
---|
241 | break;
|
---|
242 |
|
---|
243 | if (found) {
|
---|
244 | void *value = malloc(value_size);
|
---|
245 | if (value == NULL)
|
---|
246 | break;
|
---|
247 |
|
---|
248 | memcpy(value, data + pos, value_size);
|
---|
249 | free(data);
|
---|
250 |
|
---|
251 | *size = value_size;
|
---|
252 | return value;
|
---|
253 | }
|
---|
254 |
|
---|
255 | pos += value_size;
|
---|
256 | }
|
---|
257 |
|
---|
258 | free(data);
|
---|
259 |
|
---|
260 | *size = 0;
|
---|
261 | return NULL;
|
---|
262 | }
|
---|
263 |
|
---|
264 | /** @}
|
---|
265 | */
|
---|