comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
To: <comp.lang.ada@ada.eu.org>
Subject: Re: Unix-daemons in Ada
Date: Sun, 1 Jul 2001 16:47:02 -0500
Date: 2001-07-01T16:47:02-05:00	[thread overview]
Message-ID: <mailman.994024024.17837.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: 87sngg3jp8.fsf@520075220525-0001.dialin.t-online.de

[-- Attachment #1: Type: text/plain, Size: 2409 bytes --]

The attachment is the source code for a procedure that I have used for seven
years on three different flavors of UNIX -- IRIX 6.3 (mips), Solaris 2.5.1
(sparc), and
Linux (ix86).

This procedure is called immediately after any initialization (e.g.,
command-line
argument processing) in the main program.

The main program declares a Boolean variable named Is_Parent which is
initialized
to True.

That variable is set by this procedure in the child process, so, immediately
after the
call to this procedure,  the main program should contain the following
sequence:
if Is_Parent then
   return;
end if;

Thus, the parent procedure will terminate, thus returning control to its
invoking
shell, and the code following the above sequence continues as the child
process
which has been made into a true UNIX daemon -- i.e:

   1. The process has been detached from its controlling terminal, and has
been
       made the process group leader of a new process group.

  2. The current working directory is changed to "/" in case the directory
from
      which the daemon was started should become uncounted -- i.e, the current
      directory is changed to one which for sure cannot be uncounted.

  3.  Finally, the file creation mask is set appropriately.

This procedure uses the POSIX/Ada95 bindings from the florist package set.

I do not believe the earlier respondents to your request understood just what
is meant by "UNIX daemon."  This code was developed from W. Richard
Stevens' book "Advanced Programming in the Unix Environment," and was
originally implemented in Ada83, with an earlier implementation of
POSIX/Ada83 bindings  called "forest" a sort of predecessor to "florist."

----- Original Message -----
From: "Stefan Nobis" <stefan@snobis.de>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: July 01, 2001 7:41 AM
Subject: Unix-daemons in Ada


> Hi.
>
> I'm learning Ada and i tried to write a simple Unix daemon in Ada (a very
> simple web server). I want the main process, which is attached to a tty, to
> exit, something like
>
> if (fork()) exit 0;
> else do_real_work();
>
> in C. I played a little bit with Adas Tasks, but i have no clue how to
> implement the above in Ada.
>
> --
> Until the next mail...,
> Stefan.
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
>

[-- Attachment #2: ss-net-daemon-daemon_init.adb --]
[-- Type: application/octet-stream, Size: 2391 bytes --]

------------------------------------------------------------------------
-- SS.Net.Daemon.Daemon_Init
-- Purpose:
--   This procedure initializes its calling process as a daemon.  The
--   procedure sets the global variable Is_Parent FALSE if the process
--   is the child process created by the fork call within the
--   procedure. This procedure should be called by the main subprogram
--   as its first executable statement after any preliminaries such as
--   qualifying command line arguments or environment variables, etc.
--
-- Implementationnotes:
   --    This procedure implements the daemon coding rules given in the
   --    book Advanced Programming in the UNIX Environment, by
   --    W. Richard Stevens, ISBN 0-201-56317-7, pp. 417-418.
   --
------------------------------------------------------------------------
with POSIX_Permissions;
with POSIX_Process_Identification;
with POSIX_Unsafe_Process_Primitives;
separate (SS.Net.Daemon)

procedure Daemon_Init is
   The_Process_Id : Posix_Process_Identification.Process_Id;
   The_Process_Group_Id : Posix_Process_Identification.Process_Group_Id;
   use type Posix_Process_Identification.Process_Id;
begin

   -- Create a child process
   The_Process_Id := Posix_Unsafe_Process_Primitives.Fork;

   if The_Process_Id /=
      Posix_Process_Identification.Null_Process_Id then
      -- fork failed, or is parent; quit, in either case
      -- this step makes the shell think the command is done, and
      -- guarantees that the surviving child is not a process group
      -- leader.  This is a prerequisite to the call to SetSID done by
      -- the child.
      return;
   end if;

   -- Only the child process gets here.
   Is_Parent := False;

   -- Make this process the session leader of a new session, become the
   -- process group leader of a new process group, and insure that the
   -- process has no controlling terminal.
   Posix_Process_Identification.Create_Session (The_Process_Group_Id);

   -- Change the current working directory to one that for sure cannot
   -- be unmounted.
   Posix_Process_Environment.Change_Working_Directory ("/");

   -- Set the file creation mask to deny write to all but the process
   -- owner
   Posix_Permissions.Set_Allowed_Process_Permissions
     ((Posix_Permissions.Owner_Read | Posix_Permissions.Owner_Write =>
         True,
       others => False));

end Daemon_Init;

  parent reply	other threads:[~2001-07-01 21:47 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-01 12:41 Unix-daemons in Ada Stefan Nobis
2001-07-01 14:07 ` James Rogers
2001-07-01 14:35   ` Stefan Nobis
2001-07-01 17:20     ` James Rogers
2001-07-03 20:48       ` Stefan Skoglund
2001-07-03 22:39         ` Larry Kilgallen
2001-07-06 14:15           ` Tarjei T. Jensen
2001-07-06 17:05             ` Larry Kilgallen
2001-07-06 22:33               ` Tarjei Tj�stheim Jensen
2001-07-13  7:51                 ` Maxim Reznik
2001-07-01 19:51 ` Florian Weimer
2001-07-01 21:47 ` David C. Hoos, Sr. [this message]
2001-07-02 19:18   ` Stefan Nobis
2001-07-02 20:16     ` David C. Hoos
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox