summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/brand/lx/syscall/lx_poll.c
blob: e54130aff17bfd534b09abe968a061af25c3f3dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright (c) 2017, Joyent, Inc.
 */

#include <sys/types.h>
#include <sys/systm.h>
#include <sys/thread.h>
#include <sys/proc.h>
#include <sys/zone.h>
#include <sys/brand.h>
#include <sys/sunddi.h>
#include <sys/lx_brand.h>
#include <sys/lx_types.h>
#include <sys/poll_impl.h>
#include <sys/schedctl.h>
#include <sys/lx_signal.h>

/*
 * Max number of FDs that can be given to poll() or select() before we return
 * EINVAL (the Linux man page documents this value as {OPEN_MAX}, and defaults
 * it to this value).
 */
int lx_poll_max_fds = 1048576;

/* From uts/common/syscall/poll.c */
extern int poll_copyin(pollstate_t *, pollfd_t *, nfds_t);
extern int poll_common(pollstate_t *, pollfd_t *, nfds_t, timespec_t *, int *);

/*
 * These events are identical between Linux and SunOS
 */
#define	LX_POLLIN	0x001
#define	LX_POLLPRI	0x002
#define	LX_POLLOUT	0x004
#define	LX_POLLERR	0x008
#define	LX_POLLHUP	0x010
#define	LX_POLLNVAL	0x020
#define	LX_POLLRDNORM	0x040
#define	LX_POLLRDBAND	0x080

#define	LX_POLL_COMMON_EVENTS (LX_POLLIN | LX_POLLPRI | LX_POLLOUT |	\
	LX_POLLERR | LX_POLLHUP | LX_POLLNVAL | LX_POLLRDNORM | LX_POLLRDBAND)

/*
 * These events differ between Linux and SunOS
 */
#define	LX_POLLWRNORM	0x0100
#define	LX_POLLWRBAND	0x0200
#define	LX_POLLRDHUP	0x2000


#define	LX_POLL_SUPPORTED_EVENTS	\
	(LX_POLL_COMMON_EVENTS | LX_POLLWRNORM | LX_POLLWRBAND | LX_POLLRDHUP)


static int
lx_poll_copyin(pollstate_t *ps, pollfd_t *fds, nfds_t nfds, short *oldevt)
{
	int i, error = 0;
	pollfd_t *pollfdp;

	if ((error = poll_copyin(ps, fds, nfds)) != 0) {
		return (error);
	}
	pollfdp = ps->ps_pollfd;

	/* Convert the Linux events bitmask into SunOS equivalent. */
	for (i = 0; i < nfds; i++) {
		short lx_events = pollfdp[i].events;
		short events;

		/*
		 * If the caller is polling for an unsupported event, we
		 * have to bail out.
		 */
		if (lx_events & ~LX_POLL_SUPPORTED_EVENTS) {
			return (ENOTSUP);
		}

		events = lx_events & LX_POLL_COMMON_EVENTS;
		if (lx_events & LX_POLLWRNORM)
			events |= POLLWRNORM;
		if (lx_events & LX_POLLWRBAND)
			events |= POLLWRBAND;
		if (lx_events & LX_POLLRDHUP)
			events |= POLLRDHUP;
		pollfdp[i].events = events;
		oldevt[i] = lx_events;
	}
	return (0);
}

static int
lx_poll_copyout(pollfd_t *pollfdp, pollfd_t *fds, nfds_t nfds, short *oldevt)
{
	int i;

	/*
	 * Convert SunOS revents bitmask into Linux equivalent and restore
	 * cached events field which was swizzled by lx_poll_copyin.
	 */
	for (i = 0; i < nfds; i++) {
		short revents = pollfdp[i].revents;
		short lx_revents = revents & LX_POLL_COMMON_EVENTS;
		short orig_events = oldevt[i];

		if (revents & POLLWRBAND)
			lx_revents |= LX_POLLWRBAND;
		if (revents & POLLRDHUP)
			lx_revents |= LX_POLLRDHUP;
		/*
		 * Because POLLOUT and POLLWRNORM are native defined as the
		 * same value, care must be taken when translating them to
		 * Linux where they differ.
		 */
		if (revents & POLLOUT) {
			if ((orig_events & LX_POLLOUT) == 0)
				lx_revents &= ~LX_POLLOUT;
			if (orig_events & LX_POLLWRNORM)
				lx_revents |= LX_POLLWRNORM;
		}

		pollfdp[i].revents = lx_revents;
		pollfdp[i].events = orig_events;
	}

	if (copyout(pollfdp, fds, sizeof (pollfd_t) * nfds) != 0)
		return (EFAULT);

	return (0);
}

