[57ff9ab] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[57ff9ab] | 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.
|
---|
[b2951e2] | 27 | */
|
---|
| 28 |
|
---|
[fadd381] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[d9fae235] | 33 | */
|
---|
[57ff9ab] | 34 |
|
---|
| 35 | #include <libc.h>
|
---|
| 36 | #include <sysinfo.h>
|
---|
[19f857a] | 37 | #include <str.h>
|
---|
[d9fae235] | 38 | #include <errno.h>
|
---|
| 39 | #include <malloc.h>
|
---|
| 40 | #include <bool.h>
|
---|
| 41 |
|
---|
[80bfb601] | 42 | /** Get sysinfo item type
|
---|
| 43 | *
|
---|
| 44 | * @param path Sysinfo path.
|
---|
| 45 | *
|
---|
| 46 | * @return Sysinfo item type.
|
---|
| 47 | *
|
---|
| 48 | */
|
---|
[d9fae235] | 49 | sysinfo_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 |
|
---|
[80bfb601] | 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 | */
|
---|
[d9fae235] | 64 | int 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 |
|
---|
[80bfb601] | 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 | */
|
---|
[d9fae235] | 79 | static 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 | }
|
---|
[57ff9ab] | 84 |
|
---|
[80bfb601] | 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 | */
|
---|
[9dae191e] | 96 | void *sysinfo_get_data(const char *path, size_t *size)
|
---|
[57ff9ab] | 97 | {
|
---|
[311bc25] | 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 | */
|
---|
[80bfb601] | 103 |
|
---|
[311bc25] | 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 | }
|
---|
[bbda5ab] | 114 |
|
---|
[311bc25] | 115 | void *data = malloc(*size);
|
---|
| 116 | if (data == NULL) {
|
---|
| 117 | *size = 0;
|
---|
| 118 | return NULL;
|
---|
[d9fae235] | 119 | }
|
---|
[bbda5ab] | 120 |
|
---|
[311bc25] | 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 | }
|
---|
[bbda5ab] | 130 |
|
---|
[311bc25] | 131 | free(data);
|
---|
[bbda5ab] | 132 | *size = 0;
|
---|
| 133 | return NULL;
|
---|
[57ff9ab] | 134 | }
|
---|
| 135 |
|
---|
[fadd381] | 136 | /** @}
|
---|
[b2951e2] | 137 | */
|
---|