linux_misc_notalpha.c revision 1.60.2.2 1 1.60.2.2 nathanw /* $NetBSD: linux_misc_notalpha.c,v 1.60.2.2 2001/11/14 19:13:11 nathanw Exp $ */
2 1.47 erh
3 1.47 erh /*-
4 1.49 fvdl * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5 1.47 erh * All rights reserved.
6 1.47 erh *
7 1.47 erh * This code is derived from software contributed to The NetBSD Foundation
8 1.51 thorpej * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
9 1.51 thorpej * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
10 1.47 erh *
11 1.47 erh * Redistribution and use in source and binary forms, with or without
12 1.47 erh * modification, are permitted provided that the following conditions
13 1.47 erh * are met:
14 1.47 erh * 1. Redistributions of source code must retain the above copyright
15 1.47 erh * notice, this list of conditions and the following disclaimer.
16 1.47 erh * 2. Redistributions in binary form must reproduce the above copyright
17 1.47 erh * notice, this list of conditions and the following disclaimer in the
18 1.47 erh * documentation and/or other materials provided with the distribution.
19 1.47 erh * 3. All advertising materials mentioning features or use of this software
20 1.47 erh * must display the following acknowledgement:
21 1.47 erh * This product includes software developed by the NetBSD
22 1.47 erh * Foundation, Inc. and its contributors.
23 1.47 erh * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.47 erh * contributors may be used to endorse or promote products derived
25 1.47 erh * from this software without specific prior written permission.
26 1.47 erh *
27 1.47 erh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.47 erh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.47 erh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.47 erh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.47 erh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.47 erh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.47 erh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.47 erh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.47 erh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.47 erh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.47 erh * POSSIBILITY OF SUCH DAMAGE.
38 1.1 fvdl */
39 1.60.2.2 nathanw
40 1.60.2.2 nathanw #include <sys/cdefs.h>
41 1.60.2.2 nathanw __KERNEL_RCSID(0, "$NetBSD: linux_misc_notalpha.c,v 1.60.2.2 2001/11/14 19:13:11 nathanw Exp $");
42 1.1 fvdl
43 1.1 fvdl #include <sys/param.h>
44 1.1 fvdl #include <sys/systm.h>
45 1.1 fvdl #include <sys/kernel.h>
46 1.47 erh #include <sys/mman.h>
47 1.47 erh #include <sys/mount.h>
48 1.1 fvdl #include <sys/malloc.h>
49 1.1 fvdl #include <sys/mbuf.h>
50 1.47 erh #include <sys/namei.h>
51 1.57 itohy #include <sys/proc.h>
52 1.1 fvdl #include <sys/ptrace.h>
53 1.1 fvdl #include <sys/resource.h>
54 1.1 fvdl #include <sys/resourcevar.h>
55 1.1 fvdl #include <sys/wait.h>
56 1.1 fvdl
57 1.1 fvdl #include <sys/syscallargs.h>
58 1.1 fvdl
59 1.48 christos #include <compat/linux/common/linux_types.h>
60 1.48 christos #include <compat/linux/common/linux_fcntl.h>
61 1.48 christos #include <compat/linux/common/linux_misc.h>
62 1.48 christos #include <compat/linux/common/linux_mmap.h>
63 1.48 christos #include <compat/linux/common/linux_signal.h>
64 1.48 christos #include <compat/linux/common/linux_util.h>
65 1.48 christos
66 1.1 fvdl #include <compat/linux/linux_syscallargs.h>
67 1.26 christos
68 1.1 fvdl /*
69 1.47 erh * This file contains routines which are used
70 1.47 erh * on every linux architechture except the Alpha.
71 1.1 fvdl */
72 1.21 mycroft
73 1.47 erh /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
74 1.47 erh /* Not used on: alpha */
75 1.1 fvdl
76 1.1 fvdl /*
77 1.47 erh * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
78 1.47 erh * Fiddle with the timers to make it work.
79 1.1 fvdl */
80 1.1 fvdl int
81 1.60.2.1 nathanw linux_sys_alarm(l, v, retval)
82 1.60.2.1 nathanw struct lwp *l;
83 1.20 thorpej void *v;
84 1.20 thorpej register_t *retval;
85 1.20 thorpej {
86 1.47 erh struct linux_sys_alarm_args /* {
87 1.47 erh syscallarg(unsigned int) secs;
88 1.20 thorpej } */ *uap = v;
89 1.60.2.1 nathanw struct proc *p = l->l_proc;
90 1.47 erh int s;
91 1.47 erh struct itimerval *itp, it;
92 1.1 fvdl
93 1.47 erh itp = &p->p_realtimer;
94 1.47 erh s = splclock();
95 1.47 erh /*
96 1.47 erh * Clear any pending timer alarms.
97 1.47 erh */
98 1.53 thorpej callout_stop(&p->p_realit_ch);
99 1.47 erh timerclear(&itp->it_interval);
100 1.47 erh if (timerisset(&itp->it_value) &&
101 1.47 erh timercmp(&itp->it_value, &time, >))
102 1.47 erh timersub(&itp->it_value, &time, &itp->it_value);
103 1.47 erh /*
104 1.47 erh * Return how many seconds were left (rounded up)
105 1.47 erh */
106 1.47 erh retval[0] = itp->it_value.tv_sec;
107 1.47 erh if (itp->it_value.tv_usec)
108 1.47 erh retval[0]++;
109 1.1 fvdl
110 1.47 erh /*
111 1.47 erh * alarm(0) just resets the timer.
112 1.47 erh */
113 1.47 erh if (SCARG(uap, secs) == 0) {
114 1.47 erh timerclear(&itp->it_value);
115 1.47 erh splx(s);
116 1.47 erh return 0;
117 1.47 erh }
118 1.1 fvdl
119 1.47 erh /*
120 1.47 erh * Check the new alarm time for sanity, and set it.
121 1.47 erh */
122 1.47 erh timerclear(&it.it_interval);
123 1.47 erh it.it_value.tv_sec = SCARG(uap, secs);
124 1.47 erh it.it_value.tv_usec = 0;
125 1.47 erh if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
126 1.47 erh splx(s);
127 1.47 erh return (EINVAL);
128 1.16 fvdl }
129 1.1 fvdl
130 1.47 erh if (timerisset(&it.it_value)) {
131 1.56 thorpej /*
132 1.56 thorpej * Don't need to check hzto() return value, here.
133 1.56 thorpej * callout_reset() does it for us.
134 1.56 thorpej */
135 1.47 erh timeradd(&it.it_value, &time, &it.it_value);
136 1.53 thorpej callout_reset(&p->p_realit_ch, hzto(&it.it_value),
137 1.53 thorpej realitexpire, p);
138 1.16 fvdl }
139 1.47 erh p->p_realtimer = it;
140 1.47 erh splx(s);
141 1.1 fvdl
142 1.16 fvdl return 0;
143 1.1 fvdl }
144 1.1 fvdl
145 1.1 fvdl int
146 1.60.2.1 nathanw linux_sys_nice(l, v, retval)
147 1.60.2.1 nathanw struct lwp *l;
148 1.20 thorpej void *v;
149 1.20 thorpej register_t *retval;
150 1.20 thorpej {
151 1.47 erh struct linux_sys_nice_args /* {
152 1.47 erh syscallarg(int) incr;
153 1.20 thorpej } */ *uap = v;
154 1.47 erh struct sys_setpriority_args bsa;
155 1.20 thorpej
156 1.47 erh SCARG(&bsa, which) = PRIO_PROCESS;
157 1.47 erh SCARG(&bsa, who) = 0;
158 1.47 erh SCARG(&bsa, prio) = SCARG(uap, incr);
159 1.60.2.1 nathanw return sys_setpriority(l, &bsa, retval);
160 1.1 fvdl }
161 1.1 fvdl
162 1.1 fvdl /*
163 1.47 erh * The old Linux readdir was only able to read one entry at a time,
164 1.47 erh * even though it had a 'count' argument. In fact, the emulation
165 1.47 erh * of the old call was better than the original, because it did handle
166 1.47 erh * the count arg properly. Don't bother with it anymore now, and use
167 1.47 erh * it to distinguish between old and new. The difference is that the
168 1.47 erh * newer one actually does multiple entries, and the reclen field
169 1.47 erh * really is the reclen, not the namelength.
170 1.1 fvdl */
171 1.1 fvdl int
172 1.60.2.1 nathanw linux_sys_readdir(l, v, retval)
173 1.60.2.1 nathanw struct lwp *l;
174 1.20 thorpej void *v;
175 1.20 thorpej register_t *retval;
176 1.20 thorpej {
177 1.47 erh struct linux_sys_readdir_args /* {
178 1.47 erh syscallarg(int) fd;
179 1.47 erh syscallarg(struct linux_dirent *) dent;
180 1.47 erh syscallarg(unsigned int) count;
181 1.20 thorpej } */ *uap = v;
182 1.1 fvdl
183 1.47 erh SCARG(uap, count) = 1;
184 1.60.2.1 nathanw return linux_sys_getdents(l, uap, retval);
185 1.1 fvdl }
186 1.1 fvdl
187 1.1 fvdl /*
188 1.1 fvdl * I wonder why Linux has gettimeofday() _and_ time().. Still, we
189 1.1 fvdl * need to deal with it.
190 1.1 fvdl */
191 1.1 fvdl int
192 1.60.2.1 nathanw linux_sys_time(l, v, retval)
193 1.60.2.1 nathanw struct lwp *l;
194 1.20 thorpej void *v;
195 1.20 thorpej register_t *retval;
196 1.20 thorpej {
197 1.21 mycroft struct linux_sys_time_args /* {
198 1.1 fvdl linux_time_t *t;
199 1.20 thorpej } */ *uap = v;
200 1.1 fvdl struct timeval atv;
201 1.1 fvdl linux_time_t tt;
202 1.1 fvdl int error;
203 1.1 fvdl
204 1.1 fvdl microtime(&atv);
205 1.1 fvdl
206 1.1 fvdl tt = atv.tv_sec;
207 1.1 fvdl if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
208 1.1 fvdl return error;
209 1.1 fvdl
210 1.1 fvdl retval[0] = tt;
211 1.1 fvdl return 0;
212 1.1 fvdl }
213 1.1 fvdl
214 1.1 fvdl /*
215 1.47 erh * utime(). Do conversion to things that utimes() understands,
216 1.47 erh * and pass it on.
217 1.1 fvdl */
218 1.1 fvdl int
219 1.60.2.1 nathanw linux_sys_utime(l, v, retval)
220 1.60.2.1 nathanw struct lwp *l;
221 1.20 thorpej void *v;
222 1.20 thorpej register_t *retval;
223 1.20 thorpej {
224 1.47 erh struct linux_sys_utime_args /* {
225 1.50 christos syscallarg(const char *) path;
226 1.47 erh syscallarg(struct linux_utimbuf *)times;
227 1.20 thorpej } */ *uap = v;
228 1.60.2.1 nathanw struct proc *p = l->l_proc;
229 1.2 fvdl caddr_t sg;
230 1.2 fvdl int error;
231 1.47 erh struct sys_utimes_args ua;
232 1.47 erh struct timeval tv[2], *tvp;
233 1.47 erh struct linux_utimbuf lut;
234 1.2 fvdl
235 1.9 christos sg = stackgap_init(p->p_emul);
236 1.58 sommerfe tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
237 1.59 jdolecek CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
238 1.2 fvdl
239 1.47 erh SCARG(&ua, path) = SCARG(uap, path);
240 1.2 fvdl
241 1.47 erh if (SCARG(uap, times) != NULL) {
242 1.47 erh if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
243 1.47 erh return error;
244 1.47 erh tv[0].tv_usec = tv[1].tv_usec = 0;
245 1.47 erh tv[0].tv_sec = lut.l_actime;
246 1.47 erh tv[1].tv_sec = lut.l_modtime;
247 1.47 erh if ((error = copyout(tv, tvp, sizeof tv)))
248 1.47 erh return error;
249 1.47 erh SCARG(&ua, tptr) = tvp;
250 1.47 erh }
251 1.47 erh else
252 1.47 erh SCARG(&ua, tptr) = NULL;
253 1.2 fvdl
254 1.60.2.1 nathanw return sys_utimes(l, &ua, retval);
255 1.1 fvdl }
256 1.1 fvdl
257 1.47 erh /*
258 1.47 erh * waitpid(2). Passed on to the NetBSD call, surrounded by code to
259 1.47 erh * reserve some space for a NetBSD-style wait status, and converting
260 1.47 erh * it to what Linux wants.
261 1.47 erh */
262 1.1 fvdl int
263 1.60.2.1 nathanw linux_sys_waitpid(l, v, retval)
264 1.60.2.1 nathanw struct lwp *l;
265 1.20 thorpej void *v;
266 1.20 thorpej register_t *retval;
267 1.20 thorpej {
268 1.47 erh struct linux_sys_waitpid_args /* {
269 1.47 erh syscallarg(int) pid;
270 1.47 erh syscallarg(int *) status;
271 1.47 erh syscallarg(int) options;
272 1.20 thorpej } */ *uap = v;
273 1.60.2.1 nathanw struct proc *p = l->l_proc;
274 1.47 erh struct sys_wait4_args w4a;
275 1.47 erh int error, *status, tstat;
276 1.2 fvdl caddr_t sg;
277 1.2 fvdl
278 1.47 erh if (SCARG(uap, status) != NULL) {
279 1.47 erh sg = stackgap_init(p->p_emul);
280 1.47 erh status = (int *) stackgap_alloc(&sg, sizeof status);
281 1.47 erh } else
282 1.47 erh status = NULL;
283 1.2 fvdl
284 1.47 erh SCARG(&w4a, pid) = SCARG(uap, pid);
285 1.47 erh SCARG(&w4a, status) = status;
286 1.47 erh SCARG(&w4a, options) = SCARG(uap, options);
287 1.47 erh SCARG(&w4a, rusage) = NULL;
288 1.2 fvdl
289 1.60.2.1 nathanw if ((error = sys_wait4(l, &w4a, retval)))
290 1.2 fvdl return error;
291 1.2 fvdl
292 1.60 jdolecek sigdelset(&p->p_sigctx.ps_siglist, SIGCHLD);
293 1.2 fvdl
294 1.47 erh if (status != NULL) {
295 1.47 erh if ((error = copyin(status, &tstat, sizeof tstat)))
296 1.47 erh return error;
297 1.2 fvdl
298 1.47 erh bsd_to_linux_wstat(&tstat);
299 1.47 erh return copyout(&tstat, SCARG(uap, status), sizeof tstat);
300 1.44 thorpej }
301 1.1 fvdl
302 1.47 erh return 0;
303 1.51 thorpej }
304 1.51 thorpej
305 1.51 thorpej int
306 1.60.2.1 nathanw linux_sys_setresgid(l, v, retval)
307 1.60.2.1 nathanw struct lwp *l;
308 1.51 thorpej void *v;
309 1.51 thorpej register_t *retval;
310 1.51 thorpej {
311 1.51 thorpej struct linux_sys_setresgid_args /* {
312 1.51 thorpej syscallarg(gid_t) rgid;
313 1.51 thorpej syscallarg(gid_t) egid;
314 1.51 thorpej syscallarg(gid_t) sgid;
315 1.51 thorpej } */ *uap = v;
316 1.60.2.1 nathanw struct proc *p = l->l_proc;
317 1.51 thorpej struct pcred *pc = p->p_cred;
318 1.51 thorpej gid_t rgid, egid, sgid;
319 1.51 thorpej int error;
320 1.51 thorpej
321 1.51 thorpej rgid = SCARG(uap, rgid);
322 1.51 thorpej egid = SCARG(uap, egid);
323 1.51 thorpej sgid = SCARG(uap, sgid);
324 1.51 thorpej
325 1.51 thorpej /*
326 1.51 thorpej * Note: These checks are a little different than the NetBSD
327 1.51 thorpej * setregid(2) call performs. This precisely follows the
328 1.51 thorpej * behavior of the Linux kernel.
329 1.51 thorpej */
330 1.51 thorpej if (rgid != (gid_t)-1 &&
331 1.51 thorpej rgid != pc->p_rgid &&
332 1.51 thorpej rgid != pc->pc_ucred->cr_gid &&
333 1.51 thorpej rgid != pc->p_svgid &&
334 1.51 thorpej (error = suser(pc->pc_ucred, &p->p_acflag)))
335 1.51 thorpej return (error);
336 1.51 thorpej
337 1.51 thorpej if (egid != (gid_t)-1 &&
338 1.51 thorpej egid != pc->p_rgid &&
339 1.51 thorpej egid != pc->pc_ucred->cr_gid &&
340 1.51 thorpej egid != pc->p_svgid &&
341 1.51 thorpej (error = suser(pc->pc_ucred, &p->p_acflag)))
342 1.51 thorpej return (error);
343 1.51 thorpej
344 1.51 thorpej if (sgid != (gid_t)-1 &&
345 1.51 thorpej sgid != pc->p_rgid &&
346 1.51 thorpej sgid != pc->pc_ucred->cr_gid &&
347 1.51 thorpej sgid != pc->p_svgid &&
348 1.51 thorpej (error = suser(pc->pc_ucred, &p->p_acflag)))
349 1.51 thorpej return (error);
350 1.51 thorpej
351 1.51 thorpej /*
352 1.51 thorpej * Now assign the real, effective, and saved GIDs.
353 1.51 thorpej * Note that Linux, unlike NetBSD in setregid(2), does not
354 1.51 thorpej * set the saved UID in this call unless the user specifies
355 1.51 thorpej * it.
356 1.51 thorpej */
357 1.51 thorpej if (rgid != (gid_t)-1)
358 1.51 thorpej pc->p_rgid = rgid;
359 1.51 thorpej
360 1.51 thorpej if (egid != (gid_t)-1) {
361 1.51 thorpej pc->pc_ucred = crcopy(pc->pc_ucred);
362 1.51 thorpej pc->pc_ucred->cr_gid = egid;
363 1.51 thorpej }
364 1.51 thorpej
365 1.51 thorpej if (sgid != (gid_t)-1)
366 1.51 thorpej pc->p_svgid = sgid;
367 1.51 thorpej
368 1.51 thorpej if (rgid != (gid_t)-1 && egid != (gid_t)-1 && sgid != (gid_t)-1)
369 1.51 thorpej p->p_flag |= P_SUGID;
370 1.51 thorpej return (0);
371 1.51 thorpej }
372 1.51 thorpej
373 1.51 thorpej int
374 1.60.2.1 nathanw linux_sys_getresgid(l, v, retval)
375 1.60.2.1 nathanw struct lwp *l;
376 1.51 thorpej void *v;
377 1.51 thorpej register_t *retval;
378 1.51 thorpej {
379 1.51 thorpej struct linux_sys_getresgid_args /* {
380 1.51 thorpej syscallarg(gid_t *) rgid;
381 1.51 thorpej syscallarg(gid_t *) egid;
382 1.51 thorpej syscallarg(gid_t *) sgid;
383 1.51 thorpej } */ *uap = v;
384 1.60.2.1 nathanw struct proc *p = l->l_proc;
385 1.51 thorpej struct pcred *pc = p->p_cred;
386 1.51 thorpej int error;
387 1.51 thorpej
388 1.51 thorpej /*
389 1.51 thorpej * Linux copies these values out to userspace like so:
390 1.51 thorpej *
391 1.51 thorpej * 1. Copy out rgid.
392 1.51 thorpej * 2. If that succeeds, copy out egid.
393 1.51 thorpej * 3. If both of those succeed, copy out sgid.
394 1.51 thorpej */
395 1.51 thorpej if ((error = copyout(&pc->p_rgid, SCARG(uap, rgid),
396 1.51 thorpej sizeof(gid_t))) != 0)
397 1.51 thorpej return (error);
398 1.51 thorpej
399 1.51 thorpej if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, egid),
400 1.51 thorpej sizeof(gid_t))) != 0)
401 1.51 thorpej return (error);
402 1.51 thorpej
403 1.51 thorpej return (copyout(&pc->p_svgid, SCARG(uap, sgid), sizeof(gid_t)));
404 1.52 tron }
405 1.52 tron
406 1.52 tron /*
407 1.52 tron * I wonder why Linux has settimeofday() _and_ stime().. Still, we
408 1.52 tron * need to deal with it.
409 1.52 tron */
410 1.52 tron int
411 1.60.2.1 nathanw linux_sys_stime(l, v, retval)
412 1.60.2.1 nathanw struct lwp *l;
413 1.52 tron void *v;
414 1.52 tron register_t *retval;
415 1.52 tron {
416 1.52 tron struct linux_sys_time_args /* {
417 1.52 tron linux_time_t *t;
418 1.52 tron } */ *uap = v;
419 1.60.2.1 nathanw struct proc *p = l->l_proc;
420 1.52 tron struct timeval atv;
421 1.52 tron linux_time_t tt;
422 1.52 tron int error;
423 1.52 tron
424 1.52 tron if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
425 1.52 tron return (error);
426 1.52 tron
427 1.52 tron if ((error = copyin(&tt, SCARG(uap, t), sizeof tt)) != 0)
428 1.52 tron return error;
429 1.52 tron
430 1.52 tron atv.tv_sec = tt;
431 1.52 tron atv.tv_usec = 0;
432 1.52 tron
433 1.52 tron if ((error = settime(&atv)))
434 1.52 tron return (error);
435 1.52 tron
436 1.52 tron return 0;
437 1.1 fvdl }
438