]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/cloudabi/cloudabi_fd.c
Merge ACPICA 20150717.
[FreeBSD/FreeBSD.git] / sys / compat / cloudabi / cloudabi_fd.c
1 /*-
2  * Copyright (c) 2015 Nuxi, https://nuxi.nl/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/filedesc.h>
31 #include <sys/proc.h>
32 #include <sys/syscallsubr.h>
33 #include <sys/sysproto.h>
34 #include <sys/unistd.h>
35
36 #include <compat/cloudabi/cloudabi_proto.h>
37
38 int
39 cloudabi_sys_fd_close(struct thread *td, struct cloudabi_sys_fd_close_args *uap)
40 {
41
42         return (kern_close(td, uap->fd));
43 }
44
45 int
46 cloudabi_sys_fd_create1(struct thread *td,
47     struct cloudabi_sys_fd_create1_args *uap)
48 {
49
50         /* Not implemented. */
51         return (ENOSYS);
52 }
53
54 int
55 cloudabi_sys_fd_create2(struct thread *td,
56     struct cloudabi_sys_fd_create2_args *uap)
57 {
58
59         /* Not implemented. */
60         return (ENOSYS);
61 }
62
63 int
64 cloudabi_sys_fd_datasync(struct thread *td,
65     struct cloudabi_sys_fd_datasync_args *uap)
66 {
67         struct fsync_args fsync_args = {
68                 .fd = uap->fd
69         };
70
71         /* Call into fsync(), as FreeBSD lacks fdatasync(). */
72         return (sys_fsync(td, &fsync_args));
73 }
74
75 int
76 cloudabi_sys_fd_dup(struct thread *td, struct cloudabi_sys_fd_dup_args *uap)
77 {
78
79         return (kern_dup(td, FDDUP_NORMAL, 0, uap->from, 0));
80 }
81
82 int
83 cloudabi_sys_fd_replace(struct thread *td,
84     struct cloudabi_sys_fd_replace_args *uap)
85 {
86         int error;
87
88         /*
89          * CloudABI's equivalent to dup2(). CloudABI processes should
90          * not depend on hardcoded file descriptor layouts, but simply
91          * use the file descriptor numbers that are allocated by the
92          * kernel. Duplicating file descriptors to arbitrary numbers
93          * should not be done.
94          *
95          * Invoke kern_dup() with FDDUP_MUSTREPLACE, so that we return
96          * EBADF when duplicating to a nonexistent file descriptor. Also
97          * clear the return value, as this system call yields no return
98          * value.
99          */
100         error = kern_dup(td, FDDUP_MUSTREPLACE, 0, uap->from, uap->to);
101         td->td_retval[0] = 0;
102         return (error);
103 }
104
105 int
106 cloudabi_sys_fd_seek(struct thread *td, struct cloudabi_sys_fd_seek_args *uap)
107 {
108         struct lseek_args lseek_args = {
109                 .fd     = uap->fd,
110                 .offset = uap->offset
111         };
112
113         switch (uap->whence) {
114         case CLOUDABI_WHENCE_CUR:
115                 lseek_args.whence = SEEK_CUR;
116                 break;
117         case CLOUDABI_WHENCE_END:
118                 lseek_args.whence = SEEK_END;
119                 break;
120         case CLOUDABI_WHENCE_SET:
121                 lseek_args.whence = SEEK_SET;
122                 break;
123         default:
124                 return (EINVAL);
125         }
126
127         return (sys_lseek(td, &lseek_args));
128 }
129
130 int
131 cloudabi_sys_fd_stat_get(struct thread *td,
132     struct cloudabi_sys_fd_stat_get_args *uap)
133 {
134
135         /* Not implemented. */
136         return (ENOSYS);
137 }
138
139 int
140 cloudabi_sys_fd_stat_put(struct thread *td,
141     struct cloudabi_sys_fd_stat_put_args *uap)
142 {
143
144         /* Not implemented. */
145         return (ENOSYS);
146 }
147
148 int
149 cloudabi_sys_fd_sync(struct thread *td, struct cloudabi_sys_fd_sync_args *uap)
150 {
151         struct fsync_args fsync_args = {
152                 .fd = uap->fd
153         };
154
155         return (sys_fsync(td, &fsync_args));
156 }