File: um_socket.c

package info (click to toggle)
umview 0.8.2-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 5,472 kB
  • sloc: ansic: 67,309; sh: 11,160; ruby: 914; makefile: 424; python: 141
file content (695 lines) | stat: -rw-r--r-- 18,953 bytes parent folder | download | duplicates (4)
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
/*   This is part of um-ViewOS
 *   The user-mode implementation of OSVIEW -- A Process with a View
 *
 *   um_socket: socketcall wrappers
 *   
 *   Copyright 2005 Renzo Davoli University of Bologna - Italy
 *   
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License, version 2, as
 *   published by the Free Software Foundation.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not, write to the Free Software Foundation, Inc.,
 *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *   $Id: um_socket.c 994 2011-08-17 14:50:25Z rd235 $
 *
 */   
#include <assert.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <asm/ptrace.h>
#include <asm/unistd.h>
#include <linux/net.h>
#include <sys/uio.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <alloca.h>
#include <config.h>
#include "defs.h"
#include "umproc.h"
#include "services.h"
#include "hashtab.h"
#include "um_services.h"
#include "sctab.h"
#include "scmap.h"
#include "utils.h"

#define SOCK_DEFAULT 0

/* SOCKET & MSOCKET call management (IN) */

int wrap_in_msocket(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	/* path = pc->sysarg[0] = pc->path */
	int domain  =pc->sysargs[1];
	int type    =pc->sysargs[2];
	int protocol=pc->sysargs[3];
	/* msocket is ALWAYS called: msocket(NULL...) calls must be converted
	 * into socket(...) syscalls */
	if (hte != NULL) {
		if (type == SOCK_DEFAULT) {
			if (pc->path == NULL) {
				pc->retval = -1;
				pc->erno = EINVAL;
			} else {
				if ((pc->retval = um_syscall(pc->path,domain,type,protocol)) < 0) {
					pc->erno = errno;
				}
				//printk("SOCK_DEFAULT %s %d\n",pc->path,domain);
			}
			return SC_FAKE;
		} else {
			if ((pc->retval = um_syscall(pc->path,domain,type,protocol)) < 0) {
				if (errno == ENOSYS && pc->path==NULL) {
					/* backward compatibility:
					 * modules implementing only "socket". 
					 * the code reaches this case only from wrap_in_socket */
#if (__NR_socketcall != __NR_doesnotexist)
					um_syscall=ht_socketcall(hte,SYS_SOCKET);
#else
					um_syscall=ht_syscall(hte,uscno(__NR_socket));
#endif
					if ((pc->retval = um_syscall(domain,type,protocol)) < 0) {
						pc->erno = errno;
					}
				} else
					pc->erno = errno;
			}
			/* create the comm fifo with the user process */
			if (pc->retval >= 0 &&
					(pc->retval=lfd_open(hte,pc->retval,NULL,O_RDWR,0)) >= 0) {
				um_x_rewritepath(pc,lfd_getfilename(pc->retval),0,0);
				putscno(__NR_open,pc);
				pc->sysargs[1]=O_RDONLY;
				return SC_CALLONXIT;
			} else
				return SC_FAKE;
		} 
	} else {
		/* msocket -> socket translation for native system calls
		 * just for the case path=NULL */
		if (pc->sysargs[0]==umNULL) {
#if (__NR_socketcall != __NR_doesnotexist)
			struct {
				long domain;
				long type;
				long protocol;
			} socketcallparms = {domain,type,protocol};
			long sp=getsp(pc);
			ustoren(pc,sp-sizeof(socketcallparms),
					sizeof(socketcallparms),&socketcallparms);
			pc->sysargs[0]=SYS_SOCKET;
			pc->sysargs[1]=sp-sizeof(socketcallparms);
			putscno(__NR_socketcall,pc);
			return SC_MODICALL;
#else
			pc->sysargs[0]=domain;
			pc->sysargs[1]=type;
			pc->sysargs[2]=protocol;
			putscno(__NR_socket,pc);
			return SC_MODICALL;
#endif
		} else {
			pc->retval = -1;
			pc->erno = ENOTSUP;
			return SC_FAKE;
		}
	}
}

