Sei sulla pagina 1di 7

1.

Write an ioctl command to sort the first quantum of the first Sculldev in scull
device(modify main.c)
Solution

#define SCULL_DEBUG
#include <linux/slab.h> /* kmalloc() */
#include<linux/string.h>
int scull_ioctl(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{

int err = 0, tmp;


int j,dlen,i=0,ret = 0;
Scull_Dev *dev;

case SCULL_IOSORT :
dev=&scull_devices[0];
// printk("<1> %s",dev->data[0]);
// while(dev!=NULL)
{
char *str='\0';
str = dev->data[0];
dlen=0;
printk("<1>str %s ",dev->data[0]);
printk("\n inside ");
dlen = strlen(str);
*(str+dlen)='\0';
printk("<1>str %s ",str);
printk("<1>dlen = %d ",dlen);
for(i=0;i<dlen-1;i++)
for(j=0;j<dlen-i-1;j++)
{
// printk("<1> str i %c \n ", *(str+i));
if(*(str+j) > *(str+j+1))
{
temp =*(str+j);
*(str+j)=*(str+j+1);
*(str+j+1) =temp;
}

}
printk("<1>str %s ",str);
*(str+dlen)='\0';
printk("<1> %s", str);
dev->data[0]=str;
// dev=dev->next;
}
break;
In Scull.h header file
#define SCULL_IOSORT _IO(SCULL_IOC_MAGIC,16)
#define SCULL_IOC_MAXNR 16

2.Write an ioctl command to sort the nth quantum of the first Sculldev in scull
device(modify main.c)

int scull_ioctl(struct inode *inode, struct file *filp,


unsigned int cmd, unsigned long arg)
{

int err = 0, tmp;


int j,dlen,i=0,ret = 0;
Scull_Dev *dev;

case SCULL_IOCSORT :
dev=&scull_devices[0];
ret=__get_user(nquantum,(int *)arg);
if(ret > dev->quantum )return ret;
// printk("<1> %s",dev->data[0]);
// while(dev!=NULL)
{
char *str = dev->data[nquantum];
dlen=0;
// printk("<1>str %s ",str);
printk("\n inside ");
dlen = strlen(str);
//printk("<1>dlen = %d ",dlen);
for(i=0;i<dlen-1;i++)
for(j=0;j<dlen-i-1;j++)
{
// printk("<1> str i %c \n ", *(str+i));
if(*(str+j) > *(str+j+1))
{
temp =*(str+j);
*(str+j)=*(str+j+1);
*(str+j+1) =temp;
}
}
printk("<1> %s", str);
dev->data[nquantum]=str;
// dev=dev->next;
}
break;

In scull.h file

#define SCULL_IOCSORT _IOW(SCULL_IOC_MAGIC,16,nquantum)


#define SCULL_IOC_MAXNR 16
3.Implement an ioctl command to change the first and second pointers in the first sculldev of
the scull device.
Solution
In scull_ioctl
Scull_Dev *temp;

case SCULL_IOCCHANGE:

temp=kmalloc(sizeof(Scull_Dev),GFP_KERNEL);
memset(scull_devices,0,sizeof(Scull_Dev));
temp = &scull_devices[1];
scull_devices[1]=scull_devices[0];
scull_devices=temp;
printk("<1>first \n second %p %p %p
\n",&scull_devices[1],&scull_devices[0],scull_devices);
break;

4.Display the first 50 characters of the first quantum of each of the 4 scull devices using
proc filesystem.
In scull_read _procmem
int m;
for (j = 0; j < d->qset; j++) {
if (d->data[j])
{
if(j==0)
{
char *str = d->data[0];
char *st="";
m=0;
while(*str&& m<50)
{
*st=*str;
str++;
st++;
m=m+1;
}
len+=sprintf(buf+len," %4i : %s
\n",j,st);
}}
In scull_ioctl()

int err = 0, tmp;


int j,dlen,i=0,ret = 0;
Scull_Dev *dev;
char temp;
5. Display the last 50 characters of the of each of the 4 scull devices using proc
filesystem.
In scull_read_procmem()
int m;
for (j = 0; j < d->qset; j++) {
if (d->data[j])
{
if(d->data[0])
{
char *str = d->data[0];
printk("<1> data = %s ", str);
complement everything until
len+=sprintf(buf+len," %4i : %s \n",j,st);
}}
In scull_ioctl
int err = 0, tmp;
int j,dlen,i=0,ret = 0;
Scull_Dev *dev;
char temp;

6. Implement an ioctl command to empty the scull device( use scull trim).

In scull_trim()
printk("<1> trim apply");

In scull_ioctl()
int err = 0, tmp;
int j,dlen,i=0,ret = 0;
Scull_Dev *dev;
char temp;

case SCULL_IOCTRIM:
for(i=0;i<scull_nr_devs;i++)
scull_trim(&scull_devices[i]);
break;

In scull.h add

#define SCULL_IOCTRIM _IO(SCULL_IOC_MAGIC,16)


#define SCULL_IOC_MAXNR 16

7 Modify lseek to move fops to the ith qpos of the first quantum. Suppose the call is lseek (fd, 5,
SEEK_SET), move to the 5th quantum.

In ssize_t scull_read()
printk("<1>fpos %d spos %d qpos = %d \n", *f_pos,s_pos,q_pos);
In scull_llseek()
off=off*dev->quantum;
In scull_ioctl()
int err = 0, tmp;
int j,dlen,i=0,ret = 0;
Scull_Dev *dev;
char temp;

8. Create a proc entry to change the quantum in each scull device.

In
int m;
for (i = 0; i < scull_nr_devs && len <= limit; i++) {
Scull_Dev *d = &scull_devices[i];
if (down_interruptible(&d->sem))
return -ERESTARTSYS;
d->quantum=20;
len += sprintf(buf+len,"\nDevice %i: qset %i, q %i, sz %li\n",
i, d->qset, d->quantum, d->size);
for (; d && len <= limit; d = d->next) { /* scan the list */
d->quantum=20;
len += sprintf(buf+len, " item at %p, qset at %p\n", d,
d->data);
if (d->data && !d->next) /* dump only the last item - save
space */

for (j = 0; j < d->qset; j++) {


if (d->data[j])
{
len += sprintf(buf+len," % 4i: %8p\n",j,d-
>data[j]);

9. Create a proc entry to sort the characters in the first quantum of each scull device.
int scull_read_procmem() add

int k,m,len=0,dlen=0;
char temp;
char *str;
at 8th line add
if (d->data[0])
{
str = d->data[0];
dlen=5;

dlen=strlen(d->data[0]);
for(k=0;k<dlen-1;k++)
{
for(m=0;m<dlen-k-1;m++)
{
if(*(str+j)>*(str+j+1))
{
temp=*(str+j);
*(str+j)=*(str+j+1);
*(str+j+1)=temp;}}}}
10 Create a proc entry to blank out the first quantum of each scull device.

int scull_read_procmem() add

int m;
d->data[0]=""; @line 8

In scull_ioctl()add
int err = 0, tmp;
int j,dlen,i=0,ret = 0;
Scull_Dev *dev;
char temp;

11 Modify scull_priv to allow each user to have a single persistent set of data.

In scull_ioctl()add
int err = 0, tmp;
int j,dlen,i=0,ret = 0;
Scull_Dev *dev;
char temp;

In scull_init_module()
#ifdef CONFIG_DEVFS_FS//after this add following statement

printk("<1>registering scull012");
printk("<1>looking for other deviecs\n");//after 4 lines
if ( (result = scull_p_init()) )
goto fail;
printk("<1>res1 = %d \n",result);
if ( (result = scull_access_init()) )
goto fail;

printk("<1>res2= %d \n",result);

In Access.c

@line 273 add in function static Scull_Dev *scull_c_lookfor_device()


printk("<1> entered into the function loof for device");
for (lptr = scull_c_head; lptr && (lptr->user != current->uid); lptr =
lptr->next)
{
prev=lptr;
printk("<1> inside loop list K VALUE %d \n",lptr->key);
}
lptr->user=current->uid;@line 8
printk("<1>curret user %d ",current->uid);@ line 11

In scull_c_open() function add


lptr->user=current->uid;//after lptr->key=key;
printk("<1>curret user %d ",current->uid);//after 2 lines
printk("<1> K VALUE %d \n",key);//after key = MINOR(current->tty->
In scull_access_init() add

printk("<1> i am inside init");//after SET_MODULE_OWNER(&scull_priv_


printk("<1> i reguster\n");//last 3rd line

12 Modify scull_priv to allow each group id to have a single persistent set of data.

In acess.c
Replace all user by group;

Potrebbero piacerti anche