Inspirated

 
 

August 1, 2009

Writing a minimal shell in Python

Filed under: Blog — krkhan @ 1:48 am

Being a Unix geek has its own little benefits. For example, when a friend wants you to code a shell for his Operating Systems assignment, you can extort a lunch out of him for the services. Moreover, if you can somehow get him to convince his teacher for accepting submissions in Python, you can have s** with the code itself.

The bare-essentials shell was required to have the following:

  • Support for internal commands such as cd or ls which would be implemented using system calls.
  • Support for launching external programs with arguments provided to the shell — optionally putting them to background with an ampersand at the end of the arguments.

That is it. No fancy intput/output stream redirection, no jobs, no pipes. Just a simple launcher. A quick doze of reflective programming resulted in a class that checked the given commands against member functions (e.g., Shell.on_cd is queried for “cd path“) and called them to process the input. In case of unrecognized commands, the default handler is called which in turn uses the subprocess module and attempts to launch the command as an external program. Support for escaped or quoted arguments wasn’t required (the assignment recommended tokenizing strings at spaces), but the godsent module … :

import shlex
# ...
args = shlex.split(command)

… allowed me to support proper argument parsing with only half a line of code anyway. Similarly, connecting to the standard input/output of the child program wasn’t in the requirements, but the simple call … :

import subprocess
# ...
proc = subprocess.Popen(args,
	executable = args[0],
	stdin = sys.stdin,
	stdout = sys.stdout,
	stderr = sys.stderr)

… did the trick perfectly; allowing proper executions of commands such as “cat -“.

The complete code is downloadable here. Unfortunately, coding was the easy part. Getting the guy to pay for the extorted lunch wasn’t and as of this writing, I’m still deadlocked on that front.

Tags: , , , , , ,

July 30, 2009

Blog structure change

Filed under: Blog — krkhan @ 5:45 am

While starting this blog back in 2007, I didn’t care much about the URL being redundantly long in form of https://www.inspirated.com/wordpress/. Truth be told — being the lazy bum that I am — I didn’t want to go through the administrative troubles of updating the site structure to have something brief. That is, until today. As I was registering this blog as my OpenID at Slashdot I really got fed up with the sub-directory being part of my claimed identity. The result of this major aggravation was a regular Apache nutjob. First of all, I followed the instructions on this page. Then, I put together a quick .htaccess rewrite to ensure that old addresses (e.g. /wordpress/anything) still get permanently redirected to new ones (i.e. /anything):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*wordpress\/(.*)$ \/$1 [R=301,L]
</IfModule>

And just as everything seemed to work perfectly, the fgallery plugin insisted on ignoring the new URL. Digging up a bit, the culprit turned out to be a bug in fgallery‘s implementation which constructed gallery links with the WordPress address instead of the Blog address (the difference is critical in installations such as mine where WP’s core files resides in a subdirectory but the blog uses root directory for friendly URLs). The fix was trivial (had to replace a few get_bloginfo('wpurl') calls with get_bloginfo('url') instead), and lo and behold! Everything’s functional again. I should have done this little exercise a long while ago but well, better late than never. As a sidenote, this does not mean that I am reconsidering my slothful position on other pending tasks for the blog. Since for most of them I adhere to Georgy Shaw’s version of the quote instead, i.e., better never than late.

Tags: , ,

July 23, 2009

Microsoft’s open-source contribution traced back to GPL violation

Filed under: Blog — krkhan @ 11:36 pm

(Or: Things still make sense.)

Last week — just as Wayne Rosso stopped comparing RIAA to Stalin — M$ did its own volte-face and not only stopped calling GPL a “cancer” but decided to release some 20,000 lines of code carrying the plague itself. Pigs started flying. Some of the open-source evangelists started fantasizing about Microsoft as an ally suddenoutbreakofcommonsense and the cautious ones — who suspected some ulterior motive behind the move — were declared by them to be paranoid and rabid haters.

The storm didn’t last long though, as it was later reported that the change of heart was prompted by a GPL violation in Hyper-V. Some see this as a win for GPL. Some see this as a perfect backdrop for FUD against open-source (“it’s so goddamned viral it even infected us when we wanted interoperablity”) while some see this as a failure at M$’ part for not honestly explaining the reasons behind the code release. Whichever category you belong to, it’s nice to know that swine still have their feet firmly on ground.

Tags: , , , ,

July 20, 2009

The final nail in Pirate Bay’s coffin

Filed under: Blog — krkhan @ 10:33 am

The news of Pirate Bay’s change of ownership actually broke out last month, but I was saving a post for it in order to wait for details to emerge about the new “business model”. Now that Wayne Rosso has spelled out his plans, I can finally pay my condolences to fellow pirates. This ship has hit its iceberg.

Wayne Rosso happens to be the guy who — as the title of the linked article pointed out — once compared RIAA to Stalin during his Grokster days. Nowadays he’s busy in getting “friendly with these good guys” since they have been “wonderful” to him. Not only that, he and his new BFFs also conceived a mind-blowing plan to legitimize Pirate Bay’s usage. Mind-blowing in the sense that reading particulars of the failtarded scheme might leave you utterly bewildered as you try to scavenge an ounce of sense in it. Prepare yourself:

As for the new business model, Rosso said The Pirate Bay will offer users all the music they can download for a small monthly fee. Eventually, users can whittle that fee down to nothing by tying their computers to The Pirate Bay’s “cloud” network. For example, a person may dedicate a gig of hardware space to the network and the fee may go from $9 to $5. (Rosso declined to discuss pricing yet so the numbers are made up just for the example).

“The more of your computer resources you contribute to the network, the less you pay down to zero,” Rosso said. “The user is in control.”

To summarize:

  • “We are now a paysite. Either you pay with your money, or you loan your PC to us.”
  • TPB was such a force because its users thrived as a community based on sharing. Paysite: Noone cares.
  • BitTorrent protocol offers no way of tracking payments. Which would probably result in a proprietary client for Windows by TPB for this specific purpose.
  • Not only that, but dedicating storage resources to a cloud is not at all same as dedicating processing power in projects such as Folding@home. When you donate your processor to distributed computing, you’re only losing your CPU cycles and some electricity/heat. On the other hand, with TPB your disks’ life will degrade and your internet bandwidth will get maxed out.
  • Speaking of bandwidth, how on earth is Rosso planning to sell ISPs back their own bandwidth is beyond me. Also, most of the ISPs forbid home users to “resell” their bandwidth. It’ll be interesting to see what they’ll think of this pay-cloud.

The whole business plan, the whole corporate speak, the whole “going legit” business is mega-fail. TPB was good while it lasted, and I really liked the crew behind the original website. They knew what they were talking about and expressed their views astutely (see: Steal This Film). Still, as is the custom, named entities — regardless of how efficient they are — can never hold off MPAA and RIAA for long. Fortunately, as is customary too, piracy can never be brought down either. As long as *AA continue taking down individuals and services instead of devising better business models, piracy will thrive. Hosting a tracker is not at all something hard to do and the crowd will move on without breaking a sweat. Demonoid, PeerHub and countless others are waiting in line to take the spot. The golden quote of Ambrose Bierce that’s embedded in the blog header says it all:

“PIRACY, n. Commerce without its folly-swaddles, just as God made it.” — The Devil’s Dictionary

Tags: , , , , , ,

July 16, 2009

Control your web-pages’ typography with @font-face

Filed under: Blog — krkhan @ 11:47 pm

One of the most common mistakes a n00b web-designer makes — and one that took me a long time to figure out back when I started — is to assume that the way their shiny web-pages look in the WYSIWYG editors is actually the way how they’ll be presented to all variety of end-users. That crazy gothic font can totally get you hawt on FrontPage, but to say that it would look crippled on most of your visitors’ screens would probably be an understatement.

The CSS @font-face feature debuted a little while ago to rectify the font mess. To put it simply, it allows authors to “embed” fonts on their web-pages. And I’m a bit late on this, but it has found its way into the newest release of Firefox as well. The example rendered perfectly on version 3.5 using the new font, raising a few points in my head:

  • The rule certainly has considerable space for abuse. But then, which technology on web doesn’t?
  • For the paranoid, Firefox does provide the gfx.downloadable_fonts.enabled in about:config to turn the feature off.
  • Out of curiosity, I checked out IE’s status on @font-face support. Turns out that it also supports font-embedding, albeit using an obscure format called EOT which is basically what one would label as “DRM for fonts“. How surprising.
  • Prior to downloading the new fonts, the demo page gracefully degraded to stock ones.

