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