static long
lx_poll_common(pollfd_t *fds, nfds_t nfds, timespec_t *tsp, k_sigset_t *ksetp)
{
	kthread_t *t = curthread;
	klwp_t *lwp = ttolwp(t);
	proc_t *p = ttoproc(t);
	pollstate_t *ps = NULL;
	pollfd_t *pollfdp = NULL;
	short *oldevt = NULL;
	int error = 0, fdcnt = 0;

	/*
	 * Reset our signal mask, if requested.
	 */
	if (ksetp != NULL) {
		mutex_enter(&p->p_lock);
		schedctl_finish_sigblock(t);
		lwp->lwp_sigoldmask = t->t_hold;
		t->t_hold = *ksetp;
		t->t_flag |= T_TOMASK;
		/*
		 * Call cv_reltimedwait_sig() just to check for signals.
		 * We will return immediately with either 0 or -1.
		 */
		if (!cv_reltimedwait_sig(&t->t_delay_cv, &p->p_lock, 0,
		    TR_CLOCK_TICK)) {
			mutex_exit(&p->p_lock);
			error = EINTR;
			goto pollout;
		}
		mutex_exit(&p->p_lock);
	}

	/*
	 * Initialize pollstate and copy in pollfd data if present.
	 */
	if (nfds != 0) {
		/*
		 * Cap the number of FDs they can give us so we don't go
		 * allocating a huge chunk of memory. Note that this is *not*
		 * the RLIMIT_NOFILE rctl.
		 */
		if (nfds > lx_poll_max_fds) {
			error = EINVAL;
			goto pollout;
		}

		/*
		 * Need to allocate memory for pollstate before anything
		 * because the mutex and cv are created in this space
		 */
		ps = pollstate_create();
		if (ps->ps_pcache == NULL)
			ps->ps_pcache = pcache_alloc();

		/*
		 * Certain event types which are distinct on Linux are aliased
		 * against each other on illumos.  In order properly translate
		 * back into the Linux format, the original events of interest
		 * are stored in 'oldevt' for use during lx_poll_copyout.
		 */
		oldevt = kmem_alloc(nfds * sizeof (short), KM_SLEEP);
		if ((error = lx_poll_copyin(ps, fds, nfds, oldevt)) != 0)
			goto pollout;
		pollfdp = ps->ps_pollfd;

		/*
		 * The Linux poll(2) implicitly polls for POLLERR and POLLHUP
		 * in addition to any other events specified for the file
		 * descriptors in question.  It does not modify pollfd_t`events
		 * to reflect that fact when performing a later copyout.
		 */
		ps->ps_implicit_ev = POLLERR | POLLHUP;
	}

	/*
	 * Perform the actual poll.
	 */
	error = poll_common(ps, fds, nfds, tsp, &fdcnt);

	/*
	 * Clear implicit event interest, if needed.
	 */
	if (ps != NULL) {
		ps->ps_implicit_ev = 0;
	}


pollout:
	/*
	 * If we changed the signal mask but we received no signal then restore
	 * the signal mask.  Otherwise psig() will deal with the signal mask.
	 */
	if (ksetp != NULL) {
		mutex_enter(&p->p_lock);
		if (lwp->lwp_cursig == 0) {
			t->t_hold = lwp->lwp_sigoldmask;
			t->t_flag &= ~T_TOMASK;
		}
		mutex_exit(&p->p_lock);
	}

	/*
	 * Copy out the events and return the fdcnt to the user.
	 */
	if (nfds != 0 && error == 0) {
		error = lx_poll_copyout(pollfdp, fds, nfds, oldevt);
	}
	if (oldevt != NULL) {
		kmem_free(oldevt, nfds * sizeof (short));
	}
	if (error) {
		return (set_errno(error));
	}
	return (fdcnt);
}

