[a16210b5] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jiri Zarevucky
|
---|
[4f4b4e7] | 3 | * Copyright (c) 2011 Petr Koupy
|
---|
[a16210b5] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[4f4b4e7] | 30 | /** @addtogroup libposix
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
[4cf8ca6] | 33 | /** @file Miscellaneous standard definitions.
|
---|
[4f4b4e7] | 34 | */
|
---|
| 35 |
|
---|
[491e1ee] | 36 | #define LIBPOSIX_INTERNAL
|
---|
[4f4b4e7] | 37 |
|
---|
[9b1503e] | 38 | #include "internal/common.h"
|
---|
[a16210b5] | 39 | #include "unistd.h"
|
---|
[ec18957a] | 40 |
|
---|
| 41 | #include "errno.h"
|
---|
[fb872c1] | 42 | #include "string.h"
|
---|
| 43 | #include "fcntl.h"
|
---|
[ec18957a] | 44 |
|
---|
| 45 | #include "libc/task.h"
|
---|
| 46 | #include "libc/stats.h"
|
---|
[fb872c1] | 47 | #include "libc/malloc.h"
|
---|
[a16210b5] | 48 |
|
---|
[b4d6252] | 49 | /* Array of environment variable strings (NAME=VALUE). */
|
---|
[7530a00] | 50 | char **posix_environ = NULL;
|
---|
[b4d6252] | 51 |
|
---|
[244d6fd] | 52 | /**
|
---|
| 53 | * Get current user name.
|
---|
[fb872c1] | 54 | *
|
---|
| 55 | * @return User name (static) string or NULL if not found.
|
---|
[244d6fd] | 56 | */
|
---|
| 57 | char *posix_getlogin(void)
|
---|
| 58 | {
|
---|
[fb872c1] | 59 | /* There is currently no support for user accounts in HelenOS. */
|
---|
[244d6fd] | 60 | return (char *) "user";
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Get current user name.
|
---|
| 65 | *
|
---|
| 66 | * @param name Pointer to a user supplied buffer.
|
---|
| 67 | * @param namesize Length of the buffer.
|
---|
[fb872c1] | 68 | * @return Zero on success, error code otherwise.
|
---|
[244d6fd] | 69 | */
|
---|
| 70 | int posix_getlogin_r(char *name, size_t namesize)
|
---|
| 71 | {
|
---|
[fb872c1] | 72 | /* There is currently no support for user accounts in HelenOS. */
|
---|
| 73 | if (namesize >= 5) {
|
---|
| 74 | posix_strcpy(name, (char *) "user");
|
---|
| 75 | return 0;
|
---|
| 76 | } else {
|
---|
| 77 | errno = ERANGE;
|
---|
| 78 | return -1;
|
---|
| 79 | }
|
---|
[244d6fd] | 80 | }
|
---|
| 81 |
|
---|
[4f4b4e7] | 82 | /**
|
---|
[fb872c1] | 83 | * Test whether open file descriptor is associated with a terminal.
|
---|
[8d7e82c1] | 84 | *
|
---|
[fb872c1] | 85 | * @param fd Open file descriptor to test.
|
---|
| 86 | * @return Boolean result of the test.
|
---|
[4f4b4e7] | 87 | */
|
---|
| 88 | int posix_isatty(int fd)
|
---|
| 89 | {
|
---|
[fb872c1] | 90 | /* Always returns false, because there is no easy way to find
|
---|
| 91 | * out under HelenOS. */
|
---|
[94f8b81c] | 92 | return 0;
|
---|
[a16210b5] | 93 | }
|
---|
| 94 |
|
---|
[221afc9e] | 95 | /**
|
---|
| 96 | * Get the pathname of the current working directory.
|
---|
| 97 | *
|
---|
| 98 | * @param buf Buffer into which the pathname shall be put.
|
---|
| 99 | * @param size Size of the buffer.
|
---|
| 100 | * @return Buffer pointer on success, NULL on failure.
|
---|
| 101 | */
|
---|
| 102 | char *posix_getcwd(char *buf, size_t size)
|
---|
| 103 | {
|
---|
| 104 | /* Native getcwd() does not set any errno despite the fact that general
|
---|
| 105 | * usage pattern of this function depends on it (caller is repeatedly
|
---|
| 106 | * guessing the required size of the buffer by checking for ERANGE on
|
---|
| 107 | * failure). */
|
---|
| 108 | if (size == 0) {
|
---|
| 109 | errno = EINVAL;
|
---|
| 110 | return NULL;
|
---|
| 111 | }
|
---|
| 112 | char *ret = getcwd(buf, size);
|
---|
| 113 | if (ret == NULL && errno == EOK) {
|
---|
| 114 | errno = ERANGE;
|
---|
| 115 | }
|
---|
| 116 | return ret;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[72ec8cc] | 119 | /**
|
---|
[fb872c1] | 120 | * Determine the page size of the current run of the process.
|
---|
| 121 | *
|
---|
| 122 | * @return Page size of the process.
|
---|
[72ec8cc] | 123 | */
|
---|
| 124 | int posix_getpagesize(void)
|
---|
| 125 | {
|
---|
| 126 | return getpagesize();
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[823a929] | 129 | /**
|
---|
[fb872c1] | 130 | * Get the process ID of the calling process.
|
---|
| 131 | *
|
---|
| 132 | * @return Process ID.
|
---|
[823a929] | 133 | */
|
---|
| 134 | posix_pid_t posix_getpid(void)
|
---|
| 135 | {
|
---|
| 136 | return task_get_id();
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[59f799b] | 139 | /**
|
---|
[fb872c1] | 140 | * Get the real user ID of the calling process.
|
---|
[59f799b] | 141 | *
|
---|
[fb872c1] | 142 | * @return User ID.
|
---|
[59f799b] | 143 | */
|
---|
| 144 | posix_uid_t posix_getuid(void)
|
---|
| 145 | {
|
---|
[fb872c1] | 146 | /* There is currently no support for user accounts in HelenOS. */
|
---|
| 147 | return 0;
|
---|
[59f799b] | 148 | }
|
---|
| 149 |
|
---|
| 150 | /**
|
---|
[fb872c1] | 151 | * Get the real group ID of the calling process.
|
---|
[59f799b] | 152 | *
|
---|
[fb872c1] | 153 | * @return Group ID.
|
---|
[59f799b] | 154 | */
|
---|
| 155 | posix_gid_t posix_getgid(void)
|
---|
| 156 | {
|
---|
[fb872c1] | 157 | /* There is currently no support for user accounts in HelenOS. */
|
---|
| 158 | return 0;
|
---|
[59f799b] | 159 | }
|
---|
| 160 |
|
---|
[221afc9e] | 161 | /**
|
---|
| 162 | * Read from a file.
|
---|
| 163 | *
|
---|
| 164 | * @param fildes File descriptor of the opened file.
|
---|
| 165 | * @param buf Buffer to which the read bytes shall be stored.
|
---|
| 166 | * @param nbyte Upper limit on the number of read bytes.
|
---|
| 167 | * @return Number of read bytes on success, -1 otherwise.
|
---|
| 168 | */
|
---|
| 169 | ssize_t posix_read(int fildes, void *buf, size_t nbyte)
|
---|
| 170 | {
|
---|
| 171 | int rc = read(fildes, buf, nbyte);
|
---|
| 172 | if (rc < 0) {
|
---|
| 173 | errno = -rc;
|
---|
| 174 | return -1;
|
---|
| 175 | } else {
|
---|
| 176 | return rc;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /**
|
---|
| 181 | * Remove a link to a file.
|
---|
| 182 | *
|
---|
| 183 | * @param path File pathname.
|
---|
| 184 | * @return Zero on success, -1 otherwise.
|
---|
| 185 | */
|
---|
| 186 | int posix_unlink(const char *path)
|
---|
| 187 | {
|
---|
| 188 | int rc = unlink(path);
|
---|
| 189 | if (rc < 0) {
|
---|
| 190 | errno = -rc;
|
---|
| 191 | return -1;
|
---|
| 192 | } else {
|
---|
| 193 | return rc;
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[b08ef1fd] | 197 | /**
|
---|
[fb872c1] | 198 | * Determine accessibility of a file.
|
---|
| 199 | *
|
---|
| 200 | * @param path File to check accessibility for.
|
---|
| 201 | * @param amode Either check for existence or intended access mode.
|
---|
| 202 | * @return Zero on success, -1 otherwise.
|
---|
[b08ef1fd] | 203 | */
|
---|
| 204 | int posix_access(const char *path, int amode)
|
---|
| 205 | {
|
---|
[fb872c1] | 206 | if (amode == F_OK) {
|
---|
| 207 | /* Check file existence by attempt to open it. */
|
---|
| 208 | int fd = open(path, O_RDONLY);
|
---|
[94f8b81c] | 209 | if (fd != -1) {
|
---|
[ec18957a] | 210 | close(fd);
|
---|
| 211 | }
|
---|
| 212 | return fd;
|
---|
[fb872c1] | 213 | } else if (amode & (X_OK | W_OK | R_OK)) {
|
---|
| 214 | /* HelenOS doesn't support permissions, return success. */
|
---|
| 215 | return 0;
|
---|
| 216 | } else {
|
---|
| 217 | /* Invalid amode argument. */
|
---|
| 218 | errno = EINVAL;
|
---|
| 219 | return -1;
|
---|
| 220 | }
|
---|
[b08ef1fd] | 221 | }
|
---|
| 222 |
|
---|
[59f799b] | 223 | /**
|
---|
[fb872c1] | 224 | * Get configurable system variables.
|
---|
[59f799b] | 225 | *
|
---|
[fb872c1] | 226 | * @param name Variable name.
|
---|
| 227 | * @return Variable value.
|
---|
[59f799b] | 228 | */
|
---|
| 229 | long posix_sysconf(int name)
|
---|
| 230 | {
|
---|
[fb872c1] | 231 | long clk_tck = 0;
|
---|
| 232 | size_t cpu_count = 0;
|
---|
| 233 | stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
|
---|
| 234 | if (cpu_stats && cpu_count > 0) {
|
---|
| 235 | clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
|
---|
| 236 | }
|
---|
[94f8b81c] | 237 | if (cpu_stats) {
|
---|
| 238 | free(cpu_stats);
|
---|
| 239 | cpu_stats = 0;
|
---|
| 240 | }
|
---|
[fb872c1] | 241 |
|
---|
| 242 | long phys_pages = 0;
|
---|
| 243 | long avphys_pages = 0;
|
---|
| 244 | stats_physmem_t *mem_stats = stats_get_physmem();
|
---|
| 245 | if (mem_stats) {
|
---|
| 246 | phys_pages = (long) (mem_stats->total / getpagesize());
|
---|
| 247 | avphys_pages = (long) (mem_stats->free / getpagesize());
|
---|
[94f8b81c] | 248 | free(mem_stats);
|
---|
| 249 | mem_stats = 0;
|
---|
[fb872c1] | 250 | }
|
---|
| 251 |
|
---|
| 252 | switch (name) {
|
---|
| 253 | case _SC_PHYS_PAGES:
|
---|
| 254 | return phys_pages;
|
---|
| 255 | case _SC_AVPHYS_PAGES:
|
---|
| 256 | return avphys_pages;
|
---|
| 257 | case _SC_PAGESIZE:
|
---|
| 258 | return getpagesize();
|
---|
| 259 | case _SC_CLK_TCK:
|
---|
| 260 | return clk_tck;
|
---|
| 261 | default:
|
---|
| 262 | errno = EINVAL;
|
---|
| 263 | return -1;
|
---|
| 264 | }
|
---|
[59f799b] | 265 | }
|
---|
| 266 |
|
---|
[823a929] | 267 | /**
|
---|
| 268 | *
|
---|
| 269 | * @param path
|
---|
| 270 | * @param name
|
---|
| 271 | * @return
|
---|
| 272 | */
|
---|
| 273 | long posix_pathconf(const char *path, int name)
|
---|
| 274 | {
|
---|
| 275 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 276 | not_implemented();
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | /**
|
---|
| 280 | *
|
---|
| 281 | * @return
|
---|
| 282 | */
|
---|
| 283 | posix_pid_t posix_fork(void)
|
---|
| 284 | {
|
---|
| 285 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 286 | not_implemented();
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | /**
|
---|
| 290 | *
|
---|
| 291 | * @param path
|
---|
| 292 | * @param argv
|
---|
| 293 | * @return
|
---|
| 294 | */
|
---|
| 295 | int posix_execv(const char *path, char *const argv[])
|
---|
| 296 | {
|
---|
| 297 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 298 | not_implemented();
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /**
|
---|
| 302 | *
|
---|
| 303 | * @param file
|
---|
| 304 | * @param argv
|
---|
| 305 | * @return
|
---|
| 306 | */
|
---|
| 307 | int posix_execvp(const char *file, char *const argv[])
|
---|
| 308 | {
|
---|
| 309 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 310 | not_implemented();
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | /**
|
---|
| 314 | *
|
---|
| 315 | * @param fildes
|
---|
| 316 | * @return
|
---|
| 317 | */
|
---|
| 318 | int posix_pipe(int fildes[2])
|
---|
| 319 | {
|
---|
| 320 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 321 | not_implemented();
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[4f4b4e7] | 324 | /** @}
|
---|
| 325 | */
|
---|