Thursday, 12 April 2012
What is Hard link in Unix?
By using Hard link we can create an alias for an existing file. Now this file can be accessed by any of the two names, i.e. either by alias or by its original name. Suppose, I create a file "Hello" as following
[code]cat>Hello
echo Hello World
[/code]
Now assign executable permission to this file
[code]chmod u+x Hello[/code]
Now run the file using
[code]./Hello[/code]
And the output will be
Hello World
Now check the details of this file using
[code]ls -l Hello[/code]
-rwxr- -r- - 1 user1 user1 0 2012-04-12 17:58 Hello
Just examine the number 1 after file permission column. This number tells the number of links a file has. At this point its value is 1 wich means that the file can be accessed by only one name i.e. "Hello".
Now, when we create the Hard link for a file, we actually create an alias for a file. When we create a Hard link for a file then it can be accessed by more than one name, but actually it is present at only one space in Hard disk. This means with the help of Hard link more than one name can point to the actual file. We make changes to the file using any name, and those changes are reflected in the actual file. Next time when we access the file using some other name, our changes can be seen in the file
Now we will see it with the help of example
To create a Hard link for file Hello write following command on shell.
[code]ln Hello Hi[/code]
Now run the following command
[code]ls -l Hello[/code]
And the output will be
-rwxr- -r- - 2 user1 user1 0 2012-04-12 17:58 Hello
Note: Link number has changed to 2.
This was because alias "Hi" also links to the same file.
In order to confirm that both are same files, type the following command
[code]ls -li Hello Hi[/code]
The output will be
4882474 -rwxr- -r- - 2 user1 user1 0 2012-04-12 17:58 Hello
4882474 -rwxr- -r- - 2 user1 user1 0 2012-04-12 17:58 Hi
Note: inode numbers for both the outputs are same. This means there is only one file physically, or in directory system.
Now we can access this file using any of the aliases.
ls -l Hello or ls -l Hi both are same
Now when we execute remove command on Hello
[code]rm Hello [/code]
After that execute ls -l Hi. Output will be
4882474 -rwxr- -r- - 1 user1 user1 0 2012-04-12 17:58 Hi
Note: File is not deleted physically, only the alias Hello, which was earlier pointing to the file, was removed. Our data is still conserved in the file and can be accessed using alias "Hi".
Why do we use Hard links? What are the advantages of using Hard links?
i) Hard links provide protection against accidental deletions. Actually this has been shown above, even after we deleted alias "Hello", we could access the file using alias "Hi".
ii) Suppose we have some programs which expect a file in their directory. Now, we moved that file from that directory to some other directory. So, instead of changing path of the file in individual programs we can just make a hard link of that file in programs' directory.
For more information visit
What is Sticky bit?
i) SUID bit
ii) SGID bit
iii) Sticky bit
I already explained the use of SUID bit in this post.
Sticky bit can be used with files, as well as directories. Sticky bit is rarely used with files these days. The main use of Sticky bit comes when it is used with directories.
Sticky bit with files
Sticky bit is used with files in order to make it persist in the swap area or memory. In earlier days when RAM was limited in size, the scheduler used to swap programs very swiftly from RAM to Hard drive. This would cause problems when the part of the program was used very frequently in some process. So, what the programmer would do. He used to set sticky bit on files which were used very frequently, in order to retain them in the memory.Now a days we have RAM of very high capacities, so Sticky bit is rarely used on files.
Sticky bit with directories
The main use of sticky bit is with directories. For understanding this, we will have to understand file permissions on directories.Suppose there is a public directory, "pubdir" , which has permissions like following
rwxrwxrwx
Since it is a public directory, it has executable and write permissions for everyone. Now suppose, I have a file "myfile" which is in this public directory , and I have given it permission like following
rwxr- -r- -
I have not given write or execute permission to others. This means that any other user will not be able to edit or execute my file. But will heshe able to delete my file? Well that depends on the directory permissions in which my file is residing.
Now, let's understand "pubdir" directory permissions.
i) Everyone has read permission: Everyone will be able to see the contents of this directory using ls -l.
ii) Everyone has execute permission: Everyone will be able to access the directory, access on a directory means, anyone can go inside directory using cd command.
iii) Everyone has write permission: Anyone will be able to edit/delete any of the contents of directory.
Now, what are the contents of a directory?...........wwwoooooo FFFFFFFFiiiiiiiilllllllllleeeeeeeeeeessssssssss.
Damn it. All the files, owned by any user, are vulnerable to a lot of threats.
Now you understood the problem.
Don't worry, we have a solution in the form of Sticky bit. When we set sticky bit for a directory then it puts a restriction on the directory that only the file owner of the file, which is inside the directory, will be able to delete it, and not anyone else.
Sticky bit can be set in the following way.
chmod +t filename
After setting the sticky bit, the directory permission would look like
rwxrwxrwt
The most practical use of sticky bit is in /var/temp. temp directory is public and common to every user.
For more information visit
Wednesday, 11 April 2012
Why is SUID bit used?
Suppose we have a file "file1" whose permissions are shown as below.
-rwxr-xr-x
Let this file be owned by John. Now let there be another file "file2", owned by John, which has permissions as follows.
-rwxr- -r- -
"file2" can only be modified by John, who is the owner of the file. Suppose, this file stores the information about the employees working under John. Lee is one of the employee working under John, i.e. Lee's data is also stored in "file2". Now if John had given write permission to others and groups, then anyone would have been able to change anyone's data using vi editor.
John didn't want that, but also he wanted to make things simpler. So, what he did? He didn't give write permission to any of the employees for "file2", instead, he made another file "file1" on which he gave execute permission to everyone. He also set the SUID bit for "file1" because of which,when some user tries to execute "file1", the process will be executed with John's identity and permission.
Now when the process is executing with John's identity, it can very well change the contents in "file2", since John has write permission on "file2". In other words, John has cutomized the changes which an employee can make to "file2". Now a user cannot just open a file in vi editor and make any changes he/she wants. Instead, he will only be allowed to make changes in "file2" as determined by the program written in "file1".
So, that is the advantage of using SUID bit.
We set the SUID bit for "file1" as
chmod +s file1
After the SUID bit is set, the file permissions for "file2" will be as follows
-rwsr-xr-x
Did you notice the 'x' bit in user set changed to 's' ?. This confirms that SUID bit has been set for "file1".
In this way "file2" could not be modified by any employee using vi editor but can be modified by script/program in "file1" using owner's permission. Wasn't that amazing?
For more information visit
How to write and execute a named PL/SQL block?
There are many ways to write a named PL/SQL block. I will be dealing with each and every way in subsequent posts. This post deals with writing of a stored Procedure. Stored procedures and functions are stored inside Oracle, and can be called at any place in the same way as we call any function in any programming language.
The main advantage of stored procedure is that, you don't have to write your logic again and again. You can write your logic once, and use it whenever you want, on the simple call of a procedure/function.
Now we will write the code for a simple stored procedure
[code]CREATE OR REPLACE PROCEDURE sampleproc(p_arg number)
AS
v_var number(4);
BEGIN
v_var:=p_arg;
IF(v_var>1000) then
DBMS_OUTPUT.PUT_LINE('Input greater than 1000');
else
DBMS_OUTPUT.PUT_LINE('Input less than 1000');
END IF;
END sampleproc;
/
[/code]
Now we can execute this procedure in a number of ways.
a) Execute it from some other block
The stored procedure created above can be executed from any other block, it may be anonymous block, procedure or function.[code]
BEGIN
sampleproc(1001);
END;
/
[/code]
b) Execute it directly from SQLPlus.
EXECUTE sampleproc(1001);Note: SET SERVEROUTPUT ON for output.
For more information visit
Saturday, 24 March 2012
Your first simple Pl/SQL program...
The program written below assigns a string to a variable and prints it.
Before writing your PL/SQL block set your serveroutput on in Sql plus. We set serveroutput on, so that we could see output on sql plus.
SET SERVEROUTPUT ONNow we will write the block/program.
DECLARE
v_name varchar2(20);
BEGIN
v_name:='John';
DBMS_OUTPUT.PUT_LINE('My name is '||v_name);
END;
/
Above block is divided into 3 sections, i.e.
DECLARE: We have declared a variable "v_name" in this section.
BEGIN: In this section we assign a value to v_name and later print it using PUT_LINE procedure of DBMS_OUTPUT package. "||" is the concatenation operator as we have "+'' in java or "." in PHP. Also notice the line of code after BEGIN, we use ":=" for assignment in oracle instead of "=".
END: END is used to end the block and after that "/" is used to execute the block.
If everything goes right, you will see the output "John" on Sql plus.
Tuesday, 20 March 2012
What is PL/SQL programming?
Seeing the limitations of SQL, Oracle came up with the concept of PL/SQL which provides a complete programming solution to its users. Some of the features of PL/SQL are
a) It is a highly structured language, same as any other procedural language.
b) It follows the concept of "Write once, run anywhere", i.e programs written in PL/SQL can be run in any version of Oracle database on any machine. Infact, Oracle introduced the concept of "write once run anywhere" long before Java appeared.
c) It is tightly integrated with SQL. SQL query can be used seamlessly inside PL/SQL which makes it very flexible.d) You can call PL/SQL program from other languages(C, C++, Java ....).
Structure of a PL/SQL program
Header- Header is used in case of named blocks so that program could be called or used by some name. In case of anonymous block header is irrelevant.
Declaration- This section can be used to declare variables local to your program. In PL/SQL, variables can be declared inside this section only.
Execution- This section contains the programming logic. All the looping and conditional structures can be used inside this block.
Exception- This section catches the exception raised in execution section. You can use this section to handle the exceptions.
For more information visit
Wednesday, 14 September 2011
Popper live on AppStore.....
Initial version of this game consists of 6 exciting stages.
More exciting stages are coming soon.
To download click here