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