[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 | {
|
---|
[4c8f5e7] | 90 | // TODO
|
---|
[fb872c1] | 91 | /* Always returns false, because there is no easy way to find
|
---|
[d542aad] | 92 | * out under HelenOS. */
|
---|
[94f8b81c] | 93 | return 0;
|
---|
[a16210b5] | 94 | }
|
---|
| 95 |
|
---|
[221afc9e] | 96 | /**
|
---|
| 97 | * Get the pathname of the current working directory.
|
---|
| 98 | *
|
---|
| 99 | * @param buf Buffer into which the pathname shall be put.
|
---|
| 100 | * @param size Size of the buffer.
|
---|
| 101 | * @return Buffer pointer on success, NULL on failure.
|
---|
| 102 | */
|
---|
| 103 | char *posix_getcwd(char *buf, size_t size)
|
---|
| 104 | {
|
---|
| 105 | /* Native getcwd() does not set any errno despite the fact that general
|
---|
| 106 | * usage pattern of this function depends on it (caller is repeatedly
|
---|
| 107 | * guessing the required size of the buffer by checking for ERANGE on
|
---|
| 108 | * failure). */
|
---|
| 109 | if (size == 0) {
|
---|
| 110 | errno = EINVAL;
|
---|
| 111 | return NULL;
|
---|
| 112 | }
|
---|
[9350bfdd] | 113 |
|
---|
| 114 | /* Save the original value to comply with the "no modification on
|
---|
| 115 | * success" semantics.
|
---|
| 116 | */
|
---|
| 117 | int orig_errno = errno;
|
---|
| 118 | errno = EOK;
|
---|
| 119 |
|
---|
[221afc9e] | 120 | char *ret = getcwd(buf, size);
|
---|
[9350bfdd] | 121 | if (ret == NULL) {
|
---|
| 122 | /* Check errno to avoid shadowing other possible errors. */
|
---|
| 123 | if (errno == EOK) {
|
---|
| 124 | errno = ERANGE;
|
---|
| 125 | }
|
---|
| 126 | } else {
|
---|
| 127 | /* Success, restore previous errno value. */
|
---|
| 128 | errno = orig_errno;
|
---|
[221afc9e] | 129 | }
|
---|
[9350bfdd] | 130 |
|
---|
[221afc9e] | 131 | return ret;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[75406dc] | 134 | /**
|
---|
| 135 | * Change the current working directory.
|
---|
| 136 | *
|
---|
| 137 | * @param path New working directory.
|
---|
| 138 | */
|
---|
| 139 | int posix_chdir(const char *path)
|
---|
| 140 | {
|
---|
| 141 | return errnify(chdir, path);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[72ec8cc] | 144 | /**
|
---|
[fb872c1] | 145 | * Determine the page size of the current run of the process.
|
---|
| 146 | *
|
---|
| 147 | * @return Page size of the process.
|
---|
[72ec8cc] | 148 | */
|
---|
| 149 | int posix_getpagesize(void)
|
---|
| 150 | {
|
---|
| 151 | return getpagesize();
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[823a929] | 154 | /**
|
---|
[fb872c1] | 155 | * Get the process ID of the calling process.
|
---|
| 156 | *
|
---|
| 157 | * @return Process ID.
|
---|
[823a929] | 158 | */
|
---|
| 159 | posix_pid_t posix_getpid(void)
|
---|
| 160 | {
|
---|
| 161 | return task_get_id();
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[59f799b] | 164 | /**
|
---|
[fb872c1] | 165 | * Get the real user ID of the calling process.
|
---|
[59f799b] | 166 | *
|
---|
[fb872c1] | 167 | * @return User ID.
|
---|
[59f799b] | 168 | */
|
---|
| 169 | posix_uid_t posix_getuid(void)
|
---|
| 170 | {
|
---|
[fb872c1] | 171 | /* There is currently no support for user accounts in HelenOS. */
|
---|
| 172 | return 0;
|
---|
[59f799b] | 173 | }
|
---|
| 174 |
|
---|
| 175 | /**
|
---|
[fb872c1] | 176 | * Get the real group ID of the calling process.
|
---|
[59f799b] | 177 | *
|
---|
[fb872c1] | 178 | * @return Group ID.
|
---|
[59f799b] | 179 | */
|
---|
| 180 | posix_gid_t posix_getgid(void)
|
---|
| 181 | {
|
---|
[fb872c1] | 182 | /* There is currently no support for user accounts in HelenOS. */
|
---|
| 183 | return 0;
|
---|
[59f799b] | 184 | }
|
---|
| 185 |
|
---|
[75406dc] | 186 | /**
|
---|
| 187 | * Close a file.
|
---|
| 188 | *
|
---|
[2a53f71] | 189 | * @param fildes File descriptor of the opened file.
|
---|
[75406dc] | 190 | * @return 0 on success, -1 on error.
|
---|
| 191 | */
|
---|
| 192 | int posix_close(int fildes)
|
---|
| 193 | {
|
---|
| 194 | return errnify(close, fildes);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[221afc9e] | 197 | /**
|
---|
| 198 | * Read from a file.
|
---|
| 199 | *
|
---|
| 200 | * @param fildes File descriptor of the opened file.
|
---|
| 201 | * @param buf Buffer to which the read bytes shall be stored.
|
---|
| 202 | * @param nbyte Upper limit on the number of read bytes.
|
---|
| 203 | * @return Number of read bytes on success, -1 otherwise.
|
---|
| 204 | */
|
---|
| 205 | ssize_t posix_read(int fildes, void *buf, size_t nbyte)
|
---|
| 206 | {
|
---|
[75406dc] | 207 | return errnify(read, fildes, buf, nbyte);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /**
|
---|
| 211 | * Write to a file.
|
---|
| 212 | *
|
---|
| 213 | * @param fildes File descriptor of the opened file.
|
---|
| 214 | * @param buf Buffer to write.
|
---|
| 215 | * @param nbyte Size of the buffer.
|
---|
| 216 | * @return Number of written bytes on success, -1 otherwise.
|
---|
| 217 | */
|
---|
| 218 | ssize_t posix_write(int fildes, const void *buf, size_t nbyte)
|
---|
| 219 | {
|
---|
| 220 | return errnify(write, fildes, buf, nbyte);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | /**
|
---|
| 224 | * Requests outstanding data to be written to the underlying storage device.
|
---|
| 225 | *
|
---|
[2a53f71] | 226 | * @param fildes File descriptor of the opened file.
|
---|
| 227 | * @return Zero on success, -1 otherwise.
|
---|
[75406dc] | 228 | */
|
---|
| 229 | int posix_fsync(int fildes)
|
---|
| 230 | {
|
---|
| 231 | return errnify(fsync, fildes);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[2a53f71] | 234 | /**
|
---|
| 235 | * Truncate a file to a specified length.
|
---|
| 236 | *
|
---|
| 237 | * @param fildes File descriptor of the opened file.
|
---|
| 238 | * @param length New length of the file.
|
---|
| 239 | * @return Zero on success, -1 otherwise.
|
---|
| 240 | */
|
---|
[75406dc] | 241 | int posix_ftruncate(int fildes, posix_off_t length)
|
---|
| 242 | {
|
---|
| 243 | return errnify(ftruncate, fildes, (aoff64_t) length);
|
---|
[221afc9e] | 244 | }
|
---|
| 245 |
|
---|
[58115ae] | 246 | /**
|
---|
| 247 | * Remove a directory.
|
---|
| 248 | *
|
---|
| 249 | * @param path Directory pathname.
|
---|
| 250 | * @return Zero on success, -1 otherwise.
|
---|
| 251 | */
|
---|
| 252 | int posix_rmdir(const char *path)
|
---|
| 253 | {
|
---|
[75406dc] | 254 | return errnify(rmdir, path);
|
---|
[58115ae] | 255 | }
|
---|
| 256 |
|
---|
[221afc9e] | 257 | /**
|
---|
| 258 | * Remove a link to a file.
|
---|
| 259 | *
|
---|
| 260 | * @param path File pathname.
|
---|
| 261 | * @return Zero on success, -1 otherwise.
|
---|
| 262 | */
|
---|
| 263 | int posix_unlink(const char *path)
|
---|
| 264 | {
|
---|
[75406dc] | 265 | return errnify(unlink, path);
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[2a53f71] | 268 | /**
|
---|
| 269 | * Duplicate an open file descriptor.
|
---|
| 270 | *
|
---|
| 271 | * @param fildes File descriptor to be duplicated.
|
---|
| 272 | * @return On success, new file descriptor for the same file, otherwise -1.
|
---|
| 273 | */
|
---|
[75406dc] | 274 | int posix_dup(int fildes)
|
---|
| 275 | {
|
---|
| 276 | return posix_fcntl(fildes, F_DUPFD, 0);
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[2a53f71] | 279 | /**
|
---|
| 280 | * Duplicate an open file descriptor.
|
---|
| 281 | *
|
---|
| 282 | * @param fildes File descriptor to be duplicated.
|
---|
| 283 | * @param fildes2 File descriptor to be paired with the same file description
|
---|
| 284 | * as is paired fildes.
|
---|
| 285 | * @return fildes2 on success, -1 otherwise.
|
---|
| 286 | */
|
---|
[75406dc] | 287 | int posix_dup2(int fildes, int fildes2)
|
---|
| 288 | {
|
---|
| 289 | return errnify(dup2, fildes, fildes2);
|
---|
[221afc9e] | 290 | }
|
---|
| 291 |
|
---|
[b08ef1fd] | 292 | /**
|
---|
[fb872c1] | 293 | * Determine accessibility of a file.
|
---|
| 294 | *
|
---|
| 295 | * @param path File to check accessibility for.
|
---|
| 296 | * @param amode Either check for existence or intended access mode.
|
---|
| 297 | * @return Zero on success, -1 otherwise.
|
---|
[b08ef1fd] | 298 | */
|
---|
| 299 | int posix_access(const char *path, int amode)
|
---|
| 300 | {
|
---|
[75406dc] | 301 | if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
|
---|
| 302 | /* HelenOS doesn't support permissions, permission checks
|
---|
| 303 | * are equal to existence check.
|
---|
| 304 | *
|
---|
| 305 | * Check file existence by attempting to open it.
|
---|
| 306 | */
|
---|
[fb872c1] | 307 | int fd = open(path, O_RDONLY);
|
---|
[75406dc] | 308 | if (fd < 0) {
|
---|
| 309 | errno = -fd;
|
---|
[955c2b0] | 310 | return -1;
|
---|
[ec18957a] | 311 | }
|
---|
[75406dc] | 312 | close(fd);
|
---|
[955c2b0] | 313 | return 0;
|
---|
[fb872c1] | 314 | } else {
|
---|
| 315 | /* Invalid amode argument. */
|
---|
| 316 | errno = EINVAL;
|
---|
| 317 | return -1;
|
---|
| 318 | }
|
---|
[b08ef1fd] | 319 | }
|
---|
| 320 |
|
---|
[59f799b] | 321 | /**
|
---|
[fb872c1] | 322 | * Get configurable system variables.
|
---|
[59f799b] | 323 | *
|
---|
[fb872c1] | 324 | * @param name Variable name.
|
---|
| 325 | * @return Variable value.
|
---|
[59f799b] | 326 | */
|
---|
| 327 | long posix_sysconf(int name)
|
---|
| 328 | {
|
---|
[fb872c1] | 329 | long clk_tck = 0;
|
---|
| 330 | size_t cpu_count = 0;
|
---|
| 331 | stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
|
---|
| 332 | if (cpu_stats && cpu_count > 0) {
|
---|
| 333 | clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
|
---|
| 334 | }
|
---|
[94f8b81c] | 335 | if (cpu_stats) {
|
---|
| 336 | free(cpu_stats);
|
---|
| 337 | cpu_stats = 0;
|
---|
| 338 | }
|
---|
[fb872c1] | 339 |
|
---|
| 340 | long phys_pages = 0;
|
---|
| 341 | long avphys_pages = 0;
|
---|
| 342 | stats_physmem_t *mem_stats = stats_get_physmem();
|
---|
| 343 | if (mem_stats) {
|
---|
| 344 | phys_pages = (long) (mem_stats->total / getpagesize());
|
---|
| 345 | avphys_pages = (long) (mem_stats->free / getpagesize());
|
---|
[94f8b81c] | 346 | free(mem_stats);
|
---|
| 347 | mem_stats = 0;
|
---|
[fb872c1] | 348 | }
|
---|
| 349 |
|
---|
| 350 | switch (name) {
|
---|
| 351 | case _SC_PHYS_PAGES:
|
---|
| 352 | return phys_pages;
|
---|
| 353 | case _SC_AVPHYS_PAGES:
|
---|
| 354 | return avphys_pages;
|
---|
| 355 | case _SC_PAGESIZE:
|
---|
| 356 | return getpagesize();
|
---|
| 357 | case _SC_CLK_TCK:
|
---|
| 358 | return clk_tck;
|
---|
| 359 | default:
|
---|
| 360 | errno = EINVAL;
|
---|
| 361 | return -1;
|
---|
| 362 | }
|
---|
[59f799b] | 363 | }
|
---|
| 364 |
|
---|
[823a929] | 365 | /**
|
---|
| 366 | *
|
---|
| 367 | * @param path
|
---|
| 368 | * @param name
|
---|
| 369 | * @return
|
---|
| 370 | */
|
---|
| 371 | long posix_pathconf(const char *path, int name)
|
---|
| 372 | {
|
---|
| 373 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 374 | not_implemented();
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | /**
|
---|
| 378 | *
|
---|
| 379 | * @return
|
---|
| 380 | */
|
---|
| 381 | posix_pid_t posix_fork(void)
|
---|
| 382 | {
|
---|
| 383 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 384 | not_implemented();
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | /**
|
---|
| 388 | *
|
---|
| 389 | * @param path
|
---|
| 390 | * @param argv
|
---|
| 391 | * @return
|
---|
| 392 | */
|
---|
| 393 | int posix_execv(const char *path, char *const argv[])
|
---|
| 394 | {
|
---|
| 395 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 396 | not_implemented();
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | /**
|
---|
| 400 | *
|
---|
| 401 | * @param file
|
---|
| 402 | * @param argv
|
---|
| 403 | * @return
|
---|
| 404 | */
|
---|
| 405 | int posix_execvp(const char *file, char *const argv[])
|
---|
| 406 | {
|
---|
| 407 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 408 | not_implemented();
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | /**
|
---|
| 412 | *
|
---|
| 413 | * @param fildes
|
---|
| 414 | * @return
|
---|
| 415 | */
|
---|
| 416 | int posix_pipe(int fildes[2])
|
---|
| 417 | {
|
---|
| 418 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
| 419 | not_implemented();
|
---|
| 420 | }
|
---|
| 421 |
|
---|
[4f4b4e7] | 422 | /** @}
|
---|
| 423 | */
|
---|