<?xml version="1.0"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
<document xmlns="http://maven.apache.org/XDOC/2.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 https://maven.apache.org/xsd/xdoc-2.0.xsd">
 <properties>
  <title>Daemon : Java based daemons or services</title>
  <author email="jfrederic.clere@fujitsu-siemens.con">Jean-Frederic Clere</author>
 </properties>

<body>
<section name="Introduction">
<p>
      Since 1994, the Java programming language evolved and became a
      valid tool to develop reliable and performant server applications as
      opposed to just applets and client applications. The major disadvantage of
      the Java platform is that still today the only portable way to
      start a Java application relies on a single point of entry: the
      <code><em class="key">public static void</em>
      main(<em class="ref">String</em>[])</code> method.
</p>
<p>
      Having a single-point of entry is a valid solution for client
      applications, where interactively a user can command to the application
      to quit (which can terminate the Virtual Machine process at calling the
      <code><em class="ref">System</em>.exit(<em class="key">int</em>)</code>
      method), but in those cases where the application is not interactive
      (server applications) there is currently no portable way to notify
      the Virtual Machine of its imminent shutdown.
</p>
<p>
      A server application written in Java might have to perform several tasks
      before being able to shut down the Virtual Machine process. For example
      in the case of a Servlet container, before the VM process is shut down,
      sessions might need to be serialized to disk, and web applications need
      to be destroyed.
</p>
<p>
      One common solution to this problem is to create (for example) a
      <code><em class="ref">ServerSocket</em></code> and wait for a particular
      message to be issued. When the message is received, all operations
      required to shut down the server applications are performed and at the
      end the <code><em class="ref">System</em>.exit</code> method is called
      to terminate the Virtual Machine process. This method however, has
      several disadvantages and risks:
</p>
      <ul>
      <li>
      In case of a system-wide shutdown, the Virtual Machine process may be
      shut down directly by the operating system without notifying the running
      server application.
      </li>
      <li>
      If an attacker finds out the shutdown message to send to the server
      and discovers a way to send this message, he can easily interrupt
      the server's operation, bypassing all the security restrictions
      implemented in the operating system.
      </li>
      </ul>
<p>
      Most multi-user operating systems already have a way in which server
      applications are started and stopped. Under Unix based operating systems
      non-interactive server applications are called <em>daemons</em> and are
      controlled by the operating system with a set of specified
      <em>signals</em>. Under Windows such programs are called <em>services</em>
      and are controlled by appropriate calls to specific functions defined in
      the application binary, but although the ways of dealing with the problem
      are different, in both cases the operating system can notify a server
      application of its imminent shutdown, and the application has the
      ability to perform certain tasks before its process of execution is
      destroyed.
</p>
</section>

<section name="Structure">
<p>
      Daemon is made of 2 parts. One written in C that makes the interface to
      the operating system and the other in Java that provides the
      Daemon API.
</p>
</section>

<section name="Platforms">
<p>
      Both Win32 and UNIX like platforms are supported.
      For Win32 platforms use <a href="procrun.html">procrun</a>.
      For UNIX like platforms use <a href="jsvc.html">jsvc</a>.
</p>
</section>
<section name="Initial Source of the Package">

<p>The original Java classes came from the Jakarta Tomcat 4.0 project.</p>

<p>The package name for the Daemon component is
<code>org.apache.commons.daemon</code>.
</p>
</section>

</body>
</document>
