Remove unistd.h
[helenos.git] / uspace / lib / c / generic / sysinfo.c
blobd9a05d74ec6ab8538f779c9507c7d8b1f716f889
1 /*
2 * Copyright (c) 2006 Jakub Jermar
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
29 /** @addtogroup libc
30 * @{
32 /** @file
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>
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);
57 /** Get sysinfo keys
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
65 * freed by free().
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.
82 *size = 0;
83 return NULL;
86 char *data = malloc(*size);
87 if (data == NULL) {
88 *size = 0;
89 return NULL;
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;
102 free(data);
103 *size = 0;
104 return NULL;
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
158 * freed by free().
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
174 * or an empty item.
176 *size = 0;
177 return NULL;
180 void *data = malloc(*size);
181 if (data == NULL) {
182 *size = 0;
183 return NULL;
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;
196 free(data);
197 *size = 0;
198 return NULL;
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
210 * freed by free().
213 void *sysinfo_get_property(const char *path, const char *name, size_t *size)
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;
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;
229 bool found = (str_cmp(data + pos, name) == 0);
231 pos += cur_size + 1;
232 if (pos >= total_size)
233 break;
235 /* Process value size */
236 size_t 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))
241 break;
243 if (found) {
244 void *value = malloc(value_size);
245 if (value == NULL)
246 break;
248 memcpy(value, data + pos, value_size);
249 free(data);
251 *size = value_size;
252 return value;
255 pos += value_size;
258 free(data);
260 *size = 0;
261 return NULL;
264 /** @}