xref: /freebsd/sys/compat/linux/linux_misc.c (revision f3cec688772252a00e244ae8e7e116f0a3c2473f)
1 /*-
2  * Copyright (c) 2002 Doug Rabson
3  * Copyright (c) 1994-1995 S�ren Schmidt
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_compat.h"
34 #include "opt_mac.h"
35 
36 #include <sys/param.h>
37 #include <sys/blist.h>
38 #include <sys/fcntl.h>
39 #if defined(__i386__)
40 #include <sys/imgact_aout.h>
41 #endif
42 #include <sys/jail.h>
43 #include <sys/kernel.h>
44 #include <sys/limits.h>
45 #include <sys/lock.h>
46 #include <sys/mac.h>
47 #include <sys/malloc.h>
48 #include <sys/mman.h>
49 #include <sys/mount.h>
50 #include <sys/mutex.h>
51 #include <sys/namei.h>
52 #include <sys/proc.h>
53 #include <sys/reboot.h>
54 #include <sys/resourcevar.h>
55 #include <sys/signalvar.h>
56 #include <sys/stat.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysproto.h>
60 #include <sys/systm.h>
61 #include <sys/time.h>
62 #include <sys/vmmeter.h>
63 #include <sys/vnode.h>
64 #include <sys/wait.h>
65 
66 #include <vm/vm.h>
67 #include <vm/pmap.h>
68 #include <vm/vm_kern.h>
69 #include <vm/vm_map.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_object.h>
72 #include <vm/swap_pager.h>
73 
74 #include <posix4/sched.h>
75 
76 #include <compat/linux/linux_sysproto.h>
77 #include <compat/linux/linux_emul.h>
78 
79 #ifdef COMPAT_LINUX32
80 #include <machine/../linux32/linux.h>
81 #include <machine/../linux32/linux32_proto.h>
82 #else
83 #include <machine/../linux/linux.h>
84 #include <machine/../linux/linux_proto.h>
85 #endif
86 
87 #include <compat/linux/linux_mib.h>
88 #include <compat/linux/linux_util.h>
89 
90 #ifdef __i386__
91 #include <machine/cputypes.h>
92 #endif
93 
94 #define BSD_TO_LINUX_SIGNAL(sig)	\
95 	(((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
96 
97 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
98 	RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
99 	RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
100 	RLIMIT_MEMLOCK, -1
101 };
102 
103 struct l_sysinfo {
104 	l_long		uptime;		/* Seconds since boot */
105 	l_ulong		loads[3];	/* 1, 5, and 15 minute load averages */
106 #define LINUX_SYSINFO_LOADS_SCALE 65536
107 	l_ulong		totalram;	/* Total usable main memory size */
108 	l_ulong		freeram;	/* Available memory size */
109 	l_ulong		sharedram;	/* Amount of shared memory */
110 	l_ulong		bufferram;	/* Memory used by buffers */
111 	l_ulong		totalswap;	/* Total swap space size */
112 	l_ulong		freeswap;	/* swap space still available */
113 	l_ushort	procs;		/* Number of current processes */
114 	l_ulong		totalbig;
115 	l_ulong		freebig;
116 	l_uint		mem_unit;
117 	char		_f[6];		/* Pads structure to 64 bytes */
118 };
119 int
120 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
121 {
122 	struct l_sysinfo sysinfo;
123 	vm_object_t object;
124 	int i, j;
125 	struct timespec ts;
126 
127 	getnanouptime(&ts);
128 	if (ts.tv_nsec != 0)
129 		ts.tv_sec++;
130 	sysinfo.uptime = ts.tv_sec;
131 
132 	/* Use the information from the mib to get our load averages */
133 	for (i = 0; i < 3; i++)
134 		sysinfo.loads[i] = averunnable.ldavg[i] *
135 		    LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
136 
137 	sysinfo.totalram = physmem * PAGE_SIZE;
138 	sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
139 
140 	sysinfo.sharedram = 0;
141 	mtx_lock(&vm_object_list_mtx);
142 	TAILQ_FOREACH(object, &vm_object_list, object_list)
143 		if (object->shadow_count > 1)
144 			sysinfo.sharedram += object->resident_page_count;
145 	mtx_unlock(&vm_object_list_mtx);
146 
147 	sysinfo.sharedram *= PAGE_SIZE;
148 	sysinfo.bufferram = 0;
149 
150 	swap_pager_status(&i, &j);
151 	sysinfo.totalswap= i * PAGE_SIZE;
152 	sysinfo.freeswap = (i - j) * PAGE_SIZE;
153 
154 	sysinfo.procs = nprocs;
155 
156 	/* The following are only present in newer Linux kernels. */
157 	sysinfo.totalbig = 0;
158 	sysinfo.freebig = 0;
159 	sysinfo.mem_unit = 1;
160 
161 	return copyout(&sysinfo, args->info, sizeof(sysinfo));
162 }
163 
164 int
165 linux_alarm(struct thread *td, struct linux_alarm_args *args)
166 {
167 	struct itimerval it, old_it;
168 	int error;
169 
170 #ifdef DEBUG
171 	if (ldebug(alarm))
172 		printf(ARGS(alarm, "%u"), args->secs);
173 #endif
174 
175 	if (args->secs > 100000000)
176 		return (EINVAL);
177 
178 	it.it_value.tv_sec = (long)args->secs;
179 	it.it_value.tv_usec = 0;
180 	it.it_interval.tv_sec = 0;
181 	it.it_interval.tv_usec = 0;
182 	error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
183 	if (error)
184 		return (error);
185 	if (timevalisset(&old_it.it_value)) {
186 		if (old_it.it_value.tv_usec != 0)
187 			old_it.it_value.tv_sec++;
188 		td->td_retval[0] = old_it.it_value.tv_sec;
189 	}
190 	return (0);
191 }
192 
193 int
194 linux_brk(struct thread *td, struct linux_brk_args *args)
195 {
196 	struct vmspace *vm = td->td_proc->p_vmspace;
197 	vm_offset_t new, old;
198 	struct obreak_args /* {
199 		char * nsize;
200 	} */ tmp;
201 
202 #ifdef DEBUG
203 	if (ldebug(brk))
204 		printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
205 #endif
206 	old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
207 	new = (vm_offset_t)args->dsend;
208 	tmp.nsize = (char *) new;
209 	if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
210 		td->td_retval[0] = (long)new;
211 	else
212 		td->td_retval[0] = (long)old;
213 
214 	return 0;
215 }
216 
217 #if defined(__i386__)
218 /* XXX: what about amd64/linux32? */
219 
220 int
221 linux_uselib(struct thread *td, struct linux_uselib_args *args)
222 {
223 	struct nameidata ni;
224 	struct vnode *vp;
225 	struct exec *a_out;
226 	struct vattr attr;
227 	vm_offset_t vmaddr;
228 	unsigned long file_offset;
229 	vm_offset_t buffer;
230 	unsigned long bss_size;
231 	char *library;
232 	int error;
233 	int locked, vfslocked;
234 
235 	LCONVPATHEXIST(td, args->library, &library);
236 
237 #ifdef DEBUG
238 	if (ldebug(uselib))
239 		printf(ARGS(uselib, "%s"), library);
240 #endif
241 
242 	a_out = NULL;
243 	vfslocked = 0;
244 	locked = 0;
245 	vp = NULL;
246 
247 	NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
248 	    UIO_SYSSPACE, library, td);
249 	error = namei(&ni);
250 	LFREEPATH(library);
251 	if (error)
252 		goto cleanup;
253 
254 	vp = ni.ni_vp;
255 	vfslocked = NDHASGIANT(&ni);
256 	NDFREE(&ni, NDF_ONLY_PNBUF);
257 
258 	/*
259 	 * From here on down, we have a locked vnode that must be unlocked.
260 	 * XXX: The code below largely duplicates exec_check_permissions().
261 	 */
262 	locked = 1;
263 
264 	/* Writable? */
265 	if (vp->v_writecount) {
266 		error = ETXTBSY;
267 		goto cleanup;
268 	}
269 
270 	/* Executable? */
271 	error = VOP_GETATTR(vp, &attr, td->td_ucred, td);
272 	if (error)
273 		goto cleanup;
274 
275 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
276 	    ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
277 		/* EACCESS is what exec(2) returns. */
278 		error = ENOEXEC;
279 		goto cleanup;
280 	}
281 
282 	/* Sensible size? */
283 	if (attr.va_size == 0) {
284 		error = ENOEXEC;
285 		goto cleanup;
286 	}
287 
288 	/* Can we access it? */
289 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
290 	if (error)
291 		goto cleanup;
292 
293 	/*
294 	 * XXX: This should use vn_open() so that it is properly authorized,
295 	 * and to reduce code redundancy all over the place here.
296 	 * XXX: Not really, it duplicates far more of exec_check_permissions()
297 	 * than vn_open().
298 	 */
299 #ifdef MAC
300 	error = mac_check_vnode_open(td->td_ucred, vp, FREAD);
301 	if (error)
302 		goto cleanup;
303 #endif
304 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
305 	if (error)
306 		goto cleanup;
307 
308 	/* Pull in executable header into kernel_map */
309 	error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
310 	    VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
311 	if (error)
312 		goto cleanup;
313 
314 	/* Is it a Linux binary ? */
315 	if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
316 		error = ENOEXEC;
317 		goto cleanup;
318 	}
319 
320 	/*
321 	 * While we are here, we should REALLY do some more checks
322 	 */
323 
324 	/* Set file/virtual offset based on a.out variant. */
325 	switch ((int)(a_out->a_magic & 0xffff)) {
326 	case 0413:	/* ZMAGIC */
327 		file_offset = 1024;
328 		break;
329 	case 0314:	/* QMAGIC */
330 		file_offset = 0;
331 		break;
332 	default:
333 		error = ENOEXEC;
334 		goto cleanup;
335 	}
336 
337 	bss_size = round_page(a_out->a_bss);
338 
339 	/* Check various fields in header for validity/bounds. */
340 	if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
341 		error = ENOEXEC;
342 		goto cleanup;
343 	}
344 
345 	/* text + data can't exceed file size */
346 	if (a_out->a_data + a_out->a_text > attr.va_size) {
347 		error = EFAULT;
348 		goto cleanup;
349 	}
350 
351 	/*
352 	 * text/data/bss must not exceed limits
353 	 * XXX - this is not complete. it should check current usage PLUS
354 	 * the resources needed by this library.
355 	 */
356 	PROC_LOCK(td->td_proc);
357 	if (a_out->a_text > maxtsiz ||
358 	    a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) {
359 		PROC_UNLOCK(td->td_proc);
360 		error = ENOMEM;
361 		goto cleanup;
362 	}
363 	PROC_UNLOCK(td->td_proc);
364 
365 	/*
366 	 * Prevent more writers.
367 	 * XXX: Note that if any of the VM operations fail below we don't
368 	 * clear this flag.
369 	 */
370 	vp->v_vflag |= VV_TEXT;
371 
372 	/*
373 	 * Lock no longer needed
374 	 */
375 	locked = 0;
376 	VOP_UNLOCK(vp, 0, td);
377 	VFS_UNLOCK_GIANT(vfslocked);
378 
379 	/*
380 	 * Check if file_offset page aligned. Currently we cannot handle
381 	 * misalinged file offsets, and so we read in the entire image
382 	 * (what a waste).
383 	 */
384 	if (file_offset & PAGE_MASK) {
385 #ifdef DEBUG
386 		printf("uselib: Non page aligned binary %lu\n", file_offset);
387 #endif
388 		/* Map text+data read/write/execute */
389 
390 		/* a_entry is the load address and is page aligned */
391 		vmaddr = trunc_page(a_out->a_entry);
392 
393 		/* get anon user mapping, read+write+execute */
394 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
395 		    &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
396 		    VM_PROT_ALL, 0);
397 		if (error)
398 			goto cleanup;
399 
400 		/* map file into kernel_map */
401 		error = vm_mmap(kernel_map, &buffer,
402 		    round_page(a_out->a_text + a_out->a_data + file_offset),
403 		    VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp,
404 		    trunc_page(file_offset));
405 		if (error)
406 			goto cleanup;
407 
408 		/* copy from kernel VM space to user space */
409 		error = copyout(PTRIN(buffer + file_offset),
410 		    (void *)vmaddr, a_out->a_text + a_out->a_data);
411 
412 		/* release temporary kernel space */
413 		vm_map_remove(kernel_map, buffer, buffer +
414 		    round_page(a_out->a_text + a_out->a_data + file_offset));
415 
416 		if (error)
417 			goto cleanup;
418 	} else {
419 #ifdef DEBUG
420 		printf("uselib: Page aligned binary %lu\n", file_offset);
421 #endif
422 		/*
423 		 * for QMAGIC, a_entry is 20 bytes beyond the load address
424 		 * to skip the executable header
425 		 */
426 		vmaddr = trunc_page(a_out->a_entry);
427 
428 		/*
429 		 * Map it all into the process's space as a single
430 		 * copy-on-write "data" segment.
431 		 */
432 		error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
433 		    a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
434 		    MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
435 		if (error)
436 			goto cleanup;
437 	}
438 #ifdef DEBUG
439 	printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
440 	    ((long*)vmaddr)[1]);
441 #endif
442 	if (bss_size != 0) {
443 		/* Calculate BSS start address */
444 		vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
445 		    a_out->a_data;
446 
447 		/* allocate some 'anon' space */
448 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
449 		    &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
450 		if (error)
451 			goto cleanup;
452 	}
453 
454 cleanup:
455 	/* Unlock vnode if needed */
456 	if (locked) {
457 		VOP_UNLOCK(vp, 0, td);
458 		VFS_UNLOCK_GIANT(vfslocked);
459 	}
460 
461 	/* Release the kernel mapping. */
462 	if (a_out)
463 		vm_map_remove(kernel_map, (vm_offset_t)a_out,
464 		    (vm_offset_t)a_out + PAGE_SIZE);
465 
466 	return error;
467 }
468 
469 #endif	/* __i386__ */
470 
471 int
472 linux_select(struct thread *td, struct linux_select_args *args)
473 {
474 	l_timeval ltv;
475 	struct timeval tv0, tv1, utv, *tvp;
476 	int error;
477 
478 #ifdef DEBUG
479 	if (ldebug(select))
480 		printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
481 		    (void *)args->readfds, (void *)args->writefds,
482 		    (void *)args->exceptfds, (void *)args->timeout);
483 #endif
484 
485 	/*
486 	 * Store current time for computation of the amount of
487 	 * time left.
488 	 */
489 	if (args->timeout) {
490 		if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
491 			goto select_out;
492 		utv.tv_sec = ltv.tv_sec;
493 		utv.tv_usec = ltv.tv_usec;
494 #ifdef DEBUG
495 		if (ldebug(select))
496 			printf(LMSG("incoming timeout (%jd/%ld)"),
497 			    (intmax_t)utv.tv_sec, utv.tv_usec);
498 #endif
499 
500 		if (itimerfix(&utv)) {
501 			/*
502 			 * The timeval was invalid.  Convert it to something
503 			 * valid that will act as it does under Linux.
504 			 */
505 			utv.tv_sec += utv.tv_usec / 1000000;
506 			utv.tv_usec %= 1000000;
507 			if (utv.tv_usec < 0) {
508 				utv.tv_sec -= 1;
509 				utv.tv_usec += 1000000;
510 			}
511 			if (utv.tv_sec < 0)
512 				timevalclear(&utv);
513 		}
514 		microtime(&tv0);
515 		tvp = &utv;
516 	} else
517 		tvp = NULL;
518 
519 	error = kern_select(td, args->nfds, args->readfds, args->writefds,
520 	    args->exceptfds, tvp);
521 
522 #ifdef DEBUG
523 	if (ldebug(select))
524 		printf(LMSG("real select returns %d"), error);
525 #endif
526 	if (error) {
527 		/*
528 		 * See fs/select.c in the Linux kernel.  Without this,
529 		 * Maelstrom doesn't work.
530 		 */
531 		if (error == ERESTART)
532 			error = EINTR;
533 		goto select_out;
534 	}
535 
536 	if (args->timeout) {
537 		if (td->td_retval[0]) {
538 			/*
539 			 * Compute how much time was left of the timeout,
540 			 * by subtracting the current time and the time
541 			 * before we started the call, and subtracting
542 			 * that result from the user-supplied value.
543 			 */
544 			microtime(&tv1);
545 			timevalsub(&tv1, &tv0);
546 			timevalsub(&utv, &tv1);
547 			if (utv.tv_sec < 0)
548 				timevalclear(&utv);
549 		} else
550 			timevalclear(&utv);
551 #ifdef DEBUG
552 		if (ldebug(select))
553 			printf(LMSG("outgoing timeout (%jd/%ld)"),
554 			    (intmax_t)utv.tv_sec, utv.tv_usec);
555 #endif
556 		ltv.tv_sec = utv.tv_sec;
557 		ltv.tv_usec = utv.tv_usec;
558 		if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
559 			goto select_out;
560 	}
561 
562 select_out:
563 #ifdef DEBUG
564 	if (ldebug(select))
565 		printf(LMSG("select_out -> %d"), error);
566 #endif
567 	return error;
568 }
569 
570 int
571 linux_mremap(struct thread *td, struct linux_mremap_args *args)
572 {
573 	struct munmap_args /* {
574 		void *addr;
575 		size_t len;
576 	} */ bsd_args;
577 	int error = 0;
578 
579 #ifdef DEBUG
580 	if (ldebug(mremap))
581 		printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
582 		    (void *)(uintptr_t)args->addr,
583 		    (unsigned long)args->old_len,
584 		    (unsigned long)args->new_len,
585 		    (unsigned long)args->flags);
586 #endif
587 	args->new_len = round_page(args->new_len);
588 	args->old_len = round_page(args->old_len);
589 
590 	if (args->new_len > args->old_len) {
591 		td->td_retval[0] = 0;
592 		return ENOMEM;
593 	}
594 
595 	if (args->new_len < args->old_len) {
596 		bsd_args.addr =
597 		    (caddr_t)((uintptr_t)args->addr + args->new_len);
598 		bsd_args.len = args->old_len - args->new_len;
599 		error = munmap(td, &bsd_args);
600 	}
601 
602 	td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
603 	return error;
604 }
605 
606 #define LINUX_MS_ASYNC       0x0001
607 #define LINUX_MS_INVALIDATE  0x0002
608 #define LINUX_MS_SYNC        0x0004
609 
610 int
611 linux_msync(struct thread *td, struct linux_msync_args *args)
612 {
613 	struct msync_args bsd_args;
614 
615 	bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
616 	bsd_args.len = (uintptr_t)args->len;
617 	bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
618 
619 	return msync(td, &bsd_args);
620 }
621 
622 int
623 linux_time(struct thread *td, struct linux_time_args *args)
624 {
625 	struct timeval tv;
626 	l_time_t tm;
627 	int error;
628 
629 #ifdef DEBUG
630 	if (ldebug(time))
631 		printf(ARGS(time, "*"));
632 #endif
633 
634 	microtime(&tv);
635 	tm = tv.tv_sec;
636 	if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
637 		return error;
638 	td->td_retval[0] = tm;
639 	return 0;
640 }
641 
642 struct l_times_argv {
643 	l_long		tms_utime;
644 	l_long		tms_stime;
645 	l_long		tms_cutime;
646 	l_long		tms_cstime;
647 };
648 
649 #define CLK_TCK 100	/* Linux uses 100 */
650 
651 #define CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
652 
653 int
654 linux_times(struct thread *td, struct linux_times_args *args)
655 {
656 	struct timeval tv, utime, stime, cutime, cstime;
657 	struct l_times_argv tms;
658 	struct proc *p;
659 	int error;
660 
661 #ifdef DEBUG
662 	if (ldebug(times))
663 		printf(ARGS(times, "*"));
664 #endif
665 
666 	if (args->buf != NULL) {
667 		p = td->td_proc;
668 		PROC_LOCK(p);
669 		calcru(p, &utime, &stime);
670 		calccru(p, &cutime, &cstime);
671 		PROC_UNLOCK(p);
672 
673 		tms.tms_utime = CONVTCK(utime);
674 		tms.tms_stime = CONVTCK(stime);
675 
676 		tms.tms_cutime = CONVTCK(cutime);
677 		tms.tms_cstime = CONVTCK(cstime);
678 
679 		if ((error = copyout(&tms, args->buf, sizeof(tms))))
680 			return error;
681 	}
682 
683 	microuptime(&tv);
684 	td->td_retval[0] = (int)CONVTCK(tv);
685 	return 0;
686 }
687 
688 int
689 linux_newuname(struct thread *td, struct linux_newuname_args *args)
690 {
691 	struct l_new_utsname utsname;
692 	char osname[LINUX_MAX_UTSNAME];
693 	char osrelease[LINUX_MAX_UTSNAME];
694 	char *p;
695 
696 #ifdef DEBUG
697 	if (ldebug(newuname))
698 		printf(ARGS(newuname, "*"));
699 #endif
700 
701 	linux_get_osname(td, osname);
702 	linux_get_osrelease(td, osrelease);
703 
704 	bzero(&utsname, sizeof(utsname));
705 	strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
706 	getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
707 	strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
708 	strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
709 	for (p = utsname.version; *p != '\0'; ++p)
710 		if (*p == '\n') {
711 			*p = '\0';
712 			break;
713 		}
714 #ifdef __i386__
715 	{
716 		const char *class;
717 		switch (cpu_class) {
718 		case CPUCLASS_686:
719 			class = "i686";
720 			break;
721 		case CPUCLASS_586:
722 			class = "i586";
723 			break;
724 		case CPUCLASS_486:
725 			class = "i486";
726 			break;
727 		default:
728 			class = "i386";
729 		}
730 		strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME);
731 	}
732 #elif defined(__amd64__)	/* XXX: Linux can change 'personality'. */
733 #ifdef COMPAT_LINUX32
734 	strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
735 #else
736 	strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
737 #endif /* COMPAT_LINUX32 */
738 #else /* something other than i386 or amd64 - assume we and Linux agree */
739 	strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
740 #endif /* __i386__ */
741 	strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
742 
743 	return (copyout(&utsname, args->buf, sizeof(utsname)));
744 }
745 
746 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
747 struct l_utimbuf {
748 	l_time_t l_actime;
749 	l_time_t l_modtime;
750 };
751 
752 int
753 linux_utime(struct thread *td, struct linux_utime_args *args)
754 {
755 	struct timeval tv[2], *tvp;
756 	struct l_utimbuf lut;
757 	char *fname;
758 	int error;
759 
760 	LCONVPATHEXIST(td, args->fname, &fname);
761 
762 #ifdef DEBUG
763 	if (ldebug(utime))
764 		printf(ARGS(utime, "%s, *"), fname);
765 #endif
766 
767 	if (args->times) {
768 		if ((error = copyin(args->times, &lut, sizeof lut))) {
769 			LFREEPATH(fname);
770 			return error;
771 		}
772 		tv[0].tv_sec = lut.l_actime;
773 		tv[0].tv_usec = 0;
774 		tv[1].tv_sec = lut.l_modtime;
775 		tv[1].tv_usec = 0;
776 		tvp = tv;
777 	} else
778 		tvp = NULL;
779 
780 	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
781 	LFREEPATH(fname);
782 	return (error);
783 }
784 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
785 
786 #define __WCLONE 0x80000000
787 
788 int
789 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
790 {
791 	int error, options, tmpstat;
792 
793 #ifdef DEBUG
794 	if (ldebug(waitpid))
795 		printf(ARGS(waitpid, "%d, %p, %d"),
796 		    args->pid, (void *)args->status, args->options);
797 #endif
798 
799 	options = (args->options & (WNOHANG | WUNTRACED));
800 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
801 	if (args->options & __WCLONE)
802 		options |= WLINUXCLONE;
803 
804 	error = kern_wait(td, args->pid, &tmpstat, options, NULL);
805 	if (error)
806 		return error;
807 
808 	if (args->status) {
809 		tmpstat &= 0xffff;
810 		if (WIFSIGNALED(tmpstat))
811 			tmpstat = (tmpstat & 0xffffff80) |
812 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
813 		else if (WIFSTOPPED(tmpstat))
814 			tmpstat = (tmpstat & 0xffff00ff) |
815 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
816 		return copyout(&tmpstat, args->status, sizeof(int));
817 	}
818 
819 	return 0;
820 }
821 
822 int
823 linux_wait4(struct thread *td, struct linux_wait4_args *args)
824 {
825 	int error, options, tmpstat;
826 	struct rusage ru, *rup;
827 	struct proc *p;
828 
829 #ifdef DEBUG
830 	if (ldebug(wait4))
831 		printf(ARGS(wait4, "%d, %p, %d, %p"),
832 		    args->pid, (void *)args->status, args->options,
833 		    (void *)args->rusage);
834 #endif
835 
836 	options = (args->options & (WNOHANG | WUNTRACED));
837 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
838 	if (args->options & __WCLONE)
839 		options |= WLINUXCLONE;
840 
841 	if (args->rusage != NULL)
842 		rup = &ru;
843 	else
844 		rup = NULL;
845 	error = kern_wait(td, args->pid, &tmpstat, options, rup);
846 	if (error)
847 		return error;
848 
849 	p = td->td_proc;
850 	PROC_LOCK(p);
851 	sigqueue_delete(&p->p_sigqueue, SIGCHLD);
852 	PROC_UNLOCK(p);
853 
854 	if (args->status) {
855 		tmpstat &= 0xffff;
856 		if (WIFSIGNALED(tmpstat))
857 			tmpstat = (tmpstat & 0xffffff80) |
858 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
859 		else if (WIFSTOPPED(tmpstat))
860 			tmpstat = (tmpstat & 0xffff00ff) |
861 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
862 		error = copyout(&tmpstat, args->status, sizeof(int));
863 	}
864 	if (args->rusage != NULL && error == 0)
865 		error = copyout(&ru, args->rusage, sizeof(ru));
866 
867 	return (error);
868 }
869 
870 int
871 linux_mknod(struct thread *td, struct linux_mknod_args *args)
872 {
873 	char *path;
874 	int error;
875 
876 	LCONVPATHCREAT(td, args->path, &path);
877 
878 #ifdef DEBUG
879 	if (ldebug(mknod))
880 		printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
881 #endif
882 
883 	if (args->mode & S_IFIFO)
884 		error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
885 	else
886 		error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
887 		    args->dev);
888 	LFREEPATH(path);
889 	return (error);
890 }
891 
892 /*
893  * UGH! This is just about the dumbest idea I've ever heard!!
894  */
895 int
896 linux_personality(struct thread *td, struct linux_personality_args *args)
897 {
898 #ifdef DEBUG
899 	if (ldebug(personality))
900 		printf(ARGS(personality, "%lu"), (unsigned long)args->per);
901 #endif
902 	if (args->per != 0)
903 		return EINVAL;
904 
905 	/* Yes Jim, it's still a Linux... */
906 	td->td_retval[0] = 0;
907 	return 0;
908 }
909 
910 struct l_itimerval {
911 	l_timeval it_interval;
912 	l_timeval it_value;
913 };
914 
915 #define	B2L_ITIMERVAL(bip, lip) 					\
916 	(bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;		\
917 	(bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;	\
918 	(bip)->it_value.tv_sec = (lip)->it_value.tv_sec;		\
919 	(bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
920 
921 int
922 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
923 {
924 	int error;
925 	struct l_itimerval ls;
926 	struct itimerval aitv, oitv;
927 
928 #ifdef DEBUG
929 	if (ldebug(setitimer))
930 		printf(ARGS(setitimer, "%p, %p"),
931 		    (void *)uap->itv, (void *)uap->oitv);
932 #endif
933 
934 	if (uap->itv == NULL) {
935 		uap->itv = uap->oitv;
936 		return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
937 	}
938 
939 	error = copyin(uap->itv, &ls, sizeof(ls));
940 	if (error != 0)
941 		return (error);
942 	B2L_ITIMERVAL(&aitv, &ls);
943 #ifdef DEBUG
944 	if (ldebug(setitimer)) {
945 		printf("setitimer: value: sec: %jd, usec: %ld\n",
946 		    (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
947 		printf("setitimer: interval: sec: %jd, usec: %ld\n",
948 		    (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
949 	}
950 #endif
951 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
952 	if (error != 0 || uap->oitv == NULL)
953 		return (error);
954 	B2L_ITIMERVAL(&ls, &oitv);
955 
956 	return (copyout(&ls, uap->oitv, sizeof(ls)));
957 }
958 
959 int
960 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
961 {
962 	int error;
963 	struct l_itimerval ls;
964 	struct itimerval aitv;
965 
966 #ifdef DEBUG
967 	if (ldebug(getitimer))
968 		printf(ARGS(getitimer, "%p"), (void *)uap->itv);
969 #endif
970 	error = kern_getitimer(td, uap->which, &aitv);
971 	if (error != 0)
972 		return (error);
973 	B2L_ITIMERVAL(&ls, &aitv);
974 	return (copyout(&ls, uap->itv, sizeof(ls)));
975 }
976 
977 int
978 linux_nice(struct thread *td, struct linux_nice_args *args)
979 {
980 	struct setpriority_args	bsd_args;
981 
982 	bsd_args.which = PRIO_PROCESS;
983 	bsd_args.who = 0;	/* current process */
984 	bsd_args.prio = args->inc;
985 	return setpriority(td, &bsd_args);
986 }
987 
988 int
989 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
990 {
991 	struct ucred *newcred, *oldcred;
992 	l_gid_t linux_gidset[NGROUPS];
993 	gid_t *bsd_gidset;
994 	int ngrp, error;
995 	struct proc *p;
996 
997 	ngrp = args->gidsetsize;
998 	if (ngrp < 0 || ngrp >= NGROUPS)
999 		return (EINVAL);
1000 	error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1001 	if (error)
1002 		return (error);
1003 	newcred = crget();
1004 	p = td->td_proc;
1005 	PROC_LOCK(p);
1006 	oldcred = p->p_ucred;
1007 
1008 	/*
1009 	 * cr_groups[0] holds egid. Setting the whole set from
1010 	 * the supplied set will cause egid to be changed too.
1011 	 * Keep cr_groups[0] unchanged to prevent that.
1012 	 */
1013 
1014 	if ((error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
1015 		PROC_UNLOCK(p);
1016 		crfree(newcred);
1017 		return (error);
1018 	}
1019 
1020 	crcopy(newcred, oldcred);
1021 	if (ngrp > 0) {
1022 		newcred->cr_ngroups = ngrp + 1;
1023 
1024 		bsd_gidset = newcred->cr_groups;
1025 		ngrp--;
1026 		while (ngrp >= 0) {
1027 			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1028 			ngrp--;
1029 		}
1030 	}
1031 	else
1032 		newcred->cr_ngroups = 1;
1033 
1034 	setsugid(p);
1035 	p->p_ucred = newcred;
1036 	PROC_UNLOCK(p);
1037 	crfree(oldcred);
1038 	return (0);
1039 }
1040 
1041 int
1042 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1043 {
1044 	struct ucred *cred;
1045 	l_gid_t linux_gidset[NGROUPS];
1046 	gid_t *bsd_gidset;
1047 	int bsd_gidsetsz, ngrp, error;
1048 
1049 	cred = td->td_ucred;
1050 	bsd_gidset = cred->cr_groups;
1051 	bsd_gidsetsz = cred->cr_ngroups - 1;
1052 
1053 	/*
1054 	 * cr_groups[0] holds egid. Returning the whole set
1055 	 * here will cause a duplicate. Exclude cr_groups[0]
1056 	 * to prevent that.
1057 	 */
1058 
1059 	if ((ngrp = args->gidsetsize) == 0) {
1060 		td->td_retval[0] = bsd_gidsetsz;
1061 		return (0);
1062 	}
1063 
1064 	if (ngrp < bsd_gidsetsz)
1065 		return (EINVAL);
1066 
1067 	ngrp = 0;
1068 	while (ngrp < bsd_gidsetsz) {
1069 		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1070 		ngrp++;
1071 	}
1072 
1073 	if ((error = copyout(linux_gidset, args->grouplist,
1074 	    ngrp * sizeof(l_gid_t))))
1075 		return (error);
1076 
1077 	td->td_retval[0] = ngrp;
1078 	return (0);
1079 }
1080 
1081 int
1082 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1083 {
1084 	struct rlimit bsd_rlim;
1085 	struct l_rlimit rlim;
1086 	u_int which;
1087 	int error;
1088 
1089 #ifdef DEBUG
1090 	if (ldebug(setrlimit))
1091 		printf(ARGS(setrlimit, "%d, %p"),
1092 		    args->resource, (void *)args->rlim);
1093 #endif
1094 
1095 	if (args->resource >= LINUX_RLIM_NLIMITS)
1096 		return (EINVAL);
1097 
1098 	which = linux_to_bsd_resource[args->resource];
1099 	if (which == -1)
1100 		return (EINVAL);
1101 
1102 	error = copyin(args->rlim, &rlim, sizeof(rlim));
1103 	if (error)
1104 		return (error);
1105 
1106 	bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1107 	bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1108 	return (kern_setrlimit(td, which, &bsd_rlim));
1109 }
1110 
1111 int
1112 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1113 {
1114 	struct l_rlimit rlim;
1115 	struct proc *p = td->td_proc;
1116 	struct rlimit bsd_rlim;
1117 	u_int which;
1118 
1119 #ifdef DEBUG
1120 	if (ldebug(old_getrlimit))
1121 		printf(ARGS(old_getrlimit, "%d, %p"),
1122 		    args->resource, (void *)args->rlim);
1123 #endif
1124 
1125 	if (args->resource >= LINUX_RLIM_NLIMITS)
1126 		return (EINVAL);
1127 
1128 	which = linux_to_bsd_resource[args->resource];
1129 	if (which == -1)
1130 		return (EINVAL);
1131 
1132 	PROC_LOCK(p);
1133 	lim_rlimit(p, which, &bsd_rlim);
1134 	PROC_UNLOCK(p);
1135 
1136 #ifdef COMPAT_LINUX32
1137 	rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1138 	if (rlim.rlim_cur == UINT_MAX)
1139 		rlim.rlim_cur = INT_MAX;
1140 	rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1141 	if (rlim.rlim_max == UINT_MAX)
1142 		rlim.rlim_max = INT_MAX;
1143 #else
1144 	rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1145 	if (rlim.rlim_cur == ULONG_MAX)
1146 		rlim.rlim_cur = LONG_MAX;
1147 	rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1148 	if (rlim.rlim_max == ULONG_MAX)
1149 		rlim.rlim_max = LONG_MAX;
1150 #endif
1151 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1152 }
1153 
1154 int
1155 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1156 {
1157 	struct l_rlimit rlim;
1158 	struct proc *p = td->td_proc;
1159 	struct rlimit bsd_rlim;
1160 	u_int which;
1161 
1162 #ifdef DEBUG
1163 	if (ldebug(getrlimit))
1164 		printf(ARGS(getrlimit, "%d, %p"),
1165 		    args->resource, (void *)args->rlim);
1166 #endif
1167 
1168 	if (args->resource >= LINUX_RLIM_NLIMITS)
1169 		return (EINVAL);
1170 
1171 	which = linux_to_bsd_resource[args->resource];
1172 	if (which == -1)
1173 		return (EINVAL);
1174 
1175 	PROC_LOCK(p);
1176 	lim_rlimit(p, which, &bsd_rlim);
1177 	PROC_UNLOCK(p);
1178 
1179 	rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1180 	rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1181 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1182 }
1183 
1184 int
1185 linux_sched_setscheduler(struct thread *td,
1186     struct linux_sched_setscheduler_args *args)
1187 {
1188 	struct sched_setscheduler_args bsd;
1189 
1190 #ifdef DEBUG
1191 	if (ldebug(sched_setscheduler))
1192 		printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1193 		    args->pid, args->policy, (const void *)args->param);
1194 #endif
1195 
1196 	switch (args->policy) {
1197 	case LINUX_SCHED_OTHER:
1198 		bsd.policy = SCHED_OTHER;
1199 		break;
1200 	case LINUX_SCHED_FIFO:
1201 		bsd.policy = SCHED_FIFO;
1202 		break;
1203 	case LINUX_SCHED_RR:
1204 		bsd.policy = SCHED_RR;
1205 		break;
1206 	default:
1207 		return EINVAL;
1208 	}
1209 
1210 	bsd.pid = args->pid;
1211 	bsd.param = (struct sched_param *)args->param;
1212 	return sched_setscheduler(td, &bsd);
1213 }
1214 
1215 int
1216 linux_sched_getscheduler(struct thread *td,
1217     struct linux_sched_getscheduler_args *args)
1218 {
1219 	struct sched_getscheduler_args bsd;
1220 	int error;
1221 
1222 #ifdef DEBUG
1223 	if (ldebug(sched_getscheduler))
1224 		printf(ARGS(sched_getscheduler, "%d"), args->pid);
1225 #endif
1226 
1227 	bsd.pid = args->pid;
1228 	error = sched_getscheduler(td, &bsd);
1229 
1230 	switch (td->td_retval[0]) {
1231 	case SCHED_OTHER:
1232 		td->td_retval[0] = LINUX_SCHED_OTHER;
1233 		break;
1234 	case SCHED_FIFO:
1235 		td->td_retval[0] = LINUX_SCHED_FIFO;
1236 		break;
1237 	case SCHED_RR:
1238 		td->td_retval[0] = LINUX_SCHED_RR;
1239 		break;
1240 	}
1241 
1242 	return error;
1243 }
1244 
1245 int
1246 linux_sched_get_priority_max(struct thread *td,
1247     struct linux_sched_get_priority_max_args *args)
1248 {
1249 	struct sched_get_priority_max_args bsd;
1250 
1251 #ifdef DEBUG
1252 	if (ldebug(sched_get_priority_max))
1253 		printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1254 #endif
1255 
1256 	switch (args->policy) {
1257 	case LINUX_SCHED_OTHER:
1258 		bsd.policy = SCHED_OTHER;
1259 		break;
1260 	case LINUX_SCHED_FIFO:
1261 		bsd.policy = SCHED_FIFO;
1262 		break;
1263 	case LINUX_SCHED_RR:
1264 		bsd.policy = SCHED_RR;
1265 		break;
1266 	default:
1267 		return EINVAL;
1268 	}
1269 	return sched_get_priority_max(td, &bsd);
1270 }
1271 
1272 int
1273 linux_sched_get_priority_min(struct thread *td,
1274     struct linux_sched_get_priority_min_args *args)
1275 {
1276 	struct sched_get_priority_min_args bsd;
1277 
1278 #ifdef DEBUG
1279 	if (ldebug(sched_get_priority_min))
1280 		printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1281 #endif
1282 
1283 	switch (args->policy) {
1284 	case LINUX_SCHED_OTHER:
1285 		bsd.policy = SCHED_OTHER;
1286 		break;
1287 	case LINUX_SCHED_FIFO:
1288 		bsd.policy = SCHED_FIFO;
1289 		break;
1290 	case LINUX_SCHED_RR:
1291 		bsd.policy = SCHED_RR;
1292 		break;
1293 	default:
1294 		return EINVAL;
1295 	}
1296 	return sched_get_priority_min(td, &bsd);
1297 }
1298 
1299 #define REBOOT_CAD_ON	0x89abcdef
1300 #define REBOOT_CAD_OFF	0
1301 #define REBOOT_HALT	0xcdef0123
1302 
1303 int
1304 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1305 {
1306 	struct reboot_args bsd_args;
1307 
1308 #ifdef DEBUG
1309 	if (ldebug(reboot))
1310 		printf(ARGS(reboot, "0x%x"), args->cmd);
1311 #endif
1312 	if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1313 		return (0);
1314 	bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1315 	return (reboot(td, &bsd_args));
1316 }
1317 
1318 
1319 /*
1320  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1321  * td->td_retval[1] when COMPAT_43 is defined. This
1322  * globbers registers that are assumed to be preserved. The following
1323  * lightweight syscalls fixes this. See also linux_getgid16() and
1324  * linux_getuid16() in linux_uid16.c.
1325  *
1326  * linux_getpid() - MP SAFE
1327  * linux_getgid() - MP SAFE
1328  * linux_getuid() - MP SAFE
1329  */
1330 
1331 int
1332 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1333 {
1334    	struct linux_emuldata *em;
1335 	char osrel[LINUX_MAX_UTSNAME];
1336 
1337 	linux_get_osrelease(td, osrel);
1338 	if (strlen(osrel) >= 3 && osrel[2] == '6') {
1339    	   	em = em_find(td->td_proc, EMUL_UNLOCKED);
1340 		KASSERT(em != NULL, ("getpid: emuldata not found.\n"));
1341    		td->td_retval[0] = em->shared->group_pid;
1342 		EMUL_UNLOCK(&emul_lock);
1343 	} else {
1344 	   	PROC_LOCK(td->td_proc);
1345    	   	td->td_retval[0] = td->td_proc->p_pid;
1346 	   	PROC_UNLOCK(td->td_proc);
1347 	}
1348 
1349 	return (0);
1350 }
1351 
1352 int
1353 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1354 {
1355 #ifdef DEBUG
1356 	if (ldebug(gettid))
1357 		printf(ARGS(gettid, ""));
1358 #endif
1359 
1360 	td->td_retval[0] = td->td_proc->p_pid;
1361 	return (0);
1362 }
1363 
1364 
1365 int
1366 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1367 {
1368    	struct linux_emuldata *em;
1369 	struct proc *p, *pp;
1370 	char osrel[LINUX_MAX_UTSNAME];
1371 
1372 	linux_get_osrelease(td, osrel);
1373 	if (strlen(osrel) >= 3 && osrel[2] != '6') {
1374 	   	PROC_LOCK(td->td_proc);
1375 	   	td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1376 	   	PROC_UNLOCK(td->td_proc);
1377 		return (0);
1378 	}
1379 
1380 	em = em_find(td->td_proc, EMUL_UNLOCKED);
1381 
1382 	KASSERT(em != NULL, ("getppid: process emuldata not found.\n"));
1383 
1384 	/* find the group leader */
1385 	p = pfind(em->shared->group_pid);
1386 
1387 	if (p == NULL) {
1388 #ifdef DEBUG
1389 	   	printf(LMSG("parent process not found.\n"));
1390 #endif
1391 		return (0);
1392 	}
1393 
1394 	pp = p->p_pptr;		/* switch to parent */
1395 	PROC_LOCK(pp);
1396 	PROC_UNLOCK(p);
1397 
1398 	/* if its also linux process */
1399 	if (pp->p_sysent == &elf_linux_sysvec) {
1400    	   	em = em_find(pp, EMUL_LOCKED);
1401 		KASSERT(em != NULL, ("getppid: parent emuldata not found.\n"));
1402 
1403 		td->td_retval[0] = em->shared->group_pid;
1404 	} else
1405 	   	td->td_retval[0] = pp->p_pid;
1406 
1407 	EMUL_UNLOCK(&emul_lock);
1408 	PROC_UNLOCK(pp);
1409 
1410 	return (0);
1411 }
1412 
1413 int
1414 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1415 {
1416 
1417 	td->td_retval[0] = td->td_ucred->cr_rgid;
1418 	return (0);
1419 }
1420 
1421 int
1422 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1423 {
1424 
1425 	td->td_retval[0] = td->td_ucred->cr_ruid;
1426 	return (0);
1427 }
1428 
1429 
1430 int
1431 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1432 {
1433 	struct getsid_args bsd;
1434 	bsd.pid = args->pid;
1435 	return getsid(td, &bsd);
1436 }
1437 
1438 int
1439 linux_nosys(struct thread *td, struct nosys_args *ignore)
1440 {
1441 
1442 	return (ENOSYS);
1443 }
1444 
1445 int
1446 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1447 {
1448 	struct getpriority_args	bsd_args;
1449 	int error;
1450 
1451 	bsd_args.which = args->which;
1452 	bsd_args.who = args->who;
1453 	error = getpriority(td, &bsd_args);
1454 	td->td_retval[0] = 20 - td->td_retval[0];
1455 	return error;
1456 }
1457 
1458 int
1459 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1460 {
1461 	int name[2];
1462 
1463 	name[0] = CTL_KERN;
1464 	name[1] = KERN_HOSTNAME;
1465 	return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1466 		 args->len, 0, 0));
1467 }
1468 
1469 int
1470 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1471 {
1472    	struct linux_emuldata *em, *td_em, *tmp_em;
1473 	struct proc *sp;
1474 	char osrel[LINUX_MAX_UTSNAME];
1475 
1476 #ifdef DEBUG
1477 	if (ldebug(exit_group))
1478 		printf(ARGS(exit_group, "%i"), args->error_code);
1479 #endif
1480 
1481 	linux_get_osrelease(td, osrel);
1482 	if (strlen(osrel) >= 3 && osrel[2] == '6') {
1483    	   	td_em = em_find(td->td_proc, EMUL_UNLOCKED);
1484 
1485 		KASSERT(td_em != NULL, ("exit_group: emuldata not found.\n"));
1486 
1487    		EMUL_SHARED_RLOCK(&emul_shared_lock);
1488      		LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
1489 	   		if (em->pid == td_em->pid)
1490 		   		continue;
1491 
1492 			sp = pfind(em->pid);
1493 			psignal(sp, SIGKILL);
1494 			PROC_UNLOCK(sp);
1495 #ifdef DEBUG
1496 			printf(LMSG("linux_sys_exit_group: kill PID %d\n"), em->pid);
1497 #endif
1498 		}
1499 
1500 		EMUL_SHARED_RUNLOCK(&emul_shared_lock);
1501 		EMUL_UNLOCK(&emul_lock);
1502 	}
1503 
1504 	exit1(td, W_EXITCODE(args->error_code,0));
1505 
1506    	return (0);
1507 }
1508