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