long
lx_poll(pollfd_t *fds, nfds_t nfds, int timeout)
{
	timespec_t ts, *tsp = NULL;

	if (timeout >= 0) {
		ts.tv_sec = timeout / MILLISEC;
		ts.tv_nsec = (timeout % MILLISEC) * MICROSEC;
		tsp = &ts;
	}

	return (lx_poll_common(fds, nfds, tsp, NULL));
}

long
lx_ppoll(pollfd_t *fds, nfds_t nfds, timespec_t *timeoutp, lx_sigset_t *setp)
{
	timespec_t ts, *tsp = NULL;
	k_sigset_t kset, *ksetp = NULL;

	/*
	 * Copy in timeout and sigmask.
	 */
	if (timeoutp != NULL) {
		if (get_udatamodel() == DATAMODEL_NATIVE) {
			if (copyin(timeoutp, &ts, sizeof (ts)))
				return (set_errno(EFAULT));
		} else {
			timespec32_t ts32;

			if (copyin(timeoutp, &ts32, sizeof (ts32)))
				return (set_errno(EFAULT));
			TIMESPEC32_TO_TIMESPEC(&ts, &ts32)
		}

		if (itimerspecfix(&ts))
			return (set_errno(EINVAL));
		tsp = &ts;
	}
	if (setp != NULL) {
		lx_sigset_t lset;

		if (copyin(setp, &lset, sizeof (lset)))
			return (set_errno(EFAULT));
		lx_ltos_sigset(&lset, &kset);
		ksetp = &kset;
	}

	return (lx_poll_common(fds, nfds, tsp, ksetp));
}

typedef struct lx_select_buf_s {
	long		*lsb_rfds;
	long		*lsb_wfds;
	long		*lsb_efds;
	unsigned int	lsb_size;
} lx_select_buf_t;

/*
 * Size (in bytes) of buffer appropriate for fd_set copyin/copyout.
 * Linux uses buffers of 'long' to accomplish this.
 */
#define	LX_FD_SET_BYTES		(sizeof (long))
#define	LX_FD_SET_BITS		(8 * LX_FD_SET_BYTES)
#define	LX_FD_SET_SIZE(nfds)	\
	((((nfds) + (LX_FD_SET_BITS - 1)) / LX_FD_SET_BITS) * LX_FD_SET_BYTES)

static int
lx_select_copyin(pollstate_t *ps, lx_select_buf_t *sbuf, int nfds,
    long *rfds, long *wfds, long *efds)
{
	int n;
	long *in, *out, *ex;
	long absent = 0;
	pollfd_t *pfd;
	nfds_t old_nfds;

	/*
	 * Just like pollsys and lx_poll, attempt to reuse ps_pollfd if it is
	 * appropriately sized.  See poll_copyin for more detail.
	 */
	old_nfds = ps->ps_nfds;
	if (nfds != old_nfds) {
		kmem_free(ps->ps_pollfd, old_nfds * sizeof (pollfd_t));
		pfd = kmem_alloc(nfds * sizeof (pollfd_t), KM_SLEEP);
		ps->ps_pollfd = pfd;
		ps->ps_nfds = nfds;
	} else {
		pfd = ps->ps_pollfd;
	}

	if (rfds != NULL) {
		if (copyin(rfds, sbuf->lsb_rfds, sbuf->lsb_size) != 0) {
			return (EFAULT);
		}
	}
	if (wfds != NULL) {
		if (copyin(wfds, sbuf->lsb_wfds, sbuf->lsb_size) != 0) {
			return (EFAULT);
		}
	}
	if (efds != NULL) {
		if (copyin(efds, sbuf->lsb_efds, sbuf->lsb_size) != 0) {
			return (EFAULT);
		}
	}

	/*
	 * For each fd, if any bits are set convert them into the appropriate
	 * pollfd struct. (Derived from libc's select logic)
	 */
	in = (rfds != NULL) ? sbuf->lsb_rfds : &absent;
	out = (wfds != NULL) ? sbuf->lsb_wfds : &absent;
	ex = (efds != NULL) ? sbuf->lsb_efds : &absent;
	for (n = 0; n < nfds; n += LX_FD_SET_BITS) {
		unsigned long b, m, j;

		b = (unsigned long)(*in | *out | *ex);
		m = 1;
		for (j = 0; j < LX_FD_SET_BITS; j++) {
			int fd = n + j;

			if (fd >= nfds)
				return (0);
			pfd->events = 0;
			if (b & 1) {
				pfd->fd = fd;
				if (*in & m)
					pfd->events |= POLLRDNORM;
				if (*out & m)
					pfd->events |= POLLWRNORM;
				if (*ex & m)
					pfd->events |= POLLRDBAND;
			} else {
				pfd->fd = -1;
			}
			pfd++;
			b >>= 1;
			m <<= 1;
		}

		if (rfds != NULL)
			in++;
		if (wfds != NULL)
			out++;
		if (efds != NULL)
			ex++;
	}
	return (0);
}

