Hi
I am having this code in a server
if ((fd = open (path, O_RDWR , 0660)) == FAIL)
{
FATAL_EXIT ();
}
else if (fd <= 2)/* stdio */
{
FATAL_EXIT ();
}
it does not always go into the "else if" part , but sometimes it does.
what i need to know is why does it go into elseif even though i havent
closed any of the stdout,stderr or stdin streams !!
Regards,
Gaurav Kadyan
|
|
0
|
|
|
|
Reply
|
gauravkadyan (1)
|
7/19/2006 8:12:05 AM |
|
gauravkadyan@gmail.com wrote:
> if ((fd = open (path, O_RDWR , 0660)) == FAIL)
> {
> FATAL_EXIT ();
> }
> else if (fd <= 2)/* stdio */
> {
> FATAL_EXIT ();
> }
> it does not always go into the "else if" part , but sometimes it does.
Gaurav,
open() is required to return the smallest available
filedescriptor on success. So, if it returs something in the
0,1,2 range, then this filedescriptor was available (meaning,
was not opened before this call to open). It is not necessarily
YOU who closed this filedescriptor, it can be either inherited
from the parentprocess, or elsewhere in the coding of your
server-process.
Don't forget, the fd's 0,1,2 are nothing special from the viewpoint
of low-level-IO. They can be used as all the other fds. Only
high-level-IO treats them as stdin, stdout,stderr. A programm that
doesn't use highlevel-IO can (in principle) use them for low-level-IO.
Warning: It is wise for a program that uses only low-level-IO (or that
doesn't use stdin.stdout,stderr) to close these fd's and open them on
/dev/null ... to avoid that they are used for normal low-level-IO.
This seems to contradict what I wrote before. Well, the reason is, that
there might be still some sick library-routines left, that use stderr to
output something. (I remember a "domain error" appearing on stderr, when
one of the exponential functions was called with an invalid argiment).
Rainer
|
|
0
|
|
|
|
Reply
|
Rainer
|
7/19/2006 8:21:36 AM
|
|
gauravkadyan@gmail.com writes:
> I am having this code in a server
>
> if ((fd = open (path, O_RDWR , 0660)) == FAIL)
> {
> FATAL_EXIT ();
> }
> else if (fd <= 2)/* stdio */
> {
> FATAL_EXIT ();
> }
>
> it does not always go into the "else if" part , but sometimes it does.
> what i need to know is why does it go into elseif even though i havent
> closed any of the stdout,stderr or stdin streams !!
The stdin=0,stdout=1,stderr=2 convention really depends on the calling
process. That's what is usually done by the shells, but what about an
application launched from a GUI manager? Or merely, a process
fork/exec'ed by a random program.
--
__Pascal Bourguignon__ http://www.informatimago.com/
PUBLIC NOTICE AS REQUIRED BY LAW: Any use of this product, in any
manner whatsoever, will increase the amount of disorder in the
universe. Although no liability is implied herein, the consumer is
warned that this process will ultimately lead to the heat death of
the universe.
|
|
0
|
|
|
|
Reply
|
Pascal
|
7/19/2006 8:24:10 AM
|
|
Pascal Bourguignon wrote:
> The stdin=0,stdout=1,stderr=2 convention really depends on the calling
> process.
Yes, a point that is often forgotten ...
look at this small snippet of code:
$ cat x.c
#include <stdio.h>
int main(int c,char **v)
{
FILE *f;
int fd;
fclose(stdin);
fd=open("x.c",1);
printf("fd=%d\n",fd);
f=freopen(v[1],"r",stdin);
printf("f=%p stdin=%p fileno=%d\n",f,stdin,fileno(f));
}
$ x x.c
fd=0
f=0x40132720 stdin=0x40132720 fileno=3
first stdin is fclose()d
second an fd is opened on "x.c"
it gets fd==0 (to be expected)
stdin is reopened
the output shows, that f==stdin, and that now the fd associated with
stdin is no longer fd==0 but fd==3
Rainer
|
|
0
|
|
|
|
Reply
|
Rainer
|
7/19/2006 8:40:55 AM
|
|
So that should mean that somewhere the code is actually closing the
stdio related fds !!
i will now investigate in that direction.
Thanks for your replies,
Gaurav Kadyan
Rainer Temme wrote:
> Pascal Bourguignon wrote:
> > The stdin=0,stdout=1,stderr=2 convention really depends on the calling
> > process.
>
>
> Yes, a point that is often forgotten ...
>
> look at this small snippet of code:
>
> $ cat x.c
>
> #include <stdio.h>
>
> int main(int c,char **v)
> {
> FILE *f;
> int fd;
>
> fclose(stdin);
> fd=open("x.c",1);
> printf("fd=%d\n",fd);
> f=freopen(v[1],"r",stdin);
> printf("f=%p stdin=%p fileno=%d\n",f,stdin,fileno(f));
> }
>
> $ x x.c
> fd=0
> f=0x40132720 stdin=0x40132720 fileno=3
>
> first stdin is fclose()d
> second an fd is opened on "x.c"
> it gets fd==0 (to be expected)
> stdin is reopened
> the output shows, that f==stdin, and that now the fd associated with
> stdin is no longer fd==0 but fd==3
>
> Rainer
|
|
0
|
|
|
|
Reply
|
GauravKadyan
|
7/19/2006 10:03:00 AM
|
|
GauravKadyan wrote:
> So that should mean that somewhere the code is actually closing the
> stdio related fds !!
> i will now investigate in that direction.
If it only happens after awhile, then yes. If it happens immediately,
then you started out with the stdio related fds available and your
first 'open' got the lowest available 'fd'.
You should definitely sanitize your environment when your code starts
up. One thing to make sure is that fd's 0, 1, and 2 are in use. If not,
you can open "/dev/null" and put it there. You may also want to 'close'
any other fds you inherited if you weren't expecting them.
*If* getting <= 2 is a problem for you, then you need to make sure that
doesn't happen by burning fd's 0, 1, and 2 at process startup if they
are not in use. If that's not a problem, don't test for it. ;)
DS
|
|
0
|
|
|
|
Reply
|
davids
|
7/20/2006 10:36:20 AM
|
|
|
5 Replies
181 Views
(page loaded in 0.221 seconds)
Similiar Articles: macro or vb app to set view scale factor equal to drawing scale ...Trying to return the scale of a drawing view... - solidworks macro or vb app to set view scale factor equal to drawing scale factor ... scale factor equal to the drawing ... how to calcule the division modulo 2 reminder - comp.sys.hp48 ...open returns lessthan equal to 2 - comp.unix.programmer how to calcule the division modulo 2 reminder - comp.sys.hp48 ... Hi Veli, 2 MODSTO DIV2MOD doesn't return the ... Yule-Walker and Levinson-Durbin Algorithm - comp.soft-sys.matlab ...open returns lessthan equal to 2 - comp.unix.programmer Yule-Walker and Levinson-Durbin Algorithm - comp.soft-sys.matlab ..... Does any body know how to open the m ... question about execl(), fork(), stdin, stdout, stderr - comp.unix ...open( STDOUT, ">/dev/null") || die $!; open( STDERR, ">/dev/null") || die $!; ... dev/null", "2>&1", NULL); > > if( n == -1) /* execl failed */ > } else return ... How to test if stdout == stderr? - comp.unix.programmerImagine: close(1); close(2); creat("/tmp/out-and-err",0600); open("/tmp/out-and-err",O_WRONLY,0 ... different" : "equivilant" ); > > return 0; > } It ... How to exec "login" from the lowest level "shell" - comp.unix ...open returns lessthan equal to 2 - comp.unix.programmer Or merely, a process fork/exec'ed by a random program. ... fds available and your first 'open' got the lowest ... OpenBoot Prom / 'go' Question - comp.unix.solarisIn less than 1/2 day (on site? tele-commute?) they should be able to get ... open returns lessthan equal to 2 - comp.unix.programmer OpenBoot Prom / 'go' Question - comp ... delete columns csv file - comp.lang.awkReaders, I have a csv file (greater than 256 columns hence unable to open ... As long as i is less than or > equal to the total number of fields, or columns, add a value 1 ... How does FD_ISSET() return 0 after returned non-zero? - comp.unix ...... fd, &fdset) will be always equal to non-zero in the next checks of select return ... parachutes, function best when open ... set by alarm() From the read(2) manpage: RETURN ... Detect interface failure. - comp.unix.programmerHi: I have two interface cards A1 and A2 with addresses IP1 and IP2. I open a RAW socket ... programmer... switches will be equal ... experiment and find that PING returns ... Equal, Greater or Less Than - Math is Fun - Maths ResourcesEqual, Greater or Less Than. As well as the familiar equals sign (=) it is also very useful to show if something is not equal to (≠) greater than (>) or less than (<) Inequality (mathematics) - Wikipedia, the free encyclopediaIn mathematics, an inequality is a relation that holds between two values when they are different (see also: equality). The notation a ≠ b means that a is not equal ... 7/24/2012 1:36:23 AM
|