Wednesday, April 09, 2008

Some Linux Tips

This is mainly from my experience in Red hat Linux 9.0 and Fedora Core (from as far back as 5 years). I have used the double quotes just to highlight the phrase to be typed on the command line. Remove the double quotes and type the commands.

How to check free space in my hard disk?

Typing "free" in your terminal command line lists out all the hard drives details.



"How do I find out the location of an executable file?

which <executable>" will show you the location from which it is called.



How can shutdown my machine with a command/shell script?

Create a shell script and put the following line in it (without the double quotes)

"shutdown -h now"

Executing this shell script shuts down your machine.



How do I mount a network shared folder in my LAN ?

For example sake, assume the network machine name is "nwsys" and the shared folder in that is "shared". Now create a directory to which you want to associate the shared folder to. Lets assume you create a folder "myshare" under "root" directory. Now the command will be

"smbmount //nwsys/shared /root/myshare -o username=<username>,workgroup=<workgroup>"

If all directories exist, you would be asked for the password, enter it. If no error message pops up, you are through.

From FC5, smbfs is removed and the command needs to modified to this:

mount -t cifs //nwsys/shared /root/myshare -o 
username=<myname>,workgroup=<myworkgroup>



I  had disconnected my LAN cable and reconnected it. Now, I am not able to connect to network machines through nautilius.

Close all nautilius windows and execute the following command at the command prompt

"killall nautilius"

This restarts the nautilius service and you should be able to connect to network systems.



 I created a shell script in windows and moved into linux. Here, i set all the requisite user rights for execution. When i tried to execute the script, it gives the error

bad interpreter: No such file or directory
The script seems to be syntatically fine.

This is a problem becaus of the difference in OSes. Open the script in vim and type the following command


:set ff=unix

This converts the script to unix based file format.

Tuesday, January 01, 2008

Difference between CR and LF

Sourced from here

The main difference is that and are characters, while EOF is more of a concept - though the end of a file can be denoted by a special marker character, too.

Generally speaking, under Windows and DOS, lines of text in a file end with the two byte character sequence ; these characters have the ASCII values 13 and 10, respectively. Under *nix, a line of text ends with just ASCII character # 10.

Under DOS and Windows, a file might also contain an end of file marker character; the ASCII value of this character is 0x1A (26 decimal). This character was used more to speed things up when working with certain kinds of files (text files, as opposed to binary files) than anything.

Exactly how you open a file, read from it, or write to it, will vary with the programming language or tool that you're using; there are a lot of commonalities between the majority of them, though. Hopefully, a few examples in C will help clear this up. A few important points first, though: a carriage return (CR) - ASCII character code 13 - is also represented by the escape sequence "\r" (not including the quote marks), and a line feed (LF) - ASCII character code 10 - is also represented by the escape sequence "\n" (again, not including the quote marks).

Under DOS or Windows - assuming that we're using the C run-time library (RTL) and not the Windows application programming interface (API), or DOS function calls - you have to tell the RTL how you would like it to treat the file. Your option are "as a text file" and "as a binary file". If you tell the RTL to treat a file as a text file, the following things will happen:

When the sequence is encountered in a file, it's replaced, "behind the scenes", by just , which is ASCII character 10. Which matches the constant '\n'.

If the end-of-file character is encountered, the file's end-of-file flag is set: fgetc() returns the value EOF (defined in stdio.h); feof() will return true (non-zero), and so on.

However, if you tell the RTL to treat the file as a binary file, the data is returned to you "raw" - no end-of-line marker translation is done, you get both the and the ; and, the end-of-file marker - if present - is treated just like any other byte in the file; it's value is returned to you.

With most compilers, under Windows or DOS, in C, a file is opened as a binary file by using fopen() and specifying the letter "b" after the mode you wish to open the file in (r, w, a, etc.). For example, to open a file named "test.dat" in read-only mode, in binary mode (without any translation of the file's contents), one could write:

FILE *f;

if (!(f = fopen(f,"rb")))
{
/* an error occurred */
}

A note regarding porting code between DOS/Windows and *nix systems: if you were to attempt to compile the above code on a *nix system, the compiler might or might not give you an indication that there was a problem: generally speaking, *nix systems always open files in "binary" mode, and "rb" (or "wb", "ab", etc.) is not a valid parameter. Exactly what would happen can be expected to vary from compiler to compiler; on some, the presence of the "b" might cause the fopen() call to fail; others might print a warning message.



One last note regarding end-of-file and translation of characters in "text" mode on DOS and Windows systems: while, in theory, the rules are simple, in practice, they're a lot more complicated. If you were to take a program that used text files in "text" mode, and compile it, on a DOS / Windows system, with different compilers from different vendors, you can expect that, to some extent, you'll get different results. One of the ways they differ in is in how the final pair in a file is treated. In "text" mode, sometimes, you'll get a final "\n", and other times you won't. In "binary" mode, you'll always get the entire contents of the file.

Wednesday, October 24, 2007

Too much security

On a security enhanced linux system (selinux enabled OS), when you try to execute an application which links with user created libraries, you will (in all probabilities) end up with an error like below
./myapp : error while loading shared libaries :
/usr/local/lib/libmylib.so :cannot restore segment prot
after reloc permission denied.
. There are two ways to escape this pain.

1. Kill the source of pain once and for all. Disable selinux. Open /etc/selinux/config and either comment out all lines or modify the following line as below
SELINUX=disabled. Save the file and voila, you are a killer.

2. Request selinux.

Dear Selinux,

Since I have to work with user created libraries, I request you to kindly allow me to access the same and run corresponding applications.

Thanking you,
A Poor suffering soul.


Well, not exactly like this, but something like below (for libraries in /usr/local/lib path)
find /usr/local/lib -name '*.so*' -exec chcon -t 
texrel_shlib_t {} \;

Monday, October 22, 2007

Wrong Architecture issue in Mac

I maintain a common source code base for both PPC and x86 architecture in Mac and just recompile (with different Makefiles) in both architectures. Once, I had forgotten to clean up a library archive which I had compiled in x86. So, when I tried compiling the library in PPC, I ended up with the following error.

ld: common symbols not allowed with MH_DYLIB output format with 
the -multi_module option
mymainfile.o definition of common _gsDeviceFreeMutex (size 44)
/usr/bin/libtool: internal link edit command failed


Just cleaning up the x86-compiled library archive removed the above error. And this drank few hours off my work-day. But, I ended up wiser ;-)

Tuesday, May 08, 2007

static linking in Mac - But when?

This is a linker part of my Makefile for compiling an app in Linux. It statically links a library which again I did.
Shared Library Generation
gcc  -shared -Wl,-soname,libmylib.so   -o libmylib.so.1.0.1 myownfile.o

App Generation
gcc -o testapp -L/usr/local/lib -L. -lmylib appfile1.o appfile2.o appfile3.o


This went fine in Linux. But, in Mac, the app generation started shouting. Yes, I have to compile it as a dylib file. And I did that too.

libtool -o libmylib.A.dylib myownfile.o -L/usr/local/lib -lotherlib

Ok. All are ready. Now, when I tried to compile it as below (similar to Linux), I was encountered with "undefined symbols", the same functions which I was linking from my dylib file. \

gcc -o testapp -L/usr/local/lib -L. -lmylib appfile1.o appfile2.o appfile3.o


aaaaaaarrggghhhhhh.

Googling led me to a startling (er... a rather novice mistake) revelation. With the above flow, the compiler will first encounter that library, see that it has no use, throw it off and then, try to link the object files. Now (and NOWWWW), it tries to solve the dependencies in the object files. Failing to solve, it shouts back at the user "LOSSEERRRRRR".... :-(

So, with the sombre face of a loser, I changed the compile script to follow the reverse pattern, ie, link the object files first and THEN, solve the dependencies, if any.



gcc -o testapp  appfile1.o appfile2.o appfile3.o -L/usr/local/lib -L. -lmylib



Voila. It works.

I did it! I did it! yeah yeah. I did it!

Monday, April 30, 2007

Calling one kernel module function in another

Say I have a LKM foo1.ko which has "my_foo1()" as the exported function. Now, another LKM foo2.ko calls this "my_foo1()". When this second LKM is compiled, it is generating a warning that "my_foo1() undefined!". Though this is just a warning, this can be avoided. How? I found it out via a forum. There are FOUR ways to remove the warning.

ONE: "insmod" foo1 before comiling foo2.

TWO: Compile both the LKMs in the same Makefile, in one shot; like below



$ cat Makefile
obj-m = foo1/foo1.o foo2/foo2.o

here:
make -C /lib/modules/`uname -r`/build M=`pwd` modules


THREE: Starting 2.6.17, you can copy the Module.symvers generated by the compilation of foo1 to the foo2 source directory and compile foo2.

FOUR: Be a proper programmer and ignore it. It works anyway ! Why Worry :-)

Common symbol error in Mac

