[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
|
---|
[fdf97f6] | 37 | #define __POSIX_DEF__(x) posix_##x
|
---|
[4f4b4e7] | 38 |
|
---|
[9b1503e] | 39 | #include "internal/common.h"
|
---|
[a3da2b2] | 40 | #include "posix/unistd.h"
|
---|
[ec18957a] | 41 |
|
---|
[a3da2b2] | 42 | #include "posix/errno.h"
|
---|
| 43 | #include "posix/string.h"
|
---|
| 44 | #include "posix/fcntl.h"
|
---|
[ec18957a] | 45 |
|
---|
| 46 | #include "libc/task.h"
|
---|
| 47 | #include "libc/stats.h"
|
---|
[fb872c1] | 48 | #include "libc/malloc.h"
|
---|
[a16210b5] | 49 |
|
---|
[b4d6252] | 50 | /* Array of environment variable strings (NAME=VALUE). */
|
---|
[7530a00] | 51 | char **posix_environ = NULL;
|
---|
[a92f13d] | 52 | char *posix_optarg;
|
---|
[b4d6252] | 53 |
|
---|
[244d6fd] | 54 | /**
|
---|
| 55 | * Get current user name.
|
---|
[fb872c1] | 56 | *
|
---|
| 57 | * @return User name (static) string or NULL if not found.
|
---|
[244d6fd] | 58 | */
|
---|
| 59 | char *posix_getlogin(void)
|
---|
| 60 | {
|
---|
[fb872c1] | 61 | /* There is currently no support for user accounts in HelenOS. */
|
---|
[244d6fd] | 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.
|
---|
[fb872c1] | 70 | * @return Zero on success, error code otherwise.
|
---|
[244d6fd] | 71 | */
|
---|
| 72 | int posix_getlogin_r(char *name, size_t namesize)
|
---|
| 73 | {
|
---|
[fb872c1] | 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 | }
|
---|
[244d6fd] | 82 | }
|
---|
| 83 |
|
---|
[4f4b4e7] | 84 | /**
|
---|
[fb872c1] | 85 | * Test whether open file descriptor is associated with a terminal.
|
---|
[8d7e82c1] | 86 | *
|
---|
[fb872c1] | 87 | * @param fd Open file descriptor to test.
|
---|
| 88 | * @return Boolean result of the test.
|
---|
[4f4b4e7] | 89 | */
|
---|
| 90 | int posix_isatty(int fd)
|
---|
| 91 | {
|
---|
[4c8f5e7] | 92 | // TODO
|
---|
[fb872c1] | 93 | /* Always returns false, because there is no easy way to find
|
---|
[d542aad] | 94 | * out under HelenOS. */
|
---|
[94f8b81c] | 95 | return 0;
|
---|
[a16210b5] | 96 | }
|
---|
| 97 |
|
---|
[221afc9e] | 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 | }
|
---|
[9350bfdd] | 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 |
|
---|
[221afc9e] | 122 | char *ret = getcwd(buf, size);
|
---|
[9350bfdd] | 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;
|
---|
[221afc9e] | 131 | }
|
---|
[9350bfdd] | 132 |
|
---|
[221afc9e] | 133 | return ret;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[75406dc] | 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 |
|
---|
[72ec8cc] | 146 | /**
|
---|
[fb872c1] | 147 | * Determine the page size of the current run of the process.
|
---|
| 148 | *
|
---|
| 149 | * @return Page size of the process.
|
---|
[72ec8cc] | 150 | */
|
---|
| 151 | int posix_getpagesize(void)
|
---|
| 152 | {
|
---|
| 153 | return getpagesize();
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[823a929] | 156 | /**
|
---|
[fb872c1] | 157 | * Get the process ID of the calling process.
|
---|
| 158 | *
|
---|
| 159 | * @return Process ID.
|
---|
[823a929] | 160 | */
|
---|
| 161 | posix_pid_t posix_getpid(void)
|
---|
| 162 | {
|
---|
| 163 | return task_get_id();
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[59f799b] | 166 | /**
|
---|
[fb872c1] | 167 | * Get the real user ID of the calling process.
|
---|
[59f799b] | 168 | *
|
---|
[fb872c1] | 169 | * @return User ID.
|
---|
[59f799b] | 170 | */
|
---|
| 171 | posix_uid_t posix_getuid(void)
|
---|
| 172 | {
|
---|
[fb872c1] | 173 | /* There is currently no support for user accounts in HelenOS. */
|
---|
| 174 | return 0;
|
---|
[59f799b] | 175 | }
|
---|
| 176 |
|
---|
| 177 | /**
|
---|
[fb872c1] | 178 | * Get the real group ID of the calling process.
|
---|
[59f799b] | 179 | *
|
---|
[fb872c1] | 180 | * @return Group ID.
|
---|
[59f799b] | 181 | */
|
---|
| 182 | posix_gid_t posix_getgid(void)
|
---|
| 183 | {
|
---|
[fb872c1] | 184 | /* There is currently no support for user accounts in HelenOS. */
|
---|
| 185 | return 0;
|
---|
[59f799b] | 186 | }
|
---|
| 187 |
|
---|
[75406dc] | 188 | /**
|
---|
| 189 | * Close a file.
|
---|
| 190 | *
|
---|
[2a53f71] | 191 | * @param fildes File descriptor of the opened file.
|
---|
[75406dc] | 192 | * @return 0 on success, -1 on error.
|
---|
| 193 | */
|
---|
| 194 | int posix_close(int fildes)
|
---|
| 195 | {
|
---|
| 196 | return errnify(close, fildes);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[221afc9e] | 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 | {
|
---|
[75406dc] | 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 |
|
---|
[e3480d5] | 225 | /**
|
---|
| 226 | * Reposition read/write file offset
|
---|
| 227 | *
|
---|
| 228 | * @param fildes File descriptor of the opened file.
|
---|
| 229 | * @return Upon successful completion, returns the resulting offset
|
---|
| 230 | * as measured in bytes from the beginning of the file, -1 otherwise.
|
---|
| 231 | */
|
---|
| 232 | posix_off_t posix_lseek(int fildes, posix_off_t offset, int whence)
|
---|
| 233 | {
|
---|
| 234 | return errnify(lseek, fildes, offset, whence);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[75406dc] | 237 | /**
|
---|
| 238 | * Requests outstanding data to be written to the underlying storage device.
|
---|
| 239 | *
|
---|
[2a53f71] | 240 | * @param fildes File descriptor of the opened file.
|
---|
| 241 | * @return Zero on success, -1 otherwise.
|
---|
[75406dc] | 242 | */
|
---|
| 243 | int posix_fsync(int fildes)
|
---|
| 244 | {
|
---|
| 245 | return errnify(fsync, fildes);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[2a53f71] | 248 | /**
|
---|
| 249 | * Truncate a file to a specified length.
|
---|
| 250 | *
|
---|
| 251 | * @param fildes File descriptor of the opened file.
|
---|
| 252 | * @param length New length of the file.
|
---|
| 253 | * @return Zero on success, -1 otherwise.
|
---|
| 254 | */
|
---|
[75406dc] | 255 | int posix_ftruncate(int fildes, posix_off_t length)
|
---|
| 256 | {
|
---|
| 257 | return errnify(ftruncate, fildes, (aoff64_t) length);
|
---|
[221afc9e] | 258 | }
|
---|
| 259 |
|
---|
[58115ae] | 260 | /**
|
---|
| 261 | * Remove a directory.
|
---|
| 262 | *
|
---|
| 263 | * @param path Directory pathname.
|
---|
| 264 | * @return Zero on success, -1 otherwise.
|
---|
| 265 | */
|
---|
| 266 | int posix_rmdir(const char *path)
|
---|
| 267 | {
|
---|
[75406dc] | 268 | return errnify(rmdir, path);
|
---|
[58115ae] | 269 | }
|
---|
| 270 |
|
---|
[221afc9e] | 271 | /**
|
---|
| 272 | * Remove a link to a file.
|
---|
| 273 | *
|
---|
| 274 | * @param path File pathname.
|
---|
| 275 | * @return Zero on success, -1 otherwise.
|
---|
| 276 | */
|
---|
| 277 | int posix_unlink(const char *path)
|
---|
| 278 | {
|
---|
[75406dc] | 279 | return errnify(unlink, path);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[2a53f71] | 282 | /**
|
---|
| 283 | * Duplicate an open file descriptor.
|
---|
| 284 | *
|
---|
| 285 | * @param fildes File descriptor to be duplicated.
|
---|
| 286 | * @return On success, new file descriptor for the same file, otherwise -1.
|
---|
| 287 | */
|
---|
[75406dc] | 288 | int posix_dup(int fildes)
|
---|
| 289 | {
|
---|
| 290 | return posix_fcntl(fildes, F_DUPFD, 0);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
[2a53f71] | 293 | /**
|
---|
| 294 | * Duplicate an open file descriptor.
|
---|
| 295 | *
|
---|
| 296 | * @param fildes File descriptor to be duplicated.
|
---|
| 297 | * @param fildes2 File descriptor to be paired with the same file description
|
---|
| 298 | * as is paired fildes.
|
---|
| 299 | * @return fildes2 on success, -1 otherwise.
|
---|
| 300 | */
|
---|
[75406dc] | 301 | int posix_dup2(int fildes, int fildes2)
|
---|
| 302 | {
|
---|
| 303 | return errnify(dup2, fildes, fildes2);
|
---|
[221afc9e] | 304 | }
|
---|
| 305 |
|
---|
[b08ef1fd] | 306 | /**
|
---|
[fb872c1] | 307 | * Determine accessibility of a file.
|
---|
| 308 | *
|
---|
| 309 | * @param path File to check accessibility for.
|
---|
| 310 | * @param amode Either check for existence or intended access mode.
|
---|
| 311 | * @return Zero on success, -1 otherwise.
|
---|
[b08ef1fd] | 312 | */
|
---|
| 313 | int posix_access(const char *path, int amode)
|
---|
| 314 | {
|
---|
[75406dc] | 315 | if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
|
---|
| 316 | /* HelenOS doesn't support permissions, permission checks
|
---|
| 317 | * are equal to existence check.
|
---|
| 318 | *
|
---|
| 319 | * Check file existence by attempting to open it.
|
---|
| 320 | */
|
---|
[fb872c1] | 321 | int fd = open(path, O_RDONLY);
|
---|
[75406dc] | 322 | if (fd < 0) {
|
---|
| 323 | errno = -fd;
|
---|
[955c2b0] | 324 | return -1;
|
---|
[ec18957a] | 325 | }
|
---|
[75406dc] | 326 | close(fd);
|
---|
[955c2b0] | 327 | return 0;
|
---|
[fb872c1] | 328 | } else {
|
---|
| 329 | /* Invalid amode argument. */
|
---|
| 330 | errno = EINVAL;
|
---|
| 331 | return -1;
|
---|
| 332 | }
|
---|
[b08ef1fd] | 333 | }
|
---|
| 334 |
|
---|
[59f799b] | 335 | /**
|
---|
[fb872c1] | 336 | * Get configurable system variables.
|
---|
[59f799b] | 337 | *
|
---|
[fb872c1] | 338 | * @param name Variable name.
|
---|
| 339 | * @return Variable value.
|
---|
[59f799b] | 340 | */
|
---|
| 341 | long posix_sysconf(int name)
|
---|
| 342 | {
|
---|
[fb872c1] | 343 | long clk_tck = 0;
|
---|
| 344 | size_t cpu_count = 0;
|
---|
| 345 | stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
|
---|
| 346 | if (cpu_stats && cpu_count > 0) {
|
---|
| 347 | clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
|
---|
| 348 | }
|
---|
[94f8b81c] | 349 | if (cpu_stats) {
|
---|
| 350 | free(cpu_stats);
|
---|
| 351 | cpu_stats = 0;
|
---|
| 352 | }
|
---|
[fb872c1] | 353 |
|
---|
| 354 | long phys_pages = 0;
|
---|
| 355 | long avphys_pages = 0;
|
---|
| 356 | stats_physmem_t *mem_stats = stats_get_physmem();
|
---|
| 357 | if (mem_stats) {
|
---|
| 358 | phys_pages = (long) (mem_stats->total / getpagesize());
|
---|
| 359 | avphys_pages = (long) (mem_stats->free / getpagesize());
|
---|
[94f8b81c] | 360 | free(mem_stats);
|
---|
| 361 | mem_stats = 0;
|
---|
[fb872c1] | 362 | }
|
---|
| 363 |
|
---|
| 364 | switch (name) {
|
---|
| 365 | case _SC_PHYS_PAGES:
|
---|
| 366 | return phys_pages;
|
---|
| 367 | case _SC_AVPHYS_PAGES:
|
---|
| 368 | return avphys_pages;
|
---|
| 369 | case _SC_PAGESIZE:
|
---|
| 370 | return getpagesize();
|
---|
| 371 | case _SC_CLK_TCK:
|
---|
| 372 | return clk_tck;
|
---|
| 373 | default:
|
---|
| 374 | errno = EINVAL;
|
---|
| 375 | return -1;
|
---|
| 376 | }
|
---|
[59f799b] | 377 | }
|
---|
| 378 |
|
---|
[823a929] | 379 | /**
|
---|
| 380 | *
|
---|
| 381 | * @param path
|
---|
| 382 | * @param name
|
---|
| 383 | * @return
|
---|
| 384 | */
|
---|
| 385 | long posix_pathconf(const char *path, int name)
|
---|
| 386 | {
|
---|
| 387 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 388 | not_implemented();
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | /**
|
---|
| 392 | *
|
---|
| 393 | * @return
|
---|
| 394 | */
|
---|
| 395 | posix_pid_t posix_fork(void)
|
---|
| 396 | {
|
---|
| 397 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 398 | not_implemented();
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | /**
|
---|
| 402 | *
|
---|
| 403 | * @param path
|
---|
| 404 | * @param argv
|
---|
| 405 | * @return
|
---|
| 406 | */
|
---|
| 407 | int posix_execv(const char *path, char *const argv[])
|
---|
| 408 | {
|
---|
| 409 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 410 | not_implemented();
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | /**
|
---|
| 414 | *
|
---|
| 415 | * @param file
|
---|
| 416 | * @param argv
|
---|
| 417 | * @return
|
---|
| 418 | */
|
---|
| 419 | int posix_execvp(const char *file, char *const argv[])
|
---|
| 420 | {
|
---|
| 421 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 422 | not_implemented();
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | /**
|
---|
| 426 | *
|
---|
| 427 | * @param fildes
|
---|
| 428 | * @return
|
---|
| 429 | */
|
---|
| 430 | int posix_pipe(int fildes[2])
|
---|
| 431 | {
|
---|
| 432 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 433 | not_implemented();
|
---|
| 434 | }
|
---|
| 435 |
|
---|
[5759642f] | 436 | unsigned int posix_alarm(unsigned int seconds)
|
---|
| 437 | {
|
---|
| 438 | not_implemented();
|
---|
| 439 | return 0;
|
---|
| 440 | }
|
---|
| 441 |
|
---|
[4f4b4e7] | 442 | /** @}
|
---|
| 443 | */
|
---|