int wrap_in_socket(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	pc->sysargs[3]=pc->sysargs[2];
	pc->sysargs[2]=pc->sysargs[1];
	pc->sysargs[1]=pc->sysargs[0];
	pc->sysargs[0]=umNULL;
	pc->path=NULL;
	return wrap_in_msocket(__NR_msocket,pc,hte,
			ht_virsyscall(hte,VIRSYS_MSOCKET));
}

#define MAX_SOCKLEN 1024
/* accept creates a new fd! */
int wrap_in_accept(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	int sfd=fd2sfd(pc->fds,pc->sysargs[0]);
	/* the virtual file does not exist */
	if (sfd < 0) {
		pc->retval= -1;
		pc->erno= EBADF;
	} else {
		/* get the system call args */
		long sock_plen=pc->sysargs[2];
		int sock_len;
		if (sock_plen != umNULL) {
			umoven(pc,sock_plen,4,&sock_len);
			/* safety check for sock */
			if (sock_len == 0 || sock_len > MAX_SOCKLEN) {
				pc->retval= -1;
				pc->erno= EINVAL;
			} else {
				long sock_addr=pc->sysargs[1];
				char *sock;
				if (__builtin_expect((sock_len > MAX_SOCKET_NAME),0)) 
					sock_len=MAX_SOCKET_NAME;
				sock=(char *)alloca(sock_len);
				/* get the sock_addr */
				umoven(pc,sock_addr,sock_len,sock);
				/* virtual syscall */
				if ((pc->retval = um_syscall(sfd,sock,&sock_len)) < 0)
					pc->erno=errno;
				else {
					/* store the results (if the call was successful) */
					if (sock_addr != umNULL)
						ustoren(pc,sock_addr,sock_len,sock);
					if (sock_plen != umNULL)
						umoven(pc,sock_plen,4,&sock_len);
				}
			}
		} else {
			if ((pc->retval = um_syscall(sfd,NULL,NULL)) < 0)
				pc->erno=errno;
		}

		/* open the new fifo, (accept creates a new fd) */
		if (pc->retval >= 0 && 
				(pc->retval=lfd_open(hte,pc->retval,NULL,O_RDWR,0)) >= 0) {
			um_x_rewritepath(pc,lfd_getfilename(pc->retval),0,0);
			putscno(__NR_open,pc);
			pc->sysargs[1]=O_RDONLY;
			return SC_CALLONXIT;
		} else
			return SC_FAKE;
	}
	return SC_FAKE;
}

/* SOCKET & MSOCKET & ACCEPT wrap out */
int wrap_out_socket(int sc_number,struct pcb *pc) {
	/*int lerno=errno;*/
	//printk("wrap_out_socket %d %d\n",pc->behavior,SC_FAKE);
	/* if everything was okay for the virtual call */
	if (pc->behavior==SC_CALLONXIT && pc->retval >= 0) {
		int fd=getrv(pc);	
		/* if the syscall issued by the process was also okay */
		if (fd >= 0 && addfd(pc,fd) == 0) {
			/* update open file table*/
			lfd_register(pc->fds,fd,pc->retval);
#ifdef __NR_accept4
			if (sc_number == __NR_accept4) {
				int flags = pc->sysargs[3];
				if (flags & SOCK_CLOEXEC) 
					fd_setfdfl(pc->fds,fd,FD_CLOEXEC);
			}
#endif
		} else {
			putrv(pc->retval,pc);
			puterrno(pc->erno,pc);
			//printk("wrap_out_socket!!\n");
			lfd_close(pc->retval);
		}
	} else {
		putrv(pc->retval,pc);
		puterrno(pc->erno,pc);
	}
	return SC_MODICALL;
}

int wrap_in_bind_connect(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	int sfd=fd2sfd(pc->fds,pc->sysargs[0]);
	if (sfd < 0) {
		pc->retval= -1;
		pc->erno= EBADF;
	} else {
		long sock_addr=pc->sysargs[1];
		long sock_len=pc->sysargs[2];
		char *sock;
		if (__builtin_expect((sock_len > MAX_SOCKET_NAME),0)) 
			sock_len=MAX_SOCKET_NAME;
		sock=(char *)alloca(sock_len);
		umoven(pc,sock_addr,sock_len,sock);
		if ((pc->retval = um_syscall(sfd,sock,sock_len)) < 0)
			pc->erno=errno;
	}
	return SC_FAKE;
}

