File: sctab.c

package info (click to toggle)
umview 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,472 kB
  • sloc: ansic: 67,305; sh: 11,160; ruby: 914; makefile: 424; python: 141
file content (1557 lines) | stat: -rw-r--r-- 42,052 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
/*   This is iart of um-ViewOS
 *   The user-mode implementation of OSVIEW -- A Process with a View
 *
 *   sctab.c: um-ViewOS interface to capture_*
 *   
 *   Copyright 2005 Renzo Davoli University of Bologna - Italy
 *   Modified 2005 Mattia Belletti, Ludovico Gardenghi, Andrea Gasparini
 *   
 *   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: sctab.c 995 2011-08-17 14:56:22Z rd235 $
 *
 */   
#include <assert.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <sys/select.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sched.h>
#include <asm/ptrace.h>
#include <asm/unistd.h>
#include <linux/net.h>
#include <errno.h>
#include <limits.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef OLDVIRSC
#include <linux/sysctl.h>
#endif
#include <config.h>

#include "defs.h"
#include "umproc.h"
#include "services.h"
#include "um_services.h"
#include "sctab.h"
#include "um_select.h"
#include "scmap.h"
#include "utils.h"
#include "canonicalize.h"
#include "capture.h"
#include "capture_nested.h"
#include "hashtab.h"
#include "gdebug.h"

uid_t host_uid;
gid_t host_gid;

static const char *const _sys_sigabbrev[NSIG] =
{
#define init_sig(sig, abbrev, desc)   [sig] = abbrev,
#include <siglist.h>
#undef init_sig
};

/* set the errno */
void um_set_errno(struct pcb *pc,int i) {
	if (pc->flags && PCB_INUSE) 
		pc->erno=i;
	else {
		struct npcb *npc=(struct npcb *)pc;
		npc->erno=i;
	}
}

char *um_getroot(struct pcb *pc)
{
	if (pc->flags && PCB_INUSE) 
		return pc->fdfs->root;
	else
		/* nested chroot: TODO */
		return "/";
}

/* internal call: get the timestamp */
struct timestamp *um_x_gettst()
{
	struct pcb *pc=get_pcb();
	if (pc)
		return &(pc->tst);
	else
		return NULL;
}

/* set the epoch for nesting (further system calls)
 * this call returns the previous value.
 * If epoch == 0, the new epoch is not set */
epoch_t um_setnestepoch(epoch_t epoch)
{
	struct pcb *pc=get_pcb();
	epoch_t oldepoch=pc->nestepoch;
	if (epoch > 0)
		pc->nestepoch=epoch;
	return oldepoch;
}

/* internal call: load the stat info for a file */
int um_x_lstat64(char *filename, struct stat64 *buf, struct pcb *pc, int isdotdot)
{
	struct ht_elem *hte;
	int retval;
	long oldscno;
	epoch_t epoch;
	/* printk("-> um_lstat: %s %p %d\n",filename,pc->hte,isdotdot); */
	/* internal nested call save data */
	oldscno = pc->sysscno;
	pc->sysscno = NR64_lstat;
	epoch=pc->nestepoch;
	if ((hte=ht_check(CHECKPATH,filename,NULL,1)) == NULL) {
		if (pc->hte!=NULL && isdotdot) {
			pc->needs_path_rewrite=1;
			// printk("dotdot cage error %s %d\n",filename,pc->sysscno);
		}
		pc->hte=hte;
		retval = r_lstat64(filename,buf);
	} else {
		pc->hte=hte;
		retval = ht_syscall(hte,uscno(NR64_lstat))(filename,buf,-1);
	}
	/* internal nested call restore data */
	//printk("%s %lld->%lld\n",filename,epoch,pc->nestepoch);
	pc->sysscno = oldscno;
	pc->nestepoch=epoch;
	return retval;
}

