Representation of File System in the Kernel

Representation of data on a floppy and hard disk may consistently differ, but representing them in the kernel is almost same.

VFS mounts the file system through the function

  • register_filesystem(struct file_system_type *fs),
  • Example

register_filesystem(&ext2_fs_type);

Mounting

  • Before a file can be accessed, the file system containing the file should be mounted.
  • Done by the call using mount or function mount_root()
  • Every mounted file system is represented by a super_block structure.
  • The function read_super()of the VFS is used to initialize the superblock
    • Managing the file system
    • Will issue some flags during the mounting like

MS_RDONLY //File system is read only

MS_REMOUNT //flags have been changed

 

Superblock Operations

Super block provides functions needed to access the file system and its processing. Some of the operation includes

read_inode();// must execute and Responsible for filling the submitted inode structure

write_inode(); //must execute and Used to store information about the inode structure

put_inode();// optional and Release all the blocks occupied by the inodes and release other resources

delete_inode(); //optional and Delete the inode and no longer be referenced to the filesystem

 

Inode Operation

  • Inode is useful for file management
  • Each inode contains a unique number which addressable to a file
  • Inode operations

create();//fills the mode attributes of the inode dentry

lookup();

link(); //used to create a hard link

unlink(); //deletes the file indicated by dentry.

mkdir();

rmdir();//deletes the subdirectory entry

rename(); moves a file or changes its name

permission();// checking for access rights

File and its Operation

File structure is helpful is providing

Access rights like reading and writing

Current position

Access flags and number of accesses

File Operations

read(); //read the data and put in the user address space

write();//copy data from user address space to the file

mmap(); // the file is mapped to user address space

open(); //create a file structure

flush(file); //called when the file is closed

lock(); // lock is called if file locks are set

Linux File System

Linux supports many number of file systems, that’s why Linux is accepted quickly as one of the best Operating systems.

Linux has the unified interface called as Virtual File system (VFS) which serves as the interface defined in between the OS kernel and the different file systems as illustrated below.

clip_image002

The Virtual file System

  • Supplies the applications with the system calls for file management
  • Maintains the internal structures
  • Passes tasks on to the appropriate actual file system
  • Performance of default actions

Basic Principles

  • Two main factors taken into consideration while designing any file system
    • Speed of access of data and
    • Facility for Random access
      • is made possible by means of the block oriented devices which are divided into specific number of equal sized blocks.
      • When using the blocks, linux uses a buffer cache to speed up the process in random access.
  • In Linux/Unix, the data is stored in a hierarchical file system containing not only files and directories, but also the device files, FIFO (Named pipes), symbolic links and sockets.
  • Every file is represented by a file structure and an inode structure. It is always possible to access a particular file through the inode file with the help of the inode unique number.
  • Directories
    • allow the file system to be given a hierarchical structure. These are also implemented as files, but the kernel assumes them to contain pairs consisting of a filename and its inode number.
    • In older version of unix, it was possible to modify directory files using simple text editor, but for consistency this is no longer available in the new version of Unix.

Structure of UNIX File system

The UNIX file system passes through various processes like

clip_image004

· Boot block

  1. Each file system starts with a boot block. The block is reserved for the code required to boot the operating system.

· Super block

o All information which is essential for managing the file system is held in the superblock

· Inode block

o Contains the inode structure for the files

· Data blocks

o The data blocks contains ordinary files along with the directory entries and the indirect blocks.

In Unix, the separate file systems are not accessed via device identifiers as is the case for other operating systems, but are combined in a hierarchical tree.

The arrangement is made of mounting the file system, which adds another file system to the existing directory tree. A new file system can be mounted to any directory. The original directory is then called as the mount point. Unmounting the file system release the directory structure again.

Sockets for Inter process communication

Socket programming interfaces provides communication via a network as well as locally on a single computer. Example is INET daemon which waits for incoming network service requests and then call the appropriate service program using the socket file descriptor as standard input and output.

Implementation of Unix domain sockets

  • Represented by a kernel data structure socket
  • Socket specific functions like Socket(), setsockout()
  • The functions are implemented with a single system call socketcall which calls all the necessary functions by reference to the first parameter. The file operation read(), write(), poll(), ioctl(), lseek(), close() are called directly
  • Operations of unix domain sockets
    • long sys_socket(int family, int type, int protocol);// creates a socket file descriptor
    • long sys_connect(int fd, struct sockaddr * uservaddr, int addrlen); //bind the socket to the unix domain address with specified length
    • long sys_listen(int fd, int backlog);//checks whether any connections are being accepted at the server address.
    • long sys_accept(int fd, struct sockaddr *upeer_sockaddr, int *upeer_addrlen));//server informs the kernel that the connections are being accepted from now on
    • long sys_getsockname(int fd, struct sockaddr *usockaddr, int *usockaddr_len); //the address bound to the socket is returned.
    • long sys_shutdown(int fd, int how); //to shutdown but status to be given that whether the sending and receiving still allowed
    • sending and receiving from the sockets
      • Messages can be sent either as a datagram or stream of bytes
      • long sys_send(int fd, void *buff, int len, unsigned flags);
      • long sys_sendmsg(int fd, struct msghdr *msg, unsigned int flags);
      • long sys_recv(int fd, void *buff, int len, unsigned flags);
      • long sys_recvmsg(int fd, struct msghdr *msg, unsigned int flags)

Debugging using ptrace

Ptrace is a system call provided in unix for one process to take control of another process to debug errors or bugs in the system

The process under control can be run step by step and its memory can be read and modified.

int sys_ptrace(long request, long pid, long addr, long data);

the function processes various request defined in the parameter request and pid indicates the process id of the process to be controlled.

Using the request PTRACE_TRACEME, a process can specify that its parent process controls iut via ptrace().

System V IPC

Three forms of System V IPC

  • Semaphores

  • Message Queues

  • Shared Memory

System V IPC is different from POSIX API, but both are available in the linux kernel.

GNU C library in kernel version 2.2 includes the interfaces for shared memory and the semaphore according to POSIX.