static int
lx_select_copyout(pollfd_t *pollfdp, lx_select_buf_t *sbuf, int nfds,
    long *rfds, long *wfds, long *efds, int *fdcnt)
{
	int n;
	pollfd_t *pfd;
	long rv = 0;

	/*
	 * If poll did not find any fds of interest, we can just zero out the
	 * fd_set fields for copyout.
	 */
	if (*fdcnt == 0) {
		if (rfds != NULL) {
			bzero(sbuf->lsb_rfds, sbuf->lsb_size);
		}
		if (wfds != NULL) {
			bzero(sbuf->lsb_wfds, sbuf->lsb_size);
		}
		if (efds != NULL) {
			bzero(sbuf->lsb_efds, sbuf->lsb_size);
		}
		goto copyout;
	}

	/*
	 * For each fd, if any bits are set convert them into the appropriate
	 * pollfd struct. (Derived from libc's select logic)
	 */
	pfd = pollfdp;
	for (n = 0; n < nfds; n += LX_FD_SET_BITS) {
		unsigned long m, j;
		long in = 0, out = 0, ex = 0;

		m = 1;
		for (j = 0; j < LX_FD_SET_BITS; j++) {
			if ((n + j) >= nfds)
				break;
			if (pfd->revents != 0) {
				if (pfd->revents & POLLNVAL) {
					return (EBADF);
				}
				if (pfd->revents & POLLRDNORM) {
					in |= m;
					rv++;
				}
				if (pfd->revents & POLLWRNORM) {
					out |= m;
					rv++;
				}
				if (pfd->revents & POLLRDBAND) {
					ex |= m;
					rv++;
				}
				/*
				 * Only set this bit on return if we asked
				 * about input conditions.
				 */
				if ((pfd->revents & (POLLHUP|POLLERR)) &&
				    (pfd->events & POLLRDNORM)) {
					if ((in & m) == 0) {
						/* wasn't already set */
						rv++;
					}
					in |= m;
				}
				/*
				 * Only set this bit on return if we asked
				 * about output conditions.
				 */
				if ((pfd->revents & (POLLHUP|POLLERR)) &&
				    (pfd->events & POLLWRNORM)) {
					if ((out & m) == 0) {
						/* wasn't already set */
						rv++;
					}
					out |= m;
				}
				/*
				 * Only set this bit on return if we asked
				 * about output conditions.
				 */
				if ((pfd->revents & (POLLHUP|POLLERR)) &&
				    (pfd->events & POLLRDBAND)) {
					if ((ex & m) == 0) {
						/* wasn't already set */
						rv++;
					}
					ex |= m;
				}
			}
			m <<= 1;
			pfd++;
		}
		if (rfds != NULL)
			sbuf->lsb_rfds[n / LX_FD_SET_BITS] = in;
		if (wfds != NULL)
			sbuf->lsb_wfds[n / LX_FD_SET_BITS] = out;
		if (efds != NULL)
			sbuf->lsb_efds[n / LX_FD_SET_BITS] = ex;
	}

copyout:
	if (rfds != NULL) {
		if (copyout(sbuf->lsb_rfds, rfds, sbuf->lsb_size) != 0) {
			return (EFAULT);
		}
	}
	if (wfds != NULL) {
		if (copyout(sbuf->lsb_wfds, wfds, sbuf->lsb_size) != 0) {
			return (EFAULT);
		}
	}
	if (efds != NULL) {
		if (copyout(sbuf->lsb_efds, efds, sbuf->lsb_size) != 0) {
			return (EFAULT);
		}
	}
	*fdcnt = rv;
	return (0);
}


