Apache Daemon Specification - Version 1.0

# Apache Daemon Specification

#### Version 1.0

Abstract: This document specifies the behavior and life cycle of an abstract Java™ daemon, in relation to its native container. In addition it defines a mechanism for controlling a daemon, and its interaction with the native OS process in which its existance is confined.

## Index

1. Introduction
2. Scope of this specification
3. The Daemon interface and its life cycle
   1. Instantiation
   2. Initialization
   3. Startup
   4. Stop
   5. Destruction

## Introduction

Since 1994, the Java™ programming language evolved and became a valid tool to develop, other than applets and client applications, reliable and performant server applications. The major disadvantage of the Java™ platform is that still today the only portable way to start a Java™ applcation relies on a single point of entry: the `public static void main(String[])` method.

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 `System.exit(int)` 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.

A server application written in Java might have to perform several tasks before being able to shutdown 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.

One common solution to this problem is to create (for example) a `ServerSocket` 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 `System.exit` method is called to terminate the Virtual Machine process. This method, however, implies several disadvantages and risks: for example in case of a system-wide shutdown, it might happen that the Virtual Machine process will be shut down directly by the operating system, without notifying the running server application. Or, for example, if an attacker finds out what is the required message to send to the server, and discovers a way to send this message to the running server application, he can easily interrupt the operation of a server, bypassing all the security restrictions implemented in the operating system.

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 daemons and are controlled by the operating system with a set of specified signals. Under Windows such programs are called daemons 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.

## Scope of this specification

The scope of this specification is to define an API in line with the current Java™ Platform APIs to support an alternative invocation mechanism which could be used instead of the above mentioned `public static void main(String[])` method. This specification cover the behavior and life cycle of what we define as "Java ™ daemons", or, in other words, non interactive Java™ applications.

This specification does not cover how the container of a Java™ daemon must be implemented, or how to build a native liaison between the operating system and the `Daemon` interface, but defines the relation between the an operating system process and the `Daemon` implementation life cycle. It should be trivial for implementors to build a native liaison and container for Java™ daemons.

This specification, together with the related API documentation, can be used by software deveopers to build portable non interactive applications based on the Java™ platform.

## The Daemon interface and its life cycle

The `Daemon` interface (defined as follows) is the main interface representing a Java™ daemon:

    package org.apache.commons.daemon;

    public interface Daemon
    extends Runnable {

        public void init(DaemonContext context)
        throws Exception;

        public void run();

        public void die();

        public void destroy();
    }

A daemon is managed through a well defined life cycle that defines how it is loaded, initialized, started, stopped and destroyed. This life cycle is expressed in the API by the `init(...)`, `run()`, `die()` and `destroy()` methods of the `Daemon` interface.

### Instantiation

The daemon container is responsible for instantiating daemons. Because of this, concrete implementations of the `Daemon` interface must always expose a public void constructor, which will be used by the daemon container to create instances of the class. For example:

    package mypackage;

    import org.apache.commons.daemon.*;

    public class MyDaemon
    implements Daemon {

        /** This constructor must be here. */
        public MyDaemon() {
            super();
            ...
        }
        ...
    }

Once the Virtual Machine process is started in the underlying operating system, and the Virtual Machine itself is created and configured, the daemon container associated with its native liaison constructs a new instance of the concrete class implementing the `Daemon` instance. It is not defined by this specification how the instance is constructed, or how many instances of one or more concrete daemons implementation can coexist in the same Virtual Machine.

As a side note, in case of multiple daemons running within the scope of the same virtual machine, developers should not rely on static methods and variables for proper operation, as it is advisable for security reasons to load each different daemon instance in a separate class loader.

### Initialization

After the daemon instance has been constructed, the container must initialize it before it can be started. Initialization is provided so that a daemon can read persisten configuration data, access native resources, and perform other one-time activities.

Under certain operating systems (typically Unix based operating systems), and if the native liaison of the container supports and it is configured accordingly, this method might be called with super-user privileges. For example, within the scope of this method, it might be wise to create all required `ServerSocket` instances (as under several operating systems this requires super-user privileges).

That said, it is very important that within the scope of this method "untrusted" data cannot be processed (for example, starting an acceptor thread on a `ServerSocket` which receives and processes data from unknown sources), as this would serious security hazards.

The daemon container must guarantee that the `init(...)` method is called at least once, and only once on an instance of a daemon object, and that it will be called before any access to the `run()`, `die()` or `destroy()` methods.

During initialization, the daemon instance can throw any `Exception` preventing a the successful completion of the `init(...)` method. In this case the daemon instance must not be started, and the `destroy()` method must not be called before the instance is released by the container to allow garbage collecting.

### Startup

As the `Daemon` interface extends the `Runnable` interface, to start the operation of a daemon, the container calls the `run()` method within the scope of a `Thread`. A daemon is said to be "running" until the thread

Author Pier Fumagalli <pier.fumagalli@sun.com>
Copyright © 2001, The Apache Software Foundation <http://www.apache.org/>
All rights reserved.

Sun, Sun Microsystems, Solaris, and Java are trademarks or registered trademarks of Sun Microsystems, Inc. <http://www.sun.com/>
UNIX® is a registered trademark in the United States and other countries, exclusively licensed through X/Open Company, Ltd. <http://www.opengroup.org/>
Windows, WinNT and Win32 are registered trademark of Microsoft Corp. <http://www.microsoft.com/>