In a nutshell, intriguing at worst and exciting at best. Also, it’s nice to see that IE 6’s death is prompting a steady progress in CSS and HTML. About time as well.

Tags: , , , ,

July 10, 2009

Semi-annual blog conscience report

Filed under: Blog — krkhan @ 1:17 am

Inspirated Browser Stats (January -- July 2009)

If there ever was an insanely staggering year in terms of unexpected geekological developments, it has to be 2009. Since January I have regularly been taken aback by news such as record labels dropping DRM, Duke Nukem Forever finally bowing out; Microsoft confessing that ActiveX is retarded from security’s point of view, Google Apps moving out of beta, VLC reaching 1.0, Chrome OS’ announcement, XHTML Part Deux’s quiet death, HTML 5 and CSS 3’s adoption in major browsers and well; defying all expectations, Inspirated’s browser hit stats managing to keep their head high even in the half-yearly round-up. It’s been about 136,000 hits on the blog from Firefox alone, markedly more than twice the IE hits. The first time I noticed the vulpine victory I did dedicate a post to the stats. Nevertheless, consistency achieved over six months just gives me another chance to gloat about it.

I don’t know if this is at long last the year of Linux on desktop, but one thing is for sure: only a final release of GNU Hurd now stands between our planet and the apocalypse. If that does happen, however, please make sure that you refer to the calamity by its correct technical term “GNU/Apocalpyse” and not just the ignorant layman’s phrase which totally undermines the FSF’s impact on universe’s evolution.

Tags: , , ,

July 5, 2009

The top 5 worst mistakes on command-line

Filed under: Blog — krkhan @ 10:58 pm

I could start off with an intro paragraph here but I’ll prefer keeping it sweet and simple: command-line is addictive.

For many kinds of tasks — ranging from system administration to organizing folders — I find CLI to be extraordinarily more productive than GUI clicking. For example, vim-ing through a code, if I decide I need to lookup a particular symbol in the current directory, I can quickly do a recursive grep without even taking my hands off the keyboard. Similarly, I find utilities such as mv or cp to be significantly faster than GUI file managers’ equivalent features. The learning curve is definitely steep and I also am not implying that everyone should find it equally productive but for me at least, it works like a charm.

It wouldn’t be an exaggeration to say that CLI provides a terrible amount of power at fingertips of its users. While that power is tremendous fun, it also can be a source of epic fails if not handled with caution. The fact of the matter is, as one grows accustomed to quickly doing work through text-based input, overlooking those cautions almost becomes second nature. It’s not uncommon to find a commandaholic holding his head in his hands while staring at the screen in disbelief. GUI does get credit for being a little more prone to accidental mistakes by consistently providing a visual view of what’s about to happen.

