Message ID | 20151117133154.72b61a52@pinnacle.lan |
---|---|
State | Superseded |
Delegated to: | Mike Frysinger |
Headers | show |
On 17 Nov 2015 13:31, Kevin Buettner wrote: > On Tue, 17 Nov 2015 00:41:33 -0500 Mike Frysinger wrote: > > > So sim/common is doing the same thing as my proposed patch for ppc; > > > sim/common is just using a more elegant mechanism to avoid calling > > > close() on these three file descriptors. > > > > the difference is that this code sequence misbehaves after your change: > > close(1); > > write(1, "foo", 3); > > under the common sim, the write will return EBADF. > > > > considering how much of common/ came from ppc/ i'm a little surprised > > virtualization of the fd table didn't. > > > > it would be nice if we could at least hide these three fds (a static > > array of 3 bools maybe?), but i won't push hard for you to do that. > > Do you mean an array which indicates the open / closed status of each > of stdin, stdout, and stderr? This status would then be used to > return EBADF in the right places when the descriptor is "closed". correct. maybe add a helper func like the common code does, and then have every wrapper (read, write, etc...) check that before making the actual call. i don't think we have to get fancy and preserve exact behavior since the standard does not require it; i.e. this code: close(2) int fd = open(...) fd would normally be 2, but since we haven't really closed it, you'd get back a higher fd. -mike
diff --git a/sim/ppc/emul_netbsd.c b/sim/ppc/emul_netbsd.c index 6bef370..5e39975 100644 --- a/sim/ppc/emul_netbsd.c +++ b/sim/ppc/emul_netbsd.c @@ -440,8 +440,13 @@ do_close(os_emul_data *emul, SYS(close); - /* Can't combine these statements, cuz close sets errno. */ - status = close(d); + /* Do not close stdin, stdout, or stderr. GDB may still need access to + these descriptors. */ + if (d == 0 || d == 1 || d == 2) + status = 0; + else + status = close(d); + emul_write_status(processor, status, errno); } diff --git a/sim/ppc/emul_unix.c b/sim/ppc/emul_unix.c index d72525d..47b2b98 100644 --- a/sim/ppc/emul_unix.c +++ b/sim/ppc/emul_unix.c @@ -310,7 +310,11 @@ do_unix_close(os_emul_data *emul, if (WITH_TRACE && ppc_trace[trace_os_emul]) printf_filtered ("%d", d); - status = close(d); + if (d == 0 || d == 1 || d == 2) + status = 0; + else + status = close(d); + emul_write_status(processor, status, errno); }