<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>It takes guts to make mistakes</title>
	<atom:link href="http://jineshkj.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jineshkj.wordpress.com</link>
	<description>This blog contains answers to my questions</description>
	<lastBuildDate>Fri, 18 Nov 2011 14:29:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jineshkj.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>It takes guts to make mistakes</title>
		<link>http://jineshkj.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jineshkj.wordpress.com/osd.xml" title="It takes guts to make mistakes" />
	<atom:link rel='hub' href='http://jineshkj.wordpress.com/?pushpress=hub'/>
		<item>
		<title>select() vs pselect()</title>
		<link>http://jineshkj.wordpress.com/2008/02/02/why-pselect/</link>
		<comments>http://jineshkj.wordpress.com/2008/02/02/why-pselect/#comments</comments>
		<pubDate>Sat, 02 Feb 2008 16:55:00 +0000</pubDate>
		<dc:creator>Jinesh K J</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[pselect]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[system call]]></category>

		<guid isPermaLink="false">http://jineshkj.wordpress.com/?p=19</guid>
		<description><![CDATA[All this time I had thought pselect() is simply a luxury, a waste of system call entry, something that i never expected from open source community. I should have blamed POSIX for introducing such a one, but, why would any open source operating system implement it. If you ever wanted to block a set of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=19&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>All this time I had thought pselect() is simply a luxury, a waste of system call entry, something that i never expected from open source community. I should have blamed POSIX for introducing such a one, but, why would any open source operating system implement it. If you ever wanted to block a set of signal, just calling sigprocmask() before select would be sufficient.</p>
<p>This is where the need for &#8216;!&#8217; operator helps. ie, right, pselect() is not meant for blocking signals,  but rather to unblock them <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  . Such a requirement arises out in event loops, which  normally anyone would expect to be implemented as:</p>
<p>while(1)<br />
{</p>
<blockquote><p>if(need_to_quit)</p>
<blockquote><p>break;</p></blockquote>
<p>if(select(&#8230;) == -1)<br />
{</p>
<blockquote><p> if(errno == EINTR)</p>
<blockquote><p> continue;</p></blockquote>
<p>&#8230;</p></blockquote>
<p>}<br />
&#8230;</p></blockquote>
<p>}</p>
<p>and in the SIGQUIT signal handler you would write :</p>
<p>sigquit_handler()<br />
{</p>
<blockquote><p> need_to_quit  = 1;</p></blockquote>
<p>}</p>
<p>The problem with such an approach is that if the event comes and get handled after the global volatile variable &#8216;need_to_quit&#8217; is checked, but before select() is called, then the event loop would behave as if it has lost the event, and the select() will block till we again get an event.</p>
<p>POSIX recommends the solution to be to use pselect(). Now, how to use it would be what you are thinking, so, here it goes:</p>
<p>step 1 : you block all signals and save the current sigmask<br />
step 2 : check the event condition and do what is required<br />
step 3 : call pselect() and pass it a signal mask to enable all the signals that would provide you the events. when pselect() returns, it will restore the sigmask it had when it was entered(ie, here all the signals masked).<br />
step 4 : you restore the old signal mask</p>
<p>thats it, the race condition is solved. This now makes sure that any signal that comes after checking the variable will be received only after select() has entered. You might now be wondering why we can not do the same thing by writing our own function than a system call. Well here is how it would look :</p>
<p>pselect(..)<br />
{</p>
<blockquote><p>sigprocmask(.., &amp;new_mask, &amp;old_mask); // enable the signals to be received</p>
<p>select();</p>
<p>sigprocmask(.., &amp;old_mask, null);</p></blockquote>
<p>}</p>
<p>YES.. If you have noticed, this still gives you a race condition when a signal arrives before select() is called and gets handled immediately after we set new_mask. So, this is the trick with pselect() : Once you call a system call(enter kernel space), you can not be interrupted by anyone else. ie, you can stay in the kernel mode as long as you want; inside the implementation of the system call, we can sleep on blocking functions which can get unblocked on signals if and only if the programmer wishes to do so.</p>
<p>So, the above shown code works when we place it in as a system call; being in kernel mode is itself  enough to prevent the race condition <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  . So, if we are using pselect(), this is how our event loop will look like :</p>
<p><code></code></p>
<p>sigset_t new_set, old_set;<br />
int ret;</p>
<p>sigfillmask(&amp;new_set);<br />
sigprocmask(.., null, &amp;old_set);<br />
while(1)<br />
{</p>
<blockquote><p> sigprocmask(.., &amp;new_set, null);</p>
<p>if(need_to_quit)</p>
<blockquote><p>break;</p></blockquote>
<p>ret = pselect(.., &amp;old_set) ;</p>
<p>sigprocmask(.., &amp;old_set, null);</p>
<p>if(ret == -1)<br />
{</p>
<blockquote><p> if(errno == EINTR)</p>
<blockquote><p> continue;</p></blockquote>
<p>&#8230;</p></blockquote>
<p>}</p>
<p>&#8230;</p>
<p>sigprocmask(.., &amp;old_set, null);</p></blockquote>
<p>}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jineshkj.wordpress.com/19/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jineshkj.wordpress.com/19/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jineshkj.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jineshkj.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jineshkj.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jineshkj.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jineshkj.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jineshkj.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jineshkj.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jineshkj.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jineshkj.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jineshkj.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jineshkj.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jineshkj.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jineshkj.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jineshkj.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=19&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jineshkj.wordpress.com/2008/02/02/why-pselect/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ece9a0480646bde6908cfafca49d8feb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jineshkj</media:title>
		</media:content>
	</item>
		<item>
		<title>Reversing a linked list</title>
		<link>http://jineshkj.wordpress.com/2007/08/19/reversing-a-linked-list/</link>
		<comments>http://jineshkj.wordpress.com/2007/08/19/reversing-a-linked-list/#comments</comments>
		<pubDate>Sun, 19 Aug 2007 07:50:02 +0000</pubDate>
		<dc:creator>Jinesh K J</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jineshkj.wordpress.com/2007/08/19/reversing-a-linked-list/</guid>
		<description><![CDATA[So again, here is how you can reverse a singly-linked list : reverse(struct node ** start) { struct node * prev = NULL; struct node * curr = *start; while(curr) { swap(&#38;prev, &#38;curr-&#62;next); swap(&#38;curr, &#38;prev); } *start = prev; } and here is for a doubly-linked list : reverse(struct node **start) { struct node *curr [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=18&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So again, here is how you can reverse a singly-linked list :</p>
<p>reverse(struct node ** start)<br />
{</p>
<blockquote><p> struct node * prev = NULL;<br />
struct node * curr = *start;</p>
<p>while(curr)<br />
{</p>
<blockquote><p> swap(&amp;prev, &amp;curr-&gt;next);<br />
swap(&amp;curr, &amp;prev);</p></blockquote>
<p>}<br />
*start = prev;</p></blockquote>
<p>}<br />
and here is for a doubly-linked list :</p>
<p>reverse(struct node **start)<br />
{</p>
<blockquote><p> struct node *curr = *start;</p>
<p>while(curr)<br />
{</p>
<blockquote><p> swap(&amp;curr-&gt;prev, &amp;curr-&gt;next);<br />
if(!curr-&gt;prev)</p>
<blockquote><p>*start = curr;</p></blockquote>
<p>curr = curr-&gt;prev;</p></blockquote>
<p>}</p></blockquote>
<p>}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jineshkj.wordpress.com/18/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jineshkj.wordpress.com/18/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jineshkj.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jineshkj.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jineshkj.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jineshkj.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jineshkj.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jineshkj.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jineshkj.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jineshkj.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jineshkj.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jineshkj.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jineshkj.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jineshkj.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jineshkj.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jineshkj.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=18&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jineshkj.wordpress.com/2007/08/19/reversing-a-linked-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ece9a0480646bde6908cfafca49d8feb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jineshkj</media:title>
		</media:content>
	</item>
		<item>
		<title>Power of 2</title>
		<link>http://jineshkj.wordpress.com/2007/08/17/power-of-2/</link>
		<comments>http://jineshkj.wordpress.com/2007/08/17/power-of-2/#comments</comments>
		<pubDate>Fri, 17 Aug 2007 14:09:52 +0000</pubDate>
		<dc:creator>Jinesh K J</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jineshkj.wordpress.com/2007/08/17/power-of-2/</guid>
		<description><![CDATA[This is out of popular request.. nothing special about it. Below is a func that tells you whether the given number is a power of 2 : int isPower(int i) { return !(i &#38; (-i ^ i)); }<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=17&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is out of popular request.. nothing special about it. Below is a func that tells you whether the given number is a power of 2 :</p>
<p><code></p>
<p>int isPower(int i)<br />
{</p>
<blockquote><p>return  !(i &amp; (-i ^ i));</p></blockquote>
<p>}</p>
<p></code></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jineshkj.wordpress.com/17/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jineshkj.wordpress.com/17/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jineshkj.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jineshkj.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jineshkj.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jineshkj.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jineshkj.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jineshkj.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jineshkj.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jineshkj.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jineshkj.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jineshkj.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jineshkj.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jineshkj.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jineshkj.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jineshkj.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=17&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jineshkj.wordpress.com/2007/08/17/power-of-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ece9a0480646bde6908cfafca49d8feb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jineshkj</media:title>
		</media:content>
	</item>
		<item>
		<title>A game with array</title>
		<link>http://jineshkj.wordpress.com/2007/08/05/a-game-with-array/</link>
		<comments>http://jineshkj.wordpress.com/2007/08/05/a-game-with-array/#comments</comments>
		<pubDate>Sun, 05 Aug 2007 08:04:26 +0000</pubDate>
		<dc:creator>Jinesh K J</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jineshkj.wordpress.com/2007/08/05/a-game-with-array/</guid>
		<description><![CDATA[Hi &#8230; I know that its been too long since I had last posted into my blog and the main reason for that was a lack of interesting topic to discuss on my page. But last day I happen to meet a person(named Rajesh) who coined a simple question of re-ordering numbers in an array. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=16&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hi &#8230; I know that its been too long since I had last posted into my blog and the main reason for that was a lack of interesting topic to discuss on my page. But last day I happen to meet a person(named Rajesh) who coined a simple question of re-ordering numbers in an array.</p>
<p>The situation is like this: we have an array of integers which contains randomly ordered numbers. We would wanna move all odd numbers to the beginning of the array and the even numbers to the end &#8211; still the ordering doesn&#8217;t matter. Below is the code that I believe would do the job in the least number of iterations:</p>
<p><code></p>
<p>int arr[]={1,2,3,4,5,6,7,8,9,10};</p>
<p>swap(int *p, int *q)<br />
{</p>
<blockquote><p>
  *p = *p ^ *q;<br />
  *q = *p ^ *q;<br />
  *p = *p ^ *q;
</p></blockquote>
<p>}</p>
<p>int checkeven(int value)<br />
{</p>
<blockquote><p> return !(value &amp; 0x1);</p></blockquote>
<p>}</p>
<p>int checkodd(int value)<br />
{</p>
<blockquote><p>  return (value &amp; 0x1);</p></blockquote>
<p>}</p>
<p>int *find(int *start, int *end, int step, int (*cond)(int) )<br />
{</p>
<blockquote><p>
  while(((step &gt; 0) &amp;&amp; (start &lt;= end)) || ((step &lt; 0) &amp;&amp; (end &lt;= start)))<br />
    {</p>
<blockquote><p>
      if(cond(*start))</p>
<blockquote><p>break;</p></blockquote>
<p>      start += step;
</p></blockquote>
<p>    }</p>
<p>  return start;
</p></blockquote>
<p>}</p>
<p>reorder(int *start, int size)<br />
{</p>
<blockquote><p>
 int *end = start + size - 1;</p>
<p>  while(1)<br />
    {</p>
<blockquote><p>
     // find an even no. starting from the front<br />
      start = find(start, end, 1, checkeven);</p>
<p>      //find an odd no. starting from the back<br />
      end = find(end, start, -1, checkodd);</p>
<p>      if(start &gt;= end)</p>
<blockquote><p>break;</p></blockquote>
<p>      swap(start, end);
</p></blockquote>
<p>    }</p></blockquote>
<p>}</p>
<p>main()<br />
{</p>
<blockquote><p>
  reorder(arr, sizeof(arr)/sizeof(arr[0]));
</p></blockquote>
<p>}</p>
<p></code></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jineshkj.wordpress.com/16/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jineshkj.wordpress.com/16/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jineshkj.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jineshkj.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jineshkj.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jineshkj.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jineshkj.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jineshkj.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jineshkj.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jineshkj.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jineshkj.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jineshkj.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jineshkj.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jineshkj.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jineshkj.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jineshkj.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=16&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jineshkj.wordpress.com/2007/08/05/a-game-with-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ece9a0480646bde6908cfafca49d8feb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jineshkj</media:title>
		</media:content>
	</item>
		<item>
		<title>Listing out the sysconf() values</title>
		<link>http://jineshkj.wordpress.com/2007/05/24/listing-out-the-sysconf-values/</link>
		<comments>http://jineshkj.wordpress.com/2007/05/24/listing-out-the-sysconf-values/#comments</comments>
		<pubDate>Thu, 24 May 2007 13:52:22 +0000</pubDate>
		<dc:creator>Jinesh K J</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://jineshkj.wordpress.com/2007/05/24/listing-out-the-sysconf-values/</guid>
		<description><![CDATA[Ever wondered how you could list out the defined sysconf() arguments constants and the sysconf() values for them. Well here is a simple bash script that worked for me: #!/bin/bash rm -f test.c echo &#8220;#include &#60;unistd.h&#62;&#8221; &#62;&#62; test.c echo &#8220;#include &#60;stdio.h&#62;&#8221; &#62;&#62; test.c echo &#8220;main()&#8221; &#62;&#62; test.c echo &#8220;{&#8221; &#62;&#62; test.c for x in `echo [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=15&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ever wondered how you could list out the defined sysconf() arguments constants and the sysconf() values for them. Well here is a simple bash script that worked for me:</p>
<p><code></code></p>
<p>#!/bin/bash</p>
<p>rm -f test.c</p>
<p>echo &#8220;#include &lt;unistd.h&gt;&#8221; &gt;&gt; test.c<br />
echo &#8220;#include &lt;stdio.h&gt;&#8221; &gt;&gt; test.c<br />
echo &#8220;main()&#8221; &gt;&gt; test.c<br />
echo &#8220;{&#8221; &gt;&gt; test.c</p>
<p>for x in `echo &#8220;#include  &lt;unistd.h&gt;&#8221; | cpp | grep _SC_ | cut -d &#8216;,&#8217; -f 1 | awk &#8216;{print $1}&#8217;`<br />
do<br />
echo &#8220;printf(\&#8221;$x : %d\\n\&#8221;, sysconf($x));&#8221; &gt;&gt; test.c<br />
done</p>
<p>echo &#8220;}&#8221; &gt;&gt; test.c</p>
<p>gcc test.c -o test</p>
<p>./test</p>
<p>rm -f test{,.c}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jineshkj.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jineshkj.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jineshkj.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jineshkj.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jineshkj.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jineshkj.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jineshkj.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jineshkj.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jineshkj.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jineshkj.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jineshkj.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jineshkj.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jineshkj.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jineshkj.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jineshkj.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jineshkj.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=15&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jineshkj.wordpress.com/2007/05/24/listing-out-the-sysconf-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ece9a0480646bde6908cfafca49d8feb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jineshkj</media:title>
		</media:content>
	</item>
		<item>
		<title>ati-drivers on gentoo linux</title>
		<link>http://jineshkj.wordpress.com/2007/03/31/ati-drivers-on-gentoo-linux/</link>
		<comments>http://jineshkj.wordpress.com/2007/03/31/ati-drivers-on-gentoo-linux/#comments</comments>
		<pubDate>Sat, 31 Mar 2007 09:32:42 +0000</pubDate>
		<dc:creator>Jinesh K J</dc:creator>
				<category><![CDATA[Gentoo]]></category>

		<guid isPermaLink="false">http://jineshkj.wordpress.com/2007/03/31/ati-drivers-on-gentoo-linux/</guid>
		<description><![CDATA[So with much hard work I have successfully installed the great gentoo linux on my laptop (thanks to the LFS; it was a very useful experience). Unfortunately I have an amd turion based system and many packages in the gentoo portage still are masked with ~amd64 keyword. But, still I could install the complete gentoo [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=13&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So with much hard work I have successfully installed the great gentoo linux on my laptop (thanks to the LFS; it was a very useful experience). Unfortunately I have an amd turion based system and many packages in the gentoo portage still are masked with ~amd64 keyword. But, still I could install the complete gentoo without much trouble except with the display hardware which is the ATI Radeon XPRESS 200.</p>
<p>As usual, I did an emerge on the ati-drivers package which gave me pretty much all the normal output except the following:</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; ACCESS VIOLATION SUMMARY &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
LOG FILE = &#8220;/var/log/sandbox/sandbox-x11-drivers_-_ati-drivers-8.32.5-11100.log&#8221;</p>
<p>open_wr:   /usr/src/linux-2.6.19-gentoo-r5/-.gcda<br />
open_wr:   /usr/src/linux-2.6.19-gentoo-r5/-.gcda<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>With a lot of googling, I found that problem is with the sandbox USE flag and the solution seem to be adding the following lines to the /etc/portage/package.use :</p>
<p>x11-drivers/ati-drivers -sandbox -usersandbox</p>
<p>I hope this information would be useful to someone else too..</p>
<p>bye..</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jineshkj.wordpress.com/13/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jineshkj.wordpress.com/13/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jineshkj.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jineshkj.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jineshkj.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jineshkj.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jineshkj.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jineshkj.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jineshkj.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jineshkj.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jineshkj.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jineshkj.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jineshkj.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jineshkj.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jineshkj.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jineshkj.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=13&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jineshkj.wordpress.com/2007/03/31/ati-drivers-on-gentoo-linux/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ece9a0480646bde6908cfafca49d8feb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jineshkj</media:title>
		</media:content>
	</item>
		<item>
		<title>Macro arguments &#8211; be careful with them</title>
		<link>http://jineshkj.wordpress.com/2007/01/08/macro-arguments-be-careful-with-them/</link>
		<comments>http://jineshkj.wordpress.com/2007/01/08/macro-arguments-be-careful-with-them/#comments</comments>
		<pubDate>Mon, 08 Jan 2007 07:16:45 +0000</pubDate>
		<dc:creator>Jinesh K J</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://jineshkj.wordpress.com/2007/01/08/macro-arguments-be-careful-with-them/</guid>
		<description><![CDATA[Debugging applications that has lots of macro()s are considered to be very tough. And it seems that most people prefer to expand macros as blocks with new temporary variables. Let us try to find out why one would not prefer to use the macro arguments directly in their block code. Consider for example a macro [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=12&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Debugging applications that has lots of macro()s are considered to be very tough. And it seems that most people prefer to expand macros as blocks with new temporary variables. Let us try to find out why one would not prefer to use the macro arguments directly in their block code. Consider for example a macro that returns the square of a number. Normally, any careful programmer would write the macro as given below:</p>
<blockquote><p><code>#define square(a) ((a) * (a))</code></p></blockquote>
<p>And yes, what the macro will do is precisely what we would expect of it. The above macro is just perfect as long as what we provide as argument to it is a plain value &#8211; like a variable or a constant. For example:</p>
<blockquote><p><code>int i = 10;</code></p></blockquote>
<blockquote><p><code>printf("square : %d\n", square(i));</code></p></blockquote>
<p>would print 100 as we had desired. The preprocessed text is just the expression ((i) * (i)). This will continue to go on fine until you start to do some fancy tricks like the following:</p>
<blockquote><p><code>int i = 10;</code></p></blockquote>
<blockquote><p><code>printf("square : %d\n", square(++i));</code></p></blockquote>
<p>Obviously, what we had expected was the square of 11 but the one we got was that of 12(for gcc, can change for others). So, how did that happen? Yes.. after preprocessing, your code became ((++i) * (++i)) and according to gcc, the values of i used in the expression is that after doing all the pre-increments in the expression. Since there are 2 pre-increments, the compiler takes the expression as ((12) * (12)).</p>
<p>Apart from the wrong answer we got, another side effect that we have to acknowledge is that the variable i has been incremented twice after calling square(), instead of just once. So, what is the solution for such a situation? Simple, use only dummy variables for computations:</p>
<blockquote><p><code>#define square(a) ({ typeof(a) var=(a); var*var; })</code></p></blockquote>
<p>so thats it ! you dont need to worry at all about the new variable that you have created above &#8211; compilers are intelligent enough to nullify its overhead. Well, so now our problem is solved <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  we can create new ones <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jineshkj.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jineshkj.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jineshkj.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jineshkj.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jineshkj.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jineshkj.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jineshkj.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jineshkj.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jineshkj.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jineshkj.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jineshkj.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jineshkj.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jineshkj.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jineshkj.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jineshkj.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jineshkj.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=12&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jineshkj.wordpress.com/2007/01/08/macro-arguments-be-careful-with-them/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ece9a0480646bde6908cfafca49d8feb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jineshkj</media:title>
		</media:content>
	</item>
		<item>
		<title>The essence of do{ } while(0)</title>
		<link>http://jineshkj.wordpress.com/2007/01/06/the-essence-of-do-while0/</link>
		<comments>http://jineshkj.wordpress.com/2007/01/06/the-essence-of-do-while0/#comments</comments>
		<pubDate>Sat, 06 Jan 2007 11:26:24 +0000</pubDate>
		<dc:creator>Jinesh K J</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://jineshkj.wordpress.com/2007/01/06/the-essence-of-do-while0/</guid>
		<description><![CDATA[This is something that I&#8217;ve been longing to document in my blog. Many times I have wondered why almost all the #define macros in linux kernel is made up of do-while blocks rather than simple {} blocks. The reason is illustrated below: Consider for example a swap() macro defined in the two different ways as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=11&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is something that I&#8217;ve been longing to document in my blog. Many times I have wondered why almost all the #define macros in linux kernel is made up of do-while blocks rather than simple {} blocks. The reason is illustrated below:</p>
<p>Consider for example a swap() macro defined in the two different ways as shown below:</p>
<p><code>#define swap(a,b) { \</code></p>
<blockquote><p><code>typeof(a) tmp; \</code></p>
<p><code>tmp=a; a=b; b=tmp; \</code></p>
<p><code>}</code></p></blockquote>
<p>and</p>
<p><code>#define swap(a,b) do { \</code></p>
<blockquote><p><code>typeof(a) tmp; \</code></p>
<p><code>tmp=a;a=b;b=tmp; \</code></p>
<p><code>} while(0)</code></p></blockquote>
<p>At a glance both seem to do the same job of creating a new block of code where the variable tmp is created and the values a and b are swapped through it. And they both will work similarly under all circumstances except the following:</p>
<p><code>if(a&lt;b)</code></p>
<blockquote><p><code>swap(a,b);</code></p></blockquote>
<p><code>else</code></p>
<blockquote><p><code>printf("a is already greater than b\n");</code></p></blockquote>
<p>The above code when using the first form of swap() macro will expand as following:</p>
<p><code>if(a&lt;b)</code></p>
<p><code>{</code></p>
<blockquote><p><code>typeof(a) tmp;</code></p>
<p><code>tmp=a;a=b;b=tmp;</code></p></blockquote>
<p><code>}</code></p>
<p><code>;</code></p>
<p><code>else</code></p>
<blockquote><p><code>printf("a is already greater than b\n");</code></p></blockquote>
<p>The issue here is that the semicolon (;) that we have put after swap() infact terminates the if block and remains as an empty statement. This causes the else keyword not to be attached to the if block anymore and will cause a compilation error saying that the else keyword is misplaced. huh !</p>
<p>Anyway, good that we got a workaround for this problem, otherwise writing system software would have become a hell <img src='http://s0.wp.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' />  . By the way, if you are using gcc, you can use ({ }) blocks as well which would be very much helpful if your macro needs to return some value <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  &#8211; the return value is the last evaluated expression.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jineshkj.wordpress.com/11/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jineshkj.wordpress.com/11/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jineshkj.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jineshkj.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jineshkj.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jineshkj.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jineshkj.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jineshkj.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jineshkj.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jineshkj.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jineshkj.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jineshkj.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jineshkj.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jineshkj.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jineshkj.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jineshkj.wordpress.com/11/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=11&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jineshkj.wordpress.com/2007/01/06/the-essence-of-do-while0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ece9a0480646bde6908cfafca49d8feb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jineshkj</media:title>
		</media:content>
	</item>
		<item>
		<title>&#8216;Expect&#8217; as an administration tool</title>
		<link>http://jineshkj.wordpress.com/2007/01/03/expect-as-an-administration-tool/</link>
		<comments>http://jineshkj.wordpress.com/2007/01/03/expect-as-an-administration-tool/#comments</comments>
		<pubDate>Wed, 03 Jan 2007 07:20:20 +0000</pubDate>
		<dc:creator>Jinesh K J</dc:creator>
				<category><![CDATA[System Administration]]></category>

		<guid isPermaLink="false">http://jineshkj.wordpress.com/2007/01/03/expect-as-an-administration-tool/</guid>
		<description><![CDATA[This is an article that I had posted in my livejournal on 6th-May-2006 06:58 pm. I’m trying to migrate the most valuable stuff to wordpress. Last time we saw how to execute those special system tools(ssh, ftp, su, &#8230;) by providing the password through PTYs. But, the problem is that its not easy or ideal [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=10&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><em><em>This is an article that I had posted in my livejournal on <span class="entryHeaderDate"></span></em><span class="entryHeaderDate">6th-May-2006 06:58 pm</span><em>. I’m trying to migrate the most valuable stuff to wordpress.</em></em></p>
<p>Last time we saw how to execute those special system tools(ssh, ftp, su, &#8230;) by providing the password through PTYs. But, the problem is that its not easy or ideal at all to write a C program to do such system maintenance or automation tasks. Also, if we need to process the output of the running child program and respond it with dynamic inputs, the task becomes very complicated. Then, the obvious guess would be&#8230; right, a script. Today, we&#8217;re going to see how &#8216;Expect&#8217; can be used to do our task with ease.</p>
<p>Expect can be thought of as a tool which can run other programs in a PTY with the knowledge of what to expect from the child program. ie, Expect can be told how to behave towards the child program for its various outputs. And its being done through a script similar to Tcl. Unfortunately, I don&#8217;t know Tcl at all, but certainly know how to use expect for my needs. So here&#8217;s the code that does what we required when we wrote the last C program &#8211; ptymagic.c:</p>
<p><font face="courier">#!/usr/bin/expect</font></p>
<p><font face="courier">spawn [lrange $argv 0 0] [lrange $argv 1 $argc]</font></p>
<p><font face="courier">expect {<br />
</font></p>
<blockquote><p><font face="courier"> &#8220;Password:&#8221; {</font></p>
<blockquote><p><font face="courier"> expect_user -re &#8220;(.*)\n&#8221;</font><br />
<font face="courier"> send_user &#8220;\n&#8221;</font><br />
<font face="courier"> send &#8220;$expect_out(1,string)\r&#8221;</font><br />
<font face="courier"> exp_continue</font></p></blockquote>
<p><font face="courier"> }</font></p></blockquote>
<p><font face="courier"> }</font></p>
<p>Suppose you name the file as ptymagic, and give execute permission for the same, test it as we did previously:</p>
<p><strong> echo &#8220;Your_Root_Password&#8221; | ./ptymagic su -c &#8220;cat /etc/shadow&#8221; </strong></p>
<p>May be it&#8217;ll seem slow, but hey, we are here to write maintenance script for the system and not any scientific supercomputing simulations&#8230;</p>
<p>best of luck&#8230;</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jineshkj.wordpress.com/10/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jineshkj.wordpress.com/10/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jineshkj.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jineshkj.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jineshkj.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jineshkj.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jineshkj.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jineshkj.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jineshkj.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jineshkj.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jineshkj.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jineshkj.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jineshkj.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jineshkj.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jineshkj.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jineshkj.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=10&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jineshkj.wordpress.com/2007/01/03/expect-as-an-administration-tool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ece9a0480646bde6908cfafca49d8feb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jineshkj</media:title>
		</media:content>
	</item>
		<item>
		<title>Using PTYs to fool getpass()</title>
		<link>http://jineshkj.wordpress.com/2006/12/26/using-ptys-to-fool-getpass/</link>
		<comments>http://jineshkj.wordpress.com/2006/12/26/using-ptys-to-fool-getpass/#comments</comments>
		<pubDate>Tue, 26 Dec 2006 07:20:01 +0000</pubDate>
		<dc:creator>Jinesh K J</dc:creator>
				<category><![CDATA[System Administration]]></category>

		<guid isPermaLink="false">http://jineshkj.wordpress.com/2006/12/26/using-ptys-to-fool-getpass/</guid>
		<description><![CDATA[This is an article that I had posted in my livejournal on 4th-May-2006 03:08 pm. I’m trying to migrate the most valuable stuff to wordpress. Today we&#8217;ll discuss the getpass() library function and the hurdles it presents for a system administrator for system automation. The usual bash pipe (&#8216;&#124;&#8217;) wont help us in providing password [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=9&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><em>This is an article that I had posted in my livejournal on <span class="entryHeaderDate">4th-May-2006 03:08 pm</span>. I’m trying to migrate the most valuable stuff to wordpress.</em></p>
<p>Today we&#8217;ll discuss the getpass() library function and the hurdles it presents for a system administrator for system automation. The usual bash pipe (&#8216;|&#8217;) wont help us in providing password for tools like ssh, ftp, su, etc. The main reason is the way in which getpass works to get the password from the terminal. Unlike other library routines like getchar() or scanf() which read from stdin, this one opens the /dev/tty to read the user input.</p>
<p>The only solution been provided to overcome this is to create pseudo terminals(pty) and run your application in that. One of the most useful of the various functions used for this is forkpty(). Yes, here I&#8217;m talking about writing your own program to create a new pty, fork and run its child in that pty, and exec the application(like ssh, ftp,&#8230;) in the child. You will get a file descriptor which can be used to read or write data to the pty(ie, the application i/o). Below is given a program that tries to make a connection between you and your application.</p>
<p><font face="courier">// ptymagic.c<br />
#include &lt;pty.h&gt;<br />
#include &lt;utmp.h&gt;</font></p>
<p><font face="courier">#include &lt;stdio.h&gt;<br />
#include &lt;stdlib.h&gt;</font></p>
<p><font face="courier">#include &lt;unistd.h&gt;<br />
#include &lt;pthread.h&gt;</font></p>
<p><font face="courier">int readtunnel(int pty)  // thread for reading from appl<br />
{<br />
</font></p>
<blockquote><p><font face="courier"> char ch;</font><br />
<font face="courier"> while(read(pty,&amp;ch,1) != -1)</font><br />
<font face="courier"> write(1,&amp;ch,1);</font></p></blockquote>
<p><font face="courier">}</font></p>
<p><font face="courier">int writetunnel(int pty)   // thread for writing to appl<br />
{<br />
</font></p>
<blockquote><p><font face="courier"> char ch;</font><br />
<font face="courier"> while(read(0,&amp;ch,1))</font><br />
<font face="courier"> write(pty,&amp;ch,1);</font></p></blockquote>
<p><font face="courier"> }</font></p>
<p><font face="courier">main(int argc, char **argv)<br />
{<br />
</font></p>
<blockquote><p><font face="courier"> int pty,child;</font><br />
<font face="courier"> int ret;</font><br />
<font face="courier"> pthread_t pread,pwrite;</font></p>
<p><font face="courier">if(argc &lt; 2)<br />
{<br />
</font></p>
<blockquote><p><font face="courier"> exit(-1);</font></p></blockquote>
<p><font face="courier"> }</font></p>
<p><font face="courier">child = forkpty(&amp;pty,0,0,0);</font></p>
<p><font face="courier">if(!child)<br />
{<br />
</font></p>
<blockquote><p><font face="courier"> struct termios  tios;</font><font face="courier">tcgetattr(0, &amp;tios);<br />
tios.c_lflag &amp;= ~(ECHO | ECHOE | ECHOK | ECHONL);<br />
tios.c_oflag &amp;= ~(ONLCR);<br />
tcsetattr(0, TCSANOW, &amp;tios);</font></p>
<p><font face="courier">execv(argv[1],&amp;argv[1]);</font></p>
<p><font face="courier">exit(-1);</font></p></blockquote>
<p><font face="courier">}</font></p>
<p><font face="courier">if(child == -1)<br />
</font></p>
<blockquote><p><font face="courier"> exit(-1);</font></p></blockquote>
<p><font face="courier">read(pty,&amp;ret,1); // wait till the child has outputted atleast one character<br />
write(1,&amp;ret,1);</font></p>
<p><font face="courier">pthread_create(&amp;pread,0,readtunnel,pty);<br />
pthread_create(&amp;pwrite,0, writetunnel,pty);</font></p>
<p><font face="courier">pthread_join(pread,0);</font></p>
<p><font face="courier">wait(&amp;ret);</font></p>
<p><font face="courier">if(ret &gt;= 0 &amp;&amp; ret &lt;= 255)<br />
</font></p>
<blockquote><p><font face="courier"> exit(ret);</font></p></blockquote>
<p><font face="courier">exit(-1);</font></p></blockquote>
<p><font face="courier"> }</font></p>
<p>Compile the program as:</p>
<p><strong> gcc -l pthread -l util -o ptymagic ptymagic.c </strong></p>
<p>And try to run as:</p>
<p><strong> echo &#8220;Your_Root_Password&#8221; | ./ptymagic su -c &#8220;cat /etc/shadow&#8221; </strong></p>
<p>and see the magic.</p>
<p>Here, the defect is that we have to write a c program for every different kind of application which is going to be a tedious task. A better alternative is also there about which I&#8217;ll explain later.</p>
<p>Till then,</p>
<p>BYE.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jineshkj.wordpress.com/9/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jineshkj.wordpress.com/9/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jineshkj.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jineshkj.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jineshkj.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jineshkj.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jineshkj.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jineshkj.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jineshkj.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jineshkj.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jineshkj.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jineshkj.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jineshkj.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jineshkj.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jineshkj.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jineshkj.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jineshkj.wordpress.com&amp;blog=435641&amp;post=9&amp;subd=jineshkj&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jineshkj.wordpress.com/2006/12/26/using-ptys-to-fool-getpass/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ece9a0480646bde6908cfafca49d8feb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jineshkj</media:title>
		</media:content>
	</item>
	</channel>
</rss>
