setemul.c revision 1.7 1 1.7 manu /* $NetBSD: setemul.c,v 1.7 2001/02/16 23:28:44 manu Exp $ */
2 1.1 jdolecek
3 1.1 jdolecek /*-
4 1.1 jdolecek * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 jdolecek * All rights reserved.
6 1.1 jdolecek *
7 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.1 jdolecek * modification, are permitted provided that the following conditions
9 1.1 jdolecek * are met:
10 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.1 jdolecek * documentation and/or other materials provided with the distribution.
15 1.1 jdolecek * 3. All advertising materials mentioning features or use of this software
16 1.1 jdolecek * must display the following acknowledgement:
17 1.1 jdolecek * This product includes software developed by the NetBSD
18 1.1 jdolecek * Foundation, Inc. and its contributors.
19 1.1 jdolecek * 4. Neither the name of The NetBSD Foundation nor the names of its
20 1.1 jdolecek * contributors may be used to endorse or promote products derived
21 1.1 jdolecek * from this software without specific prior written permission.
22 1.1 jdolecek *
23 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.1 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.1 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 jdolecek * POSSIBILITY OF SUCH DAMAGE.
34 1.1 jdolecek */
35 1.1 jdolecek
36 1.1 jdolecek /*
37 1.1 jdolecek * Copyright (c) 1988, 1993
38 1.1 jdolecek * The Regents of the University of California. All rights reserved.
39 1.1 jdolecek * (c) UNIX System Laboratories, Inc.
40 1.1 jdolecek * All or some portions of this file are derived from material licensed
41 1.1 jdolecek * to the University of California by American Telephone and Telegraph
42 1.1 jdolecek * Co. or Unix System Laboratories, Inc. and are reproduced herein with
43 1.1 jdolecek * the permission of UNIX System Laboratories, Inc.
44 1.1 jdolecek *
45 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
46 1.1 jdolecek * modification, are permitted provided that the following conditions
47 1.1 jdolecek * are met:
48 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
49 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
50 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
51 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
52 1.1 jdolecek * documentation and/or other materials provided with the distribution.
53 1.1 jdolecek * 3. All advertising materials mentioning features or use of this software
54 1.1 jdolecek * must display the following acknowledgement:
55 1.1 jdolecek * This product includes software developed by the University of
56 1.1 jdolecek * California, Berkeley and its contributors.
57 1.1 jdolecek * 4. Neither the name of the University nor the names of its contributors
58 1.1 jdolecek * may be used to endorse or promote products derived from this software
59 1.1 jdolecek * without specific prior written permission.
60 1.1 jdolecek *
61 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 1.1 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 1.1 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 1.1 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 1.1 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 1.1 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 1.1 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 1.1 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 1.1 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 1.1 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 1.1 jdolecek * SUCH DAMAGE.
72 1.1 jdolecek */
73 1.1 jdolecek
74 1.1 jdolecek #include <sys/cdefs.h>
75 1.1 jdolecek #ifndef lint
76 1.7 manu __RCSID("$NetBSD: setemul.c,v 1.7 2001/02/16 23:28:44 manu Exp $");
77 1.1 jdolecek #endif /* not lint */
78 1.1 jdolecek
79 1.1 jdolecek #include <sys/param.h>
80 1.1 jdolecek #include <sys/errno.h>
81 1.1 jdolecek #include <sys/time.h>
82 1.1 jdolecek
83 1.1 jdolecek #include <err.h>
84 1.1 jdolecek #include <stdio.h>
85 1.1 jdolecek #include <stdlib.h>
86 1.1 jdolecek #include <string.h>
87 1.1 jdolecek #include <unistd.h>
88 1.1 jdolecek #include <vis.h>
89 1.1 jdolecek
90 1.1 jdolecek #include "setemul.h"
91 1.1 jdolecek
92 1.1 jdolecek #include <sys/syscall.h>
93 1.1 jdolecek
94 1.1 jdolecek #include "../../sys/compat/netbsd32/netbsd32_syscall.h"
95 1.1 jdolecek #include "../../sys/compat/freebsd/freebsd_syscall.h"
96 1.1 jdolecek #include "../../sys/compat/hpux/hpux_syscall.h"
97 1.1 jdolecek #include "../../sys/compat/ibcs2/ibcs2_syscall.h"
98 1.1 jdolecek #include "../../sys/compat/linux/linux_syscall.h"
99 1.1 jdolecek #include "../../sys/compat/osf1/osf1_syscall.h"
100 1.6 mrg #include "../../sys/compat/sunos32/sunos32_syscall.h"
101 1.1 jdolecek #include "../../sys/compat/sunos/sunos_syscall.h"
102 1.1 jdolecek #include "../../sys/compat/svr4/svr4_syscall.h"
103 1.1 jdolecek #include "../../sys/compat/ultrix/ultrix_syscall.h"
104 1.1 jdolecek
105 1.1 jdolecek #define KTRACE
106 1.1 jdolecek #include "../../sys/kern/syscalls.c"
107 1.1 jdolecek
108 1.1 jdolecek #include "../../sys/compat/netbsd32/netbsd32_syscalls.c"
109 1.1 jdolecek #include "../../sys/compat/freebsd/freebsd_syscalls.c"
110 1.1 jdolecek #include "../../sys/compat/hpux/hpux_syscalls.c"
111 1.1 jdolecek #include "../../sys/compat/ibcs2/ibcs2_syscalls.c"
112 1.1 jdolecek #include "../../sys/compat/linux/linux_syscalls.c"
113 1.1 jdolecek #include "../../sys/compat/osf1/osf1_syscalls.c"
114 1.1 jdolecek #include "../../sys/compat/sunos/sunos_syscalls.c"
115 1.6 mrg #include "../../sys/compat/sunos32/sunos32_syscalls.c"
116 1.1 jdolecek #include "../../sys/compat/svr4/svr4_syscalls.c"
117 1.1 jdolecek #include "../../sys/compat/ultrix/ultrix_syscalls.c"
118 1.1 jdolecek
119 1.1 jdolecek #include "../../sys/compat/hpux/hpux_errno.c"
120 1.1 jdolecek #include "../../sys/compat/svr4/svr4_errno.c"
121 1.1 jdolecek #include "../../sys/compat/ibcs2/ibcs2_errno.c"
122 1.1 jdolecek #include "../../sys/compat/linux/common/linux_errno.c"
123 1.1 jdolecek #undef KTRACE
124 1.1 jdolecek
125 1.1 jdolecek #define NELEM(a) (sizeof(a) / sizeof(a[0]))
126 1.1 jdolecek
127 1.5 jdolecek static const struct emulation emulations[] = {
128 1.1 jdolecek { "netbsd", syscallnames, SYS_MAXSYSCALL,
129 1.1 jdolecek NULL, 0 },
130 1.1 jdolecek { "netbsd32", netbsd32_syscallnames, SYS_MAXSYSCALL,
131 1.1 jdolecek NULL, 0 },
132 1.1 jdolecek { "freebsd", freebsd_syscallnames, FREEBSD_SYS_MAXSYSCALL,
133 1.1 jdolecek NULL, 0 },
134 1.1 jdolecek { "hpux", hpux_syscallnames, HPUX_SYS_MAXSYSCALL,
135 1.1 jdolecek native_to_hpux_errno, NELEM(native_to_hpux_errno) },
136 1.1 jdolecek { "ibcs2", ibcs2_syscallnames, IBCS2_SYS_MAXSYSCALL,
137 1.1 jdolecek native_to_ibcs2_errno, NELEM(native_to_ibcs2_errno) },
138 1.1 jdolecek { "linux", linux_syscallnames, LINUX_SYS_MAXSYSCALL,
139 1.1 jdolecek native_to_linux_errno, NELEM(native_to_linux_errno) },
140 1.1 jdolecek { "osf1", osf1_syscallnames, OSF1_SYS_MAXSYSCALL,
141 1.6 mrg NULL, 0 },
142 1.6 mrg { "sunos32",sunos32_syscallnames, SUNOS32_SYS_MAXSYSCALL,
143 1.1 jdolecek NULL, 0 },
144 1.1 jdolecek { "sunos", sunos_syscallnames, SUNOS_SYS_MAXSYSCALL,
145 1.1 jdolecek NULL, 0 },
146 1.1 jdolecek { "svr4", svr4_syscallnames, SVR4_SYS_MAXSYSCALL,
147 1.1 jdolecek native_to_svr4_errno, NELEM(native_to_svr4_errno) },
148 1.1 jdolecek { "ultrix", ultrix_syscallnames, ULTRIX_SYS_MAXSYSCALL,
149 1.1 jdolecek NULL, 0 },
150 1.3 itohy { "pecoff", syscallnames, SYS_MAXSYSCALL,
151 1.3 itohy NULL, 0 },
152 1.1 jdolecek { NULL, NULL, 0,
153 1.1 jdolecek NULL, 0 }
154 1.1 jdolecek };
155 1.1 jdolecek
156 1.1 jdolecek struct emulation_ctx {
157 1.1 jdolecek pid_t pid;
158 1.5 jdolecek const struct emulation *emulation;
159 1.1 jdolecek struct emulation_ctx *next;
160 1.1 jdolecek };
161 1.1 jdolecek
162 1.5 jdolecek const struct emulation *current;
163 1.7 manu const struct emulation *previous;
164 1.1 jdolecek
165 1.5 jdolecek static const struct emulation *default_emul=NULL;
166 1.1 jdolecek
167 1.1 jdolecek struct emulation_ctx *current_ctx;
168 1.1 jdolecek struct emulation_ctx *emul_ctx = NULL;
169 1.1 jdolecek
170 1.1 jdolecek static struct emulation_ctx *ectx_find __P((pid_t));
171 1.5 jdolecek static void ectx_update __P((pid_t, const struct emulation *));
172 1.1 jdolecek
173 1.1 jdolecek void
174 1.1 jdolecek setemul(name, pid, update_ectx)
175 1.1 jdolecek const char *name;
176 1.1 jdolecek pid_t pid;
177 1.1 jdolecek int update_ectx;
178 1.1 jdolecek {
179 1.1 jdolecek int i;
180 1.5 jdolecek const struct emulation *match = NULL;
181 1.1 jdolecek
182 1.1 jdolecek for (i = 0; emulations[i].name != NULL; i++) {
183 1.1 jdolecek if (strcmp(emulations[i].name, name) == 0) {
184 1.1 jdolecek match = &emulations[i];
185 1.1 jdolecek break;
186 1.1 jdolecek }
187 1.1 jdolecek }
188 1.1 jdolecek
189 1.1 jdolecek if (!match) {
190 1.1 jdolecek warnx("Emulation `%s' unknown", name);
191 1.1 jdolecek return;
192 1.1 jdolecek }
193 1.1 jdolecek
194 1.1 jdolecek if (update_ectx)
195 1.1 jdolecek ectx_update(pid, match);
196 1.1 jdolecek
197 1.1 jdolecek if (!default_emul)
198 1.1 jdolecek default_emul = match;
199 1.7 manu
200 1.7 manu if (current != NULL)
201 1.7 manu previous = current;
202 1.7 manu else
203 1.7 manu previous = match;
204 1.1 jdolecek
205 1.1 jdolecek current = match;
206 1.1 jdolecek }
207 1.1 jdolecek
208 1.1 jdolecek /*
209 1.1 jdolecek * Emulation context list is very simple chained list, not even hashed.
210 1.1 jdolecek * We expect the number of separate traced contexts/processes to be
211 1.1 jdolecek * fairly low, so it's not worth it to optimize this.
212 1.1 jdolecek */
213 1.1 jdolecek
214 1.1 jdolecek /*
215 1.1 jdolecek * Find an emulation context appropriate for the given pid.
216 1.1 jdolecek */
217 1.1 jdolecek static struct emulation_ctx *
218 1.1 jdolecek ectx_find(pid)
219 1.1 jdolecek pid_t pid;
220 1.1 jdolecek {
221 1.1 jdolecek struct emulation_ctx *ctx;
222 1.1 jdolecek
223 1.1 jdolecek for(ctx = emul_ctx; ctx != NULL; ctx = ctx->next) {
224 1.1 jdolecek if (ctx->pid == pid)
225 1.1 jdolecek return ctx;
226 1.1 jdolecek }
227 1.1 jdolecek
228 1.1 jdolecek return NULL;
229 1.1 jdolecek }
230 1.1 jdolecek
231 1.1 jdolecek /*
232 1.1 jdolecek * Update emulation context for given pid, or create new if no context
233 1.1 jdolecek * for this pid exists.
234 1.1 jdolecek */
235 1.1 jdolecek static void
236 1.1 jdolecek ectx_update(pid, emul)
237 1.1 jdolecek pid_t pid;
238 1.5 jdolecek const struct emulation *emul;
239 1.1 jdolecek {
240 1.1 jdolecek struct emulation_ctx *ctx;
241 1.1 jdolecek
242 1.1 jdolecek
243 1.1 jdolecek if ((ctx = ectx_find(pid)) != NULL) {
244 1.1 jdolecek /* found and entry, ensure the emulation is right (exec!) */
245 1.1 jdolecek ctx->emulation = emul;
246 1.1 jdolecek return;
247 1.1 jdolecek }
248 1.1 jdolecek
249 1.1 jdolecek ctx = (struct emulation_ctx *)malloc(sizeof(struct emulation_ctx));
250 1.1 jdolecek ctx->pid = pid;
251 1.1 jdolecek ctx->emulation = emul;
252 1.1 jdolecek
253 1.1 jdolecek /* put the entry on start of emul_ctx chain */
254 1.1 jdolecek ctx->next = emul_ctx;
255 1.1 jdolecek emul_ctx = ctx;
256 1.1 jdolecek }
257 1.1 jdolecek
258 1.1 jdolecek /*
259 1.1 jdolecek * Ensure current emulation context is correct for given pid.
260 1.1 jdolecek */
261 1.1 jdolecek void
262 1.1 jdolecek ectx_sanify(pid)
263 1.1 jdolecek pid_t pid;
264 1.1 jdolecek {
265 1.1 jdolecek struct emulation_ctx *ctx;
266 1.1 jdolecek
267 1.4 jdolecek if ((ctx = ectx_find(pid)) != NULL)
268 1.1 jdolecek current = ctx->emulation;
269 1.2 jdolecek else if (default_emul)
270 1.2 jdolecek current = default_emul;
271 1.1 jdolecek else
272 1.2 jdolecek current = &emulations[0]; /* NetBSD */
273 1.1 jdolecek }
274