<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>therning.org/ magnus &#187; exec</title>
	<atom:link href="http://therning.org/magnus/archives/tag/exec/feed" rel="self" type="application/rss+xml" />
	<link>http://therning.org/magnus</link>
	<description>Incoherent mumblings</description>
	<lastBuildDate>Thu, 12 Jan 2012 13:40:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Fork/exec in Haskell</title>
		<link>http://therning.org/magnus/archives/727</link>
		<comments>http://therning.org/magnus/archives/727#comments</comments>
		<pubDate>Fri, 21 Aug 2009 15:42:57 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[exec]]></category>
		<category><![CDATA[fork]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/?p=727</guid>
		<description><![CDATA[Here&#8217;s some simple code I put together. I&#8217;m mostly posting it so I won&#8217;t have any problems finding it in the future. module Main where &#160; import Control.Monad import System.Exit import System.Posix.IO import System.Posix.Process &#160; executeChild = do mapM_ closeFd &#91;stdInput, stdOutput, stdError&#93; devnull &#60;- openFd &#34;/dev/null&#34; ReadWrite Nothing defaultFileFlags dup devnull; dup devnull executeFile [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s some simple code I put together.  I&#8217;m mostly posting it so I won&#8217;t have any problems finding it in the future.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">module</span> Main <span style="color: #06c; font-weight: bold;">where</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Monad</span>
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span>Exit
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span>Posix<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">IO</span>
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span>Posix<span style="color: #339933; font-weight: bold;">.</span>Process
&nbsp;
executeChild <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
    <span style="font-weight: bold;">mapM_</span> closeFd <span style="color: green;">&#91;</span>stdInput<span style="color: #339933; font-weight: bold;">,</span> stdOutput<span style="color: #339933; font-weight: bold;">,</span> stdError<span style="color: green;">&#93;</span>
    devnull <span style="color: #339933; font-weight: bold;">&lt;-</span> openFd <span style="background-color: #3cb371;">&quot;/dev/null&quot;</span> ReadWrite Nothing defaultFileFlags
    dup devnull; dup devnull
    executeFile <span style="background-color: #3cb371;">&quot;./Child&quot;</span> False <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> Nothing
&nbsp;
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
    child <span style="color: #339933; font-weight: bold;">&lt;-</span> forkProcess executeChild
    <span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;ForkExec: main - forked, going to wait&quot;</span>
    s <span style="color: #339933; font-weight: bold;">&lt;-</span> getProcessStatus True True child
    <span style="color: #06c; font-weight: bold;">case</span> s <span style="color: #06c; font-weight: bold;">of</span>
        Nothing <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #5d478b; font-style: italic;">-- this shouldn't happen, ever</span>
            <span style="font-weight: bold;">print</span> s <span style="color: #339933; font-weight: bold;">&gt;&gt;</span>  exitFailure
        Just s <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">do</span>
            <span style="font-weight: bold;">print</span> s
            <span style="color: #06c; font-weight: bold;">case</span> s <span style="color: #06c; font-weight: bold;">of</span>
                Exited <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Child exited properly, though possibly unsuccessfully&quot;</span>
                Terminated <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Terminated!&quot;</span>
                Stopped <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Stopped (only SIGSTOP?)&quot;</span>
            exitSuccess
    exitFailure</pre></div></div>


<p>It&#8217;d be really nice to be able to, after the fork, close <em>all</em> open file descriptors in the child.  But how can I find all the open file descriptors in a process?  Ideally it should be fairly portable, though portability to major Unix/Linux systems is enough for me.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F727&amp;title=Fork%2Fexec%20in%20Haskell" id="wpa2a_2">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/727/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

