Subject: writing to /dev/stderr

From: Alex Romosan <[email-masked]>
Date: Fri, 30 Aug 1996 09:28:11 -0500

hello,

i am trying to print to /dev/stderr from within gawk 
(print $0 > "/dev/stderr") but i get the following error:

gawk: cmd. line:30: (FILENAME=- FNR=1) fatal: can't redirect to
`/dev/stderr' (Permission denied)

i know there is something wrong with the permission, i can redirect if
i do it as root, but i don't know too much about /proc to even
understand where the problem begins. this used to work fine before
some upgrade (from unstable) but unfortunately i don't know which
package did it. if anybody has any ideas, i am willing to experiment,
maybe there is a bug in some package somewhere. thanks.

--alex--

-- 
| I believe the moment is at hand when, by a paranoiac and active |
|  advance of the mind, it will be possible (simultaneously with  |
|  automatism and other passive states) to systematize confusion  |
|  and thus to help to discredit completely the world of reality. |

---

From: Guy Maor <[email-masked]>
Date: Fri, 30 Aug 1996 12:03:35 -0500 (CDT)

> `/dev/stderr' (Permission denied)

That should work, for example
	gawk '{print $0 > "/dev/stderr";}'
copies its input to stderr.

It's not a permissions problem; /dev/std{in,out,err} and /dev/fd/n are
implemented internally by gawk.  They're not really files on the
system.  Well, actually /dev/fd/n really is a symlink into the /proc
filesystem, but gawk isn't using it.

Is your gawk really gawk, not an alias for nawk or something?  I'm not
sure if those files are supported by nawk.

You could also use /dev/fd/2 which will use the /proc system.

Guy

---

From: Alex Romosan <[email-masked]>
Date: Fri, 30 Aug 1996 13:10:26 -0500

>You could also use /dev/fd/2 which will use the /proc system.
i've tried:

echo test | gawk '{print $0 > "/dev/stderr";}'

and

echo test | gawk '{print $0 > "/dev/fd/2";}'

and both give me the same error:

gawk: cmd. line:1: (FILENAME=- FNR=1) fatal: can't redirect to
`/dev/fd/2' (Permission denied)

it works fine if i do it as root. for all it's worth i think i am
using gawk, this is the output from dpkg -i:

ii  gawk            3.0.0-2        GNU awk, a pattern scanning and processing l

maybe i should try using a different version of gawk.

--alex--

---

From: Guy Maor <[email-masked]>
Date: Fri, 30 Aug 1996 14:14:14 -0500 (CDT)

> `/dev/fd/2' (Permission denied)

Check that /dev/fd is a symbolic link to /proc/self/fd.  Check that
/proc is mounted.

The permissions to /proc are determined by the kernel, so there's
really no way that they could be wrong.

Guy

---

From: Alex Romosan <[email-masked]>
Date: Fri, 30 Aug 1996 14:44:27 -0500

>really no way that they could be wrong.
the links were okay, the problem is elsewhere. if i telnet into the
machine and log in as alex gawk works fine. what i usually do is to
rlogin from an sgi to a different account and then su to alex. under
these circumstances gawk fails with permission denied. i have no idea
if this is a bug or not.

--alex--

---

From: Guy Maor <[email-masked]>
Date: Fri, 30 Aug 1996 19:44:53 -0500 (CDT)

> these circumstances gawk fails with permission denied. i have no idea

Ok, now it's clear.  The difference between those two methods is who
owns the controlling terminal.  When you su, the old user still owns
it, so the new one can't write directly to the tty.  And /dev/fd/2 is
implemented as a link to the tty:

lrwx------   1 gmaor    gmaor          64 Aug 30 19:38 /dev/fd/2 -> [0802]:556

That means device 8,2 inode 556.  8,2 is my root partition, and 556 is
ttyp4.

556 crw--w----   1 gmaor    tty        4, 196 Aug 30 19:39 ttyp4

So if I su to someone else, I still own ttyp4 and the new user can't
write to ttyp4.

As is apparent, I was wrong about /dev/stderr being implemented
internally by gawk, at least under linux.  Using strace and from the
above, it's pretty obvious that gawk really is opening /dev/stderr,  If
it were just writing to fd 2, it would work.

This is one case where using fd 2 is not the same as writing to
/dev/stderr.

The solution is to switch user id's by doing 'exec login'.  You'll
transfer the controlling terminal also then.

Guy