Access Rights and Numbers

struct kern_ipc_perm

{

key_t key;  //key

uid_t uid; //Owner

gid_t gid;  //Owner

uid_t cuid;  //Creator

gid_t cgid; //Creator

mode_t mode; //Access Mode

unsigned long seq; //counter used to calculate the identifier

};

the user and group id needs 32bit for the Intel 32 bit architecture, so the kernel supported both the IPC_OLD and IPC_64.

Semaphores (System V)

  • Array of semaphores can be setup using the system calls
  • It is always possible to modify a number of semaphores.
  • They can be incremented or decremented in steps greater than 1.

Semaphores are created using the following structure

struct sem_array

{

struct kern_ipc_perm sem_perm; //access permission

time_t sem_otime; //time of the last semaphore operation

time_t sem_ctime; //time of the last change

struct sem *sem_base; //pointer to the first semaphore

struct sem_queue *sem_pending; //operation to be reversed

struct sem_queue **sem_pending_last; //last operation to be carried out

struct sem_undo *undo; //undo operation to be carried out

unsigned long sem_nsems:// Number of semaphores in this array

};

struct sem

{

int semval; //current value of the semaphore

int sempid; //Process ID of the last operation

};

Message Queues

  • Message consists of sequence of bytes and a code.
  • Processes send messages to the queue and can receive message
  • Messages are read in the same order in which they are entered in the message queue.

struct msg_queue

{

struct kern_ipc_perm q_perm; //Access rights

time_t q_stime; //time of last send

time_t q_rtime; //time of last receive

time_t q_ctime; //time of last change

unsigned long q_cbytes; //number of bytes in the queue

unsigned long q_qnum; //number of message in the queue

unsigned long q_qbytes; //capacity of wait queue in bytes

pid_t lspid; //pid of the last sender

pid_t q_lrpid; //pid of the last receiver

};

To send message, the processes use these functions

int sys_msgsnd(int msgid, struct msgbuf *magp, size_t msgsz, int msgflg);

int sys_msgrcv(int msgid, struct msgbuf *magp, size_t msgsz, long msgtyp, int msgflg)

Shared Memory

  • shared memory is the fastest form of Inter process communication
  • exchange data between processes using the machine code commands for reading and writing
  • the main drawback is that the processes need to use additional synchronization mechanism to avoid the race condition
  • Shared segment of memory is identified by a number.
  • The structure shmid_kernel is for the kernel segment and mapped to the user segment in the virtual address space by the processes with the help of attach function, the reverse action will be through the help of detach.

struct shmid_kernel

{

struct kern_ipc_perm shm_perm; //access rights

struct file *shm_file; //file in the shared memory

int id;

unsigned long shm_nattach; //number of attachments

unsigned long shm_segsz; //size of segment

time_t shm_atim; //time of last attach

time_t shm_dtim; //time of last detach

time_t shm_ctim; //time of creation

pid_t shm_cprid; //creator process id

pid_t shm_lprid; //process id of the last operation

};

Information for Semaphore, message queue and shared memory

seminfo

Value

semmni (maximum number of semaphore arrays)

128

semmns (maximum number of semaphores in the system

32000

semmsl (number of semaphores per array)

250

semvmx (maximum value of semaphores

32767

msginfo

Value

msgmni (maximum number of message queue)

16

msgmax (maximum size of a message in bytes)

8192

msgmnb (standarad value for the maximum size of a message queue in bytes)

16384

shminfo

Value

shmmni (maximum no of shared memory segment)

4096

shmmax (maximum size of SHM segment in bytes)

33,554,432

shmmin (mi

nimum size of SHM segment in bytes)

1

shmseg (permitted no of segments/processes)

4096

Pipes and Named Pipes (FIFO)

Pipes are the classical method of interprocess communication.

For example

# ls –l | more

The symbol | indicates a pipe and the shell in the above example runs the processes ls and more which are linked with a pipe. ls writes data to the pipe and more reads it.

Named pipes otherwise called as the FIFO (First in First Out) is the other variant of pipe.

They can be created like this

# mkfifo pathname

Example

# mkfifo hello

# ls –l hello

prw-r- -r- - 1 temp users 0 Aug 28 10.45 hello |

there are many similarities between the pipes and the FIFO, but the inode specification for the both are more or less the same.

following is the inode specification for the pipe

struct pipe_inode_info

{

wait_queue_head_t wait; //wait queue

char * base; //address of the FIFO buffer

unsigned int readers; //no of processes reading at this moment

unsigned int writers; //no of processes writing at this moment

unsigned int waiting_readers; //no of blocked process reading at this moment

unsigned int waiting_writers; //no of blocked processes writing at this moment

unsigned int r_counter; //no of read processes that have opened

unsigned int w_counter; //no of write processes that have opened

};

The length of the area of the pipe is managed in the i_size field. The system call pipe creates a pipe which involves setting up a temporary inode and allocating a page of memory which is decided by the architecture dependent model.

Opening a FIFO

    Blocking Non Blocking
For reading No Writing Processes Block Open FIFO
  Writing Processes Open FIFO Open FIFO
For Writing No Reading Processes Block Error ENXIO
  Reading Process Open FIFO Open FIFO
For Reading and Writing   Open FIFO Open FIFO

Communication Via Files (IPC)

This is one of the oldest way of data exchange in which the information is sent as a file.

File needs two type of locking

Mandatory Locking

  • Read wirte is blocked during the entire locking period

Advisory locking

  • Allows Read and write even after the lock has been set
  • processes accessing the file for Read and Write has to lock it and release it again

Locking Entire Files

There are two methods for locking the entire file

First Method is by the use of several system calls

  • link – a system call
  • create – another system call
  • combination of flags like O_CREAT and O_EXEC with open (system call)
  • lock a fle with open() function using O_CREAT | O_WRONLY | O_TRUNK, but this option fails under the superuser mode

Second Method

fcntl – A system call to lock the entire file. It is also useful to lock the file areas. In Linux 2.0 it is called flock() which is not advisable to use.

Locking file areas

int sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg);

int sys_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg);

