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