static int um_stat2access(int mode, struct pcb *pc, 
		struct stat64 *stbuf, int real_uid)
{
	if (stbuf->st_mode == 0) {
		errno=ENOENT;
		return -1;
	} else if ((mode & W_OK) && (ht_get_mountflags(pc->hte) & MS_RDONLY)) {
		errno=EROFS;
		return -1;
	} else {
		uid_t uid;
		gid_t gid;
		if (pc->hte == NULL) {
			uid=host_uid;
			gid=host_gid;
		} else {
			uid=(real_uid)?pc->ruid:pc->fsuid;
			gid=(real_uid)?pc->rgid:pc->fsgid;
		}
		if (uid == 0 || secure == 0) {
			if ((mode & X_OK) && 
					!(stbuf->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
				errno=EACCES;
				return -1;
			} else
				return 0;
		} else {
			if (uid == stbuf->st_uid &&
					(((stbuf->st_mode & S_IRWXU) >> 6) & mode) == mode)
				return 0;
			else if ((gid == stbuf->st_gid ||
						in_supgrplist(stbuf->st_gid, pc)) &&
					(((stbuf->st_mode & S_IRWXG) >> 3) & mode) == mode)
				return 0;
			else if ((stbuf->st_mode & S_IRWXO & mode) == mode)
				return 0;
			else {
				errno=EACCES;
				return -1;
			}
		}
	}
}

/* internal call: check the permissions for a file,
   search for module */
int um_xx_access(char *filename, int mode, struct pcb *pc)
{
	struct ht_elem *hte;
	int retval;
	long oldscno;
	epoch_t epoch;
	/* printk("-> um_xx_access: %s\n",filename); */
	/* internal nested call save data */
	oldscno = pc->sysscno;
	epoch=pc->nestepoch;
	pc->sysscno = __NR_access;
	if ((hte=ht_check(CHECKPATH,filename,NULL,1)) == NULL)
		retval = r_access(filename,mode);
	else{
		pc->hte=hte;
		retval = ht_syscall(hte,uscno(__NR_access))(filename,mode);
	}
	/* internal nested call restore data */
	pc->sysscno=oldscno;
	pc->nestepoch=epoch;
	return retval;
}
										 
int um_parentwaccess(char *filename, struct pcb *pc)
{
	int lastslash=strlen(filename)-1;
	char *parent=filename;
	struct ht_elem *hte;
	struct stat64 stbuf;
	long oldscno;
	int retval;
	epoch_t epoch,nestepoch;
	//printk("%s \n",filename);
	while ((parent[lastslash]!='/') && (lastslash>0)) 
		lastslash--;
	if (lastslash==0)
		parent="/";
	parent[lastslash]='\0';
	//printk("-> um_parentwaccess: %s\n",parent);
	/* internal nested call save data */
	oldscno = pc->sysscno;
	epoch=pc->tst.epoch;
	nestepoch=pc->nestepoch;
	pc->sysscno = NR64_lstat;
	pc->tst.epoch=pc->nestepoch=get_epoch();
	if ((hte=ht_check(CHECKPATH,parent,NULL,0)) == NULL) {
		retval = r_lstat64(filename,&stbuf);
	} else {
		struct ht_elem *oldhte=pc->hte;
		pc->hte=hte;
		retval = ht_syscall(hte,uscno(NR64_lstat))(filename,&stbuf,-1);
		pc->hte=oldhte;
	}
	pc->sysscno = oldscno;
	pc->tst.epoch=epoch;
	pc->nestepoch=nestepoch;
	filename[lastslash]='/';
	if (retval < 0) {
		errno=EIO;
		return -1;
	}
	else if (pc->suid == stbuf.st_uid && (stbuf.st_mode & S_IWUSR))
		return 0;
	else if ((stbuf.st_mode & S_IWGRP) && 
			(pc->sgid == stbuf.st_gid || in_supgrplist(stbuf.st_gid, pc)))
		return 0;
	else if (stbuf.st_mode & S_IWOTH)
		return 0;
	else {
		errno=EACCES;
		return -1;
	}
	return 0;
}

#define FASTACCESS
/* internal call: check the permissions for a file,
   this must follow a um_x_lstat64 */
#ifdef FASTACCESS
int um_x_access(char *filename, int mode, struct pcb *pc, struct stat64 *stbuf)
{
	return um_stat2access(mode, pc, stbuf, 0);
}
#else
int um_x_access(char *filename, int mode, struct pcb *pc, struct stat64 *stbuf)
{
	int retval;
	long oldscno;
	/* printk("-> um_x_access: %s %p\n",filename,pc->hte); */
	/* internal nested call save data */
	oldscno = pc->sysscno;
	pc->sysscno = __NR_access;
	if (pc->hte == NULL)
		retval = r_access(filename,mode);
	else {
		epoch_t epoch=pc->nestepoch;
		pc->nestepoch=ht_get_epoch(pc->hte);
		retval = ht_syscall(pc->hte,uscno(__NR_access))(filename,mode);
#if 0
		{
			int test;
			test=um_stat2access(mode, pc, stbuf, 0);
			if (test != retval) 
				printk("diff %s %d -> %d %d %o %s\n",filename, mode, retval, test, pc->pathstat.st_mode, strerror(errno));
		}
#endif
		pc->nestepoch=epoch;
	}
	/* internal nested call restore data */
	pc->sysscno=oldscno;
	return retval;
}
#endif
										 
/* internal call: read a symbolic link target 
   this must follow a um_x_lstat64 */
int um_x_readlink(char *path, char *buf, size_t bufsiz, struct pcb *pc)
{
	long oldscno = pc->sysscno;
	int retval;
	/* printk("-> um_x_readlink: %s %p\n",path,pc->hte); */
	oldscno = pc->sysscno;
	pc->sysscno = __NR_readlink;
	if (pc->hte == NULL)
		retval = r_readlink(path,buf,bufsiz);
	else {
		epoch_t epoch=pc->nestepoch;
		pc->nestepoch=ht_get_epoch(pc->hte);
		retval = ht_syscall(pc->hte,uscno(__NR_readlink))(path,buf,bufsiz);
		pc->nestepoch=epoch;
	}
	/* internal nested call restore data */
	pc->sysscno = oldscno;
	return retval;
}

/* rewrite the path argument of a call */
int um_x_rewritepath(struct pcb *pc, char *path, int arg, long offset)
{
	long sp=getsp(pc);
	int pathlen=WORDALIGN(strlen(path));
	long pos=sp-(pathlen+offset);
	ustoren(pc, pos, pathlen, path);
	pc->sysargs[arg]=pos;
	return offset+pathlen;
}

/* one word string used as a tag for mistaken paths */
char um_patherror[]="PE";

/* get a path (and strdup it) from the process address space */
char *um_getpath(long laddr,struct pcb *pc)
{
	char path[PATH_MAX];
	if (umovestr(pc,laddr,PATH_MAX,path) == 0)
		return strdup(path);
	else
		return um_patherror;
}

/* utimensat has a (crazy) behavior with filename==NULL */
static char *utimensat_nullpath(int dirfd,struct pcb *pc,struct stat64 *pst,int dontfollowlink)
{
	if (dirfd==AT_FDCWD) {
		pc->erno = EFAULT;
		return um_patherror;
	} else if (dontfollowlink) {
		pc->erno = EINVAL;
		return um_patherror;
	} else {
		char *path=fd_getpath(pc->fds,dirfd);
		if (path==NULL) {
			int rv;
			path=alloca(PATH_MAX);
			snprintf(path,PATH_MAX,"/proc/%d/fd/%d",pc->pid,dirfd);
			if ((rv=readlink(path,path,PATH_MAX)) < 0) {
				pc->erno = EBADF;
				return um_patherror;
			} else
				path[rv]=0;
		}
		return strdup(path);
	}
}

/* get a path, convert it as an absolute path (and strdup it) 
 * from the process address space */
char *um_abspath(int dirfd, long laddr,struct pcb *pc,struct stat64 *pst,int dontfollowlink)
{
	char path[PATH_MAX];
	if (umovestr(pc,laddr,PATH_MAX,path) == 0) {
		char newpath[PATH_MAX];
		char *cwd;
		char *root=um_getroot(pc);
		if (dirfd==AT_FDCWD)
			cwd=pc->fdfs->cwd;
		else {
			cwd=fd_getpath(pc->fds,dirfd);
			if (cwd==NULL) {
				int rv;
				cwd=alloca(PATH_MAX);
				snprintf(cwd,PATH_MAX,"/proc/%d/fd/%d",pc->pid,dirfd);
				if ((rv=readlink(cwd,cwd,PATH_MAX)) < 0) {
					pc->erno = EBADF;
					return um_patherror;
				} else 
					cwd[rv]=0;
			}
		}
		pc->hte=NULL;
		um_realpath(path,cwd,newpath,pst,dontfollowlink,pc);
		/* View-OS core chroot management: if root is not '/' always rewrite pathnames */
		if (root[1] != 0 && pc->hte == NULL)
			pc->needs_path_rewrite=1;
		/* printk("PATH %s (%s,%s) NEWPATH %s (%p,%d) %lld\n",path,um_getroot(pc),pc->fdfs->cwd,newpath,pc->hte,pc->erno,pc->nestepoch); */
		if (pc->erno)
			return um_patherror;	//error
		else
			return strdup(newpath);
	}
	else {
		pc->erno = EINVAL;
		return um_patherror;
	}
}

/* Common framework for the dsys_{megawrap,socketwrap,virscwrap,...} - they all
 * do the usual work, but with different parameter handling.
 * What a dsys_* function do is, in general, receiving the notification of an
 * IN/OUT phase of syscall about a certain process, and decide what to do. What
 * it has to do is to deliver the notification to the correct functions.
 * This common framework asks for three more parameters to make such a
 * decision:
 * - An argument parser function (dcpa): this function extracts the arguments
 *   from the process registers/memory into our data structures (usually
 *   arg{0,1,...}, but also others - e.g., sockregs).
 * - An index function (dcif): returns the index inside the system call table
 *   that regards the current system call.
 * - A service call function (sc): the service code and system call number is
 *   given to this function, and it must return the function of the
 *   module/service which manage the syscall
 * - A system call table (sm): this is a table of sc_map entries - look at that
 *   structure for more informations.
 */
typedef void (*dsys_commonwrap_parse_arguments)(struct pcb *pc, int scno);
typedef int (*dsys_commonwrap_index_function)(struct pcb *pc, int scno);
typedef sysfun (*service_call)(struct ht_elem *hte, int scno);
int dsys_commonwrap(int sc_number,int inout,struct pcb *pc,
		dsys_commonwrap_parse_arguments dcpa,
		dsys_commonwrap_index_function dcif,
		service_call sc,
		struct sc_map *sm)
{
	/* some tmp files must be removed at the next invocation (e.g.
	 * exec creates tmp files), here is the removal */
	if (__builtin_expect(pc->tmpfile2unlink_n_free!=NULL,0)) {
		r_unlink(pc->tmpfile2unlink_n_free);
		free(pc->tmpfile2unlink_n_free);
		pc->tmpfile2unlink_n_free=NULL;
	}
	/* -- IN phase -- */
	if (inout == IN) {
		struct ht_elem *hte;
		int index;
		puterrno0(pc);
		/* timestamp the call */
		pc->tst.epoch=pc->nestepoch=get_epoch();
		/* extract argument */
		dcpa(pc, sc_number);
		/* and get the index of the system call table
		 * regarding this syscall */
		index = dcif(pc, sc_number);
		/* dotdot cage: syscalls need path rewriting when leaving
			 a virtual area by a relative .. path */
		pc->needs_path_rewrite=0;
		//printk("nested_commonwrap %d -> %lld\n",sc_number,pc->tst.epoch);
		/* looks in the system call table what is the 'choice function'
		 * and ask it the service to manage */
		pc->hte=hte=sm[index].scchoice(sc_number,pc);
		/* something went wrong during a path lookup - fake the
		 * syscall, we do not want to make it run */
		if (pc->path == um_patherror) {
			pc->retval = -1;
			return SC_FAKE;
		}
#ifdef _UM_MMAP
		/* it returns EBADF when somebody tries to access 
		 * secret files (mmap_secret) */
		if (hte == HT_ERR) {
			pc->path = um_patherror;
			pc->erno = EBADF;
			pc->retval = -1;
			return SC_FAKE;
		}
#endif
		//printk("commonwrap choice %d -> %lld %x\n",sc_number,pc->tst.epoch,hte);
		/* if some service want to manage the syscall (or the ALWAYS
		 * flag is set), we process it */
		if (hte != NULL || (sm[index].flags & ALWAYS)) {
			/* suspend management:
			 * when howsusp has at least one bit set (CB_R, CB_W, CB_X) 
			 * the system checks with a select if the call is blocking or not.
			 * when the call would be blocking the process is suspended and an event
			 * event callback is loaded.
			 */
			int howsusp = sm[index].flags & 0x7;
			int what;
			errno=0;
			if (howsusp != 0 && (what=check_suspend_on(pc,  
							pc->sysargs[0],
							howsusp))!=STD_BEHAVIOR) {
				if (what == SC_CALLONXIT) {
					if (pc->path != NULL)
						free(pc->path);
					pc->path = um_patherror;
				}
				return what;
			}
			else
				/* normal management: call the wrapin function,
				 * with the correct service syscall function */
				return sm[index].wrapin(sc_number,pc,hte,sc(hte,index));
#if 0
			int retval;
			errno=0;
			retval=sm[index].wrapin(sc_number,pc,hte,sc(hte,index));
			if (pc->erno==EUMWOULDBLOCK)
				return SC_SUSPIN;
			else
				return retval;
#endif
		}
		else {
			int retval;
			if (__builtin_expect(pc->needs_path_rewrite,0)) {
				// printk("needs_path_rewrite %s %d\n",pc->path,pc->sysscno);
				if (ISPATHARG(sm[index].nargx))
					um_x_rewritepath(pc,pc->path,PATHARG(sm[index].nargx),0);
				retval=SC_MODICALL;
			} else
				retval=STD_BEHAVIOR;
			/* we do not want to manage the syscall: free the path
			 * field in case, since we do not need it, and ask for
			 * a standard behavior */
			if (pc->path != NULL)
				free(pc->path);
			return retval;
		}
	/* -- OUT phase -- */
	} else {
		if (pc->path != um_patherror) {
			/* ok, try to call the wrapout */
			int retval;
			/* and get the index of the system call table
			 * regarding this syscall */
			int index = dcif(pc, sc_number);
			errno=0;
			/* call the wrapout */
			retval=sm[index].wrapout(sc_number,pc);
			/* check if we can free the path (not NULL and not
			 * used) */
			if (pc->path != NULL && (retval & SC_SUSPENDED) == 0)
				free(pc->path);
			return retval;
		}
		else {
			/* during the IN phase path resolution something went wrong 
			 * simply pass return value and errno to the process */
			putrv(pc->retval,pc);
			puterrno(pc->erno,pc);
			return SC_MODICALL;
		}
	}
}

#if (__NR_socketcall != __NR_doesnotexist)
/* socketcall parse arguments, (only for architectures where there is
 * one shared socketcall system call). Args must be retrieved from the
 * caller process memory */
void dsys_socketwrap_parse_arguments(struct pcb *pc, int scno)
{
	pc->private_scno = pc->sysscno | ESCNO_SOCKET;
	pc->path = NULL;
}

/* sysargs[0] is the socketcall number */
int dsys_socketwrap_index_function(struct pcb *pc, int scno)
{
	return pc->sysscno;
}

/* megawrap call for socket calls (only for architectures where there is
 * one shared socketcall system call) */
int dsys_socketwrap(int sc_number,int inout,struct pcb *pc)
{
	return dsys_commonwrap(sc_number, inout, pc,
			dsys_socketwrap_parse_arguments,
			dsys_socketwrap_index_function, ht_socketcall,
			sockmap);
}
#endif

/* virsc argument parsing function */
#ifdef OLDVIRSC
void dsys_um_virsc_parse_arguments(struct pcb *pc, int scno)
{
	struct __sysctl_args sysctlargs;
	sysctlargs.name=NULL;
	sysctlargs.nlen=0;
	pc->path = NULL;
	umoven(pc,pc->sysargs[0],sizeof(sysctlargs),&sysctlargs);
	/* private system calls are encoded with name==NULL and nlen != 0
	 * (it is usually an error case for sysctl) */
	if (sysctlargs.name == NULL && sysctlargs.nlen != 0) {
		/* virtual system call */
		if (sysctlargs.newval != NULL && sysctlargs.newlen >0 &&
				sysctlargs.newlen <= 6)
			umoven(pc,(long)(sysctlargs.newval),sysctlargs.newlen * sizeof(long), pc->sysargs);
		/* the private system call number is encoded in the nlen field */
		pc->private_scno = sysctlargs.nlen | ESCNO_VIRSC;
	} else {
		/* real sysctl, mapped on 0 */
		pc->sysargs[1] = 0;
	}
}
#else
void dsys_um_virsc_parse_arguments(struct pcb *pc, int scno)
{
	if (pc->sysargs[0] == umNULL && pc->sysargs[1] <= 6) { /* virtual syscall */
		pc->private_scno = pc->sysargs[2] | ESCNO_VIRSC;
		umoven(pc,pc->sysargs[3], pc->sysargs[1] * sizeof(long), pc->sysargs);
	} else
		pc->private_scno = ESCNO_VIRSC;
}
#endif

/* index function for sysctl: parse args above puts the 
 * number of syscall in sysargs[1]*/
int dsys_um_virsc_index_function(struct pcb *pc, int scno)
{
	return (pc->private_scno & ESCNO_MASK);
}

/* megawrap for virsc */
int dsys_um_virsc(int sc_number,int inout,struct pcb *pc)
{
	return dsys_commonwrap(sc_number, inout, pc,
			dsys_um_virsc_parse_arguments,
			dsys_um_virsc_index_function, ht_virsyscall,
			virscmap);
}

/* just the function executed by the following function (iterator) */
static void _reg_processes(struct pcb *pc,char *destination)
{
	service_ctl(MC_PROC | MC_ADD, NULL, destination, pc->umpid, (pc->pp) ? pc->pp->umpid : -1, pcbtablesize());
}

/* when a new service gets registerd all the existing processes are added
 * as a whole to the private data structures of the module, if it asked for
 * them */
static int reg_processes(char *destination)
{
	forallpcbdo(_reg_processes, destination);
	return 0;
}

/* just the function executed by the following function (iterator) */
static void _dereg_processes(struct pcb *pc,char *destination)
{
	service_ctl(MC_PROC | MC_REM, NULL, destination, pc->umpid);
}

/* when a service gets deregistered, all the data structures managed by the
 * module related to the processes must be deleted (if the module asked for
 * the processes birth history upon insertion */
static int dereg_processes(char *destination)
{
	forallpcbdo(_dereg_processes, destination);
	return 0;
}

/* UM actions for a new process entering the game*/
static void um_proc_add(struct pcb *pc)
{
	GDEBUG(0, "calling service_ctl %d %d %d %d %d %d", MC_PROC|MC_ADD, UM_NONE, -1, pc->umpid, (pc->pp)?pc->pp->umpid:-1, pcbtablesize());
	service_ctl(MC_PROC | MC_ADD, NULL, NULL, pc->umpid, (pc->pp) ? pc->pp->umpid : -1, pcbtablesize());
}

/* UM actions for a terminated process */
static void um_proc_del(struct pcb *pc)
{
	service_ctl(MC_PROC | MC_REM, NULL, NULL, pc->umpid);
}

#if 0
static mode_t local_getumask(void) {
	mode_t mask = r_umask(0);
	r_umask(mask);
	return mask;
}
#endif

/* set up all the data of the extended pcb for a new process */
void pcb_plus(struct pcb *pc,int flags,int npcflag)
{
	pc->path=pc->tmpfile2unlink_n_free=NULL;
	if (!npcflag) {
		/* CLONE_PARENT = I'm not your child, I'm your brother. So, parent is
		 * different from what we thought */
		int rootprocess=(pc->pp == pc);
		if (flags & CLONE_PARENT)
			pc->pp=pc->pp->pp;
		/* CLONE_FS = share filesystem information */
		if (flags & CLONE_FS) {
			pc->fdfs = pc->pp->fdfs;
			pc->fdfs->count++;
		} else {
			pc->fdfs =  (struct pcb_fs *) malloc(sizeof (struct pcb_fs));
			pc->fdfs->count=1;
		/* ROOT process: the first one activated by umview */
			if (rootprocess) {
				char *path=malloc(PATH_MAX);
				r_getcwd(path,PATH_MAX);
				pc->fdfs->cwd=realloc(path,strlen(path)+1);
				pc->fdfs->root=strdup("/");
				/*pc->fdfs->mask=local_getumask();*/
				pc->fdfs->mask=r_umask(0);
			} else {
				pc->fdfs->cwd=strdup(pc->pp->fdfs->cwd);
				pc->fdfs->root=strdup(pc->pp->fdfs->root);
				pc->fdfs->mask=pc->pp->fdfs->mask;
			}
		}
		if (rootprocess) {
			int ngroups=r_getgroups(0,NULL);
			r_umask(pc->fdfs->mask);
			/* create the root of the treepoch */
			pc->tst=tst_newfork(NULL);
			/* set the initial uid */
			if (secure) {
				pc->ruid=pc->euid=pc->suid=0;
				pc->rgid=pc->egid=pc->sgid=0;
				r_getresuid(NULL,&host_uid,NULL);
				r_getresgid(NULL,&host_gid,NULL);
			} else {
				r_getresuid(&pc->ruid,&pc->euid,&pc->suid);
				r_getresgid(&pc->rgid,&pc->egid,&pc->sgid);
				host_uid=pc->euid;
				host_gid=pc->egid;
			}
			pc->fsuid=pc->euid;
			pc->fsgid=pc->egid;
			pc->hte=NULL;
			pc->grouplist=supgrp_create(ngroups);
			r_getgroups(ngroups,pc->grouplist->list);
		} else {
			pc->ruid=pc->pp->ruid;
			pc->euid=pc->fsuid=pc->pp->euid;
			pc->suid=pc->pp->suid;
			pc->rgid=pc->pp->rgid;
			pc->egid=pc->fsgid=pc->pp->egid;
			pc->sgid=pc->pp->sgid;
			pc->hte=pc->pp->hte;
			pc->grouplist=supgrp_get(pc->pp->grouplist);
		}
		pc->tst=tst_newproc(&(pc->pp->tst));
#if 0
		/* if CLONE_FILES, file descriptor table is shared */
		if (flags & CLONE_FILES)
			pc->fds = pc->pp->fds;
		lfd_addproc(&(pc->fds),flags);
#endif
		um_proc_add(pc);
	}
}

/* clean up all the data structure related to a terminated process */
void pcb_minus(struct pcb *pc,int flags,int npcbflag)
{
	if (!npcbflag) {
		//printk("pcb_desctructor %d\n",pc->pid);
#if 0
		/* delete all the file descriptors */
		lfd_delproc(pc->fds);
#endif
		/* notify services */
		um_proc_del(pc);
		assert (pc->fdfs != NULL);
		/* decrement the usage couter for shared info and clean up
		 * when there are no more processes sharing the data*/
		pc->fdfs->count--;
		if (pc->fdfs->count == 0) {
			free (pc->fdfs->cwd);
			free (pc->fdfs->root);
			free (pc->fdfs);
		}
		/* notify the treepoch */
		tst_delproc(&(pc->tst));
		/*if (pc->data != NULL) {
			free(pc->data->fdfs->cwd);
			free(pc->data);
			}*/
		supgrp_put(pc->grouplist);
	}
}

/* this is the root process of a new recursive invocation for umview */
/* the process already exists, the timestamp gets converted */
int pcb_newfork(struct pcb *pc)
{
	struct treepoch *te=pc->tst.treepoch;
	pc->tst=tst_newfork(&(pc->tst));
	return (te == pc->tst.treepoch)?-1:0;
}

void pcb_getviewinfo(struct pcb *pc,struct viewinfo *vi)
{
	char *viewname;
	uname(&(vi->uname));
	vi->serverid=r_getpid();
	vi->viewid=te_getviewid(pc->tst.treepoch);
	viewname=te_getviewname(pc->tst.treepoch);
	if (viewname != NULL)
		strncpy(vi->viewname,viewname,_UTSNAME_LENGTH-1);
	else
		vi->viewname[0]=0;
}

void pcb_setviewname(struct pcb *pc,char *name)
{
	te_setviewname(pc->tst.treepoch,name);
}

struct killstruct {
	int signo;
	struct treepoch *te;
};

static void killone(struct pcb *pc, struct killstruct *ks)
{
	if (te_sameview_or_next(ks->te,pc->tst.treepoch))
	  kill(pc->pid,ks->signo);
}

void killall(struct pcb *pc, int signo)
{
	struct killstruct ks={signo, pc->tst.treepoch};
	char *viewname=te_getviewname(pc->tst.treepoch);
	viewid_t viewid=te_getviewid(pc->tst.treepoch);
	
	if (viewname)
		printk("View %d (%s): Sending processes the %s signal\n",viewid,viewname,_sys_sigabbrev[signo]);
	else
		printk("View %d: Sending processes the %s signal\n",viewid,_sys_sigabbrev[signo]);
	forallpcbdo(killone,&ks);
}
	
#if 0
int dsys_dummy(int sc_number,int inout,struct pcb *pc)
{
	if (inout == IN) {
		GDEBUG(1, "dummy diverted syscall pid %d call %d",pc->pid,sc_number);
		return STD_BEHAVIOR;
	} else {
	}
	return STD_BEHAVIOR;
}

int dsys_error(int sc_number,int inout,struct pcb *pc)
{
	GDEBUG(1, "dsys_error pid %d call %d",pc->pid,sc_number);
	pc->retval = -1;
	pc->erno = ENOSYS;
	return STD_BEHAVIOR;
}
#endif

/* choice function for system calls using a file descriptor */
struct ht_elem *choice_fd(int sc_number,struct pcb *pc)
{
	int fd=pc->sysargs[0];
	return ht_fd(pc->fds,fd,1);
}

/* choice sd (just the system call number is the choice parameter) */
struct ht_elem *choice_sc(int sc_number,struct pcb *pc)
{
	return ht_check(CHECKSC,&sc_number,NULL,1);
}

/* choice mount (mount point must be defined + filesystemtype is used
 * instead of the pathname for service selection) */
struct ht_elem *choice_mount(int sc_number,struct pcb *pc)
{
	pc->path=um_abspath(AT_FDCWD,pc->sysargs[1],pc,&(pc->pathstat),0); 