int wrap_in_listen(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	int sfd=fd2sfd(pc->fds,pc->sysargs[0]);
	if (sfd < 0) {
		pc->retval= -1;
		pc->erno= EBADF;
	} else {
		int backlog=pc->sysargs[1];
		if ((pc->retval=um_syscall(sfd,backlog)) < 0)
			pc->erno=errno;
	}
	return SC_FAKE;
}

int wrap_in_getsock(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	int sfd=fd2sfd(pc->fds,pc->sysargs[0]);
	if (sfd < 0) {
		pc->retval= -1;
		pc->erno= EBADF;
	} else {
		long sock_addr=pc->sysargs[1];
		long sock_plen=pc->sysargs[2];
		int sock_len;
		if (sock_plen != umNULL)
			umoven(pc,sock_plen,4,&sock_len);
		char *sock;
		if (__builtin_expect((sock_len > MAX_SOCKET_NAME),0)) 
			sock_len=MAX_SOCKET_NAME;
		sock=(char *)alloca(sock_len);
		umoven(pc,sock_addr,sock_len,sock);
		if ((pc->retval = um_syscall(sfd,sock,&sock_len)) < 0)
			pc->erno=errno;
		else {
			if (sock_addr != umNULL)
				ustoren(pc,sock_addr,sock_len,sock);
			if (sock_plen != umNULL)
				umoven(pc,sock_plen,4,&sock_len);
		}
	}
	return SC_FAKE;
}

int wrap_in_send(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	int sfd=fd2sfd(pc->fds,pc->sysargs[0]);
	if (sfd < 0) {
		pc->retval= -1;
		pc->erno= EBADF;
	} else 
	{
		long buf=pc->sysargs[1];
		int len=pc->sysargs[2];
		int flags=pc->sysargs[3];
		char *lbuf=(char *)lalloca(len); 
#ifdef SNDRCVMSGUNIFY
		struct iovec iov = {lbuf,len};
		struct msghdr msg = {
			.msg_name=NULL,
			.msg_namelen=0,
			.msg_iov=&iov,
			.msg_iovlen=1,
			.msg_control=NULL,
			.msg_controllen=0,
			.msg_flags=flags};
#endif
		umoven(pc,buf,len,lbuf);
#ifdef SNDRCVMSGUNIFY
		if ((pc->retval=um_syscall(sfd,&msg,flags)) < 0)
			pc->erno=errno;
#else
		if ((pc->retval=um_syscall(sfd,lbuf,len,flags)) < 0)
			pc->erno=errno;
#endif
		lfree(lbuf,len);
	}
	return SC_FAKE;
}

int wrap_in_recv(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	int sfd=fd2sfd(pc->fds,pc->sysargs[0]);
	if (sfd < 0) {
		pc->retval= -1;
		pc->erno= EBADF;
	} else {
		long buf=pc->sysargs[1];
		int len=pc->sysargs[2];
		int flags=pc->sysargs[3];
		char *lbuf=(char *)lalloca(len);
#ifdef SNDRCVMSGUNIFY
		struct iovec iov = {lbuf,len};
		struct msghdr msg = {
			.msg_name=NULL,
			.msg_namelen=0,
			.msg_iov=&iov,
			.msg_iovlen=1,
			.msg_control=NULL,
			.msg_controllen=0,
			.msg_flags=flags};
		if ((pc->retval=um_syscall(sfd,&msg,flags)) < 0)
			pc->erno=errno;
#else
		if ((pc->retval=um_syscall(sfd,lbuf,len,flags)) < 0)
			pc->erno=errno;
#endif
		if (pc->retval > 0)
			ustoren(pc,buf,pc->retval,lbuf);
		lfree(lbuf,len);
	}
	return SC_FAKE;
}

