comp.lang.ada
 help / color / mirror / Atom feed
* Unix-daemons in Ada
@ 2001-07-01 12:41 Stefan Nobis
  2001-07-01 14:07 ` James Rogers
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Stefan Nobis @ 2001-07-01 12:41 UTC (permalink / raw)


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.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unix-daemons in Ada
  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 19:51 ` Florian Weimer
  2001-07-01 21:47 ` David C. Hoos, Sr.
  2 siblings, 1 reply; 14+ messages in thread
From: James Rogers @ 2001-07-01 14:07 UTC (permalink / raw)


This is much easier using tasks in Ada. In the Ada task there will be
no forking. Instead, a task will be created to handle the work.

You have several options in your task design. You can dynamically
create (and destroy) tasks as you need them, or you can create a pool
of tasks that get used over and over. The task pool approach is 
more complicated to manage, but limits the amount of memory
required for your program. The dynamic creation approach is easier
to manage, but expands memory usage in an uncontrolled manner.

Using the dynamic task approach you would do the following:

1) Declare a task type that perfoms your "real work"
2) Have the main process create an instance of the task type
   as needed for the service.
3) Pass necessary data to the task instance through an entry
   call.
At this point the task will do its work, and the main procedure
can loop back to monitoring its inputs so that it can start
another task as needed.

Jim Rogers
Colorado Springs, Colorado USA

Stefan Nobis wrote:
> 
> 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.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unix-daemons in Ada
  2001-07-01 14:07 ` James Rogers
@ 2001-07-01 14:35   ` Stefan Nobis
  2001-07-01 17:20     ` James Rogers
  0 siblings, 1 reply; 14+ messages in thread
From: Stefan Nobis @ 2001-07-01 14:35 UTC (permalink / raw)


James Rogers <jimmaureenrogers@worldnet.att.net> writes:

> This is much easier using tasks in Ada. In the Ada task there will be
> no forking. Instead, a task will be created to handle the work.

Yes, but i want the started daemon to become unassociated to the terminal from
which it is started. But in Ada the main task waits until all other tasks have
terminated so the daemon is always associated with the controlling terminal
from which the daemon was started. I want a daemon with no controlling
terminal. How do i do this in Ada?

> Using the dynamic task approach you would do the following:

That i did understand. The problem is not how to distribute the duties to
tasks but to become undependent from the controlling terminal.

-- 
Until the next mail...,
Stefan.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unix-daemons in Ada
  2001-07-01 14:35   ` Stefan Nobis
@ 2001-07-01 17:20     ` James Rogers
  2001-07-03 20:48       ` Stefan Skoglund
  0 siblings, 1 reply; 14+ messages in thread
From: James Rogers @ 2001-07-01 17:20 UTC (permalink / raw)


Ok, now I more clearly understand your request.

What you are asking is an operating system issue and not strictly a
language issue. 

It really invloves the manner used to start the Ada program.
Many Unix daemons are started by simply placing the executable name
in the /etc/inittab file, then rebooting the system. If you only want
a background process that can keep executing after its parent
process had died, you can run the program in the background and
set nohup for the resulting process. The main Ada task will still
exist as long as its tasks live. Remember, tasks run in a single
process. The entire Ada program must be run as a daemon, not just
some side effect of the program.

Of course, you could start the Ada program from C using a fork
and execute set of calls. For that matter, you can call the
same libraries directly from Ada if you wish. 

Jim Rogers
Colorado Springs, Colorado USA

Stefan Nobis wrote:
> 
> James Rogers <jimmaureenrogers@worldnet.att.net> writes:
> 
> > This is much easier using tasks in Ada. In the Ada task there will be
> > no forking. Instead, a task will be created to handle the work.
> 
> Yes, but i want the started daemon to become unassociated to the terminal from
> which it is started. But in Ada the main task waits until all other tasks have
> terminated so the daemon is always associated with the controlling terminal
> from which the daemon was started. I want a daemon with no controlling
> terminal. How do i do this in Ada?
> 
> > Using the dynamic task approach you would do the following:
> 
> That i did understand. The problem is not how to distribute the duties to
> tasks but to become undependent from the controlling terminal.
> 
> --
> Until the next mail...,
> Stefan.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unix-daemons in Ada
  2001-07-01 12:41 Unix-daemons in Ada Stefan Nobis
  2001-07-01 14:07 ` James Rogers