	if (pc->path!=um_patherror) {
		char filesystemtype[PATH_MAX];
		unsigned long fstype=pc->sysargs[2];
		if (umovestr(pc,fstype,PATH_MAX,filesystemtype) == 0)
			return ht_check(CHECKFSTYPE,get_alias(CHECKFSALIAS,filesystemtype),NULL,0);
		else
			return NULL;
	} else
		return NULL;
}

/* choice path (filename must be defined) */
struct ht_elem *choice_path(int sc_number,struct pcb *pc)
{
	pc->path=um_abspath(AT_FDCWD,pc->sysargs[0],pc,&(pc->pathstat),0); 
	//printk("choice_path %d %s\n",sc_number,pc->path);

	if (pc->path==um_patherror){
		/*		char buff[PATH_MAX];
					umovestr(pc,pc->sysargs[0],PATH_MAX,buff);
					printk("um_patherror: %s",buff);*/
		return NULL;
	}
	else 
		return ht_check(CHECKPATH,pc->path,&(pc->pathstat),1);
}

struct ht_elem *choice_path_exact(int sc_number,struct pcb *pc)
{
	pc->path=um_abspath(AT_FDCWD,pc->sysargs[0],pc,&(pc->pathstat),1);
	//printk("choice_path_exact %d %s\n",sc_number,pc->path);
	if (pc->path==um_patherror){
		return NULL;
	}
	else if (strcmp(pc->path,"/")==0) 
		return ht_check(CHECKPATHEXACT,"",&(pc->pathstat),1);
	else
		return ht_check(CHECKPATHEXACT,pc->path,&(pc->pathstat),1);
}

/* choice pathat (filename must be defined) */
struct ht_elem *choice_pathat(int sc_number,struct pcb *pc)
{
	pc->path=um_abspath(pc->sysargs[0],pc->sysargs[1],pc,&(pc->pathstat),0);
	if (pc->path==um_patherror)
		return NULL;
	else 
		return ht_check(CHECKPATH,pc->path,&(pc->pathstat),1);
}

/* choice sockpath (filename can be NULL) */
struct ht_elem *choice_sockpath(int sc_number,struct pcb *pc)
{
	if (pc->sysargs[0] != 0) {
		pc->path=um_abspath(AT_FDCWD,pc->sysargs[0],pc,&(pc->pathstat),0); 

		if (pc->path==um_patherror){
			/*		char buff[PATH_MAX];
						umovestr(pc,pc->sysargs[0],PATH_MAX,buff);
						printk("um_patherror: %s",buff);*/
			return NULL;
		} else
			return ht_check(CHECKPATH,pc->path,&(pc->pathstat),1);
	} else
		return ht_check(CHECKSOCKET, &(pc->sysargs[1]),NULL,1);
}

/* choice link (dirname must be defined, basename can be non-existent) */
struct ht_elem *choice_link(int sc_number,struct pcb *pc)
{
	pc->path=um_abspath(AT_FDCWD,pc->sysargs[0],pc,&(pc->pathstat),1); 
	//printk("choice_path %d %s\n",sc_number,pc->path);
	if (pc->path==um_patherror)
		return NULL;
	else 
		return ht_check(CHECKPATH,pc->path,&(pc->pathstat),1);
}

/* choice linkat (dirname must be defined, basename can be non-existent) */
struct ht_elem *choice_linkat(int sc_number,struct pcb *pc)
{
	pc->path=um_abspath(pc->sysargs[0],pc->sysargs[1],pc,&(pc->pathstat),1);
	if (pc->path==um_patherror)
		return NULL;
	else
		return ht_check(CHECKPATH,pc->path,&(pc->pathstat),1);
}

/* choice unlinkat (unlink = rmdir or unlink depending on flag) */
struct ht_elem *choice_unlinkat(int sc_number,struct pcb *pc)
{
	pc->path=um_abspath(pc->sysargs[0],pc->sysargs[1],pc,&(pc->pathstat),
			!(pc->sysargs[2] & AT_REMOVEDIR));
	if (pc->path==um_patherror)
		return NULL;
	else 
		return ht_check(CHECKPATH,pc->path,&(pc->pathstat),1);
}

/* utimensat has a (crazy) behavior with filename==NULL */
struct ht_elem *choice_utimensat(int sc_number,struct pcb *pc)
{
	if (pc->sysargs[1] == umNULL)
		pc->path=utimensat_nullpath(pc->sysargs[0],pc,&(pc->pathstat),
				pc->sysargs[3] & AT_SYMLINK_NOFOLLOW);
	else
		pc->path=um_abspath(pc->sysargs[0],pc->sysargs[1],pc,&(pc->pathstat),
				pc->sysargs[3] & AT_SYMLINK_NOFOLLOW);
	if (pc->path==um_patherror)
		return NULL;
	else
		return ht_check(CHECKPATH,pc->path,&(pc->pathstat),1);
}

/* choice path or link at (filename must be defined or can be non-existent) */
/* depending on AT_SYMLINK_NOFOLLOW on the 4th parameter */
struct ht_elem *choice_pl4at(int sc_number,struct pcb *pc)
{
	pc->path=um_abspath(pc->sysargs[0],pc->sysargs[1],pc,&(pc->pathstat),
			pc->sysargs[3] & AT_SYMLINK_NOFOLLOW);
	if (pc->path==um_patherror)
		return NULL;
	else
		return ht_check(CHECKPATH,pc->path,&(pc->pathstat),1);
}

/* depending on AT_SYMLINK_NOFOLLOW on the 5th parameter */
struct ht_elem *choice_pl5at(int sc_number,struct pcb *pc)
{
	pc->path=um_abspath(pc->sysargs[0],pc->sysargs[1],pc,&(pc->pathstat),
			pc->sysargs[4] & AT_SYMLINK_NOFOLLOW);
	if (pc->path==um_patherror)
		return NULL;
	else
		return ht_check(CHECKPATH,pc->path,&(pc->pathstat),1);
}

/* choice link (dirname must be defined, basename can be non-existent second arg)*/
struct ht_elem *choice_link2(int sc_number,struct pcb *pc)
{
	pc->path=um_abspath(AT_FDCWD,pc->sysargs[1],pc,&(pc->pathstat),1); 
	//printk("choice_link2 %d %s\n",sc_number,pc->path);
	if (pc->path==um_patherror)
		return NULL;
	else {
		struct ht_elem *hte_new=ht_check(CHECKPATH,pc->path,&(pc->pathstat),1);
		/* if NEW is real and OLD is virtual ==> EXDEV */
		if (hte_new == NULL && sc_number != __NR_symlink) {
			struct stat64 oldstat;
			char *oldpath=um_abspath(AT_FDCWD,pc->sysargs[0],pc,&oldstat,1);
			if (oldpath != um_patherror) {
				struct ht_elem *hte_old=ht_check(CHECKPATH,oldpath,&oldstat,0);
				if (hte_old != NULL) {
					free(pc->path);
					pc->path = um_patherror;
					pc->erno = EXDEV;
				}
				free(oldpath);
			}
			return NULL;
		} else
			return hte_new;
	}
}

struct ht_elem *choice_link2at(int sc_number,struct pcb *pc)
{
	pc->path=um_abspath(pc->sysargs[1],pc->sysargs[2],pc,&(pc->pathstat),1); 
	//printk("choice_path %d %s\n",sc_number,pc->path);
	if (pc->path==um_patherror)
		return NULL;
	else
		return ht_check(CHECKPATH,pc->path,&(pc->pathstat),1);
}

struct ht_elem *choice_link3at(int sc_number,struct pcb *pc)
{
	pc->path=um_abspath(pc->sysargs[2],pc->sysargs[3],pc,&(pc->pathstat),1); 
	//printk("choice_path %d %s\n",sc_number,pc->path);
	if (pc->path==um_patherror)
		return NULL;
	else {
		struct ht_elem *hte_new=ht_check(CHECKPATH,pc->path,&(pc->pathstat),1);
		/* if NEW is real and OLD is virtual ==> EXDEV */
		if (hte_new == NULL) {
			struct stat64 oldstat;
			int dontfollowlink;
			char *oldpath;
			if (sc_number == __NR_linkat)
				dontfollowlink=!(pc->sysargs[4] & AT_SYMLINK_FOLLOW);
			else
				dontfollowlink=1;
			oldpath=um_abspath(pc->sysargs[0],pc->sysargs[1],pc,&oldstat,dontfollowlink);
			if (oldpath != um_patherror) {
				struct ht_elem *hte_old=ht_check(CHECKPATH,oldpath,&oldstat,0);
				if (hte_old != NULL) {
					free(pc->path);
					pc->path = um_patherror;
					pc->erno = EXDEV;
				}
				free(oldpath);
			}
			return NULL;
		} else
			return hte_new;
	}
}

/* choice function for 'socket', usually depends on the Protocol Family */
struct ht_elem *choice_socket(int sc_number,struct pcb *pc)
{
	return ht_check(CHECKSOCKET, &(pc->sysargs[0]),NULL,1);
}

/* choice function for mmap: only *non anonymous* mmap must be mapped
 * depending on the service responsible for the fd. */
struct ht_elem *choice_mmap(int sc_number,struct pcb *pc)
{
	long fd=pc->sysargs[4];
	long flags=pc->sysargs[3];

