NETB352

Daemons

Zlatin Stanimirov, F64571

Table of contents:

  • Definition
  • Purpose
  • Steps to create a daemon
  • Alternatives
  • Daemons in practice
  • Questions 1
  • Questions 2

Definition

In multitasking computer operating systems, a daemon (/ˈdiːmən/ or /ˈdeɪmən/)[1] is a computer program that runs as a background process, rather than being under the direct control of an interactive user.

Purpose

  • To die only if killed directly
  • To not have to worry about starting / stopping it
  • To be self sustaining (as much as possible, atleast)
  • The users should not be aware of it (although the administrators should)

Steps to create a daemon

There are several steps to create a daemon. Although some of them may be ignored and the program will still resemble a daemon it is strongly advisable to implement all of the steps.

Steps to create a daemon

  • Dissociating from the controlling tty

This step is most basic step. If it is not dissociated from a tty then when (eventually) the terminal window is closed the daemon will also die

Steps to create a daemon

  • Becoming a session leader and a process group leader

Since process groups are used to control the signals it is very important to make the daemon a process group leader since that way it can't get effected from signals directed at other processes

Steps to create a daemon

  • Executing as a background task

To run a process in the background
you can add an & at the end of the command when you trigger the program . For example: bash script.sh&

Detaching a process from tty at runtime: You can use SIGHALT to suspend the process to memory and then use bg to detach the job from the tty. 

Steps to create a daemon

  • Setting the root directory (/) as the current working directory

...so that the process does not keep any directory in use that may be on a mounted file system (allowing it to be unmounted).

Steps to create a daemon

  • Changing the umask to 0

to allow open(), creat(), et al. operating system calls to provide their own permission masks and not to depend on the umask of the caller

Steps to create a daemon

  • Closing all inherited files at the time of execution that are left open by the parent process
  • Using a logifle, or /dev/null as stdin, stdout, stderr

Alternatives

If you don't want to daemonize a program, but you still want to make sure that it runs periodically you can use cron. Note that this is for some very specific uses (such as reports) and in no way is a real daemonization, however it saves time from implementing it and cron comes with every *nix distribution.

Note that there is no standard and preinstalled package that daemonizes programs.

cron is not suitable for things which require processing on the fly such as web servers.

Daemons in practice

In the lab we are going to explore particular packages and applications that let you demonize processes.

A bunch of the daemons that work on a typical *nyx sistem:
dhcpdftpdhttpdidentdinetd, named (the DNX daemon), sshd, openvpnd,  sendmail, telnet

... and those are just a bunch of the network oriented daemons. There are tons more.

Questions?

Questions 

  • What is the purpose of daemonizing a process?
  • What is each step and why is it done ?
  • Are there any alternatives?

Which I would put on a test

Made with Slides.com