source: mainline/uspace/lib/c/generic/sysinfo.c@ 76f382b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 76f382b was 76f382b, checked in by Martin Decky <martin@…>, 13 years ago

support for listing sysinfo from uspace

  • Property mode set to 100644
File size: 5.1 KB
Line 
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 <bool.h>
41
42/** Get sysinfo keys size
43 *
44 * @param path Sysinfo path.
45 * @param value Pointer to store the keys size.
46 *
47 * @return EOK if the keys were successfully read.
48 *
49 */
50static int sysinfo_get_keys_size(const char *path, size_t *size)
51{
52 return (int) __SYSCALL3(SYS_SYSINFO_GET_KEYS_SIZE, (sysarg_t) path,
53 (sysarg_t) str_size(path), (sysarg_t) size);
54}
55
56/** Get sysinfo keys
57 *
58 * @param path Sysinfo path.
59 * @param value Pointer to store the keys size.
60 *
61 * @return Keys read from sysinfo or NULL if the
62 * sysinfo item has no subkeys.
63 * The returned non-NULL pointer should be
64 * freed by free().
65 *
66 */
67char *sysinfo_get_keys(const char *path, size_t *size)
68{
69 /*
70 * The size of the keys might change during time.
71 * Unfortunatelly we cannot allocate the buffer
72 * and transfer the keys as a single atomic operation.
73 */
74
75 /* Get the keys size */
76 int ret = sysinfo_get_keys_size(path, size);
77 if ((ret != EOK) || (size == 0)) {
78 /*
79 * Item with no subkeys.
80 */
81 *size = 0;
82 return NULL;
83 }
84
85 char *data = malloc(*size);
86 if (data == NULL) {
87 *size = 0;
88 return NULL;
89 }
90
91 /* Get the data */
92 size_t sz;
93 ret = __SYSCALL5(SYS_SYSINFO_GET_KEYS, (sysarg_t) path,
94 (sysarg_t) str_size(path), (sysarg_t) data, (sysarg_t) *size,
95 (sysarg_t) &sz);
96 if (ret == EOK) {
97 *size = sz;
98 return data;
99 }
100
101 free(data);
102 *size = 0;
103 return NULL;
104}
105
106/** Get sysinfo item type
107 *
108 * @param path Sysinfo path.
109 *
110 * @return Sysinfo item type.
111 *
112 */
113sysinfo_item_val_type_t sysinfo_get_val_type(const char *path)
114{
115 return (sysinfo_item_val_type_t) __SYSCALL2(SYS_SYSINFO_GET_VAL_TYPE,
116 (sysarg_t) path, (sysarg_t) str_size(path));
117}
118
119/** Get sysinfo numerical value
120 *
121 * @param path Sysinfo path.
122 * @param value Pointer to store the numerical value to.
123 *
124 * @return EOK if the value was successfully read and
125 * is of SYSINFO_VAL_VAL type.
126 *
127 */
128int sysinfo_get_value(const char *path, sysarg_t *value)
129{
130 return (int) __SYSCALL3(SYS_SYSINFO_GET_VALUE, (sysarg_t) path,
131 (sysarg_t) str_size(path), (sysarg_t) value);
132}
133
134/** Get sysinfo binary data size
135 *
136 * @param path Sysinfo path.
137 * @param value Pointer to store the binary data size.
138 *
139 * @return EOK if the value was successfully read and
140 * is of SYSINFO_VAL_DATA type.
141 *
142 */
143static int sysinfo_get_data_size(const char *path, size_t *size)
144{
145 return (int) __SYSCALL3(SYS_SYSINFO_GET_DATA_SIZE, (sysarg_t) path,
146 (sysarg_t) str_size(path), (sysarg_t) size);
147}
148
149/** Get sysinfo binary data
150 *
151 * @param path Sysinfo path.
152 * @param value Pointer to store the binary data size.
153 *
154 * @return Binary data read from sysinfo or NULL if the
155 * sysinfo item value type is not binary data.
156 * The returned non-NULL pointer should be
157 * freed by free().
158 *
159 */
160void *sysinfo_get_data(const char *path, size_t *size)
161{
162 /*
163 * The binary data size might change during time.
164 * Unfortunatelly we cannot allocate the buffer
165 * and transfer the data as a single atomic operation.
166 */
167
168 /* Get the binary data size */
169 int ret = sysinfo_get_data_size(path, size);
170 if ((ret != EOK) || (size == 0)) {
171 /*
172 * Not a binary data item
173 * or an empty item.
174 */
175 *size = 0;
176 return NULL;
177 }
178
179 void *data = malloc(*size);
180 if (data == NULL) {
181 *size = 0;
182 return NULL;
183 }
184
185 /* Get the data */
186 size_t sz;
187 ret = __SYSCALL5(SYS_SYSINFO_GET_DATA, (sysarg_t) path,
188 (sysarg_t) str_size(path), (sysarg_t) data, (sysarg_t) *size,
189 (sysarg_t) &sz);
190 if (ret == EOK) {
191 *size = sz;
192 return data;
193 }
194
195 free(data);
196 *size = 0;
197 return NULL;
198}
199
200/** @}
201 */
Note: See TracBrowser for help on using the repository browser.