| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jiri Zarevucky
|
|---|
| 3 | * Copyright (c) 2011 Petr Koupy
|
|---|
| 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 |
|
|---|
| 30 | /** @addtogroup libposix
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file Miscellaneous standard definitions.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #define LIBPOSIX_INTERNAL
|
|---|
| 37 | #define __POSIX_DEF__(x) posix_##x
|
|---|
| 38 |
|
|---|
| 39 | #include "internal/common.h"
|
|---|
| 40 | #include "posix/unistd.h"
|
|---|
| 41 |
|
|---|
| 42 | #include "posix/errno.h"
|
|---|
| 43 | #include "posix/string.h"
|
|---|
| 44 | #include "posix/fcntl.h"
|
|---|
| 45 |
|
|---|
| 46 | #include "libc/task.h"
|
|---|
| 47 | #include "libc/stats.h"
|
|---|
| 48 | #include "libc/malloc.h"
|
|---|
| 49 |
|
|---|
| 50 | /* Array of environment variable strings (NAME=VALUE). */
|
|---|
| 51 | char **posix_environ = NULL;
|
|---|
| 52 | char *posix_optarg;
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Get current user name.
|
|---|
| 56 | *
|
|---|
| 57 | * @return User name (static) string or NULL if not found.
|
|---|
| 58 | */
|
|---|
| 59 | char *posix_getlogin(void)
|
|---|
| 60 | {
|
|---|
| 61 | /* There is currently no support for user accounts in HelenOS. */
|
|---|
| 62 | return (char *) "user";
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Get current user name.
|
|---|
| 67 | *
|
|---|
| 68 | * @param name Pointer to a user supplied buffer.
|
|---|
| 69 | * @param namesize Length of the buffer.
|
|---|
| 70 | * @return Zero on success, error code otherwise.
|
|---|
| 71 | */
|
|---|
| 72 | int posix_getlogin_r(char *name, size_t namesize)
|
|---|
| 73 | {
|
|---|
| 74 | /* There is currently no support for user accounts in HelenOS. */
|
|---|
| 75 | if (namesize >= 5) {
|
|---|
| 76 | posix_strcpy(name, (char *) "user");
|
|---|
| 77 | return 0;
|
|---|
| 78 | } else {
|
|---|
| 79 | errno = ERANGE;
|
|---|
| 80 | return -1;
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * Test whether open file descriptor is associated with a terminal.
|
|---|
| 86 | *
|
|---|
| 87 | * @param fd Open file descriptor to test.
|
|---|
| 88 | * @return Boolean result of the test.
|
|---|
| 89 | */
|
|---|
| 90 | int posix_isatty(int fd)
|
|---|
| 91 | {
|
|---|
| 92 | // TODO
|
|---|
| 93 | /* Always returns false, because there is no easy way to find
|
|---|
| 94 | * out under HelenOS. */
|
|---|
| 95 | return 0;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * Get the pathname of the current working directory.
|
|---|
| 100 | *
|
|---|
| 101 | * @param buf Buffer into which the pathname shall be put.
|
|---|
| 102 | * @param size Size of the buffer.
|
|---|
| 103 | * @return Buffer pointer on success, NULL on failure.
|
|---|
| 104 | */
|
|---|
| 105 | char *posix_getcwd(char *buf, size_t size)
|
|---|
| 106 | {
|
|---|
| 107 | /* Native getcwd() does not set any errno despite the fact that general
|
|---|
| 108 | * usage pattern of this function depends on it (caller is repeatedly
|
|---|
| 109 | * guessing the required size of the buffer by checking for ERANGE on
|
|---|
| 110 | * failure). */
|
|---|
| 111 | if (size == 0) {
|
|---|
| 112 | errno = EINVAL;
|
|---|
| 113 | return NULL;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /* Save the original value to comply with the "no modification on
|
|---|
| 117 | * success" semantics.
|
|---|
| 118 | */
|
|---|
| 119 | int orig_errno = errno;
|
|---|
| 120 | errno = EOK;
|
|---|
| 121 |
|
|---|
| 122 | char *ret = getcwd(buf, size);
|
|---|
| 123 | if (ret == NULL) {
|
|---|
| 124 | /* Check errno to avoid shadowing other possible errors. */
|
|---|
| 125 | if (errno == EOK) {
|
|---|
| 126 | errno = ERANGE;
|
|---|
| 127 | }
|
|---|
| 128 | } else {
|
|---|
| 129 | /* Success, restore previous errno value. */
|
|---|
| 130 | errno = orig_errno;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | return ret;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Change the current working directory.
|
|---|
| 138 | *
|
|---|
| 139 | * @param path New working directory.
|
|---|
| 140 | */
|
|---|
| 141 | int posix_chdir(const char *path)
|
|---|
| 142 | {
|
|---|
| 143 | return errnify(chdir, path);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /**
|
|---|
| 147 | * Determine the page size of the current run of the process.
|
|---|
| 148 | *
|
|---|
| 149 | * @return Page size of the process.
|
|---|
| 150 | */
|
|---|
| 151 | int posix_getpagesize(void)
|
|---|
| 152 | {
|
|---|
| 153 | return getpagesize();
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /**
|
|---|
| 157 | * Get the process ID of the calling process.
|
|---|
| 158 | *
|
|---|
| 159 | * @return Process ID.
|
|---|
| 160 | */
|
|---|
| 161 | posix_pid_t posix_getpid(void)
|
|---|
| 162 | {
|
|---|
| 163 | return task_get_id();
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /**
|
|---|
| 167 | * Get the real user ID of the calling process.
|
|---|
| 168 | *
|
|---|
| 169 | * @return User ID.
|
|---|
| 170 | */
|
|---|
| 171 | posix_uid_t posix_getuid(void)
|
|---|
| 172 | {
|
|---|
| 173 | /* There is currently no support for user accounts in HelenOS. */
|
|---|
| 174 | return 0;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /**
|
|---|
| 178 | * Get the real group ID of the calling process.
|
|---|
| 179 | *
|
|---|
| 180 | * @return Group ID.
|
|---|
| 181 | */
|
|---|
| 182 | posix_gid_t posix_getgid(void)
|
|---|
| 183 | {
|
|---|
| 184 | /* There is currently no support for user accounts in HelenOS. */
|
|---|
| 185 | return 0;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | /**
|
|---|
| 189 | * Close a file.
|
|---|
| 190 | *
|
|---|
| 191 | * @param fildes File descriptor of the opened file.
|
|---|
| 192 | * @return 0 on success, -1 on error.
|
|---|
| 193 | */
|
|---|
| 194 | int posix_close(int fildes)
|
|---|
| 195 | {
|
|---|
| 196 | return errnify(close, fildes);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | /**
|
|---|
| 200 | * Read from a file.
|
|---|
| 201 | *
|
|---|
| 202 | * @param fildes File descriptor of the opened file.
|
|---|
| 203 | * @param buf Buffer to which the read bytes shall be stored.
|
|---|
| 204 | * @param nbyte Upper limit on the number of read bytes.
|
|---|
| 205 | * @return Number of read bytes on success, -1 otherwise.
|
|---|
| 206 | */
|
|---|
| 207 | ssize_t posix_read(int fildes, void *buf, size_t nbyte)
|
|---|
| 208 | {
|
|---|
| 209 | return errnify(read, fildes, buf, nbyte);
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | /**
|
|---|
| 213 | * Write to a file.
|
|---|
| 214 | *
|
|---|
| 215 | * @param fildes File descriptor of the opened file.
|
|---|
| 216 | * @param buf Buffer to write.
|
|---|
| 217 | * @param nbyte Size of the buffer.
|
|---|
| 218 | * @return Number of written bytes on success, -1 otherwise.
|
|---|
| 219 | */
|
|---|
| 220 | ssize_t posix_write(int fildes, const void *buf, size_t nbyte)
|
|---|
| 221 | {
|
|---|
| 222 | return errnify(write, fildes, buf, nbyte);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /**
|
|---|
| 226 | * Reposition read/write file offset
|
|---|
| 227 | *
|
|---|
| 228 | * @param fildes File descriptor of the opened file.
|
|---|
| 229 | * @param offset New offset in the file.
|
|---|
| 230 | * @param whence The position from which the offset argument is specified.
|
|---|
| 231 | * @return Upon successful completion, returns the resulting offset
|
|---|
| 232 | * as measured in bytes from the beginning of the file, -1 otherwise.
|
|---|
| 233 | */
|
|---|
| 234 | posix_off_t posix_lseek(int fildes, posix_off_t offset, int whence)
|
|---|
| 235 | {
|
|---|
| 236 | return errnify(lseek, fildes, offset, whence);
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | /**
|
|---|
| 240 | * Requests outstanding data to be written to the underlying storage device.
|
|---|
| 241 | *
|
|---|
| 242 | * @param fildes File descriptor of the opened file.
|
|---|
| 243 | * @return Zero on success, -1 otherwise.
|
|---|
| 244 | */
|
|---|
| 245 | int posix_fsync(int fildes)
|
|---|
| 246 | {
|
|---|
| 247 | return errnify(fsync, fildes);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | /**
|
|---|
| 251 | * Truncate a file to a specified length.
|
|---|
| 252 | *
|
|---|
| 253 | * @param fildes File descriptor of the opened file.
|
|---|
| 254 | * @param length New length of the file.
|
|---|
| 255 | * @return Zero on success, -1 otherwise.
|
|---|
| 256 | */
|
|---|
| 257 | int posix_ftruncate(int fildes, posix_off_t length)
|
|---|
| 258 | {
|
|---|
| 259 | return errnify(ftruncate, fildes, (aoff64_t) length);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /**
|
|---|
| 263 | * Remove a directory.
|
|---|
| 264 | *
|
|---|
| 265 | * @param path Directory pathname.
|
|---|
| 266 | * @return Zero on success, -1 otherwise.
|
|---|
| 267 | */
|
|---|
| 268 | int posix_rmdir(const char *path)
|
|---|
| 269 | {
|
|---|
| 270 | return errnify(rmdir, path);
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | /**
|
|---|
| 274 | * Remove a link to a file.
|
|---|
| 275 | *
|
|---|
| 276 | * @param path File pathname.
|
|---|
| 277 | * @return Zero on success, -1 otherwise.
|
|---|
| 278 | */
|
|---|
| 279 | int posix_unlink(const char *path)
|
|---|
| 280 | {
|
|---|
| 281 | return errnify(unlink, path);
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | /**
|
|---|
| 285 | * Duplicate an open file descriptor.
|
|---|
| 286 | *
|
|---|
| 287 | * @param fildes File descriptor to be duplicated.
|
|---|
| 288 | * @return On success, new file descriptor for the same file, otherwise -1.
|
|---|
| 289 | */
|
|---|
| 290 | int posix_dup(int fildes)
|
|---|
| 291 | {
|
|---|
| 292 | return posix_fcntl(fildes, F_DUPFD, 0);
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | /**
|
|---|
| 296 | * Duplicate an open file descriptor.
|
|---|
| 297 | *
|
|---|
| 298 | * @param fildes File descriptor to be duplicated.
|
|---|
| 299 | * @param fildes2 File descriptor to be paired with the same file description
|
|---|
| 300 | * as is paired fildes.
|
|---|
| 301 | * @return fildes2 on success, -1 otherwise.
|
|---|
| 302 | */
|
|---|
| 303 | int posix_dup2(int fildes, int fildes2)
|
|---|
| 304 | {
|
|---|
| 305 | return errnify(dup2, fildes, fildes2);
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | /**
|
|---|
| 309 | * Determine accessibility of a file.
|
|---|
| 310 | *
|
|---|
| 311 | * @param path File to check accessibility for.
|
|---|
| 312 | * @param amode Either check for existence or intended access mode.
|
|---|
| 313 | * @return Zero on success, -1 otherwise.
|
|---|
| 314 | */
|
|---|
| 315 | int posix_access(const char *path, int amode)
|
|---|
| 316 | {
|
|---|
| 317 | if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
|
|---|
| 318 | /* HelenOS doesn't support permissions, permission checks
|
|---|
| 319 | * are equal to existence check.
|
|---|
| 320 | *
|
|---|
| 321 | * Check file existence by attempting to open it.
|
|---|
| 322 | */
|
|---|
| 323 | int fd = open(path, O_RDONLY);
|
|---|
| 324 | if (fd < 0) {
|
|---|
| 325 | errno = -fd;
|
|---|
| 326 | return -1;
|
|---|
| 327 | }
|
|---|
| 328 | close(fd);
|
|---|
| 329 | return 0;
|
|---|
| 330 | } else {
|
|---|
| 331 | /* Invalid amode argument. */
|
|---|
| 332 | errno = EINVAL;
|
|---|
| 333 | return -1;
|
|---|
| 334 | }
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | /**
|
|---|
| 338 | * Get configurable system variables.
|
|---|
| 339 | *
|
|---|
| 340 | * @param name Variable name.
|
|---|
| 341 | * @return Variable value.
|
|---|
| 342 | */
|
|---|
| 343 | long posix_sysconf(int name)
|
|---|
| 344 | {
|
|---|
| 345 | long clk_tck = 0;
|
|---|
| 346 | size_t cpu_count = 0;
|
|---|
| 347 | stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
|
|---|
| 348 | if (cpu_stats && cpu_count > 0) {
|
|---|
| 349 | clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
|
|---|
| 350 | }
|
|---|
| 351 | if (cpu_stats) {
|
|---|
| 352 | free(cpu_stats);
|
|---|
| 353 | cpu_stats = 0;
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | long phys_pages = 0;
|
|---|
| 357 | long avphys_pages = 0;
|
|---|
| 358 | stats_physmem_t *mem_stats = stats_get_physmem();
|
|---|
| 359 | if (mem_stats) {
|
|---|
| 360 | phys_pages = (long) (mem_stats->total / getpagesize());
|
|---|
| 361 | avphys_pages = (long) (mem_stats->free / getpagesize());
|
|---|
| 362 | free(mem_stats);
|
|---|
| 363 | mem_stats = 0;
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | switch (name) {
|
|---|
| 367 | case _SC_PHYS_PAGES:
|
|---|
| 368 | return phys_pages;
|
|---|
| 369 | case _SC_AVPHYS_PAGES:
|
|---|
| 370 | return avphys_pages;
|
|---|
| 371 | case _SC_PAGESIZE:
|
|---|
| 372 | return getpagesize();
|
|---|
| 373 | case _SC_CLK_TCK:
|
|---|
| 374 | return clk_tck;
|
|---|
| 375 | default:
|
|---|
| 376 | errno = EINVAL;
|
|---|
| 377 | return -1;
|
|---|
| 378 | }
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | /**
|
|---|
| 382 | *
|
|---|
| 383 | * @param path
|
|---|
| 384 | * @param name
|
|---|
| 385 | * @return
|
|---|
| 386 | */
|
|---|
| 387 | long posix_pathconf(const char *path, int name)
|
|---|
| 388 | {
|
|---|
| 389 | // TODO: low priority, just a compile-time dependency of binutils
|
|---|
| 390 | not_implemented();
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | /**
|
|---|
| 394 | *
|
|---|
| 395 | * @return
|
|---|
| 396 | */
|
|---|
| 397 | posix_pid_t posix_fork(void)
|
|---|
| 398 | {
|
|---|
| 399 | // TODO: low priority, just a compile-time dependency of binutils
|
|---|
| 400 | not_implemented();
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | /**
|
|---|
| 404 | *
|
|---|
| 405 | * @param path
|
|---|
| 406 | * @param argv
|
|---|
| 407 | * @return
|
|---|
| 408 | */
|
|---|
| 409 | int posix_execv(const char *path, char *const argv[])
|
|---|
| 410 | {
|
|---|
| 411 | // TODO: low priority, just a compile-time dependency of binutils
|
|---|
| 412 | not_implemented();
|
|---|
| 413 | }
|
|---|
| 414 |
|
|---|
| 415 | /**
|
|---|
| 416 | *
|
|---|
| 417 | * @param file
|
|---|
| 418 | * @param argv
|
|---|
| 419 | * @return
|
|---|
| 420 | */
|
|---|
| 421 | int posix_execvp(const char *file, char *const argv[])
|
|---|
| 422 | {
|
|---|
| 423 | // TODO: low priority, just a compile-time dependency of binutils
|
|---|
| 424 | not_implemented();
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | /**
|
|---|
| 428 | *
|
|---|
| 429 | * @param fildes
|
|---|
| 430 | * @return
|
|---|
| 431 | */
|
|---|
| 432 | int posix_pipe(int fildes[2])
|
|---|
| 433 | {
|
|---|
| 434 | // TODO: low priority, just a compile-time dependency of binutils
|
|---|
| 435 | not_implemented();
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | unsigned int posix_alarm(unsigned int seconds)
|
|---|
| 439 | {
|
|---|
| 440 | not_implemented();
|
|---|
| 441 | return 0;
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | /** @}
|
|---|
| 445 | */
|
|---|