[ad28599] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Petr Koupy
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libposix
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #define LIBPOSIX_INTERNAL
|
---|
| 36 |
|
---|
[9b1503e] | 37 | #include "internal/common.h"
|
---|
[ad28599] | 38 | #include "fcntl.h"
|
---|
[4ec6820] | 39 | #include "libc/unistd.h"
|
---|
| 40 | #include "libc/vfs/vfs.h"
|
---|
| 41 | #include <errno.h>
|
---|
[ad28599] | 42 |
|
---|
| 43 | /**
|
---|
[4ec6820] | 44 | * Performs set of operations on the opened files.
|
---|
| 45 | *
|
---|
| 46 | * @param fd File descriptor of the opened file.
|
---|
| 47 | * @param cmd Operation to carry out.
|
---|
| 48 | * @return Non-negative on success. Might have special meaning corresponding
|
---|
| 49 | * to the requested operation.
|
---|
[ad28599] | 50 | */
|
---|
| 51 | int posix_fcntl(int fd, int cmd, ...)
|
---|
| 52 | {
|
---|
[4ec6820] | 53 | int rc;
|
---|
| 54 | int flags;
|
---|
| 55 |
|
---|
| 56 | switch (cmd) {
|
---|
| 57 | case F_DUPFD:
|
---|
| 58 | case F_DUPFD_CLOEXEC: /* FD_CLOEXEC is not supported. */
|
---|
| 59 | /* VFS does not provide means to express constraints on the new
|
---|
| 60 | * file descriptor so the third argument is ignored. */
|
---|
| 61 |
|
---|
| 62 | /* Retrieve node triplet corresponding to the file descriptor. */
|
---|
| 63 | /* Empty statement after label. */;
|
---|
| 64 | fdi_node_t node;
|
---|
| 65 | rc = fd_node(fd, &node);
|
---|
| 66 | if (rc != EOK) {
|
---|
| 67 | // TODO: propagate a POSIX compatible errno
|
---|
| 68 | return -1;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /* Reopen the node so the fresh file descriptor is generated. */
|
---|
| 72 | int newfd = open_node(&node, 0);
|
---|
| 73 | if (newfd < 0) {
|
---|
| 74 | // TODO: propagate a POSIX compatible errno
|
---|
| 75 | return -1;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /* Associate the newly generated descriptor to the file description
|
---|
| 79 | * of the old file descriptor. Just reopened node will be automatically
|
---|
| 80 | * closed. */
|
---|
| 81 | rc = dup2(fd, newfd);
|
---|
| 82 | if (rc != EOK) {
|
---|
| 83 | // TODO: propagate a POSIX compatible errno
|
---|
| 84 | return -1;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | return newfd;
|
---|
| 88 | case F_GETFD:
|
---|
| 89 | /* FD_CLOEXEC is not supported. There are no other flags. */
|
---|
| 90 | return 0;
|
---|
| 91 | case F_SETFD:
|
---|
| 92 | /* FD_CLOEXEC is not supported. Ignore arguments and report success. */
|
---|
| 93 | return 0;
|
---|
| 94 | case F_GETFL:
|
---|
| 95 | /* File status flags (i.e. O_APPEND) are currently private to the
|
---|
| 96 | * VFS server so it cannot be easily retrieved. */
|
---|
| 97 | flags = 0;
|
---|
| 98 | /* File access flags are currently not supported for file descriptors.
|
---|
| 99 | * Provide full access. */
|
---|
| 100 | flags |= O_RDWR;
|
---|
| 101 | return flags;
|
---|
| 102 | case F_SETFL:
|
---|
| 103 | /* File access flags are currently not supported for file descriptors.
|
---|
| 104 | * Ignore arguments and report success. */
|
---|
| 105 | return 0;
|
---|
| 106 | case F_GETOWN:
|
---|
| 107 | case F_SETOWN:
|
---|
| 108 | case F_GETLK:
|
---|
| 109 | case F_SETLK:
|
---|
| 110 | case F_SETLKW:
|
---|
| 111 | /* Signals (SIGURG) and file locks are not supported. */
|
---|
| 112 | errno = ENOTSUP;
|
---|
| 113 | return -1;
|
---|
| 114 | default:
|
---|
| 115 | /* Unknown command */
|
---|
| 116 | errno = EINVAL;
|
---|
| 117 | return -1;
|
---|
| 118 | }
|
---|
[ad28599] | 119 | }
|
---|
| 120 |
|
---|
| 121 | /** @}
|
---|
| 122 | */
|
---|