1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2002 Doug Rabson
5 * Copyright (c) 1994-1995 Søren Schmidt
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/fcntl.h>
35 #include <sys/jail.h>
36 #include <sys/imgact.h>
37 #include <sys/limits.h>
38 #include <sys/lock.h>
39 #include <sys/msgbuf.h>
40 #include <sys/mutex.h>
41 #include <sys/poll.h>
42 #include <sys/priv.h>
43 #include <sys/proc.h>
44 #include <sys/procctl.h>
45 #include <sys/reboot.h>
46 #include <sys/random.h>
47 #include <sys/resourcevar.h>
48 #include <sys/sched.h>
49 #include <sys/smp.h>
50 #include <sys/stat.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysctl.h>
53 #include <sys/sysent.h>
54 #include <sys/sysproto.h>
55 #include <sys/time.h>
56 #include <sys/vmmeter.h>
57 #include <sys/vnode.h>
58
59 #include <security/audit/audit.h>
60 #include <security/mac/mac_framework.h>
61
62 #include <vm/pmap.h>
63 #include <vm/vm_map.h>
64 #include <vm/swap_pager.h>
65
66 #ifdef COMPAT_LINUX32
67 #include <machine/../linux32/linux.h>
68 #include <machine/../linux32/linux32_proto.h>
69 #else
70 #include <machine/../linux/linux.h>
71 #include <machine/../linux/linux_proto.h>
72 #endif
73
74 #include <compat/linux/linux_common.h>
75 #include <compat/linux/linux_dtrace.h>
76 #include <compat/linux/linux_file.h>
77 #include <compat/linux/linux_mib.h>
78 #include <compat/linux/linux_signal.h>
79 #include <compat/linux/linux_time.h>
80 #include <compat/linux/linux_util.h>
81 #include <compat/linux/linux_sysproto.h>
82 #include <compat/linux/linux_emul.h>
83 #include <compat/linux/linux_misc.h>
84
85 int stclohz; /* Statistics clock frequency */
86
87 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = {
88 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK,
89 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE,
90 RLIMIT_MEMLOCK, RLIMIT_AS
91 };
92
93 struct l_sysinfo {
94 l_long uptime; /* Seconds since boot */
95 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */
96 #define LINUX_SYSINFO_LOADS_SCALE 65536
97 l_ulong totalram; /* Total usable main memory size */
98 l_ulong freeram; /* Available memory size */
99 l_ulong sharedram; /* Amount of shared memory */
100 l_ulong bufferram; /* Memory used by buffers */
101 l_ulong totalswap; /* Total swap space size */
102 l_ulong freeswap; /* swap space still available */
103 l_ushort procs; /* Number of current processes */
104 l_ushort pads;
105 l_ulong totalhigh;
106 l_ulong freehigh;
107 l_uint mem_unit;
108 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */
109 };
110
111 struct l_pselect6arg {
112 l_uintptr_t ss;
113 l_size_t ss_len;
114 };
115
116 static int linux_utimensat_lts_to_ts(struct l_timespec *,
117 struct timespec *);
118 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
119 static int linux_utimensat_lts64_to_ts(struct l_timespec64 *,
120 struct timespec *);
121 #endif
122 static int linux_common_utimensat(struct thread *, int,
123 const char *, struct timespec *, int);
124 static int linux_common_pselect6(struct thread *, l_int,
125 l_fd_set *, l_fd_set *, l_fd_set *,
126 struct timespec *, l_uintptr_t *);
127 static int linux_common_ppoll(struct thread *, struct pollfd *,
128 uint32_t, struct timespec *, l_sigset_t *,
129 l_size_t);
130 static int linux_pollin(struct thread *, struct pollfd *,
131 struct pollfd *, u_int);
132 static int linux_pollout(struct thread *, struct pollfd *,
133 struct pollfd *, u_int);
134
135 int
linux_sysinfo(struct thread * td,struct linux_sysinfo_args * args)136 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args)
137 {
138 struct l_sysinfo sysinfo;
139 int i, j;
140 struct timespec ts;
141
142 bzero(&sysinfo, sizeof(sysinfo));
143 getnanouptime(&ts);
144 if (ts.tv_nsec != 0)
145 ts.tv_sec++;
146 sysinfo.uptime = ts.tv_sec;
147
148 /* Use the information from the mib to get our load averages */
149 for (i = 0; i < 3; i++)
150 sysinfo.loads[i] = averunnable.ldavg[i] *
151 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale;
152
153 sysinfo.totalram = physmem * PAGE_SIZE;
154 sysinfo.freeram = (u_long)vm_free_count() * PAGE_SIZE;
155
156 /*
157 * sharedram counts pages allocated to named, swap-backed objects such
158 * as shared memory segments and tmpfs files. There is no cheap way to
159 * compute this, so just leave the field unpopulated. Linux itself only
160 * started setting this field in the 3.x timeframe.
161 */
162 sysinfo.sharedram = 0;
163 sysinfo.bufferram = 0;
164
165 swap_pager_status(&i, &j);
166 sysinfo.totalswap = i * PAGE_SIZE;
167 sysinfo.freeswap = (i - j) * PAGE_SIZE;
168
169 sysinfo.procs = nprocs;
170
171 /*
172 * Platforms supported by the emulation layer do not have a notion of
173 * high memory.
174 */
175 sysinfo.totalhigh = 0;
176 sysinfo.freehigh = 0;
177
178 sysinfo.mem_unit = 1;
179
180 return (copyout(&sysinfo, args->info, sizeof(sysinfo)));
181 }
182
183 #ifdef LINUX_LEGACY_SYSCALLS
184 int
linux_alarm(struct thread * td,struct linux_alarm_args * args)185 linux_alarm(struct thread *td, struct linux_alarm_args *args)
186 {
187 struct itimerval it, old_it;
188 u_int secs;
189 int error __diagused;
190
191 secs = args->secs;
192 /*
193 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2
194 * to match kern_setitimer()'s limit to avoid error from it.
195 *
196 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit
197 * platforms.
198 */
199 if (secs > INT32_MAX / 2)
200 secs = INT32_MAX / 2;
201
202 it.it_value.tv_sec = secs;
203 it.it_value.tv_usec = 0;
204 timevalclear(&it.it_interval);
205 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it);
206 KASSERT(error == 0, ("kern_setitimer returns %d", error));
207
208 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) ||
209 old_it.it_value.tv_usec >= 500000)
210 old_it.it_value.tv_sec++;
211 td->td_retval[0] = old_it.it_value.tv_sec;
212 return (0);
213 }
214 #endif
215
216 int
linux_brk(struct thread * td,struct linux_brk_args * args)217 linux_brk(struct thread *td, struct linux_brk_args *args)
218 {
219 struct vmspace *vm = td->td_proc->p_vmspace;
220 uintptr_t new, old;
221
222 old = (uintptr_t)vm->vm_daddr + ctob(vm->vm_dsize);
223 new = (uintptr_t)args->dsend;
224 if ((caddr_t)new > vm->vm_daddr && !kern_break(td, &new))
225 td->td_retval[0] = (register_t)new;
226 else
227 td->td_retval[0] = (register_t)old;
228
229 return (0);
230 }
231
232 #ifdef LINUX_LEGACY_SYSCALLS
233 int
linux_select(struct thread * td,struct linux_select_args * args)234 linux_select(struct thread *td, struct linux_select_args *args)
235 {
236 l_timeval ltv;
237 struct timeval tv0, tv1, utv, *tvp;
238 int error;
239
240 /*
241 * Store current time for computation of the amount of
242 * time left.
243 */
244 if (args->timeout) {
245 if ((error = copyin(args->timeout, <v, sizeof(ltv))))
246 goto select_out;
247 utv.tv_sec = ltv.tv_sec;
248 utv.tv_usec = ltv.tv_usec;
249
250 if (itimerfix(&utv)) {
251 /*
252 * The timeval was invalid. Convert it to something
253 * valid that will act as it does under Linux.
254 */
255 utv.tv_sec += utv.tv_usec / 1000000;
256 utv.tv_usec %= 1000000;
257 if (utv.tv_usec < 0) {
258 utv.tv_sec -= 1;
259 utv.tv_usec += 1000000;
260 }
261 if (utv.tv_sec < 0)
262 timevalclear(&utv);
263 }
264 microtime(&tv0);
265 tvp = &utv;
266 } else
267 tvp = NULL;
268
269 error = kern_select(td, args->nfds, args->readfds, args->writefds,
270 args->exceptfds, tvp, LINUX_NFDBITS);
271 if (error)
272 goto select_out;
273
274 if (args->timeout) {
275 if (td->td_retval[0]) {
276 /*
277 * Compute how much time was left of the timeout,
278 * by subtracting the current time and the time
279 * before we started the call, and subtracting
280 * that result from the user-supplied value.
281 */
282 microtime(&tv1);
283 timevalsub(&tv1, &tv0);
284 timevalsub(&utv, &tv1);
285 if (utv.tv_sec < 0)
286 timevalclear(&utv);
287 } else
288 timevalclear(&utv);
289 ltv.tv_sec = utv.tv_sec;
290 ltv.tv_usec = utv.tv_usec;
291 if ((error = copyout(<v, args->timeout, sizeof(ltv))))
292 goto select_out;
293 }
294
295 select_out:
296 return (error);
297 }
298 #endif
299
300 int
linux_mremap(struct thread * td,struct linux_mremap_args * args)301 linux_mremap(struct thread *td, struct linux_mremap_args *args)
302 {
303 uintptr_t addr;
304 size_t len;
305 int error = 0;
306
307 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) {
308 td->td_retval[0] = 0;
309 return (EINVAL);
310 }
311
312 /*
313 * Check for the page alignment.
314 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK.
315 */
316 if (args->addr & PAGE_MASK) {
317 td->td_retval[0] = 0;
318 return (EINVAL);
319 }
320
321 args->new_len = round_page(args->new_len);
322 args->old_len = round_page(args->old_len);
323
324 if (args->new_len > args->old_len) {
325 td->td_retval[0] = 0;
326 return (ENOMEM);
327 }
328
329 if (args->new_len < args->old_len) {
330 addr = args->addr + args->new_len;
331 len = args->old_len - args->new_len;
332 error = kern_munmap(td, addr, len);
333 }
334
335 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr;
336 return (error);
337 }
338
339 #define LINUX_MS_ASYNC 0x0001
340 #define LINUX_MS_INVALIDATE 0x0002
341 #define LINUX_MS_SYNC 0x0004
342
343 int
linux_msync(struct thread * td,struct linux_msync_args * args)344 linux_msync(struct thread *td, struct linux_msync_args *args)
345 {
346
347 return (kern_msync(td, args->addr, args->len,
348 args->fl & ~LINUX_MS_SYNC));
349 }
350
351 #ifdef LINUX_LEGACY_SYSCALLS
352 int
linux_time(struct thread * td,struct linux_time_args * args)353 linux_time(struct thread *td, struct linux_time_args *args)
354 {
355 struct timeval tv;
356 l_time_t tm;
357 int error;
358
359 microtime(&tv);
360 tm = tv.tv_sec;
361 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm))))
362 return (error);
363 td->td_retval[0] = tm;
364 return (0);
365 }
366 #endif
367
368 struct l_times_argv {
369 l_clock_t tms_utime;
370 l_clock_t tms_stime;
371 l_clock_t tms_cutime;
372 l_clock_t tms_cstime;
373 };
374
375 /*
376 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value.
377 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK
378 * auxiliary vector entry.
379 */
380 #define CLK_TCK 100
381
382 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
383 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz))
384
385 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER(2,4,0) ? \
386 CONVNTCK(r) : CONVOTCK(r))
387
388 int
linux_times(struct thread * td,struct linux_times_args * args)389 linux_times(struct thread *td, struct linux_times_args *args)
390 {
391 struct timeval tv, utime, stime, cutime, cstime;
392 struct l_times_argv tms;
393 struct proc *p;
394 int error;
395
396 if (args->buf != NULL) {
397 p = td->td_proc;
398 PROC_LOCK(p);
399 PROC_STATLOCK(p);
400 calcru(p, &utime, &stime);
401 PROC_STATUNLOCK(p);
402 calccru(p, &cutime, &cstime);
403 PROC_UNLOCK(p);
404
405 tms.tms_utime = CONVTCK(utime);
406 tms.tms_stime = CONVTCK(stime);
407
408 tms.tms_cutime = CONVTCK(cutime);
409 tms.tms_cstime = CONVTCK(cstime);
410
411 if ((error = copyout(&tms, args->buf, sizeof(tms))))
412 return (error);
413 }
414
415 microuptime(&tv);
416 td->td_retval[0] = (int)CONVTCK(tv);
417 return (0);
418 }
419
420 int
linux_newuname(struct thread * td,struct linux_newuname_args * args)421 linux_newuname(struct thread *td, struct linux_newuname_args *args)
422 {
423 struct l_new_utsname utsname;
424 char osname[LINUX_MAX_UTSNAME];
425 char osrelease[LINUX_MAX_UTSNAME];
426 char *p;
427
428 linux_get_osname(td, osname);
429 linux_get_osrelease(td, osrelease);
430
431 bzero(&utsname, sizeof(utsname));
432 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME);
433 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME);
434 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME);
435 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME);
436 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME);
437 for (p = utsname.version; *p != '\0'; ++p)
438 if (*p == '\n') {
439 *p = '\0';
440 break;
441 }
442 #if defined(__amd64__)
443 /*
444 * On amd64, Linux uname(2) needs to return "x86_64"
445 * for both 64-bit and 32-bit applications. On 32-bit,
446 * the string returned by getauxval(AT_PLATFORM) needs
447 * to remain "i686", though.
448 */
449 #if defined(COMPAT_LINUX32)
450 if (linux32_emulate_i386)
451 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
452 else
453 #endif
454 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME);
455 #elif defined(__aarch64__)
456 strlcpy(utsname.machine, "aarch64", LINUX_MAX_UTSNAME);
457 #elif defined(__i386__)
458 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME);
459 #endif
460
461 return (copyout(&utsname, args->buf, sizeof(utsname)));
462 }
463
464 struct l_utimbuf {
465 l_time_t l_actime;
466 l_time_t l_modtime;
467 };
468
469 #ifdef LINUX_LEGACY_SYSCALLS
470 int
linux_utime(struct thread * td,struct linux_utime_args * args)471 linux_utime(struct thread *td, struct linux_utime_args *args)
472 {
473 struct timeval tv[2], *tvp;
474 struct l_utimbuf lut;
475 char *fname;
476 int error;
477
478 if (args->times) {
479 if ((error = copyin(args->times, &lut, sizeof lut)) != 0)
480 return (error);
481 tv[0].tv_sec = lut.l_actime;
482 tv[0].tv_usec = 0;
483 tv[1].tv_sec = lut.l_modtime;
484 tv[1].tv_usec = 0;
485 tvp = tv;
486 } else
487 tvp = NULL;
488
489 if (!LUSECONVPATH(td)) {
490 error = kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE,
491 tvp, UIO_SYSSPACE);
492 } else {
493 LCONVPATHEXIST(args->fname, &fname);
494 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp,
495 UIO_SYSSPACE);
496 LFREEPATH(fname);
497 }
498 return (error);
499 }
500 #endif
501
502 #ifdef LINUX_LEGACY_SYSCALLS
503 int
linux_utimes(struct thread * td,struct linux_utimes_args * args)504 linux_utimes(struct thread *td, struct linux_utimes_args *args)
505 {
506 l_timeval ltv[2];
507 struct timeval tv[2], *tvp = NULL;
508 char *fname;
509 int error;
510
511 if (args->tptr != NULL) {
512 if ((error = copyin(args->tptr, ltv, sizeof ltv)) != 0)
513 return (error);
514 tv[0].tv_sec = ltv[0].tv_sec;
515 tv[0].tv_usec = ltv[0].tv_usec;
516 tv[1].tv_sec = ltv[1].tv_sec;
517 tv[1].tv_usec = ltv[1].tv_usec;
518 tvp = tv;
519 }
520
521 if (!LUSECONVPATH(td)) {
522 error = kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE,
523 tvp, UIO_SYSSPACE);
524 } else {
525 LCONVPATHEXIST(args->fname, &fname);
526 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE,
527 tvp, UIO_SYSSPACE);
528 LFREEPATH(fname);
529 }
530 return (error);
531 }
532 #endif
533
534 static int
linux_utimensat_lts_to_ts(struct l_timespec * l_times,struct timespec * times)535 linux_utimensat_lts_to_ts(struct l_timespec *l_times, struct timespec *times)
536 {
537
538 if (l_times->tv_nsec != LINUX_UTIME_OMIT &&
539 l_times->tv_nsec != LINUX_UTIME_NOW &&
540 (l_times->tv_nsec < 0 || l_times->tv_nsec > 999999999))
541 return (EINVAL);
542
543 times->tv_sec = l_times->tv_sec;
544 switch (l_times->tv_nsec)
545 {
546 case LINUX_UTIME_OMIT:
547 times->tv_nsec = UTIME_OMIT;
548 break;
549 case LINUX_UTIME_NOW:
550 times->tv_nsec = UTIME_NOW;
551 break;
552 default:
553 times->tv_nsec = l_times->tv_nsec;
554 }
555
556 return (0);
557 }
558
559 static int
linux_common_utimensat(struct thread * td,int ldfd,const char * pathname,struct timespec * timesp,int lflags)560 linux_common_utimensat(struct thread *td, int ldfd, const char *pathname,
561 struct timespec *timesp, int lflags)
562 {
563 char *path = NULL;
564 int error, dfd, flags = 0;
565
566 dfd = (ldfd == LINUX_AT_FDCWD) ? AT_FDCWD : ldfd;
567
568 if (lflags & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH))
569 return (EINVAL);
570
571 if (timesp != NULL) {
572 /* This breaks POSIX, but is what the Linux kernel does
573 * _on purpose_ (documented in the man page for utimensat(2)),
574 * so we must follow that behaviour. */
575 if (timesp[0].tv_nsec == UTIME_OMIT &&
576 timesp[1].tv_nsec == UTIME_OMIT)
577 return (0);
578 }
579
580 if (lflags & LINUX_AT_SYMLINK_NOFOLLOW)
581 flags |= AT_SYMLINK_NOFOLLOW;
582 if (lflags & LINUX_AT_EMPTY_PATH)
583 flags |= AT_EMPTY_PATH;
584
585 if (!LUSECONVPATH(td)) {
586 if (pathname != NULL) {
587 return (kern_utimensat(td, dfd, pathname,
588 UIO_USERSPACE, timesp, UIO_SYSSPACE, flags));
589 }
590 }
591
592 if (pathname != NULL)
593 LCONVPATHEXIST_AT(pathname, &path, dfd);
594 else if (lflags != 0)
595 return (EINVAL);
596
597 if (path == NULL)
598 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE);
599 else {
600 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp,
601 UIO_SYSSPACE, flags);
602 LFREEPATH(path);
603 }
604
605 return (error);
606 }
607
608 int
linux_utimensat(struct thread * td,struct linux_utimensat_args * args)609 linux_utimensat(struct thread *td, struct linux_utimensat_args *args)
610 {
611 struct l_timespec l_times[2];
612 struct timespec times[2], *timesp;
613 int error;
614
615 if (args->times != NULL) {
616 error = copyin(args->times, l_times, sizeof(l_times));
617 if (error != 0)
618 return (error);
619
620 error = linux_utimensat_lts_to_ts(&l_times[0], ×[0]);
621 if (error != 0)
622 return (error);
623 error = linux_utimensat_lts_to_ts(&l_times[1], ×[1]);
624 if (error != 0)
625 return (error);
626 timesp = times;
627 } else
628 timesp = NULL;
629
630 return (linux_common_utimensat(td, args->dfd, args->pathname,
631 timesp, args->flags));
632 }
633
634 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
635 static int
linux_utimensat_lts64_to_ts(struct l_timespec64 * l_times,struct timespec * times)636 linux_utimensat_lts64_to_ts(struct l_timespec64 *l_times, struct timespec *times)
637 {
638
639 /* Zero out the padding in compat mode. */
640 l_times->tv_nsec &= 0xFFFFFFFFUL;
641
642 if (l_times->tv_nsec != LINUX_UTIME_OMIT &&
643 l_times->tv_nsec != LINUX_UTIME_NOW &&
644 (l_times->tv_nsec < 0 || l_times->tv_nsec > 999999999))
645 return (EINVAL);
646
647 times->tv_sec = l_times->tv_sec;
648 switch (l_times->tv_nsec)
649 {
650 case LINUX_UTIME_OMIT:
651 times->tv_nsec = UTIME_OMIT;
652 break;
653 case LINUX_UTIME_NOW:
654 times->tv_nsec = UTIME_NOW;
655 break;
656 default:
657 times->tv_nsec = l_times->tv_nsec;
658 }
659
660 return (0);
661 }
662
663 int
linux_utimensat_time64(struct thread * td,struct linux_utimensat_time64_args * args)664 linux_utimensat_time64(struct thread *td, struct linux_utimensat_time64_args *args)
665 {
666 struct l_timespec64 l_times[2];
667 struct timespec times[2], *timesp;
668 int error;
669
670 if (args->times64 != NULL) {
671 error = copyin(args->times64, l_times, sizeof(l_times));
672 if (error != 0)
673 return (error);
674
675 error = linux_utimensat_lts64_to_ts(&l_times[0], ×[0]);
676 if (error != 0)
677 return (error);
678 error = linux_utimensat_lts64_to_ts(&l_times[1], ×[1]);
679 if (error != 0)
680 return (error);
681 timesp = times;
682 } else
683 timesp = NULL;
684
685 return (linux_common_utimensat(td, args->dfd, args->pathname,
686 timesp, args->flags));
687 }
688 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
689
690 #ifdef LINUX_LEGACY_SYSCALLS
691 int
linux_futimesat(struct thread * td,struct linux_futimesat_args * args)692 linux_futimesat(struct thread *td, struct linux_futimesat_args *args)
693 {
694 l_timeval ltv[2];
695 struct timeval tv[2], *tvp = NULL;
696 char *fname;
697 int error, dfd;
698
699 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
700
701 if (args->utimes != NULL) {
702 if ((error = copyin(args->utimes, ltv, sizeof ltv)) != 0)
703 return (error);
704 tv[0].tv_sec = ltv[0].tv_sec;
705 tv[0].tv_usec = ltv[0].tv_usec;
706 tv[1].tv_sec = ltv[1].tv_sec;
707 tv[1].tv_usec = ltv[1].tv_usec;
708 tvp = tv;
709 }
710
711 if (!LUSECONVPATH(td)) {
712 error = kern_utimesat(td, dfd, args->filename, UIO_USERSPACE,
713 tvp, UIO_SYSSPACE);
714 } else {
715 LCONVPATHEXIST_AT(args->filename, &fname, dfd);
716 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE,
717 tvp, UIO_SYSSPACE);
718 LFREEPATH(fname);
719 }
720 return (error);
721 }
722 #endif
723
724 static int
linux_common_wait(struct thread * td,idtype_t idtype,int id,int * statusp,int options,void * rup,l_siginfo_t * infop)725 linux_common_wait(struct thread *td, idtype_t idtype, int id, int *statusp,
726 int options, void *rup, l_siginfo_t *infop)
727 {
728 l_siginfo_t lsi;
729 siginfo_t siginfo;
730 struct __wrusage wru;
731 int error, status, tmpstat, sig;
732
733 error = kern_wait6(td, idtype, id, &status, options,
734 rup != NULL ? &wru : NULL, &siginfo);
735
736 if (error == 0 && statusp) {
737 tmpstat = status & 0xffff;
738 if (WIFSIGNALED(tmpstat)) {
739 tmpstat = (tmpstat & 0xffffff80) |
740 bsd_to_linux_signal(WTERMSIG(tmpstat));
741 } else if (WIFSTOPPED(tmpstat)) {
742 tmpstat = (tmpstat & 0xffff00ff) |
743 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8);
744 #if defined(__aarch64__) || (defined(__amd64__) && !defined(COMPAT_LINUX32))
745 if (WSTOPSIG(status) == SIGTRAP) {
746 tmpstat = linux_ptrace_status(td,
747 siginfo.si_pid, tmpstat);
748 }
749 #endif
750 } else if (WIFCONTINUED(tmpstat)) {
751 tmpstat = 0xffff;
752 }
753 error = copyout(&tmpstat, statusp, sizeof(int));
754 }
755 if (error == 0 && rup != NULL)
756 error = linux_copyout_rusage(&wru.wru_self, rup);
757 if (error == 0 && infop != NULL && td->td_retval[0] != 0) {
758 sig = bsd_to_linux_signal(siginfo.si_signo);
759 siginfo_to_lsiginfo(&siginfo, &lsi, sig);
760 error = copyout(&lsi, infop, sizeof(lsi));
761 }
762
763 return (error);
764 }
765
766 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
767 int
linux_waitpid(struct thread * td,struct linux_waitpid_args * args)768 linux_waitpid(struct thread *td, struct linux_waitpid_args *args)
769 {
770 struct linux_wait4_args wait4_args = {
771 .pid = args->pid,
772 .status = args->status,
773 .options = args->options,
774 .rusage = NULL,
775 };
776
777 return (linux_wait4(td, &wait4_args));
778 }
779 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
780
781 int
linux_wait4(struct thread * td,struct linux_wait4_args * args)782 linux_wait4(struct thread *td, struct linux_wait4_args *args)
783 {
784 struct proc *p;
785 int options, id, idtype;
786
787 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG |
788 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
789 return (EINVAL);
790
791 /* -INT_MIN is not defined. */
792 if (args->pid == INT_MIN)
793 return (ESRCH);
794
795 options = 0;
796 linux_to_bsd_waitopts(args->options, &options);
797
798 /*
799 * For backward compatibility we implicitly add flags WEXITED
800 * and WTRAPPED here.
801 */
802 options |= WEXITED | WTRAPPED;
803
804 if (args->pid == WAIT_ANY) {
805 idtype = P_ALL;
806 id = 0;
807 } else if (args->pid < 0) {
808 idtype = P_PGID;
809 id = (id_t)-args->pid;
810 } else if (args->pid == 0) {
811 idtype = P_PGID;
812 p = td->td_proc;
813 PROC_LOCK(p);
814 id = p->p_pgid;
815 PROC_UNLOCK(p);
816 } else {
817 idtype = P_PID;
818 id = (id_t)args->pid;
819 }
820
821 return (linux_common_wait(td, idtype, id, args->status, options,
822 args->rusage, NULL));
823 }
824
825 int
linux_waitid(struct thread * td,struct linux_waitid_args * args)826 linux_waitid(struct thread *td, struct linux_waitid_args *args)
827 {
828 idtype_t idtype;
829 int error, options;
830 struct proc *p;
831 pid_t id;
832
833 if (args->options & ~(LINUX_WNOHANG | LINUX_WNOWAIT | LINUX_WEXITED |
834 LINUX_WSTOPPED | LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL))
835 return (EINVAL);
836
837 options = 0;
838 linux_to_bsd_waitopts(args->options, &options);
839
840 id = args->id;
841 switch (args->idtype) {
842 case LINUX_P_ALL:
843 idtype = P_ALL;
844 break;
845 case LINUX_P_PID:
846 if (args->id <= 0)
847 return (EINVAL);
848 idtype = P_PID;
849 break;
850 case LINUX_P_PGID:
851 if (linux_kernver(td) >= LINUX_KERNVER(5,4,0) && args->id == 0) {
852 p = td->td_proc;
853 PROC_LOCK(p);
854 id = p->p_pgid;
855 PROC_UNLOCK(p);
856 } else if (args->id <= 0)
857 return (EINVAL);
858 idtype = P_PGID;
859 break;
860 case LINUX_P_PIDFD:
861 LINUX_RATELIMIT_MSG("unsupported waitid P_PIDFD idtype");
862 return (ENOSYS);
863 default:
864 return (EINVAL);
865 }
866
867 error = linux_common_wait(td, idtype, id, NULL, options,
868 args->rusage, args->info);
869 td->td_retval[0] = 0;
870
871 return (error);
872 }
873
874 #ifdef LINUX_LEGACY_SYSCALLS
875 int
linux_mknod(struct thread * td,struct linux_mknod_args * args)876 linux_mknod(struct thread *td, struct linux_mknod_args *args)
877 {
878 char *path;
879 int error;
880 enum uio_seg seg;
881 bool convpath;
882
883 convpath = LUSECONVPATH(td);
884 if (!convpath) {
885 path = args->path;
886 seg = UIO_USERSPACE;
887 } else {
888 LCONVPATHCREAT(args->path, &path);
889 seg = UIO_SYSSPACE;
890 }
891
892 switch (args->mode & S_IFMT) {
893 case S_IFIFO:
894 case S_IFSOCK:
895 error = kern_mkfifoat(td, AT_FDCWD, path, seg,
896 args->mode);
897 break;
898
899 case S_IFCHR:
900 case S_IFBLK:
901 error = kern_mknodat(td, AT_FDCWD, path, seg,
902 args->mode, linux_decode_dev(args->dev));
903 break;
904
905 case S_IFDIR:
906 error = EPERM;
907 break;
908
909 case 0:
910 args->mode |= S_IFREG;
911 /* FALLTHROUGH */
912 case S_IFREG:
913 error = kern_openat(td, AT_FDCWD, path, seg,
914 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
915 if (error == 0)
916 kern_close(td, td->td_retval[0]);
917 break;
918
919 default:
920 error = EINVAL;
921 break;
922 }
923 if (convpath)
924 LFREEPATH(path);
925 return (error);
926 }
927 #endif
928
929 int
linux_mknodat(struct thread * td,struct linux_mknodat_args * args)930 linux_mknodat(struct thread *td, struct linux_mknodat_args *args)
931 {
932 char *path;
933 int error, dfd;
934 enum uio_seg seg;
935 bool convpath;
936
937 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
938
939 convpath = LUSECONVPATH(td);
940 if (!convpath) {
941 path = __DECONST(char *, args->filename);
942 seg = UIO_USERSPACE;
943 } else {
944 LCONVPATHCREAT_AT(args->filename, &path, dfd);
945 seg = UIO_SYSSPACE;
946 }
947
948 switch (args->mode & S_IFMT) {
949 case S_IFIFO:
950 case S_IFSOCK:
951 error = kern_mkfifoat(td, dfd, path, seg, args->mode);
952 break;
953
954 case S_IFCHR:
955 case S_IFBLK:
956 error = kern_mknodat(td, dfd, path, seg, args->mode,
957 linux_decode_dev(args->dev));
958 break;
959
960 case S_IFDIR:
961 error = EPERM;
962 break;
963
964 case 0:
965 args->mode |= S_IFREG;
966 /* FALLTHROUGH */
967 case S_IFREG:
968 error = kern_openat(td, dfd, path, seg,
969 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
970 if (error == 0)
971 kern_close(td, td->td_retval[0]);
972 break;
973
974 default:
975 error = EINVAL;
976 break;
977 }
978 if (convpath)
979 LFREEPATH(path);
980 return (error);
981 }
982
983 /*
984 * UGH! This is just about the dumbest idea I've ever heard!!
985 */
986 int
linux_personality(struct thread * td,struct linux_personality_args * args)987 linux_personality(struct thread *td, struct linux_personality_args *args)
988 {
989 struct linux_pemuldata *pem;
990 struct proc *p = td->td_proc;
991 uint32_t old;
992
993 PROC_LOCK(p);
994 pem = pem_find(p);
995 old = pem->persona;
996 if (args->per != 0xffffffff)
997 pem->persona = args->per;
998 PROC_UNLOCK(p);
999
1000 td->td_retval[0] = old;
1001 return (0);
1002 }
1003
1004 struct l_itimerval {
1005 l_timeval it_interval;
1006 l_timeval it_value;
1007 };
1008
1009 #define B2L_ITIMERVAL(bip, lip) \
1010 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \
1011 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \
1012 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \
1013 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec;
1014
1015 int
linux_setitimer(struct thread * td,struct linux_setitimer_args * uap)1016 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap)
1017 {
1018 int error;
1019 struct l_itimerval ls;
1020 struct itimerval aitv, oitv;
1021
1022 if (uap->itv == NULL) {
1023 uap->itv = uap->oitv;
1024 return (linux_getitimer(td, (struct linux_getitimer_args *)uap));
1025 }
1026
1027 error = copyin(uap->itv, &ls, sizeof(ls));
1028 if (error != 0)
1029 return (error);
1030 B2L_ITIMERVAL(&aitv, &ls);
1031 error = kern_setitimer(td, uap->which, &aitv, &oitv);
1032 if (error != 0 || uap->oitv == NULL)
1033 return (error);
1034 B2L_ITIMERVAL(&ls, &oitv);
1035
1036 return (copyout(&ls, uap->oitv, sizeof(ls)));
1037 }
1038
1039 int
linux_getitimer(struct thread * td,struct linux_getitimer_args * uap)1040 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap)
1041 {
1042 int error;
1043 struct l_itimerval ls;
1044 struct itimerval aitv;
1045
1046 error = kern_getitimer(td, uap->which, &aitv);
1047 if (error != 0)
1048 return (error);
1049 B2L_ITIMERVAL(&ls, &aitv);
1050 return (copyout(&ls, uap->itv, sizeof(ls)));
1051 }
1052
1053 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1054 int
linux_nice(struct thread * td,struct linux_nice_args * args)1055 linux_nice(struct thread *td, struct linux_nice_args *args)
1056 {
1057
1058 return (kern_setpriority(td, PRIO_PROCESS, 0, args->inc));
1059 }
1060 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1061
1062 int
linux_setgroups(struct thread * td,struct linux_setgroups_args * args)1063 linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
1064 {
1065 struct ucred *newcred, *oldcred;
1066 l_gid_t *linux_gidset;
1067 gid_t *bsd_gidset;
1068 int ngrp, error;
1069 struct proc *p;
1070
1071 ngrp = args->gidsetsize;
1072 if (ngrp < 0 || ngrp >= ngroups_max + 1)
1073 return (EINVAL);
1074 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
1075 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
1076 if (error)
1077 goto out;
1078 newcred = crget();
1079 crextend(newcred, ngrp + 1);
1080 p = td->td_proc;
1081 PROC_LOCK(p);
1082 oldcred = p->p_ucred;
1083 crcopy(newcred, oldcred);
1084
1085 /*
1086 * cr_groups[0] holds egid. Setting the whole set from
1087 * the supplied set will cause egid to be changed too.
1088 * Keep cr_groups[0] unchanged to prevent that.
1089 */
1090
1091 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) {
1092 PROC_UNLOCK(p);
1093 crfree(newcred);
1094 goto out;
1095 }
1096
1097 if (ngrp > 0) {
1098 newcred->cr_ngroups = ngrp + 1;
1099
1100 bsd_gidset = newcred->cr_groups;
1101 ngrp--;
1102 while (ngrp >= 0) {
1103 bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
1104 ngrp--;
1105 }
1106 } else
1107 newcred->cr_ngroups = 1;
1108
1109 setsugid(p);
1110 proc_set_cred(p, newcred);
1111 PROC_UNLOCK(p);
1112 crfree(oldcred);
1113 error = 0;
1114 out:
1115 free(linux_gidset, M_LINUX);
1116 return (error);
1117 }
1118
1119 int
linux_getgroups(struct thread * td,struct linux_getgroups_args * args)1120 linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
1121 {
1122 struct ucred *cred;
1123 l_gid_t *linux_gidset;
1124 gid_t *bsd_gidset;
1125 int bsd_gidsetsz, ngrp, error;
1126
1127 cred = td->td_ucred;
1128 bsd_gidset = cred->cr_groups;
1129 bsd_gidsetsz = cred->cr_ngroups - 1;
1130
1131 /*
1132 * cr_groups[0] holds egid. Returning the whole set
1133 * here will cause a duplicate. Exclude cr_groups[0]
1134 * to prevent that.
1135 */
1136
1137 if ((ngrp = args->gidsetsize) == 0) {
1138 td->td_retval[0] = bsd_gidsetsz;
1139 return (0);
1140 }
1141
1142 if (ngrp < bsd_gidsetsz)
1143 return (EINVAL);
1144
1145 ngrp = 0;
1146 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
1147 M_LINUX, M_WAITOK);
1148 while (ngrp < bsd_gidsetsz) {
1149 linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
1150 ngrp++;
1151 }
1152
1153 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
1154 free(linux_gidset, M_LINUX);
1155 if (error)
1156 return (error);
1157
1158 td->td_retval[0] = ngrp;
1159 return (0);
1160 }
1161
1162 static bool
linux_get_dummy_limit(l_uint resource,struct rlimit * rlim)1163 linux_get_dummy_limit(l_uint resource, struct rlimit *rlim)
1164 {
1165
1166 if (linux_dummy_rlimits == 0)
1167 return (false);
1168
1169 switch (resource) {
1170 case LINUX_RLIMIT_LOCKS:
1171 case LINUX_RLIMIT_SIGPENDING:
1172 case LINUX_RLIMIT_MSGQUEUE:
1173 case LINUX_RLIMIT_RTTIME:
1174 rlim->rlim_cur = LINUX_RLIM_INFINITY;
1175 rlim->rlim_max = LINUX_RLIM_INFINITY;
1176 return (true);
1177 case LINUX_RLIMIT_NICE:
1178 case LINUX_RLIMIT_RTPRIO:
1179 rlim->rlim_cur = 0;
1180 rlim->rlim_max = 0;
1181 return (true);
1182 default:
1183 return (false);
1184 }
1185 }
1186
1187 int
linux_setrlimit(struct thread * td,struct linux_setrlimit_args * args)1188 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args)
1189 {
1190 struct rlimit bsd_rlim;
1191 struct l_rlimit rlim;
1192 u_int which;
1193 int error;
1194
1195 if (args->resource >= LINUX_RLIM_NLIMITS)
1196 return (EINVAL);
1197
1198 which = linux_to_bsd_resource[args->resource];
1199 if (which == -1)
1200 return (EINVAL);
1201
1202 error = copyin(args->rlim, &rlim, sizeof(rlim));
1203 if (error)
1204 return (error);
1205
1206 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur;
1207 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max;
1208 return (kern_setrlimit(td, which, &bsd_rlim));
1209 }
1210
1211 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1212 int
linux_old_getrlimit(struct thread * td,struct linux_old_getrlimit_args * args)1213 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args)
1214 {
1215 struct l_rlimit rlim;
1216 struct rlimit bsd_rlim;
1217 u_int which;
1218
1219 if (linux_get_dummy_limit(args->resource, &bsd_rlim)) {
1220 rlim.rlim_cur = bsd_rlim.rlim_cur;
1221 rlim.rlim_max = bsd_rlim.rlim_max;
1222 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1223 }
1224
1225 if (args->resource >= LINUX_RLIM_NLIMITS)
1226 return (EINVAL);
1227
1228 which = linux_to_bsd_resource[args->resource];
1229 if (which == -1)
1230 return (EINVAL);
1231
1232 lim_rlimit(td, which, &bsd_rlim);
1233
1234 #ifdef COMPAT_LINUX32
1235 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur;
1236 if (rlim.rlim_cur == UINT_MAX)
1237 rlim.rlim_cur = INT_MAX;
1238 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max;
1239 if (rlim.rlim_max == UINT_MAX)
1240 rlim.rlim_max = INT_MAX;
1241 #else
1242 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur;
1243 if (rlim.rlim_cur == ULONG_MAX)
1244 rlim.rlim_cur = LONG_MAX;
1245 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max;
1246 if (rlim.rlim_max == ULONG_MAX)
1247 rlim.rlim_max = LONG_MAX;
1248 #endif
1249 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1250 }
1251 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1252
1253 int
linux_getrlimit(struct thread * td,struct linux_getrlimit_args * args)1254 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args)
1255 {
1256 struct l_rlimit rlim;
1257 struct rlimit bsd_rlim;
1258 u_int which;
1259
1260 if (linux_get_dummy_limit(args->resource, &bsd_rlim)) {
1261 rlim.rlim_cur = bsd_rlim.rlim_cur;
1262 rlim.rlim_max = bsd_rlim.rlim_max;
1263 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1264 }
1265
1266 if (args->resource >= LINUX_RLIM_NLIMITS)
1267 return (EINVAL);
1268
1269 which = linux_to_bsd_resource[args->resource];
1270 if (which == -1)
1271 return (EINVAL);
1272
1273 lim_rlimit(td, which, &bsd_rlim);
1274
1275 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur;
1276 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max;
1277 return (copyout(&rlim, args->rlim, sizeof(rlim)));
1278 }
1279
1280 int
linux_sched_setscheduler(struct thread * td,struct linux_sched_setscheduler_args * args)1281 linux_sched_setscheduler(struct thread *td,
1282 struct linux_sched_setscheduler_args *args)
1283 {
1284 struct sched_param sched_param;
1285 struct thread *tdt;
1286 int error, policy;
1287
1288 switch (args->policy) {
1289 case LINUX_SCHED_OTHER:
1290 policy = SCHED_OTHER;
1291 break;
1292 case LINUX_SCHED_FIFO:
1293 policy = SCHED_FIFO;
1294 break;
1295 case LINUX_SCHED_RR:
1296 policy = SCHED_RR;
1297 break;
1298 default:
1299 return (EINVAL);
1300 }
1301
1302 error = copyin(args->param, &sched_param, sizeof(sched_param));
1303 if (error)
1304 return (error);
1305
1306 if (linux_map_sched_prio) {
1307 switch (policy) {
1308 case SCHED_OTHER:
1309 if (sched_param.sched_priority != 0)
1310 return (EINVAL);
1311
1312 sched_param.sched_priority =
1313 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
1314 break;
1315 case SCHED_FIFO:
1316 case SCHED_RR:
1317 if (sched_param.sched_priority < 1 ||
1318 sched_param.sched_priority >= LINUX_MAX_RT_PRIO)
1319 return (EINVAL);
1320
1321 /*
1322 * Map [1, LINUX_MAX_RT_PRIO - 1] to
1323 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
1324 */
1325 sched_param.sched_priority =
1326 (sched_param.sched_priority - 1) *
1327 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
1328 (LINUX_MAX_RT_PRIO - 1);
1329 break;
1330 }
1331 }
1332
1333 tdt = linux_tdfind(td, args->pid, -1);
1334 if (tdt == NULL)
1335 return (ESRCH);
1336
1337 error = kern_sched_setscheduler(td, tdt, policy, &sched_param);
1338 PROC_UNLOCK(tdt->td_proc);
1339 return (error);
1340 }
1341
1342 int
linux_sched_getscheduler(struct thread * td,struct linux_sched_getscheduler_args * args)1343 linux_sched_getscheduler(struct thread *td,
1344 struct linux_sched_getscheduler_args *args)
1345 {
1346 struct thread *tdt;
1347 int error, policy;
1348
1349 tdt = linux_tdfind(td, args->pid, -1);
1350 if (tdt == NULL)
1351 return (ESRCH);
1352
1353 error = kern_sched_getscheduler(td, tdt, &policy);
1354 PROC_UNLOCK(tdt->td_proc);
1355
1356 switch (policy) {
1357 case SCHED_OTHER:
1358 td->td_retval[0] = LINUX_SCHED_OTHER;
1359 break;
1360 case SCHED_FIFO:
1361 td->td_retval[0] = LINUX_SCHED_FIFO;
1362 break;
1363 case SCHED_RR:
1364 td->td_retval[0] = LINUX_SCHED_RR;
1365 break;
1366 }
1367 return (error);
1368 }
1369
1370 int
linux_sched_get_priority_max(struct thread * td,struct linux_sched_get_priority_max_args * args)1371 linux_sched_get_priority_max(struct thread *td,
1372 struct linux_sched_get_priority_max_args *args)
1373 {
1374 struct sched_get_priority_max_args bsd;
1375
1376 if (linux_map_sched_prio) {
1377 switch (args->policy) {
1378 case LINUX_SCHED_OTHER:
1379 td->td_retval[0] = 0;
1380 return (0);
1381 case LINUX_SCHED_FIFO:
1382 case LINUX_SCHED_RR:
1383 td->td_retval[0] = LINUX_MAX_RT_PRIO - 1;
1384 return (0);
1385 default:
1386 return (EINVAL);
1387 }
1388 }
1389
1390 switch (args->policy) {
1391 case LINUX_SCHED_OTHER:
1392 bsd.policy = SCHED_OTHER;
1393 break;
1394 case LINUX_SCHED_FIFO:
1395 bsd.policy = SCHED_FIFO;
1396 break;
1397 case LINUX_SCHED_RR:
1398 bsd.policy = SCHED_RR;
1399 break;
1400 default:
1401 return (EINVAL);
1402 }
1403 return (sys_sched_get_priority_max(td, &bsd));
1404 }
1405
1406 int
linux_sched_get_priority_min(struct thread * td,struct linux_sched_get_priority_min_args * args)1407 linux_sched_get_priority_min(struct thread *td,
1408 struct linux_sched_get_priority_min_args *args)
1409 {
1410 struct sched_get_priority_min_args bsd;
1411
1412 if (linux_map_sched_prio) {
1413 switch (args->policy) {
1414 case LINUX_SCHED_OTHER:
1415 td->td_retval[0] = 0;
1416 return (0);
1417 case LINUX_SCHED_FIFO:
1418 case LINUX_SCHED_RR:
1419 td->td_retval[0] = 1;
1420 return (0);
1421 default:
1422 return (EINVAL);
1423 }
1424 }
1425
1426 switch (args->policy) {
1427 case LINUX_SCHED_OTHER:
1428 bsd.policy = SCHED_OTHER;
1429 break;
1430 case LINUX_SCHED_FIFO:
1431 bsd.policy = SCHED_FIFO;
1432 break;
1433 case LINUX_SCHED_RR:
1434 bsd.policy = SCHED_RR;
1435 break;
1436 default:
1437 return (EINVAL);
1438 }
1439 return (sys_sched_get_priority_min(td, &bsd));
1440 }
1441
1442 #define REBOOT_CAD_ON 0x89abcdef
1443 #define REBOOT_CAD_OFF 0
1444 #define REBOOT_HALT 0xcdef0123
1445 #define REBOOT_RESTART 0x01234567
1446 #define REBOOT_RESTART2 0xA1B2C3D4
1447 #define REBOOT_POWEROFF 0x4321FEDC
1448 #define REBOOT_MAGIC1 0xfee1dead
1449 #define REBOOT_MAGIC2 0x28121969
1450 #define REBOOT_MAGIC2A 0x05121996
1451 #define REBOOT_MAGIC2B 0x16041998
1452
1453 int
linux_reboot(struct thread * td,struct linux_reboot_args * args)1454 linux_reboot(struct thread *td, struct linux_reboot_args *args)
1455 {
1456 struct reboot_args bsd_args;
1457
1458 if (args->magic1 != REBOOT_MAGIC1)
1459 return (EINVAL);
1460
1461 switch (args->magic2) {
1462 case REBOOT_MAGIC2:
1463 case REBOOT_MAGIC2A:
1464 case REBOOT_MAGIC2B:
1465 break;
1466 default:
1467 return (EINVAL);
1468 }
1469
1470 switch (args->cmd) {
1471 case REBOOT_CAD_ON:
1472 case REBOOT_CAD_OFF:
1473 return (priv_check(td, PRIV_REBOOT));
1474 case REBOOT_HALT:
1475 bsd_args.opt = RB_HALT;
1476 break;
1477 case REBOOT_RESTART:
1478 case REBOOT_RESTART2:
1479 bsd_args.opt = 0;
1480 break;
1481 case REBOOT_POWEROFF:
1482 bsd_args.opt = RB_POWEROFF;
1483 break;
1484 default:
1485 return (EINVAL);
1486 }
1487 return (sys_reboot(td, &bsd_args));
1488 }
1489
1490 int
linux_getpid(struct thread * td,struct linux_getpid_args * args)1491 linux_getpid(struct thread *td, struct linux_getpid_args *args)
1492 {
1493
1494 td->td_retval[0] = td->td_proc->p_pid;
1495
1496 return (0);
1497 }
1498
1499 int
linux_gettid(struct thread * td,struct linux_gettid_args * args)1500 linux_gettid(struct thread *td, struct linux_gettid_args *args)
1501 {
1502 struct linux_emuldata *em;
1503
1504 em = em_find(td);
1505 KASSERT(em != NULL, ("gettid: emuldata not found.\n"));
1506
1507 td->td_retval[0] = em->em_tid;
1508
1509 return (0);
1510 }
1511
1512 int
linux_getppid(struct thread * td,struct linux_getppid_args * args)1513 linux_getppid(struct thread *td, struct linux_getppid_args *args)
1514 {
1515
1516 td->td_retval[0] = kern_getppid(td);
1517 return (0);
1518 }
1519
1520 int
linux_getgid(struct thread * td,struct linux_getgid_args * args)1521 linux_getgid(struct thread *td, struct linux_getgid_args *args)
1522 {
1523
1524 td->td_retval[0] = td->td_ucred->cr_rgid;
1525 return (0);
1526 }
1527
1528 int
linux_getuid(struct thread * td,struct linux_getuid_args * args)1529 linux_getuid(struct thread *td, struct linux_getuid_args *args)
1530 {
1531
1532 td->td_retval[0] = td->td_ucred->cr_ruid;
1533 return (0);
1534 }
1535
1536 int
linux_getsid(struct thread * td,struct linux_getsid_args * args)1537 linux_getsid(struct thread *td, struct linux_getsid_args *args)
1538 {
1539
1540 return (kern_getsid(td, args->pid));
1541 }
1542
1543 int
linux_nosys(struct thread * td,struct nosys_args * ignore)1544 linux_nosys(struct thread *td, struct nosys_args *ignore)
1545 {
1546
1547 return (ENOSYS);
1548 }
1549
1550 int
linux_getpriority(struct thread * td,struct linux_getpriority_args * args)1551 linux_getpriority(struct thread *td, struct linux_getpriority_args *args)
1552 {
1553 int error;
1554
1555 error = kern_getpriority(td, args->which, args->who);
1556 td->td_retval[0] = 20 - td->td_retval[0];
1557 return (error);
1558 }
1559
1560 int
linux_sethostname(struct thread * td,struct linux_sethostname_args * args)1561 linux_sethostname(struct thread *td, struct linux_sethostname_args *args)
1562 {
1563 int name[2];
1564
1565 name[0] = CTL_KERN;
1566 name[1] = KERN_HOSTNAME;
1567 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname,
1568 args->len, 0, 0));
1569 }
1570
1571 int
linux_setdomainname(struct thread * td,struct linux_setdomainname_args * args)1572 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args)
1573 {
1574 int name[2];
1575
1576 name[0] = CTL_KERN;
1577 name[1] = KERN_NISDOMAINNAME;
1578 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name,
1579 args->len, 0, 0));
1580 }
1581
1582 int
linux_exit_group(struct thread * td,struct linux_exit_group_args * args)1583 linux_exit_group(struct thread *td, struct linux_exit_group_args *args)
1584 {
1585
1586 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid,
1587 args->error_code);
1588
1589 /*
1590 * XXX: we should send a signal to the parent if
1591 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?)
1592 * as it doesnt occur often.
1593 */
1594 exit1(td, args->error_code, 0);
1595 /* NOTREACHED */
1596 }
1597
1598 #define _LINUX_CAPABILITY_VERSION_1 0x19980330
1599 #define _LINUX_CAPABILITY_VERSION_2 0x20071026
1600 #define _LINUX_CAPABILITY_VERSION_3 0x20080522
1601
1602 struct l_user_cap_header {
1603 l_int version;
1604 l_int pid;
1605 };
1606
1607 struct l_user_cap_data {
1608 l_int effective;
1609 l_int permitted;
1610 l_int inheritable;
1611 };
1612
1613 int
linux_capget(struct thread * td,struct linux_capget_args * uap)1614 linux_capget(struct thread *td, struct linux_capget_args *uap)
1615 {
1616 struct l_user_cap_header luch;
1617 struct l_user_cap_data lucd[2];
1618 int error, u32s;
1619
1620 if (uap->hdrp == NULL)
1621 return (EFAULT);
1622
1623 error = copyin(uap->hdrp, &luch, sizeof(luch));
1624 if (error != 0)
1625 return (error);
1626
1627 switch (luch.version) {
1628 case _LINUX_CAPABILITY_VERSION_1:
1629 u32s = 1;
1630 break;
1631 case _LINUX_CAPABILITY_VERSION_2:
1632 case _LINUX_CAPABILITY_VERSION_3:
1633 u32s = 2;
1634 break;
1635 default:
1636 luch.version = _LINUX_CAPABILITY_VERSION_1;
1637 error = copyout(&luch, uap->hdrp, sizeof(luch));
1638 if (error)
1639 return (error);
1640 return (EINVAL);
1641 }
1642
1643 if (luch.pid)
1644 return (EPERM);
1645
1646 if (uap->datap) {
1647 /*
1648 * The current implementation doesn't support setting
1649 * a capability (it's essentially a stub) so indicate
1650 * that no capabilities are currently set or available
1651 * to request.
1652 */
1653 memset(&lucd, 0, u32s * sizeof(lucd[0]));
1654 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0]));
1655 }
1656
1657 return (error);
1658 }
1659
1660 int
linux_capset(struct thread * td,struct linux_capset_args * uap)1661 linux_capset(struct thread *td, struct linux_capset_args *uap)
1662 {
1663 struct l_user_cap_header luch;
1664 struct l_user_cap_data lucd[2];
1665 int error, i, u32s;
1666
1667 if (uap->hdrp == NULL || uap->datap == NULL)
1668 return (EFAULT);
1669
1670 error = copyin(uap->hdrp, &luch, sizeof(luch));
1671 if (error != 0)
1672 return (error);
1673
1674 switch (luch.version) {
1675 case _LINUX_CAPABILITY_VERSION_1:
1676 u32s = 1;
1677 break;
1678 case _LINUX_CAPABILITY_VERSION_2:
1679 case _LINUX_CAPABILITY_VERSION_3:
1680 u32s = 2;
1681 break;
1682 default:
1683 luch.version = _LINUX_CAPABILITY_VERSION_1;
1684 error = copyout(&luch, uap->hdrp, sizeof(luch));
1685 if (error)
1686 return (error);
1687 return (EINVAL);
1688 }
1689
1690 if (luch.pid)
1691 return (EPERM);
1692
1693 error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0]));
1694 if (error != 0)
1695 return (error);
1696
1697 /* We currently don't support setting any capabilities. */
1698 for (i = 0; i < u32s; i++) {
1699 if (lucd[i].effective || lucd[i].permitted ||
1700 lucd[i].inheritable) {
1701 linux_msg(td,
1702 "capset[%d] effective=0x%x, permitted=0x%x, "
1703 "inheritable=0x%x is not implemented", i,
1704 (int)lucd[i].effective, (int)lucd[i].permitted,
1705 (int)lucd[i].inheritable);
1706 return (EPERM);
1707 }
1708 }
1709
1710 return (0);
1711 }
1712
1713 int
linux_prctl(struct thread * td,struct linux_prctl_args * args)1714 linux_prctl(struct thread *td, struct linux_prctl_args *args)
1715 {
1716 int error = 0, max_size, arg;
1717 struct proc *p = td->td_proc;
1718 char comm[LINUX_MAX_COMM_LEN];
1719 int pdeath_signal, trace_state;
1720
1721 switch (args->option) {
1722 case LINUX_PR_SET_PDEATHSIG:
1723 if (!LINUX_SIG_VALID(args->arg2))
1724 return (EINVAL);
1725 pdeath_signal = linux_to_bsd_signal(args->arg2);
1726 return (kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_CTL,
1727 &pdeath_signal));
1728 case LINUX_PR_GET_PDEATHSIG:
1729 error = kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_STATUS,
1730 &pdeath_signal);
1731 if (error != 0)
1732 return (error);
1733 pdeath_signal = bsd_to_linux_signal(pdeath_signal);
1734 return (copyout(&pdeath_signal,
1735 (void *)(register_t)args->arg2,
1736 sizeof(pdeath_signal)));
1737 /*
1738 * In Linux, this flag controls if set[gu]id processes can coredump.
1739 * There are additional semantics imposed on processes that cannot
1740 * coredump:
1741 * - Such processes can not be ptraced.
1742 * - There are some semantics around ownership of process-related files
1743 * in the /proc namespace.
1744 *
1745 * In FreeBSD, we can (and by default, do) disable setuid coredump
1746 * system-wide with 'sugid_coredump.' We control tracability on a
1747 * per-process basis with the procctl PROC_TRACE (=> P2_NOTRACE flag).
1748 * By happy coincidence, P2_NOTRACE also prevents coredumping. So the
1749 * procctl is roughly analogous to Linux's DUMPABLE.
1750 *
1751 * So, proxy these knobs to the corresponding PROC_TRACE setting.
1752 */
1753 case LINUX_PR_GET_DUMPABLE:
1754 error = kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_STATUS,
1755 &trace_state);
1756 if (error != 0)
1757 return (error);
1758 td->td_retval[0] = (trace_state != -1);
1759 return (0);
1760 case LINUX_PR_SET_DUMPABLE:
1761 /*
1762 * It is only valid for userspace to set one of these two
1763 * flags, and only one at a time.
1764 */
1765 switch (args->arg2) {
1766 case LINUX_SUID_DUMP_DISABLE:
1767 trace_state = PROC_TRACE_CTL_DISABLE_EXEC;
1768 break;
1769 case LINUX_SUID_DUMP_USER:
1770 trace_state = PROC_TRACE_CTL_ENABLE;
1771 break;
1772 default:
1773 return (EINVAL);
1774 }
1775 return (kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_CTL,
1776 &trace_state));
1777 case LINUX_PR_GET_KEEPCAPS:
1778 /*
1779 * Indicate that we always clear the effective and
1780 * permitted capability sets when the user id becomes
1781 * non-zero (actually the capability sets are simply
1782 * always zero in the current implementation).
1783 */
1784 td->td_retval[0] = 0;
1785 break;
1786 case LINUX_PR_SET_KEEPCAPS:
1787 /*
1788 * Ignore requests to keep the effective and permitted
1789 * capability sets when the user id becomes non-zero.
1790 */
1791 break;
1792 case LINUX_PR_SET_NAME:
1793 /*
1794 * To be on the safe side we need to make sure to not
1795 * overflow the size a Linux program expects. We already
1796 * do this here in the copyin, so that we don't need to
1797 * check on copyout.
1798 */
1799 max_size = MIN(sizeof(comm), sizeof(p->p_comm));
1800 error = copyinstr((void *)(register_t)args->arg2, comm,
1801 max_size, NULL);
1802
1803 /* Linux silently truncates the name if it is too long. */
1804 if (error == ENAMETOOLONG) {
1805 /*
1806 * XXX: copyinstr() isn't documented to populate the
1807 * array completely, so do a copyin() to be on the
1808 * safe side. This should be changed in case
1809 * copyinstr() is changed to guarantee this.
1810 */
1811 error = copyin((void *)(register_t)args->arg2, comm,
1812 max_size - 1);
1813 comm[max_size - 1] = '\0';
1814 }
1815 if (error)
1816 return (error);
1817
1818 PROC_LOCK(p);
1819 strlcpy(p->p_comm, comm, sizeof(p->p_comm));
1820 PROC_UNLOCK(p);
1821 break;
1822 case LINUX_PR_GET_NAME:
1823 PROC_LOCK(p);
1824 strlcpy(comm, p->p_comm, sizeof(comm));
1825 PROC_UNLOCK(p);
1826 error = copyout(comm, (void *)(register_t)args->arg2,
1827 strlen(comm) + 1);
1828 break;
1829 case LINUX_PR_GET_SECCOMP:
1830 case LINUX_PR_SET_SECCOMP:
1831 /*
1832 * Same as returned by Linux without CONFIG_SECCOMP enabled.
1833 */
1834 error = EINVAL;
1835 break;
1836 case LINUX_PR_CAPBSET_READ:
1837 #if 0
1838 /*
1839 * This makes too much noise with Ubuntu Focal.
1840 */
1841 linux_msg(td, "unsupported prctl PR_CAPBSET_READ %d",
1842 (int)args->arg2);
1843 #endif
1844 error = EINVAL;
1845 break;
1846 case LINUX_PR_SET_NO_NEW_PRIVS:
1847 arg = args->arg2 == 1 ?
1848 PROC_NO_NEW_PRIVS_ENABLE : PROC_NO_NEW_PRIVS_DISABLE;
1849 error = kern_procctl(td, P_PID, p->p_pid,
1850 PROC_NO_NEW_PRIVS_CTL, &arg);
1851 break;
1852 case LINUX_PR_SET_PTRACER:
1853 linux_msg(td, "unsupported prctl PR_SET_PTRACER");
1854 error = EINVAL;
1855 break;
1856 default:
1857 linux_msg(td, "unsupported prctl option %d", args->option);
1858 error = EINVAL;
1859 break;
1860 }
1861
1862 return (error);
1863 }
1864
1865 int
linux_sched_setparam(struct thread * td,struct linux_sched_setparam_args * uap)1866 linux_sched_setparam(struct thread *td,
1867 struct linux_sched_setparam_args *uap)
1868 {
1869 struct sched_param sched_param;
1870 struct thread *tdt;
1871 int error, policy;
1872
1873 error = copyin(uap->param, &sched_param, sizeof(sched_param));
1874 if (error)
1875 return (error);
1876
1877 tdt = linux_tdfind(td, uap->pid, -1);
1878 if (tdt == NULL)
1879 return (ESRCH);
1880
1881 if (linux_map_sched_prio) {
1882 error = kern_sched_getscheduler(td, tdt, &policy);
1883 if (error)
1884 goto out;
1885
1886 switch (policy) {
1887 case SCHED_OTHER:
1888 if (sched_param.sched_priority != 0) {
1889 error = EINVAL;
1890 goto out;
1891 }
1892 sched_param.sched_priority =
1893 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE;
1894 break;
1895 case SCHED_FIFO:
1896 case SCHED_RR:
1897 if (sched_param.sched_priority < 1 ||
1898 sched_param.sched_priority >= LINUX_MAX_RT_PRIO) {
1899 error = EINVAL;
1900 goto out;
1901 }
1902 /*
1903 * Map [1, LINUX_MAX_RT_PRIO - 1] to
1904 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down).
1905 */
1906 sched_param.sched_priority =
1907 (sched_param.sched_priority - 1) *
1908 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) /
1909 (LINUX_MAX_RT_PRIO - 1);
1910 break;
1911 }
1912 }
1913
1914 error = kern_sched_setparam(td, tdt, &sched_param);
1915 out: PROC_UNLOCK(tdt->td_proc);
1916 return (error);
1917 }
1918
1919 int
linux_sched_getparam(struct thread * td,struct linux_sched_getparam_args * uap)1920 linux_sched_getparam(struct thread *td,
1921 struct linux_sched_getparam_args *uap)
1922 {
1923 struct sched_param sched_param;
1924 struct thread *tdt;
1925 int error, policy;
1926
1927 tdt = linux_tdfind(td, uap->pid, -1);
1928 if (tdt == NULL)
1929 return (ESRCH);
1930
1931 error = kern_sched_getparam(td, tdt, &sched_param);
1932 if (error) {
1933 PROC_UNLOCK(tdt->td_proc);
1934 return (error);
1935 }
1936
1937 if (linux_map_sched_prio) {
1938 error = kern_sched_getscheduler(td, tdt, &policy);
1939 PROC_UNLOCK(tdt->td_proc);
1940 if (error)
1941 return (error);
1942
1943 switch (policy) {
1944 case SCHED_OTHER:
1945 sched_param.sched_priority = 0;
1946 break;
1947 case SCHED_FIFO:
1948 case SCHED_RR:
1949 /*
1950 * Map [0, RTP_PRIO_MAX - RTP_PRIO_MIN] to
1951 * [1, LINUX_MAX_RT_PRIO - 1] (rounding up).
1952 */
1953 sched_param.sched_priority =
1954 (sched_param.sched_priority *
1955 (LINUX_MAX_RT_PRIO - 1) +
1956 (RTP_PRIO_MAX - RTP_PRIO_MIN - 1)) /
1957 (RTP_PRIO_MAX - RTP_PRIO_MIN) + 1;
1958 break;
1959 }
1960 } else
1961 PROC_UNLOCK(tdt->td_proc);
1962
1963 error = copyout(&sched_param, uap->param, sizeof(sched_param));
1964 return (error);
1965 }
1966
1967 /*
1968 * Get affinity of a process.
1969 */
1970 int
linux_sched_getaffinity(struct thread * td,struct linux_sched_getaffinity_args * args)1971 linux_sched_getaffinity(struct thread *td,
1972 struct linux_sched_getaffinity_args *args)
1973 {
1974 struct thread *tdt;
1975 cpuset_t *mask;
1976 size_t size;
1977 int error;
1978 id_t tid;
1979
1980 tdt = linux_tdfind(td, args->pid, -1);
1981 if (tdt == NULL)
1982 return (ESRCH);
1983 tid = tdt->td_tid;
1984 PROC_UNLOCK(tdt->td_proc);
1985
1986 mask = malloc(sizeof(cpuset_t), M_LINUX, M_WAITOK | M_ZERO);
1987 size = min(args->len, sizeof(cpuset_t));
1988 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
1989 tid, size, mask);
1990 if (error == ERANGE)
1991 error = EINVAL;
1992 if (error == 0)
1993 error = copyout(mask, args->user_mask_ptr, size);
1994 if (error == 0)
1995 td->td_retval[0] = size;
1996 free(mask, M_LINUX);
1997 return (error);
1998 }
1999
2000 /*
2001 * Set affinity of a process.
2002 */
2003 int
linux_sched_setaffinity(struct thread * td,struct linux_sched_setaffinity_args * args)2004 linux_sched_setaffinity(struct thread *td,
2005 struct linux_sched_setaffinity_args *args)
2006 {
2007 struct thread *tdt;
2008 cpuset_t *mask;
2009 int cpu, error;
2010 size_t len;
2011 id_t tid;
2012
2013 tdt = linux_tdfind(td, args->pid, -1);
2014 if (tdt == NULL)
2015 return (ESRCH);
2016 tid = tdt->td_tid;
2017 PROC_UNLOCK(tdt->td_proc);
2018
2019 len = min(args->len, sizeof(cpuset_t));
2020 mask = malloc(sizeof(cpuset_t), M_TEMP, M_WAITOK | M_ZERO);;
2021 error = copyin(args->user_mask_ptr, mask, len);
2022 if (error != 0)
2023 goto out;
2024 /* Linux ignore high bits */
2025 CPU_FOREACH_ISSET(cpu, mask)
2026 if (cpu > mp_maxid)
2027 CPU_CLR(cpu, mask);
2028
2029 error = kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID,
2030 tid, mask);
2031 if (error == EDEADLK)
2032 error = EINVAL;
2033 out:
2034 free(mask, M_TEMP);
2035 return (error);
2036 }
2037
2038 struct linux_rlimit64 {
2039 uint64_t rlim_cur;
2040 uint64_t rlim_max;
2041 };
2042
2043 int
linux_prlimit64(struct thread * td,struct linux_prlimit64_args * args)2044 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args)
2045 {
2046 struct rlimit rlim, nrlim;
2047 struct linux_rlimit64 lrlim;
2048 struct proc *p;
2049 u_int which;
2050 int flags;
2051 int error;
2052
2053 if (args->new == NULL && args->old != NULL) {
2054 if (linux_get_dummy_limit(args->resource, &rlim)) {
2055 lrlim.rlim_cur = rlim.rlim_cur;
2056 lrlim.rlim_max = rlim.rlim_max;
2057 return (copyout(&lrlim, args->old, sizeof(lrlim)));
2058 }
2059 }
2060
2061 if (args->resource >= LINUX_RLIM_NLIMITS)
2062 return (EINVAL);
2063
2064 which = linux_to_bsd_resource[args->resource];
2065 if (which == -1)
2066 return (EINVAL);
2067
2068 if (args->new != NULL) {
2069 /*
2070 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux
2071 * rlim is unsigned 64-bit. FreeBSD treats negative limits
2072 * as INFINITY so we do not need a conversion even.
2073 */
2074 error = copyin(args->new, &nrlim, sizeof(nrlim));
2075 if (error != 0)
2076 return (error);
2077 }
2078
2079 flags = PGET_HOLD | PGET_NOTWEXIT;
2080 if (args->new != NULL)
2081 flags |= PGET_CANDEBUG;
2082 else
2083 flags |= PGET_CANSEE;
2084 if (args->pid == 0) {
2085 p = td->td_proc;
2086 PHOLD(p);
2087 } else {
2088 error = pget(args->pid, flags, &p);
2089 if (error != 0)
2090 return (error);
2091 }
2092 if (args->old != NULL) {
2093 PROC_LOCK(p);
2094 lim_rlimit_proc(p, which, &rlim);
2095 PROC_UNLOCK(p);
2096 if (rlim.rlim_cur == RLIM_INFINITY)
2097 lrlim.rlim_cur = LINUX_RLIM_INFINITY;
2098 else
2099 lrlim.rlim_cur = rlim.rlim_cur;
2100 if (rlim.rlim_max == RLIM_INFINITY)
2101 lrlim.rlim_max = LINUX_RLIM_INFINITY;
2102 else
2103 lrlim.rlim_max = rlim.rlim_max;
2104 error = copyout(&lrlim, args->old, sizeof(lrlim));
2105 if (error != 0)
2106 goto out;
2107 }
2108
2109 if (args->new != NULL)
2110 error = kern_proc_setrlimit(td, p, which, &nrlim);
2111
2112 out:
2113 PRELE(p);
2114 return (error);
2115 }
2116
2117 int
linux_pselect6(struct thread * td,struct linux_pselect6_args * args)2118 linux_pselect6(struct thread *td, struct linux_pselect6_args *args)
2119 {
2120 struct timespec ts, *tsp;
2121 int error;
2122
2123 if (args->tsp != NULL) {
2124 error = linux_get_timespec(&ts, args->tsp);
2125 if (error != 0)
2126 return (error);
2127 tsp = &ts;
2128 } else
2129 tsp = NULL;
2130
2131 error = linux_common_pselect6(td, args->nfds, args->readfds,
2132 args->writefds, args->exceptfds, tsp, args->sig);
2133
2134 if (args->tsp != NULL)
2135 linux_put_timespec(&ts, args->tsp);
2136 return (error);
2137 }
2138
2139 static int
linux_common_pselect6(struct thread * td,l_int nfds,l_fd_set * readfds,l_fd_set * writefds,l_fd_set * exceptfds,struct timespec * tsp,l_uintptr_t * sig)2140 linux_common_pselect6(struct thread *td, l_int nfds, l_fd_set *readfds,
2141 l_fd_set *writefds, l_fd_set *exceptfds, struct timespec *tsp,
2142 l_uintptr_t *sig)
2143 {
2144 struct timeval utv, tv0, tv1, *tvp;
2145 struct l_pselect6arg lpse6;
2146 sigset_t *ssp;
2147 sigset_t ss;
2148 int error;
2149
2150 ssp = NULL;
2151 if (sig != NULL) {
2152 error = copyin(sig, &lpse6, sizeof(lpse6));
2153 if (error != 0)
2154 return (error);
2155 error = linux_copyin_sigset(td, PTRIN(lpse6.ss),
2156 lpse6.ss_len, &ss, &ssp);
2157 if (error != 0)
2158 return (error);
2159 } else
2160 ssp = NULL;
2161
2162 /*
2163 * Currently glibc changes nanosecond number to microsecond.
2164 * This mean losing precision but for now it is hardly seen.
2165 */
2166 if (tsp != NULL) {
2167 TIMESPEC_TO_TIMEVAL(&utv, tsp);
2168 if (itimerfix(&utv))
2169 return (EINVAL);
2170
2171 microtime(&tv0);
2172 tvp = &utv;
2173 } else
2174 tvp = NULL;
2175
2176 error = kern_pselect(td, nfds, readfds, writefds,
2177 exceptfds, tvp, ssp, LINUX_NFDBITS);
2178
2179 if (tsp != NULL) {
2180 /*
2181 * Compute how much time was left of the timeout,
2182 * by subtracting the current time and the time
2183 * before we started the call, and subtracting
2184 * that result from the user-supplied value.
2185 */
2186 microtime(&tv1);
2187 timevalsub(&tv1, &tv0);
2188 timevalsub(&utv, &tv1);
2189 if (utv.tv_sec < 0)
2190 timevalclear(&utv);
2191 TIMEVAL_TO_TIMESPEC(&utv, tsp);
2192 }
2193 return (error);
2194 }
2195
2196 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2197 int
linux_pselect6_time64(struct thread * td,struct linux_pselect6_time64_args * args)2198 linux_pselect6_time64(struct thread *td,
2199 struct linux_pselect6_time64_args *args)
2200 {
2201 struct timespec ts, *tsp;
2202 int error;
2203
2204 if (args->tsp != NULL) {
2205 error = linux_get_timespec64(&ts, args->tsp);
2206 if (error != 0)
2207 return (error);
2208 tsp = &ts;
2209 } else
2210 tsp = NULL;
2211
2212 error = linux_common_pselect6(td, args->nfds, args->readfds,
2213 args->writefds, args->exceptfds, tsp, args->sig);
2214
2215 if (args->tsp != NULL)
2216 linux_put_timespec64(&ts, args->tsp);
2217 return (error);
2218 }
2219 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
2220
2221 int
linux_ppoll(struct thread * td,struct linux_ppoll_args * args)2222 linux_ppoll(struct thread *td, struct linux_ppoll_args *args)
2223 {
2224 struct timespec uts, *tsp;
2225 int error;
2226
2227 if (args->tsp != NULL) {
2228 error = linux_get_timespec(&uts, args->tsp);
2229 if (error != 0)
2230 return (error);
2231 tsp = &uts;
2232 } else
2233 tsp = NULL;
2234
2235 error = linux_common_ppoll(td, args->fds, args->nfds, tsp,
2236 args->sset, args->ssize);
2237 if (error == 0 && args->tsp != NULL)
2238 error = linux_put_timespec(&uts, args->tsp);
2239 return (error);
2240 }
2241
2242 static int
linux_common_ppoll(struct thread * td,struct pollfd * fds,uint32_t nfds,struct timespec * tsp,l_sigset_t * sset,l_size_t ssize)2243 linux_common_ppoll(struct thread *td, struct pollfd *fds, uint32_t nfds,
2244 struct timespec *tsp, l_sigset_t *sset, l_size_t ssize)
2245 {
2246 struct timespec ts0, ts1;
2247 struct pollfd stackfds[32];
2248 struct pollfd *kfds;
2249 sigset_t *ssp;
2250 sigset_t ss;
2251 int error;
2252
2253 if (kern_poll_maxfds(nfds))
2254 return (EINVAL);
2255 if (sset != NULL) {
2256 error = linux_copyin_sigset(td, sset, ssize, &ss, &ssp);
2257 if (error != 0)
2258 return (error);
2259 } else
2260 ssp = NULL;
2261 if (tsp != NULL)
2262 nanotime(&ts0);
2263
2264 if (nfds > nitems(stackfds))
2265 kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK);
2266 else
2267 kfds = stackfds;
2268 error = linux_pollin(td, kfds, fds, nfds);
2269 if (error != 0)
2270 goto out;
2271
2272 error = kern_poll_kfds(td, kfds, nfds, tsp, ssp);
2273 if (error == 0)
2274 error = linux_pollout(td, kfds, fds, nfds);
2275
2276 if (error == 0 && tsp != NULL) {
2277 if (td->td_retval[0]) {
2278 nanotime(&ts1);
2279 timespecsub(&ts1, &ts0, &ts1);
2280 timespecsub(tsp, &ts1, tsp);
2281 if (tsp->tv_sec < 0)
2282 timespecclear(tsp);
2283 } else
2284 timespecclear(tsp);
2285 }
2286
2287 out:
2288 if (nfds > nitems(stackfds))
2289 free(kfds, M_TEMP);
2290 return (error);
2291 }
2292
2293 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2294 int
linux_ppoll_time64(struct thread * td,struct linux_ppoll_time64_args * args)2295 linux_ppoll_time64(struct thread *td, struct linux_ppoll_time64_args *args)
2296 {
2297 struct timespec uts, *tsp;
2298 int error;
2299
2300 if (args->tsp != NULL) {
2301 error = linux_get_timespec64(&uts, args->tsp);
2302 if (error != 0)
2303 return (error);
2304 tsp = &uts;
2305 } else
2306 tsp = NULL;
2307 error = linux_common_ppoll(td, args->fds, args->nfds, tsp,
2308 args->sset, args->ssize);
2309 if (error == 0 && args->tsp != NULL)
2310 error = linux_put_timespec64(&uts, args->tsp);
2311 return (error);
2312 }
2313 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
2314
2315 static int
linux_pollin(struct thread * td,struct pollfd * fds,struct pollfd * ufds,u_int nfd)2316 linux_pollin(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
2317 {
2318 int error;
2319 u_int i;
2320
2321 error = copyin(ufds, fds, nfd * sizeof(*fds));
2322 if (error != 0)
2323 return (error);
2324
2325 for (i = 0; i < nfd; i++) {
2326 if (fds->events != 0)
2327 linux_to_bsd_poll_events(td, fds->fd,
2328 fds->events, &fds->events);
2329 fds++;
2330 }
2331 return (0);
2332 }
2333
2334 static int
linux_pollout(struct thread * td,struct pollfd * fds,struct pollfd * ufds,u_int nfd)2335 linux_pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
2336 {
2337 int error = 0;
2338 u_int i, n = 0;
2339
2340 for (i = 0; i < nfd; i++) {
2341 if (fds->revents != 0) {
2342 bsd_to_linux_poll_events(fds->revents,
2343 &fds->revents);
2344 n++;
2345 }
2346 error = copyout(&fds->revents, &ufds->revents,
2347 sizeof(ufds->revents));
2348 if (error)
2349 return (error);
2350 fds++;
2351 ufds++;
2352 }
2353 td->td_retval[0] = n;
2354 return (0);
2355 }
2356
2357 static int
linux_sched_rr_get_interval_common(struct thread * td,pid_t pid,struct timespec * ts)2358 linux_sched_rr_get_interval_common(struct thread *td, pid_t pid,
2359 struct timespec *ts)
2360 {
2361 struct thread *tdt;
2362 int error;
2363
2364 /*
2365 * According to man in case the invalid pid specified
2366 * EINVAL should be returned.
2367 */
2368 if (pid < 0)
2369 return (EINVAL);
2370
2371 tdt = linux_tdfind(td, pid, -1);
2372 if (tdt == NULL)
2373 return (ESRCH);
2374
2375 error = kern_sched_rr_get_interval_td(td, tdt, ts);
2376 PROC_UNLOCK(tdt->td_proc);
2377 return (error);
2378 }
2379
2380 int
linux_sched_rr_get_interval(struct thread * td,struct linux_sched_rr_get_interval_args * uap)2381 linux_sched_rr_get_interval(struct thread *td,
2382 struct linux_sched_rr_get_interval_args *uap)
2383 {
2384 struct timespec ts;
2385 int error;
2386
2387 error = linux_sched_rr_get_interval_common(td, uap->pid, &ts);
2388 if (error != 0)
2389 return (error);
2390 return (linux_put_timespec(&ts, uap->interval));
2391 }
2392
2393 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2394 int
linux_sched_rr_get_interval_time64(struct thread * td,struct linux_sched_rr_get_interval_time64_args * uap)2395 linux_sched_rr_get_interval_time64(struct thread *td,
2396 struct linux_sched_rr_get_interval_time64_args *uap)
2397 {
2398 struct timespec ts;
2399 int error;
2400
2401 error = linux_sched_rr_get_interval_common(td, uap->pid, &ts);
2402 if (error != 0)
2403 return (error);
2404 return (linux_put_timespec64(&ts, uap->interval));
2405 }
2406 #endif
2407
2408 /*
2409 * In case when the Linux thread is the initial thread in
2410 * the thread group thread id is equal to the process id.
2411 * Glibc depends on this magic (assert in pthread_getattr_np.c).
2412 */
2413 struct thread *
linux_tdfind(struct thread * td,lwpid_t tid,pid_t pid)2414 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid)
2415 {
2416 struct linux_emuldata *em;
2417 struct thread *tdt;
2418 struct proc *p;
2419
2420 tdt = NULL;
2421 if (tid == 0 || tid == td->td_tid) {
2422 if (pid != -1 && td->td_proc->p_pid != pid)
2423 return (NULL);
2424 PROC_LOCK(td->td_proc);
2425 return (td);
2426 } else if (tid > PID_MAX)
2427 return (tdfind(tid, pid));
2428
2429 /*
2430 * Initial thread where the tid equal to the pid.
2431 */
2432 p = pfind(tid);
2433 if (p != NULL) {
2434 if (SV_PROC_ABI(p) != SV_ABI_LINUX ||
2435 (pid != -1 && tid != pid)) {
2436 /*
2437 * p is not a Linuxulator process.
2438 */
2439 PROC_UNLOCK(p);
2440 return (NULL);
2441 }
2442 FOREACH_THREAD_IN_PROC(p, tdt) {
2443 em = em_find(tdt);
2444 if (tid == em->em_tid)
2445 return (tdt);
2446 }
2447 PROC_UNLOCK(p);
2448 }
2449 return (NULL);
2450 }
2451
2452 void
linux_to_bsd_waitopts(int options,int * bsdopts)2453 linux_to_bsd_waitopts(int options, int *bsdopts)
2454 {
2455
2456 if (options & LINUX_WNOHANG)
2457 *bsdopts |= WNOHANG;
2458 if (options & LINUX_WUNTRACED)
2459 *bsdopts |= WUNTRACED;
2460 if (options & LINUX_WEXITED)
2461 *bsdopts |= WEXITED;
2462 if (options & LINUX_WCONTINUED)
2463 *bsdopts |= WCONTINUED;
2464 if (options & LINUX_WNOWAIT)
2465 *bsdopts |= WNOWAIT;
2466
2467 if (options & __WCLONE)
2468 *bsdopts |= WLINUXCLONE;
2469 }
2470
2471 int
linux_getrandom(struct thread * td,struct linux_getrandom_args * args)2472 linux_getrandom(struct thread *td, struct linux_getrandom_args *args)
2473 {
2474 struct uio uio;
2475 struct iovec iov;
2476 int error;
2477
2478 if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM))
2479 return (EINVAL);
2480 if (args->count > INT_MAX)
2481 args->count = INT_MAX;
2482
2483 iov.iov_base = args->buf;
2484 iov.iov_len = args->count;
2485
2486 uio.uio_iov = &iov;
2487 uio.uio_iovcnt = 1;
2488 uio.uio_resid = iov.iov_len;
2489 uio.uio_segflg = UIO_USERSPACE;
2490 uio.uio_rw = UIO_READ;
2491 uio.uio_td = td;
2492
2493 error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK);
2494 if (error == 0)
2495 td->td_retval[0] = args->count - uio.uio_resid;
2496 return (error);
2497 }
2498
2499 int
linux_mincore(struct thread * td,struct linux_mincore_args * args)2500 linux_mincore(struct thread *td, struct linux_mincore_args *args)
2501 {
2502
2503 /* Needs to be page-aligned */
2504 if (args->start & PAGE_MASK)
2505 return (EINVAL);
2506 return (kern_mincore(td, args->start, args->len, args->vec));
2507 }
2508
2509 #define SYSLOG_TAG "<6>"
2510
2511 int
linux_syslog(struct thread * td,struct linux_syslog_args * args)2512 linux_syslog(struct thread *td, struct linux_syslog_args *args)
2513 {
2514 char buf[128], *src, *dst;
2515 u_int seq;
2516 int buflen, error;
2517
2518 if (args->type != LINUX_SYSLOG_ACTION_READ_ALL) {
2519 linux_msg(td, "syslog unsupported type 0x%x", args->type);
2520 return (EINVAL);
2521 }
2522
2523 if (args->len < 6) {
2524 td->td_retval[0] = 0;
2525 return (0);
2526 }
2527
2528 error = priv_check(td, PRIV_MSGBUF);
2529 if (error)
2530 return (error);
2531
2532 mtx_lock(&msgbuf_lock);
2533 msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
2534 mtx_unlock(&msgbuf_lock);
2535
2536 dst = args->buf;
2537 error = copyout(&SYSLOG_TAG, dst, sizeof(SYSLOG_TAG));
2538 /* The -1 is to skip the trailing '\0'. */
2539 dst += sizeof(SYSLOG_TAG) - 1;
2540
2541 while (error == 0) {
2542 mtx_lock(&msgbuf_lock);
2543 buflen = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq);
2544 mtx_unlock(&msgbuf_lock);
2545
2546 if (buflen == 0)
2547 break;
2548
2549 for (src = buf; src < buf + buflen && error == 0; src++) {
2550 if (*src == '\0')
2551 continue;
2552
2553 if (dst >= args->buf + args->len)
2554 goto out;
2555
2556 error = copyout(src, dst, 1);
2557 dst++;
2558
2559 if (*src == '\n' && *(src + 1) != '<' &&
2560 dst + sizeof(SYSLOG_TAG) < args->buf + args->len) {
2561 error = copyout(&SYSLOG_TAG,
2562 dst, sizeof(SYSLOG_TAG));
2563 dst += sizeof(SYSLOG_TAG) - 1;
2564 }
2565 }
2566 }
2567 out:
2568 td->td_retval[0] = dst - args->buf;
2569 return (error);
2570 }
2571
2572 int
linux_getcpu(struct thread * td,struct linux_getcpu_args * args)2573 linux_getcpu(struct thread *td, struct linux_getcpu_args *args)
2574 {
2575 int cpu, error, node;
2576
2577 cpu = td->td_oncpu; /* Make sure it doesn't change during copyout(9) */
2578 error = 0;
2579 node = cpuid_to_pcpu[cpu]->pc_domain;
2580
2581 if (args->cpu != NULL)
2582 error = copyout(&cpu, args->cpu, sizeof(l_int));
2583 if (args->node != NULL)
2584 error = copyout(&node, args->node, sizeof(l_int));
2585 return (error);
2586 }
2587
2588 #if defined(__i386__) || defined(__amd64__)
2589 int
linux_poll(struct thread * td,struct linux_poll_args * args)2590 linux_poll(struct thread *td, struct linux_poll_args *args)
2591 {
2592 struct timespec ts, *tsp;
2593
2594 if (args->timeout != INFTIM) {
2595 if (args->timeout < 0)
2596 return (EINVAL);
2597 ts.tv_sec = args->timeout / 1000;
2598 ts.tv_nsec = (args->timeout % 1000) * 1000000;
2599 tsp = &ts;
2600 } else
2601 tsp = NULL;
2602
2603 return (linux_common_ppoll(td, args->fds, args->nfds,
2604 tsp, NULL, 0));
2605 }
2606 #endif /* __i386__ || __amd64__ */
2607
2608 int
linux_seccomp(struct thread * td,struct linux_seccomp_args * args)2609 linux_seccomp(struct thread *td, struct linux_seccomp_args *args)
2610 {
2611
2612 switch (args->op) {
2613 case LINUX_SECCOMP_GET_ACTION_AVAIL:
2614 return (EOPNOTSUPP);
2615 default:
2616 /*
2617 * Ignore unknown operations, just like Linux kernel built
2618 * without CONFIG_SECCOMP.
2619 */
2620 return (EINVAL);
2621 }
2622 }
2623
2624 #ifndef COMPAT_LINUX32
2625 int
linux_execve(struct thread * td,struct linux_execve_args * args)2626 linux_execve(struct thread *td, struct linux_execve_args *args)
2627 {
2628 struct image_args eargs;
2629 char *path;
2630 int error;
2631
2632 LINUX_CTR(execve);
2633
2634 if (!LUSECONVPATH(td)) {
2635 error = exec_copyin_args(&eargs, args->path, UIO_USERSPACE,
2636 args->argp, args->envp);
2637 } else {
2638 LCONVPATHEXIST(args->path, &path);
2639 error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp,
2640 args->envp);
2641 LFREEPATH(path);
2642 }
2643 if (error == 0)
2644 error = linux_common_execve(td, &eargs);
2645 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
2646 return (error);
2647 }
2648 #endif
2649