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