⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch17_16.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 17.15. Making a Daemon Server (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:44:44Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch17_01.htm"TITLE="17. Sockets"><LINKREL="prev"HREF="ch17_15.htm"TITLE="17.14. Writing a Multi-Homed Server"><LINKREL="next"HREF="ch17_17.htm"TITLE="17.16. Restarting a Server on Demand"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch17_15.htm"TITLE="17.14. Writing a Multi-Homed Server"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 17.14. Writing a Multi-Homed Server"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch17_01.htm"TITLE="17. Sockets"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch17_17.htm"TITLE="17.16. Restarting a Server on Demand"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.16. Restarting a Server on Demand"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch17-chap17_making_0">17.15. Making a Daemon Server</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-1942">Problem<ACLASS="indexterm"NAME="ch17-idx-1000004871-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004871-1"></A><ACLASS="indexterm"NAME="ch17-idx-1000004871-2"></A></A></H3><PCLASS="para">You want your program to run as a daemon.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-1948">Solution</A></H3><PCLASS="para">If you are paranoid and running as root, <CODECLASS="literal">chroot</CODE> to a safe directory:</P><PRECLASS="programlisting">chroot(&quot;/var/daemon&quot;)    or die &quot;Couldn't chroot to /var/daemon: $!&quot;;</PRE><PCLASS="para">Fork once, and let the parent exit.</P><PRECLASS="programlisting">$pid = fork;exit if $pid;die &quot;Couldn't fork: $!&quot; unless defined($pid);</PRE><PCLASS="para">Dissociate from the controlling terminal that started us and stop being part of whatever process group we had been a member of.</P><PRECLASS="programlisting">use POSIX;POSIX::setsid()    or die &quot;Can't start a new session: $!&quot;;</PRE><PCLASS="para">Trap fatal signals, setting a flag to indicate we need to gracefully exit.</P><PRECLASS="programlisting">$time_to_die = 0;sub signal_handler {    $time_to_die = 1;}$SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&amp;signal_handler;# trap or ignore $SIG{PIPE}</PRE><PCLASS="para">Wrap your actual server code in a loop:</P><PRECLASS="programlisting">until ($time_to_die) {    # ...}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-2002">Discussion</A></H3><PCLASS="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.</P><PCLASS="para">The <CODECLASS="literal">chroot</CODE> call is one of those non-POSIX calls. It makes a process change where it thinks the directory <CODECLASS="literal">/</CODE> is. For instance, after <CODECLASS="literal">chroot</CODE> <CODECLASS="literal">&quot;/var/daemon&quot;</CODE>, if the process tries to read the file <EMCLASS="emphasis">/etc/passwd</EM>, it will read <EMCLASS="emphasis">/var/daemon/etc/passwd</EM>. A <CODECLASS="literal">chroot</CODE>ed process needs copies of any files it will run made available inside its new <CODECLASS="literal">/</CODE>, of course. For instance, our chrooted process would need <EMCLASS="emphasis">/var/daemon/bin/csh</EM> if it were going to glob files. For security reasons, only the superuser may <CODECLASS="literal">chroot</CODE>. This is done by FTP servers if you login to them anonymously. It isn't really necessary to become a daemon.</P><PCLASS="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 <CODECLASS="literal">fork</CODE>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 <ACLASS="indexterm"NAME="ch17-idx-1000005969-0"></A><CODECLASS="literal">POSIX::setsid</CODE> to ensure that it is completely dissociated from its parent's terminal.</P><PCLASS="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 <CODECLASS="literal">%SIG</CODE> 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."</P><PCLASS="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 (<CODECLASS="literal">$SIG{PIPE}</CODE> <CODECLASS="literal">=</CODE> <CODECLASS="literal">'IGNORE'</CODE>) or define your own signal handler to deal with it appropriately.<ACLASS="indexterm"NAME="ch17-idx-1000004873-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004873-1"></A><ACLASS="indexterm"NAME="ch17-idx-1000004873-2"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-2016">See Also</A></H3><PCLASS="para">Your system's <ICLASS="filename">setsid </I>(2) and <ICLASS="filename">chroot </I>(1) manpage (if you have them); <ACLASS="olink"HREF="../prog/ch03_017.htm">&#13;the <CODECLASS="literal">chroot</CODE></A> function in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and in <ICLASS="filename">perlfunc </I>(1); the Unix Socket FAQ at <ACLASS="systemitem.url"HREF="http://www.ibrado.com/sock-faq/">http://www.ibrado.com/sock-faq/</A>. <CITECLASS="citetitle">Unix Network Programming</CITE></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch17_15.htm"TITLE="17.14. Writing a Multi-Homed Server"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 17.14. Writing a Multi-Homed Server"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch17_17.htm"TITLE="17.16. Restarting a Server on Demand"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.16. Restarting a Server on Demand"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">17.14. Writing a Multi-Homed Server</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">17.16. Restarting a Server on Demand</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -