xref: /freebsd/sys/compat/linux/linux_misc.c (revision 1164383c76c6e7e6d701434bbb21764c7ae90a03)
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 
78 #ifdef COMPAT_LINUX32
79 #include <machine/../linux32/linux.h>
80 #include <machine/../linux32/linux32_proto.h>
81 #else
82 #include <machine/../linux/linux.h>
83 #include <machine/../linux/linux_proto.h>
84 #endif
85 
86 #include <compat/linux/linux_mib.h>
87 #include <compat/linux/linux_util.h>
88 
89 #ifdef __i386__
90 #include <machine/cputypes.h>
91 #endif
92 
93 #define BSD_TO_LINUX_SIGNAL(sig)	\
94 	(((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig)
95 
96 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
97 	RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
98 	RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
99 	RLIMIT_MEMLOCK, -1
100 };
101 
102 struct l_sysinfo {
103 	l_long		uptime;		/* Seconds since boot */
104 	l_ulong		loads[3];	/* 1, 5, and 15 minute load averages */
105 #define LINUX_SYSINFO_LOADS_SCALE 65536
106 	l_ulong		totalram;	/* Total usable main memory size */
107 	l_ulong		freeram;	/* Available memory size */
108 	l_ulong		sharedram;	/* Amount of shared memory */
109 	l_ulong		bufferram;	/* Memory used by buffers */
110 	l_ulong		totalswap;	/* Total swap space size */
111 	l_ulong		freeswap;	/* swap space still available */
112 	l_ushort	procs;		/* Number of current processes */
113 	l_ulong		totalbig;
114 	l_ulong		freebig;
115 	l_uint		mem_unit;
116 	char		_f[6];		/* Pads structure to 64 bytes */
117 };
118 int
119 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
120 {
121 	struct l_sysinfo sysinfo;
122 	vm_object_t object;
123 	int i, j;
124 	struct timespec ts;
125 
126 	getnanouptime(&ts);
127 	if (ts.tv_nsec != 0)
128 		ts.tv_sec++;
129 	sysinfo.uptime = ts.tv_sec;
130 
131 	/* Use the information from the mib to get our load averages */
132 	for (i = 0; i < 3; i++)
133 		sysinfo.loads[i] = averunnable.ldavg[i] *
134 		    LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
135 
136 	sysinfo.totalram = physmem * PAGE_SIZE;
137 	sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
138 
139 	sysinfo.sharedram = 0;
140 	mtx_lock(&vm_object_list_mtx);
141 	TAILQ_FOREACH(object, &vm_object_list, object_list)
142 		if (object->shadow_count > 1)
143 			sysinfo.sharedram += object->resident_page_count;
144 	mtx_unlock(&vm_object_list_mtx);
145 
146 	sysinfo.sharedram *= PAGE_SIZE;
147 	sysinfo.bufferram = 0;
148 
149 	swap_pager_status(&i, &j);
150 	sysinfo.totalswap= i * PAGE_SIZE;
151 	sysinfo.freeswap = (i - j) * PAGE_SIZE;
152 
153 	sysinfo.procs = nprocs;
154 
155 	/* The following are only present in newer Linux kernels. */
156 	sysinfo.totalbig = 0;
157 	sysinfo.freebig = 0;
158 	sysinfo.mem_unit = 1;
159 
160 	return copyout(&sysinfo, args->info, sizeof(sysinfo));
161 }
162 
163 int
164 linux_alarm(struct thread *td, struct linux_alarm_args *args)
165 {
166 	struct itimerval it, old_it;
167 	int error;
168 
169 #ifdef DEBUG
170 	if (ldebug(alarm))
171 		printf(ARGS(alarm, "%u"), args->secs);
172 #endif
173 
174 	if (args->secs > 100000000)
175 		return (EINVAL);
176 
177 	it.it_value.tv_sec = (long)args->secs;
178 	it.it_value.tv_usec = 0;
179 	it.it_interval.tv_sec = 0;
180 	it.it_interval.tv_usec = 0;
181 	error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
182 	if (error)
183 		return (error);
184 	if (timevalisset(&old_it.it_value)) {
185 		if (old_it.it_value.tv_usec != 0)
186 			old_it.it_value.tv_sec++;
187 		td->td_retval[0] = old_it.it_value.tv_sec;
188 	}
189 	return (0);
190 }
191 
192 int
193 linux_brk(struct thread *td, struct linux_brk_args *args)
194 {
195 	struct vmspace *vm = td->td_proc->p_vmspace;
196 	vm_offset_t new, old;
197 	struct obreak_args /* {
198 		char * nsize;
199 	} */ tmp;
200 
201 #ifdef DEBUG
202 	if (ldebug(brk))
203 		printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
204 #endif
205 	old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
206 	new = (vm_offset_t)args->dsend;
207 	tmp.nsize = (char *) new;
208 	if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp))
209 		td->td_retval[0] = (long)new;
210 	else
211 		td->td_retval[0] = (long)old;
212 
213 	return 0;
214 }
215 
216 #if defined(__i386__)
217 /* XXX: what about amd64/linux32? */
218 
219 int
220 linux_uselib(struct thread *td, struct linux_uselib_args *args)
221 {
222 	struct nameidata ni;
223 	struct vnode *vp;
224 	struct exec *a_out;
225 	struct vattr attr;
226 	vm_offset_t vmaddr;
227 	unsigned long file_offset;
228 	vm_offset_t buffer;
229 	unsigned long bss_size;
230 	char *library;
231 	int error;
232 	int locked;
233 
234 	LCONVPATHEXIST(td, args->library, &library);
235 
236 #ifdef DEBUG
237 	if (ldebug(uselib))
238 		printf(ARGS(uselib, "%s"), library);
239 #endif
240 
241 	a_out = NULL;
242 	locked = 0;
243 	vp = NULL;
244 
245 	/*
246 	 * XXX: This code should make use of vn_open(), rather than doing
247 	 * all this stuff itself.
248 	 */
249 	NDINIT(&ni, LOOKUP, ISOPEN|FOLLOW|LOCKLEAF, UIO_SYSSPACE, library, td);
250 	error = namei(&ni);
251 	LFREEPATH(library);
252 	if (error)
253 		goto cleanup;
254 
255 	vp = ni.ni_vp;
256 	/*
257 	 * XXX - This looks like a bogus check. A LOCKLEAF namei should not
258 	 * succeed without returning a vnode.
259 	 */
260 	if (vp == NULL) {
261 		error = ENOEXEC;	/* ?? */
262 		goto cleanup;
263 	}
264 	NDFREE(&ni, NDF_ONLY_PNBUF);
265 
266 	/*
267 	 * From here on down, we have a locked vnode that must be unlocked.
268 	 */
269 	locked++;
270 
271 	/* Writable? */
272 	if (vp->v_writecount) {
273 		error = ETXTBSY;
274 		goto cleanup;
275 	}
276 
277 	/* Executable? */
278 	error = VOP_GETATTR(vp, &attr, td->td_ucred, td);
279 	if (error)
280 		goto cleanup;
281 
282 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
283 	    ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
284 		error = ENOEXEC;
285 		goto cleanup;
286 	}
287 
288 	/* Sensible size? */
289 	if (attr.va_size == 0) {
290 		error = ENOEXEC;
291 		goto cleanup;
292 	}
293 
294 	/* Can we access it? */
295 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
296 	if (error)
297 		goto cleanup;
298 
299 	/*
300 	 * XXX: This should use vn_open() so that it is properly authorized,
301 	 * and to reduce code redundancy all over the place here.
302 	 */
303 #ifdef MAC
304 	error = mac_check_vnode_open(td->td_ucred, vp, FREAD);
305 	if (error)
306 		goto cleanup;
307 #endif
308 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
309 	if (error)
310 		goto cleanup;
311 
312 	/* Pull in executable header into kernel_map */
313 	error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
314 	    VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
315 	/*
316 	 * Lock no longer needed
317 	 */
318 	locked = 0;
319 	VOP_UNLOCK(vp, 0, td);
320 
321 	if (error)
322 		goto cleanup;
323 
324 	/* Is it a Linux binary ? */
325 	if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
326 		error = ENOEXEC;
327 		goto cleanup;
328 	}
329 
330 	/*
331 	 * While we are here, we should REALLY do some more checks
332 	 */
333 
334 	/* Set file/virtual offset based on a.out variant. */
335 	switch ((int)(a_out->a_magic & 0xffff)) {
336 	case 0413:	/* ZMAGIC */
337 		file_offset = 1024;
338 		break;
339 	case 0314:	/* QMAGIC */
340 		file_offset = 0;
341 		break;
342 	default:
343 		error = ENOEXEC;
344 		goto cleanup;
345 	}
346 
347 	bss_size = round_page(a_out->a_bss);
348 
349 	/* Check various fields in header for validity/bounds. */
350 	if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
351 		error = ENOEXEC;
352 		goto cleanup;
353 	}
354 
355 	/* text + data can't exceed file size */
356 	if (a_out->a_data + a_out->a_text > attr.va_size) {
357 		error = EFAULT;
358 		goto cleanup;
359 	}
360 
361 	/*
362 	 * text/data/bss must not exceed limits
363 	 * XXX - this is not complete. it should check current usage PLUS
364 	 * the resources needed by this library.
365 	 */
366 	PROC_LOCK(td->td_proc);
367 	if (a_out->a_text > maxtsiz ||
368 	    a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) {
369 		PROC_UNLOCK(td->td_proc);
370 		error = ENOMEM;
371 		goto cleanup;
372 	}
373 	PROC_UNLOCK(td->td_proc);
374 
375 	mp_fixme("Unlocked vflags access.");
376 	/* prevent more writers */
377 	vp->v_vflag |= VV_TEXT;
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 
459 	/* Release the kernel mapping. */
460 	if (a_out)
461 		vm_map_remove(kernel_map, (vm_offset_t)a_out,
462 		    (vm_offset_t)a_out + PAGE_SIZE);
463 
464 	return error;
465 }
466 
467 #endif	/* __i386__ */
468 
469 int
470 linux_select(struct thread *td, struct linux_select_args *args)
471 {
472 	l_timeval ltv;
473 	struct timeval tv0, tv1, utv, *tvp;
474 	int error;
475 
476 #ifdef DEBUG
477 	if (ldebug(select))
478 		printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
479 		    (void *)args->readfds, (void *)args->writefds,
480 		    (void *)args->exceptfds, (void *)args->timeout);
481 #endif
482 
483 	/*
484 	 * Store current time for computation of the amount of
485 	 * time left.
486 	 */
487 	if (args->timeout) {
488 		if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
489 			goto select_out;
490 		utv.tv_sec = ltv.tv_sec;
491 		utv.tv_usec = ltv.tv_usec;
492 #ifdef DEBUG
493 		if (ldebug(select))
494 			printf(LMSG("incoming timeout (%jd/%ld)"),
495 			    (intmax_t)utv.tv_sec, utv.tv_usec);
496 #endif
497 
498 		if (itimerfix(&utv)) {
499 			/*
500 			 * The timeval was invalid.  Convert it to something
501 			 * valid that will act as it does under Linux.
502 			 */
503 			utv.tv_sec += utv.tv_usec / 1000000;
504 			utv.tv_usec %= 1000000;
505 			if (utv.tv_usec < 0) {
506 				utv.tv_sec -= 1;
507 				utv.tv_usec += 1000000;
508 			}
509 			if (utv.tv_sec < 0)
510 				timevalclear(&utv);
511 		}
512 		microtime(&tv0);
513 		tvp = &utv;
514 	} else
515 		tvp = NULL;
516 
517 	error = kern_select(td, args->nfds, args->readfds, args->writefds,
518 	    args->exceptfds, tvp);
519 
520 #ifdef DEBUG
521 	if (ldebug(select))
522 		printf(LMSG("real select returns %d"), error);
523 #endif
524 	if (error) {
525 		/*
526 		 * See fs/select.c in the Linux kernel.  Without this,
527 		 * Maelstrom doesn't work.
528 		 */
529 		if (error == ERESTART)
530 			error = EINTR;
531 		goto select_out;
532 	}
533 
534 	if (args->timeout) {
535 		if (td->td_retval[0]) {
536 			/*
537 			 * Compute how much time was left of the timeout,
538 			 * by subtracting the current time and the time
539 			 * before we started the call, and subtracting
540 			 * that result from the user-supplied value.
541 			 */
542 			microtime(&tv1);
543 			timevalsub(&tv1, &tv0);
544 			timevalsub(&utv, &tv1);
545 			if (utv.tv_sec < 0)
546 				timevalclear(&utv);
547 		} else
548 			timevalclear(&utv);
549 #ifdef DEBUG
550 		if (ldebug(select))
551 			printf(LMSG("outgoing timeout (%jd/%ld)"),
552 			    (intmax_t)utv.tv_sec, utv.tv_usec);
553 #endif
554 		ltv.tv_sec = utv.tv_sec;
555 		ltv.tv_usec = utv.tv_usec;
556 		if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
557 			goto select_out;
558 	}
559 
560 select_out:
561 #ifdef DEBUG
562 	if (ldebug(select))
563 		printf(LMSG("select_out -> %d"), error);
564 #endif
565 	return error;
566 }
567 
568 int
569 linux_mremap(struct thread *td, struct linux_mremap_args *args)
570 {
571 	struct munmap_args /* {
572 		void *addr;
573 		size_t len;
574 	} */ bsd_args;
575 	int error = 0;
576 
577 #ifdef DEBUG
578 	if (ldebug(mremap))
579 		printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
580 		    (void *)(uintptr_t)args->addr,
581 		    (unsigned long)args->old_len,
582 		    (unsigned long)args->new_len,
583 		    (unsigned long)args->flags);
584 #endif
585 	args->new_len = round_page(args->new_len);
586 	args->old_len = round_page(args->old_len);
587 
588 	if (args->new_len > args->old_len) {
589 		td->td_retval[0] = 0;
590 		return ENOMEM;
591 	}
592 
593 	if (args->new_len < args->old_len) {
594 		bsd_args.addr =
595 		    (caddr_t)((uintptr_t)args->addr + args->new_len);
596 		bsd_args.len = args->old_len - args->new_len;
597 		error = munmap(td, &bsd_args);
598 	}
599 
600 	td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
601 	return error;
602 }
603 
604 #define LINUX_MS_ASYNC       0x0001
605 #define LINUX_MS_INVALIDATE  0x0002
606 #define LINUX_MS_SYNC        0x0004
607 
608 int
609 linux_msync(struct thread *td, struct linux_msync_args *args)
610 {
611 	struct msync_args bsd_args;
612 
613 	bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
614 	bsd_args.len = (uintptr_t)args->len;
615 	bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
616 
617 	return msync(td, &bsd_args);
618 }
619 
620 int
621 linux_time(struct thread *td, struct linux_time_args *args)
622 {
623 	struct timeval tv;
624 	l_time_t tm;
625 	int error;
626 
627 #ifdef DEBUG
628 	if (ldebug(time))
629 		printf(ARGS(time, "*"));
630 #endif
631 
632 	microtime(&tv);
633 	tm = tv.tv_sec;
634 	if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
635 		return error;
636 	td->td_retval[0] = tm;
637 	return 0;
638 }
639 
640 struct l_times_argv {
641 	l_long		tms_utime;
642 	l_long		tms_stime;
643 	l_long		tms_cutime;
644 	l_long		tms_cstime;
645 };
646 
647 #define CLK_TCK 100	/* Linux uses 100 */
648 
649 #define CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
650 
651 int
652 linux_times(struct thread *td, struct linux_times_args *args)
653 {
654 	struct timeval tv, utime, stime, cutime, cstime;
655 	struct l_times_argv tms;
656 	struct proc *p;
657 	int error;
658 
659 #ifdef DEBUG
660 	if (ldebug(times))
661 		printf(ARGS(times, "*"));
662 #endif
663 
664 	p = td->td_proc;
665 	PROC_LOCK(p);
666 	calcru(p, &utime, &stime);
667 	calccru(p, &cutime, &cstime);
668 	PROC_UNLOCK(p);
669 
670 	tms.tms_utime = CONVTCK(utime);
671 	tms.tms_stime = CONVTCK(stime);
672 
673 	tms.tms_cutime = CONVTCK(cutime);
674 	tms.tms_cstime = CONVTCK(cstime);
675 
676 	if ((error = copyout(&tms, args->buf, sizeof(tms))))
677 		return error;
678 
679 	microuptime(&tv);
680 	td->td_retval[0] = (int)CONVTCK(tv);
681 	return 0;
682 }
683 
684 int
685 linux_newuname(struct thread *td, struct linux_newuname_args *args)
686 {
687 	struct l_new_utsname utsname;
688 	char osname[LINUX_MAX_UTSNAME];
689 	char osrelease[LINUX_MAX_UTSNAME];
690 	char *p;
691 
692 #ifdef DEBUG
693 	if (ldebug(newuname))
694 		printf(ARGS(newuname, "*"));
695 #endif
696 
697 	linux_get_osname(td, osname);
698 	linux_get_osrelease(td, osrelease);
699 
700 	bzero(&utsname, sizeof(utsname));
701 	strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
702 	getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
703 	strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
704 	strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
705 	for (p = utsname.version; *p != '\0'; ++p)
706 		if (*p == '\n') {
707 			*p = '\0';
708 			break;
709 		}
710 #ifdef __i386__
711 	{
712 		const char *class;
713 		switch (cpu_class) {
714 		case CPUCLASS_686:
715 			class = "i686";
716 			break;
717 		case CPUCLASS_586:
718 			class = "i586";
719 			break;
720 		case CPUCLASS_486:
721 			class = "i486";
722 			break;
723 		default:
724 			class = "i386";
725 		}
726 		strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME);
727 	}
728 #elif defined(__amd64__)	/* XXX: Linux can change 'personality'. */
729 #ifdef COMPAT_LINUX32
730 	strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
731 #else
732 	strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
733 #endif /* COMPAT_LINUX32 */
734 #else /* something other than i386 or amd64 - assume we and Linux agree */
735 	strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
736 #endif /* __i386__ */
737 	strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
738 
739 	return (copyout(&utsname, args->buf, sizeof(utsname)));
740 }
741 
742 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
743 struct l_utimbuf {
744 	l_time_t l_actime;
745 	l_time_t l_modtime;
746 };
747 
748 int
749 linux_utime(struct thread *td, struct linux_utime_args *args)
750 {
751 	struct timeval tv[2], *tvp;
752 	struct l_utimbuf lut;
753 	char *fname;
754 	int error;
755 
756 	LCONVPATHEXIST(td, args->fname, &fname);
757 
758 #ifdef DEBUG
759 	if (ldebug(utime))
760 		printf(ARGS(utime, "%s, *"), fname);
761 #endif
762 
763 	if (args->times) {
764 		if ((error = copyin(args->times, &lut, sizeof lut))) {
765 			LFREEPATH(fname);
766 			return error;
767 		}
768 		tv[0].tv_sec = lut.l_actime;
769 		tv[0].tv_usec = 0;
770 		tv[1].tv_sec = lut.l_modtime;
771 		tv[1].tv_usec = 0;
772 		tvp = tv;
773 	} else
774 		tvp = NULL;
775 
776 	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
777 	LFREEPATH(fname);
778 	return (error);
779 }
780 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
781 
782 #define __WCLONE 0x80000000
783 
784 int
785 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
786 {
787 	int error, options, tmpstat;
788 
789 #ifdef DEBUG
790 	if (ldebug(waitpid))
791 		printf(ARGS(waitpid, "%d, %p, %d"),
792 		    args->pid, (void *)args->status, args->options);
793 #endif
794 
795 	options = (args->options & (WNOHANG | WUNTRACED));
796 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
797 	if (args->options & __WCLONE)
798 		options |= WLINUXCLONE;
799 
800 	error = kern_wait(td, args->pid, &tmpstat, options, NULL);
801 	if (error)
802 		return error;
803 
804 	if (args->status) {
805 		tmpstat &= 0xffff;
806 		if (WIFSIGNALED(tmpstat))
807 			tmpstat = (tmpstat & 0xffffff80) |
808 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
809 		else if (WIFSTOPPED(tmpstat))
810 			tmpstat = (tmpstat & 0xffff00ff) |
811 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
812 		return copyout(&tmpstat, args->status, sizeof(int));
813 	}
814 
815 	return 0;
816 }
817 
818 int
819 linux_wait4(struct thread *td, struct linux_wait4_args *args)
820 {
821 	int error, options, tmpstat;
822 	struct rusage ru, *rup;
823 	struct proc *p;
824 
825 #ifdef DEBUG
826 	if (ldebug(wait4))
827 		printf(ARGS(wait4, "%d, %p, %d, %p"),
828 		    args->pid, (void *)args->status, args->options,
829 		    (void *)args->rusage);
830 #endif
831 
832 	options = (args->options & (WNOHANG | WUNTRACED));
833 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
834 	if (args->options & __WCLONE)
835 		options |= WLINUXCLONE;
836 
837 	if (args->rusage != NULL)
838 		rup = &ru;
839 	else
840 		rup = NULL;
841 	error = kern_wait(td, args->pid, &tmpstat, options, rup);
842 	if (error)
843 		return error;
844 
845 	p = td->td_proc;
846 	PROC_LOCK(p);
847 	sigqueue_delete(&p->p_sigqueue, SIGCHLD);
848 	PROC_UNLOCK(p);
849 
850 	if (args->status) {
851 		tmpstat &= 0xffff;
852 		if (WIFSIGNALED(tmpstat))
853 			tmpstat = (tmpstat & 0xffffff80) |
854 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
855 		else if (WIFSTOPPED(tmpstat))
856 			tmpstat = (tmpstat & 0xffff00ff) |
857 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
858 		error = copyout(&tmpstat, args->status, sizeof(int));
859 	}
860 	if (args->rusage != NULL && error == 0)
861 		error = copyout(&ru, args->rusage, sizeof(ru));
862 
863 	return (error);
864 }
865 
866 int
867 linux_mknod(struct thread *td, struct linux_mknod_args *args)
868 {
869 	char *path;
870 	int error;
871 
872 	LCONVPATHCREAT(td, args->path, &path);
873 
874 #ifdef DEBUG
875 	if (ldebug(mknod))
876 		printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
877 #endif
878 
879 	if (args->mode & S_IFIFO)
880 		error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
881 	else
882 		error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
883 		    args->dev);
884 	LFREEPATH(path);
885 	return (error);
886 }
887 
888 /*
889  * UGH! This is just about the dumbest idea I've ever heard!!
890  */
891 int
892 linux_personality(struct thread *td, struct linux_personality_args *args)
893 {
894 #ifdef DEBUG
895 	if (ldebug(personality))
896 		printf(ARGS(personality, "%lu"), (unsigned long)args->per);
897 #endif
898 	if (args->per != 0)
899 		return EINVAL;
900 
901 	/* Yes Jim, it's still a Linux... */
902 	td->td_retval[0] = 0;
903 	return 0;
904 }
905 
906 struct l_itimerval {
907 	l_timeval it_interval;
908 	l_timeval it_value;
909 };
910 
911 #define	B2L_ITIMERVAL(bip, lip) 					\
912 	(bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;		\
913 	(bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;	\
914 	(bip)->it_value.tv_sec = (lip)->it_value.tv_sec;		\
915 	(bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
916 
917 int
918 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
919 {
920 	int error;
921 	struct l_itimerval ls;
922 	struct itimerval aitv, oitv;
923 
924 #ifdef DEBUG
925 	if (ldebug(setitimer))
926 		printf(ARGS(setitimer, "%p, %p"),
927 		    (void *)uap->itv, (void *)uap->oitv);
928 #endif
929 
930 	if (uap->itv == NULL) {
931 		uap->itv = uap->oitv;
932 		return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
933 	}
934 
935 	error = copyin(uap->itv, &ls, sizeof(ls));
936 	if (error != 0)
937 		return (error);
938 	B2L_ITIMERVAL(&aitv, &ls);
939 #ifdef DEBUG
940 	if (ldebug(setitimer)) {
941 		printf("setitimer: value: sec: %jd, usec: %ld\n",
942 		    (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
943 		printf("setitimer: interval: sec: %jd, usec: %ld\n",
944 		    (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
945 	}
946 #endif
947 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
948 	if (error != 0 || uap->oitv == NULL)
949 		return (error);
950 	B2L_ITIMERVAL(&ls, &oitv);
951 
952 	return (copyout(&ls, uap->oitv, sizeof(ls)));
953 }
954 
955 int
956 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
957 {
958 	int error;
959 	struct l_itimerval ls;
960 	struct itimerval aitv;
961 
962 #ifdef DEBUG
963 	if (ldebug(getitimer))
964 		printf(ARGS(getitimer, "%p"), (void *)uap->itv);
965 #endif
966 	error = kern_getitimer(td, uap->which, &aitv);
967 	if (error != 0)
968 		return (error);
969 	B2L_ITIMERVAL(&ls, &aitv);
970 	return (copyout(&ls, uap->itv, sizeof(ls)));
971 }
972 
973 int
974 linux_nice(struct thread *td, struct linux_nice_args *args)
975 {
976 	struct setpriority_args	bsd_args;
977 
978 	bsd_args.which = PRIO_PROCESS;
979 	bsd_args.who = 0;	/* current process */
980 	bsd_args.prio = args->inc;
981 	return setpriority(td, &bsd_args);
982 }
983 
984 int
985 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
986 {
987 	struct ucred *newcred, *oldcred;
988 	l_gid_t linux_gidset[NGROUPS];
989 	gid_t *bsd_gidset;
990 	int ngrp, error;
991 	struct proc *p;
992 
993 	ngrp = args->gidsetsize;
994 	if (ngrp < 0 || ngrp >= NGROUPS)
995 		return (EINVAL);
996 	error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
997 	if (error)
998 		return (error);
999 	newcred = crget();
1000 	p = td->td_proc;
1001 	PROC_LOCK(p);
1002 	oldcred = p->p_ucred;
1003 
1004 	/*
1005 	 * cr_groups[0] holds egid. Setting the whole set from
1006 	 * the supplied set will cause egid to be changed too.
1007 	 * Keep cr_groups[0] unchanged to prevent that.
1008 	 */
1009 
1010 	if ((error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
1011 		PROC_UNLOCK(p);
1012 		crfree(newcred);
1013 		return (error);
1014 	}
1015 
1016 	crcopy(newcred, oldcred);
1017 	if (ngrp > 0) {
1018 		newcred->cr_ngroups = ngrp + 1;
1019 
1020 		bsd_gidset = newcred->cr_groups;
1021 		ngrp--;
1022 		while (ngrp >= 0) {
1023 			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1024 			ngrp--;
1025 		}
1026 	}
1027 	else
1028 		newcred->cr_ngroups = 1;
1029 
1030 	setsugid(p);
1031 	p->p_ucred = newcred;
1032 	PROC_UNLOCK(p);
1033 	crfree(oldcred);
1034 	return (0);
1035 }
1036 
1037 int
1038 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1039 {
1040 	struct ucred *cred;
1041 	l_gid_t linux_gidset[NGROUPS];
1042 	gid_t *bsd_gidset;
1043 	int bsd_gidsetsz, ngrp, error;
1044 
1045 	cred = td->td_ucred;
1046 	bsd_gidset = cred->cr_groups;
1047 	bsd_gidsetsz = cred->cr_ngroups - 1;
1048 
1049 	/*
1050 	 * cr_groups[0] holds egid. Returning the whole set
1051 	 * here will cause a duplicate. Exclude cr_groups[0]
1052 	 * to prevent that.
1053 	 */
1054 
1055 	if ((ngrp = args->gidsetsize) == 0) {
1056 		td->td_retval[0] = bsd_gidsetsz;
1057 		return (0);
1058 	}
1059 
1060 	if (ngrp < bsd_gidsetsz)
1061 		return (EINVAL);
1062 
1063 	ngrp = 0;
1064 	while (ngrp < bsd_gidsetsz) {
1065 		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1066 		ngrp++;
1067 	}
1068 
1069 	if ((error = copyout(linux_gidset, args->grouplist,
1070 	    ngrp * sizeof(l_gid_t))))
1071 		return (error);
1072 
1073 	td->td_retval[0] = ngrp;
1074 	return (0);
1075 }
1076 
1077 int
1078 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1079 {
1080 	struct rlimit bsd_rlim;
1081 	struct l_rlimit rlim;
1082 	u_int which;
1083 	int error;
1084 
1085 #ifdef DEBUG
1086 	if (ldebug(setrlimit))
1087 		printf(ARGS(setrlimit, "%d, %p"),
1088 		    args->resource, (void *)args->rlim);
1089 #endif
1090 
1091 	if (args->resource >= LINUX_RLIM_NLIMITS)
1092 		return (EINVAL);
1093 
1094 	which = linux_to_bsd_resource[args->resource];
1095 	if (which == -1)
1096 		return (EINVAL);
1097 
1098 	error = copyin(args->rlim, &rlim, sizeof(rlim));
1099 	if (error)
1100 		return (error);
1101 
1102 	bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1103 	bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1104 	return (kern_setrlimit(td, which, &bsd_rlim));
1105 }
1106 
1107 int
1108 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1109 {
1110 	struct l_rlimit rlim;
1111 	struct proc *p = td->td_proc;
1112 	struct rlimit bsd_rlim;
1113 	u_int which;
1114 
1115 #ifdef DEBUG
1116 	if (ldebug(old_getrlimit))
1117 		printf(ARGS(old_getrlimit, "%d, %p"),
1118 		    args->resource, (void *)args->rlim);
1119 #endif
1120 
1121 	if (args->resource >= LINUX_RLIM_NLIMITS)
1122 		return (EINVAL);
1123 
1124 	which = linux_to_bsd_resource[args->resource];
1125 	if (which == -1)
1126 		return (EINVAL);
1127 
1128 	PROC_LOCK(p);
1129 	lim_rlimit(p, which, &bsd_rlim);
1130 	PROC_UNLOCK(p);
1131 
1132 #ifdef COMPAT_LINUX32
1133 	rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1134 	if (rlim.rlim_cur == UINT_MAX)
1135 		rlim.rlim_cur = INT_MAX;
1136 	rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1137 	if (rlim.rlim_max == UINT_MAX)
1138 		rlim.rlim_max = INT_MAX;
1139 #else
1140 	rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1141 	if (rlim.rlim_cur == ULONG_MAX)
1142 		rlim.rlim_cur = LONG_MAX;
1143 	rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1144 	if (rlim.rlim_max == ULONG_MAX)
1145 		rlim.rlim_max = LONG_MAX;
1146 #endif
1147 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1148 }
1149 
1150 int
1151 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1152 {
1153 	struct l_rlimit rlim;
1154 	struct proc *p = td->td_proc;
1155 	struct rlimit bsd_rlim;
1156 	u_int which;
1157 
1158 #ifdef DEBUG
1159 	if (ldebug(getrlimit))
1160 		printf(ARGS(getrlimit, "%d, %p"),
1161 		    args->resource, (void *)args->rlim);
1162 #endif
1163 
1164 	if (args->resource >= LINUX_RLIM_NLIMITS)
1165 		return (EINVAL);
1166 
1167 	which = linux_to_bsd_resource[args->resource];
1168 	if (which == -1)
1169 		return (EINVAL);
1170 
1171 	PROC_LOCK(p);
1172 	lim_rlimit(p, which, &bsd_rlim);
1173 	PROC_UNLOCK(p);
1174 
1175 	rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1176 	rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1177 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1178 }
1179 
1180 int
1181 linux_sched_setscheduler(struct thread *td,
1182     struct linux_sched_setscheduler_args *args)
1183 {
1184 	struct sched_setscheduler_args bsd;
1185 
1186 #ifdef DEBUG
1187 	if (ldebug(sched_setscheduler))
1188 		printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1189 		    args->pid, args->policy, (const void *)args->param);
1190 #endif
1191 
1192 	switch (args->policy) {
1193 	case LINUX_SCHED_OTHER:
1194 		bsd.policy = SCHED_OTHER;
1195 		break;
1196 	case LINUX_SCHED_FIFO:
1197 		bsd.policy = SCHED_FIFO;
1198 		break;
1199 	case LINUX_SCHED_RR:
1200 		bsd.policy = SCHED_RR;
1201 		break;
1202 	default:
1203 		return EINVAL;
1204 	}
1205 
1206 	bsd.pid = args->pid;
1207 	bsd.param = (struct sched_param *)args->param;
1208 	return sched_setscheduler(td, &bsd);
1209 }
1210 
1211 int
1212 linux_sched_getscheduler(struct thread *td,
1213     struct linux_sched_getscheduler_args *args)
1214 {
1215 	struct sched_getscheduler_args bsd;
1216 	int error;
1217 
1218 #ifdef DEBUG
1219 	if (ldebug(sched_getscheduler))
1220 		printf(ARGS(sched_getscheduler, "%d"), args->pid);
1221 #endif
1222 
1223 	bsd.pid = args->pid;
1224 	error = sched_getscheduler(td, &bsd);
1225 
1226 	switch (td->td_retval[0]) {
1227 	case SCHED_OTHER:
1228 		td->td_retval[0] = LINUX_SCHED_OTHER;
1229 		break;
1230 	case SCHED_FIFO:
1231 		td->td_retval[0] = LINUX_SCHED_FIFO;
1232 		break;
1233 	case SCHED_RR:
1234 		td->td_retval[0] = LINUX_SCHED_RR;
1235 		break;
1236 	}
1237 
1238 	return error;
1239 }
1240 
1241 int
1242 linux_sched_get_priority_max(struct thread *td,
1243     struct linux_sched_get_priority_max_args *args)
1244 {
1245 	struct sched_get_priority_max_args bsd;
1246 
1247 #ifdef DEBUG
1248 	if (ldebug(sched_get_priority_max))
1249 		printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1250 #endif
1251 
1252 	switch (args->policy) {
1253 	case LINUX_SCHED_OTHER:
1254 		bsd.policy = SCHED_OTHER;
1255 		break;
1256 	case LINUX_SCHED_FIFO:
1257 		bsd.policy = SCHED_FIFO;
1258 		break;
1259 	case LINUX_SCHED_RR:
1260 		bsd.policy = SCHED_RR;
1261 		break;
1262 	default:
1263 		return EINVAL;
1264 	}
1265 	return sched_get_priority_max(td, &bsd);
1266 }
1267 
1268 int
1269 linux_sched_get_priority_min(struct thread *td,
1270     struct linux_sched_get_priority_min_args *args)
1271 {
1272 	struct sched_get_priority_min_args bsd;
1273 
1274 #ifdef DEBUG
1275 	if (ldebug(sched_get_priority_min))
1276 		printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1277 #endif
1278 
1279 	switch (args->policy) {
1280 	case LINUX_SCHED_OTHER:
1281 		bsd.policy = SCHED_OTHER;
1282 		break;
1283 	case LINUX_SCHED_FIFO:
1284 		bsd.policy = SCHED_FIFO;
1285 		break;
1286 	case LINUX_SCHED_RR:
1287 		bsd.policy = SCHED_RR;
1288 		break;
1289 	default:
1290 		return EINVAL;
1291 	}
1292 	return sched_get_priority_min(td, &bsd);
1293 }
1294 
1295 #define REBOOT_CAD_ON	0x89abcdef
1296 #define REBOOT_CAD_OFF	0
1297 #define REBOOT_HALT	0xcdef0123
1298 
1299 int
1300 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1301 {
1302 	struct reboot_args bsd_args;
1303 
1304 #ifdef DEBUG
1305 	if (ldebug(reboot))
1306 		printf(ARGS(reboot, "0x%x"), args->cmd);
1307 #endif
1308 	if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1309 		return (0);
1310 	bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1311 	return (reboot(td, &bsd_args));
1312 }
1313 
1314 
1315 /*
1316  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1317  * td->td_retval[1] when COMPAT_43 is defined. This
1318  * globbers registers that are assumed to be preserved. The following
1319  * lightweight syscalls fixes this. See also linux_getgid16() and
1320  * linux_getuid16() in linux_uid16.c.
1321  *
1322  * linux_getpid() - MP SAFE
1323  * linux_getgid() - MP SAFE
1324  * linux_getuid() - MP SAFE
1325  */
1326 
1327 int
1328 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1329 {
1330 
1331 	td->td_retval[0] = td->td_proc->p_pid;
1332 	return (0);
1333 }
1334 
1335 int
1336 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1337 {
1338 
1339 	td->td_retval[0] = td->td_ucred->cr_rgid;
1340 	return (0);
1341 }
1342 
1343 int
1344 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1345 {
1346 
1347 	td->td_retval[0] = td->td_ucred->cr_ruid;
1348 	return (0);
1349 }
1350 
1351 
1352 int
1353 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1354 {
1355 	struct getsid_args bsd;
1356 	bsd.pid = args->pid;
1357 	return getsid(td, &bsd);
1358 }
1359 
1360 int
1361 linux_nosys(struct thread *td, struct nosys_args *ignore)
1362 {
1363 
1364 	return (ENOSYS);
1365 }
1366 
1367 int
1368 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1369 {
1370 	struct getpriority_args	bsd_args;
1371 	int error;
1372 
1373 	bsd_args.which = args->which;
1374 	bsd_args.who = args->who;
1375 	error = getpriority(td, &bsd_args);
1376 	td->td_retval[0] = 20 - td->td_retval[0];
1377 	return error;
1378 }
1379 
1380 int
1381 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1382 {
1383 	int name[2];
1384 	int error;
1385 
1386 	name[0] = CTL_KERN;
1387 	name[1] = KERN_HOSTNAME;
1388 	if ((error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL)))
1389 		return (error);
1390 	return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1391 		 args->len, 0, 0));
1392 }
1393 
1394