Operating System - HP-UX
1748051 Members
5034 Online
108758 Solutions
New Discussion юеВ

Re: Curious case of HPUX popen!

 
SOLVED
Go to solution
SubhasisB
New Member

Curious case of HPUX popen!

I have a interesting case with popen and file descriptors in HPUX.

Attached are two sample "C" programs (a parent calling a child with popen). The parent opens a socket and "popens" the child program, which does a fstat/getsockname on the socket fd passed from the parent.

Funny thing is all socket descriptors except fd's 24-29 works fine. If the parents socket fd is between 24-29, then the child does not see that as a open fd at all. (I traced the system calls and seems like popen is closing fd 24-29, ie, treating these few fd's in a special way).

Does anyone have any info about this? Is this a bug? Since this behavior of popen seems to defy posix standards!

(BTW - i did a regular cc to compile these programs on hpux, where it fails. It works fine on all other unixes i tried - linux, solarix, aix etc)

Thanks in Advance.
- Subhasis
3 REPLIES 3
Dennis Handly
Acclaimed Contributor
Solution

Re: Curious case of HPUX popen!

>Does anyone have any info about this? Is this a bug?

Not really, it is fully documented. You shouldn't be using popen(3) but fork(2), exec(2) and pipe(2) directly, if you are going to be this tricky.

>I traced the system calls and seems like popen is closing fd 24-29,

If you look closely, you'll see that it is the posix shell that is closing them as documented: sh-posix(1):
WARNINGS
Some file descriptors are used internally by the POSIX shell. For HP-UX releases 10.10 and beyond, file descriptors 24 through 30 are reserved. Applications using these and forking a subshell should not depend upon them surviving in the subshell or its descendants.

Any reason you fclose stdout in the child? It's probably better to just fflush(3).
SubhasisB
New Member

Re: Curious case of HPUX popen!

Hi Denis,

Many thanks much for your detailed and informative reply. This really solves the perplexing question that was bugging me.

Best Regards,
Subhasis
SubhasisB
New Member

Re: Curious case of HPUX popen!

Dennis's reply answers my query completely. So closing this thread.

- Subhasis