]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/compat/linux/linux_misc.c
MFC r283415:
[FreeBSD/stable/10.git] / sys / compat / linux / linux_misc.c
1 /*-
2  * Copyright (c) 2002 Doug Rabson
3  * Copyright (c) 1994-1995 Søren Schmidt
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_compat.h"
34 #include "opt_kdtrace.h"
35
36 #include <sys/param.h>
37 #include <sys/blist.h>
38 #include <sys/fcntl.h>
39 #if defined(__i386__)
40 #include <sys/imgact_aout.h>
41 #endif
42 #include <sys/jail.h>
43 #include <sys/kernel.h>
44 #include <sys/limits.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mman.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/reboot.h>
54 #include <sys/racct.h>
55 #include <sys/resourcevar.h>
56 #include <sys/sched.h>
57 #include <sys/signalvar.h>
58 #include <sys/stat.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysproto.h>
62 #include <sys/systm.h>
63 #include <sys/time.h>
64 #include <sys/vmmeter.h>
65 #include <sys/vnode.h>
66 #include <sys/wait.h>
67 #include <sys/cpuset.h>
68
69 #include <security/mac/mac_framework.h>
70
71 #include <vm/vm.h>
72 #include <vm/pmap.h>
73 #include <vm/vm_kern.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_object.h>
77 #include <vm/swap_pager.h>
78
79 #ifdef COMPAT_LINUX32
80 #include <machine/../linux32/linux.h>
81 #include <machine/../linux32/linux32_proto.h>
82 #else
83 #include <machine/../linux/linux.h>
84 #include <machine/../linux/linux_proto.h>
85 #endif
86
87 #include <compat/linux/linux_file.h>
88 #include <compat/linux/linux_mib.h>
89 #include <compat/linux/linux_signal.h>
90 #include <compat/linux/linux_util.h>
91 #include <compat/linux/linux_sysproto.h>
92 #include <compat/linux/linux_emul.h>
93 #include <compat/linux/linux_misc.h>
94
95 int stclohz;                            /* Statistics clock frequency */
96
97 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
98         RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
99         RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
100         RLIMIT_MEMLOCK, RLIMIT_AS 
101 };
102
103 struct l_sysinfo {
104         l_long          uptime;         /* Seconds since boot */
105         l_ulong         loads[3];       /* 1, 5, and 15 minute load averages */
106 #define LINUX_SYSINFO_LOADS_SCALE 65536
107         l_ulong         totalram;       /* Total usable main memory size */
108         l_ulong         freeram;        /* Available memory size */
109         l_ulong         sharedram;      /* Amount of shared memory */
110         l_ulong         bufferram;      /* Memory used by buffers */
111         l_ulong         totalswap;      /* Total swap space size */
112         l_ulong         freeswap;       /* swap space still available */
113         l_ushort        procs;          /* Number of current processes */
114         l_ushort        pads;
115         l_ulong         totalbig;
116         l_ulong         freebig;
117         l_uint          mem_unit;
118         char            _f[20-2*sizeof(l_long)-sizeof(l_int)];  /* padding */
119 };
120
121 struct l_pselect6arg {
122         l_uintptr_t     ss;
123         l_size_t        ss_len;
124 };
125
126 int
127 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
128 {
129         struct l_sysinfo sysinfo;
130         vm_object_t object;
131         int i, j;
132         struct timespec ts;
133
134         getnanouptime(&ts);
135         if (ts.tv_nsec != 0)
136                 ts.tv_sec++;
137         sysinfo.uptime = ts.tv_sec;
138
139         /* Use the information from the mib to get our load averages */
140         for (i = 0; i < 3; i++)
141                 sysinfo.loads[i] = averunnable.ldavg[i] *
142                     LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
143
144         sysinfo.totalram = physmem * PAGE_SIZE;
145         sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE;
146
147         sysinfo.sharedram = 0;
148         mtx_lock(&vm_object_list_mtx);
149         TAILQ_FOREACH(object, &vm_object_list, object_list)
150                 if (object->shadow_count > 1)
151                         sysinfo.sharedram += object->resident_page_count;
152         mtx_unlock(&vm_object_list_mtx);
153
154         sysinfo.sharedram *= PAGE_SIZE;
155         sysinfo.bufferram = 0;
156
157         swap_pager_status(&i, &j);
158         sysinfo.totalswap = i * PAGE_SIZE;
159         sysinfo.freeswap = (i - j) * PAGE_SIZE;
160
161         sysinfo.procs = nprocs;
162
163         /* The following are only present in newer Linux kernels. */
164         sysinfo.totalbig = 0;
165         sysinfo.freebig = 0;
166         sysinfo.mem_unit = 1;
167
168         return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
169 }
170
171 int
172 linux_alarm(struct thread *td, struct linux_alarm_args *args)
173 {
174         struct itimerval it, old_it;
175         u_int secs;
176         int error;
177
178 #ifdef DEBUG
179         if (ldebug(alarm))
180                 printf(ARGS(alarm, "%u"), args->secs);
181 #endif
182         
183         secs = args->secs;
184
185         if (secs > INT_MAX)
186                 secs = INT_MAX;
187
188         it.it_value.tv_sec = (long) secs;
189         it.it_value.tv_usec = 0;
190         it.it_interval.tv_sec = 0;
191         it.it_interval.tv_usec = 0;
192         error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
193         if (error)
194                 return (error);
195         if (timevalisset(&old_it.it_value)) {
196                 if (old_it.it_value.tv_usec != 0)
197                         old_it.it_value.tv_sec++;
198                 td->td_retval[0] = old_it.it_value.tv_sec;
199         }
200         return (0);
201 }
202
203 int
204 linux_brk(struct thread *td, struct linux_brk_args *args)
205 {
206         struct vmspace *vm = td->td_proc->p_vmspace;
207         vm_offset_t new, old;
208         struct obreak_args /* {
209                 char * nsize;
210         } */ tmp;
211
212 #ifdef DEBUG
213         if (ldebug(brk))
214                 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend);
215 #endif
216         old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize);
217         new = (vm_offset_t)args->dsend;
218         tmp.nsize = (char *)new;
219         if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp))
220                 td->td_retval[0] = (long)new;
221         else
222                 td->td_retval[0] = (long)old;
223
224         return (0);
225 }
226
227 #if defined(__i386__)
228 /* XXX: what about amd64/linux32? */
229
230 int
231 linux_uselib(struct thread *td, struct linux_uselib_args *args)
232 {
233         struct nameidata ni;
234         struct vnode *vp;
235         struct exec *a_out;
236         struct vattr attr;
237         vm_offset_t vmaddr;
238         unsigned long file_offset;
239         unsigned long bss_size;
240         char *library;
241         ssize_t aresid;
242         int error, locked, writecount;
243
244         LCONVPATHEXIST(td, args->library, &library);
245
246 #ifdef DEBUG
247         if (ldebug(uselib))
248                 printf(ARGS(uselib, "%s"), library);
249 #endif
250
251         a_out = NULL;
252         locked = 0;
253         vp = NULL;
254
255         NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1,
256             UIO_SYSSPACE, library, td);
257         error = namei(&ni);
258         LFREEPATH(library);
259         if (error)
260                 goto cleanup;
261
262         vp = ni.ni_vp;
263         NDFREE(&ni, NDF_ONLY_PNBUF);
264
265         /*
266          * From here on down, we have a locked vnode that must be unlocked.
267          * XXX: The code below largely duplicates exec_check_permissions().
268          */
269         locked = 1;
270
271         /* Writable? */
272         error = VOP_GET_WRITECOUNT(vp, &writecount);
273         if (error != 0)
274                 goto cleanup;
275         if (writecount != 0) {
276                 error = ETXTBSY;
277                 goto cleanup;
278         }
279
280         /* Executable? */
281         error = VOP_GETATTR(vp, &attr, td->td_ucred);
282         if (error)
283                 goto cleanup;
284
285         if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
286             ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) {
287                 /* EACCESS is what exec(2) returns. */
288                 error = ENOEXEC;
289                 goto cleanup;
290         }
291
292         /* Sensible size? */
293         if (attr.va_size == 0) {
294                 error = ENOEXEC;
295                 goto cleanup;
296         }
297
298         /* Can we access it? */
299         error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
300         if (error)
301                 goto cleanup;
302
303         /*
304          * XXX: This should use vn_open() so that it is properly authorized,
305          * and to reduce code redundancy all over the place here.
306          * XXX: Not really, it duplicates far more of exec_check_permissions()
307          * than vn_open().
308          */
309 #ifdef MAC
310         error = mac_vnode_check_open(td->td_ucred, vp, VREAD);
311         if (error)
312                 goto cleanup;
313 #endif
314         error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL);
315         if (error)
316                 goto cleanup;
317
318         /* Pull in executable header into exec_map */
319         error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE,
320             VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0);
321         if (error)
322                 goto cleanup;
323
324         /* Is it a Linux binary ? */
325         if (((a_out->a_magic >> 16) & 0xff) != 0x64) {
326                 error = ENOEXEC;
327                 goto cleanup;
328         }
329
330         /*
331          * While we are here, we should REALLY do some more checks
332          */
333
334         /* Set file/virtual offset based on a.out variant. */
335         switch ((int)(a_out->a_magic & 0xffff)) {
336         case 0413:                      /* ZMAGIC */
337                 file_offset = 1024;
338                 break;
339         case 0314:                      /* QMAGIC */
340                 file_offset = 0;
341                 break;
342         default:
343                 error = ENOEXEC;
344                 goto cleanup;
345         }
346
347         bss_size = round_page(a_out->a_bss);
348
349         /* Check various fields in header for validity/bounds. */
350         if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) {
351                 error = ENOEXEC;
352                 goto cleanup;
353         }
354
355         /* text + data can't exceed file size */
356         if (a_out->a_data + a_out->a_text > attr.va_size) {
357                 error = EFAULT;
358                 goto cleanup;
359         }
360
361         /*
362          * text/data/bss must not exceed limits
363          * XXX - this is not complete. it should check current usage PLUS
364          * the resources needed by this library.
365          */
366         PROC_LOCK(td->td_proc);
367         if (a_out->a_text > maxtsiz ||
368             a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA) ||
369             racct_set(td->td_proc, RACCT_DATA, a_out->a_data +
370             bss_size) != 0) {
371                 PROC_UNLOCK(td->td_proc);
372                 error = ENOMEM;
373                 goto cleanup;
374         }
375         PROC_UNLOCK(td->td_proc);
376
377         /*
378          * Prevent more writers.
379          * XXX: Note that if any of the VM operations fail below we don't
380          * clear this flag.
381          */
382         VOP_SET_TEXT(vp);
383
384         /*
385          * Lock no longer needed
386          */
387         locked = 0;
388         VOP_UNLOCK(vp, 0);
389
390         /*
391          * Check if file_offset page aligned. Currently we cannot handle
392          * misalinged file offsets, and so we read in the entire image
393          * (what a waste).
394          */
395         if (file_offset & PAGE_MASK) {
396 #ifdef DEBUG
397                 printf("uselib: Non page aligned binary %lu\n", file_offset);
398 #endif
399                 /* Map text+data read/write/execute */
400
401                 /* a_entry is the load address and is page aligned */
402                 vmaddr = trunc_page(a_out->a_entry);
403
404                 /* get anon user mapping, read+write+execute */
405                 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
406                     &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE,
407                     VM_PROT_ALL, VM_PROT_ALL, 0);
408                 if (error)
409                         goto cleanup;
410
411                 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset,
412                     a_out->a_text + a_out->a_data, UIO_USERSPACE, 0,
413                     td->td_ucred, NOCRED, &aresid, td);
414                 if (error != 0)
415                         goto cleanup;
416                 if (aresid != 0) {
417                         error = ENOEXEC;
418                         goto cleanup;
419                 }
420         } else {
421 #ifdef DEBUG
422                 printf("uselib: Page aligned binary %lu\n", file_offset);
423 #endif
424                 /*
425                  * for QMAGIC, a_entry is 20 bytes beyond the load address
426                  * to skip the executable header
427                  */
428                 vmaddr = trunc_page(a_out->a_entry);
429
430                 /*
431                  * Map it all into the process's space as a single
432                  * copy-on-write "data" segment.
433                  */
434                 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr,
435                     a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL,
436                     MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset);
437                 if (error)
438                         goto cleanup;
439         }
440 #ifdef DEBUG
441         printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0],
442             ((long *)vmaddr)[1]);
443 #endif
444         if (bss_size != 0) {
445                 /* Calculate BSS start address */
446                 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text +
447                     a_out->a_data;
448
449                 /* allocate some 'anon' space */
450                 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0,
451                     &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL,
452                     VM_PROT_ALL, 0);
453                 if (error)
454                         goto cleanup;
455         }
456
457 cleanup:
458         /* Unlock vnode if needed */
459         if (locked)
460                 VOP_UNLOCK(vp, 0);
461
462         /* Release the temporary mapping. */
463         if (a_out)
464                 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE);
465
466         return (error);
467 }
468
469 #endif  /* __i386__ */
470
471 int
472 linux_select(struct thread *td, struct linux_select_args *args)
473 {
474         l_timeval ltv;
475         struct timeval tv0, tv1, utv, *tvp;
476         int error;
477
478 #ifdef DEBUG
479         if (ldebug(select))
480                 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds,
481                     (void *)args->readfds, (void *)args->writefds,
482                     (void *)args->exceptfds, (void *)args->timeout);
483 #endif
484
485         /*
486          * Store current time for computation of the amount of
487          * time left.
488          */
489         if (args->timeout) {
490                 if ((error = copyin(args->timeout, &ltv, sizeof(ltv))))
491                         goto select_out;
492                 utv.tv_sec = ltv.tv_sec;
493                 utv.tv_usec = ltv.tv_usec;
494 #ifdef DEBUG
495                 if (ldebug(select))
496                         printf(LMSG("incoming timeout (%jd/%ld)"),
497                             (intmax_t)utv.tv_sec, utv.tv_usec);
498 #endif
499
500                 if (itimerfix(&utv)) {
501                         /*
502                          * The timeval was invalid.  Convert it to something
503                          * valid that will act as it does under Linux.
504                          */
505                         utv.tv_sec += utv.tv_usec / 1000000;
506                         utv.tv_usec %= 1000000;
507                         if (utv.tv_usec < 0) {
508                                 utv.tv_sec -= 1;
509                                 utv.tv_usec += 1000000;
510                         }
511                         if (utv.tv_sec < 0)
512                                 timevalclear(&utv);
513                 }
514                 microtime(&tv0);
515                 tvp = &utv;
516         } else
517                 tvp = NULL;
518
519         error = kern_select(td, args->nfds, args->readfds, args->writefds,
520             args->exceptfds, tvp, sizeof(l_int) * 8);
521
522 #ifdef DEBUG
523         if (ldebug(select))
524                 printf(LMSG("real select returns %d"), error);
525 #endif
526         if (error)
527                 goto select_out;
528
529         if (args->timeout) {
530                 if (td->td_retval[0]) {
531                         /*
532                          * Compute how much time was left of the timeout,
533                          * by subtracting the current time and the time
534                          * before we started the call, and subtracting
535                          * that result from the user-supplied value.
536                          */
537                         microtime(&tv1);
538                         timevalsub(&tv1, &tv0);
539                         timevalsub(&utv, &tv1);
540                         if (utv.tv_sec < 0)
541                                 timevalclear(&utv);
542                 } else
543                         timevalclear(&utv);
544 #ifdef DEBUG
545                 if (ldebug(select))
546                         printf(LMSG("outgoing timeout (%jd/%ld)"),
547                             (intmax_t)utv.tv_sec, utv.tv_usec);
548 #endif
549                 ltv.tv_sec = utv.tv_sec;
550                 ltv.tv_usec = utv.tv_usec;
551                 if ((error = copyout(&ltv, args->timeout, sizeof(ltv))))
552                         goto select_out;
553         }
554
555 select_out:
556 #ifdef DEBUG
557         if (ldebug(select))
558                 printf(LMSG("select_out -> %d"), error);
559 #endif
560         return (error);
561 }
562
563 int
564 linux_mremap(struct thread *td, struct linux_mremap_args *args)
565 {
566         struct munmap_args /* {
567                 void *addr;
568                 size_t len;
569         } */ bsd_args;
570         int error = 0;
571
572 #ifdef DEBUG
573         if (ldebug(mremap))
574                 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"),
575                     (void *)(uintptr_t)args->addr,
576                     (unsigned long)args->old_len,
577                     (unsigned long)args->new_len,
578                     (unsigned long)args->flags);
579 #endif
580
581         if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
582                 td->td_retval[0] = 0;
583                 return (EINVAL);
584         }
585
586         /*
587          * Check for the page alignment.
588          * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
589          */
590         if (args->addr & PAGE_MASK) {
591                 td->td_retval[0] = 0;
592                 return (EINVAL);
593         }
594
595         args->new_len = round_page(args->new_len);
596         args->old_len = round_page(args->old_len);
597
598         if (args->new_len > args->old_len) {
599                 td->td_retval[0] = 0;
600                 return (ENOMEM);
601         }
602
603         if (args->new_len < args->old_len) {
604                 bsd_args.addr =
605                     (caddr_t)((uintptr_t)args->addr + args->new_len);
606                 bsd_args.len = args->old_len - args->new_len;
607                 error = sys_munmap(td, &bsd_args);
608         }
609
610         td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
611         return (error);
612 }
613
614 #define LINUX_MS_ASYNC       0x0001
615 #define LINUX_MS_INVALIDATE  0x0002
616 #define LINUX_MS_SYNC        0x0004
617
618 int
619 linux_msync(struct thread *td, struct linux_msync_args *args)
620 {
621         struct msync_args bsd_args;
622
623         bsd_args.addr = (caddr_t)(uintptr_t)args->addr;
624         bsd_args.len = (uintptr_t)args->len;
625         bsd_args.flags = args->fl & ~LINUX_MS_SYNC;
626
627         return (sys_msync(td, &bsd_args));
628 }
629
630 int
631 linux_time(struct thread *td, struct linux_time_args *args)
632 {
633         struct timeval tv;
634         l_time_t tm;
635         int error;
636
637 #ifdef DEBUG
638         if (ldebug(time))
639                 printf(ARGS(time, "*"));
640 #endif
641
642         microtime(&tv);
643         tm = tv.tv_sec;
644         if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
645                 return (error);
646         td->td_retval[0] = tm;
647         return (0);
648 }
649
650 struct l_times_argv {
651         l_clock_t       tms_utime;
652         l_clock_t       tms_stime;
653         l_clock_t       tms_cutime;
654         l_clock_t       tms_cstime;
655 };
656
657
658 /*
659  * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
660  * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
661  * auxiliary vector entry.
662  */
663 #define CLK_TCK         100
664
665 #define CONVOTCK(r)     (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
666 #define CONVNTCK(r)     (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
667
668 #define CONVTCK(r)      (linux_kernver(td) >= LINUX_KERNVER_2004000 ?           \
669                             CONVNTCK(r) : CONVOTCK(r))
670
671 int
672 linux_times(struct thread *td, struct linux_times_args *args)
673 {
674         struct timeval tv, utime, stime, cutime, cstime;
675         struct l_times_argv tms;
676         struct proc *p;
677         int error;
678
679 #ifdef DEBUG
680         if (ldebug(times))
681                 printf(ARGS(times, "*"));
682 #endif
683
684         if (args->buf != NULL) {
685                 p = td->td_proc;
686                 PROC_LOCK(p);
687                 PROC_STATLOCK(p);
688                 calcru(p, &utime, &stime);
689                 PROC_STATUNLOCK(p);
690                 calccru(p, &cutime, &cstime);
691                 PROC_UNLOCK(p);
692
693                 tms.tms_utime = CONVTCK(utime);
694                 tms.tms_stime = CONVTCK(stime);
695
696                 tms.tms_cutime = CONVTCK(cutime);
697                 tms.tms_cstime = CONVTCK(cstime);
698
699                 if ((error = copyout(&tms, args->buf, sizeof(tms))))
700                         return (error);
701         }
702
703         microuptime(&tv);
704         td->td_retval[0] = (int)CONVTCK(tv);
705         return (0);
706 }
707
708 int
709 linux_newuname(struct thread *td, struct linux_newuname_args *args)
710 {
711         struct l_new_utsname utsname;
712         char osname[LINUX_MAX_UTSNAME];
713         char osrelease[LINUX_MAX_UTSNAME];
714         char *p;
715
716 #ifdef DEBUG
717         if (ldebug(newuname))
718                 printf(ARGS(newuname, "*"));
719 #endif
720
721         linux_get_osname(td, osname);
722         linux_get_osrelease(td, osrelease);
723
724         bzero(&utsname, sizeof(utsname));
725         strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
726         getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
727         getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
728         strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
729         strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
730         for (p = utsname.version; *p != '\0'; ++p)
731                 if (*p == '\n') {
732                         *p = '\0';
733                         break;
734                 }
735         strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME);
736
737         return (copyout(&utsname, args->buf, sizeof(utsname)));
738 }
739
740 struct l_utimbuf {
741         l_time_t l_actime;
742         l_time_t l_modtime;
743 };
744
745 int
746 linux_utime(struct thread *td, struct linux_utime_args *args)
747 {
748         struct timeval tv[2], *tvp;
749         struct l_utimbuf lut;
750         char *fname;
751         int error;
752
753         LCONVPATHEXIST(td, args->fname, &fname);
754
755 #ifdef DEBUG
756         if (ldebug(utime))
757                 printf(ARGS(utime, "%s, *"), fname);
758 #endif
759
760         if (args->times) {
761                 if ((error = copyin(args->times, &lut, sizeof lut))) {
762                         LFREEPATH(fname);
763                         return (error);
764                 }
765                 tv[0].tv_sec = lut.l_actime;
766                 tv[0].tv_usec = 0;
767                 tv[1].tv_sec = lut.l_modtime;
768                 tv[1].tv_usec = 0;
769                 tvp = tv;
770         } else
771                 tvp = NULL;
772
773         error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
774         LFREEPATH(fname);
775         return (error);
776 }
777
778 int
779 linux_utimes(struct thread *td, struct linux_utimes_args *args)
780 {
781         l_timeval ltv[2];
782         struct timeval tv[2], *tvp = NULL;
783         char *fname;
784         int error;
785
786         LCONVPATHEXIST(td, args->fname, &fname);
787
788 #ifdef DEBUG
789         if (ldebug(utimes))
790                 printf(ARGS(utimes, "%s, *"), fname);
791 #endif
792
793         if (args->tptr != NULL) {
794                 if ((error = copyin(args->tptr, ltv, sizeof ltv))) {
795                         LFREEPATH(fname);
796                         return (error);
797                 }
798                 tv[0].tv_sec = ltv[0].tv_sec;
799                 tv[0].tv_usec = ltv[0].tv_usec;
800                 tv[1].tv_sec = ltv[1].tv_sec;
801                 tv[1].tv_usec = ltv[1].tv_usec;
802                 tvp = tv;
803         }
804
805         error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
806         LFREEPATH(fname);
807         return (error);
808 }
809
810 int
811 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
812 {
813         l_timeval ltv[2];
814         struct timeval tv[2], *tvp = NULL;
815         char *fname;
816         int error, dfd;
817
818         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
819         LCONVPATHEXIST_AT(td, args->filename, &fname, dfd);
820
821 #ifdef DEBUG
822         if (ldebug(futimesat))
823                 printf(ARGS(futimesat, "%s, *"), fname);
824 #endif
825
826         if (args->utimes != NULL) {
827                 if ((error = copyin(args->utimes, ltv, sizeof ltv))) {
828                         LFREEPATH(fname);
829                         return (error);
830                 }
831                 tv[0].tv_sec = ltv[0].tv_sec;
832                 tv[0].tv_usec = ltv[0].tv_usec;
833                 tv[1].tv_sec = ltv[1].tv_sec;
834                 tv[1].tv_usec = ltv[1].tv_usec;
835                 tvp = tv;
836         }
837
838         error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE);
839         LFREEPATH(fname);
840         return (error);
841 }
842
843 int
844 linux_common_wait(struct thread *td, int pid, int *status,
845     int options, struct rusage *ru)
846 {
847         int error, tmpstat;
848
849         error = kern_wait(td, pid, &tmpstat, options, ru);
850         if (error)
851                 return (error);
852
853         if (status) {
854                 tmpstat &= 0xffff;
855                 if (WIFSIGNALED(tmpstat))
856                         tmpstat = (tmpstat & 0xffffff80) |
857                             BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat));
858                 else if (WIFSTOPPED(tmpstat))
859                         tmpstat = (tmpstat & 0xffff00ff) |
860                             (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8);
861                 error = copyout(&tmpstat, status, sizeof(int));
862         }
863
864         return (error);
865 }
866
867 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
868 int
869 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
870 {
871         int options;
872  
873 #ifdef DEBUG
874         if (ldebug(waitpid))
875                 printf(ARGS(waitpid, "%d, %p, %d"),
876                     args->pid, (void *)args->status, args->options);
877 #endif
878         /*
879          * this is necessary because the test in kern_wait doesn't work
880          * because we mess with the options here
881          */
882         if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE))
883                 return (EINVAL);
884    
885         options = (args->options & (WNOHANG | WUNTRACED));
886         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
887         if (args->options & __WCLONE)
888                 options |= WLINUXCLONE;
889
890         return (linux_common_wait(td, args->pid, args->status, options, NULL));
891 }
892 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
893
894 int
895 linux_wait4(struct thread *td, struct linux_wait4_args *args)
896 {
897         int error, options;
898         struct rusage ru, *rup;
899
900 #ifdef DEBUG
901         if (ldebug(wait4))
902                 printf(ARGS(wait4, "%d, %p, %d, %p"),
903                     args->pid, (void *)args->status, args->options,
904                     (void *)args->rusage);
905 #endif
906
907         options = (args->options & (WNOHANG | WUNTRACED));
908         /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
909         if (args->options & __WCLONE)
910                 options |= WLINUXCLONE;
911
912         if (args->rusage != NULL)
913                 rup = &ru;
914         else
915                 rup = NULL;
916         error = linux_common_wait(td, args->pid, args->status, options, rup);
917         if (error != 0)
918                 return (error);
919         if (args->rusage != NULL)
920                 error = linux_copyout_rusage(&ru, args->rusage);
921         return (error);
922 }
923
924 int
925 linux_waitid(struct thread *td, struct linux_waitid_args *args)
926 {
927         int status, options, sig;
928         struct __wrusage wru;
929         siginfo_t siginfo;
930         l_siginfo_t lsi;
931         idtype_t idtype;
932         struct proc *p;
933         int error;
934
935         options = 0;
936         linux_to_bsd_waitopts(args->options, &options);
937
938         if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED))
939                 return (EINVAL);
940         if (!(options & (WEXITED | WUNTRACED | WCONTINUED)))
941                 return (EINVAL);
942
943         switch (args->idtype) {
944         case LINUX_P_ALL:
945                 idtype = P_ALL;
946                 break;
947         case LINUX_P_PID:
948                 if (args->id <= 0)
949                         return (EINVAL);
950                 idtype = P_PID;
951                 break;
952         case LINUX_P_PGID:
953                 if (args->id <= 0)
954                         return (EINVAL);
955                 idtype = P_PGID;
956                 break;
957         default:
958                 return (EINVAL);
959         }
960
961         error = kern_wait6(td, idtype, args->id, &status, options,
962             &wru, &siginfo);
963         if (error != 0)
964                 return (error);
965         if (args->rusage != NULL) {
966                 error = linux_copyout_rusage(&wru.wru_children,
967                     args->rusage);
968                 if (error != 0)
969                         return (error);
970         }
971         if (args->info != NULL) {
972                 p = td->td_proc;
973                 if (td->td_retval[0] == 0)
974                         bzero(&lsi, sizeof(lsi));
975                 else {
976                         sig = BSD_TO_LINUX_SIGNAL(siginfo.si_signo);
977                         siginfo_to_lsiginfo(&siginfo, &lsi, sig);
978                 }
979                 error = copyout(&lsi, args->info, sizeof(lsi));
980         }
981         td->td_retval[0] = 0;
982
983         return (error);
984 }
985
986 int
987 linux_mknod(struct thread *td, struct linux_mknod_args *args)
988 {
989         char *path;
990         int error;
991
992         LCONVPATHCREAT(td, args->path, &path);
993
994 #ifdef DEBUG
995         if (ldebug(mknod))
996                 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev);
997 #endif
998
999         switch (args->mode & S_IFMT) {
1000         case S_IFIFO:
1001         case S_IFSOCK:
1002                 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode);
1003                 break;
1004
1005         case S_IFCHR:
1006         case S_IFBLK:
1007                 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode,
1008                     args->dev);
1009                 break;
1010
1011         case S_IFDIR:
1012                 error = EPERM;
1013                 break;
1014
1015         case 0:
1016                 args->mode |= S_IFREG;
1017                 /* FALLTHROUGH */
1018         case S_IFREG:
1019                 error = kern_open(td, path, UIO_SYSSPACE,
1020                     O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1021                 if (error == 0)
1022                         kern_close(td, td->td_retval[0]);
1023                 break;
1024
1025         default:
1026                 error = EINVAL;
1027                 break;
1028         }
1029         LFREEPATH(path);
1030         return (error);
1031 }
1032
1033 int
1034 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
1035 {
1036         char *path;
1037         int error, dfd;
1038
1039         dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1040         LCONVPATHCREAT_AT(td, args->filename, &path, dfd);
1041
1042 #ifdef DEBUG
1043         if (ldebug(mknodat))
1044                 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev);
1045 #endif
1046
1047         switch (args->mode & S_IFMT) {
1048         case S_IFIFO:
1049         case S_IFSOCK:
1050                 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode);
1051                 break;
1052
1053         case S_IFCHR:
1054         case S_IFBLK:
1055                 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode,
1056                     args->dev);
1057                 break;
1058
1059         case S_IFDIR:
1060                 error = EPERM;
1061                 break;
1062
1063         case 0:
1064                 args->mode |= S_IFREG;
1065                 /* FALLTHROUGH */
1066         case S_IFREG:
1067                 error = kern_openat(td, dfd, path, UIO_SYSSPACE,
1068                     O_WRONLY | O_CREAT | O_TRUNC, args->mode);
1069                 if (error == 0)
1070                         kern_close(td, td->td_retval[0]);
1071                 break;
1072
1073         default:
1074                 error = EINVAL;
1075                 break;
1076         }
1077         LFREEPATH(path);
1078         return (error);
1079 }
1080
1081 /*
1082  * UGH! This is just about the dumbest idea I've ever heard!!
1083  */
1084 int
1085 linux_personality(struct thread *td, struct linux_personality_args *args)
1086 {
1087 #ifdef DEBUG
1088         if (ldebug(personality))
1089                 printf(ARGS(personality, "%lu"), (unsigned long)args->per);
1090 #endif
1091         if (args->per != 0)
1092                 return (EINVAL);
1093
1094         /* Yes Jim, it's still a Linux... */
1095         td->td_retval[0] = 0;
1096         return (0);
1097 }
1098
1099 struct l_itimerval {
1100         l_timeval it_interval;
1101         l_timeval it_value;
1102 };
1103
1104 #define B2L_ITIMERVAL(bip, lip)                                         \
1105         (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec;          \
1106         (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec;        \
1107         (bip)->it_value.tv_sec = (lip)->it_value.tv_sec;                \
1108         (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1109
1110 int
1111 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1112 {
1113         int error;
1114         struct l_itimerval ls;
1115         struct itimerval aitv, oitv;
1116
1117 #ifdef DEBUG
1118         if (ldebug(setitimer))
1119                 printf(ARGS(setitimer, "%p, %p"),
1120                     (void *)uap->itv, (void *)uap->oitv);
1121 #endif
1122
1123         if (uap->itv == NULL) {
1124                 uap->itv = uap->oitv;
1125                 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1126         }
1127
1128         error = copyin(uap->itv, &ls, sizeof(ls));
1129         if (error != 0)
1130                 return (error);
1131         B2L_ITIMERVAL(&aitv, &ls);
1132 #ifdef DEBUG
1133         if (ldebug(setitimer)) {
1134                 printf("setitimer: value: sec: %jd, usec: %ld\n",
1135                     (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec);
1136                 printf("setitimer: interval: sec: %jd, usec: %ld\n",
1137                     (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec);
1138         }
1139 #endif
1140         error = kern_setitimer(td, uap->which, &aitv, &oitv);
1141         if (error != 0 || uap->oitv == NULL)
1142                 return (error);
1143         B2L_ITIMERVAL(&ls, &oitv);
1144
1145         return (copyout(&ls, uap->oitv, sizeof(ls)));
1146 }
1147
1148 int
1149 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1150 {
1151         int error;
1152         struct l_itimerval ls;
1153         struct itimerval aitv;
1154
1155 #ifdef DEBUG
1156         if (ldebug(getitimer))
1157                 printf(ARGS(getitimer, "%p"), (void *)uap->itv);
1158 #endif
1159         error = kern_getitimer(td, uap->which, &aitv);
1160         if (error != 0)
1161                 return (error);
1162         B2L_ITIMERVAL(&ls, &aitv);
1163         return (copyout(&ls, uap->itv, sizeof(ls)));
1164 }
1165
1166 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1167 int
1168 linux_nice(struct thread *td, struct linux_nice_args *args)
1169 {
1170         struct setpriority_args bsd_args;
1171
1172         bsd_args.which = PRIO_PROCESS;
1173         bsd_args.who = 0;               /* current process */
1174         bsd_args.prio = args->inc;
1175         return (sys_setpriority(td, &bsd_args));
1176 }
1177 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1178
1179 int
1180 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1181 {
1182         struct ucred *newcred, *oldcred;
1183         l_gid_t *linux_gidset;
1184         gid_t *bsd_gidset;
1185         int ngrp, error;
1186         struct proc *p;
1187
1188         ngrp = args->gidsetsize;
1189         if (ngrp < 0 || ngrp >= ngroups_max + 1)
1190                 return (EINVAL);
1191         linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
1192         error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1193         if (error)
1194                 goto out;
1195         newcred = crget();
1196         p = td->td_proc;
1197         PROC_LOCK(p);
1198         oldcred = crcopysafe(p, newcred);
1199
1200         /*
1201          * cr_groups[0] holds egid. Setting the whole set from
1202          * the supplied set will cause egid to be changed too.
1203          * Keep cr_groups[0] unchanged to prevent that.
1204          */
1205
1206         if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
1207                 PROC_UNLOCK(p);
1208                 crfree(newcred);
1209                 goto out;
1210         }
1211
1212         if (ngrp > 0) {
1213                 newcred->cr_ngroups = ngrp + 1;
1214
1215                 bsd_gidset = newcred->cr_groups;
1216                 ngrp--;
1217                 while (ngrp >= 0) {
1218                         bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1219                         ngrp--;
1220                 }
1221         } else
1222                 newcred->cr_ngroups = 1;
1223
1224         setsugid(p);
1225         p->p_ucred = newcred;
1226         PROC_UNLOCK(p);
1227         crfree(oldcred);
1228         error = 0;
1229 out:
1230         free(linux_gidset, M_TEMP);
1231         return (error);
1232 }
1233
1234 int
1235 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1236 {
1237         struct ucred *cred;
1238         l_gid_t *linux_gidset;
1239         gid_t *bsd_gidset;
1240         int bsd_gidsetsz, ngrp, error;
1241
1242         cred = td->td_ucred;
1243         bsd_gidset = cred->cr_groups;
1244         bsd_gidsetsz = cred->cr_ngroups - 1;
1245
1246         /*
1247          * cr_groups[0] holds egid. Returning the whole set
1248          * here will cause a duplicate. Exclude cr_groups[0]
1249          * to prevent that.
1250          */
1251
1252         if ((ngrp = args->gidsetsize) == 0) {
1253                 td->td_retval[0] = bsd_gidsetsz;
1254                 return (0);
1255         }
1256
1257         if (ngrp < bsd_gidsetsz)
1258                 return (EINVAL);
1259
1260         ngrp = 0;
1261         linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1262             M_TEMP, M_WAITOK);
1263         while (ngrp < bsd_gidsetsz) {
1264                 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1265                 ngrp++;
1266         }
1267
1268         error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1269         free(linux_gidset, M_TEMP);
1270         if (error)
1271                 return (error);
1272
1273         td->td_retval[0] = ngrp;
1274         return (0);
1275 }
1276
1277 int
1278 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1279 {
1280         struct rlimit bsd_rlim;
1281         struct l_rlimit rlim;
1282         u_int which;
1283         int error;
1284
1285 #ifdef DEBUG
1286         if (ldebug(setrlimit))
1287                 printf(ARGS(setrlimit, "%d, %p"),
1288                     args->resource, (void *)args->rlim);
1289 #endif
1290
1291         if (args->resource >= LINUX_RLIM_NLIMITS)
1292                 return (EINVAL);
1293
1294         which = linux_to_bsd_resource[args->resource];
1295         if (which == -1)
1296                 return (EINVAL);
1297
1298         error = copyin(args->rlim, &rlim, sizeof(rlim));
1299         if (error)
1300                 return (error);
1301
1302         bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1303         bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1304         return (kern_setrlimit(td, which, &bsd_rlim));
1305 }
1306
1307 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1308 int
1309 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1310 {
1311         struct l_rlimit rlim;
1312         struct proc *p = td->td_proc;
1313         struct rlimit bsd_rlim;
1314         u_int which;
1315
1316 #ifdef DEBUG
1317         if (ldebug(old_getrlimit))
1318                 printf(ARGS(old_getrlimit, "%d, %p"),
1319                     args->resource, (void *)args->rlim);
1320 #endif
1321
1322         if (args->resource >= LINUX_RLIM_NLIMITS)
1323                 return (EINVAL);
1324
1325         which = linux_to_bsd_resource[args->resource];
1326         if (which == -1)
1327                 return (EINVAL);
1328
1329         PROC_LOCK(p);
1330         lim_rlimit(p, which, &bsd_rlim);
1331         PROC_UNLOCK(p);
1332
1333 #ifdef COMPAT_LINUX32
1334         rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1335         if (rlim.rlim_cur == UINT_MAX)
1336                 rlim.rlim_cur = INT_MAX;
1337         rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1338         if (rlim.rlim_max == UINT_MAX)
1339                 rlim.rlim_max = INT_MAX;
1340 #else
1341         rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1342         if (rlim.rlim_cur == ULONG_MAX)
1343                 rlim.rlim_cur = LONG_MAX;
1344         rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1345         if (rlim.rlim_max == ULONG_MAX)
1346                 rlim.rlim_max = LONG_MAX;
1347 #endif
1348         return (copyout(&rlim, args->rlim, sizeof(rlim)));
1349 }
1350 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1351
1352 int
1353 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1354 {
1355         struct l_rlimit rlim;
1356         struct proc *p = td->td_proc;
1357         struct rlimit bsd_rlim;
1358         u_int which;
1359
1360 #ifdef DEBUG
1361         if (ldebug(getrlimit))
1362                 printf(ARGS(getrlimit, "%d, %p"),
1363                     args->resource, (void *)args->rlim);
1364 #endif
1365
1366         if (args->resource >= LINUX_RLIM_NLIMITS)
1367                 return (EINVAL);
1368
1369         which = linux_to_bsd_resource[args->resource];
1370         if (which == -1)
1371                 return (EINVAL);
1372
1373         PROC_LOCK(p);
1374         lim_rlimit(p, which, &bsd_rlim);
1375         PROC_UNLOCK(p);
1376
1377         rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1378         rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1379         return (copyout(&rlim, args->rlim, sizeof(rlim)));
1380 }
1381
1382 int
1383 linux_sched_setscheduler(struct thread *td,
1384     struct linux_sched_setscheduler_args *args)
1385 {
1386         struct sched_param sched_param;
1387         struct thread *tdt;
1388         int error, policy;
1389
1390 #ifdef DEBUG
1391         if (ldebug(sched_setscheduler))
1392                 printf(ARGS(sched_setscheduler, "%d, %d, %p"),
1393                     args->pid, args->policy, (const void *)args->param);
1394 #endif
1395
1396         switch (args->policy) {
1397         case LINUX_SCHED_OTHER:
1398                 policy = SCHED_OTHER;
1399                 break;
1400         case LINUX_SCHED_FIFO:
1401                 policy = SCHED_FIFO;
1402                 break;
1403         case LINUX_SCHED_RR:
1404                 policy = SCHED_RR;
1405                 break;
1406         default:
1407                 return (EINVAL);
1408         }
1409
1410         error = copyin(args->param, &sched_param, sizeof(sched_param));
1411         if (error)
1412                 return (error);
1413
1414         tdt = linux_tdfind(td, args->pid, -1);
1415         if (tdt == NULL)
1416                 return (ESRCH);
1417
1418         error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1419         PROC_UNLOCK(tdt->td_proc);
1420         return (error);
1421 }
1422
1423 int
1424 linux_sched_getscheduler(struct thread *td,
1425     struct linux_sched_getscheduler_args *args)
1426 {
1427         struct thread *tdt;
1428         int error, policy;
1429
1430 #ifdef DEBUG
1431         if (ldebug(sched_getscheduler))
1432                 printf(ARGS(sched_getscheduler, "%d"), args->pid);
1433 #endif
1434
1435         tdt = linux_tdfind(td, args->pid, -1);
1436         if (tdt == NULL)
1437                 return (ESRCH);
1438
1439         error = kern_sched_getscheduler(td, tdt, &policy);
1440         PROC_UNLOCK(tdt->td_proc);
1441
1442         switch (policy) {
1443         case SCHED_OTHER:
1444                 td->td_retval[0] = LINUX_SCHED_OTHER;
1445                 break;
1446         case SCHED_FIFO:
1447                 td->td_retval[0] = LINUX_SCHED_FIFO;
1448                 break;
1449         case SCHED_RR:
1450                 td->td_retval[0] = LINUX_SCHED_RR;
1451                 break;
1452         }
1453         return (error);
1454 }
1455
1456 int
1457 linux_sched_get_priority_max(struct thread *td,
1458     struct linux_sched_get_priority_max_args *args)
1459 {
1460         struct sched_get_priority_max_args bsd;
1461
1462 #ifdef DEBUG
1463         if (ldebug(sched_get_priority_max))
1464                 printf(ARGS(sched_get_priority_max, "%d"), args->policy);
1465 #endif
1466
1467         switch (args->policy) {
1468         case LINUX_SCHED_OTHER:
1469                 bsd.policy = SCHED_OTHER;
1470                 break;
1471         case LINUX_SCHED_FIFO:
1472                 bsd.policy = SCHED_FIFO;
1473                 break;
1474         case LINUX_SCHED_RR:
1475                 bsd.policy = SCHED_RR;
1476                 break;
1477         default:
1478                 return (EINVAL);
1479         }
1480         return (sys_sched_get_priority_max(td, &bsd));
1481 }
1482
1483 int
1484 linux_sched_get_priority_min(struct thread *td,
1485     struct linux_sched_get_priority_min_args *args)
1486 {
1487         struct sched_get_priority_min_args bsd;
1488
1489 #ifdef DEBUG
1490         if (ldebug(sched_get_priority_min))
1491                 printf(ARGS(sched_get_priority_min, "%d"), args->policy);
1492 #endif
1493
1494         switch (args->policy) {
1495         case LINUX_SCHED_OTHER:
1496                 bsd.policy = SCHED_OTHER;
1497                 break;
1498         case LINUX_SCHED_FIFO:
1499                 bsd.policy = SCHED_FIFO;
1500                 break;
1501         case LINUX_SCHED_RR:
1502                 bsd.policy = SCHED_RR;
1503                 break;
1504         default:
1505                 return (EINVAL);
1506         }
1507         return (sys_sched_get_priority_min(td, &bsd));
1508 }
1509
1510 #define REBOOT_CAD_ON   0x89abcdef
1511 #define REBOOT_CAD_OFF  0
1512 #define REBOOT_HALT     0xcdef0123
1513 #define REBOOT_RESTART  0x01234567
1514 #define REBOOT_RESTART2 0xA1B2C3D4
1515 #define REBOOT_POWEROFF 0x4321FEDC
1516 #define REBOOT_MAGIC1   0xfee1dead
1517 #define REBOOT_MAGIC2   0x28121969
1518 #define REBOOT_MAGIC2A  0x05121996
1519 #define REBOOT_MAGIC2B  0x16041998
1520
1521 int
1522 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1523 {
1524         struct reboot_args bsd_args;
1525
1526 #ifdef DEBUG
1527         if (ldebug(reboot))
1528                 printf(ARGS(reboot, "0x%x"), args->cmd);
1529 #endif
1530
1531         if (args->magic1 != REBOOT_MAGIC1)
1532                 return (EINVAL);
1533
1534         switch (args->magic2) {
1535         case REBOOT_MAGIC2:
1536         case REBOOT_MAGIC2A:
1537         case REBOOT_MAGIC2B:
1538                 break;
1539         default:
1540                 return (EINVAL);
1541         }
1542
1543         switch (args->cmd) {
1544         case REBOOT_CAD_ON:
1545         case REBOOT_CAD_OFF:
1546                 return (priv_check(td, PRIV_REBOOT));
1547         case REBOOT_HALT:
1548                 bsd_args.opt = RB_HALT;
1549                 break;
1550         case REBOOT_RESTART:
1551         case REBOOT_RESTART2:
1552                 bsd_args.opt = 0;
1553                 break;
1554         case REBOOT_POWEROFF:
1555                 bsd_args.opt = RB_POWEROFF;
1556                 break;
1557         default:
1558                 return (EINVAL);
1559         }
1560         return (sys_reboot(td, &bsd_args));
1561 }
1562
1563
1564 /*
1565  * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify
1566  * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that
1567  * are assumed to be preserved. The following lightweight syscalls fixes
1568  * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c
1569  *
1570  * linux_getpid() - MP SAFE
1571  * linux_getgid() - MP SAFE
1572  * linux_getuid() - MP SAFE
1573  */
1574
1575 int
1576 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1577 {
1578
1579 #ifdef DEBUG
1580         if (ldebug(getpid))
1581                 printf(ARGS(getpid, ""));
1582 #endif
1583         td->td_retval[0] = td->td_proc->p_pid;
1584
1585         return (0);
1586 }
1587
1588 int
1589 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1590 {
1591         struct linux_emuldata *em;
1592
1593 #ifdef DEBUG
1594         if (ldebug(gettid))
1595                 printf(ARGS(gettid, ""));
1596 #endif
1597
1598         em = em_find(td);
1599         KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1600
1601         td->td_retval[0] = em->em_tid;
1602
1603         return (0);
1604 }
1605
1606
1607 int
1608 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1609 {
1610
1611 #ifdef DEBUG
1612         if (ldebug(getppid))
1613                 printf(ARGS(getppid, ""));
1614 #endif
1615
1616         PROC_LOCK(td->td_proc);
1617         td->td_retval[0] = td->td_proc->p_pptr->p_pid;
1618         PROC_UNLOCK(td->td_proc);
1619         return (0);
1620 }
1621
1622 int
1623 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1624 {
1625
1626 #ifdef DEBUG
1627         if (ldebug(getgid))
1628                 printf(ARGS(getgid, ""));
1629 #endif
1630
1631         td->td_retval[0] = td->td_ucred->cr_rgid;
1632         return (0);
1633 }
1634
1635 int
1636 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1637 {
1638
1639 #ifdef DEBUG
1640         if (ldebug(getuid))
1641                 printf(ARGS(getuid, ""));
1642 #endif
1643
1644         td->td_retval[0] = td->td_ucred->cr_ruid;
1645         return (0);
1646 }
1647
1648
1649 int
1650 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1651 {
1652         struct getsid_args bsd;
1653
1654 #ifdef DEBUG
1655         if (ldebug(getsid))
1656                 printf(ARGS(getsid, "%i"), args->pid);
1657 #endif
1658
1659         bsd.pid = args->pid;
1660         return (sys_getsid(td, &bsd));
1661 }
1662
1663 int
1664 linux_nosys(struct thread *td, struct nosys_args *ignore)
1665 {
1666
1667         return (ENOSYS);
1668 }
1669
1670 int
1671 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1672 {
1673         struct getpriority_args bsd_args;
1674         int error;
1675
1676 #ifdef DEBUG
1677         if (ldebug(getpriority))
1678                 printf(ARGS(getpriority, "%i, %i"), args->which, args->who);
1679 #endif
1680
1681         bsd_args.which = args->which;
1682         bsd_args.who = args->who;
1683         error = sys_getpriority(td, &bsd_args);
1684         td->td_retval[0] = 20 - td->td_retval[0];
1685         return (error);
1686 }
1687
1688 int
1689 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1690 {
1691         int name[2];
1692
1693 #ifdef DEBUG
1694         if (ldebug(sethostname))
1695                 printf(ARGS(sethostname, "*, %i"), args->len);
1696 #endif
1697
1698         name[0] = CTL_KERN;
1699         name[1] = KERN_HOSTNAME;
1700         return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1701             args->len, 0, 0));
1702 }
1703
1704 int
1705 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1706 {
1707         int name[2];
1708
1709 #ifdef DEBUG
1710         if (ldebug(setdomainname))
1711                 printf(ARGS(setdomainname, "*, %i"), args->len);
1712 #endif
1713
1714         name[0] = CTL_KERN;
1715         name[1] = KERN_NISDOMAINNAME;
1716         return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1717             args->len, 0, 0));
1718 }
1719
1720 int
1721 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1722 {
1723
1724 #ifdef DEBUG
1725         if (ldebug(exit_group))
1726                 printf(ARGS(exit_group, "%i"), args->error_code);
1727 #endif
1728
1729         LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1730             args->error_code);
1731
1732         /*
1733          * XXX: we should send a signal to the parent if
1734          * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1735          * as it doesnt occur often.
1736          */
1737         exit1(td, W_EXITCODE(args->error_code, 0));
1738                 /* NOTREACHED */
1739 }
1740
1741 #define _LINUX_CAPABILITY_VERSION  0x19980330
1742
1743 struct l_user_cap_header {
1744         l_int   version;
1745         l_int   pid;
1746 };
1747
1748 struct l_user_cap_data {
1749         l_int   effective;
1750         l_int   permitted;
1751         l_int   inheritable;
1752 };
1753
1754 int
1755 linux_capget(struct thread *td, struct linux_capget_args *args)
1756 {
1757         struct l_user_cap_header luch;
1758         struct l_user_cap_data lucd;
1759         int error;
1760
1761         if (args->hdrp == NULL)
1762                 return (EFAULT);
1763
1764         error = copyin(args->hdrp, &luch, sizeof(luch));
1765         if (error != 0)
1766                 return (error);
1767
1768         if (luch.version != _LINUX_CAPABILITY_VERSION) {
1769                 luch.version = _LINUX_CAPABILITY_VERSION;
1770                 error = copyout(&luch, args->hdrp, sizeof(luch));
1771                 if (error)
1772                         return (error);
1773                 return (EINVAL);
1774         }
1775
1776         if (luch.pid)
1777                 return (EPERM);
1778
1779         if (args->datap) {
1780                 /*
1781                  * The current implementation doesn't support setting
1782                  * a capability (it's essentially a stub) so indicate
1783                  * that no capabilities are currently set or available
1784                  * to request.
1785                  */
1786                 bzero (&lucd, sizeof(lucd));
1787                 error = copyout(&lucd, args->datap, sizeof(lucd));
1788         }
1789
1790         return (error);
1791 }
1792
1793 int
1794 linux_capset(struct thread *td, struct linux_capset_args *args)
1795 {
1796         struct l_user_cap_header luch;
1797         struct l_user_cap_data lucd;
1798         int error;
1799
1800         if (args->hdrp == NULL || args->datap == NULL)
1801                 return (EFAULT);
1802
1803         error = copyin(args->hdrp, &luch, sizeof(luch));
1804         if (error != 0)
1805                 return (error);
1806
1807         if (luch.version != _LINUX_CAPABILITY_VERSION) {
1808                 luch.version = _LINUX_CAPABILITY_VERSION;
1809                 error = copyout(&luch, args->hdrp, sizeof(luch));
1810                 if (error)
1811                         return (error);
1812                 return (EINVAL);
1813         }
1814
1815         if (luch.pid)
1816                 return (EPERM);
1817
1818         error = copyin(args->datap, &lucd, sizeof(lucd));
1819         if (error != 0)
1820                 return (error);
1821
1822         /* We currently don't support setting any capabilities. */
1823         if (lucd.effective || lucd.permitted || lucd.inheritable) {
1824                 linux_msg(td,
1825                           "capset effective=0x%x, permitted=0x%x, "
1826                           "inheritable=0x%x is not implemented",
1827                           (int)lucd.effective, (int)lucd.permitted,
1828                           (int)lucd.inheritable);
1829                 return (EPERM);
1830         }
1831
1832         return (0);
1833 }
1834
1835 int
1836 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1837 {
1838         int error = 0, max_size;
1839         struct proc *p = td->td_proc;
1840         char comm[LINUX_MAX_COMM_LEN];
1841         struct linux_emuldata *em;
1842         int pdeath_signal;
1843
1844 #ifdef DEBUG
1845         if (ldebug(prctl))
1846                 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option,
1847                     args->arg2, args->arg3, args->arg4, args->arg5);
1848 #endif
1849
1850         switch (args->option) {
1851         case LINUX_PR_SET_PDEATHSIG:
1852                 if (!LINUX_SIG_VALID(args->arg2))
1853                         return (EINVAL);
1854                 em = em_find(td);
1855                 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1856                 em->pdeath_signal = args->arg2;
1857                 break;
1858         case LINUX_PR_GET_PDEATHSIG:
1859                 em = em_find(td);
1860                 KASSERT(em != NULL, ("prctl: emuldata not found.\n"));
1861                 pdeath_signal = em->pdeath_signal;
1862                 error = copyout(&pdeath_signal,
1863                     (void *)(register_t)args->arg2,
1864                     sizeof(pdeath_signal));
1865                 break;
1866         case LINUX_PR_GET_KEEPCAPS:
1867                 /*
1868                  * Indicate that we always clear the effective and
1869                  * permitted capability sets when the user id becomes
1870                  * non-zero (actually the capability sets are simply
1871                  * always zero in the current implementation).
1872                  */
1873                 td->td_retval[0] = 0;
1874                 break;
1875         case LINUX_PR_SET_KEEPCAPS:
1876                 /*
1877                  * Ignore requests to keep the effective and permitted
1878                  * capability sets when the user id becomes non-zero.
1879                  */
1880                 break;
1881         case LINUX_PR_SET_NAME:
1882                 /*
1883                  * To be on the safe side we need to make sure to not
1884                  * overflow the size a linux program expects. We already
1885                  * do this here in the copyin, so that we don't need to
1886                  * check on copyout.
1887                  */
1888                 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1889                 error = copyinstr((void *)(register_t)args->arg2, comm,
1890                     max_size, NULL);
1891
1892                 /* Linux silently truncates the name if it is too long. */
1893                 if (error == ENAMETOOLONG) {
1894                         /*
1895                          * XXX: copyinstr() isn't documented to populate the
1896                          * array completely, so do a copyin() to be on the
1897                          * safe side. This should be changed in case
1898                          * copyinstr() is changed to guarantee this.
1899                          */
1900                         error = copyin((void *)(register_t)args->arg2, comm,
1901                             max_size - 1);
1902                         comm[max_size - 1] = '\0';
1903                 }
1904                 if (error)
1905                         return (error);
1906
1907                 PROC_LOCK(p);
1908                 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1909                 PROC_UNLOCK(p);
1910                 break;
1911         case LINUX_PR_GET_NAME:
1912                 PROC_LOCK(p);
1913                 strlcpy(comm, p->p_comm, sizeof(comm));
1914                 PROC_UNLOCK(p);
1915                 error = copyout(comm, (void *)(register_t)args->arg2,
1916                     strlen(comm) + 1);
1917                 break;
1918         default:
1919                 error = EINVAL;
1920                 break;
1921         }
1922
1923         return (error);
1924 }
1925
1926 int
1927 linux_sched_setparam(struct thread *td,
1928     struct linux_sched_setparam_args *uap)
1929 {
1930         struct sched_param sched_param;
1931         struct thread *tdt;
1932         int error;
1933
1934 #ifdef DEBUG
1935         if (ldebug(sched_setparam))
1936                 printf(ARGS(sched_setparam, "%d, *"), uap->pid);
1937 #endif
1938
1939         error = copyin(uap->param, &sched_param, sizeof(sched_param));
1940         if (error)
1941                 return (error);
1942
1943         tdt = linux_tdfind(td, uap->pid, -1);
1944         if (tdt == NULL)
1945                 return (ESRCH);
1946
1947         error = kern_sched_setparam(td, tdt, &sched_param);
1948         PROC_UNLOCK(tdt->td_proc);
1949         return (error);
1950 }
1951
1952 int
1953 linux_sched_getparam(struct thread *td,
1954     struct linux_sched_getparam_args *uap)
1955 {
1956         struct sched_param sched_param;
1957         struct thread *tdt;
1958         int error;
1959
1960 #ifdef DEBUG
1961         if (ldebug(sched_getparam))
1962                 printf(ARGS(sched_getparam, "%d, *"), uap->pid);
1963 #endif
1964
1965         tdt = linux_tdfind(td, uap->pid, -1);
1966         if (tdt == NULL)
1967                 return (ESRCH);
1968
1969         error = kern_sched_getparam(td, tdt, &sched_param);
1970         PROC_UNLOCK(tdt->td_proc);
1971         if (error == 0)
1972                 error = copyout(&sched_param, uap->param,
1973                     sizeof(sched_param));
1974         return (error);
1975 }
1976
1977 /*
1978  * Get affinity of a process.
1979  */
1980 int
1981 linux_sched_getaffinity(struct thread *td,
1982     struct linux_sched_getaffinity_args *args)
1983 {
1984         int error;
1985         struct thread *tdt;
1986         struct cpuset_getaffinity_args cga;
1987
1988 #ifdef DEBUG
1989         if (ldebug(sched_getaffinity))
1990                 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid,
1991                     args->len);
1992 #endif
1993         if (args->len < sizeof(cpuset_t))
1994                 return (EINVAL);
1995
1996         tdt = linux_tdfind(td, args->pid, -1);
1997         if (tdt == NULL)
1998                 return (ESRCH);
1999
2000         PROC_UNLOCK(tdt->td_proc);
2001         cga.level = CPU_LEVEL_WHICH;
2002         cga.which = CPU_WHICH_TID;
2003         cga.id = tdt->td_tid;
2004         cga.cpusetsize = sizeof(cpuset_t);
2005         cga.mask = (cpuset_t *) args->user_mask_ptr;
2006
2007         if ((error = sys_cpuset_getaffinity(td, &cga)) == 0)
2008                 td->td_retval[0] = sizeof(cpuset_t);
2009
2010         return (error);
2011 }
2012
2013 /*
2014  *  Set affinity of a process.
2015  */
2016 int
2017 linux_sched_setaffinity(struct thread *td,
2018     struct linux_sched_setaffinity_args *args)
2019 {
2020         struct cpuset_setaffinity_args csa;
2021         struct thread *tdt;
2022
2023 #ifdef DEBUG
2024         if (ldebug(sched_setaffinity))
2025                 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid,
2026                     args->len);
2027 #endif
2028         if (args->len < sizeof(cpuset_t))
2029                 return (EINVAL);
2030
2031         tdt = linux_tdfind(td, args->pid, -1);
2032         if (tdt == NULL)
2033                 return (ESRCH);
2034
2035         PROC_UNLOCK(tdt->td_proc);
2036         csa.level = CPU_LEVEL_WHICH;
2037         csa.which = CPU_WHICH_TID;
2038         csa.id = tdt->td_tid;
2039         csa.cpusetsize = sizeof(cpuset_t);
2040         csa.mask = (cpuset_t *) args->user_mask_ptr;
2041
2042         return (sys_cpuset_setaffinity(td, &csa));
2043 }
2044
2045 struct linux_rlimit64 {
2046         uint64_t        rlim_cur;
2047         uint64_t        rlim_max;
2048 };
2049
2050 int
2051 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2052 {
2053         struct rlimit rlim, nrlim;
2054         struct linux_rlimit64 lrlim;
2055         struct proc *p;
2056         u_int which;
2057         int flags;
2058         int error;
2059
2060 #ifdef DEBUG
2061         if (ldebug(prlimit64))
2062                 printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid,
2063                     args->resource, (void *)args->new, (void *)args->old);
2064 #endif
2065
2066         if (args->resource >= LINUX_RLIM_NLIMITS)
2067                 return (EINVAL);
2068
2069         which = linux_to_bsd_resource[args->resource];
2070         if (which == -1)
2071                 return (EINVAL);
2072
2073         if (args->new != NULL) {
2074                 /*
2075                  * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2076                  * rlim is unsigned 64-bit. FreeBSD treats negative limits
2077                  * as INFINITY so we do not need a conversion even.
2078                  */
2079                 error = copyin(args->new, &nrlim, sizeof(nrlim));
2080                 if (error != 0)
2081                         return (error);
2082         }
2083
2084         flags = PGET_HOLD | PGET_NOTWEXIT;
2085         if (args->new != NULL)
2086                 flags |= PGET_CANDEBUG;
2087         else
2088                 flags |= PGET_CANSEE;
2089         error = pget(args->pid, flags, &p);
2090         if (error != 0)
2091                 return (error);
2092
2093         if (args->old != NULL) {
2094                 PROC_LOCK(p);
2095                 lim_rlimit(p, which, &rlim);
2096                 PROC_UNLOCK(p);
2097                 if (rlim.rlim_cur == RLIM_INFINITY)
2098                         lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2099                 else
2100                         lrlim.rlim_cur = rlim.rlim_cur;
2101                 if (rlim.rlim_max == RLIM_INFINITY)
2102                         lrlim.rlim_max = LINUX_RLIM_INFINITY;
2103                 else
2104                         lrlim.rlim_max = rlim.rlim_max;
2105                 error = copyout(&lrlim, args->old, sizeof(lrlim));
2106                 if (error != 0)
2107                         goto out;
2108         }
2109
2110         if (args->new != NULL)
2111                 error = kern_proc_setrlimit(td, p, which, &nrlim);
2112
2113  out:
2114         PRELE(p);
2115         return (error);
2116 }
2117
2118 int
2119 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2120 {
2121         struct timeval utv, tv0, tv1, *tvp;
2122         struct l_pselect6arg lpse6;
2123         struct l_timespec lts;
2124         struct timespec uts;
2125         l_sigset_t l_ss;
2126         sigset_t *ssp;
2127         sigset_t ss;
2128         int error;
2129
2130         ssp = NULL;
2131         if (args->sig != NULL) {
2132                 error = copyin(args->sig, &lpse6, sizeof(lpse6));
2133                 if (error != 0)
2134                         return (error);
2135                 if (lpse6.ss_len != sizeof(l_ss))
2136                         return (EINVAL);
2137                 if (lpse6.ss != 0) {
2138                         error = copyin(PTRIN(lpse6.ss), &l_ss,
2139                             sizeof(l_ss));
2140                         if (error != 0)
2141                                 return (error);
2142                         linux_to_bsd_sigset(&l_ss, &ss);
2143                         ssp = &ss;
2144                 }
2145         }
2146
2147         /*
2148          * Currently glibc changes nanosecond number to microsecond.
2149          * This mean losing precision but for now it is hardly seen.
2150          */
2151         if (args->tsp != NULL) {
2152                 error = copyin(args->tsp, &lts, sizeof(lts));
2153                 if (error != 0)
2154                         return (error);
2155                 uts.tv_sec = lts.tv_sec;
2156                 uts.tv_nsec = lts.tv_nsec;
2157
2158                 TIMESPEC_TO_TIMEVAL(&utv, &uts);
2159                 if (itimerfix(&utv))
2160                         return (EINVAL);
2161
2162                 microtime(&tv0);
2163                 tvp = &utv;
2164         } else
2165                 tvp = NULL;
2166
2167         error = kern_pselect(td, args->nfds, args->readfds, args->writefds,
2168             args->exceptfds, tvp, ssp, sizeof(l_int) * 8);
2169
2170         if (error == 0 && args->tsp != NULL) {
2171                 if (td->td_retval[0] != 0) {
2172                         /*
2173                          * Compute how much time was left of the timeout,
2174                          * by subtracting the current time and the time
2175                          * before we started the call, and subtracting
2176                          * that result from the user-supplied value.
2177                          */
2178
2179                         microtime(&tv1);
2180                         timevalsub(&tv1, &tv0);
2181                         timevalsub(&utv, &tv1);
2182                         if (utv.tv_sec < 0)
2183                                 timevalclear(&utv);
2184                 } else
2185                         timevalclear(&utv);
2186
2187                 TIMEVAL_TO_TIMESPEC(&utv, &uts);
2188                 lts.tv_sec = uts.tv_sec;
2189                 lts.tv_nsec = uts.tv_nsec;
2190                 error = copyout(&lts, args->tsp, sizeof(lts));
2191         }
2192
2193         return (error);
2194 }
2195
2196 int
2197 linux_sched_rr_get_interval(struct thread *td,
2198     struct linux_sched_rr_get_interval_args *uap)
2199 {
2200         struct timespec ts;
2201         struct l_timespec lts;
2202         struct thread *tdt;
2203         int error;
2204
2205         /*
2206          * According to man in case the invalid pid specified
2207          * EINVAL should be returned.
2208          */
2209         if (uap->pid < 0)
2210                 return (EINVAL);
2211
2212         tdt = linux_tdfind(td, uap->pid, -1);
2213         if (tdt == NULL)
2214                 return (ESRCH);
2215
2216         error = kern_sched_rr_get_interval_td(td, tdt, &ts);
2217         PROC_UNLOCK(tdt->td_proc);
2218         if (error != 0)
2219                 return (error);
2220         lts.tv_sec = ts.tv_sec;
2221         lts.tv_nsec = ts.tv_nsec;
2222         return (copyout(&lts, uap->interval, sizeof(lts)));
2223 }
2224
2225 /*
2226  * In case when the Linux thread is the initial thread in
2227  * the thread group thread id is equal to the process id.
2228  * Glibc depends on this magic (assert in pthread_getattr_np.c).
2229  */
2230 struct thread *
2231 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2232 {
2233         struct linux_emuldata *em;
2234         struct thread *tdt;
2235         struct proc *p;
2236
2237         tdt = NULL;
2238         if (tid == 0 || tid == td->td_tid) {
2239                 tdt = td;
2240                 PROC_LOCK(tdt->td_proc);
2241         } else if (tid > PID_MAX)
2242                 tdt = tdfind(tid, pid);
2243         else {
2244                 /*
2245                  * Initial thread where the tid equal to the pid.
2246                  */
2247                 p = pfind(tid);
2248                 if (p != NULL) {
2249                         if (SV_PROC_ABI(p) != SV_ABI_LINUX) {
2250                                 /*
2251                                  * p is not a Linuxulator process.
2252                                  */
2253                                 PROC_UNLOCK(p);
2254                                 return (NULL);
2255                         }
2256                         FOREACH_THREAD_IN_PROC(p, tdt) {
2257                                 em = em_find(tdt);
2258                                 if (tid == em->em_tid)
2259                                         return (tdt);
2260                         }
2261                         PROC_UNLOCK(p);
2262                 }
2263                 return (NULL);
2264         }
2265
2266         return (tdt);
2267 }
2268
2269 void
2270 linux_to_bsd_waitopts(int options, int *bsdopts)
2271 {
2272
2273         if (options & LINUX_WNOHANG)
2274                 *bsdopts |= WNOHANG;
2275         if (options & LINUX_WUNTRACED)
2276                 *bsdopts |= WUNTRACED;
2277         if (options & LINUX_WEXITED)
2278                 *bsdopts |= WEXITED;
2279         if (options & LINUX_WCONTINUED)
2280                 *bsdopts |= WCONTINUED;
2281         if (options & LINUX_WNOWAIT)
2282                 *bsdopts |= WNOWAIT;
2283
2284         if (options & __WCLONE)
2285                 *bsdopts |= WLINUXCLONE;
2286 }