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