	if (flags & MAP_ANONYMOUS)
		return NULL;
	else
		return ht_fd(pc->fds,fd,1);
}

/* dummy choice function for unimplemented syscalls */
struct ht_elem *always_null(int sc_number,struct pcb *pc)
{
	return NULL;
}

/* dummy choice function for unimplemented syscalls */
char always_umnone(int sc_number,struct pcb *pc)
{
	return UM_NONE;
}

/* preload arguments: convert socket args in case socket calls are
 * ordinary system calls */
void dsys_megawrap_parse_arguments(struct pcb *pc, int scno)
{
	pc->private_scno = pc->sysscno;
	pc->path = NULL;
}

/* index function for system call: uscno gives the index */
int dsys_megawrap_index_function(struct pcb *pc, int scno)
{
	return uscno(scno);
}

/* system call megawrap */
int dsys_megawrap(int sc_number,int inout,struct pcb *pc)
{
	return dsys_commonwrap(sc_number, inout, pc,
			dsys_megawrap_parse_arguments,
			dsys_megawrap_index_function, ht_syscall, scmap);
}

/* for modules: get the caller pid */
int um_mod_getpid()
{
	struct pcb *pc=get_pcb();
	return ((pc && (pc->flags & PCB_INUSE))?pc->pid:0);
}

/* for modules: get data from the caller process */
void um_mod_set_hte(struct ht_elem *hte)
{
	struct pcb *pc=get_pcb();
	pc->hte=hte;
}

struct ht_elem *um_mod_get_hte(void)
{
	struct pcb *pc=get_pcb();
	return pc->hte;
}

int um_mod_umoven(long addr, int len, void *_laddr)
{
	struct pcb *pc=get_pcb();
	if (pc) {
		if (pc->flags && PCB_INUSE)
			return (umoven(pc,addr,len,_laddr));
		else {
			memcpy(_laddr,(void *)addr,len);
			return 0;
		}
	}
	else 
		return -1;
}

/* for modules: get string data from the caller process */
int um_mod_umovestr(long addr, int len, void *_laddr)
{
	struct pcb *pc=get_pcb();
	if (pc) {
		if (pc->flags && PCB_INUSE)
			return (umovestr(pc,addr,len,_laddr));
		else {
			strncpy((char *)_laddr,(char *)addr, len);
			return 0;
		}
	}
	else 
		return -1;
}

/* for modules: store data to the caller process memory */
int um_mod_ustoren(long addr, int len, void *_laddr)
{
	struct pcb *pc=get_pcb();
	if (pc) {
		if (pc->flags && PCB_INUSE)
			return (ustoren(pc,addr,len,_laddr));
		else {
			memcpy((void *)addr,_laddr,len);
			return 0;
		}
	}
	else 
		return -1;
}

/* for modules: store string data to the caller process memory */
int um_mod_ustorestr(long addr, int len, void *_laddr)
{
	struct pcb *pc=get_pcb();
	if (pc) {
		if (pc->flags && PCB_INUSE)
			return (ustorestr(pc,addr,len,_laddr));
		else {
			strncpy((char *)addr,(char *)_laddr,len);
			return 0;
		}
	}
	else 
		return -1;
}

/* for modules: get the syscall number */
int um_mod_getsyscallno(void)
{
	struct pcb *pc=get_pcb();
	if (pc) {
		if (pc->flags && PCB_INUSE)
			return (pc->private_scno);
		else {
			struct npcb *npc=(struct npcb *)pc;
			return (npc->private_scno);
		}
	} else 
		return 0;
}

/* for modules: get the syscall args */
unsigned long *um_mod_getargs(void)
{
	struct pcb *pc=get_pcb();
	if (pc) {
		if (pc->flags && PCB_INUSE)
			return (pc->sysargs);
		else {
			struct npcb *npc=(struct npcb *)pc;
			return (npc->sysargs);
		}
	} else{
		return NULL;
	}
}

/* for modules: get the user-mode process id (small integer,
 * suitable for storing private data into arrays) */
int um_mod_getumpid(void)
{
	struct pcb *pc=get_pcb();
	/* this returns 0 for nested calls */
	return ((pc && (pc->flags & PCB_INUSE))?pc->umpid:0);
	/* this returns the id of the caller process that originally made the
	 * call
	 * UMPID4NESTED
	 * return pc->umpid;
	 */
}

/* for modules: get the stat info for the current path */ 
struct stat64 *um_mod_getpathstat(void)
{
	struct pcb *pc=get_pcb();
	if (pc) {
		if (pc->pathstat.st_mode == 0)
			return NULL;
		else
			return &(pc->pathstat);
	}
	else
		return NULL;
}

/* for modules: get the absolute path*/ 
char *um_mod_getpath(void)
{
	struct pcb *pc=get_pcb();
	if (pc) 
		return pc->path;
	else
		return NULL;
}

int um_mod_getresuid(uid_t *ruid, uid_t *euid, uid_t *suid)
{
	struct pcb *pc=get_pcb();
	if (pc) {
		if (ruid) *ruid=pc->ruid;
		if (euid) *euid=pc->euid;
		if (suid) *suid=pc->suid;
		return 0;
	} else
		return -1;
}

int um_mod_getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid)
{
	struct pcb *pc=get_pcb();
	if (pc) {
		if (rgid) *rgid=pc->rgid;
		if (egid) *egid=pc->egid;
		if (sgid) *sgid=pc->sgid;
		return 0;
	} else
		return -1;
}

