> > Recipe 17.15. Making a Daemon Server (Perl Cookbook) NAME="DC.title" CONTENT="Perl Cookbook"> NAME="DC.creator" CONTENT="Tom Christiansen & Nathan Torkington"> NAME="DC.publisher" CONTENT="O'Reilly & Associates, Inc."> NAME="DC.date" CONTENT="1999-07-02T01:44:44Z"> NAME="DC.type" CONTENT="Text.Monograph"> NAME="DC.format" CONTENT="text/html" SCHEME="MIME"> NAME="DC.source" CONTENT="1-56592-243-3" SCHEME="ISBN"> NAME="DC.language" CONTENT="en-US"> NAME="generator" CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"> REV="made" HREF="mailto:online-books@oreilly.com" TITLE="Online Books Comments"> REL="up" HREF="ch17_01.htm" TITLE="17. Sockets"> REL="prev" HREF="ch17_15.htm" TITLE="17.14. Writing a Multi-Homed Server"> REL="next" HREF="ch17_17.htm" TITLE="17.16. Restarting a Server on Demand"> > BGCOLOR="#FFFFFF"> WIDTH="684" BORDER="0" CELLSPACING="0" CELLPADDING="0" > > ALIGN="LEFT" VALIGN="TOP" WIDTH="228" > CLASS="sect1" HREF="ch17_15.htm" TITLE="17.14. Writing a Multi-Homed Server" > SRC="../gifs/txtpreva.gif" ALT="Previous: 17.14. Writing a Multi-Homed Server" BORDER="0"> > > ALIGN="CENTER" VALIGN="TOP" WIDTH="228" > > FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1" > CLASS="chapter" REL="up" HREF="ch17_01.htm" TITLE="17. Sockets" > > > > > ALIGN="RIGHT" VALIGN="TOP" WIDTH="228" > CLASS="sect1" HREF="ch17_17.htm" TITLE="17.16. Restarting a Server on Demand" > SRC="../gifs/txtnexta.gif" ALT="Next: 17.16. Restarting a Server on Demand" BORDER="0"> > > > > > CLASS="sect1" > CLASS="sect1" > CLASS="title" NAME="ch17-chap17_making_0" >17.15. Making a Daemon Server > > CLASS="sect2" > CLASS="sect2" > CLASS="title" NAME="ch17-pgfId-1942" >Problem CLASS="indexterm" NAME="ch17-idx-1000004871-0" > > CLASS="indexterm" NAME="ch17-idx-1000004871-1" > > CLASS="indexterm" NAME="ch17-idx-1000004871-2" > > > > CLASS="para" >You want your program to run as a daemon. > > CLASS="sect2" > CLASS="sect2" > CLASS="title" NAME="ch17-pgfId-1948" >Solution > > CLASS="para" >If you are paranoid and running as root, CLASS="literal" >chroot > to a safe directory: > CLASS="programlisting" >chroot("/var/daemon") or die "Couldn't chroot to /var/daemon: $!"; > CLASS="para" >Fork once, and let the parent exit. > CLASS="programlisting" >$pid = fork; exit if $pid; die "Couldn't fork: $!" unless defined($pid); > CLASS="para" >Dissociate from the controlling terminal that started us and stop being part of whatever process group we had been a member of. > CLASS="programlisting" >use POSIX; POSIX::setsid() or die "Can't start a new session: $!"; > CLASS="para" >Trap fatal signals, setting a flag to indicate we need to gracefully exit. > CLASS="programlisting" >$time_to_die = 0; sub signal_handler { $time_to_die = 1; } $SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&signal_handler; # trap or ignore $SIG{PIPE} > CLASS="para" >Wrap your actual server code in a loop: > CLASS="programlisting" >until ($time_to_die) { # ... } > > CLASS="sect2" > CLASS="sect2" > CLASS="title" NAME="ch17-pgfId-2002" >Discussion > > CLASS="para" >Before POSIX, every operating system had its own way for a process to tell the operating system "I'm going it alone, please interfere with me as little as possible." POSIX makes it much cleaner. That said, you can still take advantage of any operating system-specific calls if you want to. > CLASS="para" >The CLASS="literal" >chroot > call is one of those non-POSIX calls. It makes a process change where it thinks the directory CLASS="literal" >/ > is. For instance, after CLASS="literal" >chroot > CLASS="literal" >"/var/daemon" >, if the process tries to read the file CLASS="emphasis" >/etc/passwd >, it will read CLASS="emphasis" >/var/daemon/etc/passwd >. A CLASS="literal" >chroot >ed process needs copies of any files it will run made available inside its new CLASS="literal" >/ >, of course. For instance, our chrooted process would need CLASS="emphasis" >/var/daemon/bin/csh > if it were going to glob files. For security reasons, only the superuser may CLASS="literal" >chroot >. This is done by FTP servers if you login to them anonymously. It isn't really necessary to become a daemon. > CLASS="para" >The operating system expects a child's parent to wait when the child dies. Our daemon process has no particular parent to do this, so we need to disinherit it. This we do by CLASS="literal" >fork >ing once and having our parent exit, so that the child is not associated with the process that started the parent. The child then closes all the filehandles it got from its parent (STDIN, STDERR, and STDOUT) and calls CLASS="indexterm" NAME="ch17-idx-1000005969-0" > > CLASS="literal" >POSIX::setsid > to ensure that it is completely dissociated from its parent's terminal. > CLASS="para" >Now we're almost ready to begin. We don't want signals like SIGINT to kill us immediately (its default behavior), so we use CLASS="literal" >%SIG > to catch them and set a flag saying it's time to exit. Then our main program simply becomes: "While we weren't killed, do something." > CLASS="para" >The signal SIGPIPE is a special case. It's easy to get (by writing to a filehandle whose other end is closed) and has unforgiving default behavior (it terminates your process). You probably want to either ignore it ( CLASS="literal" >$SIG{PIPE} > CLASS="literal" >= > CLASS="literal" >'IGNORE' >) or define your own signal handler to deal with it appropriately. CLASS="indexterm" NAME="ch17-idx-1000004873-0" > > CLASS="indexterm" NAME="ch17-idx-1000004873-1" > > CLASS="indexterm" NAME="ch17-idx-1000004873-2" > > > > CLASS="sect2" > CLASS="sect2" > CLASS="title" NAME="ch17-pgfId-2016" >See Also > > CLASS="para" >Your system's CLASS="filename" >setsid >(2) and CLASS="filename" >chroot >(1) manpage (if you have them); CLASS="olink" HREF="../prog/ch03_017.htm" >
the CLASS="literal" >chroot > > function in CLASS="olink" HREF="../prog/ch03_01.htm" >Chapter 3 > of CLASS="citetitle" HREF="../prog/index.htm" TITLE="Programming Perl" > CLASS="citetitle" >Programming Perl > > and in CLASS="filename" >perlfunc >(1); the Unix Socket FAQ at CLASS="systemitem.url" HREF="http://www.ibrado.com/sock-faq/" >http://www.ibrado.com/sock-faq/ >. CLASS="citetitle" >Unix Network Programming > > > > CLASS="htmlnav" > > > ALIGN="LEFT" WIDTH="684" TITLE="footer"> WIDTH="684" BORDER="0" CELLSPACING="0" CELLPADDING="0" > > ALIGN="LEFT" VALIGN="TOP" WIDTH="228" > CLASS="sect1" HREF="ch17_15.htm" TITLE="17.14. Writing a Multi-Homed Server" > SRC="../gifs/txtpreva.gif" ALT="Previous: 17.14. Writing a Multi-Homed Server" BORDER="0"> > > ALIGN="CENTER" VALIGN="TOP" WIDTH="228" > CLASS="book" HREF="index.htm" TITLE="Perl Cookbook" > SRC="../gifs/txthome.gif" ALT="Perl Cookbook" BORDER="0"> > > ALIGN="RIGHT" VALIGN="TOP" WIDTH="228" > CLASS="sect1" HREF="ch17_17.htm" TITLE="17.16. Restarting a Server on Demand" > SRC="../gifs/txtnexta.gif" ALT="Next: 17.16. Restarting a Server on Demand" BORDER="0"> > > > > ALIGN="LEFT" VALIGN="TOP" WIDTH="228" >17.14. Writing a Multi-Homed Server > ALIGN="CENTER" VALIGN="TOP" WIDTH="228" > CLASS="index" HREF="index/index.htm" TITLE="Book Index" > SRC="../gifs/index.gif" ALT="Book Index" BORDER="0"> > > ALIGN="RIGHT" VALIGN="TOP" WIDTH="228" >17.16. Restarting a Server on Demand > > > ALIGN="LEFT" WIDTH="684" TITLE="footer"> SIZE="-1" > > >