Moving on from the ill-starred mischief that I posted about last week, I thought I should compile a list of all time worst incidents of me cursing my fingers for being so familiar with the CLI. Here they are:

  1. Ctrl-C

    Sometimes, I blame Christopher Sholes for putting the Z and C keys so close on the keyboard. ‘Nuff said.

  2. Deleting the wrong partition in parted

    (parted) help rm                                                          
      rm NUMBER                                delete partition NUMBER
    
    	NUMBER is the partition number used by Linux.  On MS-DOS disk labels,
            the primary partitions number from 1 to 4, logical partitions from 5
            onwards.

    If you’re wondering why deleting a partition is placed so low on the list, the answer is TestDisk. Mere seconds after I deleted my primary partition containing all my data, I stopped all activities, booted into a rescue mode and used the God-sent utility to restructure my partition table like before with a cumulative data-loss of 0%.

  3. e2fscking e2fucking a mounted file-system

    [root@orthanc ~]# e2fsck /dev/sda2
    e2fsck 1.41.4 (27-Jan-2009)
    /dev/sda2 is mounted.  
    
    WARNING!!!  Running e2fsck on a mounted filesystem may cause
    SEVERE filesystem damage.
    
    Do you really want to continue (y/n)? 

    See that SHOUTING WARNING? I did too. But back then, I ignored it as casually as anyone ignores licensing agreements. Needless to say, the results weren’t as inconsequential as clicking “I accept” and moving on without a hint of doing something legally binding.

  4. rm -rfing the wrong directory

           -f, --force
                  ignore nonexistent files, never prompt
    
           -r, -R, --recursive
                  remove directories and their contents recursively
    

    If the last tool had the F-word in its title as the warning, this one should be read as rm --recursive-f***. The H-bomb of command-line tools, once you detonate it on a directory you didn’t mean to set it upon, even CTRL-C won’t be able keep you in one piece because of rm‘s ruthless speed and efficiency. The only ray of hope is ext3grep, but depending on numerous factors (partition structure, number of files, file types, alignment of stars etc.) your recovery prospects would range anywhere from ±100% to ±100%. You read that right.

  5. mkfsing the wrong partition

           mkfs  is  used to build a Linux file system on a device, usually a hard
           disk partition.  filesys is either the device  name  (e.g.   /dev/hda1,
           /dev/sdb2).   blocks  is  the  number of blocks to be used for the file
           system.

    The granddaddy of all command-line fuckups. If you have confused /dev/sdb for /dev/sda (an easy slip up — as I learned the hard way), it’s time to move on. Sure, you will find people selling tools for recovering data from formatted Ext3 partitions; expecting those tools to work would be a lot like expecting the King of Pop to miraculously pop up from his coffin on Tuesday and perform a ground-breaking reenactment of 83’s Motown performance.

“Blessed are the forgetful; for they get the better even of their blunder.” — Friedrich Nietzsche

Tags: , , , , , ,

July 2, 2009

Workaround for autostarting applications in a particular order on Xfce/GNOME/KDE

Filed under: Blog — krkhan @ 11:04 pm

Among many of freedesktop.org’s vital contributions to GUI computing on POSIX platforms, a divine one has been the uniform specification for automatically launching programs at initiation of a desktop session. Informally put (or theoretically at least), it means that you can add programs to startup in the appropriate settings, and they will be launched automatically at initiation of your session regardless of which desktop environment you use.

It’s been about 4 years since I migrated to Xfce for everyday work and it does come with a basic tool for managing startup entries (accessed through Application Menu > Preferences > Session and Startup). Still, if you need anything more than rudimentary addition/deletion of the entries you’re pretty much out of luck. For example, I wanted to start the applications in a particular order. I don’t know whether GNOME or KDE have the feature yet, but Xfce lacked the support for maintaining any such arrangement. The proper way to deal with the issue would have been to code the feature in the session manager myself, but since I barely get time to even read fortune-mod these days I instead had to rely on an ugly workaround. If you’re in the same sticky situation, here it is: Go to the autostart directory (usually ~/.config/autostart), open the startup script you’re concerned with, and edit the Exec line like this:

Before:
Exec=awn-autostart

After:
Exec=bash -c "sleep 3 && awn-autostart"

The ugliness arises from the fact that the new Exec line is merely launching the program with a delay of 3 seconds. Now, you can add another program with a delay of 4 and it will almost be guaranteed to start after the 3 seconds’ one across all desktop environments. I really hope that the specification itself would be revised soon to provide a uniform way of setting the startup order. But until that gets done, I can thank sleep for making my life simpler.

Tags: , , , , , ,

June 28, 2009

Damage assessment

Filed under: Blog — krkhan @ 3:40 pm

I have spent past 24 hours fiddling and messing with ext3grep in hope of recovering the lost data. To the point where I have dozens of papers lying around me with block numbers, inode values, diagrams and histograms spread all over them. To the point where I have a pretty good understanding of how Ext3 filesystem stores stuff — a fact for which on any other occasion, I would have been pretty thrilled about. To the point where every time I go to sleep, my dreams revolve around group descriptors and journal transactions with me recovering the data gleefully before the harsh reality of overwritten inodes gives me a rude awakening.

It’s about time I give up.

