source: mainline/uspace/lib/c/generic/sysinfo.c@ 0f4532e

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

avoid the possibility for infinite looping on fetching sysinfo values
still make the tests in statistical functions more robust
this fixes ticket #237

  • Property mode set to 100644
File size: 3.7 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 item type
43 *
44 * @param path Sysinfo path.
45 *
46 * @return Sysinfo item type.
47 *
48 */
49sysinfo_item_tag_t sysinfo_get_tag(const char *path)
50{
51 return (sysinfo_item_tag_t) __SYSCALL2(SYS_SYSINFO_GET_TAG,
52 (sysarg_t) path, (sysarg_t) str_size(path));
53}
54
55/** Get sysinfo numerical value
56 *
57 * @param path Sysinfo path.
58 * @param value Pointer to store the numerical value to.
59 *
60 * @return EOK if the value was successfully read and
61 * is of SYSINFO_VAL_VAL type.
62 *
63 */
64int sysinfo_get_value(const char *path, sysarg_t *value)
65{
66 return (int) __SYSCALL3(SYS_SYSINFO_GET_VALUE, (sysarg_t) path,
67 (sysarg_t) str_size(path), (sysarg_t) value);
68}
69
70/** Get sysinfo binary data size
71 *
72 * @param path Sysinfo path.
73 * @param value Pointer to store the binary data size.
74 *
75 * @return EOK if the value was successfully read and
76 * is of SYSINFO_VAL_DATA type.
77 *
78 */
79static int sysinfo_get_data_size(const char *path, size_t *size)
80{
81 return (int) __SYSCALL3(SYS_SYSINFO_GET_DATA_SIZE, (sysarg_t) path,
82 (sysarg_t) str_size(path), (sysarg_t) size);
83}
84
85/** Get sysinfo binary data
86 *
87 * @param path Sysinfo path.
88 * @param value Pointer to store the binary data size.
89 *
90 * @return Binary data read from sysinfo or NULL if the
91 * sysinfo item value type is not binary data.
92 * The returned non-NULL pointer should be
93 * freed by free().
94 *
95 */
96void *sysinfo_get_data(const char *path, size_t *size)
97{
98 /*
99 * The binary data size might change during time.
100 * Unfortunatelly we cannot allocate the buffer
101 * and transfer the data as a single atomic operation.
102 */
103
104 /* Get the binary data size */
105 int ret = sysinfo_get_data_size(path, size);
106 if ((ret != EOK) || (size == 0)) {
107 /*
108 * Not a binary data item
109 * or an empty item.
110 */
111 *size = 0;
112 return NULL;
113 }
114
115 void *data = malloc(*size);
116 if (data == NULL) {
117 *size = 0;
118 return NULL;
119 }
120
121 /* Get the data */
122 size_t sz;
123 ret = __SYSCALL5(SYS_SYSINFO_GET_DATA, (sysarg_t) path,
124 (sysarg_t) str_size(path), (sysarg_t) data, (sysarg_t) *size,
125 (sysarg_t) &sz);
126 if (ret == EOK) {
127 *size = sz;
128 return data;
129 }
130
131 free(data);
132 *size = 0;
133 return NULL;
134}
135
136/** @}
137 */
Note: See TracBrowser for help on using the repository browser.