J3VKW


## SIMPLE LINUX DAEMON

  https://github.com/jirihnidek/daemon

This repository contains simple example of daemon for Linux OS. This
repository also contains examples of starting scripts.


# CLONE AND DISCOVER

  $ git clone https://github.com/jirihnidek/daemon
  $ ls
  $ rm -rf daemon/.git
  $ tree daemon/
  $ ls -lR daemon/
  $ find daemon/ -name '*' -print
  $ find daemon/ -name '*' -type f -print -exec cat {} \; | wc -l
  $ cat daemon/src/daemon.c | wc -l

Build a script to show each entry like this:

  ------

  <src/daemon.c>

    #include <stdio.h>
    #include <stdlib.h>
    ..
      return EXIT_SUCCESS;
    }

  EOF

  333 lines 8088 bytes

  ~~~~~~

Build and install simple daemon on Debian 9 virtual machine

  apt install build-essential cmake
  cd ~/daemon
  mkdir build
  cd build
  cmake -DCMAKE_INSTALL_PREFIX=/usr ../
  make
  sudo make install

Show 3 ways of handling our service

  1. (native)
     systemctl start|stop|restart|status|enable|disable simple-daemon.service
     systemctl start|stop|restart|status|enable|disable forking-daemon.service

  2. (forward)
     service simple-daemon start|stop|restart|status
     service simple-daemon start|stop|restart|status

  3. (create scripts)
     /etc/init.d/simple-daemon start|stop|restart|reload
     /etc/init.d/forking-daemon start|stop|restart|reload

NOTE: 1. Check location for daemon executable and daemon.service

      2. fix Makefile install target and daemon.init script


## WORKING TEMPLATE OF /etc/init.d/simple-daemon (DEBIAN STRETCH)

<simple-daemon>

  #!/bin/sh

  ### BEGIN INIT INFO
  # Default-Start:  2 3 4 5
  # Default-Stop: 0 1 6
  ### END INIT INFO

  set -e

  # Source function library.
  . /lib/lsb/init-functions

  # Are we running from init?
  run_by_init() {
      ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
  }

  start() {
    /usr/bin/daemon -d \
        --conf_file /etc/daemon/daemon.conf \
        --log_file /var/log/daemon.log
  }

  stop() {
    killall -9 daemon
  }

  prg="simple-daemon"
  export PATH="${PATH:+$PATH:}/usr/local/bin"

  case "$1" in
    start)
          echo "STARTING: $prg"
          start
          ;;
    stop)
          echo "STOPPING: $prg"
          stop
          ;;
    restart)
          echo "RESTARTING: $prg"
          start; stop
          ;;
    status)
          echo "QUERING STATUS: $prg"
	  ps -A | grep -i daemon
          ;;
    *)
          log_action_msg "Usage: /etc/init.d/$prg {start|stop|restart|status}" || true
          exit 1
  esac

  exit 0

EOF

------

<forking-daemon>

  #!/bin/sh

  ### BEGIN INIT INFO
  # Default-Start:  2 3 4 5
  # Default-Stop: 0 1 6
  ### END INIT INFO

  set -e

  # Source function library.
  . /lib/lsb/init-functions

  # Are we running from init?
  run_by_init() {
      ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
  }

  start() {
    /usr/bin/daemon \
         --conf_file /etc/daemon/daemon.conf \
         --log_file /var/log/daemon.log \
         --pid_file /var/run/daemon.pid \
         --daemon
  }

  stop() {
    /bin/kill -9 `cat /var/run/daemon.pid`
  }

  prg="forking-daemon"
  export PATH="${PATH:+$PATH:}/usr/local/bin"

  case "$1" in
    start)
          echo "STARTING: $prg"
          start
          ;;
    stop)
          echo "STOPPING: $prg"
          stop
          ;;
    restart)
          echo "RESTARTING: $prg"
          start; stop
          ;;
    status)
          echo "QUERING STATUS: $prg"
          status_of_proc -p /var/run/daemon.pid /usr/bin/daemon daemon && exit 0 || exit $?
          ;;
    *)
          log_action_msg "Usage: /etc/init.d/daemon {start|stop|restart|status}" || true
          exit 1
  esac

  exit 0

EOF

------

Replacing <tab> with TAB using sed:

..solution is to press Control+V and then the tab char.

  https://stackoverflow.com
    /questions/6363328/replacing-with-tab-using-sed

<Makefile>

  daemon:
  <tab>gcc -D_REETRANT -DNDEBUG -fPIC -Wall -Wextra -pedantic \
           -O3 -DNDEBUG -o daemon.c.o -c daemon.c
  <tab>gcc -D_REETRANT -DNDEBUG -fPIC -Wall -Wextra -pedantic \
           -O3 -DNDEBUG -o daemon -rdynamic daemon.c.o

  .PHONY: install
  install:
  <tab>echo 'Installing daemon'
  <tab>cp daemon.conf /etc/daemon/daemon.conf
  <tab>cp init/simple-daemon /etc/init.d/simple-daemon
  <tab>cp init/forking-daemon /etc/init.d/forking-daemon
  <tab>cp simple-daemon.service /lib/systemd/system/simple-daemon.service
  <tab>cp forking-daemon.service /lib/systemd/system/forking-daemon.service
  <tab>cp daemon /usr/bin/daemon

  .PHONY: uninstall
  uninstall:
  <tab>echo 'Uninstall daemon'
  <tab>rm -f /etc/daemon/daemon.conf
  <tab>rm -f /etc/init.d/simple-daemon
  <tab>rm -f /etc/init.d/forking-daemon
  <tab>rm -f /lib/systemd/system/simple-daemon.service
  <tab>rm -f /lib/systemd/system/forking-daemon.service
  <tab>rm -f /usr/bin/daemon

  .PHONY: clean
  clean:
  <tab>rm -f *.o daemon


