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