int wrap_in_sendto(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	int sfd=fd2sfd(pc->fds,pc->sysargs[0]);
	if (sfd < 0) {
		pc->retval= -1;
		pc->erno= EBADF;
	} else {
		long buf=pc->sysargs[1];
		int len=pc->sysargs[2];
		int flags=pc->sysargs[3];
		long pto=pc->sysargs[4];
		int tolen=pc->sysargs[5];
		char *lbuf=(char *)lalloca(len); 
		char *tosock=NULL;
#ifdef SNDRCVMSGUNIFY
		struct iovec iov = {lbuf,len};
		struct msghdr msg = {
			.msg_name=NULL,
			.msg_namelen=0,
			.msg_iov=&iov,
			.msg_iovlen=1,
			.msg_control=NULL,
			.msg_controllen=0,
			.msg_flags=flags};
#endif
		umoven(pc,buf,len,lbuf);
		if (pto != umNULL) {
			if (__builtin_expect((tolen > MAX_SOCKET_NAME),0)) 
				tolen=MAX_SOCKET_NAME;
			tosock=alloca(tolen);
			umoven(pc,pto,tolen,tosock);
#ifdef SNDRCVMSGUNIFY
			msg.msg_name=tosock;
			msg.msg_namelen=tolen;
#endif
		}
#ifdef SNDRCVMSGUNIFY
		if ((pc->retval=um_syscall(sfd,&msg,flags)) < 0)
			pc->erno=errno;
#else
		if ((pc->retval=um_syscall(sfd,lbuf,len,flags,tosock,tolen)) < 0)
			pc->erno=errno;
#endif
		lfree(lbuf,len);
	}
	return SC_FAKE;
}

int wrap_in_recvfrom(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	int sfd=fd2sfd(pc->fds,pc->sysargs[0]);
	if (sfd < 0) {
		pc->retval= -1;
		pc->erno= EBADF;
	} else {
		long buf=pc->sysargs[1];
		int len=pc->sysargs[2];
		int flags=pc->sysargs[3];
		long pfrom=pc->sysargs[4];
		long pfromlen=pc->sysargs[5];
		int fromlen=0;
		char *lbuf=(char *)lalloca(len);
		char *fromsock=NULL;
#ifdef SNDRCVMSGUNIFY
		struct iovec iov = {lbuf,len};
		struct msghdr msg = {
			.msg_name=NULL,
			.msg_namelen=0,
			.msg_iov=&iov,
			.msg_iovlen=1,
			.msg_control=NULL,
			.msg_controllen=0,
			.msg_flags=flags};
#endif
		if (pfromlen != umNULL) {
			umoven(pc,pfromlen,4,(char *)&fromlen);
			if (pfrom != umNULL && fromlen != 0) {
				if (__builtin_expect((fromlen > MAX_SOCKET_NAME),0)) 
					fromlen=MAX_SOCKET_NAME;
				fromsock=alloca(fromlen);
				umoven(pc,pfrom,fromlen,fromsock);
			}
#ifdef SNDRCVMSGUNIFY
			msg.msg_name=fromsock;
			msg.msg_namelen=pfromlen;
#endif
		}
#ifdef SNDRCVMSGUNIFY
		if ((pc->retval=um_syscall(sfd,&msg,flags)) < 0)
			pc->erno=errno;
#else
		if ((pc->retval=um_syscall(sfd,lbuf,len,flags,fromsock,&fromlen)) < 0)
			pc->erno=errno;
#endif
		if (pc->retval > 0) {
			ustoren(pc,buf,pc->retval,lbuf);
			if (pfrom != umNULL)
				ustoren(pc,pfrom,fromlen,fromsock);
			if (pfromlen != umNULL)
				ustoren(pc,pfromlen,4,&fromlen);
		}
		lfree(lbuf,len);
	}
	return SC_FAKE;
}

int wrap_in_shutdown(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	int sfd=fd2sfd(pc->fds,pc->sysargs[0]);
	if (sfd < 0) {
		pc->retval= -1;
		pc->erno= EBADF;
	} else {
		int how=pc->sysargs[1];
		if ((pc->retval=um_syscall(sfd,how)) < 0)
			pc->erno=errno;
	}
	return SC_FAKE;
}

int wrap_in_getsockopt(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	int sfd=fd2sfd(pc->fds,pc->sysargs[0]);
	if (sfd < 0) {
		pc->retval= -1;
		pc->erno= EBADF;
	} else {
		int level=pc->sysargs[1];
		int optname=pc->sysargs[2];
		long poptval=pc->sysargs[3];
		long poptlen=pc->sysargs[4];
		int optlen;
		void *optval;
		if (poptlen != umNULL) {
			umoven(pc,poptlen,4,(char *)&optlen);
			if (__builtin_expect((optlen > MAX_SOCKOPT_LEN),0))
				optlen=MAX_SOCKOPT_LEN;
			optval=(optlen > 0)?alloca(optlen):NULL;
		} else {
			optlen=0;
			optval=NULL;
		}
		if ((pc->retval=um_syscall(sfd,level,optname,optval,&optlen)) < 0)
			pc->erno=errno;
		if (poptlen != umNULL)
			ustoren(pc,poptlen,4,&optlen);
		if (poptval != umNULL)
			ustoren(pc,poptval,optlen,optval);
	}
	return SC_FAKE;
}

