Linux Daemons

Post at — Apr 15, 2026
#linux #daemons #C

What is a daemon and how to make one in C

What is a daemon.

A daemon is a program or script that runs in an “astral plane”. It is not attached to a terminal nor to a regular job session. Its existence goes beyond a normal process job.

Run in the background

The process must not depend on an interactive terminal.

Perform a fork() and terminate the parent process

This allows the daemon to continue running independently of the process that started it.

Create a new session with setsid()

This ensures that the process:

  1. Has no controlling terminal
  2. Becomes the leader of a new session.

Change the working directory (normally to /)

This prevents the daemon from locking directories that might later need to be unmounted.

Reset the file creation mask with umask(0)

This prevents inheriting unexpected permission restrictions from the parent process.

Close the standard file descriptors

Close:

  1. stdin
  2. stdout
  3. stderr

Use a logging system instead of printing to the terminal

Normally using syslog.

Maintain a continuous execution loop

Usually a loop that performs periodic tasks.

Example of a Daemon in C.

This example was taken from the video of the reference with authorship by Last Dragon.

Purpose of the daemon.

The purpose of this daemon is to mount an HDD and prevent it from being unmounted. When the system restarts or shuts down the daemon will unmount the device so the shutdown process can complete without problems.

Code.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
daemon.c

Example of a simple daemon that tries to mount a device periodically
and unmounts it when it receives SIGTERM.

This code demonstrates several classic daemon programming concepts
in Unix/Linux systems:

- fork()
- setsid()
- closing terminal file descriptors
- using syslog
- signal handling
- checking mounted filesystems
*/

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/mount.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>

#define DEVICE "/dev/sdaX"
#define MOUNTPOINT "/demonio"
#define FSTYPE "ext4"

int is_mounted() {

FILE *f = fopen("/proc/mounts", "r");

if (!f)
return 0;

char line[512];

while (fgets(line, sizeof(line), f)) {

if (strstr(line, MOUNTPOINT)) {
  fclose(f);
  return 1;
}

}

fclose(f);
return 0;
}

void terminate(int signal) {

if (is_mounted()) {

if (umount(MOUNTPOINT) == 0) {
  syslog(LOG_INFO, "Unmount successful");
} else {
  syslog(LOG_ERR, "Unmount failed");
}


}

syslog(LOG_INFO, "Daemon terminated");

closelog();

exit(0);
}

int main()
{

pid_t process = fork();

if (process < 0) {
exit(EXIT_FAILURE);
}

if (process > 0) {
printf("Daemon started\n");
return 0;
}

signal(SIGTERM, terminate);

chdir("/");

close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

setsid();

openlog("mount_daemon", LOG_PID, LOG_DAEMON);

while (1) {

if (!is_mounted()) {

  if (mount(DEVICE, MOUNTPOINT, FSTYPE, 0, NULL) == 0) {
    syslog(LOG_INFO, "Mount successful");
  } else {
    syslog(LOG_ERR, "Mount failed");
  }
}

sleep(10);

}

return 0;
}

References:

Create a daemon in Linux with Bash and C