Daemons in Linux

Daemons in Linux

Post by Dieter Lun » Wed, 23 Apr 2003 05:36:57



I am writing a server and want to make it a daemon.  I know this
has been asked many times before and the answer is different with
each question.  Now which would be easier using fork(), threads,
or looking at apache to see how it is done?
 
 
 

Daemons in Linux

Post by Fredrik Roube » Wed, 23 Apr 2003 05:53:20



> I am writing a server and want to make it a daemon.

http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16

Cheers // Fredrik Roubert

--
M?llev?ngsv?gen 6c  |  +46 46 188127
SE-222 40 Lund      |  http://www.df.lth.se/~roubert/

 
 
 

Daemons in Linux

Post by Chuck Dillo » Wed, 23 Apr 2003 22:41:47



> I am writing a server and want to make it a daemon.  I know this
> has been asked many times before and the answer is different with
> each question.  Now which would be easier using fork(), threads,
> or looking at apache to see how it is done?

The answer is actually quite consistent.  It depends on the
requirements for the server.

-- ced

--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

 
 
 

Daemons in Linux

Post by Duke Robillar » Fri, 09 May 2003 06:03:55



> I am writing a server and want to make it a daemon.  I know this
> has been asked many times before and the answer is different with
> each question.  Now which would be easier using fork(), threads,
> or looking at apache to see how it is done?

"daemon" is normally used to describe a specific mechanism for
building a server--it means fork() (not threads) and a number
of other things.

Here's a function that daemonises a process.

Duke

/*
  * int daemonise()
  *
  *  This function creates a child of the the current process
  *  and turns it into a daemon using setsid().  The current
  *  process dies in here.
  *
  */

#include <sys/file.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>

int daemonise()
{
   int num_files;
   int fd;

   pid_t pgid;

   int cur_file;

   /* become a child; parent dies */
   pid= fork();
   if (pid == -1) {

     /* error */
     return(1);

   } else if (pid > 0) {

     /* this is the parent.  die */
     exit(0);

   } else {

     /* this is the child, do your thing */

     /* create a new session and become session leader. */
     /* This gets rid of the controlling terminal, too */
     pgid= setsid();
     if (pgid==-1) {
       /* error */
       return(2);
     }

     /* back before setsid(), you use to do this to */
     /* get rid of the controlling terminal */
     //    if (fd= open("/dev/tty", O_RDWR|O_NOCTTY) >= 0) {
     //      ioctl(fd, TIOCNOTTY, 0);
     //      (void) close(fd);
     //    } else {
     //      return(2);
     //    }

     /* close all file descriptors */
     num_files= getdtablesize();
     for (cur_file = 0; cur_file < num_files; cur_file++)
       (void) close(cur_file);

     /* attach file descriptors 0,1,2 to /dev/null */
     (void) open("/dev/null", O_RDONLY);
     (void) open("/dev/null", O_WRONLY);
     (void) dup(1);

   }

   /* that's it, we're a daemon. Pretty cool, huh? */
   return(0);

Quote:}