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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,a91b527833b1772f X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!p25g2000hsf.googlegroups.com!not-for-mail From: Jacob Sparre Andersen Newsgroups: comp.lang.ada Subject: Re: Ada daemon Date: Tue, 29 Jul 2008 02:05:52 -0700 (PDT) Organization: http://groups.google.com Message-ID: <2d794119-2f5e-4f3a-b679-314b159d1b16@p25g2000hsf.googlegroups.com> References: <488a3916$0$15889$edfadb0f@dtext01.news.tele.dk> NNTP-Posting-Host: 195.249.127.231 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1217322352 882 127.0.0.1 (29 Jul 2008 09:05:52 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 29 Jul 2008 09:05:52 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: p25g2000hsf.googlegroups.com; posting-host=195.249.127.231; posting-account=UqUC7goAAAAaIopcWPSUmIYxkLwIeB6X User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; da; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:1369 Date: 2008-07-29T02:05:52-07:00 List-Id: Thomas wrote: > My good friend Dwight and I are wondering about how to create a daemon > in Ada. So far we've only been able to locate this example: > > http://www.pegasoft.ca/resources/boblap/16.html#16.26 > > which honestly looks both hideous and unpleasant, to us. Systems programming isn't always beautiful. The trick is to notice that almost all the code you see is actually a reusable library. Also, several of the subprograms in the library do actually exist in the POSIX packages, so there's no need to manually bind to the C versions of the subprograms. In practice the important part of daemonizing a process is to disconnect Standard_Input, Standard_Output and Standard_Error. You can do this with: POSIX.IO.Close (File => POSIX.IO.Standard_Input); POSIX.IO.Close (File => POSIX.IO.Standard_Output); POSIX.IO.Close (File => POSIX.IO.Standard_Error); > Yes, it might > be because we're just a bunch of beginners, but still we were hoping for > a "prettier", more Ada like approach. This one seems to rely on a lot of > pragma import C stuff. Using the POSIX standard packages definitely makes it prettier. > So, is this the only way to create a simple daemon, or can you point us > in other directions? Here's an (untested) implementation of Daemonize using "pure" Ada: with POSIX.IO, POSIX.Process_Environment, POSIX.Process_Identification, POSIX.Unsafe_Process_Primitives; procedure Daemonize is begin POSIX.Process_Environment.Change_Working_Directory ("/"); for File in POSIX.IO.File_Descriptor'Range loop if POSIX.IO.Is_Open (File) then POSIX.IO.Close (File); end if; end loop; case POSIX.Unsafe_Process_Primitives.Fork is when -1 => POSIX.Process_Primitives.Exit_Process (POSIX.Process_Primitives.Failed_Creation_Exit); when 0 => declare Session_Leader : POSIX.Process_Identification.Process_Group_ID; begin POSIX.Process_Identification.Create_Session (Session_Leader); end; when others => POSIX.Process_Primitives.Exit_Process; end case; end Daemonize; On Debian you need to install the package "libflorist-dev", to have access to the POSIX packages. /Jacob