<?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>chys&#039;s random notes &#187; Solaris</title>
	<atom:link href="http://en.chys.info/tag/solaris/feed/" rel="self" type="application/rss+xml" />
	<link>http://en.chys.info</link>
	<description>Study more problems; Talk less of isms.</description>
	<lastBuildDate>Tue, 27 Dec 2011 11:56:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Difference between dup(0) and open(&#8220;/dev/fd/0&#8243;,&#8230;);</title>
		<link>http://en.chys.info/2009/01/difference-between-dup0-and-opendevfd0/</link>
		<comments>http://en.chys.info/2009/01/difference-between-dup0-and-opendevfd0/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 06:40:00 +0000</pubDate>
		<dc:creator>chys</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Solaris]]></category>

		<guid isPermaLink="false">http://blog.chys.info/2009/01/difference-between-dup0-and-opendevfd0/</guid>
		<description><![CDATA[I believe APUE (2nd ed.; Sec. 3.16) is not correct. APUE says fd = open("/dev/fd/0", mode); is equivalent to fd = dup (0);, and mode is completely ignored. It seems this is the case in Solaris, but wrong in Linux. (I don’t have access to other Unices at this moment.) A test program: 01 #include [...]<hr/>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>I believe <a href="http://www.apuebook.com/">APUE</a> (2nd ed.; Sec. 3.16) is not correct.</p>
<p>APUE says <code>fd = open("/dev/fd/0", mode);</code> is equivalent to <code>fd = dup (0);</code>, and <code>mode</code> is completely ignored. It seems this is the case in Solaris, but wrong in Linux. (I don’t have access to other Unices at this moment.)</p>
<p>A test program:<br />
<blockquote>
<pre>01 #include &lt;unistd.h&gt;
02 #include &lt;fcntl.h&gt;
03
04 int main ()
05 {
06     close (0);
07     printf ("%dn", open ("a.txt", O_RDONLY)); // Should be 0
08     //int f2 = open ("/dev/fd/0", O_WRONLY);
09     int f2 = dup(0);
10     printf ("%dn", f2);
11     write (f2, "Hello worldn", 12);
12     return 0;
13 }</pre>
</blockquote>
<p>
Let’s run the program with an empty <code>a.txt</code>. Certainly the <code>write</code> function in Line 11 is going to fail.</p>
<p>Now, let’s comment out Line 9 and uncomment line 8 and try it again.</p>
<p>First I ran it in Solaris, the <code>write</code> call still failed. The behavior is like what APUE tells us.</p>
<p>Try it again in Linux &#8211; It was successful!</p>
<p>It seems that in Linux, <code>/dev/fd/0</code> is considered by <code>open</code> as nothing but a normal symlink to <code>a.txt</code>. So it returns a completely new descriptor instead of a duplicate of the old.</p>
<p>Let’s try it again with a shell script:<br />
<blockquote>
<pre>rm -f a.txt
touch a.txt
exec 0&lt;a.txt
exec 3&gt;/dev/fd/0
echo 'Hello world' &gt;&amp;3
cat a.txt</pre>
</blockquote>
<p>
Run it in Linux (with <a href="http://en.wikipedia.org/wiki/Debian_Almquist_shell">DASH</a> or BASH): Both outputed ‘Hello world’.</p>
<p>Run it in Solaris (with Bourne shell and BASH): Both failed, outputting nothing (Bourne shell) or failing with ‘Bad file number’ (BASH).</p>
<p>Conclusion:</p>
<p>(1) Solaris handles <code>/dev/fd/..</code> specially, as APUE tells us;</p>
<p>(2) Linux simply consider <code>/dev/fd/0</code> a symlink to the actual file.</p>
<p>(I’ll try later how Linux handles <code>open("/dev/fd/0",mode)</code> if the descriptor is an anonymous pipe or socket or something else that a normal symlink is unable to link to.</p>
<p>Kernels used in the above tests:</p>
<p>Linux: <code>Linux desktop 2.6.28-gentoo #4 SMP Mon Jan 12 17:39:23 CST 2009 x86_64 Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz GenuineIntel GNU/Linux</code></p>
<p>Solaris: <code>SunOS caesar 5.8 Generic_117350-51 sun4u sparc SUNW,Ultra-80 Solaris</code>
<div class="blogger-post-footer">
<hr />
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://creativecommons.org/images/public/somerights20.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License</a>.</div>
<hr/><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://en.chys.info/2009/01/difference-between-dup0-and-opendevfd0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

