summaryrefslogtreecommitdiff
path: root/emulators/ski/patches/patch-aj
blob: 4a6f2e5ec37c6e36379a07e1d8bb4d5529c27a6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
$NetBSD: patch-aj,v 1.1.1.1 2011/07/27 10:23:49 cherry Exp $

--- src/netbsd/misc-netbsd.c.orig	2009-12-16 12:47:32.000000000 +0000
+++ src/netbsd/misc-netbsd.c
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 1995-2007, Hewlett-Packard Development Company, L.P.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or 
+ * (at your option) any later version.
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <sys/time.h>
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "std.h"
+#include "types.h"
+#include "state.h"
+
+#include "netbsd/machdep-netbsd.h"
+
+int _close(int);
+int _open(const char *, int, int);
+
+CTR prof_insts = ~0ULL;
+char *src_root;
+
+#define	SKI_NFDS	20
+int fdlimit;
+int fdmap[SKI_NFDS];
+
+void setFdmap(int fd1, int fd2)
+{
+	assert(0);
+}
+
+void profCnt(void)
+{
+	assert(0);
+}
+
+void saveOpenFiles(FILE *f)
+{
+}
+
+void restoreOpenFile(char *name, unsigned flag, unsigned mode, unsigned offset)
+{
+}
+
+/*
+ * We use our own implementation of open to force file descriptors
+ * opened by ski for ski to be larger than what we allow for the
+ * simulated process. This avoids nasty mappings...
+ */
+int open(const char *path, int flags, int mode)
+{
+	int res, fd;
+
+	if (!unixABI)
+		return (_open(path, flags, mode));
+
+	if (fdlimit == 0) {
+		fdlimit = getdtablesize() - SKI_NFDS;
+		if (fdlimit < 20) {
+			fprintf(stderr, "FD table size underflow\n");
+			abort();
+		}
+		dup2(0, fdlimit);
+		dup2(1, fdlimit + 1);
+		dup2(2, fdlimit + 2);
+		fdmap[0] = fdmap[1] = fdmap[2] = 1;
+		/* XXX we shouldn't go under the hood */
+		stdin->_file += fdlimit;
+		stdout->_file += fdlimit;
+		stderr->_file += fdlimit;
+	}
+
+	res = _open(path, flags, mode);
+	if (res >= fdlimit) {
+		_close(res);
+		errno = EMFILE;
+		return (-1);
+	} else if (res == -1)
+		return (res);
+
+	fd = 0;
+	while (fd < SKI_NFDS && fdmap[fd])
+		fd++;
+	if (fd == SKI_NFDS) {
+		_close(res);
+		errno = EMFILE;
+		return (-1);
+	}
+	if (dup2(res, fd + fdlimit) == -1) {
+		_close(res);
+		errno = EMFILE;
+		return (-1);
+	}
+	_close(res);
+	fdmap[fd] = 1;
+	return (fd + fdlimit);
+}
+
+int close(int fd)
+{
+	if (fd >= fdlimit && fdlimit != 0)
+		fdmap[fd - fdlimit] = 0;
+	_close(fd);
+}