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