Daemon(2)


デーモンの仕組み(2)

apache などのサーバはOS起動時からデーモンとしてメモリに常駐して ポートを監視します。 これらのサーバは要求を処理していないときは、単に要求が来るかどうか 特定のポートを監視しているだけです。

一つ一つのサーバ自体が使うメモリ量は少なくても、いくつものサーバが 動作していると計算機のメモリを圧迫してしまいます。 そこで、ポート監視専門のデーモンを1つだけ動かしてポートを 監視させておいて、クライアントからの要求がポートに届いた時点で その要求を処理するサーバを起動する、という方法が考えられます。 ポートへ要求がたまにしか来ないサーバや、立ち上がりにあまり 時間が掛からないサーバは、このような起動方法が適しています。

このようなポート監視専門のデーモンは、 「サーバを起動するためのサーバ」ということで 「スーパーサーバ」や「スーパーサーバデーモン」と 呼ばれることがあります。

スーパーサーバとしてよく使われているのが次の2種類のソフトウェアです。

inetd

ポート監視用デーモンとしてよく利用されていたのが inetd です。 inetdの設定は /etc/inetd.conf で行ないます。

/etc/inetd.conf を変更した場合は、inetd に HUP シグナルを送っておく 必要があります。

# kill -HUP `cat /var/run/inetd.pid`

[注意]tcpd

inetd によって起動されるデーモンは、RedHat6.2J の場合 tcpdによってフィルタが掛けられています。

tcpd は、次の動作をします。

xinetd

最近のRedHat や Fedora Core Linux では inetd の代りに xinetd が 使われるようになりました。

詳しくはmanコマンドを使って xinetd のマニュアルを読んで下さい。
   # man xinetd
   # man xinetd.conf
man xinetd.confの抜粋
XINETD.CONF(5)                                     XINETD.CONF(5)

NAME
       xinetd.conf - Extended Internet Services Daemon configura-
       tion file

DESCRIPTION
       xinetd.conf is the configuration file that determines  the
       services  provided  by  xinetd.  Any line whose first non-
       whitespace character is a  '#'  is  considered  a  comment
       line. Empty lines are ignored.

       The file contains entries of the form:

              service <service_name>
              {
                     <attribute> <assign_op> <value> <value> ...
                     ...
              }

       The  assignment  operator,  assign_op,  can be one of '=',
       '+=', '='.  The majority of attributes  support  only  the
       simple  assignment  operator, '='.  Attributes whose value
       is a set of values support all assignment operators.   For
       such  attributes, '+=' means adding a value to the set and
       '=' means removing a value from the set.  A list of  these
       attributes  will  be  given  after  all the attributes are
       described.

       Each entry  defines  a  service  identified  by  the  ser-
       vice_name.    The   following   is  a  list  of  available
       attributes:

       socket_type      Possible   values   for   this  attribute
                        include:

                        stream      streambased service

                        dgram       datagrambased service

                        raw         service that requires  direct
                                    access to IP

                        seqpacket   service  that  requires reli-
                                    able   sequential    datagram
                                    transmission

       wait             This  attribute determines if the service
                        is singlethreaded  or  multithreaded.  If
                        its  value  is yes the service is single-
                        threaded; this  means  that  xinetd  will
                        start  the  server  and then it will stop
                        handling requests for the  service  until
                        the  server dies.  If the attribute value
                        is no, the service is  multithreaded  and
                        xinetd  will  keep  handling  new service
                        requests.

       user             determines the uid for  the  server  pro-
                        cess.   The   user  name  must  exist  in
                        /etc/passwd.  This attribute is  ineffec-
                        tive  if  the effective user ID of xinetd
                        is not superuser.

       server           determines  the  program  to  execute for
                        this service.

       server_args      determines the arguments  passed  to  the
                        server.  In contrast to inetd, the server
                        name   should   not   be   included    in
                        server_args.

       only_from        determines  the remote hosts to which the
                        particular  service  is  available.   Its
                        value is a list of IP addresses which can
                        be specified in any  combination  of  the
                        following ways:

                        a)   a  numeric  address  in  the form of
                             %d.%d.%d.%d. If the rightmost compo-
                             nents  are  0,  they  are treated as
                             wildcards (for example, 128.138.12.0
                             matches  all hosts on the 128.138.12
                             subnet).  0.0.0.0 matches all Inter-
                             net addresses.

                        b)   a  factorized address in the form of
                             %d.%d.%d.{%d,%d,...}.  There  is  no
                             need  for  all  4  components  (i.e.
                             %d.%d.{%d,%d,...%d}  is  also   ok).
                             However, the factorized part must be
                             at the end of the address.

                        c)   a network name (from /etc/networks)

                        d)   a host name.  When a  connection  is
                             made  to xinetd, a reverse lookup is
                             performed, and  the  canonical  name
                             returned  is  compared to the speci-
                             fied host name.  You  may  also  use
                             domain   names   in   the   form  of
                             .domain.com.  If the reverse  lookup
                             of   the   client's   IP  is  within
                             .domain.com, a match occurs.

                        e)   an ip address/netmask range  in  the
                             form of 1.2.3.4/32.

                        Specifying this attribute without a value
                        makes the service available to nobody.

(略)
       port             determines  the  service  port.  If  this
                        attribute  is  specified  for  a  service
                        listed in /etc/services, it must be equal
                        to the port number listed in that file.

(略)

EXAMPLE
              #
              # Sample configuration file for xinetd
              #

              defaults
              {
                     log_type            = FILE /var/log/servicelog
                     log_on_success      = PID
                     log_on_failure      = HOST RECORD
                     only_from           = 128.138.193.0 128.138.204.0 128.138.209.0
                     only_from           = 128.138.252.1
                     instances           = 10
                     disabled            = rstatd
              }

              #
              # Note 1: the protocol attribute is not required
              # Note 2: the instances attribute overrides the default
              #
              service login
              {
                     socket_type         = stream
                     protocol            = tcp
                     wait                = no
                     user                = root
                     server              = /usr/etc/in.rlogind
                     instances           = UNLIMITED
              }

              #
              # Note 1: the instances attribute overrides the default
              # Note 2: the log_on_success flags are augmented
              #
              service shell
              {
                     socket_type         = stream
                     wait                = no
                     user                = root
                     instances           = UNLIMITED
                     server              = /usr/etc/in.rshd
                     log_on_success      += HOST RECORD
              }

              service ftp
              {
                     socket_type         = stream
                     wait                = no
                     nice                = 10
                     user                = root
                     server              = /usr/etc/in.ftpd
                     server_args         = l
                     instances           = 4
                     log_on_success      += DURATION HOST USERID
                     access_times        = 2:009:00 12:0024:00
              }

              # Limit telnet sessions to 8 Mbytes of memory and a total
              # 20 CPU seconds for child processes.
              service telnet
              {
                     socket_type         = stream
                     wait                = no
                     nice                = 10
                     user                = root
                     server              = /usr/etc/in.telnetd
                     rlimit_as           = 8M
                     rlimit_cpu          = 20
              }

(略)