Tuesday, December 30, 2008

Google News Blackberry Storm Blunder

Just saw this headline on the Google News ...

I mean c'mon - Blackberry Storm isn't even on the AT&T network! Unless the RIM engineers found a way to bring down those iPhone users.

I for one welcome our new outage causing overlords! :)

Sunday, September 28, 2008

Using Blackberry 8830 as a tethered modem over Bluetooth on Ubuntu

I was recently toying around trying to get my Blackberry 8830 to work as a tethered modem with my eee pc running Ubuntu 8.04 LTS. This was one of the last hurdles keeping me from switching over to Ubuntu on my eee pc completely. I had to switch over to Windows XP everytime I needed to connect to the Internet on the road. After searching high and low I found what I needed on the Ubuntu documentation website. The following is just a reproduction of the same with specific details for Sprint Blackberry 8830.

These are the details of what I have:

Device: Blackberry 8830 (World Edition)
Carrier: Sprint
Laptop: eee pc 900
OS: Ubuntu 8.04 LTS (Hardy Heron)

My laptop does not have built-in Bluetooth so I picked up one of those USB dongles.

The following is a one time setup before you can use your device as a tethered modem.

Before you get started, first make sure you have bluez-utils and ppp installed on your machine.

$sudo apt-get install bluez-utils ppp

Next, make sure your phone is discoverable and run the following:

$hcitool scan

This will give you something like:

Scanning ...
xx:xx:xx:xx:xx:xx your_device_name

where xx:xx:xx:xx:xx:xx is your device's mac address.

If you haven't paired your device with your laptop yet, do so using the following command,

$sudo hcitool cc xx:xx:xx:xx:xx:xx

On my machine this opened up the Bluetooth Preferences window where I was able to enter the shared passkey and pair the two devices.

Next, check your device to see on what channel Dial-Up Networking runs.

$sdptool search DUN xx:xx:xx:xx:xx:xx

This will return something like:

Inquiring ...
Searching for DUN on xx:xx:xx:xx:xx:xx ...
Service Name: Dialup Networking
Service RecHandle: 0x10002
Service Class ID List:
"Dialup Networking" (0x1103)
"Generic Networking" (0x1201)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 3
Profile Descriptor List:
"Dialup Networking" (0x1103)
Version: 0x0100

As we can see from above the RFCOMM channel for this device is 3. This is what we use to modify our rfcomm.conf file as follows:

$sudo pico /etc/bluetooth/rfcomm.conf

and paste the following into the file.

rfcomm0 {
bind yes;
device xx:xx:xx:xx:xx:xx;
channel your-phone-rfcomm-channel;
comment "Bluetooth PPP Connection";
}

Save and close this file.

Running the following will create the rfcomm0 device:
$sudo /etc/init.d/bluetooth restart

Running rfcomm on your machine should show that it is connected to your Blackberry.

$rfcomm
rfcomm0:
xx:xx:xx:xx:xx:xx channel 3 closed

Next, we will configure PPP.

$sudo pico /etc/ppp/peers/BluetoothDialup

and paste in the following:

debug
noauth
connect "/usr/sbin/chat -v -f /etc/chatscripts/BluetoothDialup"
usepeerdns
/dev/rfcomm0 115200
defaultroute
crtscts
lcp-echo-failure 0

Save and close this file.

Next, edit the chatscript file. I found that the Verizon file in the Ubuntu documentation worked just fine for Sprint too.

$ sudo pico /etc/chatscripts/BluetoothDialup

and paste in the following

# abortstring
ABORT 'NO CARRIER' ABORT 'ERROR' ABORT 'NO DIALTONE' ABORT 'BUSY' ABORT 'NO ANSWER'
# modeminit
'' ATZ
# ispnumber
OK-AT-OKL3 ATDT#777
# ispconnect
CONNECT \d\c

Save and close the above file.

pon and poff are two commands to start and stop ppp connections. You'll use these to enable and disable Dial-Up Networking on your Blackberry everytime.

$pon BluetoothDialup

You should see "Modem Mode Enabled" now on your Blackberry. Try pinging any public website and see if it responds.

To turn off Dial-Up Networking simply type

$poff

That's it. Enjoy your tethered Blackberry modem!

btw, if you need to use your Blackberry 8830 as a modem over Windows XP, the software can be downloaded from here.

Saturday, July 26, 2008

Estimating the value of pi

Here's something I learned recently and found quite interesting. A neat way to calculate the value of pi using estimation is to take a unit circle inside a square of side 2 units and take random shots on the surface of the square.

unit circle in square








Now, the ratio of the area between the circle and square can be given by,

pi*r^2/L^2 = hits/shots




Thus, we can estimate the value of pi as,

pi=4*hits/shots




