]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linux/linux_misc.c
MFC r358673 by tijl:
[FreeBSD/FreeBSD.git] / sys / compat / linux / linux_misc.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 Doug Rabson
5  * Copyright (c) 1994-1995 Søren Schmidt
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer
13  *    in this position and unchanged.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_compat.h"
36
37 #include <sys/param.h>
38 #include <sys/blist.h>
39 #include <sys/fcntl.h>
40 #if defined(__i386__)
41 #include <sys/imgact_aout.h>
42 #endif
43 #include <sys/jail.h>
44 #include <sys/kernel.h>
45 #include <sys/limits.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mman.h>
49 #include <sys/mount.h>
50 #include <sys/msgbuf.h>
51 #include <sys/mutex.h>
52 #include <sys/namei.h>
53 #include <sys/priv.h>
54 #include <sys/proc.h>
55 #include <sys/procctl.h>
56 #include <sys/reboot.h>
57 #include <sys/racct.h>
58 #include <sys/random.h>
59 #include <sys/resourcevar.h>
60 #include <sys/sched.h>
61 #include <sys/sdt.h>
62 #include <sys/signalvar.h>
63 #include <sys/stat.h>
64 #include <sys/syscallsubr.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysproto.h>
67 #include <sys/systm.h>
68 #include <sys/time.h>
69 #include <sys/vmmeter.h>
70 #include <sys/vnode.h>
71 #include <sys/wait.h>
72 #include <sys/cpuset.h>
73 #include <sys/uio.h>
74
75 #include <security/mac/mac_framework.h>
76
77 #include <vm/vm.h>
78 #include <vm/pmap.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_extern.h>
82 #include <vm/swap_pager.h>
83
84 #ifdef COMPAT_LINUX32
85 #include <machine/../linux32/linux.h>
86 #include <machine/../linux32/linux32_proto.h>
87 #else
88 #include <machine/../linux/linux.h>
89 #include <machine/../linux/linux_proto.h>
90 #endif
91
92 #include <compat/linux/linux_dtrace.h>
93 #include <compat/linux/linux_file.h>
94 #include <compat/linux/linux_mib.h>
95 #include <compat/linux/linux_signal.h>
96 #include <compat/linux/linux_timer.h>
97 #include <compat/linux/linux_util.h>
98 #include <compat/linux/linux_sysproto.h>
99 #include <compat/linux/linux_emul.h>
100 #include <compat/linux/linux_misc.h>
101
102 /**
103  * Special DTrace provider for the linuxulator.
104  *
105  * In this file we define the provider for the entire linuxulator. All
106  * modules (= files of the linuxulator) use it.
107  *
108  * We define a different name depending on the emulated bitsize, see
109  * ../../<ARCH>/linux{,32}/linux.h, e.g.:
110  *      native bitsize          = linuxulator
111  *      amd64, 32bit emulation  = linuxulator32
112  */
113 LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE);
114
115 int stclohz;                            /* Statistics clock frequency */
116
117 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
118         RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
119         RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
120         RLIMIT_MEMLOCK, RLIMIT_AS
121 };
122
123 struct l_sysinfo {
124         l_long          uptime;         /* Seconds since boot */
125         l_ulong         loads[3];       /* 1, 5, and 15 minute load averages */
126 #define LINUX_SYSINFO_LOADS_SCALE 65536
127         l_ulong         totalram;       /* Total usable main memory size */
128         l_ulong         freeram;        /* Available memory size */
129         l_ulong         sharedram;      /* Amount of shared memory */
130         l_ulong         bufferram;      /* Memory used by buffers */
131         l_ulong         totalswap;      /* Total swap space size */
132         l_ulong         freeswap;       /* swap space still available */
133         l_ushort        procs;          /* Number of current processes */
134         l_ushort        pads;
135         l_ulong         totalhigh;
136         l_ulong         freehigh;
137         l_uint          mem_unit;
138         char            _f[20-2*sizeof(l_long)-sizeof(l_int)];  /* padding */
139 };
140
141 struct l_pselect6arg {
142         l_uintptr_t     ss;
143         l_size_t        ss_len;
144 };
145
146 static int      linux_utimensat_nsec_valid(l_long);
147
148
149 int
150 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
151 {
152         struct l_sysinfo sysinfo;
153         int i, j;
154         struct timespec ts;
155
156         bzero(&sysinfo, sizeof(sysinfo));
157         getnanouptime(&ts);
158         if (ts.tv_nsec != 0)
159                 ts.tv_sec++;
160         sysinfo.uptime = ts.tv_sec;
161
162         /* Use the information from the mib to get our load averages */
163         for (i = 0; i < 3; i++)
164                 sysinfo.loads[i] = averunnable.ldavg[i] *
165                     LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
166
167         sysinfo.totalram = physmem * PAGE_SIZE;
168         sysinfo.freeram = (u_long)vm_free_count() * PAGE_SIZE;
169
170         /*
171          * sharedram counts pages allocated to named, swap-backed objects such
172          * as shared memory segments and tmpfs files.  There is no cheap way to
173          * compute this, so just leave the field unpopulated.  Linux itself only
174          * started setting this field in the 3.x timeframe.
175          */
176         sysinfo.sharedram = 0;
177         sysinfo.bufferram = 0;
178
179         swap_pager_status(&i, &j);
180         sysinfo.totalswap = i * PAGE_SIZE;
181         sysinfo.freeswap = (i - j) * PAGE_SIZE;
182
183         sysinfo.procs = nprocs;
184
185         /*
186          * Platforms supported by the emulation layer do not have a notion of
187          * high memory.
188          */
189         sysinfo.totalhigh = 0;
190         sysinfo.freehigh = 0;
191
192         sysinfo.mem_unit = 1;
193
194         return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
195 }
196
197 #ifdef LINUX_LEGACY_SYSCALLS
198 int
199 linux_alarm(struct thread *td, struct linux_alarm_args *args)
200 {
201         struct itimerval it, old_it;
202         u_int secs;
203         int error;
204
205 #ifdef DEBUG
206         if (ldebug(alarm))
207                 printf(ARGS(alarm, "%u"), args->secs);
208 #endif
209         secs = args->secs;
210         /*
211          * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
212          * to match kern_setitimer()'s limit to avoid error from it.
213          *
214          * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
215          * platforms.
216          */
217         if (secs > INT32_MAX / 2)
218                 secs = INT32_MAX / 2;
219
220         it.it_value.tv_sec = secs;
221         it.it_value.tv_usec = 0;
222         timevalclear(&it.it_interval);
223         error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
224         KASSERT(error == 0, ("kern_setitimer returns %d", error));
225
226         if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
227             old_it.it_value.tv_usec >= 500000)
228                 old_it.it_value.tv_sec++;
229         td->td_retval[0] = old_it.it_value.tv_sec;
230         return (0);
231 }
232 #endif
233
234 int
235 linux_brk(struct thread *td, struct linux_brk_args *args)
236 {
237         struct vmspace *vm = td->td_proc->p_vmspace;
238         uintptr_t new, old;
239
240 #ifdef DEBUG
241         if (ldebug(brk))
242                 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
243 #endif
244         old = (uintptr_t)vm->vm_daddr + ctob(vm->vm_dsize);
245         new = (uintptr_t)args->dsend;
246         if ((caddr_t)new > vm->vm_daddr && !kern_break(td, &new))
247                 td->td_retval[0] = (register_t)new;
248         else
249                 td->td_retval[0] = (register_t)old;
250
251         return (0);
252 }
253
254 #if defined(__i386__)
255 /* XXX: what about amd64/linux32? */
256
257 int
258 linux_uselib(struct thread *td, struct linux_uselib_args *args)
259 {
260         struct nameidata ni;
261         struct vnode *vp;
262         struct exec *a_out;
263         vm_map_t map;
264         vm_map_entry_t entry;
265         struct vattr attr;
266         vm_offset_t vmaddr;
267         unsigned long file_offset;
268         unsigned long bss_size;
269         char *library;
270         ssize_t aresid;
271         int error;
272         bool locked, opened, textset;
273
274         LCONVPATHEXIST(td, args->library, &library);
275
276 #ifdef DEBUG
277         if (ldebug(uselib))
278                 printf(ARGS(uselib, "%s"), library);
279 #endif
280
281         a_out = NULL;
282         vp = NULL;
283         locked = false;
284         textset = false;
285         opened = false;
286
287         NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
288             UIO_SYSSPACE, library, td);
289         error = namei(&ni);
290         LFREEPATH(library);
291         if (error)
292                 goto cleanup;
293
294         vp = ni.ni_vp;
295         NDFREE(&ni, NDF_ONLY_PNBUF);
296
297         /*
298          * From here on down, we have a locked vnode that must be unlocked.
299          * XXX: The code below largely duplicates exec_check_permissions().
300          */
301         locked = true;
302
303         /* Executable? */
304         error = VOP_GETATTR(vp, &attr, td->td_ucred);
305         if (error)
306                 goto cleanup;
307
308         if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
309             ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
310                 /* EACCESS is what exec(2) returns. */
311                 error = ENOEXEC;
312                 goto cleanup;
313         }
314
315         /* Sensible size? */
316         if (attr.va_size == 0) {
317                 error = ENOEXEC;
318                 goto cleanup;
319         }
320
321         /* Can we access it? */
322         error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
323         if (error)
324                 goto cleanup;
325
326         /*
327          * XXX: This should use vn_open() so that it is properly authorized,
328          * and to reduce code redundancy all over the place here.
329          * XXX: Not really, it duplicates far more of exec_check_permissions()
330          * than vn_open().
331          */
332 #ifdef MAC
333         error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
334         if (error)
335                 goto cleanup;
336 #endif
337         error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
338         if (error)
339                 goto cleanup;
340         opened = true;
341
342         /* Pull in executable header into exec_map */
343         error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
344             VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
345         if (error)
346                 goto cleanup;
347
348         /* Is it a Linux binary ? */
349         if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
350                 error = ENOEXEC;
351                 goto cleanup;
352         }
353
354         /*
355          * While we are here, we should REALLY do some more checks
356          */
357
358         /* Set file/virtual offset based on a.out variant. */
359         switch ((int)(a_out->a_magic & 0xffff)) {
360         case 0413:                      /* ZMAGIC */
361                 file_offset = 1024;
362                 break;
363         case 0314:                      /* QMAGIC */
364                 file_offset = 0;
365                 break;
366         default:
367                 error = ENOEXEC;
368                 goto cleanup;
369         }
370
371         bss_size = round_page(a_out->a_bss);
372
373         /* Check various fields in header for validity/bounds. */
374         if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
375                 error = ENOEXEC;
376                 goto cleanup;
377         }
378
379         /* text + data can't exceed file size */
380         if (a_out->a_data + a_out->a_text > attr.va_size) {
381                 error = EFAULT;
382                 goto cleanup;
383         }
384
385         /*
386          * text/data/bss must not exceed limits
387          * XXX - this is not complete. it should check current usage PLUS
388          * the resources needed by this library.
389          */
390         PROC_LOCK(td->td_proc);
391         if (a_out->a_text > maxtsiz ||
392             a_out->a_data + bss_size > lim_cur_proc(td->td_proc, RLIMIT_DATA) ||
393             racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
394             bss_size) != 0) {
395                 PROC_UNLOCK(td->td_proc);
396                 error = ENOMEM;
397                 goto cleanup;
398         }
399         PROC_UNLOCK(td->td_proc);
400
401         /*
402          * Prevent more writers.
403          */
404         error = VOP_SET_TEXT(vp);
405         if (error != 0)
406                 goto cleanup;
407         textset = true;
408
409         /*
410          * Lock no longer needed
411          */
412         locked = false;
413         VOP_UNLOCK(vp, 0);
414
415         /*
416          * Check if file_offset page aligned. Currently we cannot handle
417          * misalinged file offsets, and so we read in the entire image
418          * (what a waste).
419          */
420         if (file_offset & PAGE_MASK) {
421 #ifdef DEBUG
422                 printf("uselib: Non page aligned binary %lu\n", file_offset);
423 #endif
424                 /* Map text+data read/write/execute */
425
426                 /* a_entry is the load address and is page aligned */
427                 vmaddr = trunc_page(a_out->a_entry);
428
429                 /* get anon user mapping, read+write+execute */
430                 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
431                     &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
432                     VM_PROT_ALL, VM_PROT_ALL, 0);
433                 if (error)
434                         goto cleanup;
435
436                 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
437                     a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
438                     td->td_ucred, NOCRED, &aresid, td);
439                 if (error != 0)
440                         goto cleanup;
441                 if (aresid != 0) {
442                         error = ENOEXEC;
443                         goto cleanup;
444                 }
445         } else {
446 #ifdef DEBUG
447                 printf("uselib: Page aligned binary %lu\n", file_offset);
448 #endif
449                 /*
450                  * for QMAGIC, a_entry is 20 bytes beyond the load address
451                  * to skip the executable header
452                  */
453                 vmaddr = trunc_page(a_out->a_entry);
454
455                 /*
456                  * Map it all into the process's space as a single
457                  * copy-on-write "data" segment.
458                  */
459                 map = &td->td_proc->p_vmspace->vm_map;
460                 error = vm_mmap(map, &vmaddr,
461                     a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
462                     MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
463                 if (error)
464                         goto cleanup;
465                 vm_map_lock(map);
466                 if (!vm_map_lookup_entry(map, vmaddr, &entry)) {
467                         vm_map_unlock(map);
468                         error = EDOOFUS;
469                         goto cleanup;
470                 }
471                 entry->eflags |= MAP_ENTRY_VN_EXEC;
472                 vm_map_unlock(map);
473                 textset = false;
474         }
475 #ifdef DEBUG
476         printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
477             ((long *)vmaddr)[1]);
478 #endif
479         if (bss_size != 0) {
480                 /* Calculate BSS start address */
481                 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
482                     a_out->a_data;
483
484                 /* allocate some 'anon' space */
485                 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
486                     &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
487                     VM_PROT_ALL, 0);
488                 if (error)
489                         goto cleanup;
490         }
491
492 cleanup:
493         if (opened) {
494                 if (locked)
495                         VOP_UNLOCK(vp, 0);
496                 locked = false;
497                 VOP_CLOSE(vp, FREAD, td->td_ucred, td);
498         }
499         if (textset) {
500                 if (!locked) {
501                         locked = true;
502                         VOP_LOCK(vp, LK_SHARED | LK_RETRY);
503                 }
504                 VOP_UNSET_TEXT_CHECKED(vp);
505         }
506         if (locked)
507                 VOP_UNLOCK(vp, 0);
508
509         /* Release the temporary mapping. */
510         if (a_out)
511                 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
512
513         return (error);
514 }
515
516 #endif  /* __i386__ */
517
518 #ifdef LINUX_LEGACY_SYSCALLS
519 int
520 linux_select(struct thread *td, struct linux_select_args *args)
521 {
522         l_timeval ltv;
523         struct timeval tv0, tv1, utv, *tvp;
524         int error;
525
526 #ifdef DEBUG
527         if (ldebug(select))
528                 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
529                     (void *)args->readfds, (void *)args->writefds,
530                     (void *)args->exceptfds, (void *)args->timeout);
531 #endif
532
533         /*
534          * Store current time for computation of the amount of
535          * time left.
536          */
537         if (args->timeout) {
538                 if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
539                         goto select_out;
540                 utv.tv_sec = ltv.tv_sec;
541                 utv.tv_usec = ltv.tv_usec;
542 #ifdef DEBUG
543                 if (ldebug(select))
544                         printf(LMSG("incoming timeout (%jd/%ld)"),
545                             (intmax_t)utv.tv_sec, utv.tv_usec);
546 #endif
547
548                 if (itimerfix(&utv)) {
549                         /*
550                          * The timeval was invalid.  Convert it to something
551                          * valid that will act as it does under Linux.
552                          */
553                         utv.tv_sec += utv.tv_usec / 1000000;
554                         utv.tv_usec %= 1000000;
555                         if (utv.tv_usec < 0) {
556                                 utv.tv_sec -= 1;
557                                 utv.tv_usec += 1000000;
558                         }
559                         if (utv.tv_sec < 0)
560                                 timevalclear(&utv);
561                 }
562                 microtime(&tv0);
563                 tvp = &utv;
564         } else
565                 tvp = NULL;
566
567         error = kern_select(td, args->nfds, args->readfds, args->writefds,
568             args->exceptfds, tvp, LINUX_NFDBITS);
569
570 #ifdef DEBUG
571         if (ldebug(select))
572                 printf(LMSG("real select returns %d"), error);
573 #endif
574         if (error)
575                 goto select_out;
576
577         if (args->timeout) {
578                 if (td->td_retval[0]) {
579                         /*
580                          * Compute how much time was left of the timeout,
581                          * by subtracting the current time and the time
582                          * before we started the call, and subtracting
583                          * that result from the user-supplied value.
584                          */
585                         microtime(&tv1);
586                         timevalsub(&tv1, &tv0);
587                         timevalsub(&utv, &tv1);
588                         if (utv.tv_sec < 0)
589                                 timevalclear(&utv);
590                 } else
591                         timevalclear(&utv);
592 #ifdef DEBUG
593                 if (ldebug(select))
594                         printf(LMSG("outgoing timeout (%jd/%ld)"),
595                             (intmax_t)utv.tv_sec, utv.tv_usec);
596 #endif
597                 ltv.tv_sec = utv.tv_sec;
598                 ltv.tv_usec = utv.tv_usec;
599                 if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
600                         goto select_out;
601         }
602
603 select_out:
604 #ifdef DEBUG
605         if (ldebug(select))
606                 printf(LMSG("select_out -> %d"), error);
607 #endif
608         return (error);
609 }
610 #endif
611
612 int
613 linux_mremap(struct thread *td, struct linux_mremap_args *args)
614 {
615         uintptr_t addr;
616         size_t len;
617         int error = 0;
618
619 #ifdef DEBUG
620         if (ldebug(mremap))
621                 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
622                     (void *)(uintptr_t)args->addr,
623                     (unsigned long)args->old_len,
624                     (unsigned long)args->new_len,
625                     (unsigned long)args->flags);
626 #endif
627
628         if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
629                 td->td_retval[0] = 0;
630                 return (EINVAL);
631         }
632
633         /*
634          * Check for the page alignment.
635          * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
636          */
637         if (args->addr & PAGE_MASK) {
638                 td->td_retval[0] = 0;
639                 return (EINVAL);
640         }
641
642         args->new_len = round_page(args->new_len);
643         args->old_len = round_page(args->old_len);
644
645         if (args->new_len > args->old_len) {
646                 td->td_retval[0] = 0;
647                 return (ENOMEM);
648         }
649
650         if (args->new_len < args->old_len) {
651                 addr = args->addr + args->new_len;
652                 len = args->old_len - args->new_len;
653                 error = kern_munmap(td, addr, len);
654         }
655
656         td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
657         return (error);
658 }
659
660 #define LINUX_MS_ASYNC       0x0001
661 #define LINUX_MS_INVALIDATE  0x0002
662 #define LINUX_MS_SYNC        0x0004
663
664 int
665 linux_msync(struct thread *td, struct linux_msync_args *args)
666 {
667
668         return (kern_msync(td, args->addr, args->len,
669             args->fl & ~LINUX_MS_SYNC));
670 }
671
672 #ifdef LINUX_LEGACY_SYSCALLS
673 int
674 linux_time(struct thread *td, struct linux_time_args *args)
675 {
676         struct timeval tv;
677         l_time_t tm;
678         int error;
679
680 #ifdef DEBUG
681         if (ldebug(time))
682                 printf(ARGS(time, "*"));
683 #endif
684
685         microtime(&tv);
686         tm = tv.tv_sec;
687         if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
688                 return (error);
689         td->td_retval[0] = tm;
690         return (0);
691 }
692 #endif
693
694 struct l_times_argv {
695         l_clock_t       tms_utime;
696         l_clock_t       tms_stime;
697         l_clock_t       tms_cutime;
698         l_clock_t       tms_cstime;
699 };
700
701
702 /*
703  * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
704  * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
705  * auxiliary vector entry.
706  */
707 #define CLK_TCK         100
708
709 #define CONVOTCK(r)     (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
710 #define CONVNTCK(r)     (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
711
712 #define CONVTCK(r)      (linux_kernver(td) >= LINUX_KERNVER_2004000 ?           \
713                             CONVNTCK(r) : CONVOTCK(r))
714
715 int
716 linux_times(struct thread *td, struct linux_times_args *args)
717 {
718         struct timeval tv, utime, stime, cutime, cstime;
719         struct l_times_argv tms;
720         struct proc *p;
721         int error;
722
723 #ifdef DEBUG
724         if (ldebug(times))
725                 printf(ARGS(times, "*"));
726 #endif
727
728         if (args->buf != NULL) {
729                 p = td->td_proc;
730                 PROC_LOCK(p);
731                 PROC_STATLOCK(p);
732                 calcru(p, &utime, &stime);
733                 PROC_STATUNLOCK(p);
734                 calccru(p, &cutime, &cstime);
735                 PROC_UNLOCK(p);
736
737                 tms.tms_utime = CONVTCK(utime);
738                 tms.tms_stime = CONVTCK(stime);
739
740                 tms.tms_cutime = CONVTCK(cutime);
741                 tms.tms_cstime = CONVTCK(cstime);
742
743                 if ((error = copyout(&tms, args->buf, sizeof(tms))))
744                         return (error);
745         }
746
747         microuptime(&tv);
748         td->td_retval[0] = (int)CONVTCK(tv);
749         return (0);
750 }
751
752 int
753 linux_newuname(struct thread *td, struct linux_newuname_args *args)
754 {
755         struct l_new_utsname utsname;
756         char osname[LINUX_MAX_UTSNAME];
757         char osrelease[LINUX_MAX_UTSNAME];
758         char *p;
759
760 #ifdef DEBUG
761         if (ldebug(newuname))
762                 printf(ARGS(newuname, "*"));
763 #endif
764
765         linux_get_osname(td, osname);
766         linux_get_osrelease(td, osrelease);
767
768         bzero(&utsname, sizeof(utsname));
769         strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
770         getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
771         getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
772         strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
773         strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
774         for (p = utsname.version; *p != '\0'; ++p)
775                 if (*p == '\n') {
776                         *p = '\0';
777                         break;
778                 }
779         strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
780
781         return (copyout(&utsname, args->buf, sizeof(utsname)));
782 }
783
784 struct l_utimbuf {
785         l_time_t l_actime;
786         l_time_t l_modtime;
787 };
788
789 #ifdef LINUX_LEGACY_SYSCALLS
790 int
791 linux_utime(struct thread *td, struct linux_utime_args *args)
792 {
793         struct timeval tv[2], *tvp;
794         struct l_utimbuf lut;
795         char *fname;
796         int error;
797
798         LCONVPATHEXIST(td, args->fname, &fname);
799
800 #ifdef DEBUG
801         if (ldebug(utime))
802                 printf(ARGS(utime, "%s, *"), fname);
803 #endif
804
805         if (args->times) {
806                 if ((error = copyin(args->times, &lut, sizeof lut))) {
807                         LFREEPATH(fname);
808                         return (error);
809                 }
810                 tv[0].tv_sec = lut.l_actime;
811                 tv[0].tv_usec = 0;
812                 tv[1].tv_sec = lut.l_modtime;
813                 tv[1].tv_usec = 0;
814                 tvp = tv;
815         } else
816                 tvp = NULL;
817
818         error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
819             UIO_SYSSPACE);
820         LFREEPATH(fname);
821         return (error);
822 }
823 #endif
824
825 #ifdef LINUX_LEGACY_SYSCALLS
826 int
827 linux_utimes(struct thread *td, struct linux_utimes_args *args)
828 {
829         l_timeval ltv[2];
830         struct timeval tv[2], *tvp = NULL;
831         char *fname;
832         int error;
833
834         LCONVPATHEXIST(td, args->fname, &fname);
835
836 #ifdef DEBUG
837         if (ldebug(utimes))
838                 printf(ARGS(utimes, "%s, *"), fname);
839 #endif
840
841         if (args->tptr != NULL) {
842                 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
843                         LFREEPATH(fname);
844                         return (error);
845                 }
846                 tv[0].tv_sec = ltv[0].tv_sec;
847                 tv[0].tv_usec = ltv[0].tv_usec;
848                 tv[1].tv_sec = ltv[1].tv_sec;
849                 tv[1].tv_usec = ltv[1].tv_usec;
850                 tvp = tv;
851         }
852
853         error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE,
854             tvp, UIO_SYSSPACE);
855         LFREEPATH(fname);
856         return (error);
857 }
858 #endif
859
860 static int
861 linux_utimensat_nsec_valid(l_long nsec)
862 {
863
864         if (nsec == LINUX_UTIME_OMIT || nsec == LINUX_UTIME_NOW)
865                 return (0);
866         if (nsec >= 0 && nsec <= 999999999)
867                 return (0);
868         return (1);
869 }
870
871 int
872 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
873 {
874         struct l_timespec l_times[2];
875         struct timespec times[2], *timesp = NULL;
876         char *path = NULL;
877         int error, dfd, flags = 0;
878
879         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
880
881 #ifdef DEBUG
882         if (ldebug(utimensat))
883                 printf(ARGS(utimensat, "%d, *"), dfd);
884 #endif
885
886         if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW)
887                 return (EINVAL);
888
889         if (args->times != NULL) {
890                 error = copyin(args->times, l_times, sizeof(l_times));
891                 if (error != 0)
892                         return (error);
893
894                 if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 ||
895                     linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0)
896                         return (EINVAL);
897
898                 times[0].tv_sec = l_times[0].tv_sec;
899                 switch (l_times[0].tv_nsec)
900                 {
901                 case LINUX_UTIME_OMIT:
902                         times[0].tv_nsec = UTIME_OMIT;
903                         break;
904                 case LINUX_UTIME_NOW:
905                         times[0].tv_nsec = UTIME_NOW;
906                         break;
907                 default:
908                         times[0].tv_nsec = l_times[0].tv_nsec;
909                 }
910
911                 times[1].tv_sec = l_times[1].tv_sec;
912                 switch (l_times[1].tv_nsec)
913                 {
914                 case LINUX_UTIME_OMIT:
915                         times[1].tv_nsec = UTIME_OMIT;
916                         break;
917                 case LINUX_UTIME_NOW:
918                         times[1].tv_nsec = UTIME_NOW;
919                         break;
920                 default:
921                         times[1].tv_nsec = l_times[1].tv_nsec;
922                         break;
923                 }
924                 timesp = times;
925
926                 /* This breaks POSIX, but is what the Linux kernel does
927                  * _on purpose_ (documented in the man page for utimensat(2)),
928                  * so we must follow that behaviour. */
929                 if (times[0].tv_nsec == UTIME_OMIT &&
930                     times[1].tv_nsec == UTIME_OMIT)
931                         return (0);
932         }
933
934         if (args->pathname != NULL)
935                 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
936         else if (args->flags != 0)
937                 return (EINVAL);
938
939         if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW)
940                 flags |= AT_SYMLINK_NOFOLLOW;
941
942         if (path == NULL)
943                 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
944         else {
945                 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
946                         UIO_SYSSPACE, flags);
947                 LFREEPATH(path);
948         }
949
950         return (error);
951 }
952
953 #ifdef LINUX_LEGACY_SYSCALLS
954 int
955 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
956 {
957         l_timeval ltv[2];
958         struct timeval tv[2], *tvp = NULL;
959         char *fname;
960         int error, dfd;
961
962         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
963         LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
964
965 #ifdef DEBUG
966         if (ldebug(futimesat))
967                 printf(ARGS(futimesat, "%s, *"), fname);
968 #endif
969
970         if (args->utimes != NULL) {
971                 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
972                         LFREEPATH(fname);
973                         return (error);
974                 }
975                 tv[0].tv_sec = ltv[0].tv_sec;
976                 tv[0].tv_usec = ltv[0].tv_usec;
977                 tv[1].tv_sec = ltv[1].tv_sec;
978                 tv[1].tv_usec = ltv[1].tv_usec;
979                 tvp = tv;
980         }
981
982         error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
983         LFREEPATH(fname);
984         return (error);
985 }
986 #endif
987
988 static int
989 linux_common_wait(struct thread *td, int pid, int *statusp,
990     int options, struct __wrusage *wrup)
991 {
992         siginfo_t siginfo;
993         idtype_t idtype;
994         id_t id;
995         int error, status, tmpstat;
996
997         if (pid == WAIT_ANY) {
998                 idtype = P_ALL;
999                 id = 0;
1000         } else if (pid < 0) {
1001                 idtype = P_PGID;
1002                 id = (id_t)-pid;
1003         } else {
1004                 idtype = P_PID;
1005                 id = (id_t)pid;
1006         }
1007
1008         /*
1009          * For backward compatibility we implicitly add flags WEXITED
1010          * and WTRAPPED here.
1011          */
1012         options |= WEXITED | WTRAPPED;
1013         error = kern_wait6(td, idtype, id, &status, options, wrup, &siginfo);
1014         if (error)
1015                 return (error);
1016
1017         if (statusp) {
1018                 tmpstat = status & 0xffff;
1019                 if (WIFSIGNALED(tmpstat)) {
1020                         tmpstat = (tmpstat & 0xffffff80) |
1021                             bsd_to_linux_signal(WTERMSIG(tmpstat));
1022                 } else if (WIFSTOPPED(tmpstat)) {
1023                         tmpstat = (tmpstat & 0xffff00ff) |
1024                             (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
1025 #if defined(__amd64__) && !defined(COMPAT_LINUX32)
1026                         if (WSTOPSIG(status) == SIGTRAP) {
1027                                 tmpstat = linux_ptrace_status(td,
1028                                     siginfo.si_pid, tmpstat);
1029                         }
1030 #endif
1031                 } else if (WIFCONTINUED(tmpstat)) {
1032                         tmpstat = 0xffff;
1033                 }
1034                 error = copyout(&tmpstat, statusp, sizeof(int));
1035         }
1036
1037         return (error);
1038 }
1039
1040 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1041 int
1042 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
1043 {
1044         struct linux_wait4_args wait4_args;
1045
1046 #ifdef DEBUG
1047         if (ldebug(waitpid))
1048                 printf(ARGS(waitpid, "%d, %p, %d"),
1049                     args->pid, (void *)args->status, args->options);
1050 #endif
1051
1052         wait4_args.pid = args->pid;
1053         wait4_args.status = args->status;
1054         wait4_args.options = args->options;
1055         wait4_args.rusage = NULL;
1056
1057         return (linux_wait4(td, &wait4_args));
1058 }
1059 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1060
1061 int
1062 linux_wait4(struct thread *td, struct linux_wait4_args *args)
1063 {
1064         int error, options;
1065         struct __wrusage wru, *wrup;
1066
1067 #ifdef DEBUG
1068         if (ldebug(wait4))
1069                 printf(ARGS(wait4, "%d, %p, %d, %p"),
1070                     args->pid, (void *)args->status, args->options,
1071                     (void *)args->rusage);
1072 #endif
1073         if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
1074             LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
1075                 return (EINVAL);
1076
1077         options = WEXITED;
1078         linux_to_bsd_waitopts(args->options, &options);
1079
1080         if (args->rusage != NULL)
1081                 wrup = &wru;
1082         else
1083                 wrup = NULL;
1084         error = linux_common_wait(td, args->pid, args->status, options, wrup);
1085         if (error != 0)
1086                 return (error);
1087         if (args->rusage != NULL)
1088                 error = linux_copyout_rusage(&wru.wru_self, args->rusage);
1089         return (error);
1090 }
1091
1092 int
1093 linux_waitid(struct thread *td, struct linux_waitid_args *args)
1094 {
1095         int status, options, sig;
1096         struct __wrusage wru;
1097         siginfo_t siginfo;
1098         l_siginfo_t lsi;
1099         idtype_t idtype;
1100         struct proc *p;
1101         int error;
1102
1103         options = 0;
1104         linux_to_bsd_waitopts(args->options, &options);
1105
1106         if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
1107                 return (EINVAL);
1108         if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
1109                 return (EINVAL);
1110
1111         switch (args->idtype) {
1112         case LINUX_P_ALL:
1113                 idtype = P_ALL;
1114                 break;
1115         case LINUX_P_PID:
1116                 if (args->id <= 0)
1117                         return (EINVAL);
1118                 idtype = P_PID;
1119                 break;
1120         case LINUX_P_PGID:
1121                 if (args->id <= 0)
1122                         return (EINVAL);
1123                 idtype = P_PGID;
1124                 break;
1125         default:
1126                 return (EINVAL);
1127         }
1128
1129         error = kern_wait6(td, idtype, args->id, &status, options,
1130             &wru, &siginfo);
1131         if (error != 0)
1132                 return (error);
1133         if (args->rusage != NULL) {
1134                 error = linux_copyout_rusage(&wru.wru_children,
1135                     args->rusage);
1136                 if (error != 0)
1137                         return (error);
1138         }
1139         if (args->info != NULL) {
1140                 p = td->td_proc;
1141                 bzero(&lsi, sizeof(lsi));
1142                 if (td->td_retval[0] != 0) {
1143                         sig = bsd_to_linux_signal(siginfo.si_signo);
1144                         siginfo_to_lsiginfo(&siginfo, &lsi, sig);
1145                 }
1146                 error = copyout(&lsi, args->info, sizeof(lsi));
1147         }
1148         td->td_retval[0] = 0;
1149
1150         return (error);
1151 }
1152
1153 #ifdef LINUX_LEGACY_SYSCALLS
1154 int
1155 linux_mknod(struct thread *td, struct linux_mknod_args *args)
1156 {
1157         char *path;
1158         int error;
1159
1160         LCONVPATHCREAT(td, args->path, &path);
1161
1162 #ifdef DEBUG
1163         if (ldebug(mknod))
1164                 printf(ARGS(mknod, "%s, %d, %ju"), path, args->mode,
1165                     (uintmax_t)args->dev);
1166 #endif
1167
1168         switch (args->mode & S_IFMT) {
1169         case S_IFIFO:
1170         case S_IFSOCK:
1171                 error = kern_mkfifoat(td, AT_FDCWD, path, UIO_SYSSPACE,
1172                     args->mode);
1173                 break;
1174
1175         case S_IFCHR:
1176         case S_IFBLK:
1177                 error = kern_mknodat(td, AT_FDCWD, path, UIO_SYSSPACE,
1178                     args->mode, args->dev);
1179                 break;
1180
1181         case S_IFDIR:
1182                 error = EPERM;
1183                 break;
1184
1185         case 0:
1186                 args->mode |= S_IFREG;
1187                 /* FALLTHROUGH */
1188         case S_IFREG:
1189                 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
1190                     O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1191                 if (error == 0)
1192                         kern_close(td, td->td_retval[0]);
1193                 break;
1194
1195         default:
1196                 error = EINVAL;
1197                 break;
1198         }
1199         LFREEPATH(path);
1200         return (error);
1201 }
1202 #endif
1203
1204 int
1205 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1206 {
1207         char *path;
1208         int error, dfd;
1209
1210         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1211         LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1212
1213 #ifdef DEBUG
1214         if (ldebug(mknodat))
1215                 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1216 #endif
1217
1218         switch (args->mode & S_IFMT) {
1219         case S_IFIFO:
1220         case S_IFSOCK:
1221                 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1222                 break;
1223
1224         case S_IFCHR:
1225         case S_IFBLK:
1226                 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1227                     args->dev);
1228                 break;
1229
1230         case S_IFDIR:
1231                 error = EPERM;
1232                 break;
1233
1234         case 0:
1235                 args->mode |= S_IFREG;
1236                 /* FALLTHROUGH */
1237         case S_IFREG:
1238                 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1239                     O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1240                 if (error == 0)
1241                         kern_close(td, td->td_retval[0]);
1242                 break;
1243
1244         default:
1245                 error = EINVAL;
1246                 break;
1247         }
1248         LFREEPATH(path);
1249         return (error);
1250 }
1251
1252 /*
1253  * UGH! This is just about the dumbest idea I've ever heard!!
1254  */
1255 int
1256 linux_personality(struct thread *td, struct linux_personality_args *args)
1257 {
1258         struct linux_pemuldata *pem;
1259         struct proc *p = td->td_proc;
1260         uint32_t old;
1261
1262 #ifdef DEBUG
1263         if (ldebug(personality))
1264                 printf(ARGS(personality, "%u"), args->per);
1265 #endif
1266
1267         PROC_LOCK(p);
1268         pem = pem_find(p);
1269         old = pem->persona;
1270         if (args->per != 0xffffffff)
1271                 pem->persona = args->per;
1272         PROC_UNLOCK(p);
1273
1274         td->td_retval[0] = old;
1275         return (0);
1276 }
1277
1278 struct l_itimerval {
1279         l_timeval it_interval;
1280         l_timeval it_value;
1281 };
1282
1283 #define B2L_ITIMERVAL(bip, lip)                                         \
1284         (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;          \
1285         (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;        \
1286         (bip)->it_value.tv_sec = (lip)->it_value.tv_sec;                \
1287         (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1288
1289 int
1290 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1291 {
1292         int error;
1293         struct l_itimerval ls;
1294         struct itimerval aitv, oitv;
1295
1296 #ifdef DEBUG
1297         if (ldebug(setitimer))
1298                 printf(ARGS(setitimer, "%p, %p"),
1299                     (void *)uap->itv, (void *)uap->oitv);
1300 #endif
1301
1302         if (uap->itv == NULL) {
1303                 uap->itv = uap->oitv;
1304                 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1305         }
1306
1307         error = copyin(uap->itv, &ls, sizeof(ls));
1308         if (error != 0)
1309                 return (error);
1310         B2L_ITIMERVAL(&aitv, &ls);
1311 #ifdef DEBUG
1312         if (ldebug(setitimer)) {
1313                 printf("setitimer: value: sec: %jd, usec: %ld\n",
1314                     (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1315                 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1316                     (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1317         }
1318 #endif
1319         error = kern_setitimer(td, uap->which, &aitv, &oitv);
1320         if (error != 0 || uap->oitv == NULL)
1321                 return (error);
1322         B2L_ITIMERVAL(&ls, &oitv);
1323
1324         return (copyout(&ls, uap->oitv, sizeof(ls)));
1325 }
1326
1327 int
1328 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1329 {
1330         int error;
1331         struct l_itimerval ls;
1332         struct itimerval aitv;
1333
1334 #ifdef DEBUG
1335         if (ldebug(getitimer))
1336                 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1337 #endif
1338         error = kern_getitimer(td, uap->which, &aitv);
1339         if (error != 0)
1340                 return (error);
1341         B2L_ITIMERVAL(&ls, &aitv);
1342         return (copyout(&ls, uap->itv, sizeof(ls)));
1343 }
1344
1345 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1346 int
1347 linux_nice(struct thread *td, struct linux_nice_args *args)
1348 {
1349         struct setpriority_args bsd_args;
1350
1351         bsd_args.which = PRIO_PROCESS;
1352         bsd_args.who = 0;               /* current process */
1353         bsd_args.prio = args->inc;
1354         return (sys_setpriority(td, &bsd_args));
1355 }
1356 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1357
1358 int
1359 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1360 {
1361         struct ucred *newcred, *oldcred;
1362         l_gid_t *linux_gidset;
1363         gid_t *bsd_gidset;
1364         int ngrp, error;
1365         struct proc *p;
1366
1367         ngrp = args->gidsetsize;
1368         if (ngrp < 0 || ngrp >= ngroups_max + 1)
1369                 return (EINVAL);
1370         linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1371         error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1372         if (error)
1373                 goto out;
1374         newcred = crget();
1375         crextend(newcred, ngrp + 1);
1376         p = td->td_proc;
1377         PROC_LOCK(p);
1378         oldcred = p->p_ucred;
1379         crcopy(newcred, oldcred);
1380
1381         /*
1382          * cr_groups[0] holds egid. Setting the whole set from
1383          * the supplied set will cause egid to be changed too.
1384          * Keep cr_groups[0] unchanged to prevent that.
1385          */
1386
1387         if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1388                 PROC_UNLOCK(p);
1389                 crfree(newcred);
1390                 goto out;
1391         }
1392
1393         if (ngrp > 0) {
1394                 newcred->cr_ngroups = ngrp + 1;
1395
1396                 bsd_gidset = newcred->cr_groups;
1397                 ngrp--;
1398                 while (ngrp >= 0) {
1399                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1400                         ngrp--;
1401                 }
1402         } else
1403                 newcred->cr_ngroups = 1;
1404
1405         setsugid(p);
1406         proc_set_cred(p, newcred);
1407         PROC_UNLOCK(p);
1408         crfree(oldcred);
1409         error = 0;
1410 out:
1411         free(linux_gidset, M_LINUX);
1412         return (error);
1413 }
1414
1415 int
1416 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1417 {
1418         struct ucred *cred;
1419         l_gid_t *linux_gidset;
1420         gid_t *bsd_gidset;
1421         int bsd_gidsetsz, ngrp, error;
1422
1423         cred = td->td_ucred;
1424         bsd_gidset = cred->cr_groups;
1425         bsd_gidsetsz = cred->cr_ngroups - 1;
1426
1427         /*
1428          * cr_groups[0] holds egid. Returning the whole set
1429          * here will cause a duplicate. Exclude cr_groups[0]
1430          * to prevent that.
1431          */
1432
1433         if ((ngrp = args->gidsetsize) == 0) {
1434                 td->td_retval[0] = bsd_gidsetsz;
1435                 return (0);
1436         }
1437
1438         if (ngrp < bsd_gidsetsz)
1439                 return (EINVAL);
1440
1441         ngrp = 0;
1442         linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1443             M_LINUX, M_WAITOK);
1444         while (ngrp < bsd_gidsetsz) {
1445                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1446                 ngrp++;
1447         }
1448
1449         error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1450         free(linux_gidset, M_LINUX);
1451         if (error)
1452                 return (error);
1453
1454         td->td_retval[0] = ngrp;
1455         return (0);
1456 }
1457
1458 int
1459 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1460 {
1461         struct rlimit bsd_rlim;
1462         struct l_rlimit rlim;
1463         u_int which;
1464         int error;
1465
1466 #ifdef DEBUG
1467         if (ldebug(setrlimit))
1468                 printf(ARGS(setrlimit, "%d, %p"),
1469                     args->resource, (void *)args->rlim);
1470 #endif
1471
1472         if (args->resource >= LINUX_RLIM_NLIMITS)
1473                 return (EINVAL);
1474
1475         which = linux_to_bsd_resource[args->resource];
1476         if (which == -1)
1477                 return (EINVAL);
1478
1479         error = copyin(args->rlim, &rlim, sizeof(rlim));
1480         if (error)
1481                 return (error);
1482
1483         bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1484         bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1485         return (kern_setrlimit(td, which, &bsd_rlim));
1486 }
1487
1488 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1489 int
1490 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1491 {
1492         struct l_rlimit rlim;
1493         struct rlimit bsd_rlim;
1494         u_int which;
1495
1496 #ifdef DEBUG
1497         if (ldebug(old_getrlimit))
1498                 printf(ARGS(old_getrlimit, "%d, %p"),
1499                     args->resource, (void *)args->rlim);
1500 #endif
1501
1502         if (args->resource >= LINUX_RLIM_NLIMITS)
1503                 return (EINVAL);
1504
1505         which = linux_to_bsd_resource[args->resource];
1506         if (which == -1)
1507                 return (EINVAL);
1508
1509         lim_rlimit(td, which, &bsd_rlim);
1510
1511 #ifdef COMPAT_LINUX32
1512         rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1513         if (rlim.rlim_cur == UINT_MAX)
1514                 rlim.rlim_cur = INT_MAX;
1515         rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1516         if (rlim.rlim_max == UINT_MAX)
1517                 rlim.rlim_max = INT_MAX;
1518 #else
1519         rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1520         if (rlim.rlim_cur == ULONG_MAX)
1521                 rlim.rlim_cur = LONG_MAX;
1522         rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1523         if (rlim.rlim_max == ULONG_MAX)
1524                 rlim.rlim_max = LONG_MAX;
1525 #endif
1526         return (copyout(&rlim, args->rlim, sizeof(rlim)));
1527 }
1528 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1529
1530 int
1531 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1532 {
1533         struct l_rlimit rlim;
1534         struct rlimit bsd_rlim;
1535         u_int which;
1536
1537 #ifdef DEBUG
1538         if (ldebug(getrlimit))
1539                 printf(ARGS(getrlimit, "%d, %p"),
1540                     args->resource, (void *)args->rlim);
1541 #endif
1542
1543         if (args->resource >= LINUX_RLIM_NLIMITS)
1544                 return (EINVAL);
1545
1546         which = linux_to_bsd_resource[args->resource];
1547         if (which == -1)
1548                 return (EINVAL);
1549
1550         lim_rlimit(td, which, &bsd_rlim);
1551
1552         rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1553         rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1554         return (copyout(&rlim, args->rlim, sizeof(rlim)));
1555 }
1556
1557 int
1558 linux_sched_setscheduler(struct thread *td,
1559     struct linux_sched_setscheduler_args *args)
1560 {
1561         struct sched_param sched_param;
1562         struct thread *tdt;
1563         int error, policy;
1564
1565 #ifdef DEBUG
1566         if (ldebug(sched_setscheduler))
1567                 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1568                     args->pid, args->policy, (const void *)args->param);
1569 #endif
1570
1571         switch (args->policy) {
1572         case LINUX_SCHED_OTHER:
1573                 policy = SCHED_OTHER;
1574                 break;
1575         case LINUX_SCHED_FIFO:
1576                 policy = SCHED_FIFO;
1577                 break;
1578         case LINUX_SCHED_RR:
1579                 policy = SCHED_RR;
1580                 break;
1581         default:
1582                 return (EINVAL);
1583         }
1584
1585         error = copyin(args->param, &sched_param, sizeof(sched_param));
1586         if (error)
1587                 return (error);
1588
1589         if (linux_map_sched_prio) {
1590                 switch (policy) {
1591                 case SCHED_OTHER:
1592                         if (sched_param.sched_priority != 0)
1593                                 return (EINVAL);
1594
1595                         sched_param.sched_priority =
1596                             PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
1597                         break;
1598                 case SCHED_FIFO:
1599                 case SCHED_RR:
1600                         if (sched_param.sched_priority < 1 ||
1601                             sched_param.sched_priority >= LINUX_MAX_RT_PRIO)
1602                                 return (EINVAL);
1603
1604                         /*
1605                          * Map [1, LINUX_MAX_RT_PRIO - 1] to
1606                          * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
1607                          */
1608                         sched_param.sched_priority =
1609                             (sched_param.sched_priority - 1) *
1610                             (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
1611                             (LINUX_MAX_RT_PRIO - 1);
1612                         break;
1613                 }
1614         }
1615
1616         tdt = linux_tdfind(td, args->pid, -1);
1617         if (tdt == NULL)
1618                 return (ESRCH);
1619
1620         error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1621         PROC_UNLOCK(tdt->td_proc);
1622         return (error);
1623 }
1624
1625 int
1626 linux_sched_getscheduler(struct thread *td,
1627     struct linux_sched_getscheduler_args *args)
1628 {
1629         struct thread *tdt;
1630         int error, policy;
1631
1632 #ifdef DEBUG
1633         if (ldebug(sched_getscheduler))
1634                 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1635 #endif
1636
1637         tdt = linux_tdfind(td, args->pid, -1);
1638         if (tdt == NULL)
1639                 return (ESRCH);
1640
1641         error = kern_sched_getscheduler(td, tdt, &policy);
1642         PROC_UNLOCK(tdt->td_proc);
1643
1644         switch (policy) {
1645         case SCHED_OTHER:
1646                 td->td_retval[0] = LINUX_SCHED_OTHER;
1647                 break;
1648         case SCHED_FIFO:
1649                 td->td_retval[0] = LINUX_SCHED_FIFO;
1650                 break;
1651         case SCHED_RR:
1652                 td->td_retval[0] = LINUX_SCHED_RR;
1653                 break;
1654         }
1655         return (error);
1656 }
1657
1658 int
1659 linux_sched_get_priority_max(struct thread *td,
1660     struct linux_sched_get_priority_max_args *args)
1661 {
1662         struct sched_get_priority_max_args bsd;
1663
1664 #ifdef DEBUG
1665         if (ldebug(sched_get_priority_max))
1666                 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1667 #endif
1668
1669         if (linux_map_sched_prio) {
1670                 switch (args->policy) {
1671                 case LINUX_SCHED_OTHER:
1672                         td->td_retval[0] = 0;
1673                         return (0);
1674                 case LINUX_SCHED_FIFO:
1675                 case LINUX_SCHED_RR:
1676                         td->td_retval[0] = LINUX_MAX_RT_PRIO - 1;
1677                         return (0);
1678                 default:
1679                         return (EINVAL);
1680                 }
1681         }
1682
1683         switch (args->policy) {
1684         case LINUX_SCHED_OTHER:
1685                 bsd.policy = SCHED_OTHER;
1686                 break;
1687         case LINUX_SCHED_FIFO:
1688                 bsd.policy = SCHED_FIFO;
1689                 break;
1690         case LINUX_SCHED_RR:
1691                 bsd.policy = SCHED_RR;
1692                 break;
1693         default:
1694                 return (EINVAL);
1695         }
1696         return (sys_sched_get_priority_max(td, &bsd));
1697 }
1698
1699 int
1700 linux_sched_get_priority_min(struct thread *td,
1701     struct linux_sched_get_priority_min_args *args)
1702 {
1703         struct sched_get_priority_min_args bsd;
1704
1705 #ifdef DEBUG
1706         if (ldebug(sched_get_priority_min))
1707                 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1708 #endif
1709
1710         if (linux_map_sched_prio) {
1711                 switch (args->policy) {
1712                 case LINUX_SCHED_OTHER:
1713                         td->td_retval[0] = 0;
1714                         return (0);
1715                 case LINUX_SCHED_FIFO:
1716                 case LINUX_SCHED_RR:
1717                         td->td_retval[0] = 1;
1718                         return (0);
1719                 default:
1720                         return (EINVAL);
1721                 }
1722         }
1723
1724         switch (args->policy) {
1725         case LINUX_SCHED_OTHER:
1726                 bsd.policy = SCHED_OTHER;
1727                 break;
1728         case LINUX_SCHED_FIFO:
1729                 bsd.policy = SCHED_FIFO;
1730                 break;
1731         case LINUX_SCHED_RR:
1732                 bsd.policy = SCHED_RR;
1733                 break;
1734         default:
1735                 return (EINVAL);
1736         }
1737         return (sys_sched_get_priority_min(td, &bsd));
1738 }
1739
1740 #define REBOOT_CAD_ON   0x89abcdef
1741 #define REBOOT_CAD_OFF  0
1742 #define REBOOT_HALT     0xcdef0123
1743 #define REBOOT_RESTART  0x01234567
1744 #define REBOOT_RESTART2 0xA1B2C3D4
1745 #define REBOOT_POWEROFF 0x4321FEDC
1746 #define REBOOT_MAGIC1   0xfee1dead
1747 #define REBOOT_MAGIC2   0x28121969
1748 #define REBOOT_MAGIC2A  0x05121996
1749 #define REBOOT_MAGIC2B  0x16041998
1750
1751 int
1752 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1753 {
1754         struct reboot_args bsd_args;
1755
1756 #ifdef DEBUG
1757         if (ldebug(reboot))
1758                 printf(ARGS(reboot, "0x%x"), args->cmd);
1759 #endif
1760
1761         if (args->magic1 != REBOOT_MAGIC1)
1762                 return (EINVAL);
1763
1764         switch (args->magic2) {
1765         case REBOOT_MAGIC2:
1766         case REBOOT_MAGIC2A:
1767         case REBOOT_MAGIC2B:
1768                 break;
1769         default:
1770                 return (EINVAL);
1771         }
1772
1773         switch (args->cmd) {
1774         case REBOOT_CAD_ON:
1775         case REBOOT_CAD_OFF:
1776                 return (priv_check(td, PRIV_REBOOT));
1777         case REBOOT_HALT:
1778                 bsd_args.opt = RB_HALT;
1779                 break;
1780         case REBOOT_RESTART:
1781         case REBOOT_RESTART2:
1782                 bsd_args.opt = 0;
1783                 break;
1784         case REBOOT_POWEROFF:
1785                 bsd_args.opt = RB_POWEROFF;
1786                 break;
1787         default:
1788                 return (EINVAL);
1789         }
1790         return (sys_reboot(td, &bsd_args));
1791 }
1792
1793
1794 int
1795 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1796 {
1797
1798 #ifdef DEBUG
1799         if (ldebug(getpid))
1800                 printf(ARGS(getpid, ""));
1801 #endif
1802         td->td_retval[0] = td->td_proc->p_pid;
1803
1804         return (0);
1805 }
1806
1807 int
1808 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1809 {
1810         struct linux_emuldata *em;
1811
1812 #ifdef DEBUG
1813         if (ldebug(gettid))
1814                 printf(ARGS(gettid, ""));
1815 #endif
1816
1817         em = em_find(td);
1818         KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1819
1820         td->td_retval[0] = em->em_tid;
1821
1822         return (0);
1823 }
1824
1825
1826 int
1827 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1828 {
1829
1830 #ifdef DEBUG
1831         if (ldebug(getppid))
1832                 printf(ARGS(getppid, ""));
1833 #endif
1834
1835         td->td_retval[0] = kern_getppid(td);
1836         return (0);
1837 }
1838
1839 int
1840 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1841 {
1842
1843 #ifdef DEBUG
1844         if (ldebug(getgid))
1845                 printf(ARGS(getgid, ""));
1846 #endif
1847
1848         td->td_retval[0] = td->td_ucred->cr_rgid;
1849         return (0);
1850 }
1851
1852 int
1853 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1854 {
1855
1856 #ifdef DEBUG
1857         if (ldebug(getuid))
1858                 printf(ARGS(getuid, ""));
1859 #endif
1860
1861         td->td_retval[0] = td->td_ucred->cr_ruid;
1862         return (0);
1863 }
1864
1865
1866 int
1867 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1868 {
1869         struct getsid_args bsd;
1870
1871 #ifdef DEBUG
1872         if (ldebug(getsid))
1873                 printf(ARGS(getsid, "%i"), args->pid);
1874 #endif
1875
1876         bsd.pid = args->pid;
1877         return (sys_getsid(td, &bsd));
1878 }
1879
1880 int
1881 linux_nosys(struct thread *td, struct nosys_args *ignore)
1882 {
1883
1884         return (ENOSYS);
1885 }
1886
1887 int
1888 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1889 {
1890         struct getpriority_args bsd_args;
1891         int error;
1892
1893 #ifdef DEBUG
1894         if (ldebug(getpriority))
1895                 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1896 #endif
1897
1898         bsd_args.which = args->which;
1899         bsd_args.who = args->who;
1900         error = sys_getpriority(td, &bsd_args);
1901         td->td_retval[0] = 20 - td->td_retval[0];
1902         return (error);
1903 }
1904
1905 int
1906 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1907 {
1908         int name[2];
1909
1910 #ifdef DEBUG
1911         if (ldebug(sethostname))
1912                 printf(ARGS(sethostname, "*, %i"), args->len);
1913 #endif
1914
1915         name[0] = CTL_KERN;
1916         name[1] = KERN_HOSTNAME;
1917         return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1918             args->len, 0, 0));
1919 }
1920
1921 int
1922 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1923 {
1924         int name[2];
1925
1926 #ifdef DEBUG
1927         if (ldebug(setdomainname))
1928                 printf(ARGS(setdomainname, "*, %i"), args->len);
1929 #endif
1930
1931         name[0] = CTL_KERN;
1932         name[1] = KERN_NISDOMAINNAME;
1933         return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1934             args->len, 0, 0));
1935 }
1936
1937 int
1938 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1939 {
1940
1941 #ifdef DEBUG
1942         if (ldebug(exit_group))
1943                 printf(ARGS(exit_group, "%i"), args->error_code);
1944 #endif
1945
1946         LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1947             args->error_code);
1948
1949         /*
1950          * XXX: we should send a signal to the parent if
1951          * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1952          * as it doesnt occur often.
1953          */
1954         exit1(td, args->error_code, 0);
1955                 /* NOTREACHED */
1956 }
1957
1958 #define _LINUX_CAPABILITY_VERSION_1  0x19980330
1959 #define _LINUX_CAPABILITY_VERSION_2  0x20071026
1960 #define _LINUX_CAPABILITY_VERSION_3  0x20080522
1961
1962 struct l_user_cap_header {
1963         l_int   version;
1964         l_int   pid;
1965 };
1966
1967 struct l_user_cap_data {
1968         l_int   effective;
1969         l_int   permitted;
1970         l_int   inheritable;
1971 };
1972
1973 int
1974 linux_capget(struct thread *td, struct linux_capget_args *uap)
1975 {
1976         struct l_user_cap_header luch;
1977         struct l_user_cap_data lucd[2];
1978         int error, u32s;
1979
1980         if (uap->hdrp == NULL)
1981                 return (EFAULT);
1982
1983         error = copyin(uap->hdrp, &luch, sizeof(luch));
1984         if (error != 0)
1985                 return (error);
1986
1987         switch (luch.version) {
1988         case _LINUX_CAPABILITY_VERSION_1:
1989                 u32s = 1;
1990                 break;
1991         case _LINUX_CAPABILITY_VERSION_2:
1992         case _LINUX_CAPABILITY_VERSION_3:
1993                 u32s = 2;
1994                 break;
1995         default:
1996 #ifdef DEBUG
1997                 if (ldebug(capget))
1998                         printf(LMSG("invalid capget capability version 0x%x"),
1999                             luch.version);
2000 #endif
2001                 luch.version = _LINUX_CAPABILITY_VERSION_1;
2002                 error = copyout(&luch, uap->hdrp, sizeof(luch));
2003                 if (error)
2004                         return (error);
2005                 return (EINVAL);
2006         }
2007
2008         if (luch.pid)
2009                 return (EPERM);
2010
2011         if (uap->datap) {
2012                 /*
2013                  * The current implementation doesn't support setting
2014                  * a capability (it's essentially a stub) so indicate
2015                  * that no capabilities are currently set or available
2016                  * to request.
2017                  */
2018                 memset(&lucd, 0, u32s * sizeof(lucd[0]));
2019                 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0]));
2020         }
2021
2022         return (error);
2023 }
2024
2025 int
2026 linux_capset(struct thread *td, struct linux_capset_args *uap)
2027 {
2028         struct l_user_cap_header luch;
2029         struct l_user_cap_data lucd[2];
2030         int error, i, u32s;
2031
2032         if (uap->hdrp == NULL || uap->datap == NULL)
2033                 return (EFAULT);
2034
2035         error = copyin(uap->hdrp, &luch, sizeof(luch));
2036         if (error != 0)
2037                 return (error);
2038
2039         switch (luch.version) {
2040         case _LINUX_CAPABILITY_VERSION_1:
2041                 u32s = 1;
2042                 break;
2043         case _LINUX_CAPABILITY_VERSION_2:
2044         case _LINUX_CAPABILITY_VERSION_3:
2045                 u32s = 2;
2046                 break;
2047         default:
2048 #ifdef DEBUG
2049                 if (ldebug(capset))
2050                         printf(LMSG("invalid capset capability version 0x%x"),
2051                             luch.version);
2052 #endif
2053                 luch.version = _LINUX_CAPABILITY_VERSION_1;
2054                 error = copyout(&luch, uap->hdrp, sizeof(luch));
2055                 if (error)
2056                         return (error);
2057                 return (EINVAL);
2058         }
2059
2060         if (luch.pid)
2061                 return (EPERM);
2062
2063         error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0]));
2064         if (error != 0)
2065                 return (error);
2066
2067         /* We currently don't support setting any capabilities. */
2068         for (i = 0; i < u32s; i++) {
2069                 if (lucd[i].effective || lucd[i].permitted ||
2070                     lucd[i].inheritable) {
2071                         linux_msg(td,
2072                             "capset[%d] effective=0x%x, permitted=0x%x, "
2073                             "inheritable=0x%x is not implemented", i,
2074                             (int)lucd[i].effective, (int)lucd[i].permitted,
2075                             (int)lucd[i].inheritable);
2076                         return (EPERM);
2077                 }
2078         }
2079
2080         return (0);
2081 }
2082
2083 int
2084 linux_prctl(struct thread *td, struct linux_prctl_args *args)
2085 {
2086         int error = 0, max_size;
2087         struct proc *p = td->td_proc;
2088         char comm[LINUX_MAX_COMM_LEN];
2089         int pdeath_signal;
2090
2091 #ifdef DEBUG
2092         if (ldebug(prctl))
2093                 printf(ARGS(prctl, "%d, %ju, %ju, %ju, %ju"), args->option,
2094                     (uintmax_t)args->arg2, (uintmax_t)args->arg3,
2095                     (uintmax_t)args->arg4, (uintmax_t)args->arg5);
2096 #endif
2097
2098         switch (args->option) {
2099         case LINUX_PR_SET_PDEATHSIG:
2100                 if (!LINUX_SIG_VALID(args->arg2))
2101                         return (EINVAL);
2102                 pdeath_signal = linux_to_bsd_signal(args->arg2);
2103                 return (kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_CTL,
2104                     &pdeath_signal));
2105         case LINUX_PR_GET_PDEATHSIG:
2106                 error = kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_STATUS,
2107                     &pdeath_signal);
2108                 if (error != 0)
2109                         return (error);
2110                 pdeath_signal = bsd_to_linux_signal(pdeath_signal);
2111                 return (copyout(&pdeath_signal,
2112                     (void *)(register_t)args->arg2,
2113                     sizeof(pdeath_signal)));
2114                 break;
2115         case LINUX_PR_GET_KEEPCAPS:
2116                 /*
2117                  * Indicate that we always clear the effective and
2118                  * permitted capability sets when the user id becomes
2119                  * non-zero (actually the capability sets are simply
2120                  * always zero in the current implementation).
2121                  */
2122                 td->td_retval[0] = 0;
2123                 break;
2124         case LINUX_PR_SET_KEEPCAPS:
2125                 /*
2126                  * Ignore requests to keep the effective and permitted
2127                  * capability sets when the user id becomes non-zero.
2128                  */
2129                 break;
2130         case LINUX_PR_SET_NAME:
2131                 /*
2132                  * To be on the safe side we need to make sure to not
2133                  * overflow the size a Linux program expects. We already
2134                  * do this here in the copyin, so that we don't need to
2135                  * check on copyout.
2136                  */
2137                 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
2138                 error = copyinstr((void *)(register_t)args->arg2, comm,
2139                     max_size, NULL);
2140
2141                 /* Linux silently truncates the name if it is too long. */
2142                 if (error == ENAMETOOLONG) {
2143                         /*
2144                          * XXX: copyinstr() isn't documented to populate the
2145                          * array completely, so do a copyin() to be on the
2146                          * safe side. This should be changed in case
2147                          * copyinstr() is changed to guarantee this.
2148                          */
2149                         error = copyin((void *)(register_t)args->arg2, comm,
2150                             max_size - 1);
2151                         comm[max_size - 1] = '\0';
2152                 }
2153                 if (error)
2154                         return (error);
2155
2156                 PROC_LOCK(p);
2157                 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
2158                 PROC_UNLOCK(p);
2159                 break;
2160         case LINUX_PR_GET_NAME:
2161                 PROC_LOCK(p);
2162                 strlcpy(comm, p->p_comm, sizeof(comm));
2163                 PROC_UNLOCK(p);
2164                 error = copyout(comm, (void *)(register_t)args->arg2,
2165                     strlen(comm) + 1);
2166                 break;
2167         default:
2168                 error = EINVAL;
2169                 break;
2170         }
2171
2172         return (error);
2173 }
2174
2175 int
2176 linux_sched_setparam(struct thread *td,
2177     struct linux_sched_setparam_args *uap)
2178 {
2179         struct sched_param sched_param;
2180         struct thread *tdt;
2181         int error, policy;
2182
2183 #ifdef DEBUG
2184         if (ldebug(sched_setparam))
2185                 printf(ARGS(sched_setparam, "%d, *"), uap->pid);
2186 #endif
2187
2188         error = copyin(uap->param, &sched_param, sizeof(sched_param));
2189         if (error)
2190                 return (error);
2191
2192         tdt = linux_tdfind(td, uap->pid, -1);
2193         if (tdt == NULL)
2194                 return (ESRCH);
2195
2196         if (linux_map_sched_prio) {
2197                 error = kern_sched_getscheduler(td, tdt, &policy);
2198                 if (error)
2199                         goto out;
2200
2201                 switch (policy) {
2202                 case SCHED_OTHER:
2203                         if (sched_param.sched_priority != 0) {
2204                                 error = EINVAL;
2205                                 goto out;
2206                         }
2207                         sched_param.sched_priority =
2208                             PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
2209                         break;
2210                 case SCHED_FIFO:
2211                 case SCHED_RR:
2212                         if (sched_param.sched_priority < 1 ||
2213                             sched_param.sched_priority >= LINUX_MAX_RT_PRIO) {
2214                                 error = EINVAL;
2215                                 goto out;
2216                         }
2217                         /*
2218                          * Map [1, LINUX_MAX_RT_PRIO - 1] to
2219                          * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
2220                          */
2221                         sched_param.sched_priority =
2222                             (sched_param.sched_priority - 1) *
2223                             (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
2224                             (LINUX_MAX_RT_PRIO - 1);
2225                         break;
2226                 }
2227         }
2228
2229         error = kern_sched_setparam(td, tdt, &sched_param);
2230 out:    PROC_UNLOCK(tdt->td_proc);
2231         return (error);
2232 }
2233
2234 int
2235 linux_sched_getparam(struct thread *td,
2236     struct linux_sched_getparam_args *uap)
2237 {
2238         struct sched_param sched_param;
2239         struct thread *tdt;
2240         int error, policy;
2241
2242 #ifdef DEBUG
2243         if (ldebug(sched_getparam))
2244                 printf(ARGS(sched_getparam, "%d, *"), uap->pid);
2245 #endif
2246
2247         tdt = linux_tdfind(td, uap->pid, -1);
2248         if (tdt == NULL)
2249                 return (ESRCH);
2250
2251         error = kern_sched_getparam(td, tdt, &sched_param);
2252         if (error) {
2253                 PROC_UNLOCK(tdt->td_proc);
2254                 return (error);
2255         }
2256
2257         if (linux_map_sched_prio) {
2258                 error = kern_sched_getscheduler(td, tdt, &policy);
2259                 PROC_UNLOCK(tdt->td_proc);
2260                 if (error)
2261                         return (error);
2262
2263                 switch (policy) {
2264                 case SCHED_OTHER:
2265                         sched_param.sched_priority = 0;
2266                         break;
2267                 case SCHED_FIFO:
2268                 case SCHED_RR:
2269                         /*
2270                          * Map [0, RTP_PRIO_MAX - RTP_PRIO_MIN] to
2271                          * [1, LINUX_MAX_RT_PRIO - 1] (rounding up).
2272                          */
2273                         sched_param.sched_priority =
2274                             (sched_param.sched_priority *
2275                             (LINUX_MAX_RT_PRIO - 1) +
2276                             (RTP_PRIO_MAX - RTP_PRIO_MIN - 1)) /
2277                             (RTP_PRIO_MAX - RTP_PRIO_MIN) + 1;
2278                         break;
2279                 }
2280         } else
2281                 PROC_UNLOCK(tdt->td_proc);
2282
2283         error = copyout(&sched_param, uap->param, sizeof(sched_param));
2284         return (error);
2285 }
2286
2287 /*
2288  * Get affinity of a process.
2289  */
2290 int
2291 linux_sched_getaffinity(struct thread *td,
2292     struct linux_sched_getaffinity_args *args)
2293 {
2294         int error;
2295         struct thread *tdt;
2296
2297 #ifdef DEBUG
2298         if (ldebug(sched_getaffinity))
2299                 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
2300                     args->len);
2301 #endif
2302         if (args->len < sizeof(cpuset_t))
2303                 return (EINVAL);
2304
2305         tdt = linux_tdfind(td, args->pid, -1);
2306         if (tdt == NULL)
2307                 return (ESRCH);
2308
2309         PROC_UNLOCK(tdt->td_proc);
2310
2311         error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2312             tdt->td_tid, sizeof(cpuset_t), (cpuset_t *)args->user_mask_ptr);
2313         if (error == 0)
2314                 td->td_retval[0] = sizeof(cpuset_t);
2315
2316         return (error);
2317 }
2318
2319 /*
2320  *  Set affinity of a process.
2321  */
2322 int
2323 linux_sched_setaffinity(struct thread *td,
2324     struct linux_sched_setaffinity_args *args)
2325 {
2326         struct thread *tdt;
2327
2328 #ifdef DEBUG
2329         if (ldebug(sched_setaffinity))
2330                 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
2331                     args->len);
2332 #endif
2333         if (args->len < sizeof(cpuset_t))
2334                 return (EINVAL);
2335
2336         tdt = linux_tdfind(td, args->pid, -1);
2337         if (tdt == NULL)
2338                 return (ESRCH);
2339
2340         PROC_UNLOCK(tdt->td_proc);
2341
2342         return (kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2343             tdt->td_tid, sizeof(cpuset_t), (cpuset_t *) args->user_mask_ptr));
2344 }
2345
2346 struct linux_rlimit64 {
2347         uint64_t        rlim_cur;
2348         uint64_t        rlim_max;
2349 };
2350
2351 int
2352 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2353 {
2354         struct rlimit rlim, nrlim;
2355         struct linux_rlimit64 lrlim;
2356         struct proc *p;
2357         u_int which;
2358         int flags;
2359         int error;
2360
2361 #ifdef DEBUG
2362         if (ldebug(prlimit64))
2363                 printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid,
2364                     args->resource, (void *)args->new, (void *)args->old);
2365 #endif
2366
2367         if (args->resource >= LINUX_RLIM_NLIMITS)
2368                 return (EINVAL);
2369
2370         which = linux_to_bsd_resource[args->resource];
2371         if (which == -1)
2372                 return (EINVAL);
2373
2374         if (args->new != NULL) {
2375                 /*
2376                  * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2377                  * rlim is unsigned 64-bit. FreeBSD treats negative limits
2378                  * as INFINITY so we do not need a conversion even.
2379                  */
2380                 error = copyin(args->new, &nrlim, sizeof(nrlim));
2381                 if (error != 0)
2382                         return (error);
2383         }
2384
2385         flags = PGET_HOLD | PGET_NOTWEXIT;
2386         if (args->new != NULL)
2387                 flags |= PGET_CANDEBUG;
2388         else
2389                 flags |= PGET_CANSEE;
2390         error = pget(args->pid, flags, &p);
2391         if (error != 0)
2392                 return (error);
2393
2394         if (args->old != NULL) {
2395                 PROC_LOCK(p);
2396                 lim_rlimit_proc(p, which, &rlim);
2397                 PROC_UNLOCK(p);
2398                 if (rlim.rlim_cur == RLIM_INFINITY)
2399                         lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2400                 else
2401                         lrlim.rlim_cur = rlim.rlim_cur;
2402                 if (rlim.rlim_max == RLIM_INFINITY)
2403                         lrlim.rlim_max = LINUX_RLIM_INFINITY;
2404                 else
2405                         lrlim.rlim_max = rlim.rlim_max;
2406                 error = copyout(&lrlim, args->old, sizeof(lrlim));
2407                 if (error != 0)
2408                         goto out;
2409         }
2410
2411         if (args->new != NULL)
2412                 error = kern_proc_setrlimit(td, p, which, &nrlim);
2413
2414  out:
2415         PRELE(p);
2416         return (error);
2417 }
2418
2419 int
2420 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2421 {
2422         struct timeval utv, tv0, tv1, *tvp;
2423         struct l_pselect6arg lpse6;
2424         struct l_timespec lts;
2425         struct timespec uts;
2426         l_sigset_t l_ss;
2427         sigset_t *ssp;
2428         sigset_t ss;
2429         int error;
2430
2431         ssp = NULL;
2432         if (args->sig != NULL) {
2433                 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2434                 if (error != 0)
2435                         return (error);
2436                 if (lpse6.ss_len != sizeof(l_ss))
2437                         return (EINVAL);
2438                 if (lpse6.ss != 0) {
2439                         error = copyin(PTRIN(lpse6.ss), &l_ss,
2440                             sizeof(l_ss));
2441                         if (error != 0)
2442                                 return (error);
2443                         linux_to_bsd_sigset(&l_ss, &ss);
2444                         ssp = &ss;
2445                 }
2446         }
2447
2448         /*
2449          * Currently glibc changes nanosecond number to microsecond.
2450          * This mean losing precision but for now it is hardly seen.
2451          */
2452         if (args->tsp != NULL) {
2453                 error = copyin(args->tsp, &lts, sizeof(lts));
2454                 if (error != 0)
2455                         return (error);
2456                 error = linux_to_native_timespec(&uts, &lts);
2457                 if (error != 0)
2458                         return (error);
2459
2460                 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2461                 if (itimerfix(&utv))
2462                         return (EINVAL);
2463
2464                 microtime(&tv0);
2465                 tvp = &utv;
2466         } else
2467                 tvp = NULL;
2468
2469         error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2470             args->exceptfds, tvp, ssp, LINUX_NFDBITS);
2471
2472         if (error == 0 && args->tsp != NULL) {
2473                 if (td->td_retval[0] != 0) {
2474                         /*
2475                          * Compute how much time was left of the timeout,
2476                          * by subtracting the current time and the time
2477                          * before we started the call, and subtracting
2478                          * that result from the user-supplied value.
2479                          */
2480
2481                         microtime(&tv1);
2482                         timevalsub(&tv1, &tv0);
2483                         timevalsub(&utv, &tv1);
2484                         if (utv.tv_sec < 0)
2485                                 timevalclear(&utv);
2486                 } else
2487                         timevalclear(&utv);
2488
2489                 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2490
2491                 error = native_to_linux_timespec(&lts, &uts);
2492                 if (error == 0)
2493                         error = copyout(&lts, args->tsp, sizeof(lts));
2494         }
2495
2496         return (error);
2497 }
2498
2499 int
2500 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2501 {
2502         struct timespec ts0, ts1;
2503         struct l_timespec lts;
2504         struct timespec uts, *tsp;
2505         l_sigset_t l_ss;
2506         sigset_t *ssp;
2507         sigset_t ss;
2508         int error;
2509
2510         if (args->sset != NULL) {
2511                 if (args->ssize != sizeof(l_ss))
2512                         return (EINVAL);
2513                 error = copyin(args->sset, &l_ss, sizeof(l_ss));
2514                 if (error)
2515                         return (error);
2516                 linux_to_bsd_sigset(&l_ss, &ss);
2517                 ssp = &ss;
2518         } else
2519                 ssp = NULL;
2520         if (args->tsp != NULL) {
2521                 error = copyin(args->tsp, &lts, sizeof(lts));
2522                 if (error)
2523                         return (error);
2524                 error = linux_to_native_timespec(&uts, &lts);
2525                 if (error != 0)
2526                         return (error);
2527
2528                 nanotime(&ts0);
2529                 tsp = &uts;
2530         } else
2531                 tsp = NULL;
2532
2533         error = kern_poll(td, args->fds, args->nfds, tsp, ssp);
2534
2535         if (error == 0 && args->tsp != NULL) {
2536                 if (td->td_retval[0]) {
2537                         nanotime(&ts1);
2538                         timespecsub(&ts1, &ts0, &ts1);
2539                         timespecsub(&uts, &ts1, &uts);
2540                         if (uts.tv_sec < 0)
2541                                 timespecclear(&uts);
2542                 } else
2543                         timespecclear(&uts);
2544
2545                 error = native_to_linux_timespec(&lts, &uts);
2546                 if (error == 0)
2547                         error = copyout(&lts, args->tsp, sizeof(lts));
2548         }
2549
2550         return (error);
2551 }
2552
2553 #if defined(DEBUG) || defined(KTR)
2554 /* XXX: can be removed when every ldebug(...) and KTR stuff are removed. */
2555
2556 #ifdef COMPAT_LINUX32
2557 #define L_MAXSYSCALL    LINUX32_SYS_MAXSYSCALL
2558 #else
2559 #define L_MAXSYSCALL    LINUX_SYS_MAXSYSCALL
2560 #endif
2561
2562 u_char linux_debug_map[howmany(L_MAXSYSCALL, sizeof(u_char))];
2563
2564 static int
2565 linux_debug(int syscall, int toggle, int global)
2566 {
2567
2568         if (global) {
2569                 char c = toggle ? 0 : 0xff;
2570
2571                 memset(linux_debug_map, c, sizeof(linux_debug_map));
2572                 return (0);
2573         }
2574         if (syscall < 0 || syscall >= L_MAXSYSCALL)
2575                 return (EINVAL);
2576         if (toggle)
2577                 clrbit(linux_debug_map, syscall);
2578         else
2579                 setbit(linux_debug_map, syscall);
2580         return (0);
2581 }
2582 #undef L_MAXSYSCALL
2583
2584 /*
2585  * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
2586  *
2587  *    E.g.: sysctl linux.debug=21.0
2588  *
2589  * As a special case, syscall "all" will apply to all syscalls globally.
2590  */
2591 #define LINUX_MAX_DEBUGSTR      16
2592 int
2593 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
2594 {
2595         char value[LINUX_MAX_DEBUGSTR], *p;
2596         int error, sysc, toggle;
2597         int global = 0;
2598
2599         value[0] = '\0';
2600         error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
2601         if (error || req->newptr == NULL)
2602                 return (error);
2603         for (p = value; *p != '\0' && *p != '.'; p++);
2604         if (*p == '\0')
2605                 return (EINVAL);
2606         *p++ = '\0';
2607         sysc = strtol(value, NULL, 0);
2608         toggle = strtol(p, NULL, 0);
2609         if (strcmp(value, "all") == 0)
2610                 global = 1;
2611         error = linux_debug(sysc, toggle, global);
2612         return (error);
2613 }
2614
2615 #endif /* DEBUG || KTR */
2616
2617 int
2618 linux_sched_rr_get_interval(struct thread *td,
2619     struct linux_sched_rr_get_interval_args *uap)
2620 {
2621         struct timespec ts;
2622         struct l_timespec lts;
2623         struct thread *tdt;
2624         int error;
2625
2626         /*
2627          * According to man in case the invalid pid specified
2628          * EINVAL should be returned.
2629          */
2630         if (uap->pid < 0)
2631                 return (EINVAL);
2632
2633         tdt = linux_tdfind(td, uap->pid, -1);
2634         if (tdt == NULL)
2635                 return (ESRCH);
2636
2637         error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2638         PROC_UNLOCK(tdt->td_proc);
2639         if (error != 0)
2640                 return (error);
2641         error = native_to_linux_timespec(&lts, &ts);
2642         if (error != 0)
2643                 return (error);
2644         return (copyout(&lts, uap->interval, sizeof(lts)));
2645 }
2646
2647 /*
2648  * In case when the Linux thread is the initial thread in
2649  * the thread group thread id is equal to the process id.
2650  * Glibc depends on this magic (assert in pthread_getattr_np.c).
2651  */
2652 struct thread *
2653 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2654 {
2655         struct linux_emuldata *em;
2656         struct thread *tdt;
2657         struct proc *p;
2658
2659         tdt = NULL;
2660         if (tid == 0 || tid == td->td_tid) {
2661                 tdt = td;
2662                 PROC_LOCK(tdt->td_proc);
2663         } else if (tid > PID_MAX)
2664                 tdt = tdfind(tid, pid);
2665         else {
2666                 /*
2667                  * Initial thread where the tid equal to the pid.
2668                  */
2669                 p = pfind(tid);
2670                 if (p != NULL) {
2671                         if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2672                                 /*
2673                                  * p is not a Linuxulator process.
2674                                  */
2675                                 PROC_UNLOCK(p);
2676                                 return (NULL);
2677                         }
2678                         FOREACH_THREAD_IN_PROC(p, tdt) {
2679                                 em = em_find(tdt);
2680                                 if (tid == em->em_tid)
2681                                         return (tdt);
2682                         }
2683                         PROC_UNLOCK(p);
2684                 }
2685                 return (NULL);
2686         }
2687
2688         return (tdt);
2689 }
2690
2691 void
2692 linux_to_bsd_waitopts(int options, int *bsdopts)
2693 {
2694
2695         if (options & LINUX_WNOHANG)
2696                 *bsdopts |= WNOHANG;
2697         if (options & LINUX_WUNTRACED)
2698                 *bsdopts |= WUNTRACED;
2699         if (options & LINUX_WEXITED)
2700                 *bsdopts |= WEXITED;
2701         if (options & LINUX_WCONTINUED)
2702                 *bsdopts |= WCONTINUED;
2703         if (options & LINUX_WNOWAIT)
2704                 *bsdopts |= WNOWAIT;
2705
2706         if (options & __WCLONE)
2707                 *bsdopts |= WLINUXCLONE;
2708 }
2709
2710 int
2711 linux_getrandom(struct thread *td, struct linux_getrandom_args *args)
2712 {
2713         struct uio uio;
2714         struct iovec iov;
2715         int error;
2716
2717         if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM))
2718                 return (EINVAL);
2719         if (args->count > INT_MAX)
2720                 args->count = INT_MAX;
2721
2722         iov.iov_base = args->buf;
2723         iov.iov_len = args->count;
2724
2725         uio.uio_iov = &iov;
2726         uio.uio_iovcnt = 1;
2727         uio.uio_resid = iov.iov_len;
2728         uio.uio_segflg = UIO_USERSPACE;
2729         uio.uio_rw = UIO_READ;
2730         uio.uio_td = td;
2731
2732         error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK);
2733         if (error == 0)
2734                 td->td_retval[0] = args->count - uio.uio_resid;
2735         return (error);
2736 }
2737
2738 int
2739 linux_mincore(struct thread *td, struct linux_mincore_args *args)
2740 {
2741
2742         /* Needs to be page-aligned */
2743         if (args->start & PAGE_MASK)
2744                 return (EINVAL);
2745         return (kern_mincore(td, args->start, args->len, args->vec));
2746 }
2747
2748 #define SYSLOG_TAG      "<6>"
2749
2750 int
2751 linux_syslog(struct thread *td, struct linux_syslog_args *args)
2752 {
2753         char buf[128], *src, *dst;
2754         u_int seq;
2755         int buflen, error;
2756
2757         if (args->type != LINUX_SYSLOG_ACTION_READ_ALL) {
2758                 linux_msg(td, "syslog unsupported type 0x%x", args->type);
2759                 return (EINVAL);
2760         }
2761
2762         if (args->len < 6) {
2763                 td->td_retval[0] = 0;
2764                 return (0);
2765         }
2766
2767         error = priv_check(td, PRIV_MSGBUF);
2768         if (error)
2769                 return (error);
2770
2771         mtx_lock(&msgbuf_lock);
2772         msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
2773         mtx_unlock(&msgbuf_lock);
2774
2775         dst = args->buf;
2776         error = copyout(&SYSLOG_TAG, dst, sizeof(SYSLOG_TAG));
2777         /* The -1 is to skip the trailing '\0'. */
2778         dst += sizeof(SYSLOG_TAG) - 1;
2779
2780         while (error == 0) {
2781                 mtx_lock(&msgbuf_lock);
2782                 buflen = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq);
2783                 mtx_unlock(&msgbuf_lock);
2784
2785                 if (buflen == 0)
2786                         break;
2787
2788                 for (src = buf; src < buf + buflen && error == 0; src++) {
2789                         if (*src == '\0')
2790                                 continue;
2791
2792                         if (dst >= args->buf + args->len)
2793                                 goto out;
2794
2795                         error = copyout(src, dst, 1);
2796                         dst++;
2797
2798                         if (*src == '\n' && *(src + 1) != '<' &&
2799                             dst + sizeof(SYSLOG_TAG) < args->buf + args->len) {
2800                                 error = copyout(&SYSLOG_TAG,
2801                                     dst, sizeof(SYSLOG_TAG));
2802                                 dst += sizeof(SYSLOG_TAG) - 1;
2803                         }
2804                 }
2805         }
2806 out:
2807         td->td_retval[0] = dst - args->buf;
2808         return (error);
2809 }
2810
2811 int
2812 linux_getcpu(struct thread *td, struct linux_getcpu_args *args)
2813 {
2814         int cpu, error, node;
2815
2816         cpu = td->td_oncpu; /* Make sure it doesn't change during copyout(9) */
2817         error = 0;
2818         node = 0; /* XXX: Fake NUMA node 0 for now */
2819
2820         if (args->cpu != NULL)
2821                 error = copyout(&cpu, args->cpu, sizeof(l_int));
2822         if (args->node != NULL)
2823                 error = copyout(&node, args->node, sizeof(l_int));
2824         return (error);
2825 }