File: demoninit.cc

package info (click to toggle)
sinfo 0.0.48-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,332 kB
  • sloc: sh: 11,213; cpp: 6,722; makefile: 271; xml: 151; perl: 149
file content (38 lines) | stat: -rw-r--r-- 736 bytes parent folder | download | duplicates (3)
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
/// demoninit.cc ///////////////////////////////////////////////////////////////

#include <iostream>
#include <signal.h>
#include <unistd.h> // location of daemon() on linux
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h> // location of daemon() on FreeBSD


// Detach ourself from the controlling terminal.
void demoninit()
{
#ifdef HAVE_DAEMON
  if (daemon(0, 0) == -1)
  {
    raise(SIGTERM);
  }
  umask(0);

#else
  int pid = 0;

  pid = fork();
  if (pid == -1)
    cout << "Cannot fork() to detach(1)";
  else if (pid)
    exit(0);

  // Close fds
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
#endif
}


/// EOF ////////////////////////////////////////////////////////////////////////