From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,cceb19dbd864b8da X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-07-01 14:47:18 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news.tele.dk!195.21.255.252!unlisys!news.snafu.de!isdnet!enst!enst.fr!not-for-mail From: "David C. Hoos, Sr." Newsgroups: comp.lang.ada Subject: Re: Unix-daemons in Ada Date: Sun, 1 Jul 2001 16:47:02 -0500 Organization: ENST, France Sender: comp.lang.ada-admin@ada.eu.org Message-ID: References: <87sngg3jp8.fsf@520075220525-0001.dialin.t-online.de> Reply-To: comp.lang.ada@ada.eu.org NNTP-Posting-Host: marvin.enst.fr Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_035C_01C1024D.73CF0910" X-Trace: avanie.enst.fr 994024037 77650 137.194.161.2 (1 Jul 2001 21:47:17 GMT) X-Complaints-To: usenet@enst.fr NNTP-Posting-Date: Sun, 1 Jul 2001 21:47:17 +0000 (UTC) To: Return-Path: X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org X-Mailman-Version: 2.0.4 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: comp.lang.ada mail<->news gateway List-Unsubscribe: , List-Archive: Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org Xref: archiver1.google.com comp.lang.ada:9303 Date: 2001-07-01T16:47:02-05:00 This is a multi-part message in MIME format. ------=_NextPart_000_035C_01C1024D.73CF0910 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit 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" Newsgroups: comp.lang.ada To: 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 > ------=_NextPart_000_035C_01C1024D.73CF0910 Content-Type: application/octet-stream; name="ss-net-daemon-daemon_init.adb" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="ss-net-daemon-daemon_init.adb" ------------------------------------------------------------------------ -- 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; ------=_NextPart_000_035C_01C1024D.73CF0910--