int um_mod_setresuid(uid_t ruid, uid_t euid, uid_t suid)
{
	struct pcb *pc=get_pcb();
	if (pc) {
		if (ruid != -1) pc->ruid=ruid;
		if (euid != -1) pc->euid=euid;
		if (suid != -1) pc->suid=suid;
		return 0;
	} else
		return -1;
}

int um_mod_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
{
	struct pcb *pc=get_pcb();
	if (pc) {
		if (rgid != -1) pc->rgid=rgid;
		if (egid != -1) pc->egid=egid;
		if (sgid != -1) pc->sgid=sgid;
		return 0;
	} else
		return -1;
}

int um_mod_getfs_uid_gid(uid_t *fsuid, gid_t *fsgid)
{
	struct pcb *pc=get_pcb();
	if (pc) {
		if (fsuid) *fsuid=pc->fsuid;
		if (fsgid) *fsgid=pc->fsgid;
		return 0;
	} else
		return -1;
}

int um_mod_setfs_uid_gid(uid_t fsuid, gid_t fsgid)
{
	struct pcb *pc=get_pcb();
	if (pc) {
		if (fsuid != -1) pc->fsuid=fsuid;
		if (fsgid != -1) pc->fsgid=fsgid;
		return 0;
	} else
		return -1;
}

