xref: /freebsd/sys/compat/linux/linux_misc.c (revision 69f1cb3c91c3377cedc28a9fe37673bda10602cd)
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, vfslocked;
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 	vfslocked = 0;
243 	locked = 0;
244 	vp = NULL;
245 
246 	NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1,
247 	    UIO_SYSSPACE, library, td);
248 	error = namei(&ni);
249 	LFREEPATH(library);
250 	if (error)
251 		goto cleanup;
252 
253 	vp = ni.ni_vp;
254 	vfslocked = NDHASGIANT(&ni);
255 	NDFREE(&ni, NDF_ONLY_PNBUF);
256 
257 	/*
258 	 * From here on down, we have a locked vnode that must be unlocked.
259 	 * XXX: The code below largely duplicates exec_check_permissions().
260 	 */
261 	locked = 1;
262 
263 	/* Writable? */
264 	if (vp->v_writecount) {
265 		error = ETXTBSY;
266 		goto cleanup;
267 	}
268 
269 	/* Executable? */
270 	error = VOP_GETATTR(vp, &attr, td->td_ucred, td);
271 	if (error)
272 		goto cleanup;
273 
274 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
275 	    ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
276 		/* EACCESS is what exec(2) returns. */
277 		error = ENOEXEC;
278 		goto cleanup;
279 	}
280 
281 	/* Sensible size? */
282 	if (attr.va_size == 0) {
283 		error = ENOEXEC;
284 		goto cleanup;
285 	}
286 
287 	/* Can we access it? */
288 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
289 	if (error)
290 		goto cleanup;
291 
292 	/*
293 	 * XXX: This should use vn_open() so that it is properly authorized,
294 	 * and to reduce code redundancy all over the place here.
295 	 * XXX: Not really, it duplicates far more of exec_check_permissions()
296 	 * than vn_open().
297 	 */
298 #ifdef MAC
299 	error = mac_check_vnode_open(td->td_ucred, vp, FREAD);
300 	if (error)
301 		goto cleanup;
302 #endif
303 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td, -1);
304 	if (error)
305 		goto cleanup;
306 
307 	/* Pull in executable header into kernel_map */
308 	error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE,
309 	    VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
310 	if (error)
311 		goto cleanup;
312 
313 	/* Is it a Linux binary ? */
314 	if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
315 		error = ENOEXEC;
316 		goto cleanup;
317 	}
318 
319 	/*
320 	 * While we are here, we should REALLY do some more checks
321 	 */
322 
323 	/* Set file/virtual offset based on a.out variant. */
324 	switch ((int)(a_out->a_magic & 0xffff)) {
325 	case 0413:	/* ZMAGIC */
326 		file_offset = 1024;
327 		break;
328 	case 0314:	/* QMAGIC */
329 		file_offset = 0;
330 		break;
331 	default:
332 		error = ENOEXEC;
333 		goto cleanup;
334 	}
335 
336 	bss_size = round_page(a_out->a_bss);
337 
338 	/* Check various fields in header for validity/bounds. */
339 	if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
340 		error = ENOEXEC;
341 		goto cleanup;
342 	}
343 
344 	/* text + data can't exceed file size */
345 	if (a_out->a_data + a_out->a_text > attr.va_size) {
346 		error = EFAULT;
347 		goto cleanup;
348 	}
349 
350 	/*
351 	 * text/data/bss must not exceed limits
352 	 * XXX - this is not complete. it should check current usage PLUS
353 	 * the resources needed by this library.
354 	 */
355 	PROC_LOCK(td->td_proc);
356 	if (a_out->a_text > maxtsiz ||
357 	    a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) {
358 		PROC_UNLOCK(td->td_proc);
359 		error = ENOMEM;
360 		goto cleanup;
361 	}
362 	PROC_UNLOCK(td->td_proc);
363 
364 	/*
365 	 * Prevent more writers.
366 	 * XXX: Note that if any of the VM operations fail below we don't
367 	 * clear this flag.
368 	 */
369 	vp->v_vflag |= VV_TEXT;
370 
371 	/*
372 	 * Lock no longer needed
373 	 */
374 	locked = 0;
375 	VOP_UNLOCK(vp, 0, td);
376 	VFS_UNLOCK_GIANT(vfslocked);
377 
378 	/*
379 	 * Check if file_offset page aligned. Currently we cannot handle
380 	 * misalinged file offsets, and so we read in the entire image
381 	 * (what a waste).
382 	 */
383 	if (file_offset & PAGE_MASK) {
384 #ifdef DEBUG
385 		printf("uselib: Non page aligned binary %lu\n", file_offset);
386 #endif
387 		/* Map text+data read/write/execute */
388 
389 		/* a_entry is the load address and is page aligned */
390 		vmaddr = trunc_page(a_out->a_entry);
391 
392 		/* get anon user mapping, read+write+execute */
393 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
394 		    &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL,
395 		    VM_PROT_ALL, 0);
396 		if (error)
397 			goto cleanup;
398 
399 		/* map file into kernel_map */
400 		error = vm_mmap(kernel_map, &buffer,
401 		    round_page(a_out->a_text + a_out->a_data + file_offset),
402 		    VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp,
403 		    trunc_page(file_offset));
404 		if (error)
405 			goto cleanup;
406 
407 		/* copy from kernel VM space to user space */
408 		error = copyout(PTRIN(buffer + file_offset),
409 		    (void *)vmaddr, a_out->a_text + a_out->a_data);
410 
411 		/* release temporary kernel space */
412 		vm_map_remove(kernel_map, buffer, buffer +
413 		    round_page(a_out->a_text + a_out->a_data + file_offset));
414 
415 		if (error)
416 			goto cleanup;
417 	} else {
418 #ifdef DEBUG
419 		printf("uselib: Page aligned binary %lu\n", file_offset);
420 #endif
421 		/*
422 		 * for QMAGIC, a_entry is 20 bytes beyond the load address
423 		 * to skip the executable header
424 		 */
425 		vmaddr = trunc_page(a_out->a_entry);
426 
427 		/*
428 		 * Map it all into the process's space as a single
429 		 * copy-on-write "data" segment.
430 		 */
431 		error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
432 		    a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
433 		    MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
434 		if (error)
435 			goto cleanup;
436 	}
437 #ifdef DEBUG
438 	printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long*)vmaddr)[0],
439 	    ((long*)vmaddr)[1]);
440 #endif
441 	if (bss_size != 0) {
442 		/* Calculate BSS start address */
443 		vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
444 		    a_out->a_data;
445 
446 		/* allocate some 'anon' space */
447 		error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
448 		    &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
449 		if (error)
450 			goto cleanup;
451 	}
452 
453 cleanup:
454 	/* Unlock vnode if needed */
455 	if (locked) {
456 		VOP_UNLOCK(vp, 0, td);
457 		VFS_UNLOCK_GIANT(vfslocked);
458 	}
459 
460 	/* Release the kernel mapping. */
461 	if (a_out)
462 		vm_map_remove(kernel_map, (vm_offset_t)a_out,
463 		    (vm_offset_t)a_out + PAGE_SIZE);
464 
465 	return error;
466 }
467 
468 #endif	/* __i386__ */
469 
470 int
471 linux_select(struct thread *td, struct linux_select_args *args)
472 {
473 	l_timeval ltv;
474 	struct timeval tv0, tv1, utv, *tvp;
475 	int error;
476 
477 #ifdef DEBUG
478 	if (ldebug(select))
479 		printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
480 		    (void *)args->readfds, (void *)args->writefds,
481 		    (void *)args->exceptfds, (void *)args->timeout);
482 #endif
483 
484 	/*
485 	 * Store current time for computation of the amount of
486 	 * time left.
487 	 */
488 	if (args->timeout) {
489 		if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
490 			goto select_out;
491 		utv.tv_sec = ltv.tv_sec;
492 		utv.tv_usec = ltv.tv_usec;
493 #ifdef DEBUG
494 		if (ldebug(select))
495 			printf(LMSG("incoming timeout (%jd/%ld)"),
496 			    (intmax_t)utv.tv_sec, utv.tv_usec);
497 #endif
498 
499 		if (itimerfix(&utv)) {
500 			/*
501 			 * The timeval was invalid.  Convert it to something
502 			 * valid that will act as it does under Linux.
503 			 */
504 			utv.tv_sec += utv.tv_usec / 1000000;
505 			utv.tv_usec %= 1000000;
506 			if (utv.tv_usec < 0) {
507 				utv.tv_sec -= 1;
508 				utv.tv_usec += 1000000;
509 			}
510 			if (utv.tv_sec < 0)
511 				timevalclear(&utv);
512 		}
513 		microtime(&tv0);
514 		tvp = &utv;
515 	} else
516 		tvp = NULL;
517 
518 	error = kern_select(td, args->nfds, args->readfds, args->writefds,
519 	    args->exceptfds, tvp);
520 
521 #ifdef DEBUG
522 	if (ldebug(select))
523 		printf(LMSG("real select returns %d"), error);
524 #endif
525 	if (error) {
526 		/*
527 		 * See fs/select.c in the Linux kernel.  Without this,
528 		 * Maelstrom doesn't work.
529 		 */
530 		if (error == ERESTART)
531 			error = EINTR;
532 		goto select_out;
533 	}
534 
535 	if (args->timeout) {
536 		if (td->td_retval[0]) {
537 			/*
538 			 * Compute how much time was left of the timeout,
539 			 * by subtracting the current time and the time
540 			 * before we started the call, and subtracting
541 			 * that result from the user-supplied value.
542 			 */
543 			microtime(&tv1);
544 			timevalsub(&tv1, &tv0);
545 			timevalsub(&utv, &tv1);
546 			if (utv.tv_sec < 0)
547 				timevalclear(&utv);
548 		} else
549 			timevalclear(&utv);
550 #ifdef DEBUG
551 		if (ldebug(select))
552 			printf(LMSG("outgoing timeout (%jd/%ld)"),
553 			    (intmax_t)utv.tv_sec, utv.tv_usec);
554 #endif
555 		ltv.tv_sec = utv.tv_sec;
556 		ltv.tv_usec = utv.tv_usec;
557 		if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
558 			goto select_out;
559 	}
560 
561 select_out:
562 #ifdef DEBUG
563 	if (ldebug(select))
564 		printf(LMSG("select_out -> %d"), error);
565 #endif
566 	return error;
567 }
568 
569 int
570 linux_mremap(struct thread *td, struct linux_mremap_args *args)
571 {
572 	struct munmap_args /* {
573 		void *addr;
574 		size_t len;
575 	} */ bsd_args;
576 	int error = 0;
577 
578 #ifdef DEBUG
579 	if (ldebug(mremap))
580 		printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
581 		    (void *)(uintptr_t)args->addr,
582 		    (unsigned long)args->old_len,
583 		    (unsigned long)args->new_len,
584 		    (unsigned long)args->flags);
585 #endif
586 	args->new_len = round_page(args->new_len);
587 	args->old_len = round_page(args->old_len);
588 
589 	if (args->new_len > args->old_len) {
590 		td->td_retval[0] = 0;
591 		return ENOMEM;
592 	}
593 
594 	if (args->new_len < args->old_len) {
595 		bsd_args.addr =
596 		    (caddr_t)((uintptr_t)args->addr + args->new_len);
597 		bsd_args.len = args->old_len - args->new_len;
598 		error = munmap(td, &bsd_args);
599 	}
600 
601 	td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
602 	return error;
603 }
604 
605 #define LINUX_MS_ASYNC       0x0001
606 #define LINUX_MS_INVALIDATE  0x0002
607 #define LINUX_MS_SYNC        0x0004
608 
609 int
610 linux_msync(struct thread *td, struct linux_msync_args *args)
611 {
612 	struct msync_args bsd_args;
613 
614 	bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
615 	bsd_args.len = (uintptr_t)args->len;
616 	bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
617 
618 	return msync(td, &bsd_args);
619 }
620 
621 int
622 linux_time(struct thread *td, struct linux_time_args *args)
623 {
624 	struct timeval tv;
625 	l_time_t tm;
626 	int error;
627 
628 #ifdef DEBUG
629 	if (ldebug(time))
630 		printf(ARGS(time, "*"));
631 #endif
632 
633 	microtime(&tv);
634 	tm = tv.tv_sec;
635 	if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
636 		return error;
637 	td->td_retval[0] = tm;
638 	return 0;
639 }
640 
641 struct l_times_argv {
642 	l_long		tms_utime;
643 	l_long		tms_stime;
644 	l_long		tms_cutime;
645 	l_long		tms_cstime;
646 };
647 
648 #define CLK_TCK 100	/* Linux uses 100 */
649 
650 #define CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
651 
652 int
653 linux_times(struct thread *td, struct linux_times_args *args)
654 {
655 	struct timeval tv, utime, stime, cutime, cstime;
656 	struct l_times_argv tms;
657 	struct proc *p;
658 	int error;
659 
660 #ifdef DEBUG
661 	if (ldebug(times))
662 		printf(ARGS(times, "*"));
663 #endif
664 
665 	if (args->buf != NULL) {
666 		p = td->td_proc;
667 		PROC_LOCK(p);
668 		calcru(p, &utime, &stime);
669 		calccru(p, &cutime, &cstime);
670 		PROC_UNLOCK(p);
671 
672 		tms.tms_utime = CONVTCK(utime);
673 		tms.tms_stime = CONVTCK(stime);
674 
675 		tms.tms_cutime = CONVTCK(cutime);
676 		tms.tms_cstime = CONVTCK(cstime);
677 
678 		if ((error = copyout(&tms, args->buf, sizeof(tms))))
679 			return error;
680 	}
681 
682 	microuptime(&tv);
683 	td->td_retval[0] = (int)CONVTCK(tv);
684 	return 0;
685 }
686 
687 int
688 linux_newuname(struct thread *td, struct linux_newuname_args *args)
689 {
690 	struct l_new_utsname utsname;
691 	char osname[LINUX_MAX_UTSNAME];
692 	char osrelease[LINUX_MAX_UTSNAME];
693 	char *p;
694 
695 #ifdef DEBUG
696 	if (ldebug(newuname))
697 		printf(ARGS(newuname, "*"));
698 #endif
699 
700 	linux_get_osname(td, osname);
701 	linux_get_osrelease(td, osrelease);
702 
703 	bzero(&utsname, sizeof(utsname));
704 	strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
705 	getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
706 	strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
707 	strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
708 	for (p = utsname.version; *p != '\0'; ++p)
709 		if (*p == '\n') {
710 			*p = '\0';
711 			break;
712 		}
713 #ifdef __i386__
714 	{
715 		const char *class;
716 		switch (cpu_class) {
717 		case CPUCLASS_686:
718 			class = "i686";
719 			break;
720 		case CPUCLASS_586:
721 			class = "i586";
722 			break;
723 		case CPUCLASS_486:
724 			class = "i486";
725 			break;
726 		default:
727 			class = "i386";
728 		}
729 		strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME);
730 	}
731 #elif defined(__amd64__)	/* XXX: Linux can change 'personality'. */
732 #ifdef COMPAT_LINUX32
733 	strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
734 #else
735 	strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
736 #endif /* COMPAT_LINUX32 */
737 #else /* something other than i386 or amd64 - assume we and Linux agree */
738 	strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME);
739 #endif /* __i386__ */
740 	strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME);
741 
742 	return (copyout(&utsname, args->buf, sizeof(utsname)));
743 }
744 
745 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
746 struct l_utimbuf {
747 	l_time_t l_actime;
748 	l_time_t l_modtime;
749 };
750 
751 int
752 linux_utime(struct thread *td, struct linux_utime_args *args)
753 {
754 	struct timeval tv[2], *tvp;
755 	struct l_utimbuf lut;
756 	char *fname;
757 	int error;
758 
759 	LCONVPATHEXIST(td, args->fname, &fname);
760 
761 #ifdef DEBUG
762 	if (ldebug(utime))
763 		printf(ARGS(utime, "%s, *"), fname);
764 #endif
765 
766 	if (args->times) {
767 		if ((error = copyin(args->times, &lut, sizeof lut))) {
768 			LFREEPATH(fname);
769 			return error;
770 		}
771 		tv[0].tv_sec = lut.l_actime;
772 		tv[0].tv_usec = 0;
773 		tv[1].tv_sec = lut.l_modtime;
774 		tv[1].tv_usec = 0;
775 		tvp = tv;
776 	} else
777 		tvp = NULL;
778 
779 	error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
780 	LFREEPATH(fname);
781 	return (error);
782 }
783 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
784 
785 #define __WCLONE 0x80000000
786 
787 int
788 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
789 {
790 	int error, options, tmpstat;
791 
792 #ifdef DEBUG
793 	if (ldebug(waitpid))
794 		printf(ARGS(waitpid, "%d, %p, %d"),
795 		    args->pid, (void *)args->status, args->options);
796 #endif
797 
798 	options = (args->options & (WNOHANG | WUNTRACED));
799 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
800 	if (args->options & __WCLONE)
801 		options |= WLINUXCLONE;
802 
803 	error = kern_wait(td, args->pid, &tmpstat, options, NULL);
804 	if (error)
805 		return error;
806 
807 	if (args->status) {
808 		tmpstat &= 0xffff;
809 		if (WIFSIGNALED(tmpstat))
810 			tmpstat = (tmpstat & 0xffffff80) |
811 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
812 		else if (WIFSTOPPED(tmpstat))
813 			tmpstat = (tmpstat & 0xffff00ff) |
814 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
815 		return copyout(&tmpstat, args->status, sizeof(int));
816 	}
817 
818 	return 0;
819 }
820 
821 int
822 linux_wait4(struct thread *td, struct linux_wait4_args *args)
823 {
824 	int error, options, tmpstat;
825 	struct rusage ru, *rup;
826 	struct proc *p;
827 
828 #ifdef DEBUG
829 	if (ldebug(wait4))
830 		printf(ARGS(wait4, "%d, %p, %d, %p"),
831 		    args->pid, (void *)args->status, args->options,
832 		    (void *)args->rusage);
833 #endif
834 
835 	options = (args->options & (WNOHANG | WUNTRACED));
836 	/* WLINUXCLONE should be equal to __WCLONE, but we make sure */
837 	if (args->options & __WCLONE)
838 		options |= WLINUXCLONE;
839 
840 	if (args->rusage != NULL)
841 		rup = &ru;
842 	else
843 		rup = NULL;
844 	error = kern_wait(td, args->pid, &tmpstat, options, rup);
845 	if (error)
846 		return error;
847 
848 	p = td->td_proc;
849 	PROC_LOCK(p);
850 	sigqueue_delete(&p->p_sigqueue, SIGCHLD);
851 	PROC_UNLOCK(p);
852 
853 	if (args->status) {
854 		tmpstat &= 0xffff;
855 		if (WIFSIGNALED(tmpstat))
856 			tmpstat = (tmpstat & 0xffffff80) |
857 			    BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
858 		else if (WIFSTOPPED(tmpstat))
859 			tmpstat = (tmpstat & 0xffff00ff) |
860 			    (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
861 		error = copyout(&tmpstat, args->status, sizeof(int));
862 	}
863 	if (args->rusage != NULL && error == 0)
864 		error = copyout(&ru, args->rusage, sizeof(ru));
865 
866 	return (error);
867 }
868 
869 int
870 linux_mknod(struct thread *td, struct linux_mknod_args *args)
871 {
872 	char *path;
873 	int error;
874 
875 	LCONVPATHCREAT(td, args->path, &path);
876 
877 #ifdef DEBUG
878 	if (ldebug(mknod))
879 		printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
880 #endif
881 
882 	if (args->mode & S_IFIFO)
883 		error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
884 	else
885 		error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
886 		    args->dev);
887 	LFREEPATH(path);
888 	return (error);
889 }
890 
891 /*
892  * UGH! This is just about the dumbest idea I've ever heard!!
893  */
894 int
895 linux_personality(struct thread *td, struct linux_personality_args *args)
896 {
897 #ifdef DEBUG
898 	if (ldebug(personality))
899 		printf(ARGS(personality, "%lu"), (unsigned long)args->per);
900 #endif
901 	if (args->per != 0)
902 		return EINVAL;
903 
904 	/* Yes Jim, it's still a Linux... */
905 	td->td_retval[0] = 0;
906 	return 0;
907 }
908 
909 struct l_itimerval {
910 	l_timeval it_interval;
911 	l_timeval it_value;
912 };
913 
914 #define	B2L_ITIMERVAL(bip, lip) 					\
915 	(bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;		\
916 	(bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;	\
917 	(bip)->it_value.tv_sec = (lip)->it_value.tv_sec;		\
918 	(bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
919 
920 int
921 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
922 {
923 	int error;
924 	struct l_itimerval ls;
925 	struct itimerval aitv, oitv;
926 
927 #ifdef DEBUG
928 	if (ldebug(setitimer))
929 		printf(ARGS(setitimer, "%p, %p"),
930 		    (void *)uap->itv, (void *)uap->oitv);
931 #endif
932 
933 	if (uap->itv == NULL) {
934 		uap->itv = uap->oitv;
935 		return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
936 	}
937 
938 	error = copyin(uap->itv, &ls, sizeof(ls));
939 	if (error != 0)
940 		return (error);
941 	B2L_ITIMERVAL(&aitv, &ls);
942 #ifdef DEBUG
943 	if (ldebug(setitimer)) {
944 		printf("setitimer: value: sec: %jd, usec: %ld\n",
945 		    (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
946 		printf("setitimer: interval: sec: %jd, usec: %ld\n",
947 		    (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
948 	}
949 #endif
950 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
951 	if (error != 0 || uap->oitv == NULL)
952 		return (error);
953 	B2L_ITIMERVAL(&ls, &oitv);
954 
955 	return (copyout(&ls, uap->oitv, sizeof(ls)));
956 }
957 
958 int
959 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
960 {
961 	int error;
962 	struct l_itimerval ls;
963 	struct itimerval aitv;
964 
965 #ifdef DEBUG
966 	if (ldebug(getitimer))
967 		printf(ARGS(getitimer, "%p"), (void *)uap->itv);
968 #endif
969 	error = kern_getitimer(td, uap->which, &aitv);
970 	if (error != 0)
971 		return (error);
972 	B2L_ITIMERVAL(&ls, &aitv);
973 	return (copyout(&ls, uap->itv, sizeof(ls)));
974 }
975 
976 int
977 linux_nice(struct thread *td, struct linux_nice_args *args)
978 {
979 	struct setpriority_args	bsd_args;
980 
981 	bsd_args.which = PRIO_PROCESS;
982 	bsd_args.who = 0;	/* current process */
983 	bsd_args.prio = args->inc;
984 	return setpriority(td, &bsd_args);
985 }
986 
987 int
988 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
989 {
990 	struct ucred *newcred, *oldcred;
991 	l_gid_t linux_gidset[NGROUPS];
992 	gid_t *bsd_gidset;
993 	int ngrp, error;
994 	struct proc *p;
995 
996 	ngrp = args->gidsetsize;
997 	if (ngrp < 0 || ngrp >= NGROUPS)
998 		return (EINVAL);
999 	error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1000 	if (error)
1001 		return (error);
1002 	newcred = crget();
1003 	p = td->td_proc;
1004 	PROC_LOCK(p);
1005 	oldcred = p->p_ucred;
1006 
1007 	/*
1008 	 * cr_groups[0] holds egid. Setting the whole set from
1009 	 * the supplied set will cause egid to be changed too.
1010 	 * Keep cr_groups[0] unchanged to prevent that.
1011 	 */
1012 
1013 	if ((error = suser_cred(oldcred, SUSER_ALLOWJAIL)) != 0) {
1014 		PROC_UNLOCK(p);
1015 		crfree(newcred);
1016 		return (error);
1017 	}
1018 
1019 	crcopy(newcred, oldcred);
1020 	if (ngrp > 0) {
1021 		newcred->cr_ngroups = ngrp + 1;
1022 
1023 		bsd_gidset = newcred->cr_groups;
1024 		ngrp--;
1025 		while (ngrp >= 0) {
1026 			bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1027 			ngrp--;
1028 		}
1029 	}
1030 	else
1031 		newcred->cr_ngroups = 1;
1032 
1033 	setsugid(p);
1034 	p->p_ucred = newcred;
1035 	PROC_UNLOCK(p);
1036 	crfree(oldcred);
1037 	return (0);
1038 }
1039 
1040 int
1041 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1042 {
1043 	struct ucred *cred;
1044 	l_gid_t linux_gidset[NGROUPS];
1045 	gid_t *bsd_gidset;
1046 	int bsd_gidsetsz, ngrp, error;
1047 
1048 	cred = td->td_ucred;
1049 	bsd_gidset = cred->cr_groups;
1050 	bsd_gidsetsz = cred->cr_ngroups - 1;
1051 
1052 	/*
1053 	 * cr_groups[0] holds egid. Returning the whole set
1054 	 * here will cause a duplicate. Exclude cr_groups[0]
1055 	 * to prevent that.
1056 	 */
1057 
1058 	if ((ngrp = args->gidsetsize) == 0) {
1059 		td->td_retval[0] = bsd_gidsetsz;
1060 		return (0);
1061 	}
1062 
1063 	if (ngrp < bsd_gidsetsz)
1064 		return (EINVAL);
1065 
1066 	ngrp = 0;
1067 	while (ngrp < bsd_gidsetsz) {
1068 		linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1069 		ngrp++;
1070 	}
1071 
1072 	if ((error = copyout(linux_gidset, args->grouplist,
1073 	    ngrp * sizeof(l_gid_t))))
1074 		return (error);
1075 
1076 	td->td_retval[0] = ngrp;
1077 	return (0);
1078 }
1079 
1080 int
1081 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1082 {
1083 	struct rlimit bsd_rlim;
1084 	struct l_rlimit rlim;
1085 	u_int which;
1086 	int error;
1087 
1088 #ifdef DEBUG
1089 	if (ldebug(setrlimit))
1090 		printf(ARGS(setrlimit, "%d, %p"),
1091 		    args->resource, (void *)args->rlim);
1092 #endif
1093 
1094 	if (args->resource >= LINUX_RLIM_NLIMITS)
1095 		return (EINVAL);
1096 
1097 	which = linux_to_bsd_resource[args->resource];
1098 	if (which == -1)
1099 		return (EINVAL);
1100 
1101 	error = copyin(args->rlim, &rlim, sizeof(rlim));
1102 	if (error)
1103 		return (error);
1104 
1105 	bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1106 	bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1107 	return (kern_setrlimit(td, which, &bsd_rlim));
1108 }
1109 
1110 int
1111 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1112 {
1113 	struct l_rlimit rlim;
1114 	struct proc *p = td->td_proc;
1115 	struct rlimit bsd_rlim;
1116 	u_int which;
1117 
1118 #ifdef DEBUG
1119 	if (ldebug(old_getrlimit))
1120 		printf(ARGS(old_getrlimit, "%d, %p"),
1121 		    args->resource, (void *)args->rlim);
1122 #endif
1123 
1124 	if (args->resource >= LINUX_RLIM_NLIMITS)
1125 		return (EINVAL);
1126 
1127 	which = linux_to_bsd_resource[args->resource];
1128 	if (which == -1)
1129 		return (EINVAL);
1130 
1131 	PROC_LOCK(p);
1132 	lim_rlimit(p, which, &bsd_rlim);
1133 	PROC_UNLOCK(p);
1134 
1135 #ifdef COMPAT_LINUX32
1136 	rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1137 	if (rlim.rlim_cur == UINT_MAX)
1138 		rlim.rlim_cur = INT_MAX;
1139 	rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1140 	if (rlim.rlim_max == UINT_MAX)
1141 		rlim.rlim_max = INT_MAX;
1142 #else
1143 	rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1144 	if (rlim.rlim_cur == ULONG_MAX)
1145 		rlim.rlim_cur = LONG_MAX;
1146 	rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1147 	if (rlim.rlim_max == ULONG_MAX)
1148 		rlim.rlim_max = LONG_MAX;
1149 #endif
1150 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1151 }
1152 
1153 int
1154 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1155 {
1156 	struct l_rlimit rlim;
1157 	struct proc *p = td->td_proc;
1158 	struct rlimit bsd_rlim;
1159 	u_int which;
1160 
1161 #ifdef DEBUG
1162 	if (ldebug(getrlimit))
1163 		printf(ARGS(getrlimit, "%d, %p"),
1164 		    args->resource, (void *)args->rlim);
1165 #endif
1166 
1167 	if (args->resource >= LINUX_RLIM_NLIMITS)
1168 		return (EINVAL);
1169 
1170 	which = linux_to_bsd_resource[args->resource];
1171 	if (which == -1)
1172 		return (EINVAL);
1173 
1174 	PROC_LOCK(p);
1175 	lim_rlimit(p, which, &bsd_rlim);
1176 	PROC_UNLOCK(p);
1177 
1178 	rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1179 	rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1180 	return (copyout(&rlim, args->rlim, sizeof(rlim)));
1181 }
1182 
1183 int
1184 linux_sched_setscheduler(struct thread *td,
1185     struct linux_sched_setscheduler_args *args)
1186 {
1187 	struct sched_setscheduler_args bsd;
1188 
1189 #ifdef DEBUG
1190 	if (ldebug(sched_setscheduler))
1191 		printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1192 		    args->pid, args->policy, (const void *)args->param);
1193 #endif
1194 
1195 	switch (args->policy) {
1196 	case LINUX_SCHED_OTHER:
1197 		bsd.policy = SCHED_OTHER;
1198 		break;
1199 	case LINUX_SCHED_FIFO:
1200 		bsd.policy = SCHED_FIFO;
1201 		break;
1202 	case LINUX_SCHED_RR:
1203 		bsd.policy = SCHED_RR;
1204 		break;
1205 	default:
1206 		return EINVAL;
1207 	}
1208 
1209 	bsd.pid = args->pid;
1210 	bsd.param = (struct sched_param *)args->param;
1211 	return sched_setscheduler(td, &bsd);
1212 }
1213 
1214 int
1215 linux_sched_getscheduler(struct thread *td,
1216     struct linux_sched_getscheduler_args *args)
1217 {
1218 	struct sched_getscheduler_args bsd;
1219 	int error;
1220 
1221 #ifdef DEBUG
1222 	if (ldebug(sched_getscheduler))
1223 		printf(ARGS(sched_getscheduler, "%d"), args->pid);
1224 #endif
1225 
1226 	bsd.pid = args->pid;
1227 	error = sched_getscheduler(td, &bsd);
1228 
1229 	switch (td->td_retval[0]) {
1230 	case SCHED_OTHER:
1231 		td->td_retval[0] = LINUX_SCHED_OTHER;
1232 		break;
1233 	case SCHED_FIFO:
1234 		td->td_retval[0] = LINUX_SCHED_FIFO;
1235 		break;
1236 	case SCHED_RR:
1237 		td->td_retval[0] = LINUX_SCHED_RR;
1238 		break;
1239 	}
1240 
1241 	return error;
1242 }
1243 
1244 int
1245 linux_sched_get_priority_max(struct thread *td,
1246     struct linux_sched_get_priority_max_args *args)
1247 {
1248 	struct sched_get_priority_max_args bsd;
1249 
1250 #ifdef DEBUG
1251 	if (ldebug(sched_get_priority_max))
1252 		printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1253 #endif
1254 
1255 	switch (args->policy) {
1256 	case LINUX_SCHED_OTHER:
1257 		bsd.policy = SCHED_OTHER;
1258 		break;
1259 	case LINUX_SCHED_FIFO:
1260 		bsd.policy = SCHED_FIFO;
1261 		break;
1262 	case LINUX_SCHED_RR:
1263 		bsd.policy = SCHED_RR;
1264 		break;
1265 	default:
1266 		return EINVAL;
1267 	}
1268 	return sched_get_priority_max(td, &bsd);
1269 }
1270 
1271 int
1272 linux_sched_get_priority_min(struct thread *td,
1273     struct linux_sched_get_priority_min_args *args)
1274 {
1275 	struct sched_get_priority_min_args bsd;
1276 
1277 #ifdef DEBUG
1278 	if (ldebug(sched_get_priority_min))
1279 		printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1280 #endif
1281 
1282 	switch (args->policy) {
1283 	case LINUX_SCHED_OTHER:
1284 		bsd.policy = SCHED_OTHER;
1285 		break;
1286 	case LINUX_SCHED_FIFO:
1287 		bsd.policy = SCHED_FIFO;
1288 		break;
1289 	case LINUX_SCHED_RR:
1290 		bsd.policy = SCHED_RR;
1291 		break;
1292 	default:
1293 		return EINVAL;
1294 	}
1295 	return sched_get_priority_min(td, &bsd);
1296 }
1297 
1298 #define REBOOT_CAD_ON	0x89abcdef
1299 #define REBOOT_CAD_OFF	0
1300 #define REBOOT_HALT	0xcdef0123
1301 
1302 int
1303 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1304 {
1305 	struct reboot_args bsd_args;
1306 
1307 #ifdef DEBUG
1308 	if (ldebug(reboot))
1309 		printf(ARGS(reboot, "0x%x"), args->cmd);
1310 #endif
1311 	if (args->cmd == REBOOT_CAD_ON || args->cmd == REBOOT_CAD_OFF)
1312 		return (0);
1313 	bsd_args.opt = (args->cmd == REBOOT_HALT) ? RB_HALT : 0;
1314 	return (reboot(td, &bsd_args));
1315 }
1316 
1317 
1318 /*
1319  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1320  * td->td_retval[1] when COMPAT_43 is defined. This
1321  * globbers registers that are assumed to be preserved. The following
1322  * lightweight syscalls fixes this. See also linux_getgid16() and
1323  * linux_getuid16() in linux_uid16.c.
1324  *
1325  * linux_getpid() - MP SAFE
1326  * linux_getgid() - MP SAFE
1327  * linux_getuid() - MP SAFE
1328  */
1329 
1330 int
1331 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1332 {
1333 
1334 	td->td_retval[0] = td->td_proc->p_pid;
1335 	return (0);
1336 }
1337 
1338 int
1339 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1340 {
1341 
1342 	td->td_retval[0] = td->td_ucred->cr_rgid;
1343 	return (0);
1344 }
1345 
1346 int
1347 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1348 {
1349 
1350 	td->td_retval[0] = td->td_ucred->cr_ruid;
1351 	return (0);
1352 }
1353 
1354 
1355 int
1356 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1357 {
1358 	struct getsid_args bsd;
1359 	bsd.pid = args->pid;
1360 	return getsid(td, &bsd);
1361 }
1362 
1363 int
1364 linux_nosys(struct thread *td, struct nosys_args *ignore)
1365 {
1366 
1367 	return (ENOSYS);
1368 }
1369 
1370 int
1371 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1372 {
1373 	struct getpriority_args	bsd_args;
1374 	int error;
1375 
1376 	bsd_args.which = args->which;
1377 	bsd_args.who = args->who;
1378 	error = getpriority(td, &bsd_args);
1379 	td->td_retval[0] = 20 - td->td_retval[0];
1380 	return error;
1381 }
1382 
1383 int
1384 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1385 {
1386 	int name[2];
1387 	int error;
1388 
1389 	name[0] = CTL_KERN;
1390 	name[1] = KERN_HOSTNAME;
1391 	if ((error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL)))
1392 		return (error);
1393 	return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1394 		 args->len, 0, 0));
1395 }
1396 
1397