where fd – file descriptor

cmd – includes F_GETLK, F_SETLK, F_SETLKW

Semantics of fcntl locks

Existing Lock Setting a read lock Write lock
None Possible Possible
More than One Possible Not Allowed
A Write Lock Not Allowed Not Allowed

Locking files may also lead to deadlock with the following example

Assume there are two processes wanted to read a file,

P1  -> 1 –> 2  (P1 tries to access the 1st block and then 2nd block within a file)

P2 –> 2  -> 1  (P2 tries to access the 2nd block and then 1st block within a file)

Now for getting the 2 and the 1st block, both processes will be in deadlock,

So Communication via files may not be a great option, of course there are some methods to avoid the deadlocks.

Inter process Communication in Linux Kernel (IPC)

When two processes trying to access a resource is called race condition.

Main challenge is to avoid the race condition, the following method is usually adopted to avoid the race condition

  1. use of shared memory can prevent race condition
  2. Use of threads. Threads use the same address space when switching it to other threads(so same page directories and tables), whereas processes uses different address which leads to exchange of page directories and page tables. This is made with the help of POSIX Threads.

Forms of IPC

  1. Resource Sharing (system V Shared memory )
  2. Synchronization (Mutex, Semaphores)
  3. Connection oriented data exchange (Virtual Sockets, Pipes, named Pipes(FIFO))
  4. Connectionless data exchange (Signals)

Linux implements all forms of IPC

Synchronization in the Kernel

When multiple processors are used, how each processor is synchronized in the kernel is what described in this section.

Processor can acquire a spinlock whenever it wanted to access a resource.

typedef struct

{

volatile unsigned int lock; //default value is 1

}spinlock_t;

  • whenever a lock is wanted, it is set to 0 and during unlock it is set to 1
  • the bus access to the other processors are blocked during the processing of this command (spinlock)
  • if spinlock cannot be set, the processor waits in a loop until a lock variable is released again.
  • single processor do not need spinlock

Other than spinlock, there is one more lock called the Read write lock

typedef struct

{

volatile unsigned int lock;

}rwlock_t;

Semaphore

struct semaphore

{

atomic_t count;

int sleepers;

wait_queue_head_t *wait;

};

  • The word count is declared with the type atomic_t indicates that the atomic means it does not leads to any race condition
  • up() is a function which increments the count variable and wakes up all the sleeping processes when the count value is less than or equal to 0
  • down() is the function which decrements the count variable, also increments the sleepers variable.
  • the sum of sleepers and count is the correct value of the semaphore

E-Learning through Blogging

E- Learning is one of the modern methods of active learning among the learners and the teachers.
Through various means, elearning can be achieved, one such modern tool available is through blogging.

So How to start a blog?
Following points indicates how to start a blog and how to post notes and other information for Elearning.
  1. There are many blogging platform like blogger, wordpress, typepad, livejournal, etc. (The best and free being the blogger and wordpress).
  2. go to http://www.blogger.com or http://www.wordpress.com and register with a username and start blogging.(If you have gmail ID, then you can use the same gmail id and password for blogger.com)
  3. Once you create a username and login the site, create a blog by identifying some title for your blog.
  4. For blogger, the url will http://tspembedded.blogspot.com (this is my own blog) and in wordpress.com, the url will be tspradeepkumar.wordpress.com 
  5. After creating a blog, write a new post, by typing a heading for the post and the content for the post.
  6. Each post can be in a category, for example, writing something about 8085 Microprocessors, (it may be in the category Or label as microprocessor. (later on if you want to add any number of posts in the microprocessor, it can be added). As an example, if you see my blog, i name the category as the subject name like Embedded Systems, Real Time Systems and now recently Linux Kernel Programming.  
  7. Once in a week, try publish the posts relevant to your lecture notes of subject content.May be at the end of the year or semester, you will be having a full book of information.
  8. The very first day of the class, may be the URL be given to the students, so that the students can be in touch with the subject on the very first day (as everyone is on the internet atleast once a day, for curiosity the students will definitely go and visit the website). If a teacher feels very tough to type the content and feels uncomfortable, then the teacher may invite students of a particular class and ask them to be contributor for that blog.
  9. All the blogging platform provides a way to comment on a particular posts, if the learner needs any doubt on a particular topic, he/she may raise a question through the comment section, which can be solved by the faculty concerned.
  10. There are other benefits also through blogging like attracting the visitors to your blog across the globe,  earn little revenues, after 2 or 3 years you may be having a notes of nearly 7 to 8 subjects and above all confidence level of the teacher will improve.

How to Recompile Network Simulator 2

Assumptions:

  • I have installed my ns-allinone-2.33 under /home/pradeep/ns-allinone-2.33
  • Path is been set in the .bash_profile (which are located at /home/pradeep/) in fedora and .bashrc in ubuntu

There are two ways in which the NS2 can be recompiled

1. You modify the existing algorithm (use the same header and the C++ source files)

2. Adding new files (adding new header and C++ source files)

In first method,

Step 1: open the shell prompt and go to the ns-allinone-2.33/ns-2.33/ folder(there are 3 makefiles, Makefile.vc, Makefile, Makefile.in, for adding any new file should be done at the Makefile.in file)

Step 2: Once the existing algorithm is changed (after modifying the existing header and the C++ source file), go to step 1

Step 3: execute the command, make and press enter, If you have no errors in your modified code, then the compilation will be success, else you may get errors, which needs to be corrected

Step 4: Test your tcl script for the new algorithm

In the second Method,

Step 1: open the shell prompt and go to the /home/pradeep/ns-allinone-2.33/ns-2.33/ folder(there are 3 makefiles namely Makefile.vc, Makefile, Makefile.in, for adding any new file should be done at the Makefile.in file)

Step 2: You may create new header and C++ source files for your new proposed algorithm and hence you need to tell that information to the Makefile.in (which is available in /home/pradeep/ns-allinone-2.33/ns-2.33/). For example, assume i created two files tcpnew.h and tcpnew.cc

Step 3: Put all your files inside a folder (for Example create a new folder tcpnew) and put the tcpnew folder under the folder /home/pradeep/ns-allinone-2.33/ns-2.33/

Step 4: Now open the Makefile.in using any editor (usually vi or gedit) and add the folder information to the OBJ_CC variable)