int wrap_in_setsockopt(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	int sfd=fd2sfd(pc->fds,pc->sysargs[0]);
	if (sfd < 0) {
		pc->retval= -1;
		pc->erno= EBADF;
	} else {
		int level=pc->sysargs[1];
		int optname=pc->sysargs[2];
		long poptval=pc->sysargs[3];
		int optlen=pc->sysargs[4];
		void *optval;
		//printk("setsockopt fd %d level %d optname %d poptval %x optlen %d\n",pc->sysargs[0],level,optname,poptval,optlen);
		if (optlen > 0 && poptval != umNULL) { 
			if (__builtin_expect((optlen > MAX_SOCKOPT_LEN),0))
				optlen=MAX_SOCKOPT_LEN;
			optval=alloca(optlen);
			umoven(pc,poptval,optlen,optval);
		} else
			optval=NULL;
		if ((pc->retval=um_syscall(sfd,level,optname,optval,optlen)) < 0)
			pc->erno=errno;
		/*if (optval != NULL)
			ustoren(pc,poptval,optlen,optval);*/
	}
	return SC_FAKE;
}
/* sendmsg and recvmsg have more complex arguments */
int wrap_in_recvmsg(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	int sfd=fd2sfd(pc->fds,pc->sysargs[0]);
	if (sfd < 0) {
		pc->retval= -1;
		pc->erno= EBADF;
	} else {
		long pmsg=pc->sysargs[1];
		int flags=pc->sysargs[2];
		struct msghdr msg;
		struct msghdr lmsg;
		struct iovec liovec;
		struct iovec *iovec;
		umoven(pc,pmsg,sizeof(struct msghdr),&msg);
		lmsg=msg;
		if (msg.msg_namelen > 0 && msg.msg_name != NULL) {
			if (__builtin_expect((msg.msg_namelen > MAX_SOCKET_NAME),0)) 
				msg.msg_namelen=MAX_SOCKET_NAME;
			lmsg.msg_name=alloca(msg.msg_namelen);
			umoven(pc,(long)msg.msg_name,msg.msg_namelen,lmsg.msg_name);
		}
		if (msg.msg_iovlen > 0 && msg.msg_iov != NULL) {
			if (__builtin_expect((msg.msg_iovlen > IOV_MAX),0)) msg.msg_iovlen=IOV_MAX;
			iovec=alloca(msg.msg_iovlen * sizeof(struct iovec));
			umoven(pc,(long)msg.msg_iov,msg.msg_iovlen * sizeof(struct iovec),iovec);
		} else {
			iovec=NULL;
			msg.msg_iovlen = 0;
		}
		if (msg.msg_controllen > 0 && msg.msg_control != NULL) {
			if (__builtin_expect((msg.msg_controllen > MAX_SOCK_CONTROLLEN),0))
				msg.msg_controllen=MAX_SOCK_CONTROLLEN;
			lmsg.msg_control=alloca(msg.msg_controllen);
			umoven(pc,(long)msg.msg_control,msg.msg_controllen,lmsg.msg_control);
		}
		{
			unsigned int i,totalsize,size;
			char *lbuf, *p;
			for (i=0,totalsize=0;i<msg.msg_iovlen;i++)
				totalsize += iovec[i].iov_len;
			p=lbuf=(char *)lalloca(totalsize);
			//printk("RECVMSG fd %d namesize %d msg_iovlen %d msg_controllen %d total %d\n",
			//		pc->sysargs[0],msg.msg_namelen, msg.msg_iovlen, msg.msg_controllen, totalsize);
			liovec.iov_base=lbuf;
			liovec.iov_len=totalsize;
			lmsg.msg_iov=&liovec;
			lmsg.msg_iovlen=1;
			//printk("%d size->%d\n",sfd,size);
			if ((pc->retval = um_syscall(sfd,&lmsg,flags)) < 0) {
				size = 0;
				pc->erno = errno;
			} else
				size = pc->retval;
			if (size > 0) {
				for (i=0;i<msg.msg_iovlen && size>0;i++) {
					int qty=(size > iovec[i].iov_len)?iovec[i].iov_len:size;
					ustoren(pc,(long)iovec[i].iov_base,qty,p);
					p += qty;
					size -= qty;
				}
			}
			if (msg.msg_namelen > 0 && msg.msg_name != NULL) {
				msg.msg_namelen=lmsg.msg_namelen;
				ustoren(pc,(long)msg.msg_name,msg.msg_namelen,lmsg.msg_name);
			}
			if (msg.msg_controllen > 0 && msg.msg_control != NULL) {
				msg.msg_controllen=lmsg.msg_controllen;
				ustoren(pc,(long)msg.msg_control,msg.msg_controllen,lmsg.msg_control);
			}
			msg.msg_flags=lmsg.msg_flags;
			ustoren(pc,pmsg,sizeof(struct msghdr),&msg);
			lfree(lbuf,totalsize);
		}
	}
	return SC_FAKE;
}