Multiple C files. Few global variables. A single dynamic library. Thats what I was trying to do in Mac. But, the compiler went jittery and complained
ld: common symbols not allowed with MH_DYLIB output format
. Googling taught me that I need to either initialize the global variable(s) or add "-fno-common" switch to the compiler flags. That solved the issue and I am happy "trying" to link it. Oh Yeah. Am facing another issue. Will update as soon as I find the solution. Cheerio.

Thursday, April 05, 2007

loading shared libraries with gdb

While trying to validate a particular utility which uses a shared library (which again was developed by me and resides in user-specified path), gdb reported error that it was not able to find the shared library (although compiling the utility was not a problem). ldd also was not able to find the library, although i had used ldconfig -n /path/to/lib/dir and properly created the soname.

After some googling, I found that gdb was trying to use the ld.so.conf directory and its contents to search for the shared library objects. So, I added my path to /etc/ld.so.conf/ directory. After this, the shared object loaded without any complaint.

mmMmm...

Monday, March 26, 2007

Friday, March 16, 2007

Expat's little problem

While trying to parse my self-built XML file (with all possible legal standard headers), expat spit out the following error
Parse error at line XXX:
not well-formed (invalid token)


The XML file was good in all aspects, is what I had thought. Expat (ver 2.0.0) didnt think so. The bad part was that it parsed the whole file (including ending token) and THEN he gave the above error. Out of the wild, a thought struck me (yup, it happens to me too).

I opened the XML file and added a new line AFTER the last token. And Voila! it worked. Now, expat was happy. I was happy. All was well that ended (well actually, started) well. Need to report this to the developers(shud have been).

Wednesday, January 24, 2007

Mounting a storage media in Solaris

There always comes a time when even the smallest issue can make you look like a full blown idiot. (Not considering the fact that I am an idiot to some extent), this issue of mounting a pen-drive in solaris 8 made me look more like one. Then, fortunately with the help of Google, and my friend, we were able to solve the issue. Now wait a minute. Shouldnt that be easy. Well yes, AFTER we found the solution. Now, if you ask me to do the same in Linux, it will be as easy as asking Bush to talk nonsense. But, in Solaris 8, it became something like making PVNR laugh. Well, all is well and we ended doing something like this.



First run
devfsadm
then:
 mount -F pcfs /dev/dsk/cXtXdXsX:c /your/mountpoint 


This ought to do the trick forya. For us, it DID :-)

Now, with Solaris 9, my friend updated another sequence which ended up with a separate GUI window popping up just like a media drive.

After connecting the media drive, you need to restart the volume manager.
/etc/init.d/volmgt stop
/etc/init.d/volmgt start

The filesystem should show up in /rmdisk/unnamed (or under a specific name if found). Also, it pops up a separate GUI window with the contents of the drive. One thing though. Sometimes, it takes time to update the volume details and popup the window. So, keep cool and wait before coming to a conclusion. Now, to unmount the device, you need to

eject unnamed
to update the cache and unmount the device properly.

Wednesday, December 27, 2006

Recovering partition table

I got these links from ilugc forum. These two links are for utilities which help in recovering the parition table of the hard disk which may have got damaged / destroyed for any god forsaken reason. I haven't personally tried this. Shall update once (if and no, i dont want such a situation) I try.

gpart - Guess PC-type hard disk partitions
testdisk - file as well as parition table recovery utility

.

Thursday, December 07, 2006

The Primary parition limitation

I know that we cannot create more than 4 primary partitions. But, why. When technology has grown in leaps and bounds, why this "limitation"? I got the answer to this from ilugc. Any Hard disk drive contains a 448-byte Boot Loader Code (Initial Program Load Code - IPL) followed by a 64-byte partition table. These make up the first 512 bytes of the first track of any HDD. Any OS installation starts from the second track only. Each partition table entry needs 16 byte, thus limitting the total number of partitions to 4.

Monday, December 04, 2006

Synergy between Mac and PC

Can they work together. Can Mac and PC get along well. In Harmony? In Peace? I don't know. But, they sure can share a keyboard and a mouse. "Synergy" achieved this feat. Made my Wintel machine as the server and the MacTel as the client and now, am using a single keyboard and mouse for both the systems. Here's a small write-up on how I got them alive.

First, go to this link and download the corresponding OS related app. Now, since I wanted to use the windows version as the server, I opened "Share this computer's keyboard and mouse (server)'s / Screens & Links" "Configure" button.

