صباح الخير
original link
مديول بسيط لو عندك kernel app وعايز تطلع information لل proc filesystem ال user ممكن يقرأها لو محتاج معلومات الي بدوره حيسهل عليك بدل ماتكتب واحد كامل تاني
مش في داعي لمقدمات عريضه لاني مش حشرح
كل مافي الامر اني استخدمت seq_file بدل من ال ordinary proc_read طبعا بناء علي ال comments
The legacy /proc
* interfaces cut loff_t down to off_t for reads
مش شرط اساسي بس احسن كتير لاني مستخدم linked list عشان اسهل عليك الاوت بوت
طبعا لو انت kernel app developer حتعرف تملي ا list بتاعتك بسطر واحد
لو في استفسارات اهلا
لو في افكار جديده اهلا جداااا
farewell
original link
مديول بسيط لو عندك kernel app وعايز تطلع information لل proc filesystem ال user ممكن يقرأها لو محتاج معلومات الي بدوره حيسهل عليك بدل ماتكتب واحد كامل تاني
مش في داعي لمقدمات عريضه لاني مش حشرح
كل مافي الامر اني استخدمت seq_file بدل من ال ordinary proc_read طبعا بناء علي ال comments
The legacy /proc
* interfaces cut loff_t down to off_t for reads
طبعا لو انت kernel app developer حتعرف تملي ا list بتاعتك بسطر واحد
كود PHP:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/stat.h>
#include <linux/stat.h>
#include <linux/slab.h>
#include <linux/seq_file.h>
#include<linux/sched.h>
#include <linux/errno.h>
/* itmes will be sequenced and sended to user interface using the lovely seq_file
* interface ; items will be initialized are initializing modules time
* any fauilr will result in whole module failure and unload immediately
*/
/* items hold data */
static struct items
{
struct items *next;
char *buffer;
}*head , *cur;
/* open sequence proto type function */
static int open_seq(struct inode *inode , struct file *filp);
/*iterator prototypes */
static void *start_seq(struct seq_file *s , loff_t *pos);
static void *next_seq(struct seq_file *s , void *data , loff_t *pos);
static void stop_seq(struct seq_file *s , void *data );
static int show_seq(struct seq_file *s , void *data);
/* Mark eof */
static int eof = 0;
/* erasing list function */
static void erase(struct items *head);
/* Seq operations */
static struct seq_operations seq_ops = {
.start = start_seq,
.next = next_seq,
.stop = stop_seq,
.show = show_seq
};
/* File operations */
struct file_operations fops =
{
.owner = THIS_MODULE,
.open = open_seq ,
.read = seq_read,
.release = seq_release,
.llseek = seq_lseek
};
/* Module initialization function definition */
static int __init proc_seq_init(void)
{
/* We need to allocate 5 items for items structure and fill buffers in them */
int i;
struct proc_dir_entry *proc_entry;
head = NULL;
cur = NULL;
for(i=0 ; i <= 5 ; i++)
{
if(head == NULL) /*first */
{
head = kmalloc(sizeof(struct items) , GFP_KERNEL);
head->buffer = kmalloc(50 *sizeof(char), GFP_KERNEL);
if( !head ||!head->buffer)
{
if(head)
{
erase(head);
printk(KERN_INFO"allocating memory error\n");
}
return -ENOMEM;
}
cur = head;
cur->next = NULL;
}else if(!cur->next) /* other */
{
cur->next = kmalloc(sizeof(struct items) , GFP_KERNEL);
cur = cur->next;
if(cur)
{
cur->buffer = kmalloc(50 *sizeof(char), GFP_KERNEL);
if(!cur->buffer)
{
erase(head); /* the only reason i did this cause
memory is not enough in this case
and i don't want somebody's system
to hang
*/
printk(KERN_INFO"allocating memory error\n");
return -ENOMEM;
}
cur->next = NULL;
}else
{
erase(head);
return -ENOMEM;
}
}
/* fill the buffer with information */
sprintf(cur->buffer , "Ok this is the %d time and your Uid is\n",i,current->uid);
}
/* initializing items is done at this case we should now register our proc_file */
/*using low level creat_proc_enrty cause we don't want to have a read only proc
* we are not implementing ordinary proc file we are using seq_file proc technique */
proc_entry = create_proc_entry("linked_info" , S_IRUSR | S_IROTH , NULL /*parent */);
if(proc_entry)
proc_entry->proc_fops = &fops;
else
return -1;
printk(KERN_INFO"All seems to be fine\n\n");
return 0;
}
static void __exit proc_seq_clean(void)
{
/* erase list */
erase(head);
/* remove file */
remove_proc_entry("linked_info" , NULL);
}
module_init(proc_seq_init);
module_exit(proc_seq_clean);
/* open_seq definition */
static int open_seq(struct inode *inode , struct file *filp)
{
/* all we want to do is register sequence operation with file */
eof = 0;
return seq_open(filp , &seq_ops);
}
/* iterator functions definitions */
static void *start_seq(struct seq_file *s , loff_t *pos)
{
/* actually it will do nothing accept updating postion */
*pos += sizeof(struct items) + 50;
if(head->next)
return head->next;
else
return NULL;
};
static void *next_seq(struct seq_file *s , void *data , loff_t *pos)
{
struct items *item;
/* update postion and move on sequence list */
item = (struct items*) data;
if(item->next)
{
return item->next;
}
else
return NULL;
}
static void stop_seq(struct seq_file *s , void *data )
{
;
}
static int show_seq(struct seq_file *s , void *data)
{
struct items *item;
int result;
item = (struct items *) data;
if(eof == 1)
{
return 0;
}
result = seq_printf( s , item->buffer );
if(item->next == NULL)
eof = 1;
return result;
}
/* erasing function definition */
static void erase(struct items *item)
{
struct items *temp;
if(!item)
return;
while(item)
{
temp=item->next;
kfree(item);
item = temp;
}
}
MODULE_LICENSE("Gpl");
MODULE_AUTHOR("Ahmed Mosatfa Programming-fr34ks.net Administrator");
MODULE_DESCRIPTION("/proc file system module display output ");
MODULE_VERSION("0.0.1");
لو في افكار جديده اهلا جداااا
farewell
تعليق