int wrap_in_sendmsg(int sc_number,struct pcb *pc,
		struct ht_elem *hte, sysfun um_syscall)
{
	int sfd=fd2sfd(pc->fds,pc->sysargs[0]);
	if (sfd < 0) {
		pc->retval= -1;
		pc->erno= EBADF;
	} else {
		long pmsg=pc->sysargs[1];
		int flags=pc->sysargs[2];
		struct msghdr msg;
		struct msghdr lmsg;
		umoven(pc,pmsg,sizeof(struct msghdr),&msg);
		lmsg=msg;
		struct iovec liovec;
		struct iovec *iovec;
		if (msg.msg_namelen > 0 && msg.msg_name != NULL) {
			if (__builtin_expect((msg.msg_namelen > MAX_SOCKET_NAME),0)) 
				msg.msg_namelen=MAX_SOCKET_NAME;
			lmsg.msg_name=alloca(msg.msg_namelen);
			umoven(pc,(long)msg.msg_name,msg.msg_namelen,lmsg.msg_name);
		}
		if (msg.msg_iovlen > 0 && msg.msg_iov != NULL) {
			if (__builtin_expect((msg.msg_iovlen > IOV_MAX),0)) msg.msg_iovlen=IOV_MAX;
			iovec=alloca(msg.msg_iovlen * sizeof(struct iovec));
			umoven(pc,(long)msg.msg_iov,msg.msg_iovlen * sizeof(struct iovec),iovec);
		} else {
			iovec=NULL;
			msg.msg_iovlen = 0;
		}
		if (msg.msg_controllen > 0 && msg.msg_control != NULL) {
			if (__builtin_expect((msg.msg_controllen > MAX_SOCK_CONTROLLEN),0))
				msg.msg_controllen=MAX_SOCK_CONTROLLEN;
			lmsg.msg_control=alloca(msg.msg_controllen);
			umoven(pc,(long)msg.msg_control,msg.msg_controllen,lmsg.msg_control);
		}
		{
			unsigned int i,totalsize;
			char *lbuf, *p;
			for (i=0,totalsize=0;i<msg.msg_iovlen;i++)
				totalsize += iovec[i].iov_len;
			p=lbuf=(char *)lalloca(totalsize);
			liovec.iov_base=lbuf;
			liovec.iov_len=totalsize;
			lmsg.msg_iov=&liovec;
			lmsg.msg_iovlen=1;
			//printk("SNDMSG fd %d namesize %d msg_iovlen %d msg_controllen %d total %d\n",
			//		pc->sysargs[0], msg.msg_namelen, msg.msg_iovlen, msg.msg_controllen, totalsize);
			for (i=0;i<msg.msg_iovlen;i++) {
				int qty=iovec[i].iov_len;
				umoven(pc,(long)iovec[i].iov_base,qty,p);
				p += qty;
			}
			if ((pc->retval = um_syscall(sfd,&lmsg,flags)) < 0)
				pc->erno=errno;
			//printk("%d size->%d\n",sfd,size);
			lfree(lbuf,totalsize);
		}
	}
	return SC_FAKE;
}