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