@ 2001-07-01 19:51 ` Florian Weimer
  2001-07-01 21:47 ` David C. Hoos, Sr.
  2 siblings, 0 replies; 14+ messages in thread
From: Florian Weimer @ 2001-07-01 19:51 UTC (permalink / raw)


Stefan Nobis <stefan@snobis.de> writes:

> 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.

Usually, you can't call fork() from Ada and do complicated things in
the child process (besides an execve()).  This has to do with the fact
that most fork() implementations don't duplicate the whole process
(only the current thread of execution), and that the runtime might
have stored PIDs which become outdated after this operation.

So it's best to detach *before* the Ada runtime is initialized.  How
you can achieve this depends on your Ada implementation.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unix-daemons in Ada
  2001-07-01 12:41 Unix-daemons in Ada Stefan Nobis
  2001-07-01 14:07 ` James Rogers
  2001-07-01 19:51 ` Florian Weimer
@ 2001-07-01 21:47 ` David C. Hoos, Sr.
  2001-07-02 19:18   ` Stefan Nobis
  2 siblings, 1 reply; 14+ messages in thread
From: David C. Hoos, Sr. @ 2001-07-01 21:47 UTC (permalink / raw)
  To: comp.lang.ada

[-- 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;

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unix-daemons in Ada
  2001-07-01 21:47 ` David C. Hoos, Sr.
@ 2001-07-02 19:18   ` Stefan Nobis
  2001-07-02 20:16     ` David C. Hoos
  0 siblings, 1 reply; 14+ messages in thread
From: Stefan Nobis @ 2001-07-02 19:18 UTC (permalink / raw)


"David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> writes:

> The attachment is the source code for a procedure that I have used for seven

> with POSIX_Permissions;
> with POSIX_Process_Identification;
> with POSIX_Unsafe_Process_Primitives;

Where can i get these packages? I use GNAT 3.13p on Linux and i get errors on
all of the packages.

-- 
Until the next mail...,
Stefan.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unix-daemons in Ada
  2001-07-02 19:18   ` Stefan Nobis
@ 2001-07-02 20:16     ` David C. Hoos
  0 siblings, 0 replies; 14+ messages in thread
From: David C. Hoos @ 2001-07-02 20:16 UTC (permalink / raw)
  To: comp.lang.ada; +Cc: stefan

As I stated in my original response, "this procedure uses the
POSIX/Ada95 bindings from the florist package set."

Look for the florist package set in the same place where you
obtained your gnat distribution. .

----- Original Message -----
From: "Stefan Nobis" <stefan@snobis.de>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Monday, July 02, 2001 2:18 PM
Subject: Re: Unix-daemons in Ada


> "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> writes:
>
> > The attachment is the source code for a procedure that I have used for
seven
>
> > with POSIX_Permissions;
> > with POSIX_Process_Identification;
> > with POSIX_Unsafe_Process_Primitives;
>
> Where can i get these packages? I use GNAT 3.13p on Linux and i get errors
on
> all of the packages.
>
> --
> 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
>




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unix-daemons in Ada
  2001-07-01 17:20     ` James Rogers
@ 2001-07-03 20:48       ` Stefan Skoglund
  2001-07-03 22:39         ` Larry Kilgallen
  0 siblings, 1 reply; 14+ messages in thread
From: Stefan Skoglund @ 2001-07-03 20:48 UTC (permalink / raw)


James Rogers wrote:
> What you are asking is an operating system issue and not strictly a
> language issue.

READ the subject !!!
He must do a number of things :
1. Get his process in such a order such that IT can't get a 
   new controlling terminal. this is to prevent a normal user from
stopping
   the process.

2. The process should be able to open /dev/console and other tty devices
   without turning the device into the processs controlling terminal !
   If not the process cant use /dev/console for error messages !



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unix-daemons in Ada
  2001-07-03 20:48       ` Stefan Skoglund
@ 2001-07-03 22:39         ` Larry Kilgallen
  2001-07-06 14:15           ` Tarjei T. Jensen
  0 siblings, 1 reply; 14+ messages in thread
From: Larry Kilgallen @ 2001-07-03 22:39 UTC (permalink / raw)


In article <3B422FAF.886FAF2B@ebox.tninet.se>, Stefan Skoglund <stetson@ebox.tninet.se> writes:
> James Rogers wrote:
>> What you are asking is an operating system issue and not strictly a
>> language issue.
> 
> READ the subject !!!
> He must do a number of things :
> 1. Get his process in such a order such that IT can't get a 
>    new controlling terminal. this is to prevent a normal user from
> stopping
>    the process.
> 
> 2. The process should be able to open /dev/console and other tty devices
>    without turning the device into the processs controlling terminal !
>    If not the process cant use /dev/console for error messages !

That is what James Rogers said.  This is an operating system issue.
Having the process "get a new controlling terminal" is a Unix issue,
as are devices like /dev/console.

I am not saying there would be no problems on VMS, but they would
be different problems, certainly with different solutions.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unix-daemons in Ada
  2001-07-03 22:39         ` Larry Kilgallen
@ 2001-07-06 14:15           ` Tarjei T. Jensen
  2001-07-06 17:05             ` Larry Kilgallen
  0 siblings, 1 reply; 14+ messages in thread
From: Tarjei T. Jensen @ 2001-07-06 14:15 UTC (permalink / raw)



Larry Kilgallen
>That is what James Rogers said.  This is an operating system issue.
>Having the process "get a new controlling terminal" is a Unix issue,
>as are devices like /dev/console.

But the point is that this is Unix. It says so in the subject line. This is
not anything about how to get this to work on VMS, Windows NTetc. This is
about Unix and Unix alone.


Greetings,







^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unix-daemons in Ada
  2001-07-06 14:15           ` Tarjei T. Jensen
@ 2001-07-06 17:05             ` Larry Kilgallen
  2001-07-06 22:33               ` Tarjei Tj�stheim Jensen
  0 siblings, 1 reply; 14+ messages in thread
From: Larry Kilgallen @ 2001-07-06 17:05 UTC (permalink / raw)


In article <9i4h63$73f1@news.kvaerner.com>, "Tarjei T. Jensen" <tarjei.jensen@kvaerner.com> writes:
> 
> Larry Kilgallen
>>That is what James Rogers said.  This is an operating system issue.
>>Having the process "get a new controlling terminal" is a Unix issue,
>>as are devices like /dev/console.
> 
> But the point is that this is Unix. It says so in the subject line. This is
> not anything about how to get this to work on VMS, Windows NTetc. This is
> about Unix and Unix alone.

My point (although possibly not that of James Rogers) is that this
problem should be no different for Ada than for any other language.
I would think it better addressed in a Unix newsgroup.

But this is not a big deal to me.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unix-daemons in Ada
  2001-07-06 17:05             ` Larry Kilgallen
@ 2001-07-06 22:33               ` Tarjei Tj�stheim Jensen
  2001-07-13  7:51                 ` Maxim Reznik
  0 siblings, 1 reply; 14+ messages in thread
From: Tarjei Tj�stheim Jensen @ 2001-07-06 22:33 UTC (permalink / raw)


Larry Kilgallen wrote:
> My point (although possibly not that of James Rogers) is that this
> problem should be no different for Ada than for any other language.
> I would think it better addressed in a Unix newsgroup.

Not really. It could be that there was a special convention for doing
this in Ada under Unix. He would not know unless he asked. Here.


Greetings,



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Unix-daemons in Ada
  2001-07-06 22:33               ` Tarjei Tj�stheim Jensen
@ 2001-07-13  7:51                 ` Maxim Reznik
  0 siblings, 0 replies; 14+ messages in thread
From: Maxim Reznik @ 2001-07-13  7:51 UTC (permalink / raw)


Just for reference.
I encountered the followin procedure in Glade 3.13p (file s-garrem.ads unit System.Garlic.Remote).

   ------------
   -- Detach --
   ------------

   procedure Detach is
      Dev_Null      : C.int;
      Dev_Null_Name : constant C.char_array := To_C ("/dev/null");
   begin
      Dev_Null := C_Open (Dev_Null_Name, O_Rdwr);
      C_Dup2 (Dev_Null, 0);
      C_Dup2 (Dev_Null, 1);
      C_Dup2 (Dev_Null, 2);
      C_Setsid;
   end Detach;

Does it help?


--
Maxim Reznik






^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2001-07-13  7:51 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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.
2001-07-02 19:18   ` Stefan Nobis
2001-07-02 20:16     ` David C. Hoos

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