linux_misc_notalpha.c revision 1.101 1 /* $NetBSD: linux_misc_notalpha.c,v 1.101 2008/04/21 00:13:46 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
9 * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: linux_misc_notalpha.c,v 1.101 2008/04/21 00:13:46 ad Exp $");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/mman.h>
47 #include <sys/mount.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/namei.h>
51 #include <sys/proc.h>
52 #include <sys/prot.h>
53 #include <sys/ptrace.h>
54 #include <sys/resource.h>
55 #include <sys/resourcevar.h>
56 #include <sys/time.h>
57 #include <sys/vfs_syscalls.h>
58 #include <sys/wait.h>
59 #include <sys/kauth.h>
60
61 #include <sys/syscallargs.h>
62
63 #include <compat/linux/common/linux_types.h>
64 #include <compat/linux/common/linux_fcntl.h>
65 #include <compat/linux/common/linux_misc.h>
66 #include <compat/linux/common/linux_mmap.h>
67 #include <compat/linux/common/linux_signal.h>
68 #include <compat/linux/common/linux_util.h>
69 #include <compat/linux/common/linux_ipc.h>
70 #include <compat/linux/common/linux_sem.h>
71
72 #include <compat/linux/linux_syscallargs.h>
73
74 /*
75 * This file contains routines which are used
76 * on every linux architechture except the Alpha.
77 */
78
79 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
80 /* Not used on: alpha */
81
82 #ifdef DEBUG_LINUX
83 #define DPRINTF(a) uprintf a
84 #else
85 #define DPRINTF(a)
86 #endif
87
88 #ifndef COMPAT_LINUX32
89 #if !defined(__m68k__) && !defined(__amd64__)
90 static void bsd_to_linux_statfs64(const struct statvfs *,
91 struct linux_statfs64 *);
92 #endif
93
94 /*
95 * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
96 * Fiddle with the timers to make it work.
97 *
98 * XXX This shouldn't be dicking about with the ptimer stuff directly.
99 */
100 int
101 linux_sys_alarm(struct lwp *l, const struct linux_sys_alarm_args *uap, register_t *retval)
102 {
103 /* {
104 syscallarg(unsigned int) secs;
105 } */
106 struct proc *p = l->l_proc;
107 struct timeval now;
108 struct itimerval *itp, it;
109 struct ptimer *ptp, *spare;
110 extern kmutex_t timer_lock;
111 struct ptimers *pts;
112
113 if ((pts = p->p_timers) == NULL)
114 pts = timers_alloc(p);
115 spare = NULL;
116
117 retry:
118 mutex_spin_enter(&timer_lock);
119 if (pts && pts->pts_timers[ITIMER_REAL])
120 itp = &pts->pts_timers[ITIMER_REAL]->pt_time;
121 else
122 itp = NULL;
123 /*
124 * Clear any pending timer alarms.
125 */
126 if (itp) {
127 callout_stop(&pts->pts_timers[ITIMER_REAL]->pt_ch);
128 timerclear(&itp->it_interval);
129 getmicrotime(&now);
130 if (timerisset(&itp->it_value) &&
131 timercmp(&itp->it_value, &now, >))
132 timersub(&itp->it_value, &now, &itp->it_value);
133 /*
134 * Return how many seconds were left (rounded up)
135 */
136 retval[0] = itp->it_value.tv_sec;
137 if (itp->it_value.tv_usec)
138 retval[0]++;
139 } else {
140 retval[0] = 0;
141 }
142
143 /*
144 * alarm(0) just resets the timer.
145 */
146 if (SCARG(uap, secs) == 0) {
147 if (itp)
148 timerclear(&itp->it_value);
149 mutex_spin_exit(&timer_lock);
150 return 0;
151 }
152
153 /*
154 * Check the new alarm time for sanity, and set it.
155 */
156 timerclear(&it.it_interval);
157 it.it_value.tv_sec = SCARG(uap, secs);
158 it.it_value.tv_usec = 0;
159 if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
160 mutex_spin_exit(&timer_lock);
161 return (EINVAL);
162 }
163
164 ptp = pts->pts_timers[ITIMER_REAL];
165 if (ptp == NULL) {
166 if (spare == NULL) {
167 mutex_spin_exit(&timer_lock);
168 spare = pool_get(&ptimer_pool, PR_WAITOK);
169 goto retry;
170 }
171 ptp = spare;
172 spare = NULL;
173 ptp->pt_ev.sigev_notify = SIGEV_SIGNAL;
174 ptp->pt_ev.sigev_signo = SIGALRM;
175 ptp->pt_overruns = 0;
176 ptp->pt_proc = p;
177 ptp->pt_type = CLOCK_REALTIME;
178 ptp->pt_entry = CLOCK_REALTIME;
179 ptp->pt_active = 0;
180 ptp->pt_queued = 0;
181 callout_init(&ptp->pt_ch, CALLOUT_MPSAFE);
182 pts->pts_timers[ITIMER_REAL] = ptp;
183 }
184
185 if (timerisset(&it.it_value)) {
186 /*
187 * Don't need to check hzto() return value, here.
188 * callout_reset() does it for us.
189 */
190 getmicrotime(&now);
191 timeradd(&it.it_value, &now, &it.it_value);
192 callout_reset(&ptp->pt_ch, hzto(&it.it_value),
193 realtimerexpire, ptp);
194 }
195 ptp->pt_time = it;
196 mutex_spin_exit(&timer_lock);
197
198 return 0;
199 }
200 #endif /* !COMPAT_LINUX32 */
201
202 #if !defined(__amd64__)
203 int
204 linux_sys_nice(struct lwp *l, const struct linux_sys_nice_args *uap, register_t *retval)
205 {
206 /* {
207 syscallarg(int) incr;
208 } */
209 struct sys_setpriority_args bsa;
210
211 SCARG(&bsa, which) = PRIO_PROCESS;
212 SCARG(&bsa, who) = 0;
213 SCARG(&bsa, prio) = SCARG(uap, incr);
214 return sys_setpriority(l, &bsa, retval);
215 }
216 #endif /* !__amd64__ */
217
218 #ifndef COMPAT_LINUX32
219 #ifndef __amd64__
220 /*
221 * The old Linux readdir was only able to read one entry at a time,
222 * even though it had a 'count' argument. In fact, the emulation
223 * of the old call was better than the original, because it did handle
224 * the count arg properly. Don't bother with it anymore now, and use
225 * it to distinguish between old and new. The difference is that the
226 * newer one actually does multiple entries, and the reclen field
227 * really is the reclen, not the namelength.
228 */
229 int
230 linux_sys_readdir(struct lwp *l, const struct linux_sys_readdir_args *uap, register_t *retval)
231 {
232 /* {
233 syscallarg(int) fd;
234 syscallarg(struct linux_dirent *) dent;
235 syscallarg(unsigned int) count;
236 } */
237 int error;
238 struct linux_sys_getdents_args da;
239
240 SCARG(&da, fd) = SCARG(uap, fd);
241 SCARG(&da, dent) = SCARG(uap, dent);
242 SCARG(&da, count) = 1;
243
244 error = linux_sys_getdents(l, &da, retval);
245 if (error == 0 && *retval > 1)
246 *retval = 1;
247
248 return error;
249 }
250 #endif /* !amd64 */
251
252 /*
253 * I wonder why Linux has gettimeofday() _and_ time().. Still, we
254 * need to deal with it.
255 */
256 int
257 linux_sys_time(struct lwp *l, const struct linux_sys_time_args *uap, register_t *retval)
258 {
259 /* {
260 syscallarg(linux_time_t) *t;
261 } */
262 struct timeval atv;
263 linux_time_t tt;
264 int error;
265
266 microtime(&atv);
267
268 tt = atv.tv_sec;
269 if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
270 return error;
271
272 retval[0] = tt;
273 return 0;
274 }
275
276 /*
277 * utime(). Do conversion to things that utimes() understands,
278 * and pass it on.
279 */
280 int
281 linux_sys_utime(struct lwp *l, const struct linux_sys_utime_args *uap, register_t *retval)
282 {
283 /* {
284 syscallarg(const char *) path;
285 syscallarg(struct linux_utimbuf *)times;
286 } */
287 int error;
288 struct timeval tv[2], *tvp;
289 struct linux_utimbuf lut;
290
291 if (SCARG(uap, times) != NULL) {
292 if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
293 return error;
294 tv[0].tv_usec = tv[1].tv_usec = 0;
295 tv[0].tv_sec = lut.l_actime;
296 tv[1].tv_sec = lut.l_modtime;
297 tvp = tv;
298 } else
299 tvp = NULL;
300
301 return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
302 tvp, UIO_SYSSPACE);
303 }
304
305 #ifndef __amd64__
306 /*
307 * waitpid(2). Just forward on to linux_sys_wait4 with a NULL rusage.
308 */
309 int
310 linux_sys_waitpid(struct lwp *l, const struct linux_sys_waitpid_args *uap, register_t *retval)
311 {
312 /* {
313 syscallarg(int) pid;
314 syscallarg(int *) status;
315 syscallarg(int) options;
316 } */
317 struct linux_sys_wait4_args linux_w4a;
318
319 SCARG(&linux_w4a, pid) = SCARG(uap, pid);
320 SCARG(&linux_w4a, status) = SCARG(uap, status);
321 SCARG(&linux_w4a, options) = SCARG(uap, options);
322 SCARG(&linux_w4a, rusage) = NULL;
323
324 return linux_sys_wait4(l, &linux_w4a, retval);
325 }
326 #endif /* !amd64 */
327
328 int
329 linux_sys_setresgid(struct lwp *l, const struct linux_sys_setresgid_args *uap, register_t *retval)
330 {
331 /* {
332 syscallarg(gid_t) rgid;
333 syscallarg(gid_t) egid;
334 syscallarg(gid_t) sgid;
335 } */
336
337 /*
338 * Note: These checks are a little different than the NetBSD
339 * setregid(2) call performs. This precisely follows the
340 * behavior of the Linux kernel.
341 */
342 return do_setresgid(l, SCARG(uap,rgid), SCARG(uap, egid),
343 SCARG(uap, sgid),
344 ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
345 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
346 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
347 }
348
349 int
350 linux_sys_getresgid(struct lwp *l, const struct linux_sys_getresgid_args *uap, register_t *retval)
351 {
352 /* {
353 syscallarg(gid_t *) rgid;
354 syscallarg(gid_t *) egid;
355 syscallarg(gid_t *) sgid;
356 } */
357 kauth_cred_t pc = l->l_cred;
358 int error;
359 gid_t gid;
360
361 /*
362 * Linux copies these values out to userspace like so:
363 *
364 * 1. Copy out rgid.
365 * 2. If that succeeds, copy out egid.
366 * 3. If both of those succeed, copy out sgid.
367 */
368 gid = kauth_cred_getgid(pc);
369 if ((error = copyout(&gid, SCARG(uap, rgid), sizeof(gid_t))) != 0)
370 return (error);
371
372 gid = kauth_cred_getegid(pc);
373 if ((error = copyout(&gid, SCARG(uap, egid), sizeof(gid_t))) != 0)
374 return (error);
375
376 gid = kauth_cred_getsvgid(pc);
377
378 return (copyout(&gid, SCARG(uap, sgid), sizeof(gid_t)));
379 }
380
381 #ifndef __amd64__
382 /*
383 * I wonder why Linux has settimeofday() _and_ stime().. Still, we
384 * need to deal with it.
385 */
386 int
387 linux_sys_stime(struct lwp *l, const struct linux_sys_stime_args *uap, register_t *retval)
388 {
389 /* {
390 syscallarg(linux_time_t) *t;
391 } */
392 struct timespec ats;
393 linux_time_t tt;
394 int error;
395
396 if ((error = copyin(&tt, SCARG(uap, t), sizeof tt)) != 0)
397 return error;
398
399 ats.tv_sec = tt;
400 ats.tv_nsec = 0;
401
402 if ((error = settime(l->l_proc, &ats)))
403 return (error);
404
405 return 0;
406 }
407 #endif /* !amd64 */
408
409 #if !defined(__m68k__) && !defined(__amd64__)
410 /*
411 * Convert NetBSD statvfs structure to Linux statfs64 structure.
412 * See comments in bsd_to_linux_statfs() for further background.
413 * We can safely pass correct bsize and frsize here, since Linux glibc
414 * statvfs() doesn't use statfs64().
415 */
416 static void
417 bsd_to_linux_statfs64(const struct statvfs *bsp, struct linux_statfs64 *lsp)
418 {
419 int i, div;
420
421 for (i = 0; i < linux_fstypes_cnt; i++) {
422 if (strcmp(bsp->f_fstypename, linux_fstypes[i].bsd) == 0) {
423 lsp->l_ftype = linux_fstypes[i].linux;
424 break;
425 }
426 }
427
428 if (i == linux_fstypes_cnt) {
429 DPRINTF(("unhandled fstype in linux emulation: %s\n",
430 bsp->f_fstypename));
431 lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC;
432 }
433
434 div = bsp->f_frsize ? (bsp->f_bsize / bsp->f_frsize) : 1;
435 if (div == 0)
436 div = 1;
437 lsp->l_fbsize = bsp->f_bsize;
438 lsp->l_ffrsize = bsp->f_frsize;
439 lsp->l_fblocks = bsp->f_blocks / div;
440 lsp->l_fbfree = bsp->f_bfree / div;
441 lsp->l_fbavail = bsp->f_bavail / div;
442 lsp->l_ffiles = bsp->f_files;
443 lsp->l_fffree = bsp->f_ffree / div;
444 /* Linux sets the fsid to 0..., we don't */
445 lsp->l_ffsid.val[0] = bsp->f_fsidx.__fsid_val[0];
446 lsp->l_ffsid.val[1] = bsp->f_fsidx.__fsid_val[1];
447 lsp->l_fnamelen = bsp->f_namemax;
448 (void)memset(lsp->l_fspare, 0, sizeof(lsp->l_fspare));
449 }
450
451 /*
452 * Implement the fs stat functions. Straightforward.
453 */
454 int
455 linux_sys_statfs64(struct lwp *l, const struct linux_sys_statfs64_args *uap, register_t *retval)
456 {
457 /* {
458 syscallarg(const char *) path;
459 syscallarg(size_t) sz;
460 syscallarg(struct linux_statfs64 *) sp;
461 } */
462 struct statvfs *sb;
463 struct linux_statfs64 ltmp;
464 int error;
465
466 if (SCARG(uap, sz) != sizeof ltmp)
467 return (EINVAL);
468
469 sb = STATVFSBUF_GET();
470 error = do_sys_pstatvfs(l, SCARG(uap, path), ST_WAIT, sb);
471 if (error == 0) {
472 bsd_to_linux_statfs64(sb, <mp);
473 error = copyout(<mp, SCARG(uap, sp), sizeof ltmp);
474 }
475 STATVFSBUF_PUT(sb);
476 return error;
477 }
478
479 int
480 linux_sys_fstatfs64(struct lwp *l, const struct linux_sys_fstatfs64_args *uap, register_t *retval)
481 {
482 /* {
483 syscallarg(int) fd;
484 syscallarg(size_t) sz;
485 syscallarg(struct linux_statfs64 *) sp;
486 } */
487 struct statvfs *sb;
488 struct linux_statfs64 ltmp;
489 int error;
490
491 if (SCARG(uap, sz) != sizeof ltmp)
492 return (EINVAL);
493
494 sb = STATVFSBUF_GET();
495 error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
496 if (error == 0) {
497 bsd_to_linux_statfs64(sb, <mp);
498 error = copyout(<mp, SCARG(uap, sp), sizeof ltmp);
499 }
500 STATVFSBUF_PUT(sb);
501 return error;
502 }
503 #endif /* !__m68k__ && !__amd64__ */
504 #endif /* !COMPAT_LINUX32 */
505