1 /* $NetBSD: linux_misc_notalpha.c,v 1.102 2008/04/28 20:23:43 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 1998, 2008 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 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: linux_misc_notalpha.c,v 1.102 2008/04/28 20:23:43 martin Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/mman.h> 40 #include <sys/mount.h> 41 #include <sys/malloc.h> 42 #include <sys/mbuf.h> 43 #include <sys/namei.h> 44 #include <sys/proc.h> 45 #include <sys/prot.h> 46 #include <sys/ptrace.h> 47 #include <sys/resource.h> 48 #include <sys/resourcevar.h> 49 #include <sys/time.h> 50 #include <sys/vfs_syscalls.h> 51 #include <sys/wait.h> 52 #include <sys/kauth.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 #include <compat/linux/common/linux_ipc.h> 63 #include <compat/linux/common/linux_sem.h> 64 65 #include <compat/linux/linux_syscallargs.h> 66 67 /* 68 * This file contains routines which are used 69 * on every linux architechture except the Alpha. 70 */ 71 72 /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */ 73 /* Not used on: alpha */ 74 75 #ifdef DEBUG_LINUX 76 #define DPRINTF(a) uprintf a 77 #else 78 #define DPRINTF(a) 79 #endif 80 81 #ifndef COMPAT_LINUX32 82 #if !defined(__m68k__) && !defined(__amd64__) 83 static void bsd_to_linux_statfs64(const struct statvfs *, 84 struct linux_statfs64 *); 85 #endif 86 87 /* 88 * Alarm. This is a libc call which uses setitimer(2) in NetBSD. 89 * Fiddle with the timers to make it work. 90 * 91 * XXX This shouldn't be dicking about with the ptimer stuff directly. 92 */ 93 int 94 linux_sys_alarm(struct lwp *l, const struct linux_sys_alarm_args *uap, register_t *retval) 95 { 96 /* { 97 syscallarg(unsigned int) secs; 98 } */ 99 struct proc *p = l->l_proc; 100 struct timeval now; 101 struct itimerval *itp, it; 102 struct ptimer *ptp, *spare; 103 extern kmutex_t timer_lock; 104 struct ptimers *pts; 105 106 if ((pts = p->p_timers) == NULL) 107 pts = timers_alloc(p); 108 spare = NULL; 109 110 retry: 111 mutex_spin_enter(&timer_lock); 112 if (pts && pts->pts_timers[ITIMER_REAL]) 113 itp = &pts->pts_timers[ITIMER_REAL]->pt_time; 114 else 115 itp = NULL; 116 /* 117 * Clear any pending timer alarms. 118 */ 119 if (itp) { 120 callout_stop(&pts->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 mutex_spin_exit(&timer_lock); 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 mutex_spin_exit(&timer_lock); 154 return (EINVAL); 155 } 156 157 ptp = pts->pts_timers[ITIMER_REAL]; 158 if (ptp == NULL) { 159 if (spare == NULL) { 160 mutex_spin_exit(&timer_lock); 161 spare = pool_get(&ptimer_pool, PR_WAITOK); 162 goto retry; 163 } 164 ptp = spare; 165 spare = NULL; 166 ptp->pt_ev.sigev_notify = SIGEV_SIGNAL; 167 ptp->pt_ev.sigev_signo = SIGALRM; 168 ptp->pt_overruns = 0; 169 ptp->pt_proc = p; 170 ptp->pt_type = CLOCK_REALTIME; 171 ptp->pt_entry = CLOCK_REALTIME; 172 ptp->pt_active = 0; 173 ptp->pt_queued = 0; 174 callout_init(&ptp->pt_ch, CALLOUT_MPSAFE); 175 pts->pts_timers[ITIMER_REAL] = ptp; 176 } 177 178 if (timerisset(&it.it_value)) { 179 /* 180 * Don't need to check hzto() return value, here. 181 * callout_reset() does it for us. 182 */ 183 getmicrotime(&now); 184 timeradd(&it.it_value, &now, &it.it_value); 185 callout_reset(&ptp->pt_ch, hzto(&it.it_value), 186 realtimerexpire, ptp); 187 } 188 ptp->pt_time = it; 189 mutex_spin_exit(&timer_lock); 190 191 return 0; 192 } 193 #endif /* !COMPAT_LINUX32 */ 194 195 #if !defined(__amd64__) 196 int 197 linux_sys_nice(struct lwp *l, const struct linux_sys_nice_args *uap, register_t *retval) 198 { 199 /* { 200 syscallarg(int) incr; 201 } */ 202 struct sys_setpriority_args bsa; 203 204 SCARG(&bsa, which) = PRIO_PROCESS; 205 SCARG(&bsa, who) = 0; 206 SCARG(&bsa, prio) = SCARG(uap, incr); 207 return sys_setpriority(l, &bsa, retval); 208 } 209 #endif /* !__amd64__ */ 210 211 #ifndef COMPAT_LINUX32 212 #ifndef __amd64__ 213 /* 214 * The old Linux readdir was only able to read one entry at a time, 215 * even though it had a 'count' argument. In fact, the emulation 216 * of the old call was better than the original, because it did handle 217 * the count arg properly. Don't bother with it anymore now, and use 218 * it to distinguish between old and new. The difference is that the 219 * newer one actually does multiple entries, and the reclen field 220 * really is the reclen, not the namelength. 221 */ 222 int 223 linux_sys_readdir(struct lwp *l, const struct linux_sys_readdir_args *uap, register_t *retval) 224 { 225 /* { 226 syscallarg(int) fd; 227 syscallarg(struct linux_dirent *) dent; 228 syscallarg(unsigned int) count; 229 } */ 230 int error; 231 struct linux_sys_getdents_args da; 232 233 SCARG(&da, fd) = SCARG(uap, fd); 234 SCARG(&da, dent) = SCARG(uap, dent); 235 SCARG(&da, count) = 1; 236 237 error = linux_sys_getdents(l, &da, retval); 238 if (error == 0 && *retval > 1) 239 *retval = 1; 240 241 return error; 242 } 243 #endif /* !amd64 */ 244 245 /* 246 * I wonder why Linux has gettimeofday() _and_ time().. Still, we 247 * need to deal with it. 248 */ 249 int 250 linux_sys_time(struct lwp *l, const struct linux_sys_time_args *uap, register_t *retval) 251 { 252 /* { 253 syscallarg(linux_time_t) *t; 254 } */ 255 struct timeval atv; 256 linux_time_t tt; 257 int error; 258 259 microtime(&atv); 260 261 tt = atv.tv_sec; 262 if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt))) 263 return error; 264 265 retval[0] = tt; 266 return 0; 267 } 268 269 /* 270 * utime(). Do conversion to things that utimes() understands, 271 * and pass it on. 272 */ 273 int 274 linux_sys_utime(struct lwp *l, const struct linux_sys_utime_args *uap, register_t *retval) 275 { 276 /* { 277 syscallarg(const char *) path; 278 syscallarg(struct linux_utimbuf *)times; 279 } */ 280 int error; 281 struct timeval tv[2], *tvp; 282 struct linux_utimbuf lut; 283 284 if (SCARG(uap, times) != NULL) { 285 if ((error = copyin(SCARG(uap, times), &lut, sizeof lut))) 286 return error; 287 tv[0].tv_usec = tv[1].tv_usec = 0; 288 tv[0].tv_sec = lut.l_actime; 289 tv[1].tv_sec = lut.l_modtime; 290 tvp = tv; 291 } else 292 tvp = NULL; 293 294 return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW, 295 tvp, UIO_SYSSPACE); 296 } 297 298 #ifndef __amd64__ 299 /* 300 * waitpid(2). Just forward on to linux_sys_wait4 with a NULL rusage. 301 */ 302 int 303 linux_sys_waitpid(struct lwp *l, const struct linux_sys_waitpid_args *uap, register_t *retval) 304 { 305 /* { 306 syscallarg(int) pid; 307 syscallarg(int *) status; 308 syscallarg(int) options; 309 } */ 310 struct linux_sys_wait4_args linux_w4a; 311 312 SCARG(&linux_w4a, pid) = SCARG(uap, pid); 313 SCARG(&linux_w4a, status) = SCARG(uap, status); 314 SCARG(&linux_w4a, options) = SCARG(uap, options); 315 SCARG(&linux_w4a, rusage) = NULL; 316 317 return linux_sys_wait4(l, &linux_w4a, retval); 318 } 319 #endif /* !amd64 */ 320 321 int 322 linux_sys_setresgid(struct lwp *l, const struct linux_sys_setresgid_args *uap, register_t *retval) 323 { 324 /* { 325 syscallarg(gid_t) rgid; 326 syscallarg(gid_t) egid; 327 syscallarg(gid_t) sgid; 328 } */ 329 330 /* 331 * Note: These checks are a little different than the NetBSD 332 * setregid(2) call performs. This precisely follows the 333 * behavior of the Linux kernel. 334 */ 335 return do_setresgid(l, SCARG(uap,rgid), SCARG(uap, egid), 336 SCARG(uap, sgid), 337 ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S | 338 ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S | 339 ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S ); 340 } 341 342 int 343 linux_sys_getresgid(struct lwp *l, const struct linux_sys_getresgid_args *uap, register_t *retval) 344 { 345 /* { 346 syscallarg(gid_t *) rgid; 347 syscallarg(gid_t *) egid; 348 syscallarg(gid_t *) sgid; 349 } */ 350 kauth_cred_t pc = l->l_cred; 351 int error; 352 gid_t gid; 353 354 /* 355 * Linux copies these values out to userspace like so: 356 * 357 * 1. Copy out rgid. 358 * 2. If that succeeds, copy out egid. 359 * 3. If both of those succeed, copy out sgid. 360 */ 361 gid = kauth_cred_getgid(pc); 362 if ((error = copyout(&gid, SCARG(uap, rgid), sizeof(gid_t))) != 0) 363 return (error); 364 365 gid = kauth_cred_getegid(pc); 366 if ((error = copyout(&gid, SCARG(uap, egid), sizeof(gid_t))) != 0) 367 return (error); 368 369 gid = kauth_cred_getsvgid(pc); 370 371 return (copyout(&gid, SCARG(uap, sgid), sizeof(gid_t))); 372 } 373 374 #ifndef __amd64__ 375 /* 376 * I wonder why Linux has settimeofday() _and_ stime().. Still, we 377 * need to deal with it. 378 */ 379 int 380 linux_sys_stime(struct lwp *l, const struct linux_sys_stime_args *uap, register_t *retval) 381 { 382 /* { 383 syscallarg(linux_time_t) *t; 384 } */ 385 struct timespec ats; 386 linux_time_t tt; 387 int error; 388 389 if ((error = copyin(&tt, SCARG(uap, t), sizeof tt)) != 0) 390 return error; 391 392 ats.tv_sec = tt; 393 ats.tv_nsec = 0; 394 395 if ((error = settime(l->l_proc, &ats))) 396 return (error); 397 398 return 0; 399 } 400 #endif /* !amd64 */ 401 402 #if !defined(__m68k__) && !defined(__amd64__) 403 /* 404 * Convert NetBSD statvfs structure to Linux statfs64 structure. 405 * See comments in bsd_to_linux_statfs() for further background. 406 * We can safely pass correct bsize and frsize here, since Linux glibc 407 * statvfs() doesn't use statfs64(). 408 */ 409 static void 410 bsd_to_linux_statfs64(const struct statvfs *bsp, struct linux_statfs64 *lsp) 411 { 412 int i, div; 413 414 for (i = 0; i < linux_fstypes_cnt; i++) { 415 if (strcmp(bsp->f_fstypename, linux_fstypes[i].bsd) == 0) { 416 lsp->l_ftype = linux_fstypes[i].linux; 417 break; 418 } 419 } 420 421 if (i == linux_fstypes_cnt) { 422 DPRINTF(("unhandled fstype in linux emulation: %s\n", 423 bsp->f_fstypename)); 424 lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC; 425 } 426 427 div = bsp->f_frsize ? (bsp->f_bsize / bsp->f_frsize) : 1; 428 if (div == 0) 429 div = 1; 430 lsp->l_fbsize = bsp->f_bsize; 431 lsp->l_ffrsize = bsp->f_frsize; 432 lsp->l_fblocks = bsp->f_blocks / div; 433 lsp->l_fbfree = bsp->f_bfree / div; 434 lsp->l_fbavail = bsp->f_bavail / div; 435 lsp->l_ffiles = bsp->f_files; 436 lsp->l_fffree = bsp->f_ffree / div; 437 /* Linux sets the fsid to 0..., we don't */ 438 lsp->l_ffsid.val[0] = bsp->f_fsidx.__fsid_val[0]; 439 lsp->l_ffsid.val[1] = bsp->f_fsidx.__fsid_val[1]; 440 lsp->l_fnamelen = bsp->f_namemax; 441 (void)memset(lsp->l_fspare, 0, sizeof(lsp->l_fspare)); 442 } 443 444 /* 445 * Implement the fs stat functions. Straightforward. 446 */ 447 int 448 linux_sys_statfs64(struct lwp *l, const struct linux_sys_statfs64_args *uap, register_t *retval) 449 { 450 /* { 451 syscallarg(const char *) path; 452 syscallarg(size_t) sz; 453 syscallarg(struct linux_statfs64 *) sp; 454 } */ 455 struct statvfs *sb; 456 struct linux_statfs64 ltmp; 457 int error; 458 459 if (SCARG(uap, sz) != sizeof ltmp) 460 return (EINVAL); 461 462 sb = STATVFSBUF_GET(); 463 error = do_sys_pstatvfs(l, SCARG(uap, path), ST_WAIT, sb); 464 if (error == 0) { 465 bsd_to_linux_statfs64(sb, <mp); 466 error = copyout(<mp, SCARG(uap, sp), sizeof ltmp); 467 } 468 STATVFSBUF_PUT(sb); 469 return error; 470 } 471 472 int 473 linux_sys_fstatfs64(struct lwp *l, const struct linux_sys_fstatfs64_args *uap, register_t *retval) 474 { 475 /* { 476 syscallarg(int) fd; 477 syscallarg(size_t) sz; 478 syscallarg(struct linux_statfs64 *) sp; 479 } */ 480 struct statvfs *sb; 481 struct linux_statfs64 ltmp; 482 int error; 483 484 if (SCARG(uap, sz) != sizeof ltmp) 485 return (EINVAL); 486 487 sb = STATVFSBUF_GET(); 488 error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb); 489 if (error == 0) { 490 bsd_to_linux_statfs64(sb, <mp); 491 error = copyout(<mp, SCARG(uap, sp), sizeof ltmp); 492 } 493 STATVFSBUF_PUT(sb); 494 return error; 495 } 496 #endif /* !__m68k__ && !__amd64__ */ 497 #endif /* !COMPAT_LINUX32 */ 498