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