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] C Questions

[ILUG] C Questions

Mark Fallon mfallon at ie.oracle.com
Mon Jan 24 12:23:05 GMT 2000


William Murphy wrote:
> 
> Hi all,
> 
> Just having one or two problems writing  a little network daemon:
> 
> 1. Most programs in the inetutils distribution seem to lose their
> controlling terminals as follows:
> 
>     int    tty = open( "/dev/tty", O_RDWR );
>     ioctl( tty, TIOCNOTTY );
>     close( tty );
> 
> All of the function calls proceed successfully, but the program stays in
> the foreground. The alternative of fork()ing seems kinda like cheating.
> Any ideas?
> 

When writing something like this I find myself using something
like this from Stevens:

----------------------------------------------------------------------------
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int daemon_init(){
  pid_t pid;

  if ((pid = fork()) < 0){
    return(-1);			/* error on fork */
  } else {
    if (pid != 0){
      exit(0);			/* parent exits */
    }
  }

  setsid();			/* Become Session leader */

  chdir("/");			/* Change working directory */

  umask(0);			/* Clear file mode creation mask */

  return(0);
}
--------------------------------------------------------------------------


1) Calling fork and having the parent exit does several things, it makes
   the shell think the command has finished. Second it insures that the
   child process is not a process group leader (prerequisite for
setsid).

2) Setsid means that the process has the following three properties:
	1) Session leader of new session
	2) Process group leader of new group
	3) No controlling terminal

3) This moves the process off any mounted file systems. Though a printer
   daemon might want to make the spooling directory the current
directory

4) Set umask to 0. The file mode creation mask inherited could be set to
   deny certain permissions. The daemon might want to create files with
   different permissions than that of the user launching it. So this
   should be set accordingly.

5) Close all unwanted file descriptors. Remember a child inherits the
   file descriptors of the parent.  


Have to think about the other question.

Regards,

Mark

-- 
_______________________________________________________________

Mark Fallon			E-mail : mfallon at ie.oracle.com
Senior Software Engineer	Phone  : +353-1-8033207
Product Line Engineering	Fax    : +353-1-8033221
_______________________________________________________________




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