linux_misc_notalpha.c revision 1.51 1 1.51 thorpej /* $NetBSD: linux_misc_notalpha.c,v 1.51 1999/05/14 18:45:31 thorpej 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.1 fvdl
40 1.1 fvdl #include <sys/param.h>
41 1.1 fvdl #include <sys/systm.h>
42 1.1 fvdl #include <sys/kernel.h>
43 1.47 erh #include <sys/mman.h>
44 1.47 erh #include <sys/mount.h>
45 1.1 fvdl #include <sys/malloc.h>
46 1.1 fvdl #include <sys/mbuf.h>
47 1.47 erh #include <sys/namei.h>
48 1.1 fvdl #include <sys/ptrace.h>
49 1.1 fvdl #include <sys/resource.h>
50 1.1 fvdl #include <sys/resourcevar.h>
51 1.1 fvdl #include <sys/wait.h>
52 1.1 fvdl
53 1.1 fvdl #include <sys/syscallargs.h>
54 1.1 fvdl
55 1.1 fvdl #include <vm/vm.h>
56 1.1 fvdl #include <vm/vm_param.h>
57 1.1 fvdl
58 1.48 christos #include <compat/linux/common/linux_types.h>
59 1.48 christos #include <compat/linux/common/linux_fcntl.h>
60 1.48 christos #include <compat/linux/common/linux_misc.h>
61 1.48 christos #include <compat/linux/common/linux_mmap.h>
62 1.48 christos #include <compat/linux/common/linux_signal.h>
63 1.48 christos #include <compat/linux/common/linux_util.h>
64 1.48 christos
65 1.1 fvdl #include <compat/linux/linux_syscallargs.h>
66 1.26 christos
67 1.1 fvdl /*
68 1.47 erh * This file contains routines which are used
69 1.47 erh * on every linux architechture except the Alpha.
70 1.1 fvdl */
71 1.21 mycroft
72 1.47 erh /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
73 1.47 erh /* Not used on: alpha */
74 1.1 fvdl
75 1.1 fvdl /*
76 1.47 erh * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
77 1.47 erh * Fiddle with the timers to make it work.
78 1.1 fvdl */
79 1.1 fvdl int
80 1.47 erh linux_sys_alarm(p, v, retval)
81 1.1 fvdl struct proc *p;
82 1.20 thorpej void *v;
83 1.20 thorpej register_t *retval;
84 1.20 thorpej {
85 1.47 erh struct linux_sys_alarm_args /* {
86 1.47 erh syscallarg(unsigned int) secs;
87 1.20 thorpej } */ *uap = v;
88 1.47 erh int s;
89 1.47 erh struct itimerval *itp, it;
90 1.1 fvdl
91 1.47 erh itp = &p->p_realtimer;
92 1.47 erh s = splclock();
93 1.47 erh /*
94 1.47 erh * Clear any pending timer alarms.
95 1.47 erh */
96 1.47 erh untimeout(realitexpire, p);
97 1.47 erh timerclear(&itp->it_interval);
98 1.47 erh if (timerisset(&itp->it_value) &&
99 1.47 erh timercmp(&itp->it_value, &time, >))
100 1.47 erh timersub(&itp->it_value, &time, &itp->it_value);
101 1.47 erh /*
102 1.47 erh * Return how many seconds were left (rounded up)
103 1.47 erh */
104 1.47 erh retval[0] = itp->it_value.tv_sec;
105 1.47 erh if (itp->it_value.tv_usec)
106 1.47 erh retval[0]++;
107 1.1 fvdl
108 1.47 erh /*
109 1.47 erh * alarm(0) just resets the timer.
110 1.47 erh */
111 1.47 erh if (SCARG(uap, secs) == 0) {
112 1.47 erh timerclear(&itp->it_value);
113 1.47 erh splx(s);
114 1.47 erh return 0;
115 1.47 erh }
116 1.1 fvdl
117 1.47 erh /*
118 1.47 erh * Check the new alarm time for sanity, and set it.
119 1.47 erh */
120 1.47 erh timerclear(&it.it_interval);
121 1.47 erh it.it_value.tv_sec = SCARG(uap, secs);
122 1.47 erh it.it_value.tv_usec = 0;
123 1.47 erh if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
124 1.47 erh splx(s);
125 1.47 erh return (EINVAL);
126 1.16 fvdl }
127 1.1 fvdl
128 1.47 erh if (timerisset(&it.it_value)) {
129 1.47 erh timeradd(&it.it_value, &time, &it.it_value);
130 1.47 erh timeout(realitexpire, p, hzto(&it.it_value));
131 1.16 fvdl }
132 1.47 erh p->p_realtimer = it;
133 1.47 erh splx(s);
134 1.1 fvdl
135 1.16 fvdl return 0;
136 1.1 fvdl }
137 1.1 fvdl
138 1.1 fvdl int
139 1.47 erh linux_sys_nice(p, v, retval)
140 1.1 fvdl struct proc *p;
141 1.20 thorpej void *v;
142 1.20 thorpej register_t *retval;
143 1.20 thorpej {
144 1.47 erh struct linux_sys_nice_args /* {
145 1.47 erh syscallarg(int) incr;
146 1.20 thorpej } */ *uap = v;
147 1.47 erh struct sys_setpriority_args bsa;
148 1.20 thorpej
149 1.47 erh SCARG(&bsa, which) = PRIO_PROCESS;
150 1.47 erh SCARG(&bsa, who) = 0;
151 1.47 erh SCARG(&bsa, prio) = SCARG(uap, incr);
152 1.47 erh return sys_setpriority(p, &bsa, retval);
153 1.1 fvdl }
154 1.1 fvdl
155 1.1 fvdl /*
156 1.47 erh * The old Linux readdir was only able to read one entry at a time,
157 1.47 erh * even though it had a 'count' argument. In fact, the emulation
158 1.47 erh * of the old call was better than the original, because it did handle
159 1.47 erh * the count arg properly. Don't bother with it anymore now, and use
160 1.47 erh * it to distinguish between old and new. The difference is that the
161 1.47 erh * newer one actually does multiple entries, and the reclen field
162 1.47 erh * really is the reclen, not the namelength.
163 1.1 fvdl */
164 1.1 fvdl int
165 1.47 erh linux_sys_readdir(p, v, retval)
166 1.1 fvdl struct proc *p;
167 1.20 thorpej void *v;
168 1.20 thorpej register_t *retval;
169 1.20 thorpej {
170 1.47 erh struct linux_sys_readdir_args /* {
171 1.47 erh syscallarg(int) fd;
172 1.47 erh syscallarg(struct linux_dirent *) dent;
173 1.47 erh syscallarg(unsigned int) count;
174 1.20 thorpej } */ *uap = v;
175 1.1 fvdl
176 1.47 erh SCARG(uap, count) = 1;
177 1.47 erh return linux_sys_getdents(p, uap, retval);
178 1.1 fvdl }
179 1.1 fvdl
180 1.1 fvdl /*
181 1.1 fvdl * I wonder why Linux has gettimeofday() _and_ time().. Still, we
182 1.1 fvdl * need to deal with it.
183 1.1 fvdl */
184 1.1 fvdl int
185 1.21 mycroft linux_sys_time(p, v, retval)
186 1.1 fvdl struct proc *p;
187 1.20 thorpej void *v;
188 1.20 thorpej register_t *retval;
189 1.20 thorpej {
190 1.21 mycroft struct linux_sys_time_args /* {
191 1.1 fvdl linux_time_t *t;
192 1.20 thorpej } */ *uap = v;
193 1.1 fvdl struct timeval atv;
194 1.1 fvdl linux_time_t tt;
195 1.1 fvdl int error;
196 1.1 fvdl
197 1.1 fvdl microtime(&atv);
198 1.1 fvdl
199 1.1 fvdl tt = atv.tv_sec;
200 1.1 fvdl if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
201 1.1 fvdl return error;
202 1.1 fvdl
203 1.1 fvdl retval[0] = tt;
204 1.1 fvdl return 0;
205 1.1 fvdl }
206 1.1 fvdl
207 1.1 fvdl /*
208 1.47 erh * utime(). Do conversion to things that utimes() understands,
209 1.47 erh * and pass it on.
210 1.1 fvdl */
211 1.1 fvdl int
212 1.47 erh linux_sys_utime(p, v, retval)
213 1.1 fvdl struct proc *p;
214 1.20 thorpej void *v;
215 1.20 thorpej register_t *retval;
216 1.20 thorpej {
217 1.47 erh struct linux_sys_utime_args /* {
218 1.50 christos syscallarg(const char *) path;
219 1.47 erh syscallarg(struct linux_utimbuf *)times;
220 1.20 thorpej } */ *uap = v;
221 1.2 fvdl caddr_t sg;
222 1.2 fvdl int error;
223 1.47 erh struct sys_utimes_args ua;
224 1.47 erh struct timeval tv[2], *tvp;
225 1.47 erh struct linux_utimbuf lut;
226 1.2 fvdl
227 1.9 christos sg = stackgap_init(p->p_emul);
228 1.9 christos LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
229 1.2 fvdl
230 1.47 erh SCARG(&ua, path) = SCARG(uap, path);
231 1.2 fvdl
232 1.47 erh if (SCARG(uap, times) != NULL) {
233 1.47 erh if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
234 1.47 erh return error;
235 1.47 erh tv[0].tv_usec = tv[1].tv_usec = 0;
236 1.47 erh tv[0].tv_sec = lut.l_actime;
237 1.47 erh tv[1].tv_sec = lut.l_modtime;
238 1.47 erh tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
239 1.47 erh if ((error = copyout(tv, tvp, sizeof tv)))
240 1.47 erh return error;
241 1.47 erh SCARG(&ua, tptr) = tvp;
242 1.47 erh }
243 1.47 erh else
244 1.47 erh SCARG(&ua, tptr) = NULL;
245 1.2 fvdl
246 1.47 erh return sys_utimes(p, &ua, retval);
247 1.1 fvdl }
248 1.1 fvdl
249 1.47 erh /*
250 1.47 erh * waitpid(2). Passed on to the NetBSD call, surrounded by code to
251 1.47 erh * reserve some space for a NetBSD-style wait status, and converting
252 1.47 erh * it to what Linux wants.
253 1.47 erh */
254 1.1 fvdl int
255 1.47 erh linux_sys_waitpid(p, v, retval)
256 1.1 fvdl struct proc *p;
257 1.20 thorpej void *v;
258 1.20 thorpej register_t *retval;
259 1.20 thorpej {
260 1.47 erh struct linux_sys_waitpid_args /* {
261 1.47 erh syscallarg(int) pid;
262 1.47 erh syscallarg(int *) status;
263 1.47 erh syscallarg(int) options;
264 1.20 thorpej } */ *uap = v;
265 1.47 erh struct sys_wait4_args w4a;
266 1.47 erh int error, *status, tstat;
267 1.2 fvdl caddr_t sg;
268 1.2 fvdl
269 1.47 erh if (SCARG(uap, status) != NULL) {
270 1.47 erh sg = stackgap_init(p->p_emul);
271 1.47 erh status = (int *) stackgap_alloc(&sg, sizeof status);
272 1.47 erh } else
273 1.47 erh status = NULL;
274 1.2 fvdl
275 1.47 erh SCARG(&w4a, pid) = SCARG(uap, pid);
276 1.47 erh SCARG(&w4a, status) = status;
277 1.47 erh SCARG(&w4a, options) = SCARG(uap, options);
278 1.47 erh SCARG(&w4a, rusage) = NULL;
279 1.2 fvdl
280 1.47 erh if ((error = sys_wait4(p, &w4a, retval)))
281 1.2 fvdl return error;
282 1.2 fvdl
283 1.47 erh sigdelset(&p->p_siglist, SIGCHLD);
284 1.2 fvdl
285 1.47 erh if (status != NULL) {
286 1.47 erh if ((error = copyin(status, &tstat, sizeof tstat)))
287 1.47 erh return error;
288 1.2 fvdl
289 1.47 erh bsd_to_linux_wstat(&tstat);
290 1.47 erh return copyout(&tstat, SCARG(uap, status), sizeof tstat);
291 1.44 thorpej }
292 1.1 fvdl
293 1.47 erh return 0;
294 1.51 thorpej }
295 1.51 thorpej
296 1.51 thorpej int
297 1.51 thorpej linux_sys_setresgid(p, v, retval)
298 1.51 thorpej struct proc *p;
299 1.51 thorpej void *v;
300 1.51 thorpej register_t *retval;
301 1.51 thorpej {
302 1.51 thorpej struct linux_sys_setresgid_args /* {
303 1.51 thorpej syscallarg(gid_t) rgid;
304 1.51 thorpej syscallarg(gid_t) egid;
305 1.51 thorpej syscallarg(gid_t) sgid;
306 1.51 thorpej } */ *uap = v;
307 1.51 thorpej struct pcred *pc = p->p_cred;
308 1.51 thorpej gid_t rgid, egid, sgid;
309 1.51 thorpej int error;
310 1.51 thorpej
311 1.51 thorpej rgid = SCARG(uap, rgid);
312 1.51 thorpej egid = SCARG(uap, egid);
313 1.51 thorpej sgid = SCARG(uap, sgid);
314 1.51 thorpej
315 1.51 thorpej /*
316 1.51 thorpej * Note: These checks are a little different than the NetBSD
317 1.51 thorpej * setregid(2) call performs. This precisely follows the
318 1.51 thorpej * behavior of the Linux kernel.
319 1.51 thorpej */
320 1.51 thorpej if (rgid != (gid_t)-1 &&
321 1.51 thorpej rgid != pc->p_rgid &&
322 1.51 thorpej rgid != pc->pc_ucred->cr_gid &&
323 1.51 thorpej rgid != pc->p_svgid &&
324 1.51 thorpej (error = suser(pc->pc_ucred, &p->p_acflag)))
325 1.51 thorpej return (error);
326 1.51 thorpej
327 1.51 thorpej if (egid != (gid_t)-1 &&
328 1.51 thorpej egid != pc->p_rgid &&
329 1.51 thorpej egid != pc->pc_ucred->cr_gid &&
330 1.51 thorpej egid != pc->p_svgid &&
331 1.51 thorpej (error = suser(pc->pc_ucred, &p->p_acflag)))
332 1.51 thorpej return (error);
333 1.51 thorpej
334 1.51 thorpej if (sgid != (gid_t)-1 &&
335 1.51 thorpej sgid != pc->p_rgid &&
336 1.51 thorpej sgid != pc->pc_ucred->cr_gid &&
337 1.51 thorpej sgid != pc->p_svgid &&
338 1.51 thorpej (error = suser(pc->pc_ucred, &p->p_acflag)))
339 1.51 thorpej return (error);
340 1.51 thorpej
341 1.51 thorpej /*
342 1.51 thorpej * Now assign the real, effective, and saved GIDs.
343 1.51 thorpej * Note that Linux, unlike NetBSD in setregid(2), does not
344 1.51 thorpej * set the saved UID in this call unless the user specifies
345 1.51 thorpej * it.
346 1.51 thorpej */
347 1.51 thorpej if (rgid != (gid_t)-1)
348 1.51 thorpej pc->p_rgid = rgid;
349 1.51 thorpej
350 1.51 thorpej if (egid != (gid_t)-1) {
351 1.51 thorpej pc->pc_ucred = crcopy(pc->pc_ucred);
352 1.51 thorpej pc->pc_ucred->cr_gid = egid;
353 1.51 thorpej }
354 1.51 thorpej
355 1.51 thorpej if (sgid != (gid_t)-1)
356 1.51 thorpej pc->p_svgid = sgid;
357 1.51 thorpej
358 1.51 thorpej if (rgid != (gid_t)-1 && egid != (gid_t)-1 && sgid != (gid_t)-1)
359 1.51 thorpej p->p_flag |= P_SUGID;
360 1.51 thorpej return (0);
361 1.51 thorpej }
362 1.51 thorpej
363 1.51 thorpej int
364 1.51 thorpej linux_sys_getresgid(p, v, retval)
365 1.51 thorpej struct proc *p;
366 1.51 thorpej void *v;
367 1.51 thorpej register_t *retval;
368 1.51 thorpej {
369 1.51 thorpej struct linux_sys_getresgid_args /* {
370 1.51 thorpej syscallarg(gid_t *) rgid;
371 1.51 thorpej syscallarg(gid_t *) egid;
372 1.51 thorpej syscallarg(gid_t *) sgid;
373 1.51 thorpej } */ *uap = v;
374 1.51 thorpej struct pcred *pc = p->p_cred;
375 1.51 thorpej int error;
376 1.51 thorpej
377 1.51 thorpej /*
378 1.51 thorpej * Linux copies these values out to userspace like so:
379 1.51 thorpej *
380 1.51 thorpej * 1. Copy out rgid.
381 1.51 thorpej * 2. If that succeeds, copy out egid.
382 1.51 thorpej * 3. If both of those succeed, copy out sgid.
383 1.51 thorpej */
384 1.51 thorpej if ((error = copyout(&pc->p_rgid, SCARG(uap, rgid),
385 1.51 thorpej sizeof(gid_t))) != 0)
386 1.51 thorpej return (error);
387 1.51 thorpej
388 1.51 thorpej if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, egid),
389 1.51 thorpej sizeof(gid_t))) != 0)
390 1.51 thorpej return (error);
391 1.51 thorpej
392 1.51 thorpej return (copyout(&pc->p_svgid, SCARG(uap, sgid), sizeof(gid_t)));
393 1.1 fvdl }
394