Now, I wrote a little script in Python which does just that. I also put in the value of pi to 10 decimal places and wrote out the error produced. Initially, I didn't take a circle of unit radius but that just produced too much error:



import math;
import random;
import sys;


#calculate the value of pi using probability
maxtries = float(sys.argv[1]);


radius = 1.0;
side = 2.0 * radius;
tries = 0.0;
hits = 0.0;
pi = 3.1415926536;


while (tries < maxtries):
tries = tries + 1;
#get a point within the square


rx = random.random()*side;
ry = random.random()*side;


#distance from center of circle
dist = math.sqrt(math.pow(rx - radius, 2.0) + math.pow(ry - radius, 2.0));
if (dist <= radius): hits = hits + 1;


#estimated value of pi
piest = (hits*4/tries);
print "Radius: %d, Tries: %d, Hits: %d, pi: %1.10f, Error:%1.10f\n" % (radius, tries, hits, piest, piest - pi);





My output looks something like:

~$ python pi.py 10.0
Radius: 1, Tries: 10, Hits: 6, pi: 2.4000000000, Error:-0.7415926536

~$ python pi.py 100.0
Radius: 1, Tries: 100, Hits: 83, pi: 3.3200000000, Error:0.1784073464

~$ python pi.py 1000.0
Radius: 1, Tries: 1000, Hits: 790, pi: 3.1600000000, Error:0.0184073464

~$ python pi.py 10000.0
Radius: 1, Tries: 10000, Hits: 7822, pi: 3.1288000000, Error:-0.0127926536

~$ python pi.py 100000.0
Radius: 1, Tries: 100000, Hits: 78591, pi: 3.1436400000, Error:0.0020473464

~$ python pi.py 1000000.0
Radius: 1, Tries: 1000000, Hits: 786368, pi: 3.1454720000, Error:0.0038793464

~$ python pi.py 10000000.0
Radius: 1, Tries: 10000000, Hits: 7853556, pi: 3.1414224000, Error:-0.0001702536

~$ python pi.py 100000000.0
Radius: 1, Tries: 100000000, Hits: 78539696, pi: 3.1415878400, Error:-0.0000048136

~$ python pi.py 1000000000.0
Radius: 1, Tries: 1000000000, Hits: 785382068, pi: 3.1415282720, Error:-0.0000643816

The last observation is the most interesting. It ran for a few hours on my machine but did absolutely nothing to improve the error. In fact, the error actually increased!

Wednesday, July 23, 2008

glibc on Ubuntu

About 6 months back I worked on an interesting project for school where we were developing a checkpointing application. My goal was to port the software onto Ubuntu where it some reason would not work. I initially put a query on ubuntuforums.org to which I never replied and people have queried me on and off about whether I did get an answer to this issue. So before the project information gets deleted from my school wiki, I'm going to put the full problem statement and solution here.

Checkpointing is a technique for storing the state of a process in order to recover it at a later point. Checkpointing is used where there are long running processes, saving the state of scientific applications to execute different conditions from that state or cases where debugging of processes is required. The different approaches to checkpointing include:

a. Kernel Level Checkpointing
b. User Level Checkpointing
c. Application Level Checkpointing

In Kernel Level Checkpointing, the operating system is modified to support checkpointing for
applications. This is the most transparent but the least efficient of the approaches. Here, the operating system does not know anything about the application and simply dumps the memory into a file for later restart. An issue with this approach is that the changes required in the kernel to support checkpointing will be different for each OS. User level checkpointing allows the user to link the application to the checkpointing framework in order to make the application checkpointable. With application level checkpointing, the developer of the application implements checkpointing within the application itself as the person has detailed knowledge about the application. At the same time this approach has the least amount of transparency and it is very difficult to migrate the checkpointing approach from one application to another.

In DMTCP (Distributed MultiThreaded CheckPointing) we study a user-level checkpointing technique used to restart or migrate a process. DMTCP works at the socket level so it can checkpoint applications without requiring modification to either the operating system or the application. DMTCP operates using a checkpoint control process which sends messages to a checkpoint manager thread about each process. The manager then uses signals to gain control of other threads before checkpointing. Our term project will be to study DMTCP and to test it for different application and to extend DMTCP to applications using LAM/MPI.

The first problem that we tackled was to port mtcp to Ubuntu. We noticed a problem when we first tried to run dmtcp on Ubuntu 7.10. When trying to run just mtcp mtcp_restore used to give a segmentation fault.

After debugging through the code, we traced the problem to the mtcp_sys_munmap() call in mtcp_restatic.c. Looking at the memory maps gave us something like this:

