Sun Jan 9 10:28:46 2011 UTC ()
dprintf to stderr.  stop doing it if stderr_fileno gets dup2()'d


(pooka)
diff -r1.4 -r1.5 src/lib/librumphijack/hijack.c

cvs diff -r1.4 -r1.5 src/lib/librumphijack/hijack.c (expand / switch to unified diff)

--- src/lib/librumphijack/hijack.c 2011/01/08 21:30:24 1.4
+++ src/lib/librumphijack/hijack.c 2011/01/09 10:28:46 1.5
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: hijack.c,v 1.4 2011/01/08 21:30:24 pooka Exp $ */ 1/* $NetBSD: hijack.c,v 1.5 2011/01/09 10:28:46 pooka Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2011 Antti Kantee. All Rights Reserved. 4 * Copyright (c) 2011 Antti Kantee. All Rights Reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright 11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the 12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution.
14 * 14 *
@@ -16,27 +16,27 @@ @@ -16,27 +16,27 @@
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE. 25 * SUCH DAMAGE.
26 */ 26 */
27 27
28#include <sys/cdefs.h> 28#include <sys/cdefs.h>
29__RCSID("$NetBSD: hijack.c,v 1.4 2011/01/08 21:30:24 pooka Exp $"); 29__RCSID("$NetBSD: hijack.c,v 1.5 2011/01/09 10:28:46 pooka Exp $");
30 30
31#include <sys/param.h> 31#include <sys/param.h>
32#include <sys/types.h> 32#include <sys/types.h>
33#include <sys/ioctl.h> 33#include <sys/ioctl.h>
34#include <sys/socket.h> 34#include <sys/socket.h>
35#include <sys/poll.h> 35#include <sys/poll.h>
36 36
37#include <rump/rump.h> 37#include <rump/rump.h>
38#include <rump/rumpclient.h> 38#include <rump/rumpclient.h>
39#include <rump/rump_syscalls.h> 39#include <rump/rump_syscalls.h>
40 40
41#include <assert.h> 41#include <assert.h>
42#include <dlfcn.h> 42#include <dlfcn.h>
@@ -149,36 +149,49 @@ rcinit(void) @@ -149,36 +149,49 @@ rcinit(void)
149 149
150 snprintf(sysname, sizeof(sysname), "rump_sys_%s", sysnames[i]); 150 snprintf(sysname, sizeof(sysname), "rump_sys_%s", sysnames[i]);
151 rumpcalls[i] = dlsym(hand, sysname); 151 rumpcalls[i] = dlsym(hand, sysname);
152 if (!rumpcalls[i]) { 152 if (!rumpcalls[i]) {
153 fprintf(stderr, "cannot find symbol: %s\n", sysname); 153 fprintf(stderr, "cannot find symbol: %s\n", sysname);
154 exit(1); 154 exit(1);
155 } 155 }
156 } 156 }
157 157
158 if (rumpcinit() == -1) 158 if (rumpcinit() == -1)
159 err(1, "rumpclient init"); 159 err(1, "rumpclient init");
160} 160}
161 161
 162static unsigned dup2mask;
 163#define ISDUP2D(fd) (((fd+1) & dup2mask) == ((fd)+1))
 164
162//#define DEBUGJACK 165//#define DEBUGJACK
163#ifdef DEBUGJACK 166#ifdef DEBUGJACK
164#define DPRINTF(x) printf x 167#define DPRINTF(x) mydprintf x
 168static void
 169mydprintf(const char *fmt, ...)
 170{
 171 va_list ap;
 172
 173 if (ISDUP2D(STDERR_FILENO))
 174 return;
 175
 176 va_start(ap, fmt);
 177 vfprintf(stderr, fmt, ap);
 178 va_end(ap);
 179}
 180
165#else 181#else
166#define DPRINTF(x) 182#define DPRINTF(x)
167#endif 183#endif
168 184
169static unsigned dup2mask; 
170#define ISDUP2D(fd) (((fd+1) & dup2mask) == ((fd)+1)) 
171 
172/* XXX: need runtime selection. low for now due to FD_SETSIZE */ 185/* XXX: need runtime selection. low for now due to FD_SETSIZE */
173#define HIJACK_FDOFF 128 186#define HIJACK_FDOFF 128
174#define HIJACK_SELECT 128 /* XXX */ 187#define HIJACK_SELECT 128 /* XXX */
175#define HIJACK_ASSERT 128 /* XXX */ 188#define HIJACK_ASSERT 128 /* XXX */
176static int 189static int
177fd_rump2host(int fd) 190fd_rump2host(int fd)
178{ 191{
179 192
180 if (fd == -1) 193 if (fd == -1)
181 return fd; 194 return fd;
182 195
183 if (!ISDUP2D(fd)) 196 if (!ISDUP2D(fd))
184 fd += HIJACK_FDOFF; 197 fd += HIJACK_FDOFF;