Step 5: anywhere inside the OBJ_CC variable, put the new folder information like this  tcpnew/tcpnew.o  \

Step 6: after editing the file entry, go to step 1 and execute the command make and press enter. If you have no errors in your modified code, then the compilation will be success, else you may get errors, which needs to be corrected.

Step 7: Test your tcl script for the new Algorithm.

url and email validation using Perl Regular expression

#!c:/perl/bin/perl -w
print “Content-Type: text/html \n\n”;
$email=”tspradeepkumar\@yahoo.com”;
print $email;
if($email =~ /^(\w|\-|\_|\.)+\@((\w|\-|\_)+\.)+[a-zA-Z]{2,}$/i)
{
print “\n $email is a valid id”;
}
else
{
print “\n Provide a valid email id”;
}

$url=”http://www.eadhoc.com“;
print “\n $url \n”;
if($url =~ /^http:\/\/+(www)+\.+[a-zA-Z0-9\-]+\.+[a-zA-Z]{2,6}$/i)
{
print “$url is a valid url”;
}
else
{
print “\n Provide a valid url “;
}

Hit Counter using PHP files

The same file (counter.txt) is used for both reading as well as writing. So each time the file should be closed before opening for the next time as read or write mode.

<?php
$filename=”counter.txt”;
$f=fopen($filename,”r”) or die(”Read Failed”);
$str=fread($f,filesize($filename));
echo “The page has been visited for $str times since 2nd June 2009″;
fclose($f);

$f=fopen($filename, “w”) or die(”Write Failed”);
$str=$str+1;
fwrite($f,$str);
fclose($f);
?>

Submitting the html form to the same page

This scripts tells how to submit a form to the same page itself….

<form method=post action=”<?php echo $_SERVER['PHP_SELF']; ?>”>
Enter your Name: <input type=text name=user>
<input type=submit name=submit>
</form>
<?php
if(isset($_POST['submit']))
{
$user=$_POST['user'];
echo “Welcome Mr. $user”;
}
?>

Setting root password in MySQL and Phpmyadmin

