Solved

Writing a Unix Daemon

Posted on 2002-07-18
7
1,695 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 29
VB.NET how to use the Vertical ScrollBar 5 90
bobThere java chalenge 45 91
Expand data scrubbing tool 13 32
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

24 Experts available now in Live!

Get 1:1 Help Now