LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] don't pass open file descriptor to children

[ILUG] don't pass open file descriptor to children

Colm Buckley colm at tuatha.org
Fri Jul 13 17:18:50 IST 2001


On Friday 13 July 2001 17:07, Padraig Brady wrote:

> Oops should have been a bit more descriptive (pardon the pun).
> This has to be general (I don't have to source to the children).

I think you're confusing the process of exec() with that of fork()
(confusing processes with programs).  If you're writing the *parent*,
you *do* have access to the source of the child processes, because
after a fork(), the child process is running the *same* *code* as the
parent.  It's only after an exec(), which loads a new *program* into
the current *process*, that you've lost control of the source.

This should work:

            switch (pid = fork()) {
		   case -1 : /* error */
		   perror "fork() failed";
		   exit();
		   break;

		   case 0 : /* child process */
		   /* close FDs we don't want to pass to the new
		   program */
		   close(fd0);
		   close(fd1);
		   /* now run the new program */
		   execp("/path/to/program", "program", "arg1", "arg2", NULL);
		   perror("exec() failed");
		   break;

		   default : /* parent */
		   printf("Child launched with pid %d\n", pid);
		   break;
            }

Alternatively, as has been mentioned, you can use fcntl() to set the
F_SETFD flag on the file descriptors, which means they would be
automatically closed as soon as the exec() was done.

        Colm

-- 
Colm Buckley @ NewWorld Commerce
Business: +353 1 4334334  / colm at nwcgroup.com / http://www.nwcgroup.com/
Personal: +353 87 2469146 / colm at tuatha.org / http://www.tuatha.org/~colm/
A fool and his money are soon partying.




More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell