Saturday, July 18, 2009

Quick popup

Today I was downloading a file using bittorrent. I was using my laptop. I kept it on my lap and started working on a document when download was going on. After a while I noticed that the download has stopped. When I checked, I found that the network cable had come out. This repeated for a while and I found that how much ever I try, the cable will come out slowly as it was not really fitting in the socket. By the time I notice this, 10-15 minutes would have elapsed without network.

How do I know just when it gets disconnected? I was running XFCE. I knew that XFCE allowed to create widgets that can do this for me. But, reading and understanding them and again writing one will take time. Also, I cannot use any built in one as I want a real popup. (My panel auto hides. Can't waste height on a wide screen monitor).

This is what I did.

while sleep 1 ;
do
ping -c 3 www.google.com ;
[ $? == "1" ] && xterm -e "wall trouble && sleep 10";
done

Thats all! When my network went down, xterm just screamed on my face that there is trouble. Then I will push the cable a bit and press ctrl-c to dismiss the message. I love xterm... :)

/dev/port

This is something that I had known for years but never had a chance to try out. Recently I had to write a program to control parallel ports. I decided to do with iopl and in/outb. However, they did not work for me. Then I tried this. Just open the file, seek till where your parallel port is, write the data! It worked like a charm. Below is a small code snippet to do it.


#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
    int fd1=-1;
    unsigned char data=0xaa;

    fd1=open("/dev/port",O_RDWR|O_NDELAY);
    if ( fd1 < 0 ) return fd1;

    lseek(fd1,(0x378+0x00),SEEK_SET);
    write(fd1,&data,1);


    return 0;
}



You will have to change 0x378 to your parallel port base address. Also data is at offset 0, status is at offset 1 and control is at offset 2.

Saturday, July 11, 2009

MAXNS

We recently had a requirement to have about 8 entries in /etc/resolv.conf. This was because the machine was to be used in various networks where the NS entry will be different. However, the default value of MAXNS in resolv.h is only 3 (There might be distros that might have set this to another value. But most of them have it at 3). That means, if I add a 4th or 5th name server to /etc/resolv.conf, all of them (starting from 4th entry) will get ignored.

What is the workaround? There are two ways to solve this. The first solution is to change MAXNS to some other value in resolv.h and recompile the stuff. Now, what is this 'stuff'? This stuff is glibc. Replacing glibc on a working machine can be disastrous. Hence I decided to recompile glibc on a new machine. Again, what if the machine did not accept a newly compiled glibc? I might not lose any data, but the effort spent will be wasted. Hence, I decided to make an lfs.

I downloaded an lfs live CD from here . Its been ages since I had done it last. This time, however, I decided to use jhalfs. LFS live CD has a user with the same name and the tool is present there. The glibc source tar ball was replaced by a modified one where the MAXNS was set to 8. It took one whole night. But it worked. I was able to set 8 entries in resolv.conf and they were all used.

What is the second solution? It is much simpler. Just install your own DNS server software and configure it in proxy mode. I did not try this. But, I am almost sure that it should work. Alas, its just a small piece of software. You can change it in anyway to accept any number of i/p and if you want you can write one overnight. We are evaluating both the solutions. They both have some common issues.

1. Resolution will take a long time if the address need to be retrieved from name server at the bottom of the list
2. If any one of the top servers decides to respond for the query with an address not found, then even if we have a correct answer from the bottom server, we will not get it.

However, the second solution has some workarounds for these two issues. The above two problems can be solved with the help of domain specific forwarders (named supports this AFAIK).