1 /* $NetBSD: linux_misc_notalpha.c,v 1.94 2007/07/09 21:10:46 ad 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.94 2007/07/09 21:10:46 ad 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/prot.h> 53 #include <sys/ptrace.h> 54 #include <sys/resource.h> 55 #include <sys/resourcevar.h> 56 #include <sys/time.h> 57 #include <sys/vfs_syscalls.h> 58 #include <sys/wait.h> 59 #include <sys/kauth.h> 60 61 #include <sys/syscallargs.h> 62 63 #include <compat/linux/common/linux_types.h> 64 #include <compat/linux/common/linux_fcntl.h> 65 #include <compat/linux/common/linux_misc.h> 66 #include <compat/linux/common/linux_mmap.h> 67 #include <compat/linux/common/linux_signal.h> 68 #include <compat/linux/common/linux_util.h> 69 70 #include <compat/linux/linux_syscallargs.h> 71 72 /* 73 * This file contains routines which are used 74 * on every linux architechture except the Alpha. 75 */ 76 77 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */ 78 /* Not used on: alpha */ 79 80 #ifdef DEBUG_LINUX 81 #define DPRINTF(a) uprintf a 82 #else 83 #define DPRINTF(a) 84 #endif 85 86 #ifndef COMPAT_LINUX32 87 #if !defined(__m68k__) && !defined(__amd64__) 88 static void bsd_to_linux_statfs64(const struct statvfs *, 89 struct linux_statfs64 *); 90 #endif 91 92 /* 93 * Alarm. This is a libc call which uses setitimer(2) in NetBSD. 94 * Fiddle with the timers to make it work. 95 */ 96 int 97 linux_sys_alarm(l, v, retval) 98 struct lwp *l; 99 void *v; 100 register_t *retval; 101 { 102 struct linux_sys_alarm_args /* { 103 syscallarg(unsigned int) secs; 104 } */ *uap = v; 105 struct proc *p = l->l_proc; 106 struct timeval now; 107 struct itimerval *itp, it; 108 struct ptimer *ptp; 109 int s; 110 111 if (p->p_timers && p->p_timers->pts_timers[ITIMER_REAL]) 112 itp = &p->p_timers->pts_timers[ITIMER_REAL]->pt_time; 113 else 114 itp = NULL; 115 s = splclock(); 116 /* 117 * Clear any pending timer alarms. 118 */ 119 if (itp) { 120 callout_stop(&p->p_timers->pts_timers[ITIMER_REAL]->pt_ch); 121 timerclear(&itp->it_interval); 122 getmicrotime(&now); 123 if (timerisset(&itp->it_value) && 124 timercmp(&itp->it_value, &now, >)) 125 timersub(&itp->it_value, &now, &itp->it_value); 126 /* 127 * Return how many seconds were left (rounded up) 128 */ 129 retval[0] = itp->it_value.tv_sec; 130 if (itp->it_value.tv_usec) 131 retval[0]++; 132 } else { 133 retval[0] = 0; 134 } 135 136 /* 137 * alarm(0) just resets the timer. 138 */ 139 if (SCARG(uap, secs) == 0) { 140 if (itp) 141 timerclear(&itp->it_value); 142 splx(s); 143 return 0; 144 } 145 146 /* 147 * Check the new alarm time for sanity, and set it. 148 */ 149 timerclear(&it.it_interval); 150 it.it_value.tv_sec = SCARG(uap, secs); 151 it.it_value.tv_usec = 0; 152 if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) { 153 splx(s); 154 return (EINVAL); 155 } 156 157 if (p->p_timers == NULL) 158 timers_alloc(p); 159 ptp = p->p_timers->pts_timers[ITIMER_REAL]; 160 if (ptp == NULL) { 161 ptp = pool_get(&ptimer_pool, PR_WAITOK); 162 ptp->pt_ev.sigev_notify = SIGEV_SIGNAL; 163 ptp->pt_ev.sigev_signo = SIGALRM; 164 ptp->pt_overruns = 0; 165 ptp->pt_proc = p; 166 ptp->pt_type = CLOCK_REALTIME; 167 ptp->pt_entry = CLOCK_REALTIME; 168 callout_init(&ptp->pt_ch, 0); 169 p->p_timers->pts_timers[ITIMER_REAL] = ptp; 170 } 171 172 if (timerisset(&it.it_value)) { 173 /* 174 * Don't need to check hzto() return value, here. 175 * callout_reset() does it for us. 176 */ 177 getmicrotime(&now); 178 timeradd(&it.it_value, &now, &it.it_value); 179 callout_reset(&ptp->pt_ch, hzto(&it.it_value), 180 realtimerexpire, ptp); 181 } 182 ptp->pt_time = it; 183 splx(s); 184 185 return 0; 186 } 187 #endif /* !COMPAT_LINUX32 */ 188 189 #if !defined(__amd64__) || defined(COMPAT_LINUX32) 190 int 191 linux_sys_nice(l, v, retval) 192 struct lwp *l; 193 void *v; 194 register_t *retval; 195 { 196 struct linux_sys_nice_args /* { 197 syscallarg(int) incr; 198 } */ *uap = v; 199 struct sys_setpriority_args bsa; 200 201 SCARG(&bsa, which) = PRIO_PROCESS; 202 SCARG(&bsa, who) = 0; 203 SCARG(&bsa, prio) = SCARG(uap, incr); 204 return sys_setpriority(l, &bsa, retval); 205 } 206 #endif /* !__amd64__ || COMPAT_LINUX32 */ 207 208 #ifndef COMPAT_LINUX32 209 #ifndef __amd64__ 210 /* 211 * The old Linux readdir was only able to read one entry at a time, 212 * even though it had a 'count' argument. In fact, the emulation 213 * of the old call was better than the original, because it did handle 214 * the count arg properly. Don't bother with it anymore now, and use 215 * it to distinguish between old and new. The difference is that the 216 * newer one actually does multiple entries, and the reclen field 217 * really is the reclen, not the namelength. 218 */ 219 int 220 linux_sys_readdir(l, v, retval) 221 struct lwp *l; 222 void *v; 223 register_t *retval; 224 { 225 struct linux_sys_readdir_args /* { 226 syscallarg(int) fd; 227 syscallarg(struct linux_dirent *) dent; 228 syscallarg(unsigned int) count; 229 } */ *uap = v; 230 231 SCARG(uap, count) = 1; 232 return linux_sys_getdents(l, uap, retval); 233 } 234 #endif /* !amd64 */ 235 236 /* 237 * I wonder why Linux has gettimeofday() _and_ time().. Still, we 238 * need to deal with it. 239 */ 240 int 241 linux_sys_time(struct lwp *l, void *v, register_t *retval) 242 { 243 struct linux_sys_time_args /* { 244 linux_time_t *t; 245 } */ *uap = v; 246 struct timeval atv; 247 linux_time_t tt; 248 int error; 249 250 microtime(&atv); 251 252 tt = atv.tv_sec; 253 if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt))) 254 return error; 255 256 retval[0] = tt; 257 return 0; 258 } 259 260 /* 261 * utime(). Do conversion to things that utimes() understands, 262 * and pass it on. 263 */ 264 int 265 linux_sys_utime(l, v, retval) 266 struct lwp *l; 267 void *v; 268 register_t *retval; 269 { 270 struct linux_sys_utime_args /* { 271 syscallarg(const char *) path; 272 syscallarg(struct linux_utimbuf *)times; 273 } */ *uap = v; 274 int error; 275 struct timeval tv[2], *tvp; 276 struct linux_utimbuf lut; 277 278 if (SCARG(uap, times) != NULL) { 279 if ((error = copyin(SCARG(uap, times), &lut, sizeof lut))) 280 return error; 281 tv[0].tv_usec = tv[1].tv_usec = 0; 282 tv[0].tv_sec = lut.l_actime; 283 tv[1].tv_sec = lut.l_modtime; 284 tvp = tv; 285 } else 286 tvp = NULL; 287 288 return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW, 289 tvp, UIO_SYSSPACE); 290 } 291 292 #ifndef __amd64__ 293 /* 294 * waitpid(2). Just forward on to linux_sys_wait4 with a NULL rusage. 295 */ 296 int 297 linux_sys_waitpid(l, v, retval) 298 struct lwp *l; 299 void *v; 300 register_t *retval; 301 { 302 struct linux_sys_waitpid_args /* { 303 syscallarg(int) pid; 304 syscallarg(int *) status; 305 syscallarg(int) options; 306 } */ *uap = v; 307 struct linux_sys_wait4_args linux_w4a; 308 309 SCARG(&linux_w4a, pid) = SCARG(uap, pid); 310 SCARG(&linux_w4a, status) = SCARG(uap, status); 311 SCARG(&linux_w4a, options) = SCARG(uap, options); 312 SCARG(&linux_w4a, rusage) = NULL; 313 314 return linux_sys_wait4(l, &linux_w4a, retval); 315 } 316 #endif /* !amd64 */ 317 318 int 319 linux_sys_setresgid(struct lwp *l, void *v, register_t *retval) 320 { 321 struct linux_sys_setresgid_args /* { 322 syscallarg(gid_t) rgid; 323 syscallarg(gid_t) egid; 324 syscallarg(gid_t) sgid; 325 } */ *uap = v; 326 327 /* 328 * Note: These checks are a little different than the NetBSD 329 * setregid(2) call performs. This precisely follows the 330 * behavior of the Linux kernel. 331 */ 332 return do_setresgid(l, SCARG(uap,rgid), SCARG(uap, egid), 333 SCARG(uap, sgid), 334 ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S | 335 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S | 336 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S ); 337 } 338 339 int 340 linux_sys_getresgid(struct lwp *l, void *v, register_t *retval) 341 { 342 struct linux_sys_getresgid_args /* { 343 syscallarg(gid_t *) rgid; 344 syscallarg(gid_t *) egid; 345 syscallarg(gid_t *) sgid; 346 } */ *uap = v; 347 kauth_cred_t pc = l->l_cred; 348 int error; 349 gid_t gid; 350 351 /* 352 * Linux copies these values out to userspace like so: 353 * 354 * 1. Copy out rgid. 355 * 2. If that succeeds, copy out egid. 356 * 3. If both of those succeed, copy out sgid. 357 */ 358 gid = kauth_cred_getgid(pc); 359 if ((error = copyout(&gid, SCARG(uap, rgid), sizeof(gid_t))) != 0) 360 return (error); 361 362 gid = kauth_cred_getegid(pc); 363 if ((error = copyout(&gid, SCARG(uap, egid), sizeof(gid_t))) != 0) 364 return (error); 365 366 gid = kauth_cred_getsvgid(pc); 367 368 return (copyout(&gid, SCARG(uap, sgid), sizeof(gid_t))); 369 } 370 371 #ifndef __amd64__ 372 /* 373 * I wonder why Linux has settimeofday() _and_ stime().. Still, we 374 * need to deal with it. 375 */ 376 int 377 linux_sys_stime(struct lwp *l, void *v, register_t *retval) 378 { 379 struct linux_sys_time_args /* { 380 linux_time_t *t; 381 } */ *uap = v; 382 struct timespec ats; 383 linux_time_t tt; 384 int error; 385 386 if ((error = kauth_authorize_system(l->l_cred, 387 KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, NULL, NULL, 388 NULL)) != 0) 389 return (error); 390 391 if ((error = copyin(&tt, SCARG(uap, t), sizeof tt)) != 0) 392 return error; 393 394 ats.tv_sec = tt; 395 ats.tv_nsec = 0; 396 397 if ((error = settime(l->l_proc, &ats))) 398 return (error); 399 400 return 0; 401 } 402 #endif /* !amd64 */ 403 404 #if !defined(__m68k__) && !defined(__amd64__) 405 /* 406 * Convert NetBSD statvfs structure to Linux statfs64 structure. 407 * See comments in bsd_to_linux_statfs() for further background. 408 * We can safely pass correct bsize and frsize here, since Linux glibc 409 * statvfs() doesn't use statfs64(). 410 */ 411 static void 412 bsd_to_linux_statfs64(bsp, lsp) 413 const struct statvfs *bsp; 414 struct linux_statfs64 *lsp; 415 { 416 int i, div; 417 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; 421 break; 422 } 423 } 424 425 if (i == linux_fstypes_cnt) { 426 DPRINTF(("unhandled fstype in linux emulation: %s\n", 427 bsp->f_fstypename)); 428 lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC; 429 } 430 431 div = bsp->f_frsize ? (bsp->f_bsize / bsp->f_frsize) : 1; 432 if (div == 0) 433 div = 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)); 446 } 447 448 /* 449 * Implement the fs stat functions. Straightforward. 450 */ 451 int 452 linux_sys_statfs64(l, v, retval) 453 struct lwp *l; 454 void *v; 455 register_t *retval; 456 { 457 struct linux_sys_statfs64_args /* { 458 syscallarg(const char *) path; 459 syscallarg(size_t) sz; 460 syscallarg(struct linux_statfs64 *) sp; 461 } */ *uap = v; 462 struct statvfs *sb; 463 struct linux_statfs64 ltmp; 464 int error; 465 466 if (SCARG(uap, sz) != sizeof ltmp) 467 return (EINVAL); 468 469 sb = STATVFSBUF_GET(); 470 error = do_sys_pstatvfs(l, SCARG(uap, path), ST_WAIT, sb); 471 if (error == 0) { 472 bsd_to_linux_statfs64(sb, <mp); 473 error = copyout(<mp, SCARG(uap, sp), sizeof ltmp); 474 } 475 STATVFSBUF_PUT(sb); 476 return error; 477 } 478 479 int 480 linux_sys_fstatfs64(l, v, retval) 481 struct lwp *l; 482 void *v; 483 register_t *retval; 484 { 485 struct linux_sys_fstatfs64_args /* { 486 syscallarg(int) fd; 487 syscallarg(size_t) sz; 488 syscallarg(struct linux_statfs64 *) sp; 489 } */ *uap = v; 490 struct statvfs *sb; 491 struct linux_statfs64 ltmp; 492 int error; 493 494 if (SCARG(uap, sz) != sizeof ltmp) 495 return (EINVAL); 496 497 sb = STATVFSBUF_GET(); 498 error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb); 499 if (error == 0) { 500 bsd_to_linux_statfs64(sb, <mp); 501 error = copyout(<mp, SCARG(uap, sp), sizeof ltmp); 502 } 503 STATVFSBUF_PUT(sb); 504 return error; 505 } 506 #endif /* !__m68k__ && !__amd64__ */ 507 #endif /* !COMPAT_LINUX32 */ 508