2 * Copyright (c) 2006 Jakub Jermar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
43 /** Get sysinfo keys size
45 * @param path Sysinfo path.
46 * @param value Pointer to store the keys size.
48 * @return EOK if the keys were successfully read.
51 static int sysinfo_get_keys_size(const char *path
, size_t *size
)
53 return (int) __SYSCALL3(SYS_SYSINFO_GET_KEYS_SIZE
, (sysarg_t
) path
,
54 (sysarg_t
) str_size(path
), (sysarg_t
) size
);
59 * @param path Sysinfo path.
60 * @param value Pointer to store the keys size.
62 * @return Keys read from sysinfo or NULL if the
63 * sysinfo item has no subkeys.
64 * The returned non-NULL pointer should be
68 char *sysinfo_get_keys(const char *path
, size_t *size
)
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.
76 /* Get the keys size */
77 int ret
= sysinfo_get_keys_size(path
, size
);
78 if ((ret
!= EOK
) || (size
== 0)) {
80 * Item with no subkeys.
86 char *data
= malloc(*size
);
94 ret
= __SYSCALL5(SYS_SYSINFO_GET_KEYS
, (sysarg_t
) path
,
95 (sysarg_t
) str_size(path
), (sysarg_t
) data
, (sysarg_t
) *size
,
107 /** Get sysinfo item type
109 * @param path Sysinfo path.
111 * @return Sysinfo item type.
114 sysinfo_item_val_type_t
sysinfo_get_val_type(const char *path
)
116 return (sysinfo_item_val_type_t
) __SYSCALL2(SYS_SYSINFO_GET_VAL_TYPE
,
117 (sysarg_t
) path
, (sysarg_t
) str_size(path
));
120 /** Get sysinfo numerical value
122 * @param path Sysinfo path.
123 * @param value Pointer to store the numerical value to.
125 * @return EOK if the value was successfully read and
126 * is of SYSINFO_VAL_VAL type.
129 int sysinfo_get_value(const char *path
, sysarg_t
*value
)
131 return (int) __SYSCALL3(SYS_SYSINFO_GET_VALUE
, (sysarg_t
) path
,
132 (sysarg_t
) str_size(path
), (sysarg_t
) value
);
135 /** Get sysinfo binary data size
137 * @param path Sysinfo path.
138 * @param size Pointer to store the binary data size.
140 * @return EOK if the value was successfully read and
141 * is of SYSINFO_VAL_DATA type.
144 static int sysinfo_get_data_size(const char *path
, size_t *size
)
146 return (int) __SYSCALL3(SYS_SYSINFO_GET_DATA_SIZE
, (sysarg_t
) path
,
147 (sysarg_t
) str_size(path
), (sysarg_t
) size
);
150 /** Get sysinfo binary data
152 * @param path Sysinfo path.
153 * @param size Pointer to store the binary data size.
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
161 void *sysinfo_get_data(const char *path
, size_t *size
)
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.
169 /* Get the binary data size */
170 int ret
= sysinfo_get_data_size(path
, size
);
171 if ((ret
!= EOK
) || (size
== 0)) {
173 * Not a binary data item
180 void *data
= malloc(*size
);
188 ret
= __SYSCALL5(SYS_SYSINFO_GET_DATA
, (sysarg_t
) path
,
189 (sysarg_t
) str_size(path
), (sysarg_t
) data
, (sysarg_t
) *size
,
201 /** Get sysinfo property
203 * @param path Sysinfo path.
204 * @param name Property name.
205 * @param size Pointer to store the binary data size.
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
213 void *sysinfo_get_property(const char *path
, const char *name
, size_t *size
)
216 void *data
= sysinfo_get_data(path
, &total_size
);
217 if ((data
== NULL
) || (total_size
== 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)
229 bool found
= (str_cmp(data
+ pos
, name
) == 0);
232 if (pos
>= total_size
)
235 /* Process value size */
237 memcpy(&value_size
, data
+ pos
, sizeof(value_size
));
239 pos
+= sizeof(value_size
);
240 if ((pos
>= total_size
) || (pos
+ value_size
> total_size
))
244 void *value
= malloc(value_size
);
248 memcpy(value
, data
+ pos
, value_size
);