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