#include<linux/proc_fs.h> #include<linux/seq_file.h> static struct seq_operations scull_seq_ops ={
48 .start = scull_seq_start,
49 .next = scull_seq_next,
50 .stop = scull_seq_stop, /*It's NULL. */
51 .show = scull_seq_show
52 };
53
54 static struct file_operations scull_proc_ops = {
55 .owner = THIS_MODULE,
56 .open = scull_proc_open,
57 .read = seq_read,
58 .llseek = seq_lseek,
59 .release = seq_release
60 };
struct proc_dir_entry *entry;
在static int scull_init_module(void)函数中加入:
107entry = create_proc_entry("scullseq",0,NULL);
108 if(entry)
109 entry->proc_fops = &scull_proc_ops;
110 else
111 printk(KERN_ALERT "Can't creat a proc entry\n");
当然在void scull_cleanup_module(void)中要加入
133if(entry) 134 remove_proc_entry("scullseq", NULL);
int scull_read_procem(char *buf,char **start,off_t offset, int count,int *eof,void *data)
318 {
319 int i,j,len = 0;
320 int limit = count - 80;/*Don't print more than this */
321
322 for(i = 0; i < scull_nr_devs && len <= limit ;i ++){
323 struct scull_dev *d = &scull_devices[i];
324 struct scull_qset *qs = d->data;
325 if(down_interruptible(&d->sem))
326 return -ERESTARTSYS;
327 len += sprintf(buf+len,"\nDevice %i:qset %i , q %i,sz %li\n",i,d->qset,d->quantum,d->size);
328 for(; qs && len <= limit; qs = qs->next){ /*scan the list */
329 len += sprintf(buf + len," item at %p,qset at %p\n",qs,qs->data);
330 if(qs->data && !qs->next){
331 for(j = 0; j < d->qset ; j ++){
332 if(qs->data[j])
333 len += sprintf(buf +len ," %4i:%ip\n",j,qs->data[j]);
334 }
335 }
336 }
337 up(&scull_devices[i].sem);
338 }
339 *eof = 1;
340 return len;
341 }
342
343 static void *scull_seq_start(struct seq_file *s,loff_t *pos)
344 {
345 if(*pos >= scull_nr_devs)
346 return NULL; /*No moer to read */
347 return scull_devices + *pos;
348 }
static void *scull_seq_next(struct seq_file *s,void *v,loff_t *pos)
351 {
352 (*pos)++;
353 if(*pos >= scull_nr_devs)
354 return NULL;
355
356 return scull_devices + *pos;
357 }
358
359 static void *scull_seq_stop(struct seq_file *sfile,void *v)
360 {
361 return NULL;
362 }
363
364 static int scull_seq_show(struct seq_file *s,void *v)
365 {
366 struct scull_dev *dev = (struct scull_dev *)v;
367 struct scull_qset *d;
368 int i;
369
370 if(down_interruptible(&dev->sem))
371 return -ERESTARTSYS;
372 seq_printf(s,"\nDevice %i:qset %i, q %i ,sz %li\n",(int)(dev - scull_devices),dev->qset,dev->quantum,dev->size);
373 for(d = dev->data; d; d = d->next){
374 seq_printf(s," item at %p,qset at %p\n",d,d->data);
375 if(d->data && !d->next)/*dump only the last item */
376 for(i - 0;i < dev->qset ; i++){
377 if(d->data[i])
378 seq_printf(s," %4i:%8p\n",i,d->data[i]);
} 381 } 382 up(&dev->sem); 383 384 return 0; 385 } 386 387 static int scull_proc_open(struct inode *inode,struct file *file) 388 { 389 return seq_open(file,&scull_seq_ops); 390 }
|