If you are using PhpMyAdmin as part of WAMP(Windows, Apache, MySQL and PHP), then you need to take care of the root password and hence follow the steps.
If you change, only the MySQL password or PHPMyAdmin Password, then the PHPMyAdmin screen does not show the databases, files, etc (instead it will show access denied).

  1. So Both MySQL and PhpMyAdmin password should be changed and both password should be equal.
  2. Go to mysql command window (by default the MySQL has no password)
  3. Execute the command set password=password(”abc123″);
  4. find the config.inc.php file under phpmyadmin folder (usually in WAMP it will be under C:\wamp\apps\phpmyadmin2.11.6) and find the following line in the config.inc.php file $cfg['Servers'][$i]['password'] = ‘ ‘; // MySQL password
  5. Replace the line to $cfg['Servers'][$i]['password'] = ‘abc123′; (as the password the mysql is also “abc123″.
  6. now restart the server and now open http://localhost/phpmyadmin/

File Upload to Server in PHP

For uploading Files to the server, we need to run two files, For example let us say upload.html (to select the file) and upload.php (to upload)…. let us define each of the two files

//upload.html

<form method=post action=upload.php enctype=multipart/form-data>
Select a File: <input type=file name=myfile>
<input type=submit value=upload>
</form>

//upload.php

<?php
echo $_FILES['myfile']['size'];
echo $_FILES['myfile']['type'];
echo $_FILES['myfile']['tmp_name'];
echo $_FILES['myfile']['name'];
@copy(echo $_FILES['myfile']['tmp_name'], “./upload/”.$_FILES['myfile']['name']) or die(”Couldn’t Upload);
?>

in the above php script, instead of copy() function, move_uploaded_file() function can also be used. Before running this script, a folder called as upload should be created in the root path, for example, if /var/www/ is the document root, then a folder upload should be created at /var/www/upload

suppose if the server allows for file size less than 1 megabytes and file types to be images of jpg only, then the above code can be modified in the following way.

//upload.php

<?php
echo $_FILES['myfile']['size'].”<BR>”;
echo $_FILES['myfile']['type'].”<BR>”;
echo $_FILES['myfile']['tmp_name'].”<BR>”;
echo $_FILES['myfile']['name'].”<BR>”;
if($_FILES['myfile']['size']<=1048576 and $_FILES['myfile']['size'] == “image/jpeg” or $_FILES['myfile']['size']==”image/jpg”)
{
@copy($_FILES['myfile']['tmp_name'], “./upload/”.$_FILES['myfile']['name']) or die(”Couldn’t Upload”);
}
else
{
echo ” The file size should be less than 1 Megabytes and file type should be jPEG files only”;
}
?>

Connect to MySQL Database using PHP

The following script tries to connect the database and if it is failed it will display relevant message. The server is localhost and username/password combination is root/abc123. it is necessary to create a database ‘dummy’ before executing the script. Click here to set password for mysql and phpmyadmin

<?php

$con=mysql_connect(”localhost”,”root”,”abc123″) or die(”Couldnt Connect to database”);

$a=mysql_select_db(”dummy”,$con);

if($a)

echo “Database Selection Success”;

else

echo “Database Selection Failed”;

?>

Sorting Functions in PHP

There are different sorting functions used to sort the arrays and other variables. Some of the their functions are listed below:

sort() – used to sort an array in normal order

rsort() – used to sort an array in normal reverse order

asort() – used to sort array and maintain the index association(key value pair maintains even after sorting)

arsort() – same as asort() but in reverse order

ksort() – used to sort an array by its keys….

Example Program

<?php

$fruits[0]=”Orange”;

$fruits[1]=”Mango”;

$fruits[2]=”Banana”;

print_r($fruits);

sort($fruits);

print_r($fruits);

?>

The above script displays the fruits array in normal order and also in sorted order…. simply replace the sort function with other sort functions and may see the results.

for key value, change the fruits array like the following script

$fruits=array(10=> “Orange”, 100=> “Mango”, 1000=>”Banana”);

and use the functions ksort, asort and arsort () and see that the key value pair is unchanged…..

User Validation/Authentication in PHP

If a user visits a website, where he is asked for validating his username and password and will be validated with the MySQL Database. the following scripts demonstrate this.
Step 1: create a database called dummy by executing “create database dummy” in mysql command window or shell window
Step 2: Create a table called usertable with two fields ‘user’ and ‘pass’ and populate with some values.
create table usertable (user varchar(20), pass varchar(30));
insert into usertable values (’pradeep’,'pradeep’);

Step 3: write the following html script and name it as user.html
<form method=post action=user.php>
Enter the Username: <input type=text name=username> <BR>
Enter the password: <input type=password name=password> <BR>
<input type=submit>
</form>
Step 4: Write the following PHP script and name it as user.php
<?php
$user=$_POST['username']; //get the username from the previous page (html page)
$pass=$_POST['password']; //get the password from the previous page
$conn=mysql_connect(”localhost”,”root”,”abc123”) or die(”Connection Failed”. mysql_error());
mysql_select_db(”dummy”,$conn) or die(mysql_error());
$query=”Select * from usertable where user=’$user’ and pass=md5(’$pass’)”;
$result=mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($result);//returns atleast 1 row if the username password combination is valid
echo $num;//display the number of rows returned
if($num != 0)
{
echo “Welcome Mr.$user, you are authenticated”;
}
else
{
echo “Username/password combination Failed”;
}
mysql_close($conn);
?>
The above script checks for the username and password combination, if the combination is success, then atleast one row will be returned, else the returned rows will be 0.

User Creation using PHP/MySQL

Username/password Creation in PHP and MySQL. a Simple form is designed to get the username and two passwords the objectives are

1. Both the username and password should contain atleast 6 characters

2. both the passwords should be same

3. upon submitting to the database, the existence of username is checked with the database, if the username already exists, then the user is allowed to select some other, else user is created.

//html file is given below, file name is

<h2>User Registration Form</h2>

<pre>

<form action=”userregis.php” method=”post”>

Enter the username: <input name=”username” type=”text” /> Enter the password: <input name=”password1″ type=”password” />

Confirm the password: <input name=”password2″ type=”password” />

<input type=”submit” value=”create” />

</form>

</pre>

//userregis.php

<?php

include(”dbconnect.php”);

$u=$_POST['username'];

$p1=$_POST['password1'];

$p2=$_POST['password2'];

$query1=”Select * from usertable where user=’$u’”;

$query2=”Insert into usertable values (’$u’,password(’$p1′))”;

if(strlen($u)<6 or strlen($p1)<6)

{

echo “The username and password should contain atleast 6 characters”;

}

else if($p1!=$p2)

{

echo “The Passwords should be same”;

}

else

{

$r1=mysql_query($query1) or die(”Database Selection Failed”);

$num=mysql_num_rows($r1);

if($num==1)

{

echo “The username <h3> $u </h3> is already exists”;

}

else

{

$r2=mysql_query($query2) or die(”Database Insertion Failed”);

if($r2)

echo “Username <h3> $u </h3> is successfully created”;

else

echo “Database Insertion Failed”;

}

}

?>

<?php

include(”dbconnect.php”);

$u=$_POST['username'];

$p1=$_POST['password1'];

$p2=$_POST['password2'];

$query1=”Select * from usertable where user=’$u’”;

$query2=”Insert into usertable values (’$u’,password(’$p1′))”;

if(strlen($u)<6 or strlen($p1)<6)

{

echo “The username and password should contain atleast 6 characters”;

}

else if($p1!=$p2)

{

echo “The Passwords should be same”;

}

else

{

$r1=mysql_query($query1) or die(”Database Selection Failed”);

$num=mysql_num_rows($r1);

if($num==1)

{

echo “The username <h3> $u </h3> is already exists”;

}

else

{

$r2=mysql_query($query2) or die(”Database Insertion Failed”);

if($r2)

echo “Username <h3> $u </h3> is successfully created”;

else

echo “Database Insertion Failed”;

}

}

?>

//dbconnect.php

<?php

$con=mysql_connect(”localhost”,”root”,”abc123″) or die(mysql_error());

$db=mysql_select_db(”dummy”,$con) or die (mysql_error());

?>

Sending Email in PHP

How to send email using PHP. The following section describes that with coding…..

Before using Mail in PHP, note that, PHP mail is intended for only a small scale use and not for large scale Email systems. So for large scale, you may use other mal softwares.

Email can be sent

  • as ordinary plain message
  • as an HTML Message
  • with attachments

We will see, one by one

Mail in PHP

there is a function mail() which helps in sending mails through PHP Scripts.

the syntax is mail(To address, Subject, Message, headers)

To address – to which address you want to send mail

Subject of the mail -

Message – body of the mail and

headers – are the http headers sent along with the mail to inform the email clients

Example Headers are

  1. “From:abc@xyz.com ” //to inform the from address
  2. “Reply-To:xyz@xyz.com ” //Reply to a different email id
  3. “Bcc:a@a.com ” //Blind Carbon Copy
  4. “Cc:b@b.com” //Carbon Copy
  5. Return-Path:abc@a.com”
  6. X-Mailer: PHP5 //inform the email client
  7. MIME-Version:1.0
  8. Content-Type: text/plain” //text/html or multipart/mixed, multipart/alternative (content type is useful for attaching files with MIME headers)
  9. Content-Disposition: attachment;filename=”image.jpg”
  10. Content-Transfer-Encoding: base64″ //Encoding Standards

Email as ordinary Plain message

<?php

$to=”abc@xyz.com”;

$subject=”This is a Simple mail from PHP”;

$message=”<h1>this is a text message</h1>”;

$m=mail($to,$subject,$message);

if($m)

echo “Mail sent successfully”;

else

echo “Mail Failed”;

?>

The mail() function returns a boolean value, so if the mail is sent successfully, it returns 1 else 0. Since the above script does not uses any headers, the mail will be sent as plain text mail and the H1 tag wont be validated for a HTML.

The following code send mail as a HTML page

Email as HTML message

<?php

$to=”abc@xyz.com”;

$subject=”This is a Simple mail from PHP”;

$message=”<h1>this is a text message</h1>”;

$headers = “Content-Type: text/html \r\n”;

$m=mail($to,$subject,$message,$headers);

if($m)

echo “Mail sent successfully”;

else

echo “Mail Failed”;

?>

The above script uses a MIME type as text/html, to the message with H1 rendered as an HTML Tag and the message will display with size of header 1

Email with attachments

Lot of headers to be used for Emails with attachments.

<?php

//set the to, subject and message
$to=”tspembedded@gmail.com”;
$subject=”Here’s the mail with attachment”;
$message=”I am attaching a PDF file for your information”;

//Open the file to attach

$filename=”rts.pdf”; //name of the file and its path

$f=fopen($filename,”rb”); //open the file under binary mode
$data=fread($f,filesize($filename));  //read the file and its size
fclose($f); //close the file pointer
//header setting
$data=chunk_split(base64_encode($data)); //split the file and encode each chunks to base 64
$headers=”Content-Type: application/pdf \r\n”; //since it is a pdf file, mention the mime type
$headers.=”Content-Disposition: attachment;filename=rts.pdf \r\n”; //header to attach the file with filename
$headers.=”Content-Transfer-Encoding: base64 \r\n”.$data.”\r\n”; //header with encode setting

$m=mail($to,$subject,$message,$headers);
if($m)
echo “mail to $to is a success”;
else
echo “Mail to $to Failed”;
?>

the Mime type used in the above script is application/pdf, so only the attachment will be shown in the email client, but the message wont be showed at all…

to make mixed mode, use the mime type as multipart/mixed.

For any other queries and suggestions, Please Comment!!!!!!

How to Set Cookies in PHP

To Set cookies using PHP, setcookie() function is used. Cookie is a piece of information that stored in the client’s machine (usually browser) by the server.

setcookie() contains 6 parameters

  1. Name of the cookie- usage- “FirstCookie”
  2. The value of the cookie. This value is stored on the clients computer; do not store sensitive information. Assuming the name is ‘Iamthecookie’, this value is retrieved through $_COOKIE['Iamthecookie']
  3. Expiration time of a cookie- usage – time()+60 //means that current time + 60 seconds,
  4. The path on the server in which the cookie will be available on. If set to ‘/’, the cookie will be available within the entire domain . If set to ‘/osp/’, the cookie will only be available within the /osp/ directory and all sub-directories such as /osp/bar/ of domain . The default value is the current directory that the cookie is being set in.
  5. The domain that the cookie is available. To make the cookie available on all subdomains of example.com then you’d set it to ‘.example.com’. The . is not required but makes it compatible with more browsers. Setting it to www.example.com will make the cookie only available in the www subdomain.
  6. Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. When set to ,  the cookie will only be set if a secure connection exists. The default is 0. On the server-side, it’s on the programmer to send this kind of cookie only on secure connection.

<?php

$c=setcookie(”name”,”Iamthecookie”,time()+100, “/”,”.eadhoc.com”, 0);

if($c)

echo “The cookie set successfully”;

else

echo “Cookies may be disabled or not set”;

?>

The above script sets a cookie of name ‘name’ and value being ‘Iamthecookie’ which will be available for 100 seconds, and it is for the entire server eadhoc.com and it is not secure (as it uses 0 – HTTP)

to delete the above said cookie,

change the code to

<?php

$c=setcookie(”name”,”",time()-100);

if($c)

echo “The cookie deleted successfully”;

else

echo “Something Wrong”;

?>

Input Validators in PHP

Source : http://www.openpeta.com

<?php
/*
* FileName:validation.php
* Created on Jan 16, 2007
* Author:Antony
*/
function validateUname($uname) {
if (ereg(’^[a-zA-Z0-9_]{3,50}$’, $uname))
return true;
else
return false;
}

function validatePassword($pwd) {
if (strlen($pwd) >= 6)
return true;
else
return false;
}

function validateEmail($email) {
if (eregi(’^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$’, $email))
return true;
else
return false;
}

function validateDate($date) {
if (ereg(’(^[0-9]{1,2})-[0-9]{1,2}-([0-9]{4}$)’, $date))
return true;
else
return false;
}

function validateMobile($mobile) {
if (ereg(’(^[+0-9]{1,3})*([0-9]{10,11}$)’, $mobile))
return true;
else
return false;
}

function validateUrl($url) {
if (ereg(’^[http://]+[www]*\.[0-9a-zA-Z_.]+\.[a-zA-Z.]{1,5}$’, $url))
return true;
else
return false;
}

function validatePin($pin) {
if (ereg(’^[0-9]{6}$’, $pin))
return true;
else
return false;
}
?>

Subscribe/Unsubscribe to a Mailing List

The following PHP scripts shows how to maintain a mailing list. the users are asked to subscribe/unsubscribe to a mailing list and the entire mails are stored in the database.

The user has to type his email id and his/her name and select the radio button subscribe/unsubscribe. Since one email ids are unique, the email id field is a primary key.

Database Queries:

create database dummy;

create table mail(id varchar(100) primary key, name varchar(100))

HTML Form:

<FORM method =post action=sub.php>

<CENTER>

<FONT FACE = “ALGERIAN” SIZE = 6><BR><BR><B><u>SUBSCRIBER / UNSUBSCRIBER </U></B></FONT><BR><BR><BR><BR>

<FONT SIZE = 4><B>Enter E-Mail ID:</B></FONT><INPUT TYPE = text NAME = eid><BR><BR>

<FONT SIZE = 4><B>Name:</B></FONT><INPUT TYPE = text NAME = ename><BR><BR>

<INPUT TYPE = “radio” name = “radio” value = “Radio1″ > Subscriber

<INPUT TYPE = “radio” name = “radio” value = “Radio2″ > Unsubscriber <BR><BR>

<INPUT TYPE = submit VALUE = submit>

<INPUT TYPE = reset VALUE = reset>

</CENTER>

</FORM>

<FORM method =post action=sub.php>

<CENTER>

<FONT FACE = “ALGERIAN” SIZE = 6><BR><BR><B><u>SUBSCRIBER / UNSUBSCRIBER </U></B></FONT><BR><BR><BR><BR>

<FONT SIZE = 4><B>Enter E-Mail ID:</B></FONT><INPUT TYPE = text NAME = eid><BR><BR>

<FONT SIZE = 4><B>Name:</B></FONT><INPUT TYPE = text NAME = ename><BR><BR>

<INPUT TYPE = “radio” name = “radio” value = “Radio1″ > Subscriber

<INPUT TYPE = “radio” name = “radio” value = “Radio2″ > Unsubscriber <BR><BR>

<INPUT TYPE = submit VALUE = submit>

<INPUT TYPE = reset VALUE = reset>

</CENTER>

</FORM>

// Sub.php

<?php

$u = $_POST['eid'];

$p1 = $_POST['ename'];

$p2 = $_POST['radio'];

$con=mysql_connect(”localhost”,”root”,”") or die(”DB connection Failed”);

$db=mysql_select_db(”dummy”,$con) or die(mysql_error());

$query1 = “select * from mail where id = ‘$u’ “;

$query2 = “insert into mail values(’$u’,'$p1′)”;

$query3 = “delete from mail where id = ‘$u’”;

if ($p2 == “Radio1″)

{

$r1 = mysql_query($query1) or die (”Query1 Failed”);

$num = mysql_num_rows($r1);

if ($num == 1)

{

echo “Hai!$u You are already subscribed”;

}

else

{

$r2 = mysql_query($query2) or die (”Insertion query Failed”);

echo “Hai!$u.Thanks for signing up”;

}

}

else if ($p2 == “Radio2″)

{

$r1 = mysql_query($query1) or die (”Query1 Failed”);

$num = mysql_num_rows($r1);

if ($num == 1)

{

$r3 = mysql_query($query3) or die (”Deletion query Failed”);

echo “Hai! $u ur unsubscribed”;

}

else

{

echo “Hai! $u couldnt find ur name in the list”;

}

}

mysql_close($con);

?>

Page Directory, Page table and Memory Mapping Functions

Page Directories and Page Tables 
Page directories and the page tables for the user and the kernel segment is managed by the kernel itself.
The short identifier for the page directory is pgd, pmd (page middle directory) and the short identifier for the page table is pte.
The data type for the page directory and the page table is respectively pgd_t, pmd_t and pte_t. 

Functions  relevant to Page directory
  • pgd_val(), pmd_val() - These functions allow to access the real value of the directory entry (either 32 bit or 64bit).
  • pgd_alloc(), pmd_alloc() - provides the memory page for the respective page directory.  
  • pgd_free(), pmd_free() - The directories are freed.
  • pgd_clear, pmd_clear() - deletes the entry in the page directory.
  • pgd_present, pmd_present() - checks whether the directoryentry refers to the page middle directory or page table, returns apositive result if there is an entry present, else returns a negativevalue.
  • pgd_none(), pmd_none() - just the reverse of pgd_present(), pmd_present()
  • set_pgd(), set_pmd() - the kernel fills the page directory and the page middle directory with entries.
Functions related to page tables
  • pte_val() -returns the value of the page table
  • pte_alloc(), pte_alloc_kernel() -allocating the page table entry
  • pte_free(), pte_free_kernel() - free a page table,
  • pte_page() -page table entry finds a pointer into the mem_map which contains all the physical memory pages of the computer.
  • set_pte() -the values of the page table entry are set, but the entry must either not be present or cannot be updated by the hardware.
Memory Mapping Functions

In the header file sys/mman.h, the functions about memory mapping is provided
extern __ptr_t mmap(__ptr_t addr, size_t len, int prot, int flags, int fd, __off_t off);
From the offset off, mmap() maps the file or the device to which the file descriptor fd refers as virtual memory area. There is also a flag to be used for anonymous mapping.
extern int munmap(_ptr_t addr, size_t len);
The above function is to unmap the memory that is already mapped to the user segment.
extern int mprotect(_ptr_t addr, size_t len, int prot);
the above function is to be provide new protection attributes to the particular area of the memory.

Conversion of Linear Address to Physical Address

picture

  • The Conversion from the linear address to physical address is done by a separate processor or a MMU
  • In architecture Independent model, page conversion is a three level process which takes four steps
    • 1st part is used as an index in the page directory which refers to the page middle directory
    • 2nd part serves as an index to the page middle directory, there it refers to the page table
    • 3rd Part used as an index to the page table, there it refers to the physical memory
    • 4th Part of the address gives the offset within the selected page of memory.
  • The x86 model supports a two level conversion of linear address to physical address.
  • Highmem support helps upto physical memory size of 4GB being the address space of 32bit address, but Intel added four additional address pins to the Pentium Pro and created the physical address extension (PAE). Since the two level conversion supported only 32 bit addresses, the PAE has the control of supporting three level address conversion as in the Linux Architecture Independent Model.

Memory Management (Linux Kernel)

 

The data related to the processes are to be stored in the memory somehow by the operating system. In the olden days, primary memory is a scarce resource. and the application demands increases, so store the data of a process, it is necessary to use other forms of memory, either we can use

  • Physical address space
  • virtual address space
  1. Mapping the virtual addresses onto the physical addresses is the responsibility of the operating system’s memory management system.
  2. Memory protection prevents two processes exchanging data by changing the areas of memory used by both.
  3. The program code of a process can be executed in different threads at the same time. In principle, these threads share a common virtual address space.

Architecture Independent Memory Model

  1. Access time is the primary factor used in Physical (main) memory. To minimize the access times, the cache memory versions like Level1 Cache (L1 cache) and level2 cache (L2 Cache) were used.
  2. Cache memory improves the performance of Personal Computers.
  3. The physical memory is divided into pages (page memory) and the size is determined by the PAGE_SIZE macro of the asm/page.h file.
  4. For the x86 architecture, the page size is usually 4kbytes and 8kbytes for the 64 bit architecture and 64kbytes for the IA64 architecture
  5. The memory management units (MMU) of various architecture has the support the page size of 4Mbytes, which is necessary when the mapping between the physical address and virtual address takes place.

Virtual Address Space

  • The Code and the data of a process is scattered in the virtual address space.
  • the size of the Virtual address space depends on the number of bits used in the memory address, for example for a 32 bit memory address, nearly 4GBytes is available and for 64 bit memory address 16exabytes are available.
  • usually the process can be either in the user mode or system mode. Each process starts in the user mode initially and changed to system mode when there is a need to access the system resources.
  • The standard procedure to change from the user mode to the system mode is to call the software interrupt 0x80. In the user mode, access to kernel memory is forbidden. Access to the memory page can be granted to system mode or user mode.
  • The physical memory of the kernel maps to the virtual memory only after an offset (PAGE_OFFSET), but this memory space is permitted only under the system mode.
  • For this reason, the user address space is always smaller than the theoretical maximum. For example, for x86 – Page Size (4Kbytes) – User Address Space (3GB), but the theoretical maximum is 4GB.

System Calls

System calls provides a layer between hardware and the user space.

  • Provides abstracted hardware interface for user space
  • system calls ensure security and stability
  • single common layer between user space and rest of the system

System calls usually accessed via function calls.

for example

  1. call to the printf () function   //through a function call
  2. Access to the C library functions
  3. access the kernel and execute the write() function to write on to the screen
  • System calls returns a long variable for success, negative value indicates error
  • Each system call is assigned a syscall number defined in sys_call_table in entry.S
  • system calls are faster than many other operating systems

Example System Calls

//Example1

asmlinkage long sys_getpid)

{

return current->tgid;

}

Pause:

Interrupts the execution of the program until the process is reactivated by a signal.

asmlinkage int sys_pause(void)

{

current-> state = TASK_INTERRUPTIBLE;

schedule();

return ERESTARTNOHAND;

}

Complex System Calls

fork()

The fork() function shall create a new process. The new process (child process) shall be an exact copy of the calling process (parent process) except as detailed below.

SYNTAX:

#include <unistd.h>

pid_t fork(void);

  • The child process shall have a unique process ID.
  • The child process shall have a different parent process ID, which shall be the process ID of the calling process.
  • The child process shall have its own copy of the parent's file descriptors. Each of the child's file descriptors shall refer to the same open file description with the corresponding file descriptor of the parent.

vfork()

create new process; share virtual memory

#include <unistd.h>

pid_t vfork(void);

The vfork() function has the same effect as fork(), except that the behavior is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before successfully calling exit() or one of the exec family of functions.

clone()

clone() is a system call on the Linux kernel related to multithreading works similar like fork() but shares the information with the calling process.

Page Directories and Page Tables in Linux Kernel

Page directories and the page tables for the user and the kernel segment is managed by the kernel itself.
The short identifier for the page directory is pgd, pmd (page middle directory) and the short identifier for the page table is pte.
The data type for the page directory and the page table is respectively pgd_t, pmd_t and pte_t. 

Functions  relevant to Page directory
  • pgd_val(), pmd_val() - These functions allow to access the real value of the directory entry (either 32 bit or 64bit).
  • pgd_alloc(), pmd_alloc() - provides the memory page for the respective page directory.  
  • pgd_free(), pmd_free() - The directories are freed.
  • pgd_clear, pmd_clear() - deletes the entry in the page directory.
  • pgd_present, pmd_present() - checks whether the directory entry refers to the page middle directory or page table, returns a positive result if there is an entry present, else returns a negative value.
  • pgd_none(), pmd_none() - just the reverse of pgd_present(), pmd_present()
  • set_pgd(), set_pmd() - the kernel fills the page directory and the page middle directory with entries.
Functions related to page tables
  • pte_val() -returns the value of the page table
  • pte_alloc(), pte_alloc_kernel() -allocating the page table entry
  • pte_free(), pte_free_kernel() - free a page table,
  • pte_page() -page table entry finds a pointer into the mem_map which contains all the physical memory pages of the computer.
  • set_pte() -the values of the page table entry are set, but the entry must either not be present or cannot be updated by the hardware.

Main Algorithms of a kernel…

Timer Interrupts

  • There is one hardware timer that generates interrupts every 10ms and all the software timer synchronizes with it.
  • Usually the timer stored in the variable jiffies.

unsigned long volatile jiffies;

The variable jiffies is modified by the timer interrupt every 10ms and hence it is declared as volatile.

volatile struct timeval xtime:

This is the actual time which again modified by the timer interrupt

Other functions of timer interrupt like

do_timer();

updates the jiffies

timer_bh();

updates the timer and processing of the timer related functions

update_process_time();

collects data for the scheduler and decides whether it has to be scheduled.

 

The Scheduler

schedule () is function declared in kernel/sched.c

The actions of the scheduler is given below, once the schedule() function is called,

  • Upcoming software interrupts are processed (so interrupts are given higher priority over the other entities in the system)
  • process with highest priority determined (if two tasks has equal priority, then the OS will determine which task to be executed first.)
  • real time process takes over normal ones. (Real time processes area associated with deadlines, whereas the normal ones doesn’t have deadlines, this factor is determined by the rt_priority of the schedule structure)
  • new process becomes current process. (whenever a process is getting scheduled by the scheduler, then it will become the current process)