static long
lx_select_common(int nfds, long *rfds, long *wfds, long *efds,
    timespec_t *tsp, k_sigset_t *ksetp)
{
	kthread_t *t = curthread;
	klwp_t *lwp = ttolwp(t);
	proc_t *p = ttoproc(t);
	pollstate_t *ps = NULL;
	pollfd_t *pollfdp = NULL, *fake_fds = NULL;
	lx_select_buf_t sbuf = {0};
	int error = 0, fdcnt = 0;

	if (nfds < 0) {
		return (set_errno(EINVAL));
	}

	/*
	 * Reset our signal mask, if requested.
	 */
	if (ksetp != NULL) {
		mutex_enter(&p->p_lock);
		schedctl_finish_sigblock(t);
		lwp->lwp_sigoldmask = t->t_hold;
		t->t_hold = *ksetp;
		t->t_flag |= T_TOMASK;
		/*
		 * Call cv_reltimedwait_sig() just to check for signals.
		 * We will return immediately with either 0 or -1.
		 */
		if (!cv_reltimedwait_sig(&t->t_delay_cv, &p->p_lock, 0,
		    TR_CLOCK_TICK)) {
			mutex_exit(&p->p_lock);
			error = EINTR;
			goto out;
		}
		mutex_exit(&p->p_lock);
	}

	/*
	 * Because poll caching uses the userspace pollfd_t pointer to verify
	 * cache reuse validity, a simulated value must be supplied when
	 * emulating Linux select(2).  The first non-NULL pointer from
	 * rfds/wfds/efds is used for this purpose.
	 */
	if (rfds != NULL) {
		fake_fds = (pollfd_t *)rfds;
	} else if (wfds != NULL) {
		fake_fds = (pollfd_t *)wfds;
	} else if (efds != NULL) {
		fake_fds = (pollfd_t *)efds;
	} else {
		/*
		 * A non-zero nfds was supplied but all three fd_set pointers
		 * were null.  Fall back to doing a simple timeout.
		 */
		nfds = 0;
	}

	/*
	 * Initialize pollstate and copy in pollfd data if present.
	 */
	if (nfds != 0) {
		/*
		 * Cap the number of FDs they can give us so we don't go
		 * allocating a huge chunk of memory. Note that this is *not*
		 * the RLIMIT_NOFILE rctl.
		 */
		if (nfds > lx_poll_max_fds) {
			error = EINVAL;
			goto out;
		}

		/*
		 * Need to allocate memory for pollstate before anything
		 * because the mutex and cv are created in this space
		 */
		ps = pollstate_create();
		if (ps->ps_pcache == NULL)
			ps->ps_pcache = pcache_alloc();

		sbuf.lsb_size = LX_FD_SET_SIZE(nfds);
		if (rfds != NULL)
			sbuf.lsb_rfds = kmem_alloc(sbuf.lsb_size, KM_SLEEP);
		if (wfds != NULL)
			sbuf.lsb_wfds = kmem_alloc(sbuf.lsb_size, KM_SLEEP);
		if (efds != NULL)
			sbuf.lsb_efds = kmem_alloc(sbuf.lsb_size, KM_SLEEP);

		error = lx_select_copyin(ps, &sbuf, nfds, rfds, wfds, efds);
		if (error != 0) {
			goto out;
		}

		pollfdp = ps->ps_pollfd;
	}

	/*
	 * Perform the actual poll.
	 */
	error = poll_common(ps, fake_fds, (nfds_t)nfds, tsp, &fdcnt);

out:
	/*
	 * If we changed the signal mask but we received no signal then restore
	 * the signal mask.  Otherwise psig() will deal with the signal mask.
	 */
	if (ksetp != NULL) {
		mutex_enter(&p->p_lock);
		if (lwp->lwp_cursig == 0) {
			t->t_hold = lwp->lwp_sigoldmask;
			t->t_flag &= ~T_TOMASK;
		}
		mutex_exit(&p->p_lock);
	}

	/*
	 * Copy out the events and return the fdcnt to the user.
	 */
	if (error == 0 && nfds != 0) {
		error = lx_select_copyout(pollfdp, &sbuf, nfds, rfds, wfds,
		    efds, &fdcnt);
	}
	if (sbuf.lsb_size != 0) {
		if (sbuf.lsb_rfds != NULL)
			kmem_free(sbuf.lsb_rfds, sbuf.lsb_size);
		if (sbuf.lsb_wfds != NULL)
			kmem_free(sbuf.lsb_wfds, sbuf.lsb_size);
		if (sbuf.lsb_efds != NULL)
			kmem_free(sbuf.lsb_efds, sbuf.lsb_size);
	}
	if (error) {
		return (set_errno(error));
	}
	return (fdcnt);
}