08048000-080be000 r-xp 00000000 08:07 594167 /home/vsriram/dmtcp/mtcp/mtcp_restore
080be000-080c0000 rw-p 00075000 08:07 594167 /home/vsriram/dmtcp/mtcp/mtcp_restore
080c0000-080e3000 rw-p 080c0000 00:00 0 [heap]
4001e000-4002c000 rwxp 4001e000 00:00 0
bfa6d000-bfa83000 rw-p bfa6d000 00:00 0 [stack]
ffffe000-fffff000 r-xp 00000000 00:00 0 [vdso]

The problem occurred when unmapping the heap area of the memory. We figured that the error would be either with the Ubuntu Linux Kernel or with glibc. This is because mtcp works fine in other Linux distros like Suse or Red Hat and even Linux versions prior to 6.10. The only other calls were to the Linux Kernel and glibc libraries. The next steps were to put printk statements in the kernel and see where it gives an error or to compile a vanilla glibc and see if the error was still reproducing using the glibc libraries.

We first approached the problem by trying to re-compile glibc. glibc would not compile easily on Ubuntu and it gave an undefined reference errorwhich we could not resolve even by setting the CFLAGS environment variable to -fno-stack-protector. So, this approach was abandoned in favor of recompiling the kernel and putting printk statements in the kernel code. After doing this and recompiling the kernel, we noticed that the sys_munmap() system call returns without any issues. This meant that the problem lay outside the system call.

As no system calls (even printf) can be called after the return from the sys_munmap() sytem call we decided to put while(1); statements to see till what points the application would continue (or in this case hang) without breaking. While doing this we could go upto the highest_userspace_address() call in code. The first thought was that the function does not exist in the address any more and this was leading to a segmentation fault.

We then recompiled mtcp_restatic.c to an assembly file (by using gcc -S) and put the assembly code for the while(1); loop in the mtcp_restatic.s file and recompiled the assembly code to an object file. This was to see if the program counter could enter the highest_userspace_address() method.

Assembly code for while(1); loop is shown below:

.L170
jmp .L170

At this point we found that the error occurs during the reference to %gs:20 which is a segment register referencing a memory location which is in the heap. Googling this lead to an interesting webpage where they talked about stack protection in Ubuntu which might be cause of this issue.

We read about Stack smashing protection where canary values are placed between a buffer and control data on the stack to monitor buffer overflows. When the buffer overflows, the first data to be corrupted will be the canary, and a failed verification of the canary data is therefore an alert of an overflow, which can then be handled. The canary values are inserted in by gcc while compiling the code.

The assembly code below sets the canary value at the beginning of the function.

movl %gs:20, %eax
movl %eax, -8(%ebp)

The assembly code below checks if the value of the canary remained the same. This code is placed at the end of the function.

movl -8(%ebp), %edx
xorl %gs:20, %edx
je .L179
call __stack_chk_fail

After the canary checking assembly code was removed from the assembly code and compiled to object code, the highest_userspace_address function worked fine but it failed further on. This validated the hypothesis that the segmentation fault was because of the reference to the memory location in the heap which by then was already unmapped.

Adding -fno-stack-protector to gcc (in the Makefile) before compiling the entire mtcp code removes the default stack protection and mtcp works fine after that.

Saturday, March 29, 2008

Close encounters of the arranged kind

Well, at last time has come for me to take the path that billions have before me. Much as I fight it, I have been convinced by the elders that my time has come. And so the hunt begins for the perfect bride. But fundamental flaws exist in this search and the basic one is with the filters that my parents use to hunt for the right girl. In their mind the perfect bahu should be devotional, god fearing, family oriented - basically, the whole nine yards (of sari).

Me, I'm a much simpler person. All I ask for is someone who is independent, ambitious, intelligent, talented, reliable, with good taste, reasonable, full of imagination, adapting, sensitive, physically fit, loyal, modest, charming, popular, hard working, honest, empathetic, spontaneous, affectionate, musically inclined and having a good sense of humor.*

This leads to my parents filters being misaligned with mine. As I shall explain using the following diagram,

And thus the girls that my parents select do not fall into my criteria.

Anyhoo, my parents became serious and tried to hook me up with this girl from Chennai. Four years younger to me, she was just out of college and had been working with a software company for the past few months. My mom made some mumbling excuses on why she had to travel to someplace south of Chennai to see a few temples that were high on her list and used that trip to just "casually" meet with the girl and her parents. Deciding that the girl was good match for me they soon sent me her email address. With that, I sent her a sobering mail writing a bit about myself, what I do, my hobbies etc. The next day, I got a reply back from her - "About me, what can I say? :))))))))". As I say to anyone who asks - smiley, smiley, smiley, smiley, smiley, smiley, smiley, smiley.

Not that I have anything against someone full of innocence and a sense of hope and a outlook towards life. Five years after graduating from college working full time, I've been marinated to near perfection with a generous coating of sarcasm and skepticism. I felt like Groucho Marx with a can of herbicide just waiting to spray it on a blooming flower on a spring morning.