/* for modules: get the system call type*/ 
int um_mod_getsyscalltype(int escno)
{
	return escmapentry(escno)->setofcall;
	/*
	int usc=uscno(scno);
	if (usc >= 0) 
		return USC_TYPE(usc);
	else
		return -1;*/
}

/* for modules: get the number of syscall for this architecture
 * (not all the archs define NR_SYSCALLS*/
int um_mod_nrsyscalls(void)
{
	return _UM_NR_syscalls;
}

/* management of supplementary groups */

struct supgroups *supgrp_create(size_t size)
{
	struct supgroups *rv=malloc(sizeof(struct supgroups) + size*sizeof(gid_t));
	assert(rv != NULL);
	rv->size=size;
	rv->count=1;
	return rv;
}

/* atomic operations as different umview threads may have access to this */

struct supgroups *supgrp_get(struct supgroups *supgrp)
{
	__sync_fetch_and_add(&(supgrp->count),1);
	return supgrp;
}

void supgrp_put(struct supgroups *supgrp)
{
	int oldval = __sync_fetch_and_sub(&(supgrp->count),1);

	if (oldval == 1)
		free(supgrp);
}

int capcheck(int capability, struct pcb *pc)
{
	if (pc->euid == 0)
		return 0;
	else {
		return -1;
	}
}

