Solved

Writing a Unix Daemon

Posted on 2002-07-18
7
1,697 Views
Last Modified: 2013-12-26
Can anyone give me a tutorial on how to write a linux daemon?
0
Comment
Question by:arcarson
7 Comments
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 7162463
The traditional meaning of Daemon is a program that starts when the system starts and does something useful.

However, I'm guessing you specifically mean a network daemon, one that listens on a well-known network port and responds to connections (like an HTTP server).

Assuming you have root access to the machine, the easy way to do this is by writing a program that reads stdin and writes stdout.  You then stick it in /etc/inetd.conf (/etc/inet/inetd.conf on some systems).  See the inetd.conf man page for details, though you can probably figure it out from the other entries.

If you can't use inetd, then I'm guessing this is a homework assignment...
0
 
LVL 86

Expert Comment

by:jkr
ID: 7163546
>>writing a program that reads stdin and writes stdout

Sorry, but daemons usualy close sdtin/out/err immediately after startup to "detach" - that makes the basic difference when writing such a thing...
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 100 total points
ID: 7163867
Assuming that you're writing the daemon in C/C++, you fork a child, kill the parent, fork a child, kill the parent, and become the session leader.  This will get you in daemon mode (detached process mode):

int main()
{
  // Fork and kill the parent
  if(fork() != 0)
    return 0;

  // Do it a second time to avoid zombie processes
  if(fork() != 0)
    return 0;

  setsid();

  while(true)
  {
    // Do stuff
  }

  return 0;
}
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 5

Expert Comment

by:ecw
ID: 7217153
I ws under the impression that a daemon process should not be a session group (and thus a process group) leader because if pre-chance it opened a tty without ONOCTTY, that tty would then become the controlling tty of the group.  So setsid() before the second fork.  Apart from that, the usual thing is to close all file descriptors, and chdir to /

ie.
fork() - let parent die
close all fd's
chidr("/");
setsid() (or setpgrp)
fork()
Do your stuff...

Unless strictly neccessary, try to avoid holding files open for any length of time.
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 7217170
I'm going to jump back into this one... Before we can answer the question, we have to know whether arcarson wants to actually write a 'daemon' or simply wants a network listener. All technical commentary is pointless before we know this.
0
 

Author Comment

by:arcarson
ID: 7225778
I want to write a daemon. Something that will start up when the server boots.
0
 

Expert Comment

by:nsuresh
ID: 7261609
make a deamon program using the above code fragment . To startup your program when the server starts put the command for your progam in a script in /etc/rc directory .
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question