1 /* $NetBSD: linux_misc_notalpha.c,v 1.105 2008/12/29 22:21:49 njoly Exp $ */
4 * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: linux_misc_notalpha.c,v 1.105 2008/12/29 22:21:49 njoly Exp $");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/mount.h>
41 #include <sys/malloc.h>
43 #include <sys/namei.h>
46 #include <sys/ptrace.h>
47 #include <sys/resource.h>
48 #include <sys/resourcevar.h>
50 #include <sys/vfs_syscalls.h>
52 #include <sys/kauth.h>
54 #include <sys/syscallargs.h>
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>
65 #include <compat/linux/linux_syscallargs.h>
68 * This file contains routines which are used
69 * on every linux architechture except the Alpha.
72 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
73 /* Not used on: alpha */
76 #define DPRINTF(a) uprintf a
81 #ifndef COMPAT_LINUX32
82 #if !defined(__m68k__) && !defined(__amd64__)
83 static void bsd_to_linux_statfs64(const struct statvfs
*,
84 struct linux_statfs64
*);
88 * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
89 * Fiddle with the timers to make it work.
91 * XXX This shouldn't be dicking about with the ptimer stuff directly.
94 linux_sys_alarm(struct lwp
*l
, const struct linux_sys_alarm_args
*uap
, register_t
*retval
)
97 syscallarg(unsigned int) secs;
99 struct proc
*p
= l
->l_proc
;
101 struct itimerspec
*itp
, it
;
102 struct ptimer
*ptp
, *spare
;
103 extern kmutex_t timer_lock
;
106 if ((pts
= p
->p_timers
) == NULL
)
107 pts
= timers_alloc(p
);
111 mutex_spin_enter(&timer_lock
);
112 if (pts
&& pts
->pts_timers
[ITIMER_REAL
])
113 itp
= &pts
->pts_timers
[ITIMER_REAL
]->pt_time
;
117 * Clear any pending timer alarms.
120 callout_stop(&pts
->pts_timers
[ITIMER_REAL
]->pt_ch
);
121 timespecclear(&itp
->it_interval
);
123 if (timespecisset(&itp
->it_value
) &&
124 timespeccmp(&itp
->it_value
, &now
, >))
125 timespecsub(&itp
->it_value
, &now
, &itp
->it_value
);
127 * Return how many seconds were left (rounded up)
129 retval
[0] = itp
->it_value
.tv_sec
;
130 if (itp
->it_value
.tv_nsec
)
137 * alarm(0) just resets the timer.
139 if (SCARG(uap
, secs
) == 0) {
141 timespecclear(&itp
->it_value
);
142 mutex_spin_exit(&timer_lock
);
147 * Check the new alarm time for sanity, and set it.
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
);
157 ptp
= pts
->pts_timers
[ITIMER_REAL
];
160 mutex_spin_exit(&timer_lock
);
161 spare
= pool_get(&ptimer_pool
, PR_WAITOK
);
166 ptp
->pt_ev
.sigev_notify
= SIGEV_SIGNAL
;
167 ptp
->pt_ev
.sigev_signo
= SIGALRM
;
168 ptp
->pt_overruns
= 0;
170 ptp
->pt_type
= CLOCK_REALTIME
;
171 ptp
->pt_entry
= CLOCK_REALTIME
;
174 callout_init(&ptp
->pt_ch
, CALLOUT_MPSAFE
);
175 pts
->pts_timers
[ITIMER_REAL
] = ptp
;
178 if (timespecisset(&it
.it_value
)) {
180 * Don't need to check tvhzto() return value, here.
181 * callout_reset() does it for us.
184 timespecadd(&it
.it_value
, &now
, &it
.it_value
);
185 callout_reset(&ptp
->pt_ch
, tshzto(&it
.it_value
),
186 realtimerexpire
, ptp
);
189 mutex_spin_exit(&timer_lock
);
193 #endif /* !COMPAT_LINUX32 */
195 #if !defined(__amd64__)
197 linux_sys_nice(struct lwp
*l
, const struct linux_sys_nice_args
*uap
, register_t
*retval
)
200 syscallarg(int) incr;
202 struct proc
*p
= l
->l_proc
;
203 struct sys_setpriority_args bsa
;
206 SCARG(&bsa
, which
) = PRIO_PROCESS
;
207 SCARG(&bsa
, who
) = 0;
208 SCARG(&bsa
, prio
) = p
->p_nice
- NZERO
+ SCARG(uap
, incr
);
210 error
= sys_setpriority(l
, &bsa
, retval
);
211 return (error
) ? EPERM
: 0;
213 #endif /* !__amd64__ */
215 #ifndef COMPAT_LINUX32
218 * The old Linux readdir was only able to read one entry at a time,
219 * even though it had a 'count' argument. In fact, the emulation
220 * of the old call was better than the original, because it did handle
221 * the count arg properly. Don't bother with it anymore now, and use
222 * it to distinguish between old and new. The difference is that the
223 * newer one actually does multiple entries, and the reclen field
224 * really is the reclen, not the namelength.
227 linux_sys_readdir(struct lwp
*l
, const struct linux_sys_readdir_args
*uap
, register_t
*retval
)
231 syscallarg(struct linux_dirent *) dent;
232 syscallarg(unsigned int) count;
235 struct linux_sys_getdents_args da
;
237 SCARG(&da
, fd
) = SCARG(uap
, fd
);
238 SCARG(&da
, dent
) = SCARG(uap
, dent
);
239 SCARG(&da
, count
) = 1;
241 error
= linux_sys_getdents(l
, &da
, retval
);
242 if (error
== 0 && *retval
> 1)
250 * I wonder why Linux has gettimeofday() _and_ time().. Still, we
251 * need to deal with it.
254 linux_sys_time(struct lwp
*l
, const struct linux_sys_time_args
*uap
, register_t
*retval
)
257 syscallarg(linux_time_t) *t;
266 if (SCARG(uap
, t
) && (error
= copyout(&tt
, SCARG(uap
, t
), sizeof tt
)))
274 * utime(). Do conversion to things that utimes() understands,
278 linux_sys_utime(struct lwp
*l
, const struct linux_sys_utime_args
*uap
, register_t
*retval
)
281 syscallarg(const char *) path;
282 syscallarg(struct linux_utimbuf *)times;
285 struct timeval tv
[2], *tvp
;
286 struct linux_utimbuf lut
;
288 if (SCARG(uap
, times
) != NULL
) {
289 if ((error
= copyin(SCARG(uap
, times
), &lut
, sizeof lut
)))
291 tv
[0].tv_usec
= tv
[1].tv_usec
= 0;
292 tv
[0].tv_sec
= lut
.l_actime
;
293 tv
[1].tv_sec
= lut
.l_modtime
;
298 return do_sys_utimes(l
, NULL
, SCARG(uap
, path
), FOLLOW
,
304 * waitpid(2). Just forward on to linux_sys_wait4 with a NULL rusage.
307 linux_sys_waitpid(struct lwp
*l
, const struct linux_sys_waitpid_args
*uap
, register_t
*retval
)
311 syscallarg(int *) status;
312 syscallarg(int) options;
314 struct linux_sys_wait4_args linux_w4a
;
316 SCARG(&linux_w4a
, pid
) = SCARG(uap
, pid
);
317 SCARG(&linux_w4a
, status
) = SCARG(uap
, status
);
318 SCARG(&linux_w4a
, options
) = SCARG(uap
, options
);
319 SCARG(&linux_w4a
, rusage
) = NULL
;
321 return linux_sys_wait4(l
, &linux_w4a
, retval
);
326 linux_sys_setresgid(struct lwp
*l
, const struct linux_sys_setresgid_args
*uap
, register_t
*retval
)
329 syscallarg(gid_t) rgid;
330 syscallarg(gid_t) egid;
331 syscallarg(gid_t) sgid;
335 * Note: These checks are a little different than the NetBSD
336 * setregid(2) call performs. This precisely follows the
337 * behavior of the Linux kernel.
339 return do_setresgid(l
, SCARG(uap
,rgid
), SCARG(uap
, egid
),
341 ID_R_EQ_R
| ID_R_EQ_E
| ID_R_EQ_S
|
342 ID_E_EQ_R
| ID_E_EQ_E
| ID_E_EQ_S
|
343 ID_S_EQ_R
| ID_S_EQ_E
| ID_S_EQ_S
);
347 linux_sys_getresgid(struct lwp
*l
, const struct linux_sys_getresgid_args
*uap
, register_t
*retval
)
350 syscallarg(gid_t *) rgid;
351 syscallarg(gid_t *) egid;
352 syscallarg(gid_t *) sgid;
354 kauth_cred_t pc
= l
->l_cred
;
359 * Linux copies these values out to userspace like so:
362 * 2. If that succeeds, copy out egid.
363 * 3. If both of those succeed, copy out sgid.
365 gid
= kauth_cred_getgid(pc
);
366 if ((error
= copyout(&gid
, SCARG(uap
, rgid
), sizeof(gid_t
))) != 0)
369 gid
= kauth_cred_getegid(pc
);
370 if ((error
= copyout(&gid
, SCARG(uap
, egid
), sizeof(gid_t
))) != 0)
373 gid
= kauth_cred_getsvgid(pc
);
375 return (copyout(&gid
, SCARG(uap
, sgid
), sizeof(gid_t
)));
380 * I wonder why Linux has settimeofday() _and_ stime().. Still, we
381 * need to deal with it.
384 linux_sys_stime(struct lwp
*l
, const struct linux_sys_stime_args
*uap
, register_t
*retval
)
387 syscallarg(linux_time_t) *t;
393 if ((error
= copyin(SCARG(uap
, t
), &tt
, sizeof tt
)) != 0)
399 if ((error
= settime(l
->l_proc
, &ats
)))
406 #if !defined(__m68k__) && !defined(__amd64__)
408 * Convert NetBSD statvfs structure to Linux statfs64 structure.
409 * See comments in bsd_to_linux_statfs() for further background.
410 * We can safely pass correct bsize and frsize here, since Linux glibc
411 * statvfs() doesn't use statfs64().
414 bsd_to_linux_statfs64(const struct statvfs
*bsp
, struct linux_statfs64
*lsp
)
418 for (i
= 0; i
< linux_fstypes_cnt
; i
++) {
419 if (strcmp(bsp
->f_fstypename
, linux_fstypes
[i
].bsd
) == 0) {
420 lsp
->l_ftype
= linux_fstypes
[i
].linux
;
425 if (i
== linux_fstypes_cnt
) {
426 DPRINTF(("unhandled fstype in linux emulation: %s\n",
428 lsp
->l_ftype
= LINUX_DEFAULT_SUPER_MAGIC
;
431 div
= bsp
->f_frsize
? (bsp
->f_bsize
/ bsp
->f_frsize
) : 1;
434 lsp
->l_fbsize
= bsp
->f_bsize
;
435 lsp
->l_ffrsize
= bsp
->f_frsize
;
436 lsp
->l_fblocks
= bsp
->f_blocks
/ div
;
437 lsp
->l_fbfree
= bsp
->f_bfree
/ div
;
438 lsp
->l_fbavail
= bsp
->f_bavail
/ div
;
439 lsp
->l_ffiles
= bsp
->f_files
;
440 lsp
->l_fffree
= bsp
->f_ffree
/ div
;
441 /* Linux sets the fsid to 0..., we don't */
442 lsp
->l_ffsid
.val
[0] = bsp
->f_fsidx
.__fsid_val
[0];
443 lsp
->l_ffsid
.val
[1] = bsp
->f_fsidx
.__fsid_val
[1];
444 lsp
->l_fnamelen
= bsp
->f_namemax
;
445 (void)memset(lsp
->l_fspare
, 0, sizeof(lsp
->l_fspare
));
449 * Implement the fs stat functions. Straightforward.
452 linux_sys_statfs64(struct lwp
*l
, const struct linux_sys_statfs64_args
*uap
, register_t
*retval
)
455 syscallarg(const char *) path;
456 syscallarg(size_t) sz;
457 syscallarg(struct linux_statfs64 *) sp;
460 struct linux_statfs64 ltmp
;
463 if (SCARG(uap
, sz
) != sizeof ltmp
)
466 sb
= STATVFSBUF_GET();
467 error
= do_sys_pstatvfs(l
, SCARG(uap
, path
), ST_WAIT
, sb
);
469 bsd_to_linux_statfs64(sb
, <mp
);
470 error
= copyout(<mp
, SCARG(uap
, sp
), sizeof ltmp
);
477 linux_sys_fstatfs64(struct lwp
*l
, const struct linux_sys_fstatfs64_args
*uap
, register_t
*retval
)
481 syscallarg(size_t) sz;
482 syscallarg(struct linux_statfs64 *) sp;
485 struct linux_statfs64 ltmp
;
488 if (SCARG(uap
, sz
) != sizeof ltmp
)
491 sb
= STATVFSBUF_GET();
492 error
= do_sys_fstatvfs(l
, SCARG(uap
, fd
), ST_WAIT
, sb
);
494 bsd_to_linux_statfs64(sb
, <mp
);
495 error
= copyout(<mp
, SCARG(uap
, sp
), sizeof ltmp
);
500 #endif /* !__m68k__ && !__amd64__ */
501 #endif /* !COMPAT_LINUX32 */