/* for modules: management of module filetab. */
#define FILETABSTEP 4 /* must be a power of two */
#define FILETABSTEP_1 (FILETABSTEP-1)
static pthread_mutex_t g_filetab_mutex = PTHREAD_MUTEX_INITIALIZER;

static void **g_filetab=NULL;
static long g_filetabmax=0;
static long g_filetabsize=0;
static long g_filetabfree=-1;

int addfiletab(int size)
{
	int rv;
	pthread_mutex_lock( &g_filetab_mutex );
	if (g_filetabfree>=0) {
		rv=g_filetabfree;
		g_filetabfree=(long)(g_filetab[rv]);
	} else {
		rv=g_filetabmax++;
		if (rv>=g_filetabsize) {
			g_filetabsize=(rv + FILETABSTEP) & ~FILETABSTEP_1;
			g_filetab=realloc(g_filetab,g_filetabsize* sizeof(void *));
			assert(g_filetab);
		}
	}
	g_filetab[rv]=malloc(size);
	assert(g_filetab[rv]);
	pthread_mutex_unlock( &g_filetab_mutex );
	return rv;
}

void delfiletab(int i)
{
	free(g_filetab[i]);
	pthread_mutex_lock( &g_filetab_mutex );
	/* unused elements gets linked by re-using the void pointers
		 as the index of the next element */
	g_filetab[i]=(void *)g_filetabfree;
	g_filetabfree=i;
	pthread_mutex_unlock( &g_filetab_mutex );
}

