Solved

Writing a Unix Daemon

Posted on 2002-07-18
7
1,701 Views
Last Modified: 2013-12-26
Can anyone give me a tutorial on how to write a linux daemon?
0
Comment
Question by:arcarson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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