<?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>BASH Cures Cancer &#187; kernel</title>
	<atom:link href="http://bashcurescancer.com/category/kernel/feed" rel="self" type="application/rss+xml" />
	<link>http://bashcurescancer.com</link>
	<description>Learn the UNIX/Linux command line</description>
	<lastBuildDate>Tue, 25 Oct 2011 18:09:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Why do CGI scripts and shell scripts fail when they contain carriage returns?</title>
		<link>http://bashcurescancer.com/why-do-cgi-scripts-and-shell-scripts-fail-when-they-contain-carriage-returns.html</link>
		<comments>http://bashcurescancer.com/why-do-cgi-scripts-and-shell-scripts-fail-when-they-contain-carriage-returns.html#comments</comments>
		<pubDate>Tue, 08 Jan 2008 05:02:31 +0000</pubDate>
		<dc:creator>Brock Noland</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[way too much information]]></category>
		<category><![CDATA[why?]]></category>

		<guid isPermaLink="false">http://bashcurescancer.com/why-do-cgi-scripts-and-shell-scripts-fail-when-they-contain-carriage-returns.html</guid>
		<description><![CDATA[Often users of CGI scripts encounter the dreaded &#8220;premature end of script headers&#8221; error. A quick Google search on that phrase proves this to be true. The cause of this, is often the same as the &#8220;bad interpreter&#8221; error received on the command line. A very common cause is where the file was created on [...]]]></description>
			<content:encoded><![CDATA[<p> Often users of CGI scripts encounter the dreaded &#8220;<strong>premature end of script headers</strong>&#8221; error.  A quick Google search on that phrase proves this to be true. The cause of this, is often the same as the &#8220;bad interpreter&#8221; error received on the command line.</p>
<p>A very common cause is  where the file was created on a Windows host and then uploaded to a Unix host for execution. (Think of the millions of websites using shared hosting.) The problem here, as no doubt you maybe aware, is that the file contains the dreaded &#8220;carriage return&#8221; before every newline. This is so common, there is even a standard command <a href="http://bashcurescancer.com/man/cmd/dos2unix">dos2unix</a> for converting these files.</p>
<p>However, I had yet to see a reason as to WHY that carriage return after the <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)" target="_blank">hash bang</a> causes a problem.  I just accepted it as fact.  Today, I decided to figure out why and where it failed.</p>
<p>As it turns out, it fails in the kernel. To figure this out, I downloaded the latest kernel (2.6.23.12) to my RHEL 5 host and compiled it with the standard config file that comes with RHEL 5. The configuration file was for a much older kernel, but worked for my purposes.  I booted it to make sure I had a working copy of 2.6.23.12.  Then I went into the kernel source tree and started modifying stuff!</p>
<p>To be sure, I am no kernel hacker, nor even a C hacker.  I had never successfully modified the kernel source, though I haven&#8217;t tried in many years.  But after three compilations, I was able to figure out where in (2.6.23.12) exec system call was taking place and was able to insert some code to print a custom message. The file I modifed was &#8220;fs/exec.c&#8221;:</p>
<p>[root@test1 linux-2.6.23.12]# ls -l fs/exec.c<br />
-rw-rw-r&#8211; 1 root root 42231 Jan  7 17:44 fs/exec.c</p>
<p>In that file, inside the function search_binary_handler() I inserted the following code:</p>
<p>1139          printk(&#8220;{&#8220;);<br />
1140          int z = 0;<br />
1141          for(; z &lt; strlen(bprm-&gt;buf); z++) {<br />
1142                  if(bprm-&gt;buf[z] == &#8216;\r&#8217;) {<br />
1143                          printk(&#8220;(carriage return)&#8221;);<br />
1144                  } else {<br />
1145                          printk(&#8220;%c&#8221;, bprm-&gt;buf[z]);<br />
1146                  }<br />
1147          }<br />
1148          printk(&#8220;}\n&#8221;);</p>
<p>The line numbers may be slightly off as I had inserted and removed code above. After compliation and booting, I was able to give it a shot. I created a sample script with a carriage return after the hash bang interpreter portion.</p>
<p>[root@test1 ~]# echo -e &#8216;#!/bin/bash\r\n/usr/bin/id&#8217; &gt;id.sh<br />
[root@test1 ~]# chmod +x id.sh</p>
<p>And BAM, it worked!</p>
<p>[root@test1 ~]# ./id.sh<br />
{#!/bin/bash(carriage return)}<br />
-bash: ./id.sh: /bin/bash^M: bad interpreter: No such file or directory</p>
<p>As you can see, the kernel was actually trying to execute &#8220;/bin/bash\r&#8221; which of course, not a valid file.  Here&#8217;s a screen shot:</p>
<p><img src="http://bashcurescancer.com/media/misc/why-bad-interpreter-premature-end-of-script-headers.png" alt="Why do CGI Scripts and Shell Scripts fail when they contain carriage returns" height="356" width="640" /></p>
<p>Update: In response to reader requests, I wrote an explanation of my investigation: <a href="http://bashcurescancer.com/on-the-case-of-carriage-returns-and-kernel-exec-function-calls.html">On the case of carriage returns and kernel exec system calls</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://bashcurescancer.com/why-do-cgi-scripts-and-shell-scripts-fail-when-they-contain-carriage-returns.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.204 seconds -->