void *getfiletab(int i)
{
	void *rv;
	pthread_mutex_lock( &g_filetab_mutex );
	rv=g_filetab[i];
	pthread_mutex_unlock( &g_filetab_mutex );
	return rv;
}

#ifdef _VIEWOS_KM
/* upcall: hashtable calls this function when an element gets added/deleted */
static void ht_zerovirt_upcall(int tag, unsigned char type,const void *obj,int objlen,long mountflags)
{
	/* ghost mount: this mount does not switch off/on zerovirt.
		 update the ghosthash table instead */
	if (type==CHECKPATH && (mountflags & MS_GHOST)) {
		switch (tag) {
			case HT_ADD: ghosthash_add(obj,objlen); break;
			case HT_DEL: ghosthash_del(obj,objlen); break;
		}
	} else {
		/* ht_count keeps track of the number of elements in the hashtable
			 for each tag*/
		static unsigned long ht_count[NCHECKS];
		int oldsum=ht_count[CHECKPATH] + ht_count[CHECKCHRDEVICE] + ht_count[CHECKBLKDEVICE];
		int newsum;
		switch (tag) {
			case HT_ADD: ht_count[type]++; break;
			case HT_DEL: ht_count[type]--; break;
		}
		newsum=ht_count[CHECKPATH] + ht_count[CHECKCHRDEVICE] + ht_count[CHECKBLKDEVICE];
		/* if this is the first (non ghost) element turn zerovirt off */
		if (oldsum == 0 && newsum == 1)
			capture_km_global_get_path_syscalls();
		/* if this is the last (non ghost) element turn zerovirt on */
		if (oldsum == 1 && newsum == 0)
			capture_km_global_skip_path_syscalls();
	}
}
#endif

/* scdtab: interface between capture_* and the wrapper (wrap-in/out)
 * implemented for each system call (see um_*.c files) */
/* capture_* call a "megawrap" that implements all the common code
 * and then forwards the call to the functions defined in scmap.c */
void scdtab_init()
{
	register int i;
	_service_init();
	/* init service management */
	service_addregfun(MC_PROC, (sysfun)reg_processes, (sysfun)dereg_processes);

#ifdef OLDVIRSC
	/* sysctl is used to define private system calls */
	scdtab[__NR__sysctl]=dsys_um_virsc;
	scdnarg[__NR__sysctl]=1;
#else
	/* pivot_root is used to define private system calls */
	scdtab[__NR_pivot_root]=dsys_um_virsc;
	scdnarg[__NR_pivot_root]=4;
#endif

	/* initialize scmap */
	init_scmap();

	/* define the megawrap for the syscalls defined in scmap */
	for (i=0; i<scmap_scmapsize; i++) {
		int scno=scmap[i].scno;
		if (scno >= 0)  {
			scdtab[scno]=dsys_megawrap;
			scdnarg[scno]=NARGS(scmap[i].nargx);
		}
	}

	/* linux has a single call for all the socket calls 
	 * in several architecture (i386, ppc), socket calls are standard
	 * system calls in others (x86_64) */
#if (__NR_socketcall != __NR_doesnotexist)
	for (i=1; i<scmap_sockmapsize; i++) 
		sockcdtab[i]=dsys_socketwrap;
#endif

	/* start umproc (file management) and define an atexit function for
	 * the final cleaning up */
	um_proc_open();
#ifdef _VIEWOS_KM
	ht_init(ht_zerovirt_upcall);
#endif
	atexit(um_proc_close);
	atexit(ht_terminate);
}