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 File control.
|
---|
33 | */
|
---|
34 |
|
---|
35 | #define LIBPOSIX_INTERNAL
|
---|
36 |
|
---|
37 | #include "internal/common.h"
|
---|
38 | #include "fcntl.h"
|
---|
39 |
|
---|
40 | #include "libc/unistd.h"
|
---|
41 | #include "libc/vfs/vfs.h"
|
---|
42 | #include "errno.h"
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Performs set of operations on the opened files.
|
---|
46 | *
|
---|
47 | * @param fd File descriptor of the opened file.
|
---|
48 | * @param cmd Operation to carry out.
|
---|
49 | * @return Non-negative on success. Might have special meaning corresponding
|
---|
50 | * to the requested operation.
|
---|
51 | */
|
---|
52 | int posix_fcntl(int fd, int cmd, ...)
|
---|
53 | {
|
---|
54 | int rc;
|
---|
55 | int flags;
|
---|
56 |
|
---|
57 | switch (cmd) {
|
---|
58 | case F_DUPFD:
|
---|
59 | case F_DUPFD_CLOEXEC: /* FD_CLOEXEC is not supported. */
|
---|
60 | /* VFS does not provide means to express constraints on the new
|
---|
61 | * file descriptor so the third argument is ignored. */
|
---|
62 |
|
---|
63 | /* Retrieve node triplet corresponding to the file descriptor. */
|
---|
64 | /* Empty statement after label. */;
|
---|
65 | fdi_node_t node;
|
---|
66 | rc = fd_node(fd, &node);
|
---|
67 | if (rc != EOK) {
|
---|
68 | errno = -rc;
|
---|
69 | return -1;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /* Reopen the node so the fresh file descriptor is generated. */
|
---|
73 | int newfd = open_node(&node, 0);
|
---|
74 | if (newfd < 0) {
|
---|
75 | errno = -newfd;
|
---|
76 | return -1;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* Associate the newly generated descriptor to the file description
|
---|
80 | * of the old file descriptor. Just reopened node will be automatically
|
---|
81 | * closed. */
|
---|
82 | rc = dup2(fd, newfd);
|
---|
83 | if (rc != EOK) {
|
---|
84 | errno = -rc;
|
---|
85 | return -1;
|
---|
86 | }
|
---|
87 |
|
---|
88 | return newfd;
|
---|
89 | case F_GETFD:
|
---|
90 | /* FD_CLOEXEC is not supported. There are no other flags. */
|
---|
91 | return 0;
|
---|
92 | case F_SETFD:
|
---|
93 | /* FD_CLOEXEC is not supported. Ignore arguments and report success. */
|
---|
94 | return 0;
|
---|
95 | case F_GETFL:
|
---|
96 | /* File status flags (i.e. O_APPEND) are currently private to the
|
---|
97 | * VFS server so it cannot be easily retrieved. */
|
---|
98 | flags = 0;
|
---|
99 | /* File access flags are currently not supported for file descriptors.
|
---|
100 | * Provide full access. */
|
---|
101 | flags |= O_RDWR;
|
---|
102 | return flags;
|
---|
103 | case F_SETFL:
|
---|
104 | /* File access flags are currently not supported for file descriptors.
|
---|
105 | * Ignore arguments and report success. */
|
---|
106 | return 0;
|
---|
107 | case F_GETOWN:
|
---|
108 | case F_SETOWN:
|
---|
109 | case F_GETLK:
|
---|
110 | case F_SETLK:
|
---|
111 | case F_SETLKW:
|
---|
112 | /* Signals (SIGURG) and file locks are not supported. */
|
---|
113 | errno = ENOTSUP;
|
---|
114 | return -1;
|
---|
115 | default:
|
---|
116 | /* Unknown command */
|
---|
117 | errno = EINVAL;
|
---|
118 | return -1;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | /** @}
|
---|
123 | */
|
---|