And as I look back over the failure for damage assessment, I recall:

  • Videos hunted/downloaded over 5 years.
  • Emails received/sent over 7 years.
  • In total 58.3 gigabytes of personal data.

I pride myself as a geek. And for those 7 years I had been emphasizing to the non-geeks again and again the importance of backups and implications of data loss. The first and penultimate data loss occurred in 2002, when I accidentally formatted my hard drive. Since then, I have been paranoid about the safety of my digital life and own a terabyte of storage space specifically for this purpose. Yet, when disaster struck, it was my own hands that orchestrated it. I could only helplessly feel the goosebumps and tingling sensations in my spine as I grasped the repercussions of my actions.

Right now, as I reminisce about the 7 years, I see lying in front of my the hard disk which perhaps — in plethora of bits containing zeros and ones — still has the data beyond my reach. The deepest onset of nostalgia I have ever had, combined with the gravest sense of loss makes me loathe with a burning passion this piece of machine I am typing on. Effectively I am back to square zero of my geekdom. Effectively, I have nothing now but vague and distressful recollections of what used to be my data kingdom.

“What’s saved affords no indication of what’s lost.” — Edward Bulwer-Lytton

Tags:

June 27, 2009

The highs and lows of Leonidas — This. Is. Fedora!

Filed under: Blog — krkhan @ 12:43 am

There is a reason why I never evangelize Fedora much: it’s far from perfect. There is also a reason why I use Fedora for my everyday Linux-ing: it’s good when it starts working. The two seemingly conflicting viewpoints are not mutually exclusive, since Fedora’s instability as well as its appeal can be termed in one word: bleeding-edge.

The biannual cycle of backup-reinstall-reconfigure for each new Fedora release works fine for me. It takes a day or two to get everything back to the way want them to be, but the trade-off is almost always worth it. Consider this, upgrading from a Fedora version to the next will almost never work seamlessly. Nevertheless, the re-installation mantra allowed me to use PulseAudio’s “perfect” setup much before most of the other distros’ users. The fact that I got a clean start for the new sound architecture also explains why I never had any incompatibility issues with it and consequently, my status as one of its huge fans.

Another reason why I’m still hanging on to Fedora is because of the familiarity factor, as I no longer have the ample time for trying out newer distributions just for the sake of it. Linux From Scratch was a mighty fun experience, but keeping it up-to-date was nothing short of a Herculean task. Gentoo sounds very appealing, but I don’t want to download a DVD, go through the hassle of installing and getting familiar with the new distro and then emerge a whole universe of updates on an internet connection as reliable as Fedora on Hurd. The perfect solution would be to install a new release of Gentoo but unfortunately, it’s been over an year now since I started waiting for one.

Which brings us to Fedora 11 — lovingly nicknamed after everyone’s favorite Spartan king.

The predominant aspects of the new release which affect me as a user are the Ext4 filesystem and the Kernel Modesetting feature. So far, I am not even using the former. Yes, yes, I know I bragged about bleeding-edge before, but let me explain why I did not go for it in this particular case:

  • Filesystem is the most critical aspect of a system for me. I can toy around and experiment with everything but this holy grail. If my audio fails, I can try fixing it. If X stops working, ditto. If my data is corrupted, I’m FUBAR.
  • Anaconda crashed when I tried to update the boot-loader configuration for a system with Ext4 partitions.
  • I couldn’t find a way to use Ext4 without using the Logical Volume Manager. I like my partitions as /dev/sdaX and entries such as /dev/mapper/yourmom/blahblahblah/finallythedrive in the fstab file turn me off.

Nevertheless, KMS made up for the Ext4 — or lack thereof.

  • Booting is now prettier.
  • Working in runlevel 3 is so much more efficient because of all the extra space provided by higher resolution.
  • Switching virtual terminals and X sessions is a breeze.
  • Suspend/Resume is a bit more stable.

Other significant changes include Xfce 4.6, Firefox 3.5 and the reworked volume management in Pulse. Overall, I’m pretty satisfied with my decision to upgrade, and here’s to the hoping that the next 6 months will go as smooth as the previous ones.

Tags: , , , , , ,
« Previous PageNext Page »