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