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