xref: /netbsd-src/sys/compat/linux32/common/linux32_stat.c (revision de2138164cd16145ee5fd4d0aef1e8f952c1a9fb)
1 /*	$NetBSD: linux32_stat.c,v 1.2 2007/02/09 21:55:21 ad Exp $ */
2 
3 /*-
4  * Copyright (c) 2006 Emmanuel Dreyfus, 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  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. 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  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Emmanuel Dreyfus
17  * 4. The name of the author may not be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 
36 __KERNEL_RCSID(0, "$NetBSD: linux32_stat.c,v 1.2 2007/02/09 21:55:21 ad Exp $");
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/fstypes.h>
41 #include <sys/signal.h>
42 #include <sys/dirent.h>
43 #include <sys/kernel.h>
44 #include <sys/fcntl.h>
45 #include <sys/select.h>
46 #include <sys/proc.h>
47 #include <sys/ucred.h>
48 #include <sys/swap.h>
49 
50 #include <machine/types.h>
51 
52 #include <sys/syscallargs.h>
53 
54 #include <compat/netbsd32/netbsd32.h>
55 #include <compat/netbsd32/netbsd32_conv.h>
56 #include <compat/netbsd32/netbsd32_syscallargs.h>
57 
58 #include <compat/linux/common/linux_types.h>
59 #include <compat/linux/common/linux_signal.h>
60 #include <compat/linux/common/linux_machdep.h>
61 #include <compat/linux/common/linux_misc.h>
62 #include <compat/linux/common/linux_oldolduname.h>
63 #include <compat/linux/linux_syscallargs.h>
64 
65 #include <compat/linux32/common/linux32_types.h>
66 #include <compat/linux32/common/linux32_signal.h>
67 #include <compat/linux32/common/linux32_machdep.h>
68 #include <compat/linux32/common/linux32_sysctl.h>
69 #include <compat/linux32/common/linux32_socketcall.h>
70 #include <compat/linux32/linux32_syscallargs.h>
71 
72 static inline void linux32_from_stat(struct stat *, struct linux32_stat64 *);
73 
74 #define linux_fakedev(x,y) (x)
75 static inline void
76 linux32_from_stat(st, st32)
77 	struct stat *st;
78 	struct linux32_stat64 *st32;
79 {
80 	bzero(st32, sizeof(*st32));
81 	st32->lst_dev = linux_fakedev(st->st_dev, 0);
82 	st32->__lst_ino = st->st_ino;
83 	st32->lst_mode = st->st_mode;
84 	if (st->st_nlink >= (1 << 15))
85 		st32->lst_nlink = (1 << 15) - 1;
86 	else
87 		st32->lst_nlink = st->st_nlink;
88 	st32->lst_uid = st->st_uid;
89 	st32->lst_gid = st->st_gid;
90 	st32->lst_rdev = linux_fakedev(st->st_rdev, 0);
91 	st32->lst_size = st->st_size;
92 	st32->lst_blksize = st->st_blksize;
93 	st32->lst_blocks = st->st_blocks;
94 	st32->lst_atime = st->st_atime;
95 	st32->lst_mtime = st->st_mtime;
96 	st32->lst_ctime = st->st_ctime;
97 #ifdef LINUX32_STAT64_HAS_NSEC
98 	st32->lst_atime_nsec = st->st_atimensec;
99 	st32->lst_mtime_nsec = st->st_mtimensec;
100 	st32->lst_ctime_nsec = st->st_ctimensec;
101 #endif
102 #ifdef LINUX32_STAT64_HAS_BROKEN_ST_INO
103 	st32->lst_ino = st->st_ino;
104 #endif
105 
106 	return;
107 }
108 
109 int
110 linux32_sys_stat64(l, v, retval)
111 	struct lwp *l;
112 	void *v;
113 	register_t *retval;
114 {
115 	struct linux32_sys_stat64_args /* {
116 	        syscallarg(netbsd32_charp) path;
117 	        syscallarg(linux32_statp) sp;
118 	} */ *uap = v;
119 	struct sys___stat30_args ua;
120 	caddr_t sg = stackgap_init(l->l_proc, 0);
121 	int error;
122 	struct stat st;
123 	struct linux32_stat64 st32;
124 	struct stat *stp;
125 	struct linux32_stat64 *st32p;
126 
127 	NETBSD32TOP_UAP(path, const char);
128 
129 	CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
130 
131 	stp = stackgap_alloc(l->l_proc, &sg, sizeof(*stp));
132 	SCARG(&ua, ub) = stp;
133 
134 	if ((error = sys___stat30(l, &ua, retval)) != 0)
135 		return error;
136 
137 	if ((error = copyin(stp, &st, sizeof(st))) != 0)
138 		return error;
139 
140 	linux32_from_stat(&st, &st32);
141 
142 	st32p = (struct linux32_stat64 *)(long)SCARG(uap, sp);
143 
144 	if ((error = copyout(&st32, st32p, sizeof(*st32p))) != 0)
145 		return error;
146 
147 	return 0;
148 }
149 
150 int
151 linux32_sys_lstat64(l, v, retval)
152 	struct lwp *l;
153 	void *v;
154 	register_t *retval;
155 {
156 	struct linux32_sys_lstat64_args /* {
157 	        syscallarg(netbsd32_charp) path;
158 	        syscallarg(linux32_stat64p) sp;
159 	} */ *uap = v;
160 	struct sys___lstat30_args ua;
161 	caddr_t sg = stackgap_init(l->l_proc, 0);
162 	int error;
163 	struct stat st;
164 	struct linux32_stat64 st32;
165 	struct stat *stp;
166 	struct linux32_stat64 *st32p;
167 
168 	NETBSD32TOP_UAP(path, const char);
169 
170 	CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
171 
172 	stp = stackgap_alloc(l->l_proc, &sg, sizeof(*stp));
173 	SCARG(&ua, ub) = stp;
174 
175 	if ((error = sys___lstat30(l, &ua, retval)) != 0)
176 		return error;
177 
178 	if ((error = copyin(stp, &st, sizeof(st))) != 0)
179 		return error;
180 
181 	linux32_from_stat(&st, &st32);
182 
183 	st32p = (struct linux32_stat64 *)(long)SCARG(uap, sp);
184 
185 	if ((error = copyout(&st32, st32p, sizeof(*st32p))) != 0)
186 		return error;
187 
188 	return 0;
189 }
190 
191 int
192 linux32_sys_fstat64(l, v, retval)
193 	struct lwp *l;
194 	void *v;
195 	register_t *retval;
196 {
197 	struct linux32_sys_fstat64_args /* {
198 	        syscallarg(int) fd;
199 	        syscallarg(linux32_stat64p) sp;
200 	} */ *uap = v;
201 	struct sys___fstat30_args ua;
202 	caddr_t sg = stackgap_init(l->l_proc, 0);
203 	int error;
204 	struct stat st;
205 	struct linux32_stat64 st32;
206 	struct stat *stp;
207 	struct linux32_stat64 *st32p;
208 
209 	NETBSD32TO64_UAP(fd);
210 
211 	stp = stackgap_alloc(l->l_proc, &sg, sizeof(*stp));
212 	SCARG(&ua, sb) = stp;
213 
214 	if ((error = sys___fstat30(l, &ua, retval)) != 0)
215 		return error;
216 
217 	if ((error = copyin(stp, &st, sizeof(st))) != 0)
218 		return error;
219 
220 	linux32_from_stat(&st, &st32);
221 
222 	st32p = (struct linux32_stat64 *)(long)SCARG(uap, sp);
223 
224 	if ((error = copyout(&st32, st32p, sizeof(*st32p))) != 0)
225 		return error;
226 
227 	return 0;
228 }
229