Solved

Writing a Unix Daemon

Posted on 2002-07-18
7
1,692 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now