forked from libos-nuse/linux-libos-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuse-glue.c
1179 lines (1019 loc) · 26.1 KB
/
nuse-glue.c
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* system calls glue code for NUSE
* Copyright (c) 2015 Hajime Tazaki
*
* Author: Hajime Tazaki <[email protected]>
*
* Note: some of the code is picked from rumpkernel, written by Antti Kantee.
*/
#include <unistd.h>
#include <linux/types.h>
#include <stdio.h>
#include <sys/types.h>
#define __USE_GNU
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/epoll.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <poll.h>
#include <time.h>
#include <pthread.h>
#include "sim-assert.h"
#include "nuse-hostcalls.h"
#include "nuse.h"
#include "nuse-libc.h"
#include "sim-init.h"
#include "sim.h"
extern struct SimExported *g_exported;
struct SimSocket;
#define weak_alias(name, aliasname) \
extern __typeof (name) aliasname __attribute__ ((weak, alias (# name)))
#define MSEC_PER_SEC 1000L
#define NSEC_PER_MSEC 1000000L
#define NSEC_PER_SEC 1000000000L
static inline __s64 timespec_to_ns(const struct timespec *ts)
{
return ((__s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
}
/* FIXME: need to be configurable */
struct nuse_fd nuse_fd_table[1024];
/* epoll relates */
struct epoll_fd {
struct epoll_fd *next;
struct epoll_event *ev;
int fd;
};
int nuse_socket(int domain, int type, int protocol)
{
struct SimSocket *kernel_socket;
int ret, real_fd;
if (domain == AF_UNIX) {
if (!host_socket) nuse_hostcall_init();
real_fd = host_socket (domain, type, protocol);
if (real_fd == -1)
return real_fd;
nuse_fd_table[real_fd].real_fd = real_fd;
nuse_fd_table[real_fd].nuse_sock = NULL;
return real_fd;
}
ret = g_exported->sock_socket(domain, type, protocol, &kernel_socket);
if (ret < 0)
errno = -ret;
real_fd = host_open("/", O_RDONLY, 0);
nuse_fd_table[real_fd].nuse_sock = malloc(sizeof(struct nuse_socket));
memset(nuse_fd_table[real_fd].nuse_sock, 0, sizeof(struct nuse_socket));
nuse_fd_table[real_fd].nuse_sock->kern_sock = kernel_socket;
nuse_fd_table[real_fd].nuse_sock->refcnt++;
nuse_fd_table[real_fd].real_fd = real_fd;
return real_fd;
}
weak_alias(nuse_socket, socket);
int nuse_close(int fd)
{
int ret = 0;
if (!nuse_fd_table[fd].nuse_sock) {
if (nuse_fd_table[fd].epoll_fd > 0) {
free(nuse_fd_table[fd].epoll_fd);
nuse_fd_table[fd].epoll_fd = NULL;
return 0;
} else if (nuse_fd_table[fd].real_fd > 0) {
if (!host_close) nuse_hostcall_init();
return host_close(nuse_fd_table[fd].real_fd);
}
return EBADF;
}
if (--nuse_fd_table[fd].nuse_sock->refcnt > 0) {
goto end;
}
ret = g_exported->sock_close(nuse_fd_table[fd].nuse_sock->kern_sock);
if (ret < 0)
errno = -ret;
end:
nuse_fd_table[fd].nuse_sock = 0;
host_close(nuse_fd_table[fd].real_fd);
return ret;
}
weak_alias(nuse_close, close);
ssize_t nuse_recvmsg(int fd, struct msghdr *msghdr, int flags)
{
struct SimSocket *kernel_socket = nuse_fd_table[fd].nuse_sock->kern_sock;
ssize_t ret;
if (nuse_fd_table[fd].nuse_sock->flags & O_NONBLOCK)
flags |= MSG_DONTWAIT;
ret = g_exported->sock_recvmsg(kernel_socket, msghdr, flags);
if (ret < 0)
errno = -ret;
return ret;
}
weak_alias(nuse_recvmsg, recvmsg);
/* XXX: timeout is not implemented. */
int nuse_recvmmsg(int fd, struct mmsghdr *msgvec, unsigned int vlen,
int flags, const struct timespec *timeout)
{
int err, datagrams;
struct mmsghdr *entry;
datagrams = 0;
entry = msgvec;
err = 0;
while (datagrams < vlen) {
err = nuse_recvmsg(fd,
(struct msghdr *)entry,
flags);
if (err < 0)
break;
entry->msg_len = err;
++entry;
++datagrams;
}
/* We only return an error if no datagrams were able to be recvmmsg */
if (datagrams != 0)
return datagrams;
return err;
}
/*
* FIXME: recvmmsg has different prototypes in different libc(s) ?
* such as recvmmsg(...., const struct timespec *) or
* recvmmsg(...., struct timespec *) etc.
* so disable weak alias for a while.
*
*/
#if 0
weak_alias(nuse_recvmmsg, recvmmsg);
weak_alias(nuse_recvmmsg, __recvmmsg);
#endif
ssize_t nuse_sendmsg(int fd, const struct msghdr *msghdr, int flags)
{
struct SimSocket *kernel_socket = nuse_fd_table[fd].nuse_sock->kern_sock;
ssize_t ret;
if (nuse_fd_table[fd].nuse_sock->flags & O_NONBLOCK)
flags |= MSG_DONTWAIT;
ret = g_exported->sock_sendmsg(kernel_socket, msghdr, flags);
if (ret < 0)
errno = -ret;
return ret;
}
weak_alias(nuse_sendmsg, sendmsg);
int nuse_sendmmsg(int fd, struct mmsghdr *msgvec, unsigned int vlen,
int flags)
{
int err, datagrams;
struct mmsghdr *entry;
datagrams = 0;
entry = msgvec;
err = 0;
while (datagrams < vlen) {
err = nuse_sendmsg(fd,
(struct msghdr *)entry,
flags);
if (err < 0)
break;
entry->msg_len = err;
++entry;
++datagrams;
}
/* We only return an error if no datagrams were able to be sent */
if (datagrams != 0)
return datagrams;
return err;
}
weak_alias(nuse_sendmmsg, sendmmsg);
weak_alias(nuse_sendmmsg, __sendmmsg);
int nuse_getsockname(int fd, struct sockaddr *name, socklen_t *namelen)
{
struct SimSocket *kernel_socket = nuse_fd_table[fd].nuse_sock->kern_sock;
int ret;
ret = g_exported->sock_getsockname(kernel_socket, name, (int *)namelen);
return ret;
}
weak_alias(nuse_getsockname, getsockname);
int nuse_getpeername(int fd, struct sockaddr *name, socklen_t *namelen)
{
struct SimSocket *kernel_socket = nuse_fd_table[fd].nuse_sock->kern_sock;
int ret;
ret = g_exported->sock_getsockname(kernel_socket, name, (int *)namelen);
return ret;
}
weak_alias(nuse_getpeername, getpeername);
int nuse_bind(int fd, const struct sockaddr *name, socklen_t namelen)
{
struct SimSocket *kernel_socket = nuse_fd_table[fd].nuse_sock->kern_sock;
int ret;
ret = g_exported->sock_bind(kernel_socket, name, namelen);
return ret;
}
weak_alias(nuse_bind, bind);
int nuse_connect(int fd, const struct sockaddr *addr, socklen_t len)
{
struct SimSocket *kernel_socket = nuse_fd_table[fd].nuse_sock->kern_sock;
int ret;
ret = g_exported->sock_connect(kernel_socket, addr, len,
nuse_fd_table[fd].nuse_sock->flags);
return ret;
}
weak_alias(nuse_connect, connect);
int nuse_listen(int fd, int v1)
{
struct SimSocket *kernel_socket = nuse_fd_table[fd].nuse_sock->kern_sock;
int retval;
retval = g_exported->sock_listen(kernel_socket, v1);
return retval;
}
weak_alias(nuse_listen, listen);
#if 0
int lib_sock_shutdown(struct SimSocket *socket, int how)
{
struct socket *sock = (struct socket *)socket;
int retval = sock->ops->shutdown(sock, how);
return retval;
}
#endif
int nuse_accept4(int fd, struct sockaddr *addr, int *addrlen, int flags)
{
struct SimSocket *kernel_socket = nuse_fd_table[fd].nuse_sock->kern_sock;
struct SimSocket *new_socket = NULL;
int retval, real_fd;
retval = g_exported->sock_accept(kernel_socket, &new_socket, flags);
if (retval < 0) {
errno = -retval;
free(new_socket);
perror("accept err");
return -1;
}
if (addr != 0) {
retval = g_exported->sock_getpeername(new_socket, addr, (int *)addrlen);
if (retval < 0) {
errno = -retval;
g_exported->sock_close(new_socket);
perror("getpeername err");
return -1;
}
}
real_fd = host_open("/", O_RDONLY, 0);
nuse_fd_table[real_fd].nuse_sock = malloc(sizeof(struct nuse_socket));
memset(nuse_fd_table[real_fd].nuse_sock, 0, sizeof(struct nuse_socket));
nuse_fd_table[real_fd].nuse_sock->kern_sock = new_socket;
nuse_fd_table[real_fd].nuse_sock->refcnt++;
nuse_fd_table[real_fd].real_fd = real_fd;
return real_fd;
}
int nuse_accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
{
return nuse_accept4(fd, addr, (int *)addrlen,
nuse_fd_table[fd].nuse_sock->flags);
}
weak_alias(nuse_accept, accept);
ssize_t nuse_write(int fd, const void *buf, size_t count)
{
if (!nuse_fd_table[fd].nuse_sock) {
if (!host_write) nuse_hostcall_init();
return host_write(nuse_fd_table[fd].real_fd, buf, count);
}
struct msghdr msg;
struct iovec iov;
msg.msg_control = 0;
msg.msg_controllen = 0;
msg.msg_iovlen = 1;
msg.msg_iov = &iov;
iov.iov_len = count;
iov.iov_base = (void *)buf;
msg.msg_name = 0;
msg.msg_namelen = 0;
return nuse_sendmsg(fd, &msg, 0);
}
weak_alias(nuse_write, write);
ssize_t nuse_writev(int fd, const struct iovec *iov, int count)
{
if (!nuse_fd_table[fd].nuse_sock) {
if (!host_writev) nuse_hostcall_init();
return host_writev(nuse_fd_table[fd].real_fd, iov, count);
}
struct msghdr msg;
msg.msg_control = 0;
msg.msg_controllen = 0;
msg.msg_iovlen = 1;
msg.msg_iov = (struct iovec *)iov;
msg.msg_name = 0;
msg.msg_namelen = 0;
return nuse_sendmsg(fd, &msg, 0);
}
weak_alias(nuse_writev, writev);
ssize_t nuse_sendto(int fd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, unsigned int addrlen)
{
struct msghdr msg;
struct iovec iov;
ssize_t retval;
memset(&msg, 0, sizeof(struct msghdr));
msg.msg_control = 0;
msg.msg_controllen = 0;
msg.msg_iovlen = 1;
msg.msg_iov = &iov;
iov.iov_len = len;
iov.iov_base = (void *)buf;
msg.msg_name = (void *)dest_addr;
msg.msg_namelen = addrlen;
retval = nuse_sendmsg(fd, &msg, flags);
return retval;
}
weak_alias(nuse_sendto, sendto);
ssize_t nuse_send(int fd, const void *buf, size_t len, int flags)
{
return nuse_sendto(fd, buf, len, flags, 0, 0);
}
weak_alias(nuse_send, send);
ssize_t nuse_read(int fd, void *buf, size_t count)
{
if (!nuse_fd_table[fd].nuse_sock) {
if (!host_read) nuse_hostcall_init();
return host_read(nuse_fd_table[fd].real_fd, buf, count);
}
struct msghdr msg;
struct iovec iov;
ssize_t retval;
msg.msg_control = 0;
msg.msg_controllen = 0;
msg.msg_iovlen = 1;
msg.msg_iov = &iov;
iov.iov_len = count;
iov.iov_base = buf;
msg.msg_name = 0;
msg.msg_namelen = 0;
retval = nuse_recvmsg(fd, &msg, 0);
return retval;
}
weak_alias(nuse_read, read);
#ifdef _K_SS_MAXSIZE
#define SOCK_MAX_ADDRESS_SIZE _K_SS_MAXSIZE
#else
#define SOCK_MAX_ADDRESS_SIZE 128
#endif
ssize_t nuse_recvfrom(int fd, void *buf, size_t len, int flags,
struct sockaddr *from, socklen_t *fromlen)
{
uint8_t address[SOCK_MAX_ADDRESS_SIZE];
struct msghdr msg;
struct iovec iov;
ssize_t retval;
msg.msg_control = 0;
msg.msg_controllen = 0;
msg.msg_iovlen = 1;
msg.msg_iov = &iov;
iov.iov_len = len;
iov.iov_base = buf;
msg.msg_name = address;
msg.msg_namelen = SOCK_MAX_ADDRESS_SIZE;
retval = nuse_recvmsg(fd, &msg, flags);
if (retval != -1 && from != 0) {
if (*fromlen < msg.msg_namelen) {
errno = EINVAL;
return -1;
} else {
*fromlen = msg.msg_namelen;
memcpy(from, msg.msg_name, msg.msg_namelen);
}
}
return retval;
}
weak_alias(nuse_recvfrom, recvfrom);
ssize_t nuse_recv(int fd, void *buf, size_t count, int flags)
{
return nuse_recvfrom(fd, buf, count, flags, 0, 0);
}
weak_alias(nuse_recv, recv);
int nuse_setsockopt(int fd, int level, int optname,
const void *optval, socklen_t optlen)
{
struct SimSocket *kernel_socket = nuse_fd_table[fd].nuse_sock->kern_sock;
int retval = g_exported->sock_setsockopt(kernel_socket, level, optname, optval,
optlen);
if (retval < 0) {
errno = -retval;
return -1;
}
return retval;
}
weak_alias(nuse_setsockopt, setsockopt);
int nuse_getsockopt(int fd, int level, int optname,
void *optval, socklen_t *optlen)
{
struct SimSocket *kernel_socket = nuse_fd_table[fd].nuse_sock->kern_sock;
int retval = g_exported->sock_getsockopt(kernel_socket, level, optname, optval,
(int *)optlen);
if (retval < 0) {
errno = -retval;
return -1;
}
return retval;
}
weak_alias(nuse_getsockopt, getsockopt);
int nuse_ioctl(int fd, int request, ...)
{
va_list vl;
char *argp;
va_start(vl, request);
argp = va_arg(vl, char *);
va_end(vl);
if (!nuse_fd_table[fd].nuse_sock) {
if (!host_ioctl) nuse_hostcall_init();
return host_ioctl(nuse_fd_table[fd].real_fd, request, argp);
}
return g_exported->sock_ioctl(nuse_fd_table[fd].nuse_sock->kern_sock, request, argp);
}
weak_alias(nuse_ioctl, ioctl);
int fcntl(int fd, int cmd, ... /* arg */ )
{
va_list vl;
int *argp;
long err = -EINVAL;
va_start(vl, cmd);
argp = va_arg(vl, int *);
va_end(vl);
if (!nuse_fd_table[fd].nuse_sock) {
if (!host_fcntl) nuse_hostcall_init();
return host_fcntl(nuse_fd_table[fd].real_fd, cmd, argp);
}
/* nuse routine */
switch (cmd) {
case F_DUPFD:
err = host_fcntl(nuse_fd_table[fd].real_fd, cmd, argp);
if (err == -1) {
return err;
}
nuse_fd_table[err].real_fd = err;
nuse_fd_table[err].nuse_sock = nuse_fd_table[fd].nuse_sock;
nuse_fd_table[err].epoll_fd = nuse_fd_table[fd].epoll_fd;
nuse_fd_table[err].nuse_sock->refcnt++;
break;
case F_GETFL:
return nuse_fd_table[fd].nuse_sock->flags;
break;
case F_SETFL:
nuse_fd_table[fd].nuse_sock->flags = (intptr_t)argp;
return 0;
break;
default:
break;
}
return err;
}
int open(const char *pathname, int flags, ...)
{
va_list vl;
va_start(vl, flags);
if (!host_open) nuse_hostcall_init();
int real_fd = host_open(pathname, flags, va_arg(vl, mode_t));
va_end(vl);
if (real_fd < 0) {
perror("open");
return -1;
}
nuse_fd_table[real_fd].real_fd = real_fd;
return real_fd;
}
int open64(const char *pathname, int flags, mode_t mode)
{
if (!host_open64) nuse_hostcall_init();
int real_fd = host_open64(pathname, flags, mode);
/* printf ("%d, %llu %s %s\n", nuse_fd_table[curfd].real_fd, curfd, pathname, __FUNCTION__); */
if (real_fd < 0) {
perror("open64");
return -1;
}
nuse_fd_table[real_fd].real_fd = real_fd;
return real_fd;
}
int pipe(int pipefd[2])
{
if (!host_pipe) nuse_hostcall_init();
int ret = host_pipe(pipefd);
if (ret == -1)
return ret;
nuse_fd_table[pipefd[0]].real_fd = pipefd[0];
nuse_fd_table[pipefd[1]].real_fd = pipefd[1];
return ret;
}
/* From librumphijack/hijack.c */
struct pollarg {
struct pollfd *pfds;
nfds_t nfds;
const struct timespec *ts;
const sigset_t *sigmask;
int pipefd;
int errnum;
};
static void *
hostpoll(void *arg)
{
struct pollarg *parg = arg;
intptr_t rv;
int to;
to = parg->ts ? timespec_to_ns(parg->ts) / NSEC_PER_MSEC : -1;
if (!host_poll) nuse_hostcall_init();
rv = host_poll(parg->pfds, parg->nfds, to);
if (rv == -1)
parg->errnum = errno;
lib_assert(write(parg->pipefd, &rv, sizeof(rv)) > 0);
return (void *)rv;
}
/*
* poll is easy as long as the call comes in the fds only in one
* kernel. otherwise its quite tricky...
*/
static int
do_host_nuse_poll(struct pollfd *fds, nfds_t nfds, struct timespec *ts)
{
/* copied from librumphijack/hijack.c */
struct pollfd *pfd_host = NULL, *pfd_rump = NULL;
int rpipe[2] = {-1,-1}, hpipe[2] = {-1,-1};
struct pollarg parg;
void *trv_val;
int sverrno = 0, rv_rump, rv_host, errno_rump, errno_host;
pthread_t pt;
nfds_t i;
int rv;
/*
* ok, this is where it gets tricky. We must support
* this since it's a very common operation in certain
* types of software (telnet, netcat, etc). We allocate
* two vectors and run two poll commands in separate
* threads. Whichever returns first "wins" and the
* other kernel's fds won't show activity.
*/
rv = -1;
/* allocate full vector for O(n) joining after call */
pfd_host = malloc(sizeof(*pfd_host)*(nfds+1));
if (!pfd_host)
goto out;
pfd_rump = malloc(sizeof(*pfd_rump)*(nfds+1));
if (!pfd_rump) {
goto out;
}
/*
* then, open two pipes, one for notifications
* to each kernel.
*
* At least the rump pipe should probably be
* cached, along with the helper threads. This
* should give a microbenchmark improvement (haven't
* experienced a macro-level problem yet, though).
*/
if ((rv = pipe(rpipe)) == -1) {
sverrno = errno;
}
if (rv == 0 && (rv = pipe(hpipe)) == -1) {
sverrno = errno;
}
/* split vectors (or signal errors) */
for (i = 0; i < nfds; i++) {
int fd;
fds[i].revents = 0;
if (fds[i].fd == -1) {
pfd_host[i].fd = -1;
pfd_rump[i].fd = -1;
} else if (nuse_fd_table[fds[i].fd].nuse_sock) {
pfd_host[i].fd = -1;
fd = fds[i].fd;
if (fd == rpipe[0] || fd == rpipe[1]) {
fds[i].revents = POLLNVAL;
if (rv != -1)
rv++;
}
pfd_rump[i].fd = fd;
pfd_rump[i].events = fds[i].events;
} else {
pfd_rump[i].fd = -1;
fd = fds[i].fd;
if (fd == hpipe[0] || fd == hpipe[1]) {
fds[i].revents = POLLNVAL;
if (rv != -1)
rv++;
}
pfd_host[i].fd = fd;
pfd_host[i].events = fds[i].events;
}
pfd_rump[i].revents = pfd_host[i].revents = 0;
}
if (rv) {
goto out;
}
pfd_host[nfds].fd = hpipe[0];
pfd_host[nfds].events = POLLIN;
pfd_rump[nfds].fd = rpipe[0];
pfd_rump[nfds].events = POLLIN;
/*
* then, create a thread to do host part and meanwhile
* do rump kernel part right here
*/
parg.pfds = pfd_host;
parg.nfds = nfds+1;
parg.ts = ts;
/* parg.sigmask = sigmask; */
parg.pipefd = rpipe[1];
host_pthread_create(&pt, NULL, hostpoll, &parg);
rv_rump = nuse_poll(pfd_rump, nfds+1, ts);
errno_rump = errno;
lib_assert(write(hpipe[1], &rv, sizeof(rv)) > 0);
host_pthread_join(pt, &trv_val);
rv_host = (int)(intptr_t)trv_val;
errno_host = parg.errnum;
/* strip cross-thread notification from real results */
if (rv_host > 0 && pfd_host[nfds].revents & POLLIN) {
rv_host--;
}
if (rv_rump > 0 && pfd_rump[nfds].revents & POLLIN) {
rv_rump--;
}
/* then merge the results into what's reported to the caller */
if (rv_rump > 0 || rv_host > 0) {
/* SUCCESS */
rv = 0;
if (rv_rump > 0) {
for (i = 0; i < nfds; i++) {
if (pfd_rump[i].fd != -1)
fds[i].revents
= pfd_rump[i].revents;
}
rv += rv_rump;
}
if (rv_host > 0) {
for (i = 0; i < nfds; i++) {
if (pfd_host[i].fd != -1)
fds[i].revents
= pfd_host[i].revents;
}
rv += rv_host;
}
lib_assert(rv > 0);
sverrno = 0;
} else if (rv_rump == -1 || rv_host == -1) {
/* ERROR */
/* just pick one kernel at "random" */
rv = -1;
if (rv_host == -1) {
sverrno = errno_host;
} else if (rv_rump == -1) {
sverrno = errno_rump;
}
} else {
/* TIMEOUT */
rv = 0;
lib_assert(rv_rump == 0 && rv_host == 0);
}
out:
if (rpipe[0] != -1)
host_close(rpipe[0]);
if (rpipe[1] != -1)
host_close(rpipe[1]);
if (hpipe[0] != -1)
host_close(hpipe[0]);
if (hpipe[1] != -1)
host_close(hpipe[1]);
free(pfd_host);
free(pfd_rump);
errno = sverrno;
return rv;
}
/* copied from librumphijack/hijack.c */
static void
checkpoll(struct pollfd *fds, nfds_t nfds, int *hostcall, int *rumpcall)
{
nfds_t i;
for (i = 0; i < nfds; i++) {
if (fds[i].fd == -1)
continue;
if (nuse_fd_table[fds[i].fd].nuse_sock)
(*rumpcall)++;
else
(*hostcall)++;
}
}
struct poll_table_ref
{
int ret;
void *opaque;
};
int
nuse_poll(struct pollfd *fds, unsigned int nfds, struct timespec *end_time)
{
struct poll_table_ref table, *current_table;
struct poll_table_ref null_table = {0};
int count = 0, error;
int i, mask, timed_out = 0;
pthread_cond_t condvar;
pthread_mutex_t cond_mutex;
struct timespec ts;
error = pthread_cond_init(&condvar, NULL);
lib_assert(error == 0);
error = pthread_mutex_init(&cond_mutex, NULL);
lib_assert(error == 0);
/* initialize all outgoing events. */
for (i = 0; i < nfds; ++i)
fds[i].revents = 0;
table.opaque = NULL;
current_table = &table;
if (end_time && end_time->tv_sec == 0 &&
end_time->tv_nsec == 0) {
current_table = NULL;
timed_out = 1;
}
/* call (sim) kernel side */
for (;;) {
for (i = 0; i < nfds; ++i) {
struct SimSocket *sock;
/* host's fd */
if (fds[i].fd == -1)
continue;
if (!nuse_fd_table[fds[i].fd].nuse_sock)
continue;
/* nuse's fd */
sock = nuse_fd_table[fds[i].fd].nuse_sock->kern_sock;
if (current_table) {
current_table->ret = fds[i].events | POLLERR | POLLHUP;
current_table->opaque = &condvar;
}
else
current_table = &null_table;
g_exported->sock_poll((struct SimSocket *)sock, current_table);
mask = current_table->ret;
mask &= (fds[i].events | POLLERR | POLLHUP);
fds[i].revents = mask;
if (mask) {
count++;
current_table = NULL;
}
}
current_table = NULL;
if (count || timed_out) {
break;
}
if (timed_out == 0) {
/* infinite wait */
if (end_time == NULL) {
pthread_mutex_lock(&cond_mutex);
pthread_cond_wait(&condvar, &cond_mutex);
pthread_mutex_unlock(&cond_mutex);
}
/* sleeped wait */
else {
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += end_time->tv_sec;
ts.tv_nsec += end_time->tv_nsec;
pthread_mutex_lock(&cond_mutex);
error = pthread_cond_timedwait(&condvar, &cond_mutex, &ts);
pthread_mutex_unlock(&cond_mutex);
}
timed_out = 1;
}
} /* for loop */
pthread_mutex_destroy(&cond_mutex);
pthread_cond_destroy(&condvar);
g_exported->sock_pollfreewait(table.opaque);
return count;
}
int
poll(struct pollfd *fds, nfds_t nfds, int timeout)
{
struct timespec *to = NULL, end_time;
int hostcall = 0, rumpcall = 0;
int count;
if (timeout >= 0) {
end_time.tv_sec = timeout / MSEC_PER_SEC;
end_time.tv_nsec = NSEC_PER_MSEC * (timeout % MSEC_PER_SEC);
to = &end_time;
}
checkpoll(fds, nfds, &hostcall, &rumpcall);
if (hostcall && rumpcall) {
/* this is the case to write carefully between host and nuse */
/* see rump/hijack.c for more detail */
count = do_host_nuse_poll(fds, nfds, to);
}
else {
if (hostcall) {
count = host_poll(fds, nfds, timeout);
}
else {
count = nuse_poll(fds, nfds, to);
}
}
return count;
}
weak_alias (poll, __poll);
int
select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout)
{
struct pollfd pollFd[nfds];
int fd;
memset(pollFd, 0, sizeof(struct pollfd) * nfds);
if (nfds == -1) {
errno = EINVAL;
return -1;
}
if (readfds == 0 && writefds == 0 && exceptfds == 0) {
errno = EINVAL;
return -1;
}
if (timeout) {
if (timeout->tv_sec < 0 || timeout->tv_usec < 0) {
errno = EINVAL;
return -1;
}
}
for (fd = 0; fd < nfds; fd++) {
int event = 0;
if (readfds != 0 && FD_ISSET(fd, readfds))
event |= POLLIN;
if (writefds != 0 && FD_ISSET(fd, writefds))
event |= POLLOUT;
if (exceptfds != 0 && FD_ISSET(fd, exceptfds))
event |= POLLPRI;
pollFd[fd].events = event;
pollFd[fd].revents = 0;
if (event) {
if (!nuse_fd_table[fd].nuse_sock) {
errno = EBADF;
return -1;
}
pollFd[fd].fd = fd;
}
else {
pollFd[fd].fd = -1;
}
}
/* select(2): */
/* Some code calls select() with all three sets empty, nfds zero,
and a non-NULL timeout as a fairly portable way to sleep with
subsecond precision. */
/* 130825: this condition will be passed by dce_poll () */
int to_msec = -1;
if (timeout)
to_msec = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
int ret = poll(pollFd, nfds, to_msec);
if (readfds)
FD_ZERO(readfds);
if (writefds)
FD_ZERO(writefds);
if (exceptfds)
FD_ZERO(exceptfds);
if (ret > 0) {