Search

Finding the current runlevel of linux

To find the current runlevel of the system we can use the following commands





The output indicates that the current runlevel is "2" , followed by the date and the time. If there was any other runlevel used before it would be listed after "last=".
The second command that can be used is



The second number is the current runlevel. The first number is the previous runlevel.

readlink: Finding file to which a softlink pointing to

readlink is a command that can be used on a softlink to find out the file to which it is actualy pointing to. For example if we have a file "temp" which is a softlink, we can run readlink of temp as follows



This indicates that readlink is a softlink of the file temp. If we want the full path to the file "test" we can pass the option "-f"



Thus we can find the full path to the file to which the softlink is pointing to .

Creating a soft link.

symbolic links are used in linux to create a link to a folder or a file which is located in a different location, so that the same file or folder can be accessed from both the locations.
Softlinks are created using the command



Example:

If we have a file "temp1" in the path



And if we want to create a link to this file in the path



We can do it as follows.



Now if we do a long listing of the new link that was created we can see that it is a soflink by the way it gets listed



The first letter, l, in the long listing indicates that the file is a softlink and the last column indicates the file to which it is a softlink

The new link file is just a pointer to the original file and not a new copy of it and hence it occupies just enough space required for a link, which is very very small, rather than for the whole file.
The file can be opened and modified from either of the locations, and the changes will be reflected in files at both the locations.

Removing grub rescue entries from grub menu

The grub2 by default creates an entry for recovery mode in its menu,using which we can log into single user mode and trouble shoot various problems in the system.

But if we do not want this entry we can remove this entry from the grub by changing the grub configuration.

Open the file



Note : Super user privileges are required to modify the file

Edit the line



Uncomment the line by removing the "#" in front of the line.

Now update the grub by running



Next time the system is rebooted the grub menu will not have the recovery entries.

Making the grub beep on displaying the menu

In grub2, when the menu is displayed it by default does not make any sound to alert the user about the display.
This behavior can be changed so that the grub creates a beep sound as soon as the grub menu gets displayed, so that the user, if he or she is not looking at the monitor, gets notified about the display.
Open the file



and go to the line



Uncomment the the line by removing the "#" in the beginning.

Save and quit the file.
Now run the command



Now reboot the system and we should be able to hear a beep when the grub menu gets displayed.

Pointer to structure from its member pointer: container_of

In c programming, we use structures to store variables of different data types in one kind of variable. Each variable in the structure is termed as a member of the structure. The memory allocation for these members is always sequential.
Example



In the above structure the addresses allocated for members "i" and "ch" will be sequential in the memory.

Using this property of the structure, if we know the pointer to a member of the structure we can retrieve the pointer to the structure with the help offsetof function.
In the post "offset of" we saw how we can retrieve the offset of a member with in the structure.

As the size occupied by each data type is fixed once we know the offset of the member and its pointer, we can subtract the offset from the pointer to get a pointer to the structure.

For example in the above structure if the pointer to the "char ch" is 1000. The offset of function will return 4 for "ch" as it is after an integer in the structure.
Thus the pointer to the structure would be "1000-4 = 996".

The same is shown in the example code below.

We allocate memory for one structure,test1, of type "temp" and assign values to "i" and "ch".



Then we assign the pointer of ch to a char pointer.

Then we subtract the offset of "ch" from the pointer and assign the return value to a new pointer to a structure,test2, of type temp.



If the pointer assigned to test2 is the correct , we should be able to access the value of "i" using test2.
Thus we print the value of "i" using test2



Here is the full code

struct_ptr.c



Compile and execute it.



Thus we can see that we were able to retrieve the pointer to the parent structure from the pointer to the member.

The same steps are used in the container_of function which is used in the linux kernel code.



By passing pointer to the member of the structure in ptr and the structure type as argument type and the name of the member of which ptr is the pointer to we can retrieve the pointer to the parent structure just as we did in the example above.

Example usage in kernel code :

File leds.c



By passing a pointer to the cdev structure,the structure type as "versatile_led" and the member of the structure as cdev we are able to get a pointer to the structure that contains the cdev and hence get access to other members of the structure.


unlink: Command to delete files

The command unlink can be used to delete files.

Example : To delete a file called temp



But unlink lacks any of the other options that come with the command "rm" which is also meant to delete files and unlink can only delete file and does not work on folders.