long
lx_select(int nfds, long *rfds, long *wfds, long *efds,
    struct timeval *timeoutp)
{
	timespec_t ts, *tsp = NULL;

	if (timeoutp != NULL) {
		if (get_udatamodel() == DATAMODEL_NATIVE) {
			struct timeval tv;

			if (copyin(timeoutp, &tv, sizeof (tv)))
				return (set_errno(EFAULT));
			ts.tv_sec = tv.tv_sec;
			ts.tv_nsec = tv.tv_usec * (NANOSEC / MICROSEC);
		} else {
			struct timeval32 tv32;

			if (copyin(timeoutp, &tv32, sizeof (tv32)))
				return (set_errno(EFAULT));
			ts.tv_sec = tv32.tv_sec;
			ts.tv_nsec = tv32.tv_usec * (NANOSEC / MICROSEC);
		}

		if (itimerspecfix(&ts))
			return (set_errno(EINVAL));
		tsp = &ts;
	}

	return (lx_select_common(nfds, rfds, wfds, efds, tsp, NULL));
}


typedef struct {
	uintptr_t lpsa_addr;
	unsigned long lpsa_len;
} lx_pselect_sig_arg_t;

#if defined(_LP64)
typedef struct {
	caddr32_t lpsa_addr;
	uint32_t lpsa_len;
} lx_pselect_sig_arg32_t;
#endif /* defined(_LP64) */

long
lx_pselect(int nfds, long *rfds, long *wfds, long *efds,
    timespec_t *timeoutp, void *setp)
{
	timespec_t ts, *tsp = NULL;
	k_sigset_t kset, *ksetp = NULL;

	/*
	 * Copy in timeout and sigmask.
	 */
	if (timeoutp != NULL) {
		if (get_udatamodel() == DATAMODEL_NATIVE) {
			if (copyin(timeoutp, &ts, sizeof (ts)))
				return (set_errno(EFAULT));
		} else {
			timespec32_t ts32;

			if (copyin(timeoutp, &ts32, sizeof (ts32)))
				return (set_errno(EFAULT));
			TIMESPEC32_TO_TIMESPEC(&ts, &ts32)
		}

		if (itimerspecfix(&ts))
			return (set_errno(EINVAL));
		tsp = &ts;
	}
	if (setp != NULL) {
		lx_sigset_t lset, *sigaddr = NULL;

		if (get_udatamodel() == DATAMODEL_NATIVE) {
			lx_pselect_sig_arg_t lpsa;

			if (copyin(setp, &lpsa, sizeof (lpsa)) != 0)
				return (set_errno(EFAULT));
			/*
			 * Linux forces a size to be passed only so it can
			 * check that it's the size of a sigset_t.
			 */
			if (lpsa.lpsa_len != sizeof (lx_sigset_t))
				return (set_errno(EINVAL));

			sigaddr = (lx_sigset_t *)lpsa.lpsa_addr;
		}
#if defined(_LP64)
		else {
			lx_pselect_sig_arg32_t lpsa32;

			if (copyin(setp, &lpsa32, sizeof (lpsa32)) != 0)
				return (set_errno(EFAULT));
			/*
			 * Linux forces a size to be passed only so it can
			 * check that it's the size of a sigset_t.
			 */
			if (lpsa32.lpsa_len != sizeof (lx_sigset_t))
				return (set_errno(EINVAL));

			sigaddr = (lx_sigset_t *)(uint64_t)lpsa32.lpsa_addr;
		}
#endif /* defined(_LP64) */

		/* This is where we check if the sigset is *really* NULL. */
		if (sigaddr != NULL) {
			if (copyin(sigaddr, &lset, sizeof (lset)) != 0)
				return (set_errno(EFAULT));

			lx_ltos_sigset(&lset, &kset);
			ksetp = &kset;
		}
	}

	return (lx_select_common(nfds, rfds, wfds, efds, tsp, ksetp));
}