In that, I added two screen names in the "Screens" window.
a) Click the "+" button.
b) Add "Joe-Wintel".
c) Here, there is an alias name. If you want, you can give that too. I did'nt.
d) Repeat above 3 steps for "Joe-Mactel" screen name.

Then, in "Links", section, add how the mouse and keyboard is to behave.
a) click the "+" button.
b) My two rules read something like below (Note that the italicized words are configurable)
0 to 100% of the Left of Joe-Wintel goes to 0 to 100% of Joe-Mactel
0 to 100% of the Right of Joe-Mactel goes to 0 to 100% of Joe-Wintel

Click "Start"
Your server is ready.

Now, on to your Mac system.
In the terminal window, go to the synergy app directory. You should see a "synergy.conf" File. Open it.
Modify to something like below.
Point to note. I kept my Mactel to the left of my Wintel. Now, go ahead.



section: screens
# my Windows system
Joe-Wintel
end

section: links
# My Windows is to my (mactel) right
Joe-Wintel:
right = Joe-Wintel
end

section: aliases
# Where you can map aliases. I have added for your reference only. this is not validated.
Joe-Wintel
joe
end


I then went on to create a script to run as client. The command is
sudo ./synergyc --name Joe-Mactel Joe-Wintel &


Now, if you want to run this in the foreground, then use "-f" option. Now, you can create the above as a shell script, and call it from /etc/rc.common file so that the client is up and running the moment the system starts up. ( This way, you can be even more lazy).

And there you have both Mac and Windows living in harmony. The world is returning to peace. Amen.

Wednesday, November 29, 2006

Disabling email notifications in Linux

In Fedora, to disable the "sendmail" service,
service sendmail stop

will suffice. But, sometimes, when you open your shell, or at other times, you may get "you have new mail in /root/***". How to deal with this. For bash, you can do the following steps. To confirm that ur shell is bash:

echo $SHELL
will confirm your shell. If the notifications are alive, then
echo $MAILCHECK
would dump some data. Just type
unset MAILCHECK
After this, the previous command should dump nothing. You are thus freed of your burning mail issue. Amen.

Tuesday, November 21, 2006

restarting bash

There came a situation wherein I had updated the bashrc file and wanted to apply the effects to current shell without restarting it. Then I ended on the "source" command. On Mac, I had to type
source /etc/bashrc
while in linux its
source .bashrc
. It ended up applying the configuration to the current shell. Similarly, for applying profile specific datas (more like the autoexec.bat file in mac), I had to modify /etc/profile file for setting up PATH and other enrivonmental variables.

Wednesday, November 15, 2006

rpm query

rpm -q --queryformat '%{NAME}: %{FILENAMES} install:=%{DIRNAMES} - %{SUMMARY}\n' git-core-1.4.3.5-1

git-core: /usr/bin/git install:=/usr/bin/ - Core git tools

Friday, November 03, 2006

Bye bye smbfs

From Fedora Core 5, ( I dont know about others as of now), they have stopped supporting "smbfs" and expects the users to replace it with "cifs". Hence, if you need to mount a network driver,
mount -t cifs //server_name/to_be_mounted /mapping/directory 
-o username=myname,workgroup=mydomain
Also, it (mount) doesn't like SMB names. You have to give it IP address or DNS hostname. Happy linking !!!

Thursday, November 02, 2006

File format issue

I was trying to execute a shell script which I moved from windows. I repeatedly got the following error
: bad interpreter: No such file or directory

Although I had set the proper shell to the script in, I couldnt get it to run. The high point was the same set of commands executed without any hitch when run on commandline. Then, archamedically, the thought of "file format" struck me. I set

set ff=unix
and then tried executing it. Voila, it ran. It saved the day (atleast for now).

Monday, October 30, 2006

Creating static and shared libraries in Linux

Am too bored to write a proper write-up as of now. So, here are the compiler commands to create static and shared libraries in linux (and mac too).

gcc -fPIC -g -c -Wall a.c
gcc -fPIC -g -c -Wall b.c
gcc -shared -Wl,-soname,libmystuff.so.1 -o libmystuff.so.1.0.1 a.o b.o -lc


Here, the bold and italicized stuff are those that you need to change to reflect to your library name and source files. In the above case, I am using two source files a.c, b.c and compiled it as a shared library named mystuff.so

To create a static library,
gcc -fPIC -g -c -Wall a.c
gcc -fPIC -g -c -Wall b.c
ar rc libmylib.a a.o b.o
ranlib libmylib.a

The last ranlib command indexes the symbols in the library archive to speed up compiler symbol lookup.