EAUJM


    -- Человек обладает шестым чувством?
    -- Разумеется, и не только человек.
       Это чувство силы тяготения (гравитации).

    Lunar Lander (игра)

I. Три подхода к управлению демонами на примере sshd:
   --------------------------------------------------

1. Стиль Дебиан.

    /etc/init.d/ssh start
    /etc/init.d/ssh restart
    /etc/init.d/ssh stop
    /etc/init.d/ssh status
    for each in /etc/init.d/*
    do
        $each status
    done

2. Стиль "Красная шляпа"

    # cat /proc/1/comm
    init

    service sshd start
    service sshd restart
    service sshd stop
    service sshd status
    service --status-all

3. Cтиль "БСД"

  virtu@debi:~$ sudo apt-get update
  NB! apt-get upgrade required !!!
  virtu@debi:~$ sudo apt-get install systemd-sysv
  Reading package lists... Done
  Building dependency tree
  Reading state information... Done
  The following packages will be REMOVED:
    sysvinit
  The following NEW packages will be installed:
    systemd-sysv
  WARNING: The following essential packages will be removed.
  This should NOT be done unless you know exactly what you are doing!
    sysvinit
  0 upgraded, 1 newly installed, 1 to remove and 33 not upgraded.
  Need to get 15.1 kB of archives.
  After this operation, 227 kB disk space will be freed.
  You are about to do something potentially harmful.
  To continue type in the phrase 'Yes, do as I say!'

4. После рестарта системы

    # cat /proc/1/comm
    systemd

    # systemctl start sshd.service
    # systemctl list-units


II. Компиляция простого демона
    --------------------------

// based on code from
//   http://www.opennet.ru/base/dev/daemon_expl.txt.html

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <signal.h>
#include <time.h>

// daemon body

inline void do_packet_loop() {
    while(1) pause();
}

// signal processing

void fsignal(int sig) {
    time_t tm;
    struct tm *ltime;
    switch(sig) {
        case SIGTERM:
        case SIGINT:
        case SIGQUIT:
            time( &tm);
            ltime = localtime( &tm);
            syslog(LOG_NOTICE,
                   "SIMPLE daemon stopped on signal %d at %s",
                   sig,
                   asctime( ltime)
            );
            closelog();
            exit(0);
        default:
            syslog( LOG_NOTICE, "Have got signal %d. Ignoring it.", sig);
            break;
    }
}

int main(int argc,char** argv)
{
    int  j;

    setgid(65534);
    setuid(65534);
    chdir("/");
    fclose(stdin); fclose(stdout); fclose(stderr);

    if (fork()) exit(0);

    setsid();         // go to background

    // all signals processed by fsignal
    for( j = 1; j < 32; j++) signal( j, fsignal);

    time_t tm;
    time( &tm);
    struct tm *local_tm = localtime(&tm);

    syslog(
        LOG_NOTICE,
        "SIMPLE daemon started at %s",
        asctime(local_tm)
    );

    do_packet_loop(); // loop daemon
}


Пример сессии
-------------

virtu@debi:~$ vi simple.c
virtu@debi:~$ gcc simple.c -o simple
virtu@debi:~$ ./simple
virtu@debi:~$ ps aux
...
virtu     1332  0.0  0.1  19416  2116 pts/0   Ss   15:11   0:00 bash
virtu     1403  0.0  0.0   4072   364 ?       Ss   15:16   0:00 ./simple
virtu     1412  0.0  0.0  16840  1236 pts/0   R+   15:17   0:00 ps aux
virtu@debi:~$ kill -5 1403
virtu@debi:~$ kill -15 1403
virtu@debi:~$ sudo cat /var/log/syslog | tail
...
Oct 31 15:16:24 debi simple: SIMPLE daemon started
                at Fri Oct 31 15:16:24 2014
Oct 31 15:17:01 debi /USR/SBIN/CRON[1410]: (root) CMD (
                cd / && run-parts --report /etc/cron.hourly)
Oct 31 15:17:50 debi simple: Have got signal 5. Ignoring it.
Oct 31 15:17:58 debi simple: SIMPLE daemon stopped on signal 15
                at Fri Oct 31 15:17:58 2014

------------------------------------------------------------------------

Appendix A. Linux signals

Signal     Number   Description
SIGHUP      1       Hangup (POSIX)
SIGINT      2       Terminal interrupt (ANSI)
SIGQUIT     3       Terminal quit (POSIX)
SIGILL      4       Illegal instruction (ANSI)
SIGTRAP     5       Trace trap (POSIX)
SIGIOT      6       IOT Trap (4.2 BSD)
SIGBUS      7       BUS error (4.2 BSD)
SIGFPE      8       Floating point exception (ANSI)
SIGKILL     9       Kill(can't be caught or ignored) (POSIX)
SIGUSR1    10       User defined signal 1 (POSIX)
SIGSEGV    11       Invalid memory segment access (ANSI)
SIGUSR2    12       User defined signal 2 (POSIX)
SIGPIPE    13       Write on a pipe with no reader, Broken pipe (POSIX)
SIGALRM    14       Alarm clock (POSIX)
SIGTERM    15       Termination (ANSI)
SIGSTKFLT  16       Stack fault
SIGCHLD    17       Child process has stopped or exited, changed (POSIX)
SIGCONT    18       Continue executing, if stopped (POSIX)
SIGSTOP    19       Stop executing(can't be caught or ignored) (POSIX)
SIGTSTP    20       Terminal stop signal (POSIX)
SIGTTIN    21       Background process trying to read, from TTY (POSIX)
SIGTTOU    22       Background process trying to write, to TTY (POSIX)
SIGURG     23       Urgent condition on socket (4.2 BSD)
SIGXCPU    24       CPU limit exceeded (4.2 BSD)
SIGXFSZ    25       File size limit exceeded (4.2 BSD)
SIGVTALRM  26       Virtual alarm clock (4.2 BSD)
SIGPROF    27       Profiling alarm clock (4.2 BSD)
SIGWINCH   28       Window size change (4.3 BSD, Sun)
SIGIO      29       I/O now possible (4.2 BSD)
SIGPWR     30       Power failure restart (System V)