Even then I decided to take the next big step and talk to her.
From the getgo, things were not well. Her polished Tamil was far different from the Mumbaiya one I'm used to at home. On the other hand, she didn't speak a word of Hindi. Five minutes into the conversation, I realized how much the culture from two cities can separate people - even those with similar backgrounds. At some point when I was describing to her my hiking activities, she asked me if I had a big "gang" over there. For a few moments there, I had a J.D.-like daydream, with me and my friends dressed up in biker leather outfits with chains and baseball bats. I wound up talking to her mother a little bit later and after what I thought was end of the conversation kept the phone definitely deciding against the marriage alliance. I called up my mother and explained her the reasons why it wouldn't work and though she was disappointed there wasn't much I could do about it.

Five minutes after I finished that conversation, I checked my messages and lo and behold there was an email from her - "hey why u kept the phone?" My stomach churned for a minute. Do I reply to her? Would that mean I haven't said a no? And what do I say to her? "Oh, I'm sorry, I thought our conversation was quite finished." "Sorry, you're chucked." "Next ...". As I said, Groucho Marx and and a can of industrial strength herbicide ...

Someone, someday should publish a book on arranged marriage etiquette. Maybe I will for all the Gen-X ones who will get trapped into arranged marriages - once I go through with it. And no, that was NOT an argument against arranged marriages. I have heard both the pros and cons of arranged marriages and the jury is still out on it.

For True Love,

Ramudu

*OK, I totally picked that up from a horoscope website. For someone with the above talents, she would have birthdays in all the 12 signs. I also really wanted to put in "has precise sense of judgment and expects complete fairness" but have no idea what a person with that characteristic would be like. And on a extreme tangent, did you know that empathic also means "
Having the capability to share the emotions of another through psychic means." That's a great quality to have. Girls, call me ...

Friday, February 15, 2008

A tale of two nail clippers

So, I was in need of a new nail cutter. My old one just disintegrated into pieces when I was using it a couple of weeks after I came back from India. And of all things I did get from India, the one thing I did forgot to buy was a 20 rupee nail cutter.

What's the big deal, you ask? Just head out to the nearest store and buy one. The problem was, I just didn't know where I could get one - if I could get a nail cutter here at all. In the land of the free (and the home of the brave) where electric toothbrushes and power razors were the norm, I didn't know whether anyone actually used a good ol' fashioned nail cutter. In my mind, everyone here owned a box type electric device - something like the electric pencil sharpener - where you could put your finger in and it would neatly trim your nails. Probably in the higher end models the device would wax and clean the nail plates, apply nail polish if you were a woman and say a little thank you at the end. Yes, I do let my imagination run wild once in a while.

Much to my misery I realized that I hadn't seen one Hollywood movie with a scene in which one of the characters was shown cutting their nails. I jogged through my memory to remember movies with scenes taking place in beauty salons. After running through the likes of Legally Blonde where I hoped for some hint towards the trimmed and neat fingernails of Americans, I wasn't one bit closer to getting a solution. I'm guessing its some sort of Hollywood conspiracy against the nail cutter manufactures - probably didn't give a good deal for product placement.

By this time I was sneaking into friends bathrooms during dinner invites to their place looking for nail cutters and using them without their consent. I'd spend so much time in their bathrooms that I'd shady looks when I came out. The nail biting finish of Super Bowl XLII helped for a few days. But I still hadn't found a permanent solution.

I wasn't daring enough to walk into a CVS Pharmacy and ask for a nail cutter. God knows whether the sales rep would understand what I was asking for. This is a country where a "cool drink" turns into soda, a "giant wheel" becomes a "Ferris Wheel", college becomes a school and you don't pass out of it - you graduate from it. So I shudder to think what a nail cutter would be called? Damn these cultural differences! Also, every time I head into a CVS asking for something I get wierded out looks from the rep that I've stopped going there altogether.

Finally, one weekend I mustered the courage to ask my cousin if there were nail cutters available in the US where I could get one and what it was called here. At first he just sat there staring at me for a few seconds. When he finally did reply that "nail clippers", as they were called here, were available at any pharmacy he had a look of incredulousness on his face - I guess it was the most dumbest question anyone had asked him.

The next day I sneaked into a CVS and quietly headed towards the beauty section before any of their overly helpful staff could come near me. And there it was! A nail clipper just like the ones from home. In a true showing of American capitalism the nail clippers, which were probably sourced for less than a dollar, were priced
obscenely at $3.99 - on sale! But what the hell ... I bought two just in case someone actually does invent the electric nail clipper and they stop carrying the good ol' ones!

Case closed.

P.S. I should have just Googled nail cutters or nail clippers and would have probably saved myself a lot of trouble - but I guess it wouldn't be this funny. I voluntarily renounce my title of Google King that was given to me by my friends after I "proved by search" that 1 + 1 = 2.